annotate crc.c @ 108:11be8e0d1344 libavutil

Add official LGPL license headers to the files that were missing them.
author diego
date Sun, 10 Sep 2006 14:02:42 +0000
parents f85d07038450
children d76a36742464
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
108
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
1 /*
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
3 *
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
4 * This library is free software; you can redistribute it and/or
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
5 * modify it under the terms of the GNU Lesser General Public
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
6 * License as published by the Free Software Foundation; either
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
7 * version 2 of the License, or (at your option) any later version.
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
8 *
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
9 * This library is distributed in the hope that it will be useful,
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
12 * Lesser General Public License for more details.
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
13 *
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
14 * You should have received a copy of the GNU Lesser General Public
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
15 * License along with this library; if not, write to the Free Software
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
17 */
11be8e0d1344 Add official LGPL license headers to the files that were missing them.
diego
parents: 61
diff changeset
18
30
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
19 #include "common.h"
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
20 #include "crc.h"
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
21
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
22 AVCRC *av_crcEDB88320;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
23 AVCRC *av_crc04C11DB7;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
24 AVCRC *av_crc8005 ;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
25 AVCRC *av_crc07 ;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
26
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
27 /**
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
28 * Inits a crc table.
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
29 * @param ctx must be an array of sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
30 * @param cts_size size of ctx in bytes
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
31 * @return <0 on failure
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
32 */
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
33 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
34 int i, j;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
35 uint32_t c;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
36
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
37 if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
38 return -1;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
39 if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
40 return -1;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
41
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
42 for (i = 0; i < 256; i++) {
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
43 if (le) {
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
44 for (c = i, j = 0; j < 8; j++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
45 c = (c>>1)^(poly & (-(c&1)));
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
46 ctx[i] = c;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
47 } else {
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
48 for (c = i << 24, j = 0; j < 8; j++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
49 c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
50 ctx[i] = bswap_32(c);
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
51 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
52 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
53 ctx[256]=1;
61
f85d07038450 put the code which is specific for the large crc table under #ifndef CONFIG_SMALL
michael
parents: 30
diff changeset
54 #ifndef CONFIG_SMALL
30
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
55 if(ctx_size >= sizeof(AVCRC)*1024)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
56 for (i = 0; i < 256; i++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
57 for(j=0; j<3; j++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
58 ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
61
f85d07038450 put the code which is specific for the large crc table under #ifndef CONFIG_SMALL
michael
parents: 30
diff changeset
59 #endif
30
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
60
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
61 return 0;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
62 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
63
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
64 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
65 const uint8_t *end= buffer+length;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
66
61
f85d07038450 put the code which is specific for the large crc table under #ifndef CONFIG_SMALL
michael
parents: 30
diff changeset
67 #ifndef CONFIG_SMALL
30
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
68 if(!ctx[256])
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
69 while(buffer<end-3){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
70 crc ^= le2me_32(*(uint32_t*)buffer); buffer+=4;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
71 crc = ctx[3*256 + ( crc &0xFF)]
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
72 ^ctx[2*256 + ((crc>>8 )&0xFF)]
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
73 ^ctx[1*256 + ((crc>>16)&0xFF)]
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
74 ^ctx[0*256 + ((crc>>24) )];
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
75 }
61
f85d07038450 put the code which is specific for the large crc table under #ifndef CONFIG_SMALL
michael
parents: 30
diff changeset
76 #endif
30
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
77 while(buffer<end)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
78 crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
79
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
80 return crc;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
81 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
82
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
83 #ifdef TEST
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
84 #undef printf
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
85 main(){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
86 uint8_t buf[1999];
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
87 int i;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
88 int p[4][4]={{1, 32, 0xedb88320L, 0x3D5CDD04},
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
89 {0, 32, 0x04c11db7L, 0xC0F5BAE0},
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
90 {0, 16, 0x8005 , 0x1FBB },
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
91 {0, 8, 0x07 , 0xE3 },};
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
92 AVCRC ctx[1 ? 1024:257];
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
93
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
94 for(i=0; i<sizeof(buf); i++)
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
95 buf[i]= i+i*i;
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
96
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
97 for(i=0; i<4; i++){
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
98 av_crc_init(ctx, p[i][0], p[i][1], p[i][2], sizeof(ctx));
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
99 printf("crc %08X =%X\n", p[i][2], av_crc(ctx, 0, buf, sizeof(buf)));
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
100 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
101 }
664e2d2b92b9 generic crc calculation code
michael
parents:
diff changeset
102 #endif