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 {
00061 unsigned i, j;
00062 uint32_t c;
00063
00064 if (bits < 8 || bits > 32 || poly >= (1LL << bits))
00065 return -1;
00066 if (ctx_size != sizeof(AVCRC) * 257 && ctx_size != sizeof(AVCRC) * 1024)
00067 return -1;
00068
00069 for (i = 0; i < 256; i++) {
00070 if (le) {
00071 for (c = i, j = 0; j < 8; j++)
00072 c = (c >> 1) ^ (poly & (-(c & 1)));
00073 ctx[i] = c;
00074 } else {
00075 for (c = i << 24, j = 0; j < 8; j++)
00076 c = (c << 1) ^ ((poly << (32 - bits)) & (((int32_t) c) >> 31));
00077 ctx[i] = av_bswap32(c);
00078 }
00079 }
00080 ctx[256] = 1;
00081 #if !CONFIG_SMALL
00082 if (ctx_size >= sizeof(AVCRC) * 1024)
00083 for (i = 0; i < 256; i++)
00084 for (j = 0; j < 3; j++)
00085 ctx[256 *(j + 1) + i] =
00086 (ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
00087 #endif
00088
00089 return 0;
00090 }
00091
00097 const AVCRC *av_crc_get_table(AVCRCId crc_id)
00098 {
00099 #if !CONFIG_HARDCODED_TABLES
00100 if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id]) - 1])
00101 if (av_crc_init(av_crc_table[crc_id],
00102 av_crc_table_params[crc_id].le,
00103 av_crc_table_params[crc_id].bits,
00104 av_crc_table_params[crc_id].poly,
00105 sizeof(av_crc_table[crc_id])) < 0)
00106 return NULL;
00107 #endif
00108 return av_crc_table[crc_id];
00109 }
00110
00118 uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
00119 const uint8_t *buffer, size_t length)
00120 {
00121 const uint8_t *end = buffer + length;
00122
00123 #if !CONFIG_SMALL
00124 if (!ctx[256]) {
00125 while (((intptr_t) buffer & 3) && buffer < end)
00126 crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
00127
00128 while (buffer < end - 3) {
00129 crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
00130 crc = ctx[3 * 256 + ( crc & 0xFF)] ^
00131 ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
00132 ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
00133 ctx[0 * 256 + ((crc >> 24) )];
00134 }
00135 }
00136 #endif
00137 while (buffer < end)
00138 crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
00139
00140 return crc;
00141 }
00142
00143 #ifdef TEST
00144 #undef printf
00145 int main(void)
00146 {
00147 uint8_t buf[1999];
00148 int i;
00149 int p[4][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 },
00150 { AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0 },
00151 { AV_CRC_16_ANSI , 0x8005 , 0x1FBB },
00152 { AV_CRC_8_ATM , 0x07 , 0xE3 }
00153 };
00154 const AVCRC *ctx;
00155
00156 for (i = 0; i < sizeof(buf); i++)
00157 buf[i] = i + i * i;
00158
00159 for (i = 0; i < 4; i++) {
00160 ctx = av_crc_get_table(p[i][0]);
00161 printf("crc %08X = %X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf)));
00162 }
00163 return 0;
00164 }
00165 #endif