cgul_crc32.h File Reference

CRC-32. More...

#include "cgul_common.h"
#include "cgul_exception.h"
Include dependency graph for cgul_crc32.h:
This graph shows which files directly or indirectly include this file:

Typedefs

typedef typedefCGUL_BEGIN_C struct cgul_crc32 * cgul_crc32_t
 

Functions

CGUL_EXPORT cgul_crc32_t cgul_crc32__new (cgul_exception_t *cex, unsigned long g, unsigned long init, unsigned long final_xor, int reflected)
 
CGUL_EXPORT cgul_crc32_t cgul_crc32__new_appends_size (cgul_exception_t *cex, unsigned long g, unsigned long init, unsigned long final_xor, int reflected, size_t size_xor)
 
CGUL_EXPORT void cgul_crc32__delete (cgul_crc32_t crc)
 
CGUL_EXPORT void cgul_crc32__assign (cgul_exception_t *cex, cgul_crc32_t lhs, cgul_crc32_t rhs)
 
CGUL_EXPORT unsigned long cgul_crc32__crc (cgul_exception_t *cex, cgul_crc32_t crc)
 
CGUL_EXPORT unsigned long cgul_crc32__crc_block (cgul_exception_t *cex, cgul_crc32_t crc, const void *block, size_t length)
 
CGUL_EXPORT unsigned long cgul_crc32__crc_range_of_block (cgul_exception_t *cex, cgul_crc32_t crc, const void *block, size_t offset, size_t length)
 
CGUL_EXPORT unsigned long cgul_crc32__crc_file (cgul_exception_t *cex, cgul_crc32_t crc, FILE *f)
 
CGUL_EXPORT unsigned long cgul_crc32__crc_fname (cgul_exception_t *cex, cgul_crc32_t crc, const char *fname)
 
CGUL_EXPORT void cgul_crc32__update_byte (cgul_exception_t *cex, cgul_crc32_t crc, unsigned char c)
 
CGUL_EXPORT void cgul_crc32__update_block (cgul_exception_t *cex, cgul_crc32_t crc, const void *block, size_t length)
 
CGUL_EXPORT void cgul_crc32__update_range_of_block (cgul_exception_t *cex, cgul_crc32_t crc, const void *block, size_t offset, size_t length)
 
CGUL_EXPORT void cgul_crc32__update_file (cgul_exception_t *cex, cgul_crc32_t crc, FILE *f)
 
CGUL_EXPORT void cgul_crc32__update_fname (cgul_exception_t *cex, cgul_crc32_t crc, const char *fname)
 
CGUL_EXPORT void cgul_crc32__reset (cgul_exception_t *cex, cgul_crc32_t crc)
 
CGUL_EXPORT char * cgul_crc32__get_table_as_string (cgul_exception_t *cex, cgul_crc32_t crc)
 

Detailed Description

The cgul_crc32 class is designed to handle any of the different common permutations on the CRC-32 algorithm. This class is parameterized to allow different generator polynomials, initial values of the shift register, final XOR values, whether to append the size, whether to XOR the size before appending, and whether the data stream is reflected.

This class was first written in java, and the naming convention for methods in this class follows what is established by java.security.MessageDigest instead what is established by java.util.zip.CRC32.

A simple example of how to use this class follows:

    cgul_exception_t* cex = NULL;
    cgul_crc32_t crc32 = cgul_crc32__new(cex,
                                         0xedb88320,
                                         0xffffffff,
                                         0xffffffff,
                                         1);
    unsigned long result = cgul_crc32__crc_block(cex,
                                                 crc,
                                                 "123456789",
                                                 9);
    cgul_libc_printf(cex, "0x%lx\n", result);

The above code will calculate the ZIP CRC for the 9 bytes that make up the ASCII string "123456789". The CRC that is printed to the screen will be 0xcbf43926.

You might also want to calculate the cksum/posix CRC. You can construct the right cgul_crc32 object as follows:

    crc32 = cgul_crc32__new_appends_size(cex,
                                         0x04c11db7,
                                         0,
                                         0xffffffff,
                                         0,
                                         0);

NOTE: If you use this class from multiple threads, you MUST provide external synchronization to all methods so that you do not corrupt internal data structures.

Author
Paul Serice
See also
cgul_crc32_incr

Typedef Documentation

§ cgul_crc32_t

typedef typedefCGUL_BEGIN_C struct cgul_crc32* cgul_crc32_t

Opaque pointer to cgul_crc32 instance.

Function Documentation

§ cgul_crc32__new()

