Botan  1.11.15
src/lib/hash/checksum/adler32/adler32.cpp
Go to the documentation of this file.
00001 /*
00002 * Adler32
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/hash_utils.h>
00009 #include <botan/adler32.h>
00010 
00011 namespace Botan {
00012 
00013 BOTAN_REGISTER_HASH_NOARGS(Adler32);
00014 
00015 namespace {
00016 
00017 void adler32_update(const byte input[], size_t length,
00018                     u16bit& S1, u16bit& S2)
00019    {
00020    u32bit S1x = S1;
00021    u32bit S2x = S2;
00022 
00023    while(length >= 16)
00024       {
00025       S1x += input[ 0]; S2x += S1x;
00026       S1x += input[ 1]; S2x += S1x;
00027       S1x += input[ 2]; S2x += S1x;
00028       S1x += input[ 3]; S2x += S1x;
00029       S1x += input[ 4]; S2x += S1x;
00030       S1x += input[ 5]; S2x += S1x;
00031       S1x += input[ 6]; S2x += S1x;
00032       S1x += input[ 7]; S2x += S1x;
00033       S1x += input[ 8]; S2x += S1x;
00034       S1x += input[ 9]; S2x += S1x;
00035       S1x += input[10]; S2x += S1x;
00036       S1x += input[11]; S2x += S1x;
00037       S1x += input[12]; S2x += S1x;
00038       S1x += input[13]; S2x += S1x;
00039       S1x += input[14]; S2x += S1x;
00040       S1x += input[15]; S2x += S1x;
00041       input += 16;
00042       length -= 16;
00043       }
00044 
00045    for(size_t j = 0; j != length; ++j)
00046       {
00047       S1x += input[j];
00048       S2x += S1x;
00049       }
00050 
00051    S1 = S1x % 65521;
00052    S2 = S2x % 65521;
00053    }
00054 
00055 }
00056 
00057 /*
00058 * Update an Adler32 Checksum
00059 */
00060 void Adler32::add_data(const byte input[], size_t length)
00061    {
00062    const size_t PROCESS_AMOUNT = 5552;
00063 
00064    while(length >= PROCESS_AMOUNT)
00065       {
00066       adler32_update(input, PROCESS_AMOUNT, S1, S2);
00067       input += PROCESS_AMOUNT;
00068       length -= PROCESS_AMOUNT;
00069       }
00070 
00071    adler32_update(input, length, S1, S2);
00072    }
00073 
00074 /*
00075 * Finalize an Adler32 Checksum
00076 */
00077 void Adler32::final_result(byte output[])
00078    {
00079    store_be(output, S2, S1);
00080    clear();
00081    }
00082 
00083 }