2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 AVCRC *av_crcEDB88320;
23 AVCRC *av_crc04C11DB7;
29 * @param ctx must be an array of sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
30 * @param cts_size size of ctx in bytes
31 * @return <0 on failure
33 int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
37 if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
39 if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
42 for (i = 0; i < 256; i++) {
44 for (c = i, j = 0; j < 8; j++)
45 c = (c>>1)^(poly & (-(c&1)));
48 for (c = i << 24, j = 0; j < 8; j++)
49 c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
55 if(ctx_size >= sizeof(AVCRC)*1024)
56 for (i = 0; i < 256; i++)
58 ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
64 uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
65 const uint8_t *end= buffer+length;
70 crc ^= le2me_32(*(uint32_t*)buffer); buffer+=4;
71 crc = ctx[3*256 + ( crc &0xFF)]
72 ^ctx[2*256 + ((crc>>8 )&0xFF)]
73 ^ctx[1*256 + ((crc>>16)&0xFF)]
74 ^ctx[0*256 + ((crc>>24) )];
78 crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
88 int p[4][4]={{1, 32, 0xedb88320L, 0x3D5CDD04},
89 {0, 32, 0x04c11db7L, 0xC0F5BAE0},
90 {0, 16, 0x8005 , 0x1FBB },
91 {0, 8, 0x07 , 0xE3 },};
92 AVCRC ctx[1 ? 1024:257];
94 for(i=0; i<sizeof(buf); i++)
98 av_crc_init(ctx, p[i][0], p[i][1], p[i][2], sizeof(ctx));
99 printf("crc %08X =%X\n", p[i][2], av_crc(ctx, 0, buf, sizeof(buf)));