home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / source / pngchksm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  2.6 KB  |  112 lines

  1. //
  2. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // See the README.TXT file that came with this software for restrictions
  9. // on the use and redistribution of this file or send E-mail to
  10. // info@colosseumbuilders.com
  11. //
  12.  
  13. //
  14. //  Title:  PNG Checksum functions
  15. //
  16. //  Author:  John M. Miano  miano@colosseumbuilders.com
  17. //
  18. //  Description:
  19. //
  20. //    PNG uses two check sums. CRC32 is used on PNG chunks. Adler32 is use
  21. //    on Deflate-compressed data.
  22. //
  23. //    CRC32 is performed using a lookup table. The lookup table must be
  24. //    initialized before any CRC32 values are calculated. The initial
  25. //    CRC32 value should be 0xFFFFFFFF.
  26. //
  27. //    The initial Adler32 value should be 1.
  28. //
  29.  
  30. #include "pngchksm.h"
  31. #include "datatype.h"
  32.  
  33. static UBYTE4 crc_table [256] ;  // Precomputed CRC lookup tables
  34. static bool table_made = false ;
  35.  
  36. //
  37. //  Description:
  38. //
  39. //    This function creates the CRC32 lookup table.
  40. //
  41. void MakeCrcTable ()
  42. {
  43.   if (table_made)
  44.     return ;
  45.  
  46.   table_made = true ;
  47.   for (unsigned int ii = 0 ; ii < 256 ; ++ ii)
  48.   {
  49.     crc_table [ii] = ii ;
  50.     for (unsigned int jj = 0 ; jj < 8 ; ++ jj)
  51.     {
  52.       if ((crc_table [ii] & 1) == 0)
  53.       {
  54.         crc_table [ii] >>= 1 ;
  55.       }
  56.       else
  57.       {
  58.         crc_table [ii] = 0xEDB88320L ^ (crc_table [ii] >> 1) ;
  59.       }
  60.     }
  61.   }
  62.   return ;
  63. }
  64.  
  65. //
  66. //  Description:
  67. //
  68. //    This function updates the CRC32 value using a string of data.
  69. //
  70. //    This implementation is taken from the PNG standard.
  71. //
  72. //  Parameters:
  73. //    crc:  The initial CRC32 value.
  74. //    buffer:  The data buffer to add to the CRC value
  75. //    length:  The data buffer length
  76. //
  77. //  Return Value:
  78. //    The updated CRC value.
  79. //
  80. UBYTE4 CRC32 (UBYTE4 crc, void *buffer, unsigned int length)
  81. {
  82.   UBYTE4 result = crc ;
  83.  
  84.   for (UBYTE4 ii = 0 ; ii < length ; ++ ii)
  85.   {
  86.     result = crc_table [(result ^ ((char *) buffer) [ii]) & 0xFF] ^ (result >> 8) ;
  87.   }
  88.   return result ;
  89. }
  90.  
  91. //
  92. //  Description:
  93. //
  94. //    This function updates an Adler32 checksum using a buffer. This
  95. //    implementation is from the Deflate standard.
  96. //
  97. //  Parameters:
  98. //    adler:  The input adler 32 value.
  99. //    value:  The data buffer
  100. //
  101. UBYTE4 Adler (UBYTE4 adler, UBYTE1 value) 
  102. {
  103.   static const UBYTE4 prime = 65521 ;
  104.  
  105.   UBYTE4 lo = adler & 0xFFFF ;
  106.   UBYTE4 hi = (adler >> 16) & 0xFFFF ;
  107.  
  108.   lo = (lo + value) % prime ;
  109.   hi = (lo + hi) % prime ;
  110.   return (hi << 16) | lo ;
  111. }
  112.