summaryrefslogtreecommitdiff
path: root/bsdiff.cc
blob: a1112d503fb1003a9e4b2ecababea0b82489b493 (plain)
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
/*-
 * Copyright 2003-2005 Colin Percival
 * All rights reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted providing that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if 0
__FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 cperciva Exp $");
#endif

#include "bsdiff/bsdiff.h"

#include <sys/types.h>

#include <err.h>
#include <string.h>

#include <algorithm>

#include "bsdiff/diff_encoder.h"
#include "bsdiff/patch_writer.h"
#include "bsdiff/suffix_array_index.h"

namespace bsdiff {

// TODO(deymo): Deprecate this version of the interface and move all callers
// to the underlying version using PatchWriterInterface instead. This allows
// more flexible options including different encodings.
int bsdiff(const uint8_t* old_buf, size_t oldsize, const uint8_t* new_buf,
           size_t newsize, const char* patch_filename,
           SuffixArrayIndexInterface** sai_cache) {
	BsdiffPatchWriter patch(patch_filename);
	return bsdiff(old_buf, oldsize, new_buf, newsize, 0, &patch,
	              sai_cache);
}

int bsdiff(const uint8_t* old_buf, size_t oldsize, const uint8_t* new_buf,
           size_t newsize, PatchWriterInterface* patch,
           SuffixArrayIndexInterface** sai_cache) {
	return bsdiff(old_buf, oldsize, new_buf, newsize, 0, patch,
	              sai_cache);
}

int bsdiff(const uint8_t* old_buf, size_t oldsize, const uint8_t* new_buf,
           size_t newsize, size_t min_length, PatchWriterInterface* patch,
           SuffixArrayIndexInterface** sai_cache) {
	size_t scsc, scan;
	uint64_t pos=0;
	size_t len;
	size_t lastscan,lastpos,lastoffset;
	uint64_t oldscore;
	ssize_t s,Sf,lenf,Sb,lenb;
	ssize_t overlap,Ss,lens;
	ssize_t i;

	std::unique_ptr<SuffixArrayIndexInterface> local_sai;
	SuffixArrayIndexInterface* sai;

	if (sai_cache && *sai_cache) {
		sai = *sai_cache;
	} else {
		local_sai = CreateSuffixArrayIndex(old_buf, oldsize);
		if (!local_sai)
			return 1;
		sai = local_sai.get();

		// Transfer ownership to the caller.
		if (sai_cache)
			*sai_cache = local_sai.release();
	}

	/* Initialize the patch file encoder */
	DiffEncoder diff_encoder(patch, old_buf, oldsize, new_buf, newsize);
	if (!diff_encoder.Init())
		return 1;

	/* Compute the differences, writing ctrl as we go */
	scan=0;len=0;
	lastscan=0;lastpos=0;lastoffset=0;
	while(scan<newsize) {
		oldscore=0;

		/* If we come across a large block of data that only differs
		 * by less than 8 bytes, this loop will take a long time to
		 * go past that block of data. We need to track the number of
		 * times we're stuck in the block and break out of it. */
		int num_less_than_eight = 0;
		size_t prev_len;
		uint64_t prev_pos, prev_oldscore;
		for(scsc=scan+=len;scan<newsize;scan++) {
			prev_len=len;
			prev_oldscore=oldscore;
			prev_pos=pos;

			sai->SearchPrefix(new_buf + scan, newsize - scan, &len, &pos);

			for(;scsc<scan+len && scsc+lastoffset<oldsize;scsc++)
				if(old_buf[scsc+lastoffset] == new_buf[scsc])
					oldscore++;

			if(((len==oldscore) && (len!=0)) ||
				(len>=oldscore+8 && len>=min_length)) break;

			if((scan+lastoffset<oldsize) &&
				(old_buf[scan+lastoffset] == new_buf[scan]))
				oldscore--;

			const size_t fuzz = 8;
			if (prev_len-fuzz<=len && len<=prev_len &&
			    prev_oldscore-fuzz<=oldscore &&
			    oldscore<=prev_oldscore &&
			    prev_pos<=pos && pos <=prev_pos+fuzz &&
			    oldscore<=len && len<=oldscore+fuzz)
				++num_less_than_eight;
			else
				num_less_than_eight=0;
			if (num_less_than_eight > 100) break;
		};

		if((len!=oldscore) || (scan==newsize)) {
			s=0;Sf=0;lenf=0;
			for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
				if(old_buf[lastpos+i]==new_buf[lastscan+i]) s++;
				i++;
				if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
			};

			lenb=0;
			if(scan<newsize) {
				s=0;Sb=0;
				for(i=1;(scan>=lastscan+i)&&(pos>=static_cast<uint64_t>(i));i++) {
					if(old_buf[pos-i]==new_buf[scan-i]) s++;
					if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
				};
			};

			if(lastscan+lenf>scan-lenb) {
				overlap=(lastscan+lenf)-(scan-lenb);
				s=0;Ss=0;lens=0;
				for(i=0;i<overlap;i++) {
					if(new_buf[lastscan+lenf-overlap+i]==
					   old_buf[lastpos+lenf-overlap+i]) s++;
					if(new_buf[scan-lenb+i]==
					   old_buf[pos-lenb+i]) s--;
					if(s>Ss) { Ss=s; lens=i+1; };
				};

				lenf+=lens-overlap;
				lenb-=lens;
			};

			if (!diff_encoder.AddControlEntry(
			        ControlEntry(lenf,
			                     (scan - lenb) - (lastscan + lenf),
			                     (pos - lenb) - (lastpos + lenf))))
				errx(1, "Writing a control entry");

			lastscan=scan-lenb;
			lastpos=pos-lenb;
			lastoffset=pos-scan;
		};
	};
	if (!diff_encoder.Close())
		errx(1, "Closing the patch file");

	return 0;
}

}  // namespace bsdiff