Functions

cbf_file.c File Reference

#include "cbf.h"
#include "cbf_alloc.h"
#include "cbf_codes.h"
#include "cbf_file.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
Include dependency graph for cbf_file.c:

Go to the source code of this file.

Functions

int cbf_make_file (cbf_file **file, FILE *stream)
int cbf_free_file (cbf_file **file)
int cbf_add_fileconnection (cbf_file **file, FILE *stream)
int cbf_delete_fileconnection (cbf_file **file)
int cbf_file_connections (cbf_file *file)
int cbf_set_buffersize (cbf_file *file, size_t size)
int cbf_reset_buffer (cbf_file *file)
int cbf_save_character (cbf_file *file, int c)
int cbf_get_buffer (cbf_file *file, const char **buffer, size_t *buffer_size)
int cbf_get_filecoordinates (cbf_file *file, unsigned int *line, unsigned int *column)
int cbf_set_filecoordinates (cbf_file *file, unsigned int line, unsigned int column)
int cbf_get_bit (cbf_file *file)
int cbf_get_bits (cbf_file *file, int *bitslist, int bitcount)
int cbf_put_bits (cbf_file *file, int *bitslist, int bitcount)
int cbf_get_integer (cbf_file *file, int *val, int valsign, int bitcount)
int cbf_put_integer (cbf_file *file, int val, int valsign, int bitcount)
int cbf_start_digest (cbf_file *file)
int cbf_end_digest (cbf_file *file, char *digest)
int cbf_flush_bits (cbf_file *file)
int cbf_flush_characters (cbf_file *file)
int cbf_reset_bits (cbf_file *file)
int cbf_reset_characters (cbf_file *file)
int cbf_get_character (cbf_file *file)
int cbf_read_character (cbf_file *file)
int cbf_put_character (cbf_file *file, int c)
int cbf_write_character (cbf_file *file, int c)
int cbf_put_string (cbf_file *file, const char *string)
int cbf_write_string (cbf_file *file, const char *string)
int cbf_read_line (cbf_file *file, const char **line)
int cbf_get_block (cbf_file *file, size_t nelem)
int cbf_put_block (cbf_file *file, size_t nelem)
int cbf_copy_file (cbf_file *destination, cbf_file *source, size_t nelem)
int cbf_get_fileposition (cbf_file *file, long int *position)
int cbf_set_fileposition (cbf_file *file, long int position, int whence)

Function Documentation

int cbf_add_fileconnection ( cbf_file **  file,
FILE *  stream 
)

Definition at line 207 of file cbf_file.c.

References CBF_ARGUMENT, cbf_make_file(), and CBF_NOTFOUND.

Referenced by cbf_lex(), cbf_open_temporary(), and cbf_set_bintext().

{
    /* Does the file pointer exist? */

  if (!file)

    return CBF_ARGUMENT;


    /* Does the file exist? */

  if (*file)

      /* Does the stream match? */

    if (stream && (*file)->stream != stream)

      return CBF_NOTFOUND;

    else
    {
      (*file)->connections++;

      return 0;
    }


    /* Create a new file */

  return cbf_make_file (file, stream);
}
int cbf_copy_file ( cbf_file destination,
cbf_file source,
size_t  nelem 
)

Definition at line 1397 of file cbf_file.c.

References cbf_file::buffer, CBF_ARGUMENT, cbf_failnez, CBF_FILEWRITE, cbf_flush_characters(), cbf_get_block(), cbf_file::digest, MD5Update(), and cbf_file::stream.

Referenced by cbf_write_binary().

{
  size_t done, todo;


    /* Do the files exist? */
    
  if (!destination || !source)

    return CBF_ARGUMENT;

  if (!destination->stream || !source->stream)

    return CBF_ARGUMENT;


    /* Flush the buffers */
    
  cbf_failnez (cbf_flush_characters (destination))
  

    /* Copy the characters in blocks of up to 1024 */
    
  while (nelem > 0)
  {
    if (nelem >= 1024)

      todo = 1024;

    else

      todo = nelem;

    cbf_failnez (cbf_get_block (source, todo))

    done = fwrite (source->buffer, 1, todo, destination->stream);
    
    
      /* Update the message digest */
      
    if (done > 0 && destination->digest)

      MD5Update (destination->digest, source->buffer, done);
      
      
      /* Fail? */

    if (done < todo)

      return CBF_FILEWRITE;

    nelem -= done;
  }


    /* Success */

  return 0;
}
int cbf_delete_fileconnection ( cbf_file **  file)

