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
00043 int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
00044 {
00045 unsigned i, j;
00046 uint32_t c;
00047
00048 if (bits < 8 || bits > 32 || poly >= (1LL << bits))
00049 return -1;
00050 if (ctx_size != sizeof(AVCRC) * 257 && ctx_size != sizeof(AVCRC) * 1024)
00051 return -1;
00052
00053 for (i = 0; i < 256; i++) {
00054 if (le) {
00055 for (c = i, j = 0; j < 8; j++)
00056 c = (c >> 1) ^ (poly & (-(c & 1)));
00057 ctx[i] = c;
00058 } else {
00059 for (c = i << 24, j = 0; j < 8; j++)
00060 c = (c << 1) ^ ((poly << (32 - bits)) & (((int32_t) c) >> 31));
00061 ctx[i] = av_bswap32(c);
00062 }
00063 }
00064 ctx[256] = 1;
00065 #if !CONFIG_SMALL
00066 if (ctx_size >= sizeof(AVCRC) * 1024)
00067 for (i = 0; i < 256; i++)
00068 for (j = 0; j < 3; j++)
00069 ctx[256 *(j + 1) + i] =
00070 (ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
00071 #endif
00072
00073 return 0;
00074 }
00075
00076 const AVCRC *av_crc_get_table(AVCRCId crc_id)
00077 {
00078 #if !CONFIG_HARDCODED_TABLES
00079 if (!av_crc_table[crc_id][FF_ARRAY_ELEMS(av_crc_table[crc_id]) - 1])
00080 if (av_crc_init(av_crc_table[crc_id],
00081 av_crc_table_params[crc_id].le,
00082 av_crc_table_params[crc_id].bits,
00083 av_crc_table_params[crc_id].poly,
00084 sizeof(av_crc_table[crc_id])) < 0)
00085 return NULL;
00086 #endif
00087 return av_crc_table[crc_id];
00088 }
00089
00090 uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
00091 const uint8_t *buffer, size_t length)
00092 {
00093 const uint8_t *end = buffer + length;
00094
00095 #if !CONFIG_SMALL
00096 if (!ctx[256]) {
00097 while (((intptr_t) buffer & 3) && buffer < end)
00098 crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
00099
00100 while (buffer < end - 3) {
00101 crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
00102 crc = ctx[3 * 256 + ( crc & 0xFF)] ^
00103 ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
00104 ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
00105 ctx[0 * 256 + ((crc >> 24) )];
00106 }
00107 }
00108 #endif
00109 while (buffer < end)
00110 crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
00111
00112 return crc;
00113 }
00114
00115 #ifdef TEST
00116 int main(void)
00117 {
00118 uint8_t buf[1999];
00119 int i;
00120 int p[4][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 },
00121 { AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0 },
00122 { AV_CRC_16_ANSI , 0x8005 , 0x1FBB },
00123 { AV_CRC_8_ATM , 0x07 , 0xE3 }
00124 };
00125 const AVCRC *ctx;
00126
00127 for (i = 0; i < sizeof(buf); i++)
00128 buf[i] = i + i * i;
00129
00130 for (i = 0; i < 4; i++) {
00131 ctx = av_crc_get_table(p[i][0]);
00132 printf("crc %08X = %X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf)));
00133 }
00134 return 0;
00135 }
00136 #endif