annotate crc.c @ 36:8795371b6c88 libavutil

<= vs. >= 10l bug
author michael
date Sat, 29 Apr 2006 15:38:20 +0000
parents 664e2d2b92b9
children f85d07038450
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
1 #include "common.h"
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
2 #include "crc.h"
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
3
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
4 AVCRC *av_crcEDB88320;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
5 AVCRC *av_crc04C11DB7;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
6 AVCRC *av_crc8005 ;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
7 AVCRC *av_crc07 ;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
8
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
9 /**
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
10 * Inits a crc table.
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
11 * @param ctx must be an array of sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
12 * @param cts_size size of ctx in bytes
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
13 * @return <0 on failure
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
14 */
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
15 int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
16 int i, j;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
17 uint32_t c;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
18
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
19 if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
20 return -1;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
21 if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
22 return -1;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
23
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
24 for (i = 0; i < 256; i++) {
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
25 if (le) {
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
26 for (c = i, j = 0; j < 8; j++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
27 c = (c>>1)^(poly & (-(c&1)));
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
28 ctx[i] = c;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
29 } else {
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
30 for (c = i << 24, j = 0; j < 8; j++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
31 c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
32 ctx[i] = bswap_32(c);
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
33 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
34 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
35 ctx[256]=1;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
36 if(ctx_size >= sizeof(AVCRC)*1024)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
37 for (i = 0; i < 256; i++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
38 for(j=0; j<3; j++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
39 ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
40
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
41 return 0;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
42 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
43
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
44 uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
45 const uint8_t *end= buffer+length;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
46
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
47 if(!ctx[256])
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
48 while(buffer<end-3){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
49 crc ^= le2me_32(*(uint32_t*)buffer); buffer+=4;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
50 crc = ctx[3*256 + ( crc &0xFF)]
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
51 ^ctx[2*256 + ((crc>>8 )&0xFF)]
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
52 ^ctx[1*256 + ((crc>>16)&0xFF)]
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
53 ^ctx[0*256 + ((crc>>24) )];
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
54 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
55 while(buffer<end)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
56 crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
57
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
58 return crc;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
59 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
60
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
61 #ifdef TEST
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
62 #undef printf
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
63 main(){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
64 uint8_t buf[1999];
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
65 int i;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
66 int p[4][4]={{1, 32, 0xedb88320L, 0x3D5CDD04},
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
67 {0, 32, 0x04c11db7L, 0xC0F5BAE0},
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
68 {0, 16, 0x8005 , 0x1FBB },
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
69 {0, 8, 0x07 , 0xE3 },};
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
70 AVCRC ctx[1 ? 1024:257];
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
71
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
72 for(i=0; i<sizeof(buf); i++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
73 buf[i]= i+i*i;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
74
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
75 for(i=0; i<4; i++){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
76 av_crc_init(ctx, p[i][0], p[i][1], p[i][2], sizeof(ctx));
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
77 printf("crc %08X =%X\n", p[i][2], av_crc(ctx, 0, buf, sizeof(buf)));
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
78 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
79 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
80 #endif