Definition at line 242 of file cbf_file.c.

References CBF_ARGUMENT, and cbf_free_file().

Referenced by cbf_close_temporary(), cbf_free_value(), cbf_lex(), cbf_mime_temp(), cbf_read_file(), cbf_set_binary(), cbf_set_bintext(), and cbf_write_file().

{
    /* Does the file pointer exist? */

  if (!file)

    return CBF_ARGUMENT;


    /* Does the file exist? */

  if (!*file)

    return CBF_ARGUMENT;


    /* Remove a connection */

  (*file)->connections--;


    /* Delete the file? */

  if ((*file)->connections == 0)

    return cbf_free_file (file);


    /* Success */

  return 0;
}
int cbf_end_digest ( cbf_file file,
char *  digest 
)

Definition at line 877 of file cbf_file.c.

References CBF_ARGUMENT, cbf_failnez, cbf_flush_characters(), cbf_free(), cbf_md5digest_to64(), cbf_file::digest, and MD5Final().

Referenced by cbf_compress().

{
  unsigned char raw_digest [16];
  
  if (!file || !digest)

    return CBF_ARGUMENT;
    
  if (!file->digest)
  
    return CBF_ARGUMENT;
  
    
    /* Flush the buffers */

  cbf_failnez (cbf_flush_characters (file))
  

    /* Get the raw digest */
    
  MD5Final (raw_digest, file->digest);
  
  
    /* Free the md5 context */
    
  cbf_failnez (cbf_free ((void **) &file->digest, NULL))
  
  
    /* Encode the digest in base-64 */
    
  cbf_md5digest_to64 (digest, raw_digest);

  
    /* Success */
    
  return 0;
}
int cbf_file_connections ( cbf_file file)

Definition at line 278 of file cbf_file.c.

References cbf_file::connections.

Referenced by cbf_close_temporary().

{
  if (!file)

    return 0;

  return file->connections;
}
int cbf_flush_bits ( cbf_file file)

Definition at line 918 of file cbf_file.c.

References cbf_file::bits, CBF_ARGUMENT, cbf_failnez, cbf_flush_characters(), and cbf_put_integer().

Referenced by cbf_compress(), cbf_read_mime(), and cbf_write_binary().

{
  if (!file)

    return CBF_ARGUMENT;


    /* Flush any partial bytes into the character buffer */
    
  cbf_failnez (cbf_put_integer (file, 0, 0, 7))
  
  
    /* Reset the bit buffers */
    
  file->bits [0] = 0;
  file->bits [1] = 0;


    /* Write the characters */

  return cbf_flush_characters (file);
}
int cbf_flush_characters ( cbf_file file)

Definition at line 944 of file cbf_file.c.

References CBF_ARGUMENT, CBF_FILEWRITE, cbf_file::characters, cbf_file::characters_used, cbf_file::digest, MD5Update(), and cbf_file::stream.

Referenced by cbf_copy_file(), cbf_end_digest(), cbf_flush_bits(), cbf_frombase64(), cbf_frombasex(), cbf_fromqp(), cbf_put_bits(), cbf_put_block(), cbf_put_character(), cbf_start_digest(), cbf_tobase64(), cbf_tobasex(), cbf_toqp(), cbf_write_ascii(), cbf_write_binary(), and cbf_write_node().

{
  int done;
  
  if (!file)

    return CBF_ARGUMENT;


    /* Write the characters */
    
  if (file->characters_used == 0)
  
    return 0;

  done = fwrite (file->characters, 1, file->characters_used, file->stream);


    /* Update the message digest */

  if (done > 0 && file->digest) 

    MD5Update (file->digest, file->characters, done);


    /* Make sure the file is really updated */

  if (done > 0)
  
    fflush (file->stream);

    
    /* Remove the characters written */

  if (done < file->characters_used)
  {
    if (done > 0)
    {
      memmove (file->characters, file->characters + done, 64 - done);
        
      file->characters_used = 64 - done;
    }

    return CBF_FILEWRITE;
  }

  file->characters_used = 0;


    /* Success */

  return 0;
}
int cbf_free_file ( cbf_file **  file)

