File: find_stream_match.c

package info (click to toggle)
rzip 2.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 320 kB
  • ctags: 249
  • sloc: ansic: 1,888; sh: 575; makefile: 86
file content (353 lines) | stat: -rw-r--r-- 8,274 bytes parent folder | download | duplicates (3)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*  Example piece of code to demonstrate how to find a match in a
    stream, without buffering, for use in rzip on *really* huge files.
    
    Copyright (C) 2003  Rusty Russell, IBM Corporation

    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 of the License, 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 <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include "md4.h"

struct hash_entry {
	uint64_t offset;
	uchar bitness;
	uchar md4[MD4_DIGEST_SIZE];
};

typedef uint32_t tag;

#define HASH_BITS 10
#define MINIMUM_BITNESS 1
#define MAXIMUM_BITNESS 32 /* need to increase tag size to increase this */
#define WINDOW_LENGTH 31

/* Simple optimization for read() */
#define BUFFER_SIZE 4096

static struct hash_entry hash[1 << HASH_BITS];
static const unsigned int hash_limit = (1 << HASH_BITS) * 2 / 3;
static unsigned int hash_min_bitness = MINIMUM_BITNESS;
static unsigned int hash_count;
static unsigned int hash_clean_ptr;
static tag hash_index[256];
static int verbosity = 2;

static void barf_perror(const char *fmt, ...)
{
	char *str = strerror(errno);
	va_list arglist;

	fprintf(stderr, "FATAL: ");

	va_start(arglist, fmt);
	vfprintf(stderr, fmt, arglist);
	va_end(arglist);

	fprintf(stderr, ": %s\n", str);
	exit(1);
}

static int bitness(tag t)
{
	return ffs(t) ?: 32;
}

static int empty_hash(unsigned int h)
{
	return !hash[h].bitness;
}

static uint32_t primary_hash(const struct hash_entry *h)
{
	return h->md4[1] & ((1 << HASH_BITS) - 1);
}

static inline int hash_equals(const struct hash_entry *a,
			      const struct hash_entry *b)
{
	return memcmp(a->md4, b->md4, sizeof(a->md4)) == 0;
}

/* Eliminate one hash entry of minimum bitness. */
static void clean_one_from_hash(void)
{
again:
	if (verbosity > 1) {
		if (!hash_clean_ptr)
			printf("Starting sweep for mask %u\n",
			       hash_min_bitness);
	}

	for (; hash_clean_ptr < (1<<HASH_BITS); hash_clean_ptr++) {
		if (empty_hash(hash_clean_ptr))
			continue;
		if (hash[hash_clean_ptr].bitness <= hash_min_bitness) {
			hash[hash_clean_ptr].bitness = 0;
			hash_count--;
			return;
		}
	}

	/* We hit the end: everything in hash satisfies the better mask. */
	hash_min_bitness++;
	hash_clean_ptr = 0;
	goto again;
}

/* If we find a duplicate, return that instead of inserting. */
static struct hash_entry *insert_hash(const struct hash_entry *new)
{
	unsigned int h;

	/* If hash bucket is taken, we spill into next bucket(s).
	   Secondary hashing works better in theory, but modern caches
	   make this 20% faster. */

	h = primary_hash(new);
	while (!empty_hash(h)) {
		if (hash_equals(&hash[h], new))
			return &hash[h];

		/* If this due for cleaning anyway, just replace it:
		   rehashing might move it behind tag_clean_ptr. */
		if (hash[h].bitness == hash_min_bitness) {
			hash_count--;
			break;
		}
		/* If we are better than current occupant, we can't
		   jump over it: it will be cleaned before us, and
		   noone would then find us in the hash table.  Rehash
		   it, and take its place. */
		if (hash[h].bitness < new->bitness) {
			struct hash_entry old = hash[h];
			hash[h] = *new;
			insert_hash(&old);
			return NULL;
		}

		h++;
		h &= ((1 << HASH_BITS) - 1);
	}

	hash[h] = *new;
	if (++hash_count > hash_limit)
		clean_one_from_hash();
	return NULL;
}

static inline tag next_tag(uchar old, uchar new, tag t)
{
	t ^= hash_index[old];
	t ^= hash_index[new];
	return t;
}

static inline tag full_tag(uchar *p, unsigned int len)
{
	tag ret = 0;
	int i;
	for (i=0;i<len;i++) {
		ret ^= hash_index[p[i]];
	}
	return ret;
}