CGUL_EXPORT cgul_crc32_t cgul_crc32__new ( cgul_exception_t cex,
unsigned long  g,
unsigned long  init,
unsigned long  final_xor,
int  reflected 
)

Create a new cgul_crc32 object. Objects created with this constructor do not append the size to the data stream that is CRCed. The caller is responsible for freeing the object by calling cgul_crc32__delete(). If memory cannot be allocated, NULL is returned, and an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]ggenerator polynomial
[in]initinitial value loaded into the accumulator
[in]final_xorvalue to xor with the calculated crc
[in]reflectedwhether to use the reflected algorithm
Returns
new cgul_crc32 instance

Referenced by cgul_crc32_cxx::cgul_crc32_cxx().

§ cgul_crc32__new_appends_size()

CGUL_EXPORT cgul_crc32_t cgul_crc32__new_appends_size ( cgul_exception_t cex,
unsigned long  g,
unsigned long  init,
unsigned long  final_xor,
int  reflected,
size_t  size_xor 
)

Create a new cgul_crc32 object. Objects created with this constructor append the size to the data stream that is CRCed. The caller is responsible for freeing the object by calling cgul_crc32__delete(). If memory cannot be allocated, NULL is returned and an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]gpolynomial
[in]initinitial value loaded into the accumulator
[in]final_xorvalue to xor with the calculated crc
[in]reflectedwhether to use the reflected algorithm
[in]size_xorvalue to xor with the size before appending
Returns
new cgul_crc32 instance

Referenced by cgul_crc32_cxx::cgul_crc32_cxx().

§ cgul_crc32__delete()

CGUL_EXPORT void cgul_crc32__delete ( cgul_crc32_t  crc)

This method frees all internally allocated memory. Do not try to access crc after calling this method.

Parameters
[in]crccgul_crc32 instance

Referenced by cgul_crc32_cxx::set_obj(), and cgul_crc32_cxx::~cgul_crc32_cxx().

§ cgul_crc32__assign()

CGUL_EXPORT void cgul_crc32__assign ( cgul_exception_t cex,
cgul_crc32_t  lhs,
cgul_crc32_t  rhs 
)

Assign the current accumulator and size of rhs to lhs. This operation probably only makes sense if lhs and rhs have been constructed identically. If an error occurs, an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]lhsleft-hand side
[in]rhsright-hand side

Referenced by cgul_crc32_cxx::assign().

§ cgul_crc32__crc()

CGUL_EXPORT unsigned long cgul_crc32__crc ( cgul_exception_t cex,
cgul_crc32_t  crc 
)

This method completes the CRC calculation by performing the size XOR and the final XOR. You will generally call one of the update() methods before calling this method. When this method returns, the internal state of the object is reset so that it is ready to calculate the next CRC. The return value is the final value for the CRC.

Parameters
[in]cexc-style exception
[in]crccgul_crc32 instance
Returns
final crc value

Referenced by cgul_crc32_cxx::crc().

§ cgul_crc32__crc_block()

CGUL_EXPORT unsigned long cgul_crc32__crc_block ( cgul_exception_t cex,
cgul_crc32_t  crc,
const void *  block,
size_t  length 
)

This is the method to call when you only have one block for which you want to calculate a CRC. You can also call this if you have called one of the update() methods before, and you know that block is your last block. When this method returns, the internal state of the object is reset so that it is ready to calculate the next CRC. length is the number of bytes inside of block that you want to add to the CRC calculation. The return value is the final value for the CRC.

Parameters
[in]cexc-style exception
[in]crccgul_crc32 instance
[in]blockblock of data to add to the crc
[in]lengthlength of block
Returns
final crc value

Referenced by cgul_crc32_cxx::crc_block().

§ cgul_crc32__crc_range_of_block()

CGUL_EXPORT unsigned long cgul_crc32__crc_range_of_block ( cgul_exception_t cex,
cgul_crc32_t  crc,
const void *  block,
size_t  offset,
size_t  length 
)

This is the method to call when you want to calculate the CRC from a range of bytes from one block. You can also call this if you have called one of the update() methods before, and you know that block is your last block. When this method returns, the internal state of the object is reset so that it is ready to calculate the next CRC. offset is the index into block where you want to add length bytes to the CRC calculation. The return value is the final value for the CRC.

Parameters
[in]cexc-style exception
[in]crccgul_crc32 instance
[in]blockblock of data to add to the crc
[in]offsetoffset into block
[in]lengthlength of block
Returns
final crc value

Referenced by cgul_crc32_cxx::crc_range_of_block().

§ cgul_crc32__crc_file()