Definition at line 174 of file cbf_file.c.

References CBF_FILECLOSE, and cbf_free().

Referenced by cbf_close_temporary(), cbf_delete_fileconnection(), and cbf_free_context().

{
  int errorcode;

  errorcode = 0;

  if (file)

    if (*file)
    {
      if ((*file)->stream)

        if (fclose ((*file)->stream))

          errorcode = CBF_FILECLOSE;

      errorcode |= cbf_free ((void **) &(*file)->buffer, 
                                       &(*file)->buffer_size);

      errorcode |= cbf_free ((void **) &(*file)->digest, NULL);
      
      errorcode |= cbf_free ((void **) file, NULL);
    }


    /* Success? */

  return errorcode;
}
int cbf_get_bit ( cbf_file file)

Definition at line 487 of file cbf_file.c.

References bit, cbf_file::bits, and cbf_file::stream.

{
  int bit;
  
  if (file->bits [0] == 0)
  {
    file->bits [1] = getc (file->stream);

    if (file->bits [1] == EOF)

      return EOF;

    file->bits [0] = 8;
  }
  
  bit = file->bits [1] & 1;

  file->bits [1] >>= 1;
  
  file->bits [0]--;


    /* Success */

  return bit;
}
int cbf_get_bits ( cbf_file file,
int *  bitslist,
int  bitcount 
)

Definition at line 517 of file cbf_file.c.

References cbf_compress_nodestruct::bitcode, cbf_compress_nodestruct::bitcount, cbf_file::bits, cbf_failnez, CBF_FILEREAD, cbf_get_bits(), cbf_compress_nodestruct::count, and cbf_file::stream.

Referenced by cbf_decompress_packed(), cbf_get_bits(), cbf_get_code(), and cbf_get_integer().

{
  int bitcode, count, m, maxbits;


    /* Number of bits in an integer */

  maxbits = sizeof (int) * CHAR_BIT;


    /* Read the bits in int-sized blocks */

  while (bitcount > maxbits)
  {
    cbf_failnez (cbf_get_bits (file, bitslist, maxbits))

    bitslist++;

    bitcount -= maxbits;
  }


    /* Read the bits into an int */
    
  count = file->bits [0];

  bitcode = file->bits [1] & 0x0ff;

  while (count < bitcount)
  {
    file->bits [1] = getc (file->stream);

    if (file->bits [1] == EOF)

      return CBF_FILEREAD;

    file->bits [0] = 8;

    bitcode |= (file->bits [1] << count) & -(1 << count);

    count += 8;
  }

  file->bits [1] = (file->bits [1] >> (file->bits [0] - (count - bitcount)));

  file->bits [0] = count - bitcount;


    /* Sign-extend */

  m = 1 << (bitcount - 1);

  if (bitcode & m)

    *bitslist = bitcode | -m;

  else

    *bitslist = bitcode & ~-m;


    /* Success */

  return 0;
}
int cbf_get_block ( cbf_file file,
size_t  nelem 
)

Definition at line 1292 of file cbf_file.c.

References cbf_file::buffer, cbf_file::buffer_used, CBF_ARGUMENT, cbf_failnez, CBF_FILEREAD, cbf_set_buffersize(), and cbf_file::stream.

Referenced by cbf_copy_file(), and cbf_md5digest().

{
  size_t done;

  
    /* Does the file exist? */
    
  if (!file)

    return CBF_ARGUMENT;


    /* Set the buffer size */

  cbf_failnez (cbf_set_buffersize (file, nelem))


    /* Read the characters */

  file->buffer_used = 0;
  
  while (file->buffer_used < nelem)
  {
    if (file->stream)
    
      done = fread (file->buffer + file->buffer_used, 1,
                           nelem - file->buffer_used, file->stream);

    else

      done = 0;

    if (done <= 0)

      return CBF_FILEREAD;

    file->buffer_used += done;
  }


    /* Success */

  return 0;
}
int cbf_get_buffer ( cbf_file file,
const char **  buffer,
size_t *  buffer_size 
)

