clsync
calc.c
Go to the documentation of this file.
1 /*
2  clsync - file tree sync utility based on inotify/kqueue
3 
4  Copyright (C) 2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "common.h"
21 
22 #include "calc.h"
23 #include "error.h"
24 
25 #ifdef HAVE_MHASH
26 #include <mhash.h>
27 #endif
28 
29 #ifndef HAVE_MHASH
30 /**
31  * @brief Calculated Adler32 value for char array
32  *
33  * @param[in] data Pointer to data
34  * @param[in] len Length of the data
35  *
36  * @retval uint32_t Adler32 value of data
37  *
38  */
39 
40 // Copied from http://en.wikipedia.org/wiki/Adler-32
41 uint32_t adler32_calc ( const unsigned char *const data, uint32_t len ) // where data is the location of the data in physical
42 {
43  // memory and len is the length of the data in bytes
44  /*
45  if (len&3)
46  warning("len [%i] & 3 == %i != 0. Wrong length (not a multiple of 4).", len, len&3);
47  */
48  debug ( 70, "%p, %i", data, len );
49  const int MOD_ADLER = 65521;
50  uint32_t a = 1, b = 0;
51  uint32_t index;
52 
53  // Process each byte of the data in order
54  for ( index = 0; index < len; ++index ) {
55  debug ( 80, "%5i: %02x %02x %02x", index, data[index], a, b );
56  a = ( a + data[index] ) % MOD_ADLER;
57  b = ( b + a ) % MOD_ADLER;
58  }
59 
60  return ( b << 16 ) | a;
61 }
62 #endif
63 
adler32_calc
uint32_t adler32_calc(const unsigned char *const data, uint32_t len)
Calculated Adler32 value for char array.
Definition: calc.c:41
calc.h
error.h
debug
#define debug(debug_level,...)
Definition: error.h:50
common.h