static void found_match(int fd_in, off_t len, off_t off, off_t old_off)
{
	char olddata[len], newdata[len];
	off_t current_off;

	/* Ignore overlapping matches. */
	if (old_off + len <= off) {
		printf("match: %llu %llu %llu\n",
		       (long long)old_off,
		       (long long)off,
		       (long long)len);
	}

	current_off = lseek(fd_in, 0, SEEK_CUR);
	lseek(fd_in, old_off, SEEK_SET);
	read(fd_in, olddata, len);
	lseek(fd_in, off, SEEK_SET);
	read(fd_in, newdata, len);
	lseek(fd_in, current_off, SEEK_SET);

	assert(memcmp(olddata, newdata, len) == 0);
}

static void strong_sums(unsigned char *data,
			struct md4_ctx md4[],
			unsigned int len)
{
	unsigned int i;

	for (i = hash_min_bitness; i <= MAXIMUM_BITNESS; i++)
		md4_update(&md4[i], data, len);
}

bool debug;

static off_t stream_search(int fd_in)
{
	unsigned int i, doff, dlen, laststrong;
	off_t off, lastlen;
	unsigned char data[BUFFER_SIZE];
	struct md4_ctx md4[MAXIMUM_BITNESS+1];
	off_t start[MAXIMUM_BITNESS+1];
	tag t;
	int r;

	/* Refill window */
	r = read(fd_in, data, BUFFER_SIZE);
	if (r < 0)
		barf_perror("read of input failed: %s");

	if (r <= WINDOW_LENGTH)
		return t;

	t = full_tag(data, WINDOW_LENGTH);

	/* Initialize the strong checksums */
	for (i = hash_min_bitness; i <= MAXIMUM_BITNESS; i++) {
		md4_init(&md4[i]);
		start[i] = 0;
	}
	dlen = r - WINDOW_LENGTH;
	doff = WINDOW_LENGTH;
	off = WINDOW_LENGTH;
	laststrong = 0;

	while (dlen > 0) {
		assert(doff >= WINDOW_LENGTH);
		/* Make sure we always have some buffer. */
		if (dlen == 1) {
			/* Keep WINDOW_LENGTH behind at all times. */
			unsigned int newstart, keep;

			newstart = doff - WINDOW_LENGTH;
			keep =  WINDOW_LENGTH + dlen;
			if (laststrong < newstart) {
				strong_sums(data+laststrong, md4,
					    newstart-laststrong);
				laststrong = 0;
			} else
				laststrong -= newstart;

			memmove(data, data+newstart, keep);
			doff = WINDOW_LENGTH;
			r = read(fd_in, data+keep, BUFFER_SIZE-keep);
			if (r < 0)
				barf_perror("read of input failed: %s");
			dlen += r;
		}
		t = next_tag(data[doff-WINDOW_LENGTH], data[doff], t);

		if (bitness(t) > hash_min_bitness) {
			/* Update strong sums. */
			strong_sums(data+laststrong, md4, doff-laststrong);
			if (debug) 
				printf("Adding: %lu - %lu %.*s\n",
				       laststrong, doff,
				       doff-laststrong,
				       data+laststrong);
				       
			laststrong = doff;
			
			/* End each block of sufficient bitness. */
			lastlen = 0;
			for (i = bitness(t); i >= hash_min_bitness; i--) {
				struct hash_entry he, *old;

				md4_final(&md4[i], (unsigned char *)he.md4);
				he.bitness = i;
				he.offset = start[i];

				/* Don't put in the same hash twice. */
				if (off - start[i] != lastlen) {
					if (verbosity > 1)
						printf("%i: Putting %lu@%lu"
						       " in hash\n",
						       i, off - start[i],
						       start[i]);
					old = insert_hash(&he);
					lastlen = off - start[i];
					if (old) {
						found_match(fd_in,
							    lastlen,
							    start[i],
							    old->offset);
					}
				}

				/* Start new strong hash, from *start* of
				   window (ie. overlaps last one). */
				md4_init(&md4[i]);
				md4_update(&md4[i],
					   data + doff - WINDOW_LENGTH,
					   WINDOW_LENGTH);
				start[i] = off - WINDOW_LENGTH;
				if (start[i] == 28039)
					debug = true;

				if (debug)
					printf("Starting %lu: %.*s\n",
					       start[i], 
					       WINDOW_LENGTH,
					       data + doff - WINDOW_LENGTH);
			}
		}
		doff++;
		dlen--;
		off++;
	}
	return off;
}

static void init_hash_indexes(void)
{
	int i;
	for (i=0;i<256;i++) {
		hash_index[i] = ((random()<<16) ^ random());
	}
}

int main(int argc, char *argv[])
{
	unsigned int i;

	init_hash_indexes();

	for (i = 1; i < argc; i++) {
		off_t len;
		int fd = open(argv[i], O_RDONLY);
		if (fd < 0)
			barf_perror("Opening %s", argv[i]);
		
		len = stream_search(fd);
		close(fd);
		printf("Length of %s: %llu\n", argv[i], (long long)len);
		
	}
	return 0;
}