crc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "common.h"
  21. #include "crc.h"
  22. #ifdef CONFIG_HARDCODED_TABLES
  23. #include "crc_data.h"
  24. #else
  25. static struct {
  26. uint8_t le;
  27. uint8_t bits;
  28. uint32_t poly;
  29. } av_crc_table_params[AV_CRC_MAX] = {
  30. [AV_CRC_8_ATM] = { 0, 8, 0x07 },
  31. [AV_CRC_16_ANSI] = { 0, 16, 0x8005 },
  32. [AV_CRC_16_CCITT] = { 0, 16, 0x1021 },
  33. [AV_CRC_32_IEEE] = { 0, 32, 0x04C11DB7 },
  34. [AV_CRC_32_IEEE_LE] = { 1, 32, 0xEDB88320 },
  35. };
  36. static AVCRC av_crc_table[AV_CRC_MAX][257];
  37. #endif
  38. /**
  39. * Inits a crc table.
  40. * @param ctx must be an array of sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
  41. * @param cts_size size of ctx in bytes
  42. * @param le if 1, lowest bit represents coefficient for highest exponent
  43. * of corresponding polynomial (both for poly and actual CRC).
  44. * If 0, you must swap the crc parameter and the result of av_crc
  45. * if you need the standard representation (can be simplified in
  46. * most cases to e.g. bswap16):
  47. * bswap_32(crc << (32-bits))
  48. * @param bits number of bits for the CRC
  49. * @param poly generator polynomial without the x**bits coefficient, in the
  50. * representation as specified by le
  51. * @return <0 on failure
  52. */
  53. int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
  54. int i, j;
  55. uint32_t c;
  56. if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
  57. return -1;
  58. if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
  59. return -1;
  60. for (i = 0; i < 256; i++) {
  61. if (le) {
  62. for (c = i, j = 0; j < 8; j++)
  63. c = (c>>1)^(poly & (-(c&1)));
  64. ctx[i] = c;
  65. } else {
  66. for (c = i << 24, j = 0; j < 8; j++)
  67. c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
  68. ctx[i] = bswap_32(c);
  69. }
  70. }
  71. ctx[256]=1;
  72. #ifndef CONFIG_SMALL
  73. if(ctx_size >= sizeof(AVCRC)*1024)
  74. for (i = 0; i < 256; i++)
  75. for(j=0; j<3; j++)
  76. ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
  77. #endif
  78. return 0;
  79. }
  80. /**
  81. * Get an initialized standard CRC table.
  82. * @param crc_id ID of a standard CRC
  83. * @return a pointer to the CRC table or NULL on failure
  84. */
  85. const AVCRC *av_crc_get_table(AVCRCId crc_id){
  86. #ifndef CONFIG_HARDCODED_TABLES
  87. if (!av_crc_table[crc_id][sizeof(av_crc_table[crc_id])/sizeof(av_crc_table[crc_id][0])-1])
  88. if (av_crc_init(av_crc_table[crc_id],
  89. av_crc_table_params[crc_id].le,
  90. av_crc_table_params[crc_id].bits,
  91. av_crc_table_params[crc_id].poly,
  92. sizeof(av_crc_table[crc_id])) < 0)
  93. return NULL;
  94. #endif
  95. return av_crc_table[crc_id];
  96. }
  97. /**
  98. * Calculate the CRC of a block
  99. * @param crc CRC of previous blocks if any or initial value for CRC.
  100. * @return CRC updated with the data from the given block
  101. *
  102. * @see av_crc_init() "le" parameter
  103. */
  104. uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
  105. const uint8_t *end= buffer+length;
  106. #ifndef CONFIG_SMALL
  107. if(!ctx[256])
  108. while(buffer<end-3){
  109. crc ^= le2me_32(*(const uint32_t*)buffer); buffer+=4;
  110. crc = ctx[3*256 + ( crc &0xFF)]
  111. ^ctx[2*256 + ((crc>>8 )&0xFF)]
  112. ^ctx[1*256 + ((crc>>16)&0xFF)]
  113. ^ctx[0*256 + ((crc>>24) )];
  114. }
  115. #endif
  116. while(buffer<end)
  117. crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
  118. return crc;
  119. }
  120. #ifdef TEST
  121. #undef printf
  122. int main(void){
  123. uint8_t buf[1999];
  124. int i;
  125. int p[4][3]={{AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04},
  126. {AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0},
  127. {AV_CRC_16_ANSI , 0x8005, 0x1FBB },
  128. {AV_CRC_8_ATM , 0x07, 0xE3 },};
  129. const AVCRC *ctx;
  130. for(i=0; i<sizeof(buf); i++)
  131. buf[i]= i+i*i;
  132. for(i=0; i<4; i++){
  133. ctx = av_crc_get_table(p[i][0]);
  134. printf("crc %08X =%X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf)));
  135. }
  136. return 0;
  137. }
  138. #endif