Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

adler32.cpp

00001 // adler32.cpp - written and placed in the public domain by Wei Dai 00002 00003 #include "pch.h" 00004 #include "adler32.h" 00005 00006 NAMESPACE_BEGIN(CryptoPP) 00007 00008 void Adler32::Update(const byte *input, unsigned int length) 00009 { 00010 const unsigned long BASE = 65521; 00011 00012 unsigned long s1 = m_s1; 00013 unsigned long s2 = m_s2; 00014 00015 if (length % 8 != 0) 00016 { 00017 do 00018 { 00019 s1 += *input++; 00020 s2 += s1; 00021 length--; 00022 } while (length % 8 != 0); 00023 00024 if (s1 >= BASE) 00025 s1 -= BASE; 00026 s2 %= BASE; 00027 } 00028 00029 while (length > 0) 00030 { 00031 s1 += input[0]; s2 += s1; 00032 s1 += input[1]; s2 += s1; 00033 s1 += input[2]; s2 += s1; 00034 s1 += input[3]; s2 += s1; 00035 s1 += input[4]; s2 += s1; 00036 s1 += input[5]; s2 += s1; 00037 s1 += input[6]; s2 += s1; 00038 s1 += input[7]; s2 += s1; 00039 00040 length -= 8; 00041 input += 8; 00042 00043 if (s1 >= BASE) 00044 s1 -= BASE; 00045 if (length % 0x8000 == 0) 00046 s2 %= BASE; 00047 } 00048 00049 assert(s1 < BASE); 00050 assert(s2 < BASE); 00051 00052 m_s1 = (word16)s1; 00053 m_s2 = (word16)s2; 00054 } 00055 00056 void Adler32::TruncatedFinal(byte *hash, unsigned int size) 00057 { 00058 ThrowIfInvalidTruncatedSize(size); 00059 00060 switch (size) 00061 { 00062 default: 00063 hash[3] = byte(m_s1); 00064 case 3: 00065 hash[2] = byte(m_s1 >> 8); 00066 case 2: 00067 hash[1] = byte(m_s2); 00068 case 1: 00069 hash[0] = byte(m_s2 >> 8); 00070 case 0: 00071 ; 00072 } 00073 00074 Reset(); 00075 } 00076 00077 NAMESPACE_END

Generated on Wed Jul 21 19:15:19 2004 for Crypto++ by doxygen 1.3.7-20040704