CRC.xs

filter: Statementstmt 87.1% 61 / 70 Branchbran 71.4% 30 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.2% 91 / 112 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/String/CRC.pm »
1 /*
2 Perl Extension for CRC computations
3 */
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 /*
10 * From Matt Dillon's Diablo package. Used with permission.
11 */
12  
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17  
18 typedef struct crc_hash_t {
19 U32 h1;
20 U32 h2;
21 } crc_hash_t;
22  
23 typedef struct crc_Hash {
24 struct crc_Hash *ha_Next;
25 crc_hash_t ha_Hv;
26 } crc_Hash;
27  
28 int crc_HashLimit = 0;
29  
30 /*
31 * Poly: 0x00600340.00F0D50A
32 */
33  
34 #define HINIT1 0xFAC432B1UL
35 #define HINIT2 0x0CD5E44AUL
36  
37 #define POLY1 0x00600340UL
38 #define POLY2 0x00F0D50BUL
39  
40 crc_hash_t CrcXor[256];
41 crc_hash_t Poly[64+1];
42  
43 void
44 6 crc_init(void)
45 {
46 int i;
47  
48 /*
49 * Polynomials to use for various crc sizes. Start with the 64 bit
50 * polynomial and shift it right to generate the polynomials for fewer
51 * bits. Note that the polynomial for N bits has no bit set above N-8.
52 * This allows us to do a simple table-driven CRC.
53 */
54  
55 6 Poly[64].h1 = POLY1;
56 6 Poly[64].h2 = POLY2;
57 294 ▶ for (i = 63; i >= 16; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 63; i >= 16; --i) { 288 6 294
58 288 Poly[i].h1 = Poly[i+1].h1 >> 1;
59 288 Poly[i].h2 = (Poly[i+1].h2 >> 1) | ((Poly[i+1].h1 & 1) << 31) | 1;
60 }
61  
62 1542 ▶ for (i = 0; i < 256; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; ++i) { 1536 6 1542
63 int j;
64 1536 int v = i;
65 1536 crc_hash_t hv = { 0, 0 };
66  
67 13824 ▶ for (j = 0; j < 8; ++j, (v <<= 1)) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 8; ++j, (v <<= 1)) { 12288 1536 13824
68 12288 hv.h1 <<= 1;
69 12288 ▶ if (hv.h2 & 0x80000000UL)
Branch: 1 branch
BranchTrueFalseTotal
if (hv.h2 & 0x80000000UL) 128 12160 12288
70 128 hv.h1 |= 1;
71 12288 hv.h2 = (hv.h2 << 1);
72 12288 ▶ if (v & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (v & 0x80) { 6144 6144 12288
73 6144 hv.h1 ^= Poly[crc_HashLimit].h1;
74 6144 hv.h2 ^= Poly[crc_HashLimit].h2;
75 }
76 }
77 1536 CrcXor[i] = hv;
78 }
79 6 }
80  
81 /*
82 * testhash() - do the CRC. The complexity is simply due to the programmable
83 * nature of the number of bits. We extract the top 8 bits to
84 * use as a table lookup to obtain the polynomial XOR 8 bits at
85 * a time rather then 1 bit at a time.
86 */
87  
88 crc_hash_t
89 6 crc_calculate(char *p, int len)
90 {
91 6 crc_hash_t hv = { HINIT1, HINIT2 };
92 6 char *e = p + len;
93  
94 6 ▶ if (crc_HashLimit <= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (crc_HashLimit <= 32) { 3 3 6
95 3 int s = crc_HashLimit - 8;
96 3 U32 m = (U32)-1 >> (32 - crc_HashLimit);
97  
98 3 hv.h1 = 0;
99 3 hv.h2 &= m;
100  
101 72 ▶ while (p < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { 69 3 72
102 69 int i = (hv.h2 >> s) & 255;
103 /* printf("i = %d %08lx\n", i, CrcXor[i].h2); */
104 69 hv.h2 = ((hv.h2 << 8) & m) ^ *p ^ CrcXor[i].h2;
105 69 ++p;
106 }
107 3 ▶ } else if (crc_HashLimit < 32+8) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (crc_HashLimit < 32+8) { 0 3 3
108 0 int s2 = 32 + 8 - crc_HashLimit; /* bits in byte from h2 */
109 0 U32 m = (U32)-1 >> (64 - crc_HashLimit);
110  
111 0 hv.h1 &= m;
112 0 ▶ while (p < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { 0 0 0
113 0 int i = ((hv.h1 << s2) | (hv.h2 >> (32 - s2))) & 255;
114 0 hv.h1 = (((hv.h1 << 8) ^ (int)(hv.h2 >> 24)) & m) ^ CrcXor[i].h1;
115 0 hv.h2 = (hv.h2 << 8) ^ *p ^ CrcXor[i].h2;
116 0 ++p;
117 }
118 } else {
119 3 int s = crc_HashLimit - 40;
120 3 U32 m = (U32)-1 >> (64 - crc_HashLimit);
121  
122 3 hv.h1 &= m;
123 72 ▶ while (p < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { 69 3 72
124 69 int i = (hv.h1 >> s) & 255;
125 69 hv.h1 = ((hv.h1 << 8) & m) ^ (int)(hv.h2 >> 24) ^ CrcXor[i].h1;
126 69 hv.h2 = (hv.h2 << 8) ^ *p ^ CrcXor[i].h2;
127 69 ++p;
128 }
129 }
130 /* printf("%08lx.%08lx\n", (long)hv.h1, (long)hv.h2); */
131 6 return(hv);
132 }
133  
134  
135 MODULE = String::CRC PACKAGE = String::CRC
136  
137 VERSIONCHECK: DISABLE
138  
139 void
140 crc(data,bits=32)
141 PREINIT:
142 int data_len;
143 INPUT:
144 char *data = (char *)SvPV(ST(0),data_len);
145 int bits;
146 PPCODE:
147 {
148 crc_hash_t h;
149 U32 *rv;
150 SV *sv;
151  
152 6 ▶ if (bits < 16 || bits > 64) {
Branch: 2 branches
BranchTrueFalseTotal
if (bits < 16 || bits > 64) { 6 0 6
if (bits < 16 || bits > 64) { 0 6 6
153 0 croak("String::CRC bits must be >= 16 and <= 64");
154 }
155 6 ▶ if (bits != crc_HashLimit) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits != crc_HashLimit) { 6 0 6
156 6 crc_HashLimit = bits;
157 6 crc_init();
158 }
159 6 h = crc_calculate(data, data_len);
160 6 ▶ if (bits > 32 && GIMME == G_ARRAY) {
Branch: 4 branches
BranchTrueFalseTotal
if (bits > 32 && GIMME == G_ARRAY) { 3 3 6
if (bits > 32 && GIMME == G_ARRAY) { 3 0 3
if (bits > 32 && GIMME == G_ARRAY) { 2 1 3
if (bits > 32 && GIMME == G_ARRAY) { 0 0 0
161 2 ▶ EXTEND(sp, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(sp, 2); 0 2 2
162 2 sv = newSV(0);
163 2 sv_setuv(sv, (UV)h.h1);
164 2 PUSHs(sv_2mortal(sv));
165 2 sv = newSV(0);
166 2 sv_setuv(sv, (UV)h.h2);
167 2 PUSHs(sv_2mortal(sv));
168 4 ▶ } else if (bits > 32) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (bits > 32) { 1 3 4
169 /*
170 * problem. how to return 64 bits in 32?
171 * answer: as a string.
172 * U32 better be == 4 bytes!
173 */
174 1 ▶ EXTEND(sp, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(sp, 1); 0 1 1
175 1 PUSHs(sv_2mortal(newSVpv((char *)&h, 8)));
176 if (sizeof(U32) != 4) croak("U32 not four bytes!");
177 } else {
178 3 ▶ EXTEND(sp, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(sp, 1); 0 3 3
179 3 sv = newSV(0);
180 3 sv_setuv(sv, (UV)h.h2);
181 3 PUSHs(sv_2mortal(sv));
182 /* PUSHs(sv_2mortal(newSViv(h.h2)));*/
183 }
184 }
185  
186  
↑ Summary blib/lib/String/CRC.pm »