Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
  2.  
  3. /* ========================================================================= */
  4. local unsigned long gf2_matrix_times(mat, vec)
  5. unsigned long *mat;
  6. unsigned long vec;
  7. {
  8. unsigned long sum;
  9.  
  10. sum = 0;
  11. while (vec) {
  12. if (vec & 1)
  13. sum ^= *mat;
  14. vec >>= 1;
  15. mat++;
  16. }
  17. return sum;
  18. }
  19.  
  20. /* ========================================================================= */
  21. local void gf2_matrix_square(square, mat)
  22. unsigned long *square;
  23. unsigned long *mat;
  24. {
  25. int n;
  26.  
  27. for (n = 0; n < GF2_DIM; n++)
  28. square[n] = gf2_matrix_times(mat, mat[n]);
  29. }
  30.  
  31. /* ========================================================================= */
  32. local uLong crc32_combine_(crc1, crc2, len2)
  33. uLong crc1;
  34. uLong crc2;
  35. z_off64_t len2;
  36. {
  37. int n;
  38. unsigned long row;
  39. unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
  40. unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
  41.  
  42. /* degenerate case (also disallow negative lengths) */
  43. if (len2 <= 0)
  44. return crc1;
  45.  
  46. /* put operator for one zero bit in odd */
  47. odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
  48. row = 1;
  49. for (n = 1; n < GF2_DIM; n++) {
  50. odd[n] = row;
  51. row <<= 1;
  52. }
  53.  
  54. /* put operator for two zero bits in even */
  55. gf2_matrix_square(even, odd);
  56.  
  57. /* put operator for four zero bits in odd */
  58. gf2_matrix_square(odd, even);
  59.  
  60. /* apply len2 zeros to crc1 (first square will put the operator for one
  61. zero byte, eight zero bits, in even) */
  62. do {
  63. /* apply zeros operator for this bit of len2 */
  64. gf2_matrix_square(even, odd);
  65. if (len2 & 1)
  66. crc1 = gf2_matrix_times(even, crc1);
  67. len2 >>= 1;
  68.  
  69. /* if no more bits set, then done */
  70. if (len2 == 0)
  71. break;
  72.  
  73. /* another iteration of the loop with odd and even swapped */
  74. gf2_matrix_square(odd, even);
  75. if (len2 & 1)
  76. crc1 = gf2_matrix_times(odd, crc1);
  77. len2 >>= 1;
  78.  
  79. /* if no more bits set, then done */
  80. } while (len2 != 0);
  81.  
  82. /* return combined crc */
  83. crc1 ^= crc2;
  84. return crc1;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement