-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabinpoly.c
More file actions
259 lines (222 loc) · 5.92 KB
/
Copy pathrabinpoly.c
File metadata and controls
259 lines (222 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// $Id$
/*
* Copyright (C) 2024 Michael Stroucken (mxs@cmu.edu)
* Copyright (C) 2013 Pavan Kumar Alampalli (pavankumar@cmu.edu)
* Copyright (C) 2004 Hyang-Ah Kim (hakim@cs.cmu.edu)
* Copyright (C) 1999 David Mazieres (dm@uun.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#include <stdlib.h>
#include <stdio.h>
#include "rabinpoly.h"
#include "msb.h"
#define INT64(n) n##LL
#define MSB64 INT64(0x8000000000000000)
#define DEFAULT_WINDOW_SIZE 32
/* Fingerprint value take from LBFS fingerprint.h. For detail on this,
* refer to the original rabin fingerprint paper.
*/
#define FINGERPRINT_PT 0xbfe6b8a5bf378d83LL
static u_int64_t polymod (u_int64_t nh, u_int64_t nl, u_int64_t d);
static void polymult (u_int64_t *php, u_int64_t *plp, u_int64_t x, u_int64_t y);
static u_int64_t polymmult (u_int64_t x, u_int64_t y, u_int64_t d);
static void calcT(rabinpoly_t *rp);
static inline u_int64_t slide8(rabinpoly_t *rp, unsigned char m);
static inline u_int64_t append8(rabinpoly_t *rp, u_int64_t p, unsigned char m);
/**
* functions to calculate the rabin hash
*/
static u_int64_t
polymod (u_int64_t nh, u_int64_t nl, u_int64_t d)
{
int i;
int k = fls64 (d) - 1;
d <<= 63 - k;
if (nh) {
if (nh & MSB64)
nh ^= d;
for (i = 62; i >= 0; i--)
if (nh & ((u_int64_t) 1) << i) {
nh ^= d >> (63 - i);
nl ^= d << (i + 1);
}
}
for (i = 63; i >= k; i--)
{
if (nl & INT64 (1) << i)
nl ^= d >> (63 - i);
}
return nl;
}
static void
polymult (u_int64_t *php, u_int64_t *plp, u_int64_t x, u_int64_t y)
{
int i;
u_int64_t ph = 0, pl = 0;
if (x & 1)
pl = y;
for (i = 1; i < 64; i++)
if (x & (INT64 (1) << i)) {
ph ^= y >> (64 - i);
pl ^= y << i;
}
if (php)
*php = ph;
if (plp)
*plp = pl;
}
static u_int64_t
polymmult (u_int64_t x, u_int64_t y, u_int64_t d)
{
u_int64_t h, l;
polymult (&h, &l, x, y);
return polymod (h, l, d);
}
/**
* Initialize the T[] and U[] array for faster computation of rabin fingerprint
* Called only once from rabin_init() during initialization
*/
static void calcT(rabinpoly_t *rp)
{
unsigned int i;
int xshift = fls64 (rp->poly) - 1;
rp->shift = xshift - 8;
u_int64_t T1 = polymod (0, INT64 (1) << xshift, rp->poly);
for (i = 0; i < 256; i++) {
rp->T[i] = polymmult (i, T1, rp->poly) | ((u_int64_t) i << xshift);
}
u_int64_t sizeshift = 1;
for (i = 1; i < rp->window_size; i++) {
sizeshift = append8 (rp, sizeshift, 0);
}
for (i = 0; i < 256; i++) {
rp->U[i] = polymmult (i, sizeshift, rp->poly);
}
}
/**
* Feed a new byte into the rabin sliding window and update
* the rabin fingerprint
*/
static inline u_int64_t slide8(rabinpoly_t *rp, unsigned char m)
{
rp->bufpos++;
if (rp->bufpos >= rp->window_size) {
rp->bufpos = 0;
}
unsigned char om = rp->buf[rp->bufpos];
rp->buf[rp->bufpos] = m;
return rp->fingerprint = append8 (rp, rp->fingerprint ^ rp->U[om], m);
}
static inline u_int64_t append8(rabinpoly_t *rp, u_int64_t p, unsigned char m)
{
return ((p << 8) | m) ^ rp->T[p >> rp->shift];
}
/**
* Interface functions exposed by the library
*/
rabinpoly_t *rabin_init(unsigned int window_size,
unsigned int avg_segment_size,
unsigned int min_segment_size,
unsigned int max_segment_size)
{
rabinpoly_t *rp;
if (!min_segment_size || !avg_segment_size || !max_segment_size ||
(min_segment_size > avg_segment_size) ||
(max_segment_size < avg_segment_size) ||
(window_size < DEFAULT_WINDOW_SIZE)) {
return NULL;
}
rp = (rabinpoly_t *)malloc(sizeof(rabinpoly_t));
if (!rp) {
return NULL;
}
rp->poly = FINGERPRINT_PT;
rp->window_size = window_size;;
rp->avg_segment_size = avg_segment_size;
rp->min_segment_size = min_segment_size;
rp->max_segment_size = max_segment_size;
rp->fingerprint_mask = (1 << (fls32(rp->avg_segment_size)-1))-1;
rp->fingerprint = 0;
rp->bufpos = -1;
rp->cur_seg_size = 0;
if (min_segment_size > window_size) {
rp->new_undersize = min_segment_size - window_size;
} else {
rp->new_undersize = 0;
}
rp->undersize = rp->new_undersize;
calcT(rp);
rp->buf = (unsigned char *)malloc(rp->window_size*sizeof(unsigned char));
if (!rp->buf){
return NULL;
}
memset(rp->buf, 0, rp->window_size*sizeof (unsigned char));
return rp;
}
int rabin_segment_next(rabinpoly_t *rp,
const char *buf,
unsigned int bytes,
int *is_new_segment)
{
unsigned int i;
if (!rp || !buf || !is_new_segment) {
return -1;
}
*is_new_segment = 0;
i = 0;
if (bytes > rp->undersize) {
i += rp->undersize;
rp->cur_seg_size += rp->undersize;
rp->undersize = 0;
} else {
i = bytes;
rp->cur_seg_size += bytes;
rp->undersize -= bytes;
}
for (; i < bytes; i++) {
slide8(rp, buf[i]);
rp->cur_seg_size++;
if (rp->cur_seg_size < rp->min_segment_size) {
continue;
}
if(((rp->fingerprint & rp->fingerprint_mask) == 0)
|| (rp->cur_seg_size == rp->max_segment_size)) {
*is_new_segment = 1;
rp->cur_seg_size = 0;
rp->undersize = rp->new_undersize;
return i+1;
}
}
return i;
}
void rabin_reset(rabinpoly_t *rp) {
rp->fingerprint = 0;
rp->bufpos = -1;
rp->cur_seg_size = 0;
rp->undersize = rp->new_undersize;
memset(rp->buf, 0, rp->window_size*sizeof (unsigned char));
}
void rabin_free(rabinpoly_t **p_rp)
{
if (!p_rp || !*p_rp) {
return;
}
free((*p_rp)->buf);
free(*p_rp);
*p_rp = NULL;
}