00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "common.h"
00022 #include "crc.h"
00023
00024 #if LIBAVUTIL_VERSION_INT < (50<<16)
00025 AVCRC *av_crcEDB88320;
00026 AVCRC *av_crc04C11DB7;
00027 AVCRC *av_crc8005 ;
00028 AVCRC *av_crc07 ;
00029 #else
00030 AVCRC av_crcEDB88320[257];
00031 AVCRC av_crc04C11DB7[257];
00032 AVCRC av_crc8005 [257];
00033 AVCRC av_crc07 [257];
00034 #endif
00035
00051 int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
00052 int i, j;
00053 uint32_t c;
00054
00055 if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
00056 return -1;
00057 if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
00058 return -1;
00059
00060 for (i = 0; i < 256; i++) {
00061 if (le) {
00062 for (c = i, j = 0; j < 8; j++)
00063 c = (c>>1)^(poly & (-(c&1)));
00064 ctx[i] = c;
00065 } else {
00066 for (c = i << 24, j = 0; j < 8; j++)
00067 c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
00068 ctx[i] = bswap_32(c);
00069 }
00070 }
00071 ctx[256]=1;
00072 #ifndef CONFIG_SMALL
00073 if(ctx_size >= sizeof(AVCRC)*1024)
00074 for (i = 0; i < 256; i++)
00075 for(j=0; j<3; j++)
00076 ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
00077 #endif
00078
00079 return 0;
00080 }
00081
00089 uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
00090 const uint8_t *end= buffer+length;
00091
00092 #ifndef CONFIG_SMALL
00093 if(!ctx[256])
00094 while(buffer<end-3){
00095 crc ^= le2me_32(*(uint32_t*)buffer); buffer+=4;
00096 crc = ctx[3*256 + ( crc &0xFF)]
00097 ^ctx[2*256 + ((crc>>8 )&0xFF)]
00098 ^ctx[1*256 + ((crc>>16)&0xFF)]
00099 ^ctx[0*256 + ((crc>>24) )];
00100 }
00101 #endif
00102 while(buffer<end)
00103 crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
00104
00105 return crc;
00106 }
00107
00108 #ifdef TEST
00109 #undef printf
00110 main(){
00111 uint8_t buf[1999];
00112 int i;
00113 int p[4][4]={{1, 32, AV_CRC_32_IEEE_LE, 0x3D5CDD04},
00114 {0, 32, AV_CRC_32_IEEE , 0xC0F5BAE0},
00115 {0, 16, AV_CRC_16 , 0x1FBB },
00116 {0, 8, AV_CRC_8_ATM , 0xE3 },};
00117 AVCRC ctx[1 ? 1024:257];
00118
00119 for(i=0; i<sizeof(buf); i++)
00120 buf[i]= i+i*i;
00121
00122 for(i=0; i<4; i++){
00123 av_crc_init(ctx, p[i][0], p[i][1], p[i][2], sizeof(ctx));
00124 printf("crc %08X =%X\n", p[i][2], av_crc(ctx, 0, buf, sizeof(buf)));
00125 }
00126 }
00127 #endif