Functions

cbf_binary.c File Reference

#include "cbf.h"
#include "cbf_tree.h"
#include "cbf_codes.h"
#include "cbf_compress.h"
#include "cbf_context.h"
#include "cbf_binary.h"
#include "cbf_read_mime.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
Include dependency graph for cbf_binary.c:

Go to the source code of this file.

Functions

int cbf_get_bintext (cbf_node *column, unsigned int row, int *type, int *id, cbf_file **file, long *start, size_t *size, int *checked_digest, char *digest, int *bits, int *sign, unsigned int *compression)
int cbf_set_bintext (cbf_node *column, unsigned int row, int type, int id, cbf_file *file, long start, long size, int checked_digest, const char *digest, int bits, int sign, unsigned int compression)
int cbf_is_binary (cbf_node *column, unsigned int row)
int cbf_is_mimebinary (cbf_node *column, unsigned int row)
int cbf_free_value (cbf_context *context, cbf_node *column, unsigned int row)
int cbf_set_binary (cbf_node *column, unsigned int row, unsigned int compression, int binary_id, void *value, size_t elsize, int elsign, size_t nelem)
int cbf_check_digest (cbf_node *column, unsigned int row)
int cbf_binary_parameters (cbf_node *column, unsigned int row, unsigned int *compression, int *id, int *eltype, size_t *elsize, int *elsigned, int *elunsigned, size_t *nelem, int *minelem, int *maxelem)
int cbf_get_binary (cbf_node *column, unsigned int row, int *id, void *value, size_t elsize, int elsign, size_t nelem, size_t *nelem_read)

Function Documentation

int cbf_binary_parameters ( cbf_node column,
unsigned int  row,
unsigned int *  compression,
int *  id,
int *  eltype,
size_t *  elsize,
int *  elsigned,
int *  elunsigned,
size_t *  nelem,
int *  minelem,
int *  maxelem 
)

Definition at line 569 of file cbf_binary.c.

References cbf_binary_parameters(), cbf_check_digest(), cbf_decompress_parameters(), cbf_failnez, cbf_get_bintext(), cbf_is_mimebinary(), cbf_mime_temp(), and cbf_set_fileposition().

Referenced by cbf_binary_parameters(), and cbf_get_integerarrayparameters().

{
  cbf_file *file;

  long start;

  size_t size, file_elsize, file_nelem;

  int text_bits, errorcode;
  
  
    /* Check the digest (this will also decode it if necessary) */

  cbf_failnez (cbf_check_digest (column, row))
  

    /* Is it an encoded binary section? */
    
  if (cbf_is_mimebinary (column, row))
  {

      /* Convert the value to a normal binary value */
      
    cbf_failnez (cbf_mime_temp (column, row))
    
    
      /* Rerun the function */

    return cbf_binary_parameters (column, row, 
                                  compression,
                                  id, 
                                  eltype, elsize, 
                                  elsigned, elunsigned,
                                  nelem,
                                  minelem, maxelem);
  }


    /* Parse the value */

  cbf_failnez (cbf_get_bintext (column, row, NULL,
                                id, &file, &start, &size, NULL,
                                NULL, &text_bits, NULL, compression))


    /* Position the file at the start of the binary section */
    
  cbf_failnez (cbf_set_fileposition (file, start, SEEK_SET))

  
    /* Get the parameters */

  errorcode = cbf_decompress_parameters (eltype, &file_elsize, elsigned, 
                                         elunsigned, 
                                         &file_nelem, minelem, maxelem,
                                         *compression, file);

  if (!errorcode)
  {
    if (elsize)
  
      if (file_elsize > 0)
  
        *elsize = file_elsize;
        
      else

        *elsize = (text_bits + CHAR_BIT - 1) / CHAR_BIT;    
        
    if (nelem)
    
      if (file_nelem > 0)
      
        *nelem = file_nelem;
        
      else
      
        *nelem = (size * 8) / text_bits;
  }

  return errorcode;
}
int cbf_check_digest ( cbf_node column,
unsigned int  row 
)

Definition at line 497 of file cbf_binary.c.