Definition at line 397 of file cbf_file.c.

References cbf_file::buffer, cbf_file::buffer_used, and CBF_ARGUMENT.

Referenced by cbf_lex(), and cbf_md5digest().

{
    /* Does the file exist? */

  if (!file)

    return CBF_ARGUMENT;
    
    
    /* Copy the buffer */
    
  if (buffer)
  
    if (file->buffer_used <= 0)
    
      *buffer = NULL;
      
    else
  
      *buffer = file->buffer;
    
    
  if (buffer_size)
    
    *buffer_size = file->buffer_used;
    

    /* Success */

  return 0;
}
int cbf_get_character ( cbf_file file)

Definition at line 1033 of file cbf_file.c.

References cbf_file::last_read, and cbf_file::stream.

Referenced by cbf_parse_binaryheader(), cbf_read_character(), cbf_tobase64(), cbf_tobasex(), and cbf_toqp().

{
  if (file->stream)

    file->last_read = fgetc (file->stream);

  else

    file->last_read = EOF;

  return file->last_read;
}
int cbf_get_filecoordinates ( cbf_file file,
unsigned int *  line,
unsigned int *  column 
)

Definition at line 433 of file cbf_file.c.

References CBF_ARGUMENT, cbf_file::column, and cbf_file::line.

Referenced by cbf_lex(), cbf_write_ascii(), and cbf_write_category().

{
    /* Does the file exist? */

  if (!file)

    return CBF_ARGUMENT;


    /* Read the coordinates */

  if (line)

    *line = file->line;
    
  if (column)

    *column = file->column;
    

    /* Success */

  return 0;
}
int cbf_get_fileposition ( cbf_file file,
long int *  position 
)

Definition at line 1460 of file cbf_file.c.

References CBF_ARGUMENT, CBF_FILETELL, and cbf_file::stream.

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

{
  long int file_position;
  
  
    /* Does the file exist? */
    
  if (!file)

    return CBF_ARGUMENT;
    
  if (!file->stream)
  
    return CBF_ARGUMENT;
    
    
    /* Get the position */
    
  file_position = ftell (file->stream);
  
  if (file_position == -1L)
  
    return CBF_FILETELL;
    
  if (position)
  
    *position = file_position;
    
    
    /* Success */
    
  return 0;
}
int cbf_get_integer ( cbf_file file,
int *  val,
int  valsign,
int  bitcount 
)

Definition at line 689 of file cbf_file.c.

References cbf_compress_nodestruct::bitcount, cbf_failnez, cbf_get_bits(), and CBF_OVERFLOW.

Referenced by cbf_decompress_canonical(), cbf_decompress_none(), cbf_decompress_packed(), cbf_decompress_parameters(), cbf_get_table(), and cbf_parse_binaryheader().

{
  int maxbits, signbits, valbits, sign, errorcode, deval;


    /* Make sure there is a destination */

  if (!val)
  
    val = &deval;
    

    /* Any bits to read? */

  if (bitcount <= 0)
  {
    *val = 0;

    return 0;
  }


    /* Number of bits in an integer */

  maxbits = sizeof (int) * CHAR_BIT;


    /* Number of bits in the value and sign parts */

  signbits = bitcount - sizeof (signed long) * CHAR_BIT;

  if (signbits > 0)

    valbits = bitcount - signbits;

  else

    valbits = bitcount;


    /* Read the value */

  cbf_failnez (cbf_get_bits (file, val, valbits))


    /* Fix the sign */

  if (valbits < maxbits && valsign == 0)

    *val &= ~-(1 << valbits);


    /* Read the sign bits */

  errorcode = 0;

  while (signbits > 0)
  {
    if (signbits < maxbits)

      cbf_failnez (cbf_get_bits (file, &sign, signbits))

    else

      cbf_failnez (cbf_get_bits (file, &sign, maxbits))

    signbits -= maxbits;


      /* Overflow? */

    if (sign != -(*val < 0 && valsign))
    {
      errorcode = CBF_OVERFLOW;

      if (valsign)

        *val = -(sign >= 0) ^ (1 << (maxbits - 1));

      else

        *val = -1;
    }
  }

  return errorcode;
}
int cbf_make_file ( cbf_file **  file,
FILE *  stream 
)

