"Fossies" - the Fresh Open Source Software Archive

Member "unrar/crc.cpp" (4 May 2022, 2867 Bytes) of package /linux/misc/unrarsrc-6.1.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "crc.cpp" see the Fossies "Dox" file reference documentation.

    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 
   23 static 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.
   28 void InitCRC32(uint *CRCTab)
   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 
   42 static void InitTables()
   43 {
   44   InitCRC32(crc_tables[0]);
   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 
   60 struct CallInitCRC {CallInitCRC() {InitTables();}} static CallInit32;
   61 
   62 uint 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.
  100 ushort 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