References bits(), cbf_failnez, CBF_FORMAT, cbf_get_bintext(), cbf_is_base64digest(), cbf_is_mimebinary(), cbf_md5digest(), cbf_mime_temp(), cbf_set_bintext(), cbf_set_fileposition(), MSG_DIGEST, and cbf_file::read_headers.

Referenced by cbf_binary_parameters(), and cbf_get_binary().

{
  cbf_file *file;

  long start;
  
  size_t size;

  char old_digest [25], new_digest [25];

  int id, bits, sign, type, checked_digest;

  unsigned int compression;


    /* Parse the value */
    
  cbf_failnez (cbf_get_bintext (column, row, &type, &id, &file, 
                                &start, &size, &checked_digest, 
                                old_digest, &bits, &sign, &compression))


    /* Recalculate and compare the digest? */

  if ((file->read_headers & MSG_DIGEST) && !checked_digest)
  
    if (cbf_is_base64digest (old_digest))
    {
        /* Is it encoded? */
    
      if (cbf_is_mimebinary (column, row))
      {
          /* Convert the value to a normal binary value */
      
        cbf_failnez (cbf_mime_temp (column, row))
    
    
          /* Rerun the function */

        return cbf_check_digest (column, row);
      }


        /* Position the file */

      cbf_failnez (cbf_set_fileposition (file, start, SEEK_SET))

        /* Recalculate and check the digest */

      cbf_failnez (cbf_md5digest (file, size, new_digest))

      if (strcmp (old_digest, new_digest) != 0)
                 
        return CBF_FORMAT;
      
      
        /* Change the text to show that the digest has been checked */
      
      cbf_failnez (cbf_set_bintext (column, row, type,
                                    id, file, start, size,
                                    1, new_digest, bits, sign, compression))
    }
  
  
    /* Success */
    
  return 0;
}
int cbf_free_value ( cbf_context context,
cbf_node column,
unsigned int  row 
)

Definition at line 371 of file cbf_binary.c.

References CBF_ARGUMENT, cbf_close_temporary(), cbf_delete_fileconnection(), cbf_failnez, cbf_free_string(), cbf_get_bintext(), cbf_get_columnrow(), cbf_is_binary(), cbf_set_columnrow(), and CBF_TOKEN_TMP_BIN.

Referenced by cbf_set_columnrow().

{
  cbf_file *file;

  const char *text;

  int is_binary, type;


    /* Check the argument */

  if (!column)

    return CBF_ARGUMENT;
    
    
    /* Is the value binary? */
    
  is_binary = cbf_is_binary (column, row);
  

    /* Parse the (binary) value */
    
  if (is_binary)
      
    cbf_failnez (cbf_get_bintext (column, row, &type, NULL, &file, NULL, 
                                           NULL, NULL, NULL, NULL, NULL, NULL))

    
    /* Get the ASCII value */

  cbf_failnez (cbf_get_columnrow (&text, column, row))
  

    /* Set the value to null */
      
  cbf_failnez (cbf_set_columnrow (column, row, NULL, 0))
    

    /* And free it */

  cbf_free_string (NULL, text);

  if (is_binary)

    if (type == CBF_TOKEN_TMP_BIN)

      cbf_failnez (cbf_close_temporary (context, &file))
      
    else

      cbf_failnez (cbf_delete_fileconnection (&file))
  
  
    /* Success */
    
  return 0;
}
int cbf_get_binary ( cbf_node column,
unsigned int  row,
int *  id,
void *  value,
size_t  elsize,
int  elsign,
size_t  nelem,
size_t *  nelem_read 
)

Definition at line 662 of file cbf_binary.c.

References bits(), cbf_check_digest(), cbf_decompress(), cbf_decompress_parameters(), cbf_failnez, cbf_get_binary(), cbf_get_bintext(), cbf_is_mimebinary(), cbf_mime_temp(), and cbf_set_fileposition().

Referenced by cbf_get_binary(), and cbf_get_integerarray().