Definition at line 136 of file cbf_file.c.

References cbf_alloc(), and cbf_failnez.

Referenced by cbf_add_fileconnection(), cbf_open_temporary(), cbf_read_file(), and cbf_write_file().

{
    /* Allocate the memory */

  cbf_failnez (cbf_alloc ((void **) file, NULL, sizeof (cbf_file), 1))


    /* Initialise */

  (*file)->stream = stream;

  (*file)->connections = 1;

  (*file)->bits [0]        = 0;
  (*file)->bits [1]        = 0;
  (*file)->characters_used = 0;
  (*file)->last_read       = 0;
  (*file)->line            = 0;
  (*file)->column          = 0;
  (*file)->buffer_size     = 0;
  (*file)->buffer_used     = 0;

  (*file)->buffer          = NULL;
  (*file)->digest          = NULL;

  (*file)->read_headers    = 0;
  (*file)->write_headers   = 0;
  (*file)->write_encoding  = 0;


    /* Success */

  return 0;
}
int cbf_put_bits ( cbf_file file,
int *  bitslist,
int  bitcount 
)

Definition at line 586 of file cbf_file.c.

References cbf_compress_nodestruct::bitcount, cbf_file::bits, cbf_failnez, cbf_flush_characters(), cbf_put_bits(), cbf_file::characters, and cbf_file::characters_used.

Referenced by cbf_pack_chunk(), cbf_put_bits(), cbf_put_code(), cbf_put_integer(), and cbf_put_stopcode().

{
  int resultcode, maxbits, bits0, bits1;


    /* Number of bits in an integer */

  maxbits = sizeof (int) * CHAR_BIT;


    /* Write the bits in int-sized blocks */

  while (bitcount > maxbits)
  {
    cbf_failnez (cbf_put_bits (file, bitslist, maxbits))

    bitslist++;

    bitcount -= maxbits;
  }


  bits0 = file->bits [0];
  bits1 = file->bits [1];


    /* Get the first 8 bits */

  bits1 |= (*bitslist & 0x0ff) << bits0;
  bits0 +=  bitcount;


    /* Write 8 bits? */

  if (bits0 >= 8)
  {
      /* Add the character to the character buffer */
      
    file->characters [file->characters_used] = bits1 & 0xff;
    
    file->characters_used++;
    
    if (file->characters_used == 64)
    {
      resultcode = cbf_flush_characters (file);
       
      if (resultcode)
      {
        file->bits [0] = bits0;
        file->bits [1] = bits1;

        return resultcode;
      }
    }

    bits0 -= 8;


      /* Get the remaining bits */

    bits1 = *bitslist >> (bitcount - bits0);


      /* Write the remaining bits */

    while (bits0 >= 8)
    {
      file->characters [file->characters_used] = bits1 & 0xff;
    
      file->characters_used++;
    
      if (file->characters_used == 64)
      {
        resultcode = cbf_flush_characters (file);
       
        if (resultcode)
        {
          file->bits [0] = bits0;
          file->bits [1] = bits1;

          return resultcode;
        }
      }

      bits1 >>= 8;
      bits0 -=  8;
    }
  }

  bits1 &= ~-(1 << bits0);

  file->bits [0] = bits0;
  file->bits [1] = bits1;


    /* Success */

  return 0;
}
int cbf_put_block ( cbf_file file,
size_t  nelem 
)

Definition at line 1340 of file cbf_file.c.

References cbf_file::buffer, cbf_file::buffer_size, CBF_ARGUMENT, cbf_failnez, CBF_FILEWRITE, cbf_flush_characters(), cbf_file::digest, MD5Update(), and cbf_file::stream.

