$treeview $search $mathjax $extrastylesheet
librsync
2.3.0
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * rollsum -- the librsync rolling checksum 00004 * 00005 * Copyright (C) 2003 by Donovan Baarda <abo@minkirri.apana.org.au> 00006 * based on work, Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net> 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation; either version 2.1 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00021 */ 00022 #ifndef _ROLLSUM_H_ 00023 # define _ROLLSUM_H_ 00024 00025 # include <stddef.h> 00026 # include <stdint.h> 00027 00028 /* We should make this something other than zero to improve the checksum 00029 algorithm: tridge suggests a prime number. */ 00030 # define ROLLSUM_CHAR_OFFSET 31 00031 00032 /** The Rollsum struct type \private. */ 00033 typedef struct _Rollsum { 00034 size_t count; /* count of bytes included in sum */ 00035 uint_fast16_t s1; /* s1 part of sum */ 00036 uint_fast16_t s2; /* s2 part of sum */ 00037 } Rollsum; 00038 00039 void RollsumUpdate(Rollsum *sum, const unsigned char *buf, size_t len); 00040 00041 /* static inline implementations of simple routines */ 00042 00043 static inline void RollsumInit(Rollsum *sum) 00044 { 00045 sum->count = sum->s1 = sum->s2 = 0; 00046 } 00047 00048 static inline void RollsumRotate(Rollsum *sum, unsigned char out, 00049 unsigned char in) 00050 { 00051 sum->s1 += in - out; 00052 sum->s2 += sum->s1 - sum->count * (out + ROLLSUM_CHAR_OFFSET); 00053 } 00054 00055 static inline void RollsumRollin(Rollsum *sum, unsigned char in) 00056 { 00057 sum->s1 += in + ROLLSUM_CHAR_OFFSET; 00058 sum->s2 += sum->s1; 00059 sum->count++; 00060 } 00061 00062 static inline void RollsumRollout(Rollsum *sum, unsigned char out) 00063 { 00064 sum->s1 -= out + ROLLSUM_CHAR_OFFSET; 00065 sum->s2 -= sum->count * (out + ROLLSUM_CHAR_OFFSET); 00066 sum->count--; 00067 } 00068 00069 static inline uint32_t RollsumDigest(Rollsum *sum) 00070 { 00071 return ((uint32_t)sum->s2 << 16) | ((uint32_t)sum->s1 & 0xffff); 00072 } 00073 00074 #endif /* _ROLLSUM_H_ */