{
  cbf_file *file;

  long start;

  int eltype_file, elsigned_file, elunsigned_file, 
                   minelem_file, maxelem_file, bits, sign;

  unsigned int compression;

  size_t nelem_file;


    /* Check the digest (this will also decode it if necessary) */

  cbf_failnez (cbf_check_digest (column, row))
  

    /* Is it an encoded binary section? */
    
  if (cbf_is_mimebinary (column, row))
  {
      /* Convert the value to a normal binary value */
      
    cbf_failnez (cbf_mime_temp (column, row))
    
    
      /* Rerun the function */

    return cbf_get_binary (column, row, 
                           id, value, elsize, elsign,
                           nelem, nelem_read);
  }


    /* Parse the value */

  cbf_failnez (cbf_get_bintext (column, row, NULL,
                                id, &file, &start, NULL, 
                                 NULL, NULL, &bits, &sign, &compression))
  

    /* Position the file at the start of the binary section */

  cbf_failnez (cbf_set_fileposition (file, start, SEEK_SET))

  
    
    /* Get the parameters and position the file */

  cbf_failnez (cbf_decompress_parameters (&eltype_file, NULL,
                                          &elsigned_file, &elunsigned_file,
                                          &nelem_file,
                                          &minelem_file, &maxelem_file,
                                          compression, 
                                          file))


    /* Decompress the binary data */

  return cbf_decompress (value, elsize, elsign, nelem, nelem_read,
                         compression, bits, sign, file);
}
int cbf_get_bintext ( cbf_node column,
unsigned int  row,
int *  type,
int *  id,
cbf_file **  file,
long *  start,
size_t *  size,
int *  checked_digest,
char *  digest,
int *  bits,
int *  sign,
unsigned int *  compression 
)

Definition at line 140 of file cbf_binary.c.

References CBF_ASCII, cbf_failnez, cbf_get_columnrow(), and cbf_is_binary().

Referenced by cbf_binary_parameters(), cbf_check_digest(), cbf_free_value(), cbf_get_binary(), cbf_mime_temp(), and cbf_write_binary().

{
  cbf_file *file_text;

  long start_text, size_text;

  int id_text, type_text, checked_digest_text, bits_text, sign_text;

  unsigned int compression_text;
  
  char digest_text [25];

  const char *text;


    /* Check that the value is binary */

  if (!cbf_is_binary (column, row))

    return CBF_ASCII;


    /* Get the value */

  cbf_failnez (cbf_get_columnrow (&text, column, row))
    
    
    /* Parse it */
    
  type_text = *text;

  sscanf (text + 1, " %x %p %lx %lx %d %24s %x %d %u", 
                      &id_text, 
                      &file_text, 
                      &start_text, 
                      &size_text, 
                      &checked_digest_text, 
                       digest_text, 
                      &bits_text, 
                      &sign_text,
                      &compression_text);


    /* Copy the values */
    
  if (type)
  
    *type = type_text;
    
  if (id)
  
    *id = id_text;
    
  if (file)
  
    *file = file_text;
    
  if (start)
  
    *start = start_text;
   
  if (size)
 
    *size = size_text;
   
  if (checked_digest)
  
    *checked_digest = checked_digest_text;
    
  if (digest)
  
    strcpy (digest, digest_text);
    
  if (bits)
  
    *bits = bits_text;
    
  if (sign)
  
    *sign = sign_text;

  if (compression)

    *compression = compression_text;
    
    
    /* Success */
    
  return 0;
}  
int cbf_is_binary ( cbf_node column,
unsigned int  row 
)

Definition at line 321 of file cbf_binary.c.

References cbf_get_columnrow(), CBF_TOKEN_BIN, CBF_TOKEN_MIME_BIN, and CBF_TOKEN_TMP_BIN.

Referenced by cbf_find_nextrow(), cbf_free_value(), cbf_get_bintext(), cbf_get_integerarrayparameters(), cbf_get_value(), and cbf_write_binary().

{
  const char *text;
  
  
    /* Get the value */

  if (cbf_get_columnrow (&text, column, row))
  
    return 0;
  
  if (text)

    return (*text == CBF_TOKEN_BIN     || 
            *text == CBF_TOKEN_TMP_BIN || 
            *text == CBF_TOKEN_MIME_BIN);
      
      
    /* Fail */

  return 0;
}
int cbf_is_mimebinary ( cbf_node column,
unsigned int  row 
)

Definition at line 347 of file cbf_binary.c.

References cbf_get_columnrow(), and CBF_TOKEN_MIME_BIN.