{
  size_t done;

  
    /* Does the file exist? */
    
  if (!file)

    return CBF_ARGUMENT;


    /* Are there enough characters in the buffer? */

  if (nelem > file->buffer_size)

    return CBF_ARGUMENT;


    /* Flush the buffers */
    
  cbf_failnez (cbf_flush_characters (file))
  

    /* Write the characters */

  if (file->stream && nelem)
    
    done = fwrite (file->buffer, 1, nelem, file->stream);

  else

    done = 0;
    
    
    /* Update the message digest */
    
  if (done > 0 && file->digest)

    MD5Update (file->digest, file->buffer, done);
    

    /* Fail? */

  if (done < nelem)

    return CBF_FILEWRITE;


    /* Success */

  return 0;
}
int cbf_put_character ( cbf_file file,
int  c 
)

Definition at line 1099 of file cbf_file.c.

References cbf_failnez, cbf_flush_characters(), cbf_file::characters, and cbf_file::characters_used.

Referenced by cbf_frombase64(), cbf_frombasex(), cbf_fromqp(), cbf_put_string(), cbf_write_binary(), and cbf_write_character().

{
    /* Does the file exist? */

  if (!file)

    return EOF;


    /* Flush the buffer? */

  if (file->characters_used == 64)
  
    cbf_failnez (cbf_flush_characters (file))


    /* Add the character */
       
  file->characters [file->characters_used] = c & 0xff;
    
  file->characters_used++;
    

    /* Success */
    
  return 0;
}
int cbf_put_integer ( cbf_file file,
int  val,
int  valsign,
int  bitcount 
)

Definition at line 781 of file cbf_file.c.

References cbf_compress_nodestruct::bitcount, cbf_failnez, and cbf_put_bits().

Referenced by cbf_compress_canonical(), cbf_compress_none(), cbf_compress_packed(), cbf_flush_bits(), cbf_pack_chunk(), cbf_put_table(), and cbf_write_binary().

{
  int maxbits, signbits, valbits, sign;


    /* Any bits to write? */

  if (bitcount <= 0)

    return 0;


    /* Number of bits in an integer */

  maxbits = sizeof (int) * CHAR_BIT;


    /* Number of bits in the value and sign parts */

  signbits = bitcount - maxbits;

  if (signbits > 0)

    valbits = bitcount - signbits;

  else

    valbits = bitcount;


    /* Sign value */

  sign = -(val < 0 && valsign);


    /* Write the value */

  cbf_failnez (cbf_put_bits (file, &val, valbits))


    /* Write the sign part */

  while (signbits >= maxbits)
  {
    cbf_failnez (cbf_put_bits (file, &sign, maxbits))

    signbits -= maxbits;
  }

  if (signbits > 0)

    cbf_failnez (cbf_put_bits (file, &sign, signbits))


    /* Success */

  return 0;
}
int cbf_put_string ( cbf_file file,
const char *  string 
)

Definition at line 1188 of file cbf_file.c.

References CBF_ARGUMENT, cbf_failnez, and cbf_put_character().

{
    /* Does the string exist? */

  if (!string)

    return CBF_ARGUMENT;
    

    /* Write the string one character at a time */
    
  while (*string)
  {
    cbf_failnez (cbf_put_character (file, *string))
    
    string++;
  }


    /* Success */

  return 0;
}
int cbf_read_character ( cbf_file file)

Definition at line 1049 of file cbf_file.c.

References cbf_get_character(), cbf_file::column, last, cbf_file::last_read, and cbf_file::line.

Referenced by cbf_frombase64(), cbf_frombasex(), cbf_fromqp(), cbf_lex(), and cbf_read_line().

{
  int last, current;


    /* Does the file exist? */

  if (!file)

    return EOF;


    /* Read the next character */

  last = file->last_read;

  current = cbf_get_character (file);

  if ((current == '\n' && last == '\r') ||
      (current == '\r' && last == '\n'))

    current = cbf_get_character (file);


    /* Convert the end-of-line character and update line and column */

  if (current == '\n' || current == '\r')
  {
    current = '\n';

    file->column = 0;

    file->line++;
  }
  else

    if (current == '\t')

      file->column = (file->column & ~0x07) + 8;
      
    else
    
      file->column++;

  return current;
}
int cbf_read_line ( cbf_file file,
const char **  line 
)

