1 /* 7zCrc.c -- CRC32 init
2 2017-06-06 : Igor Pavlov : Public domain */
9 #define kCrcPoly 0xEDB88320
12 #define CRC_NUM_TABLES 8
14 #define CRC_NUM_TABLES 9
16 #define CRC_UINT32_SWAP(v) ((v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | (v << 24))
18 UInt32 MY_FAST_CALL
CrcUpdateT1_BeT4(UInt32 v
, const void *data
, size_t size
, const UInt32
*table
);
19 UInt32 MY_FAST_CALL
CrcUpdateT1_BeT8(UInt32 v
, const void *data
, size_t size
, const UInt32
*table
);
23 UInt32 MY_FAST_CALL
CrcUpdateT4(UInt32 v
, const void *data
, size_t size
, const UInt32
*table
);
24 UInt32 MY_FAST_CALL
CrcUpdateT8(UInt32 v
, const void *data
, size_t size
, const UInt32
*table
);
27 typedef UInt32 (MY_FAST_CALL
*CRC_FUNC
)(UInt32 v
, const void *data
, size_t size
, const UInt32
*table
);
29 CRC_FUNC g_CrcUpdateT4
;
30 CRC_FUNC g_CrcUpdateT8
;
33 UInt32 g_CrcTable
[256 * CRC_NUM_TABLES
];
35 UInt32 MY_FAST_CALL
CrcUpdate(UInt32 v
, const void *data
, size_t size
)
37 return g_CrcUpdate(v
, data
, size
, g_CrcTable
);
40 UInt32 MY_FAST_CALL
CrcCalc(const void *data
, size_t size
)
42 return g_CrcUpdate(CRC_INIT_VAL
, data
, size
, g_CrcTable
) ^ CRC_INIT_VAL
;
45 #define CRC_UPDATE_BYTE_2(crc, b) (table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8))
47 UInt32 MY_FAST_CALL
CrcUpdateT1(UInt32 v
, const void *data
, size_t size
, const UInt32
*table
)
49 const Byte
*p
= (const Byte
*)data
;
50 const Byte
*pEnd
= p
+ size
;
51 for (; p
!= pEnd
; p
++)
52 v
= CRC_UPDATE_BYTE_2(v
, *p
);
56 void MY_FAST_CALL
CrcGenerateTable()
59 for (i
= 0; i
< 256; i
++)
63 for (j
= 0; j
< 8; j
++)
64 r
= (r
>> 1) ^ (kCrcPoly
& ((UInt32
)0 - (r
& 1)));
67 for (i
= 256; i
< 256 * CRC_NUM_TABLES
; i
++)
69 UInt32 r
= g_CrcTable
[(size_t)i
- 256];
70 g_CrcTable
[i
] = g_CrcTable
[r
& 0xFF] ^ (r
>> 8);
73 #if CRC_NUM_TABLES < 4
75 g_CrcUpdate
= CrcUpdateT1
;
81 g_CrcUpdateT4
= CrcUpdateT4
;
82 g_CrcUpdate
= CrcUpdateT4
;
84 #if CRC_NUM_TABLES >= 8
85 g_CrcUpdateT8
= CrcUpdateT8
;
87 #ifdef MY_CPU_X86_OR_AMD64
88 if (!CPU_Is_InOrder())
90 g_CrcUpdate
= CrcUpdateT8
;
96 UInt32 k
= 0x01020304;
97 const Byte
*p
= (const Byte
*)&k
;
98 if (p
[0] == 4 && p
[1] == 3)
100 g_CrcUpdateT4
= CrcUpdateT4
;
101 g_CrcUpdate
= CrcUpdateT4
;
102 #if CRC_NUM_TABLES >= 8
103 g_CrcUpdateT8
= CrcUpdateT8
;
104 g_CrcUpdate
= CrcUpdateT8
;
107 else if (p
[0] != 1 || p
[1] != 2)
108 g_CrcUpdate
= CrcUpdateT1
;
112 for (i
= 256 * CRC_NUM_TABLES
- 1; i
>= 256; i
--)
114 UInt32 x
= g_CrcTable
[(size_t)i
- 256];
115 g_CrcTable
[i
] = CRC_UINT32_SWAP(x
);
117 g_CrcUpdateT4
= CrcUpdateT1_BeT4
;
118 g_CrcUpdate
= CrcUpdateT1_BeT4
;
119 #if CRC_NUM_TABLES >= 8
120 g_CrcUpdateT8
= CrcUpdateT1_BeT8
;
121 g_CrcUpdate
= CrcUpdateT1_BeT8
;