unrarsrc  6.1.7
About: unrar extracts, views and tests the contents of archives created with the RAR archiver.
  Fossies Dox: unrarsrc-6.1.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

crc.cpp
Go to the documentation of this file.
1// This CRC function is based on Intel Slicing-by-8 algorithm.
2//
3// Original Intel Slicing-by-8 code is available here:
4//
5// http://sourceforge.net/projects/slicing-by-8/
6//
7// Original Intel Slicing-by-8 code is licensed as:
8//
9// Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved
10//
11// This software program is licensed subject to the BSD License,
12// available at http://www.opensource.org/licenses/bsd-license.html
13
14
15#include "rar.hpp"
16
17#ifndef SFX_MODULE
18// User suggested to avoid BSD license in SFX module, so they do not need
19// to include the license to SFX archive.
20#define USE_SLICING
21#endif
22
23static uint crc_tables[8][256]; // Tables for Slicing-by-8.
24
25
26// Build the classic CRC32 lookup table.
27// We also provide this function to legacy RAR and ZIP decryption code.
29{
30 if (CRCTab[1]!=0)
31 return;
32 for (uint I=0;I<256;I++)
33 {
34 uint C=I;
35 for (uint J=0;J<8;J++)
36 C=(C & 1) ? (C>>1)^0xEDB88320 : (C>>1);
37 CRCTab[I]=C;
38 }
39}
40
41
42static void InitTables()
43{
45
46#ifdef USE_SLICING
47 for (uint I=0;I<256;I++) // Build additional lookup tables.
48 {
49 uint C=crc_tables[0][I];
50 for (uint J=1;J<8;J++)
51 {
52 C=crc_tables[0][(byte)C]^(C>>8);
53 crc_tables[J][I]=C;
54 }
55 }
56#endif
57}
58
59
61
62uint CRC32(uint StartCRC,const void *Addr,size_t Size)
63{
64 byte *Data=(byte *)Addr;
65
66#ifdef USE_SLICING
67 // Align Data to 8 for better performance.
68 for (;Size>0 && ((size_t)Data & 7);Size--,Data++)
69 StartCRC=crc_tables[0][(byte)(StartCRC^Data[0])]^(StartCRC>>8);
70
71 for (;Size>=8;Size-=8,Data+=8)
72 {
73#ifdef BIG_ENDIAN
74 StartCRC ^= Data[0]|(Data[1] << 8)|(Data[2] << 16)|(Data[3] << 24);
75 uint NextData = Data[4]|(Data[5] << 8)|(Data[6] << 16)|(Data[7] << 24);
76#else
77 StartCRC ^= *(uint32 *) Data;
78 uint NextData = *(uint32 *) (Data+4);
79#endif
80 StartCRC = crc_tables[7][(byte) StartCRC ] ^
81 crc_tables[6][(byte)(StartCRC >> 8) ] ^
82 crc_tables[5][(byte)(StartCRC >> 16)] ^
83 crc_tables[4][(byte)(StartCRC >> 24)] ^
84 crc_tables[3][(byte) NextData ] ^
85 crc_tables[2][(byte)(NextData >> 8) ] ^
86 crc_tables[1][(byte)(NextData >> 16)] ^
87 crc_tables[0][(byte)(NextData >> 24)];
88 }
89#endif
90
91 for (;Size>0;Size--,Data++) // Process left data.
92 StartCRC=crc_tables[0][(byte)(StartCRC^Data[0])]^(StartCRC>>8);
93
94 return StartCRC;
95}
96
97
98#ifndef SFX_MODULE
99// For RAR 1.4 archives in case somebody still has them.
100ushort Checksum14(ushort StartCRC,const void *Addr,size_t Size)
101{
102 byte *Data=(byte *)Addr;
103 for (size_t I=0;I<Size;I++)
104 {
105 StartCRC=(StartCRC+Data[I])&0xffff;
106 StartCRC=((StartCRC<<1)|(StartCRC>>15))&0xffff;
107 }
108 return StartCRC;
109}
110#endif
111
112
static void InitTables()
Definition: crc.cpp:42
struct CallInitCRC CallInit32
uint CRC32(uint StartCRC, const void *Addr, size_t Size)
Definition: crc.cpp:62
void InitCRC32(uint *CRCTab)
Definition: crc.cpp:28
static uint crc_tables[8][256]
Definition: crc.cpp:23
ushort Checksum14(ushort StartCRC, const void *Addr, size_t Size)
Definition: crc.cpp:100
uint CRCTab[256]
unsigned int uint
Definition: rartypes.hpp:8
uint16_t ushort
Definition: rartypes.hpp:7
uint8_t byte
Definition: rartypes.hpp:6
uint32_t uint32
Definition: rartypes.hpp:9
CallInitCRC()
Definition: crc.cpp:60