Definition at line 1242 of file cbf_file.c.

References cbf_file::buffer, cbf_file::buffer_used, CBF_ARGUMENT, cbf_failnez, CBF_FILEREAD, cbf_read_character(), cbf_save_character(), and cbf_file::column.

Referenced by cbf_parse_mimeheader(), and cbf_skip_whitespace().

{
  int c;
  
  
    /* Does the file exist? */
    
  if (!file)

    return CBF_ARGUMENT;


    /* Empty the buffer */
    
  file->buffer_used = 0;

  file->column = 0;


    /* Read the characters */
 
  do
  {
    c = cbf_read_character (file);
    
    if (c == EOF)
    
      return CBF_FILEREAD;
      
    cbf_failnez (cbf_save_character (file, c))

  }
  while (c != '\n');


    /* Copy the pointer */
    
  if (line)
  
    *line = file->buffer;
    

    /* Success */

  return 0;
}
int cbf_reset_bits ( cbf_file file)

Definition at line 1001 of file cbf_file.c.

References cbf_file::bits, CBF_ARGUMENT, and cbf_reset_characters().

Referenced by cbf_compress(), cbf_decompress_parameters(), cbf_parse_binaryheader(), cbf_read_mime(), and cbf_write_binary().

{
  if (!file)

    return CBF_ARGUMENT;
    
  file->bits [0] = 0;
  file->bits [1] = 0;
  
  return cbf_reset_characters (file);
}
int cbf_reset_buffer ( cbf_file file)

Definition at line 329 of file cbf_file.c.

References cbf_file::buffer_used, and CBF_ARGUMENT.

Referenced by cbf_lex().

{
    /* Does the file exist? */

  if (!file)

    return CBF_ARGUMENT;


    /* Empty the buffer */

  file->buffer_used = 0;
  
  
    /* success */
    
  return 0;
}
int cbf_reset_characters ( cbf_file file)

Definition at line 1016 of file cbf_file.c.

References CBF_ARGUMENT, and cbf_file::characters_used.

Referenced by cbf_reset_bits().

{
  if (!file)

    return CBF_ARGUMENT;
    
  file->characters_used = 0;


    /* Success */

  return 0;
}
int cbf_save_character ( cbf_file file,
int  c 
)

Definition at line 351 of file cbf_file.c.

References cbf_file::buffer, cbf_file::buffer_size, cbf_file::buffer_used, CBF_ARGUMENT, cbf_failnez, and cbf_set_buffersize().

Referenced by cbf_lex(), and cbf_read_line().

{
  unsigned int new_size, kblock;

    /* Does the file exist? */

  if (!file)

    return CBF_ARGUMENT;


    /* Expand the buffer? */

  kblock = 16;

  if (file->buffer_used+2 > 128*2) kblock = 128;

  if (file->buffer_used+2 > 512*2) kblock = 512;

  if (file->buffer_used+2 > 2048*2) kblock = 2048;

  new_size = (((int)((file->buffer_used+2)/kblock)))*kblock+kblock;

  if (new_size < file->buffer_used+3) new_size = file->buffer_used+3;

  if (new_size >= file->buffer_size)
    cbf_failnez (cbf_set_buffersize (file, new_size))


    /* Add the character */

  file->buffer [file->buffer_used] = (char) c;

  file->buffer_used++;

  file->buffer [file->buffer_used] = '\0';


    /* Success */
  
  return 0;
}
int cbf_set_buffersize ( cbf_file file,
size_t  size 
)

Definition at line 290 of file cbf_file.c.

References cbf_file::buffer, cbf_file::buffer_size, CBF_ARGUMENT, and cbf_realloc().

Referenced by cbf_get_block(), and cbf_save_character().

