#include "cbf_tree.h"
Go to the source code of this file.
Functions | |
int | cbf_write_binary (cbf_node *column, unsigned int row, cbf_file *file, int isbuffer) |
Definition at line 141 of file cbf_write_binary.c.
References bits(), CBF_ARGUMENT, CBF_BYTE_OFFSET, CBF_CANONICAL, cbf_copy_file(), cbf_failnez, cbf_flush_bits(), cbf_flush_characters(), cbf_get_bintext(), cbf_get_fileposition(), cbf_is_base64digest(), cbf_is_binary(), cbf_is_mimebinary(), cbf_md5digest(), CBF_NONE, CBF_PACKED, CBF_PREDICTOR, cbf_put_character(), cbf_put_integer(), cbf_reset_bits(), cbf_set_bintext(), cbf_set_fileposition(), cbf_tobase64(), cbf_tobasex(), CBF_TOKEN_BIN, CBF_TOKEN_TMP_BIN, cbf_toqp(), cbf_write_string(), ENC_BASE10, ENC_BASE16, ENC_BASE64, ENC_BASE8, ENC_NONE, ENC_QP, MIME_HEADERS, MIME_NOHEADERS, MSG_DIGEST, cbf_node_struct::type, cbf_file::write_encoding, and cbf_file::write_headers.
Referenced by cbf_write_value().
{ cbf_file *infile; char digest [25], text [100]; long start; size_t size; unsigned int compression; int id, bits, sign, type, checked_digest, elsize; /* Check the arguments */ if (!file) return CBF_ARGUMENT; if (((file->write_encoding & ENC_QP) > 0) + ((file->write_encoding & ENC_BASE64) > 0) + ((file->write_encoding & ENC_BASE8) > 0) + ((file->write_encoding & ENC_BASE10) > 0) + ((file->write_encoding & ENC_BASE16) > 0) + ((file->write_encoding & ENC_NONE) > 0) != 1) return CBF_ARGUMENT; if (!cbf_is_binary (column, row)) return CBF_ARGUMENT; if (cbf_is_mimebinary (column, row)) return CBF_ARGUMENT; /* Parse the value */ cbf_failnez (cbf_get_bintext (column, row, &type, &id, &infile, &start, &size, &checked_digest, digest, &bits, &sign, &compression)) /* Position the file at the start of the binary section */ cbf_failnez (cbf_set_fileposition (infile, start, SEEK_SET)) /* Calculate the digest if necessary */ if (!cbf_is_base64digest (digest) && (file->write_headers & MSG_DIGEST)) { /* Discard any bits in the buffers */ cbf_failnez (cbf_reset_bits (infile)) /* Compute the message digest */ cbf_failnez (cbf_md5digest (infile, size, digest)) /* Go back to the start of the binary data */ cbf_failnez (cbf_set_fileposition (infile, start, SEEK_SET)) /* Update the entry */ checked_digest = 1; cbf_failnez (cbf_set_bintext (column, row, type, id, infile, start, size, checked_digest, digest, bits, sign, compression)) } /* Discard any bits in the buffers */ cbf_failnez (cbf_reset_bits (infile)) /* Do we need MIME headers? */ if (compression == CBF_NONE && (file->write_headers & MIME_NOHEADERS)) return CBF_ARGUMENT; /* Write the header */ cbf_failnez (cbf_write_string (file, "\n;\n")) /* MIME header? */ if (file->write_headers & MIME_HEADERS) { cbf_failnez (cbf_write_string (file, "--CIF-BINARY-FORMAT-SECTION--\n")) if (compression == CBF_NONE) cbf_failnez (cbf_write_string (file, "Content-Type: application/octet-stream\n")) else { cbf_failnez (cbf_write_string (file, "Content-Type: application/octet-stream;\n")) switch (compression) { case CBF_PACKED: cbf_failnez (cbf_write_string (file, " conversions=\"x-CBF_PACKED\"\n")) break; case CBF_CANONICAL: cbf_failnez (cbf_write_string (file, " conversions=\"x-CBF_CANONICAL\"\n")) break; case CBF_BYTE_OFFSET: cbf_failnez (cbf_write_string (file, " conversions=\"x-CBF_BYTE_OFFSET\"\n")) break; case CBF_PREDICTOR: cbf_failnez (cbf_write_string (file, " conversions=\"x-CBF_PREDICTOR\"\n")) break; default: cbf_failnez (cbf_write_string (file, " conversions=\"x-CBF_UNKNOWN\"\n")) } } if (file->write_encoding & ENC_QP) cbf_failnez (cbf_write_string (file, "Content-Transfer-Encoding: QUOTED-PRINTABLE\n")) else if (file->write_encoding & ENC_BASE64) cbf_failnez (cbf_write_string (file, "Content-Transfer-Encoding: BASE64\n")) else if (file->write_encoding & ENC_BASE8) cbf_failnez (cbf_write_string (file, "Content-Transfer-Encoding: X-BASE8\n")) else if (file->write_encoding & ENC_BASE10) cbf_failnez (cbf_write_string (file, "Content-Transfer-Encoding: X-BASE10\n")) else if (file->write_encoding & ENC_BASE16) cbf_failnez (cbf_write_string (file, "Content-Transfer-Encoding: X-BASE16\n")) else cbf_failnez (cbf_write_string (file, "Content-Transfer-Encoding: BINARY\n")) sprintf (text, "X-Binary-Size: %u\n", size); cbf_failnez (cbf_write_string (file, text)) sprintf (text, "X-Binary-ID: %d\n", id); cbf_failnez (cbf_write_string (file, text)) if (sign) sprintf (text, "X-Binary-Element-Type: \"signed %d-bit integer\"\n", bits); else sprintf (text, "X-Binary-Element-Type: \"unsigned %d-bit integer\"\n", bits); cbf_failnez (cbf_write_string (file, text)) /* Save the digest if we have one */ if (cbf_is_base64digest (digest)) { sprintf (text, "Content-MD5: %24s\n", digest); cbf_failnez (cbf_write_string (file, text)) } cbf_failnez (cbf_write_string (file, "\n")) } else /* Simple header */ cbf_failnez (cbf_write_string (file, "START OF BINARY SECTION\n")) /* Copy the binary section to the output file */ if (file->write_encoding & ENC_NONE) { /* Write the separators */ cbf_failnez (cbf_put_character (file, 12)) cbf_failnez (cbf_put_character (file, 26)) cbf_failnez (cbf_put_character (file, 4)) cbf_failnez (cbf_put_character (file, 213)) /* Flush any bits in the buffers */ cbf_failnez (cbf_flush_bits (file)) /* If no MIME header, write the necessary data here */ if ( !(file->write_headers & MIME_HEADERS) ) { /* Write the binary identifier (64 bits) */ cbf_failnez (cbf_put_integer (file, id, 1, 64)) /* Write the size of the binary section (64 bits) */ cbf_failnez (cbf_put_integer (file, size, 0, 64)) /* Write the compression type (64 bits) */ cbf_failnez (cbf_put_integer (file, compression, 0, 64)) } /* Get the current point in the new file */ cbf_failnez (cbf_get_fileposition (file, &start)) /* Copy the binary section to the output file */ cbf_failnez (cbf_copy_file (file, infile, size)) } else { /* Read the element size with no compression? */ if (compression == CBF_NONE) { elsize = (bits + 4) / 8; if (elsize < 1 || elsize == 5) elsize = 4; else if (elsize == 7) elsize = 6; else if (elsize > 8) elsize = 8; } else elsize = 4; /* Go back to the start of the binary data */ cbf_failnez (cbf_set_fileposition (infile, start, SEEK_SET)) /* Flush any bits in the buffers */ cbf_failnez (cbf_flush_bits (infile)) if (file->write_encoding & ENC_QP) cbf_failnez (cbf_toqp (infile, file, size)) else if (file->write_encoding & ENC_BASE64) cbf_failnez (cbf_tobase64 (infile, file, size)) else if (file->write_encoding & ENC_BASE8) cbf_failnez (cbf_tobasex (infile, file, size, elsize, 8)) else if (file->write_encoding & ENC_BASE10) cbf_failnez (cbf_tobasex (infile, file, size, elsize, 10)) else cbf_failnez (cbf_tobasex (infile, file, size, elsize, 16)) } /* Write the MIME footer */ if (file->write_headers & MIME_HEADERS) cbf_failnez (cbf_write_string (file, "\n--CIF-BINARY-FORMAT-SECTION----\n;\n")) else cbf_failnez (cbf_write_string (file, "\nEND OF BINARY SECTION\n;\n")) /* Flush the buffer */ cbf_failnez (cbf_flush_characters (file)) /* Replace a connection to a temporary file? */ if (start != 0 && isbuffer && type == CBF_TOKEN_TMP_BIN && (file->write_encoding & ENC_NONE)) cbf_failnez (cbf_set_bintext (column, row, CBF_TOKEN_BIN, id, file, start, size, checked_digest, digest, bits, sign, compression)) /* Success */ return 0; }