CGUL_EXPORT unsigned long cgul_crc32__crc_file ( cgul_exception_t cex,
cgul_crc32_t  crc,
FILE *  f 
)

This is the method to call when you only have one file for which you want to calculate a CRC. You can also call this if you have called one of the update() methods before (like cgul_crc32__update_file()), and you know that f is your last file. When this method returns, the internal state of the object is reset so that it is ready to calculate the next CRC. The return value is the final value for the CRC.

Parameters
[in,out]cexc-style exception
[in]crccgul_crc32 instance
[in]finput file
Returns
final crc value

Referenced by cgul_crc32_cxx::crc_file().

§ cgul_crc32__crc_fname()

CGUL_EXPORT unsigned long cgul_crc32__crc_fname ( cgul_exception_t cex,
cgul_crc32_t  crc,
const char *  fname 
)

This is the method to call when you only have one file for which you want to calculate a CRC. You can also call this if you have called one of the update() methods before (like cgul_crc32__update_fname()), and you know that fname is your last file. When this method returns, the internal state of the object is reset so that it is ready to calculate the next CRC. The return value is the final value for the CRC.

Parameters
[in,out]cexc-style exception
[in]crccgul_crc32 instance
[in]fnameinput file name
Returns
final crc value

Referenced by cgul_crc32_cxx::crc_fname().

§ cgul_crc32__update_byte()

CGUL_EXPORT void cgul_crc32__update_byte ( cgul_exception_t cex,
cgul_crc32_t  crc,
unsigned char  c 
)

This method updates the CRC by adding the byte c to the CRC calculation.

Parameters
[in]cexc-style exception
[in]crccgul_crc32 instance
[in]cbyte to add to crc

Referenced by cgul_crc32_cxx::update_byte().

§ cgul_crc32__update_block()

CGUL_EXPORT void cgul_crc32__update_block ( cgul_exception_t cex,
cgul_crc32_t  crc,
const void *  block,
size_t  length 
)

This method updates the CRC by adding all the bytes in block to the calculation.

Parameters
[in]cexc-style exception
[in]crccgul_crc32 instance
[in]blockblock of data to add to the crc
[in]lengthlength of block

Referenced by cgul_crc32_cxx::update_block().

§ cgul_crc32__update_range_of_block()

CGUL_EXPORT void cgul_crc32__update_range_of_block ( cgul_exception_t cex,
cgul_crc32_t  crc,
const void *  block,
size_t  offset,
size_t  length 
)

This method updates the CRC by adding a range of bytes in block to the calculation. The range starts at offset and extends length bytes.

Parameters
[in]cexc-style exception
[in]crccgul_crc32 instance
[in]blockblock of data to add to the crc
[in]offsetoffset into block
[in]lengthlength of block

Referenced by cgul_crc32_cxx::update_range_of_block().

§ cgul_crc32__update_file()

CGUL_EXPORT void cgul_crc32__update_file ( cgul_exception_t cex,
cgul_crc32_t  crc,
FILE *  f 
)

This method updates the CRC by adding all the bytes (starting at the current offset) in the file f to the calculation. If an error occurs, an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]crccgul_crc32 instance
[in]finput file

Referenced by cgul_crc32_cxx::update_file().

§ cgul_crc32__update_fname()

CGUL_EXPORT void cgul_crc32__update_fname ( cgul_exception_t cex,
cgul_crc32_t  crc,
const char *  fname 
)

This method updates the CRC by adding all the bytes in the file with name fname to the calculation. If an error occurs, an exception is thrown.

Parameters
[in,out]cexc-style exception
[in]crccgul_crc32 instance
[in]fnameinput file name

Referenced by cgul_crc32_cxx::update_fname().

§ cgul_crc32__reset()

CGUL_EXPORT void cgul_crc32__reset ( cgul_exception_t cex,
cgul_crc32_t  crc 
)

Reset the cgul_crc32 object for further use.

Parameters
[in]cexc-style exception
[in]crccgul_crc32 instance

Referenced by cgul_crc32_cxx::reset().

§ cgul_crc32__get_table_as_string()

CGUL_EXPORT char* cgul_crc32__get_table_as_string ( cgul_exception_t cex,
cgul_crc32_t  crc 
)

You can use this method to see the CRC lookup table that is being used. The returned value is the CRC lookup table for this object as a string. The caller is responsible for calling free() on the string that is returned.

Parameters
[in,out]cexc-style exception
[in]crccgul_crc32 instance
Returns
crc lookup table as a string

Referenced by cgul_crc32_cxx::get_table_as_string().