{
  unsigned int kblock;

  size_t  new_size;

    /* Does the file exist? */

  if (!file)

    return CBF_ARGUMENT;

  kblock = 16;

  if (size > 128*2) kblock = 128;

  if (size > 512*2) kblock = 512;

  if (size > 2048*2) kblock = 2048;

  new_size = ((int)(size/kblock))*kblock+kblock;

    /* Is the size already close enough? */

  if (size > 0 && file->buffer_size >=  size && 
                  file->buffer_size <= new_size)

    return 0;


    /* Reallocate the buffer */

  return cbf_realloc ((void **) &file->buffer, 
                                &file->buffer_size, sizeof (char), new_size);
}
int cbf_set_filecoordinates ( cbf_file file,
unsigned int  line,
unsigned int  column 
)

Definition at line 462 of file cbf_file.c.

References CBF_ARGUMENT, cbf_file::column, and cbf_file::line.

{
    /* Does the file exist? */

  if (!file)

    return CBF_ARGUMENT;


    /* Set the coordinates */

  file->line = line;
    
  file->column = column;
    

    /* Success */

  return 0;
}
int cbf_set_fileposition ( cbf_file file,
long int  position,
int  whence 
)

Definition at line 1497 of file cbf_file.c.

References CBF_ARGUMENT, CBF_FILESEEK, and cbf_file::stream.

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

{
    /* Does the file exist? */
    
  if (!file)

    return CBF_ARGUMENT;
    
  if (!file->stream)
  
    return CBF_ARGUMENT;
    
    
    /* Set the position */
    
 if (fseek (file->stream, position, whence) < 0)

   return CBF_FILESEEK;
    
    
    /* Success */
    
  return 0;
}
int cbf_start_digest ( cbf_file file)

Definition at line 844 of file cbf_file.c.

References cbf_alloc(), CBF_ARGUMENT, cbf_failnez, cbf_flush_characters(), cbf_file::digest, and MD5Init().

Referenced by cbf_compress().

{
  if (!file)

    return CBF_ARGUMENT;
    
    
    /* Flush the buffers */

  cbf_failnez (cbf_flush_characters (file))
  

    /* Allocate the md5 context */
    
  if (!file->digest)

    cbf_failnez (cbf_alloc ((void **) &file->digest, 
                                       NULL, sizeof (MD5_CTX), 1))
                                       

    /* Initialize */
    
  MD5Init (file->digest);


    /* Success */
    
  return 0;
}
int cbf_write_character ( cbf_file file,
int  c 
)

Definition at line 1130 of file cbf_file.c.

References cbf_failnez, cbf_put_character(), cbf_file::column, ENC_CRTERM, ENC_LFTERM, cbf_file::line, and cbf_file::write_encoding.

Referenced by cbf_tobase64(), cbf_tobasex(), cbf_toqp(), cbf_write_ascii(), cbf_write_category(), cbf_write_datablockname(), cbf_write_itemname(), and cbf_write_string().

{
    /* Does the file exist? */

  if (!file)

    return EOF;


    /* Write the next character */

  if (c == '\n')
  {
      /* Line termination */
      
    if (file->write_encoding & ENC_CRTERM)

      cbf_failnez (cbf_put_character (file, '\r'))

    if (file->write_encoding & ENC_LFTERM)

      cbf_failnez (cbf_put_character (file, '\n'))


      /* Update line and column */

    if (c == '\n')
    {
      file->column = 0;

      file->line++;
    }
  }
  else
  {
    cbf_failnez (cbf_put_character (file, c))


      /* Update column */

    if (c == '\t')

      file->column = (file->column & ~0x07) + 8;
      
    else
    
      file->column++;
  }


    /* Success */

  return 0;
}
int cbf_write_string ( cbf_file file,
const char *  string 
)

Definition at line 1215 of file cbf_file.c.

References CBF_ARGUMENT, cbf_failnez, and cbf_write_character().

Referenced by cbf_tobase64(), cbf_tobasex(), cbf_toqp(), cbf_write_ascii(), cbf_write_binary(), cbf_write_category(), cbf_write_datablockname(), cbf_write_itemname(), and cbf_write_node().

{
    /* Does the string exist? */

  if (!string)

    return CBF_ARGUMENT;
    

    /* Write the string */

  while (*string)
  {
    cbf_failnez (cbf_write_character (file, *string))
    
    string++;
  }
  

    /* Success */

  return 0;
}