00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "common.h"
00023 #include "bswap.h"
00024 #include "crc.h"
00025
00026 #if CONFIG_HARDCODED_TABLES
00027 #include "crc_data.h"
00028 #else
00029 static struct {
00030 uint8_t le;
00031 uint8_t bits;
00032 uint32_t poly;
00033 } av_crc_table_params[AV_CRC_MAX] = {
00034 [AV_CRC_8_ATM] = { 0, 8, 0x07 },
00035 [AV_CRC_16_ANSI] = { 0, 16, 0x8005 },
00036 [AV_CRC_16_CCITT] = { 0, 16, 0x1021 },
00037 [AV_CRC_32_IEEE] = { 0, 32, 0x04C11DB7 },
00038 [AV_CRC_32_IEEE_LE] = { 1, 32, 0xEDB88320 },
00039 };
00040 static AVCRC av_crc_table[AV_CRC_MAX][257];
00041 #endif
00042
00059 int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
00060 unsigned i, j;
00061 uint32_t c;
00062
00063 if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
00064 return -1;
00065 if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
00066 return -1;
00067
00068 for (i = 0; i < 256; i++) {
00069 if (le) {
00070 for (c = i, j = 0; j < 8; j++)
00071 c = (c>>1)^(poly & (-(c&1)));
00072 ctx[i] = c;
00073 } else {
00074 for (c = i << 24, j = 0; j < 8; j++)
00075 c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
00076 ctx[i] = av_bswap32(c);
00077 }
00078 }
00079 ctx[256]=1;
00080 #if !CONFIG_SMALL
00081 if(ctx_size >= sizeof(AVCRC)*1024)
00082 for (i = 0; i < 256; i++)
00083 for(j=0; j<3; j++)
00084 ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
00085 #endif
00086
00087 return 0;
00088 }
00089
00095 const AVCRC *av_crc_get_table(AVCRCId crc_id){
00096 #if !CONFIG_HARDCODED_TABLES
00097 if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id])-1])
00098 if (av_crc_init(av_crc_table[crc_id],
00099 av_crc_table_params[crc_id].le,
00100 av_crc_table_params[crc_id].bits,
00101 av_crc_table_params[crc_id].poly,
00102 sizeof(av_crc_table[crc_id])) < 0)
00103 return NULL;
00104 #endif
00105 return av_crc_table[crc_id];
00106 }
00107
00115 uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
00116 const uint8_t *end= buffer+length;
00117
00118 #if !CONFIG_SMALL
00119 if(!ctx[256]) {
00120 while(((intptr_t) buffer & 3) && buffer < end)
00121 crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
00122
00123 while(buffer<end-3){
00124 crc ^= av_le2ne32(*(const uint32_t*)buffer); buffer+=4;
00125 crc = ctx[3*256 + ( crc &0xFF)]
00126 ^ctx[2*256 + ((crc>>8 )&0xFF)]
00127 ^ctx[1*256 + ((crc>>16)&0xFF)]
00128 ^ctx[0*256 + ((crc>>24) )];
00129 }
00130 }
00131 #endif
00132 while(buffer<end)
00133 crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
00134
00135 return crc;
00136 }
00137
00138 #ifdef TEST
00139 #undef printf
00140 int main(void){
00141 uint8_t buf[1999];
00142 int i;
00143 int p[4][3]={{AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04},
00144 {AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0},
00145 {AV_CRC_16_ANSI , 0x8005, 0x1FBB },
00146 {AV_CRC_8_ATM , 0x07, 0xE3 },};
00147 const AVCRC *ctx;
00148
00149 for(i=0; i<sizeof(buf); i++)
00150 buf[i]= i+i*i;
00151
00152 for(i=0; i<4; i++){
00153 ctx = av_crc_get_table(p[i][0]);
00154 printf("crc %08X =%X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf)));
00155 }
00156 return 0;
00157 }
00158 #endif