Referenced by cbf_binary_parameters(), cbf_check_digest(), cbf_get_binary(), cbf_mime_temp(), and cbf_write_binary().

{
  const char *text;
  
  
    /* Get the value */

  if (cbf_get_columnrow (&text, column, row))
  
   return 0;
  
  if (text)

    return (*text == CBF_TOKEN_MIME_BIN);
        
      
    /* Fail */

  return 0;
}
int cbf_set_binary ( cbf_node column,
unsigned int  row,
unsigned int  compression,
int  binary_id,
void *  value,
size_t  elsize,
int  elsign,
size_t  nelem 
)

Definition at line 433 of file cbf_binary.c.

References bits(), cbf_compress(), cbf_delete_fileconnection(), cbf_failnez, CBF_FILESEEK, CBF_FILETELL, cbf_get_fileposition(), cbf_onfailnez, cbf_open_temporary(), cbf_set_bintext(), cbf_set_columnrow(), cbf_set_fileposition(), CBF_TOKEN_TMP_BIN, and cbf_node_struct::context.

Referenced by cbf_set_integerarray().

{
  cbf_file *tempfile;

  char digest [25];
  
  size_t size;
  
  long start;
  
  int bits;


    /* Remove the old value */

  cbf_failnez (cbf_set_columnrow (column, row, NULL, 1))


    /* Get the temporary file */

  cbf_failnez (cbf_open_temporary (column->context, &tempfile))


    /* Move to the end of the temporary file */

  if (cbf_set_fileposition (tempfile, 0, SEEK_END))

    return CBF_FILESEEK | cbf_delete_fileconnection (&tempfile);


    /* Get the starting location */

  if (cbf_get_fileposition (tempfile, &start))

    return CBF_FILETELL | cbf_delete_fileconnection (&tempfile);
    

    /* Add the binary data to the temporary file */

  cbf_onfailnez (cbf_compress (value, elsize, elsign, nelem,
                               compression, tempfile,
                               &size, &bits, digest),
                 cbf_delete_fileconnection (&tempfile))


    /* Set the value */
    
  cbf_onfailnez (cbf_set_bintext (column, row, CBF_TOKEN_TMP_BIN,
                                  binary_id, tempfile, start, size,
                                  1, digest, bits, elsign != 0, compression),
                 cbf_delete_fileconnection (&tempfile))


    /* Success */

  return 0;
}
int cbf_set_bintext ( cbf_node column,
unsigned int  row,
int  type,
int  id,
cbf_file file,
long  start,
long  size,
int  checked_digest,
const char *  digest,
int  bits,
int  sign,
unsigned int  compression 
)

Definition at line 244 of file cbf_binary.c.

References cbf_add_fileconnection(), CBF_ALLOC, cbf_copy_string(), cbf_delete_fileconnection(), cbf_free_string(), cbf_is_base64digest(), cbf_onfailnez, and cbf_set_columnrow().

Referenced by cbf_check_digest(), cbf_mime_temp(), cbf_set_binary(), and cbf_write_binary().

{
  char text [(((sizeof (void *) +
                sizeof (long int) * 2 +
                sizeof (int) * 3) * CHAR_BIT) >> 2) + 55];
                
  const char *new_text;

  int errorcode;
  

    /* Check that the digest has the correct format */
    
  if (!cbf_is_base64digest (digest))
  {
    digest = "------------------------";
    
    checked_digest = 0;
  }


    /* Create the new text */                

  sprintf (text, "%x %p %lx %lx %1d %24s %x %d %u", 
                   id, 
                   file, 
                   start, 
                   size, 
                   checked_digest != 0,
                   digest, 
                   bits, 
                   sign,
                   compression);
    
  new_text = cbf_copy_string (NULL, text, type);

  if (!new_text)
    
    return CBF_ALLOC;


    /* Add a new connection to the file */

  cbf_onfailnez (cbf_add_fileconnection (&file, NULL),
                 cbf_free_string (NULL, new_text))


    /* Set the new value */

  errorcode = cbf_set_columnrow (column, row, new_text, 1);

  if (errorcode)
  {
    cbf_free_string (NULL, new_text);
    
    return errorcode | cbf_delete_fileconnection (&file);
  }

  
    /* Success */
    
  return 0;
}