crc32.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Library: libcrc
  3. * File: src/crc32.c
  4. * Author: Lammert Bies
  5. *
  6. * This file is licensed under the MIT License as stated below
  7. *
  8. * Copyright (c) 1999-2016 Lammert Bies
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. *
  28. * Description
  29. * -----------
  30. * The source file src/crc32.c contains the routines which are needed to
  31. * calculate a 32 bit CRC value of a sequence of bytes.
  32. */
  33. #include <stdbool.h>
  34. #include <stdlib.h>
  35. #include "checksum.h"
  36. static void init_crc32_tab( void );
  37. static bool crc_tab32_init = false;
  38. static uint32_t crc_tab32[256];
  39. /*
  40. * uint32_t crc_32( const unsigned char *input_str, size_t num_bytes );
  41. *
  42. * The function crc_32() calculates in one pass the common 32 bit CRC value for
  43. * a byte string that is passed to the function together with a parameter
  44. * indicating the length.
  45. */
  46. uint32_t crc_32( const unsigned char *input_str, size_t num_bytes ) {
  47. uint32_t crc;
  48. uint32_t tmp;
  49. uint32_t long_c;
  50. const unsigned char *ptr;
  51. size_t a;
  52. if ( ! crc_tab32_init ) init_crc32_tab();
  53. crc = CRC_START_32;
  54. ptr = input_str;
  55. if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
  56. long_c = 0x000000FFL & (uint32_t) *ptr;
  57. tmp = crc ^ long_c;
  58. crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ];
  59. ptr++;
  60. }
  61. crc ^= 0xffffffffL;
  62. return crc & 0xffffffffL;
  63. } /* crc_32 */
  64. /*
  65. * uint32_t update_crc_32( uint32_t crc, unsigned char c );
  66. *
  67. * The function update_crc_32() calculates a new CRC-32 value based on the
  68. * previous value of the CRC and the next byte of the data to be checked.
  69. */
  70. uint32_t update_crc_32( uint32_t crc, unsigned char c ) {
  71. uint32_t tmp;
  72. uint32_t long_c;
  73. long_c = 0x000000ffL & (uint32_t) c;
  74. if ( ! crc_tab32_init ) init_crc32_tab();
  75. tmp = crc ^ long_c;
  76. crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ];
  77. return crc & 0xffffffffL;;
  78. } /* update_crc_32 */
  79. /*
  80. * static void init_crc32_tab( void );
  81. *
  82. * For optimal speed, the CRC32 calculation uses a table with pre-calculated
  83. * bit patterns which are used in the XOR operations in the program. This table
  84. * is generated once, the first time the CRC update routine is called.
  85. */
  86. static void init_crc32_tab( void ) {
  87. uint32_t i;
  88. uint32_t j;
  89. uint32_t crc;
  90. for (i=0; i<256; i++) {
  91. crc = i;
  92. for (j=0; j<8; j++) {
  93. if ( crc & 0x00000001L ) crc = ( crc >> 1 ) ^ CRC_POLY_32;
  94. else crc = crc >> 1;
  95. }
  96. crc_tab32[i] = crc;
  97. }
  98. crc_tab32_init = true;
  99. } /* init_crc32_tab */