@gfean/react-native-bundle-drop 0.3.8 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/BundleDrop.podspec +9 -2
  2. package/README.md +19 -0
  3. package/android/build.gradle +25 -4
  4. package/android/src/main/cpp/CMakeLists.txt +33 -0
  5. package/android/src/main/cpp/bundle_drop_xdelta_jni.c +55 -0
  6. package/android/src/main/java/com/bundledrop/BundleDropBundleVerifier.kt +73 -0
  7. package/android/src/main/java/com/bundledrop/BundleDropFileOps.kt +193 -8
  8. package/android/src/main/java/com/bundledrop/BundleDropModule.kt +60 -7
  9. package/android/src/main/java/com/bundledrop/BundleDropOtaPrefs.kt +2 -12
  10. package/android/src/main/java/com/bundledrop/BundleDropOtaResolver.kt +208 -3
  11. package/android/src/main/java/com/bundledrop/BundleDropXdeltaNative.kt +57 -0
  12. package/ios/BundleDropBridge.m +27 -0
  13. package/ios/BundleDropBundleVerifier.swift +106 -0
  14. package/ios/BundleDropFileOps.swift +149 -0
  15. package/ios/BundleDropModule.swift +100 -11
  16. package/ios/BundleDropOtaResolver.swift +149 -3
  17. package/ios/BundleDropZipExtractor.m +75 -3
  18. package/lib/CLI/cli.js +1 -8
  19. package/lib/CLI/scripts/upload-cli.d.ts +0 -1
  20. package/lib/CLI/scripts/upload-cli.js +26 -20
  21. package/lib/api/client.d.ts +11 -1
  22. package/lib/api/client.js +84 -17
  23. package/lib/api/clientApi.d.ts +2 -1
  24. package/lib/api/clientApi.js +9 -0
  25. package/lib/api/types.d.ts +65 -13
  26. package/lib/bundleInfo.d.ts +2 -0
  27. package/lib/fs/bundlePointer.d.ts +2 -1
  28. package/lib/fs/bundlePointer.js +34 -6
  29. package/lib/fs/fsUtils.js +4 -1
  30. package/lib/injectImageResolver.js +135 -45
  31. package/lib/install/bundleInstallShared.d.ts +8 -0
  32. package/lib/install/bundleInstallShared.js +97 -0
  33. package/lib/install/bundleVerification.d.ts +5 -0
  34. package/lib/install/bundleVerification.js +34 -0
  35. package/lib/install/installFromZip.d.ts +2 -5
  36. package/lib/install/installFromZip.js +33 -62
  37. package/lib/loadConfig.js +12 -1
  38. package/lib/manager/downloadAndInstall.d.ts +9 -0
  39. package/lib/manager/downloadAndInstall.js +84 -24
  40. package/lib/manager/reporting.js +26 -2
  41. package/lib/manager/rollbackState.js +21 -12
  42. package/lib/manager/updateCheck.js +89 -11
  43. package/lib/manager/updateState.js +3 -9
  44. package/lib/manifest/bundleManifest.d.ts +33 -0
  45. package/lib/manifest/bundleManifest.js +298 -0
  46. package/lib/native/bundleDropNative.js +1 -2
  47. package/lib/native/fs.d.ts +14 -0
  48. package/lib/native/fs.js +35 -0
  49. package/lib/patch-engine/installFromPatchSet.d.ts +16 -0
  50. package/lib/patch-engine/installFromPatchSet.js +143 -0
  51. package/lib/patch-engine/patchOperations.d.ts +33 -0
  52. package/lib/patch-engine/patchOperations.js +77 -0
  53. package/lib/patch-engine/patchTransport.d.ts +18 -0
  54. package/lib/patch-engine/patchTransport.js +81 -0
  55. package/lib/patchAlgorithms.d.ts +4 -0
  56. package/lib/patchAlgorithms.js +9 -0
  57. package/lib/runtime/service.js +43 -9
  58. package/lib/scripts/bundle.d.ts +3 -0
  59. package/lib/scripts/bundle.js +184 -36
  60. package/lib/scripts/download-bundle.js +18 -7
  61. package/lib/scripts/upload-bundle.js +25 -28
  62. package/package.json +44 -5
  63. package/third_party/xdelta/NOTICE +7 -0
  64. package/third_party/xdelta/PROVENANCE.md +10 -0
  65. package/third_party/xdelta/bundle_drop_xdelta.c +252 -0
  66. package/third_party/xdelta/bundle_drop_xdelta.h +26 -0
  67. package/third_party/xdelta/xdelta3/LICENSE +176 -0
  68. package/third_party/xdelta/xdelta3/config.h +34 -0
  69. package/third_party/xdelta/xdelta3/xdelta3-cfgs.h +171 -0
  70. package/third_party/xdelta/xdelta3/xdelta3-decode.h +1219 -0
  71. package/third_party/xdelta/xdelta3/xdelta3-hash.h +159 -0
  72. package/third_party/xdelta/xdelta3/xdelta3-internal.h +385 -0
  73. package/third_party/xdelta/xdelta3/xdelta3-list.h +127 -0
  74. package/third_party/xdelta/xdelta3/xdelta3.c +4819 -0
  75. package/third_party/xdelta/xdelta3/xdelta3.h +1476 -0
  76. package/lib/CLI/scripts/init-native-config.d.ts +0 -18
  77. package/lib/CLI/scripts/init-native-config.js +0 -539
@@ -0,0 +1,4819 @@
1
+ /* xdelta3 - delta compression tools and library
2
+ Copyright 2016 Joshua MacDonald
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ -------------------------------------------------------------------
17
+
18
+ Xdelta 3
19
+
20
+ The goal of this library is to to implement both the (stand-alone)
21
+ data-compression and delta-compression aspects of VCDIFF encoding, and
22
+ to support a programming interface that works like Zlib
23
+ (http://www.gzip.org/zlib.html). See RFC3284: The VCDIFF Generic
24
+ Differencing and Compression Data Format.
25
+
26
+ VCDIFF is a unified encoding that combines data-compression and
27
+ delta-encoding ("differencing").
28
+
29
+ VCDIFF has a detailed byte-code instruction set with many features.
30
+ The instruction format supports an immediate size operand for small
31
+ COPYs and ADDs (e.g., under 18 bytes). There are also instruction
32
+ "modes", which are used to compress COPY addresses by using two
33
+ address caches. An instruction mode refers to slots in the NEAR
34
+ and SAME caches for recent addresses. NEAR remembers the
35
+ previous 4 (by default) COPY addresses, and SAME catches
36
+ frequent re-uses of the same address using a 3-way (by default)
37
+ 256-entry associative cache of [ADDR mod 256], the encoded byte.
38
+ A hit in the NEAR/SAME cache requires 0/1 ADDR bytes.
39
+
40
+ VCDIFF has a default instruction table, but an alternate
41
+ instruction tables may themselves be be delta-compressed and
42
+ included in the encoding header. This allows even more freedom.
43
+ There are 9 instruction modes in the default code table, 4 near, 3
44
+ same, VCD_SELF (absolute encoding) and VCD_HERE (relative to the
45
+ current position).
46
+
47
+ ----------------------------------------------------------------------
48
+
49
+ Algorithms
50
+
51
+ Aside from the details of encoding and decoding, there are a bunch
52
+ of algorithms needed.
53
+
54
+ 1. STRING-MATCH. A two-level fingerprinting approach is used. A
55
+ single loop computes the two checksums -- small and large -- at
56
+ successive offsets in the TARGET file. The large checksum is more
57
+ accurate and is used to discover SOURCE matches, which are
58
+ potentially very long. The small checksum is used to discover
59
+ copies within the TARGET. Small matching, which is more expensive,
60
+ usually dominates the large STRING-MATCH costs in this code - the
61
+ more exhaustive the search, the better the results. Either of the
62
+ two string-matching mechanisms may be disabled.
63
+
64
+ 2. INSTRUCTION SELECTION. The IOPT buffer here represents a queue
65
+ used to store overlapping copy instructions. There are two possible
66
+ optimizations that go beyond a greedy search. Both of these fall
67
+ into the category of "non-greedy matching" optimizations.
68
+
69
+ The first optimization stems from backward SOURCE-COPY matching.
70
+ When a new SOURCE-COPY instruction covers a previous instruction in
71
+ the target completely, it is erased from the queue. Randal Burns
72
+ originally analyzed these algorithms and did a lot of related work
73
+ (\cite the 1.5-pass algorithm).
74
+
75
+ The second optimization comes by the encoding of common very-small
76
+ COPY and ADD instructions, for which there are special DOUBLE-code
77
+ instructions, which code two instructions in a single byte.
78
+
79
+ The cost of bad instruction-selection overhead is relatively high
80
+ for data-compression, relative to delta-compression, so this second
81
+ optimization is fairly important. With "lazy" matching (the name
82
+ used in Zlib for a similar optimization), the string-match
83
+ algorithm searches after a match for potential overlapping copy
84
+ instructions. In Xdelta and by default, VCDIFF, the minimum match
85
+ size is 4 bytes, whereas Zlib searches with a 3-byte minimum. This
86
+ feature, combined with double instructions, provides a nice
87
+ challenge. Search in this file for "black magic", a heuristic.
88
+
89
+ 3. STREAM ALIGNMENT. Stream alignment is needed to compress large
90
+ inputs in constant space. See xd3_srcwin_move_point().
91
+
92
+ 4. WINDOW SELECTION. When the IOPT buffer flushes, in the first call
93
+ to xd3_iopt_finish_encoding containing any kind of copy instruction,
94
+ the parameters of the source window must be decided: the offset into
95
+ the source and the length of the window. Since the IOPT buffer is
96
+ finite, the program may be forced to fix these values before knowing
97
+ the best offset/length.
98
+
99
+ 5. SECONDARY COMPRESSION. VCDIFF supports a secondary encoding to
100
+ be applied to the individual sections of the data format, which are
101
+ ADDRess, INSTruction, and DATA. Several secondary compressor
102
+ variations are implemented here, although none is standardized yet.
103
+
104
+ One is an adaptive huffman algorithm -- the FGK algorithm (Faller,
105
+ Gallager, and Knuth, 1985). This compressor is extremely slow.
106
+
107
+ The other is a simple static Huffman routine, which is the base
108
+ case of a semi-adaptive scheme published by D.J. Wheeler and first
109
+ widely used in bzip2 (by Julian Seward). This is a very
110
+ interesting algorithm, originally published in nearly cryptic form
111
+ by D.J. Wheeler. !!!NOTE!!! Because these are not standardized,
112
+ secondary compression remains off by default.
113
+ ftp://ftp.cl.cam.ac.uk/users/djw3/bred3.{c,ps}
114
+ --------------------------------------------------------------------
115
+
116
+ Other Features
117
+
118
+ 1. USER CONVENIENCE
119
+
120
+ For user convenience, it is essential to recognize Gzip-compressed
121
+ files and automatically Gzip-decompress them prior to
122
+ delta-compression (or else no delta-compression will be achieved
123
+ unless the user manually decompresses the inputs). The compressed
124
+ represention competes with Xdelta, and this must be hidden from the
125
+ command-line user interface. The Xdelta-1.x encoding was simple, not
126
+ compressed itself, so Xdelta-1.x uses Zlib internally to compress the
127
+ representation.
128
+
129
+ This implementation supports external compression, which implements
130
+ the necessary fork() and pipe() mechanics. There is a tricky step
131
+ involved to support automatic detection of a compressed input in a
132
+ non-seekable input. First you read a bit of the input to detect
133
+ magic headers. When a compressed format is recognized, exec() the
134
+ external compression program and create a second child process to
135
+ copy the original input stream. [Footnote: There is a difficulty
136
+ related to using Gzip externally. It is not possible to decompress
137
+ and recompress a Gzip file transparently. If FILE.GZ had a
138
+ cryptographic signature, then, after: (1) Gzip-decompression, (2)
139
+ Xdelta-encoding, (3) Gzip-compression the signature could be
140
+ broken. The only way to solve this problem is to guess at Gzip's
141
+ compression level or control it by other means. I recommend that
142
+ specific implementations of any compression scheme store
143
+ information needed to exactly re-compress the input, that way
144
+ external compression is transparent - however, this won't happen
145
+ here until it has stabilized.]
146
+
147
+ 2. APPLICATION-HEADER
148
+
149
+ This feature was introduced in RFC3284. It allows any application
150
+ to include a header within the VCDIFF file format. This allows
151
+ general inter-application data exchange with support for
152
+ application-specific extensions to communicate metadata.
153
+
154
+ 3. VCDIFF CHECKSUM
155
+
156
+ An optional checksum value is included with each window, which can
157
+ be used to validate the final result. This verifies the correct source
158
+ file was used for decompression as well as the obvious advantage:
159
+ checking the implementation (and underlying) correctness.
160
+
161
+ 4. LIGHT WEIGHT
162
+
163
+ The code makes efforts to avoid copying data more than necessary.
164
+ The code delays many initialization tasks until the first use, it
165
+ optimizes for identical (perfectly matching) inputs. It does not
166
+ compute any checksums until the first lookup misses. Memory usage
167
+ is reduced. String-matching is templatized (by slightly gross use
168
+ of CPP) to hard-code alternative compile-time defaults. The code
169
+ has few outside dependencies.
170
+ ----------------------------------------------------------------------
171
+
172
+ The default rfc3284 instruction table:
173
+ (see RFC for the explanation)
174
+
175
+ TYPE SIZE MODE TYPE SIZE MODE INDEX
176
+ --------------------------------------------------------------------
177
+ 1. Run 0 0 Noop 0 0 0
178
+ 2. Add 0, [1,17] 0 Noop 0 0 [1,18]
179
+ 3. Copy 0, [4,18] 0 Noop 0 0 [19,34]
180
+ 4. Copy 0, [4,18] 1 Noop 0 0 [35,50]
181
+ 5. Copy 0, [4,18] 2 Noop 0 0 [51,66]
182
+ 6. Copy 0, [4,18] 3 Noop 0 0 [67,82]
183
+ 7. Copy 0, [4,18] 4 Noop 0 0 [83,98]
184
+ 8. Copy 0, [4,18] 5 Noop 0 0 [99,114]
185
+ 9. Copy 0, [4,18] 6 Noop 0 0 [115,130]
186
+ 10. Copy 0, [4,18] 7 Noop 0 0 [131,146]
187
+ 11. Copy 0, [4,18] 8 Noop 0 0 [147,162]
188
+ 12. Add [1,4] 0 Copy [4,6] 0 [163,174]
189
+ 13. Add [1,4] 0 Copy [4,6] 1 [175,186]
190
+ 14. Add [1,4] 0 Copy [4,6] 2 [187,198]
191
+ 15. Add [1,4] 0 Copy [4,6] 3 [199,210]
192
+ 16. Add [1,4] 0 Copy [4,6] 4 [211,222]
193
+ 17. Add [1,4] 0 Copy [4,6] 5 [223,234]
194
+ 18. Add [1,4] 0 Copy 4 6 [235,238]
195
+ 19. Add [1,4] 0 Copy 4 7 [239,242]
196
+ 20. Add [1,4] 0 Copy 4 8 [243,246]
197
+ 21. Copy 4 [0,8] Add 1 0 [247,255]
198
+ --------------------------------------------------------------------
199
+
200
+ Reading the source: Overview
201
+
202
+ This file includes itself in several passes to macro-expand certain
203
+ sections with variable forms. Just read ahead, there's only a
204
+ little confusion. I know this sounds ugly, but hard-coding some of
205
+ the string-matching parameters results in a 10-15% increase in
206
+ string-match performance. The only time this hurts is when you have
207
+ unbalanced #if/endifs.
208
+
209
+ A single compilation unit tames the Makefile. In short, this is to
210
+ allow the above-described hack without an explodingMakefile. The
211
+ single compilation unit includes the core library features,
212
+ configurable string-match templates, optional main() command-line
213
+ tool, misc optional features, and a regression test. Features are
214
+ controled with CPP #defines, see Makefile.am.
215
+
216
+ The initial __XDELTA3_C_HEADER_PASS__ starts first, the _INLINE_ and
217
+ _TEMPLATE_ sections follow. Easy stuff first, hard stuff last.
218
+
219
+ Optional features include:
220
+
221
+ xdelta3-main.h The command-line interface, external compression
222
+ support, POSIX-specific, info & VCDIFF-debug tools.
223
+ xdelta3-second.h The common secondary compression routines.
224
+ xdelta3-decoder.h All decoding routines.
225
+ xdelta3-djw.h The semi-adaptive huffman secondary encoder.
226
+ xdelta3-fgk.h The adaptive huffman secondary encoder.
227
+ xdelta3-test.h The unit test covers major algorithms,
228
+ encoding and decoding. There are single-bit
229
+ error decoding tests. There are 32/64-bit file size
230
+ boundary tests. There are command-line tests.
231
+ There are compression tests. There are external
232
+ compression tests. There are string-matching tests.
233
+ There should be more tests...
234
+
235
+ Additional headers include:
236
+
237
+ xdelta3.h The public header file.
238
+ xdelta3-cfgs.h The default settings for default, built-in
239
+ encoders. These are hard-coded at
240
+ compile-time. There is also a single
241
+ soft-coded string matcher for experimenting
242
+ with arbitrary values.
243
+ xdelta3-list.h A cyclic list template
244
+
245
+ Misc little debug utilities:
246
+
247
+ badcopy.c Randomly modifies an input file based on two
248
+ parameters: (1) the probability that a byte in
249
+ the file is replaced with a pseudo-random value,
250
+ and (2) the mean change size. Changes are
251
+ generated using an expoential distribution
252
+ which approximates the expected error_prob
253
+ distribution.
254
+ --------------------------------------------------------------------
255
+
256
+ This file itself is unusually large. I hope to defend this layout
257
+ with lots of comments. Everything in this file is related to
258
+ encoding and decoding. I like it all together - the template stuff
259
+ is just a hack. */
260
+
261
+ #ifndef __XDELTA3_C_HEADER_PASS__
262
+ #define __XDELTA3_C_HEADER_PASS__
263
+
264
+ #include "xdelta3.h"
265
+ #include "xdelta3-internal.h"
266
+
267
+ /***********************************************************************
268
+ STATIC CONFIGURATION
269
+ ***********************************************************************/
270
+
271
+ #ifndef XD3_MAIN /* the main application */
272
+ #define XD3_MAIN 0
273
+ #endif
274
+
275
+ #ifndef VCDIFF_TOOLS
276
+ #define VCDIFF_TOOLS XD3_MAIN
277
+ #endif
278
+
279
+ #ifndef SECONDARY_FGK /* one from the algorithm preservation department: */
280
+ #define SECONDARY_FGK 0 /* adaptive Huffman routines */
281
+ #endif
282
+
283
+ #ifndef SECONDARY_DJW /* semi-adaptive/static Huffman for the eventual */
284
+ #define SECONDARY_DJW 0 /* standardization, off by default until such time. */
285
+ #endif
286
+
287
+ #ifndef SECONDARY_LZMA
288
+ #ifdef HAVE_LZMA_H
289
+ #define SECONDARY_LZMA 1
290
+ #else
291
+ #define SECONDARY_LZMA 0
292
+ #endif
293
+ #endif
294
+
295
+ #if XD3_ENCODER
296
+ #define IF_ENCODER(x) x
297
+ #else
298
+ #define IF_ENCODER(x)
299
+ #endif
300
+
301
+ /***********************************************************************/
302
+
303
+ /* header indicator bits */
304
+ #define VCD_SECONDARY (1U << 0) /* uses secondary compressor */
305
+ #define VCD_CODETABLE (1U << 1) /* supplies code table data */
306
+ #define VCD_APPHEADER (1U << 2) /* supplies application data */
307
+ #define VCD_INVHDR (~0x7U)
308
+
309
+ /* window indicator bits */
310
+ #define VCD_SOURCE (1U << 0) /* copy window in source file */
311
+ #define VCD_TARGET (1U << 1) /* copy window in target file */
312
+ #define VCD_ADLER32 (1U << 2) /* has adler32 checksum */
313
+ #define VCD_INVWIN (~0x7U)
314
+
315
+ #define VCD_SRCORTGT (VCD_SOURCE | VCD_TARGET)
316
+
317
+ /* delta indicator bits */
318
+ #define VCD_DATACOMP (1U << 0)
319
+ #define VCD_INSTCOMP (1U << 1)
320
+ #define VCD_ADDRCOMP (1U << 2)
321
+ #define VCD_INVDEL (~0x7U)
322
+
323
+ typedef enum {
324
+ VCD_DJW_ID = 1,
325
+ VCD_LZMA_ID = 2,
326
+ VCD_FGK_ID = 16 /* Note: these are not standard IANA-allocated IDs! */
327
+ } xd3_secondary_ids;
328
+
329
+ typedef enum {
330
+ SEC_NOFLAGS = 0,
331
+
332
+ /* Note: SEC_COUNT_FREQS Not implemented (to eliminate 1st Huffman pass) */
333
+ SEC_COUNT_FREQS = (1 << 0)
334
+ } xd3_secondary_flags;
335
+
336
+ typedef enum {
337
+ DATA_SECTION, /* These indicate which section to the secondary
338
+ * compressor. */
339
+ INST_SECTION, /* The header section is not compressed, therefore not
340
+ * listed here. */
341
+ ADDR_SECTION
342
+ } xd3_section_type;
343
+
344
+ typedef unsigned int xd3_rtype;
345
+
346
+ /***********************************************************************/
347
+
348
+ #include "xdelta3-list.h"
349
+
350
+ #if XD3_ENCODER
351
+ XD3_MAKELIST(xd3_rlist, xd3_rinst, link);
352
+ #endif
353
+
354
+ /***********************************************************************/
355
+
356
+ #define SECONDARY_MIN_SAVINGS 2 /* Secondary compression has to save
357
+ at least this many bytes. */
358
+ #define SECONDARY_MIN_INPUT 10 /* Secondary compression needs at
359
+ least this many bytes. */
360
+
361
+ #define VCDIFF_MAGIC1 0xd6 /* 1st file byte */
362
+ #define VCDIFF_MAGIC2 0xc3 /* 2nd file byte */
363
+ #define VCDIFF_MAGIC3 0xc4 /* 3rd file byte */
364
+ #define VCDIFF_VERSION 0x00 /* 4th file byte */
365
+
366
+ #define VCD_SELF 0 /* 1st address mode */
367
+ #define VCD_HERE 1 /* 2nd address mode */
368
+
369
+ #define SECONDARY_ANY (SECONDARY_DJW || SECONDARY_FGK || SECONDARY_LZMA)
370
+
371
+ #define ALPHABET_SIZE 256 /* Used in test code--size of the secondary
372
+ * compressor alphabet. */
373
+
374
+ #define HASH_CKOFFSET 1U /* Table entries distinguish "no-entry" from
375
+ * offset 0 using this offset. */
376
+
377
+ #define MAX_MATCH_SPLIT 18U /* VCDIFF code table: 18 is the default limit
378
+ * for direct-coded ADD sizes */
379
+
380
+ #define LEAST_MATCH_INCR 0 /* The least number of bytes an overlapping
381
+ * match must beat the preceding match by. This
382
+ * is a bias for the lazy match optimization. A
383
+ * non-zero value means that an adjacent match
384
+ * has to be better by more than the step
385
+ * between them. 0. */
386
+
387
+ #define MIN_MATCH 4U /* VCDIFF code table: MIN_MATCH=4 */
388
+ #define MIN_RUN 8U /* The shortest run, if it is shorter than this
389
+ * an immediate add/copy will be just as good.
390
+ * ADD1/COPY6 = 1I+1D+1A bytes, RUN18 =
391
+ * 1I+1D+1A. */
392
+
393
+ #define MAX_MODES 9 /* Maximum number of nodes used for
394
+ * compression--does not limit decompression. */
395
+
396
+ #define ENC_SECTS 4 /* Number of separate output sections. */
397
+
398
+ #define HDR_TAIL(s) ((s)->enc_tails[0])
399
+ #define DATA_TAIL(s) ((s)->enc_tails[1])
400
+ #define INST_TAIL(s) ((s)->enc_tails[2])
401
+ #define ADDR_TAIL(s) ((s)->enc_tails[3])
402
+
403
+ #define HDR_HEAD(s) ((s)->enc_heads[0])
404
+ #define DATA_HEAD(s) ((s)->enc_heads[1])
405
+ #define INST_HEAD(s) ((s)->enc_heads[2])
406
+ #define ADDR_HEAD(s) ((s)->enc_heads[3])
407
+
408
+ /* Template instances. */
409
+ #if XD3_BUILD_SLOW
410
+ #define IF_BUILD_SLOW(x) x
411
+ #else
412
+ #define IF_BUILD_SLOW(x)
413
+ #endif
414
+ #if XD3_BUILD_FAST
415
+ #define IF_BUILD_FAST(x) x
416
+ #else
417
+ #define IF_BUILD_FAST(x)
418
+ #endif
419
+ #if XD3_BUILD_FASTER
420
+ #define IF_BUILD_FASTER(x) x
421
+ #else
422
+ #define IF_BUILD_FASTER(x)
423
+ #endif
424
+ #if XD3_BUILD_FASTEST
425
+ #define IF_BUILD_FASTEST(x) x
426
+ #else
427
+ #define IF_BUILD_FASTEST(x)
428
+ #endif
429
+ #if XD3_BUILD_SOFT
430
+ #define IF_BUILD_SOFT(x) x
431
+ #else
432
+ #define IF_BUILD_SOFT(x)
433
+ #endif
434
+ #if XD3_BUILD_DEFAULT
435
+ #define IF_BUILD_DEFAULT(x) x
436
+ #else
437
+ #define IF_BUILD_DEFAULT(x)
438
+ #endif
439
+
440
+ /* Update the run-length state */
441
+ #define NEXTRUN(c) do { if ((c) == run_c) { run_l += 1; } \
442
+ else { run_c = (c); run_l = 1; } } while (0)
443
+
444
+ /* This CPP-conditional stuff can be cleaned up... */
445
+ #if REGRESSION_TEST
446
+ #define IF_REGRESSION(x) x
447
+ #else
448
+ #define IF_REGRESSION(x)
449
+ #endif
450
+
451
+ /***********************************************************************/
452
+
453
+ #if XD3_ENCODER
454
+ static void* xd3_alloc0 (xd3_stream *stream,
455
+ usize_t elts,
456
+ usize_t size);
457
+
458
+
459
+ static int xd3_alloc_iopt (xd3_stream *stream, usize_t elts);
460
+
461
+ static void xd3_free_output (xd3_stream *stream,
462
+ xd3_output *output);
463
+
464
+ static int xd3_emit_double (xd3_stream *stream, xd3_rinst *first,
465
+ xd3_rinst *second, uint8_t code);
466
+ static int xd3_emit_single (xd3_stream *stream, xd3_rinst *single,
467
+ uint8_t code);
468
+
469
+ static usize_t xd3_sizeof_output (xd3_output *output);
470
+ static void xd3_encode_reset (xd3_stream *stream);
471
+
472
+ static int xd3_source_match_setup (xd3_stream *stream, xoff_t srcpos);
473
+ static int xd3_source_extend_match (xd3_stream *stream);
474
+ static int xd3_srcwin_setup (xd3_stream *stream);
475
+ static usize_t xd3_iopt_last_matched (xd3_stream *stream);
476
+ static int xd3_emit_uint32_t (xd3_stream *stream, xd3_output **output,
477
+ uint32_t num);
478
+
479
+ static usize_t xd3_smatch (xd3_stream *stream,
480
+ usize_t base,
481
+ usize_t scksum,
482
+ usize_t *match_offset);
483
+ static int xd3_string_match_init (xd3_stream *stream);
484
+ static uint32_t xd3_scksum (uint32_t *state, const uint8_t *seg,
485
+ const usize_t ln);
486
+ static usize_t xd3_comprun (const uint8_t *seg, usize_t slook, uint8_t *run_cp);
487
+ static int xd3_srcwin_move_point (xd3_stream *stream,
488
+ usize_t *next_move_point);
489
+
490
+ static int xd3_emit_run (xd3_stream *stream, usize_t pos,
491
+ usize_t size, uint8_t *run_c);
492
+ static xoff_t xd3_source_cksum_offset(xd3_stream *stream, usize_t low);
493
+ static void xd3_scksum_insert (xd3_stream *stream,
494
+ usize_t inx,
495
+ usize_t scksum,
496
+ usize_t pos);
497
+
498
+
499
+ #if XD3_DEBUG
500
+ static void xd3_verify_run_state (xd3_stream *stream,
501
+ const uint8_t *inp,
502
+ usize_t x_run_l,
503
+ uint8_t *x_run_c);
504
+ static void xd3_verify_large_state (xd3_stream *stream,
505
+ const uint8_t *inp,
506
+ usize_t x_cksum);
507
+ static void xd3_verify_small_state (xd3_stream *stream,
508
+ const uint8_t *inp,
509
+ uint32_t x_cksum);
510
+
511
+ #endif /* XD3_DEBUG */
512
+ #endif /* XD3_ENCODER */
513
+
514
+ static int xd3_decode_allocate (xd3_stream *stream, usize_t size,
515
+ uint8_t **copied1, usize_t *alloc1);
516
+
517
+ static void* xd3_alloc (xd3_stream *stream, usize_t elts, usize_t size);
518
+ static void xd3_free (xd3_stream *stream, void *ptr);
519
+
520
+ const char* xd3_strerror (int ret)
521
+ {
522
+ switch (ret)
523
+ {
524
+ case XD3_INPUT: return "XD3_INPUT";
525
+ case XD3_OUTPUT: return "XD3_OUTPUT";
526
+ case XD3_GETSRCBLK: return "XD3_GETSRCBLK";
527
+ case XD3_GOTHEADER: return "XD3_GOTHEADER";
528
+ case XD3_WINSTART: return "XD3_WINSTART";
529
+ case XD3_WINFINISH: return "XD3_WINFINISH";
530
+ case XD3_TOOFARBACK: return "XD3_TOOFARBACK";
531
+ case XD3_INTERNAL: return "XD3_INTERNAL";
532
+ case XD3_INVALID: return "XD3_INVALID";
533
+ case XD3_INVALID_INPUT: return "XD3_INVALID_INPUT";
534
+ case XD3_NOSECOND: return "XD3_NOSECOND";
535
+ case XD3_UNIMPLEMENTED: return "XD3_UNIMPLEMENTED";
536
+ }
537
+ return NULL;
538
+ }
539
+
540
+ /***********************************************************************/
541
+
542
+ #define xd3_sec_data(s) ((s)->sec_stream_d)
543
+ #define xd3_sec_inst(s) ((s)->sec_stream_i)
544
+ #define xd3_sec_addr(s) ((s)->sec_stream_a)
545
+
546
+ struct _xd3_sec_type
547
+ {
548
+ uint8_t id;
549
+ const char *name;
550
+ xd3_secondary_flags flags;
551
+
552
+ /* xd3_sec_stream is opaque to the generic code */
553
+ xd3_sec_stream* (*alloc) (xd3_stream *stream);
554
+ void (*destroy) (xd3_stream *stream,
555
+ xd3_sec_stream *sec);
556
+ int (*init) (xd3_stream *stream,
557
+ xd3_sec_stream *sec_stream,
558
+ int is_encode);
559
+ int (*decode) (xd3_stream *stream,
560
+ xd3_sec_stream *sec_stream,
561
+ const uint8_t **input,
562
+ const uint8_t *input_end,
563
+ uint8_t **output,
564
+ const uint8_t *output_end);
565
+ #if XD3_ENCODER
566
+ int (*encode) (xd3_stream *stream,
567
+ xd3_sec_stream *sec_stream,
568
+ xd3_output *input,
569
+ xd3_output *output,
570
+ xd3_sec_cfg *cfg);
571
+ #endif
572
+ };
573
+
574
+ #define BIT_STATE_ENCODE_INIT { 0, 1 }
575
+ #define BIT_STATE_DECODE_INIT { 0, 0x100 }
576
+
577
+ typedef struct _bit_state bit_state;
578
+ struct _bit_state
579
+ {
580
+ uint8_t cur_byte;
581
+ usize_t cur_mask;
582
+ };
583
+
584
+ #if SECONDARY_ANY == 0
585
+ #define IF_SEC(x)
586
+ #define IF_NSEC(x) x
587
+ #else /* yuck */
588
+ #define IF_SEC(x) x
589
+ #define IF_NSEC(x)
590
+ static int
591
+ xd3_decode_secondary (xd3_stream *stream,
592
+ xd3_desect *sect,
593
+ xd3_sec_stream **sec_streamp);
594
+ #if XD3_ENCODER
595
+ static int
596
+ xd3_encode_secondary (xd3_stream *stream,
597
+ xd3_output **head,
598
+ xd3_output **tail,
599
+ xd3_sec_stream **sec_streamp,
600
+ xd3_sec_cfg *cfg,
601
+ int *did_it);
602
+ #endif
603
+ #endif /* SECONDARY_ANY */
604
+
605
+ #if SECONDARY_FGK
606
+ extern const xd3_sec_type fgk_sec_type;
607
+ #define IF_FGK(x) x
608
+ #define FGK_CASE(s) \
609
+ s->sec_type = & fgk_sec_type; \
610
+ break;
611
+ #else
612
+ #define IF_FGK(x)
613
+ #define FGK_CASE(s) \
614
+ s->msg = "unavailable secondary compressor: FGK Adaptive Huffman"; \
615
+ return XD3_INTERNAL;
616
+ #endif
617
+
618
+ #if SECONDARY_DJW
619
+ extern const xd3_sec_type djw_sec_type;
620
+ #define IF_DJW(x) x
621
+ #define DJW_CASE(s) \
622
+ s->sec_type = & djw_sec_type; \
623
+ break;
624
+ #else
625
+ #define IF_DJW(x)
626
+ #define DJW_CASE(s) \
627
+ s->msg = "unavailable secondary compressor: DJW Static Huffman"; \
628
+ return XD3_INTERNAL;
629
+ #endif
630
+
631
+ #if SECONDARY_LZMA
632
+ extern const xd3_sec_type lzma_sec_type;
633
+ #define IF_LZMA(x) x
634
+ #define LZMA_CASE(s) \
635
+ s->sec_type = & lzma_sec_type; \
636
+ break;
637
+ #else
638
+ #define IF_LZMA(x)
639
+ #define LZMA_CASE(s) \
640
+ s->msg = "unavailable secondary compressor: LZMA"; \
641
+ return XD3_INTERNAL;
642
+ #endif
643
+
644
+ /***********************************************************************/
645
+
646
+ #include "xdelta3-hash.h"
647
+
648
+ /* Process template passes - this includes xdelta3.c several times. */
649
+ #define __XDELTA3_C_TEMPLATE_PASS__
650
+ #include "xdelta3-cfgs.h"
651
+ #undef __XDELTA3_C_TEMPLATE_PASS__
652
+
653
+ /* Process the inline pass. */
654
+ #define __XDELTA3_C_INLINE_PASS__
655
+ #include "xdelta3.c"
656
+ #undef __XDELTA3_C_INLINE_PASS__
657
+
658
+ /* Secondary compression */
659
+ #if SECONDARY_ANY
660
+ #include "xdelta3-second.h"
661
+ #endif
662
+
663
+ #if SECONDARY_FGK
664
+ #include "xdelta3-fgk.h"
665
+ const xd3_sec_type fgk_sec_type =
666
+ {
667
+ VCD_FGK_ID,
668
+ "FGK Adaptive Huffman",
669
+ SEC_NOFLAGS,
670
+ (xd3_sec_stream* (*)(xd3_stream*)) fgk_alloc,
671
+ (void (*)(xd3_stream*, xd3_sec_stream*)) fgk_destroy,
672
+ (int (*)(xd3_stream*, xd3_sec_stream*, int)) fgk_init,
673
+ (int (*)(xd3_stream*, xd3_sec_stream*, const uint8_t**, const uint8_t*,
674
+ uint8_t**, const uint8_t*)) xd3_decode_fgk,
675
+ IF_ENCODER((int (*)(xd3_stream*, xd3_sec_stream*, xd3_output*,
676
+ xd3_output*, xd3_sec_cfg*)) xd3_encode_fgk)
677
+ };
678
+ #endif
679
+
680
+ #if SECONDARY_DJW
681
+ #include "xdelta3-djw.h"
682
+ const xd3_sec_type djw_sec_type =
683
+ {
684
+ VCD_DJW_ID,
685
+ "Static Huffman",
686
+ SEC_COUNT_FREQS,
687
+ (xd3_sec_stream* (*)(xd3_stream*)) djw_alloc,
688
+ (void (*)(xd3_stream*, xd3_sec_stream*)) djw_destroy,
689
+ (int (*)(xd3_stream*, xd3_sec_stream*, int)) djw_init,
690
+ (int (*)(xd3_stream*, xd3_sec_stream*, const uint8_t**, const uint8_t*,
691
+ uint8_t**, const uint8_t*)) xd3_decode_huff,
692
+ IF_ENCODER((int (*)(xd3_stream*, xd3_sec_stream*, xd3_output*,
693
+ xd3_output*, xd3_sec_cfg*)) xd3_encode_huff)
694
+ };
695
+ #endif
696
+
697
+ #if SECONDARY_LZMA
698
+ #include "xdelta3-lzma.h"
699
+ const xd3_sec_type lzma_sec_type =
700
+ {
701
+ VCD_LZMA_ID,
702
+ "lzma",
703
+ SEC_NOFLAGS,
704
+ (xd3_sec_stream* (*)(xd3_stream*)) xd3_lzma_alloc,
705
+ (void (*)(xd3_stream*, xd3_sec_stream*)) xd3_lzma_destroy,
706
+ (int (*)(xd3_stream*, xd3_sec_stream*, int)) xd3_lzma_init,
707
+ (int (*)(xd3_stream*, xd3_sec_stream*, const uint8_t**, const uint8_t*,
708
+ uint8_t**, const uint8_t*)) xd3_decode_lzma,
709
+ IF_ENCODER((int (*)(xd3_stream*, xd3_sec_stream*, xd3_output*,
710
+ xd3_output*, xd3_sec_cfg*)) xd3_encode_lzma)
711
+ };
712
+ #endif
713
+
714
+ #if XD3_MAIN || PYTHON_MODULE || SWIG_MODULE || NOT_MAIN
715
+ #include "xdelta3-main.h"
716
+ #endif
717
+
718
+ #if REGRESSION_TEST
719
+ #include "xdelta3-test.h"
720
+ #endif
721
+
722
+ #endif /* __XDELTA3_C_HEADER_PASS__ */
723
+ #ifdef __XDELTA3_C_INLINE_PASS__
724
+
725
+ /****************************************************************
726
+ Instruction tables
727
+ *****************************************************************/
728
+
729
+ /* The following code implements a parametrized description of the
730
+ * code table given above for a few reasons. It is not necessary for
731
+ * implementing the standard, to support compression with variable
732
+ * tables, so an implementation is only required to know the default
733
+ * code table to begin decompression. (If the encoder uses an
734
+ * alternate table, the table is included in compressed form inside
735
+ * the VCDIFF file.)
736
+ *
737
+ * Before adding variable-table support there were two functions which
738
+ * were hard-coded to the default table above.
739
+ * xd3_compute_default_table() would create the default table by
740
+ * filling a 256-elt array of xd3_dinst values. The corresponding
741
+ * function, xd3_choose_instruction(), would choose an instruction
742
+ * based on the hard-coded parameters of the default code table.
743
+ *
744
+ * Notes: The parametrized code table description here only generates
745
+ * tables of a certain regularity similar to the default table by
746
+ * allowing to vary the distribution of single- and
747
+ * double-instructions and change the number of near and same copy
748
+ * modes. More exotic tables are only possible by extending this
749
+ * code.
750
+ *
751
+ * For performance reasons, both the parametrized and non-parametrized
752
+ * versions of xd3_choose_instruction remain. The parametrized
753
+ * version is only needed for testing multi-table decoding support.
754
+ * If ever multi-table encoding is required, this can be optimized by
755
+ * compiling static functions for each table.
756
+ */
757
+
758
+ /* The XD3_CHOOSE_INSTRUCTION calls xd3_choose_instruction with the
759
+ * table description when GENERIC_ENCODE_TABLES are in use. The
760
+ * IF_GENCODETBL macro enables generic-code-table specific code
761
+ * (removed 10/2014). */
762
+ #define XD3_CHOOSE_INSTRUCTION(stream,prev,inst) \
763
+ xd3_choose_instruction (prev, inst)
764
+
765
+ /* This structure maintains information needed by
766
+ * xd3_choose_instruction to compute the code for a double instruction
767
+ * by first indexing an array of code_table_sizes by copy mode, then
768
+ * using (offset + (muliplier * X)) */
769
+ struct _xd3_code_table_sizes {
770
+ uint8_t cpy_max;
771
+ uint8_t offset;
772
+ uint8_t mult;
773
+ };
774
+
775
+ /* This contains a complete description of a code table. */
776
+ struct _xd3_code_table_desc
777
+ {
778
+ /* Assumes a single RUN instruction */
779
+ /* Assumes that MIN_MATCH is 4 */
780
+
781
+ uint8_t add_sizes; /* Number of immediate-size single
782
+ adds (default 17) */
783
+ uint8_t near_modes; /* Number of near copy modes (default 4) */
784
+ uint8_t same_modes; /* Number of same copy modes (default 3) */
785
+ uint8_t cpy_sizes; /* Number of immediate-size single
786
+ copies (default 15) */
787
+
788
+ uint8_t addcopy_add_max; /* Maximum add size for an add-copy
789
+ double instruction, all modes
790
+ (default 4) */
791
+ uint8_t addcopy_near_cpy_max; /* Maximum cpy size for an add-copy
792
+ double instruction, up through
793
+ VCD_NEAR modes (default 6) */
794
+ uint8_t addcopy_same_cpy_max; /* Maximum cpy size for an add-copy
795
+ double instruction, VCD_SAME modes
796
+ (default 4) */
797
+
798
+ uint8_t copyadd_add_max; /* Maximum add size for a copy-add
799
+ double instruction, all modes
800
+ (default 1) */
801
+ uint8_t copyadd_near_cpy_max; /* Maximum cpy size for a copy-add
802
+ double instruction, up through
803
+ VCD_NEAR modes (default 4) */
804
+ uint8_t copyadd_same_cpy_max; /* Maximum cpy size for a copy-add
805
+ double instruction, VCD_SAME modes
806
+ (default 4) */
807
+
808
+ xd3_code_table_sizes addcopy_max_sizes[MAX_MODES];
809
+ xd3_code_table_sizes copyadd_max_sizes[MAX_MODES];
810
+ };
811
+
812
+ /* The rfc3284 code table is represented: */
813
+ static const xd3_code_table_desc __rfc3284_code_table_desc = {
814
+ 17, /* add sizes */
815
+ 4, /* near modes */
816
+ 3, /* same modes */
817
+ 15, /* copy sizes */
818
+
819
+ 4, /* add-copy max add */
820
+ 6, /* add-copy max cpy, near */
821
+ 4, /* add-copy max cpy, same */
822
+
823
+ 1, /* copy-add max add */
824
+ 4, /* copy-add max cpy, near */
825
+ 4, /* copy-add max cpy, same */
826
+
827
+ /* addcopy */
828
+ { {6,163,3},{6,175,3},{6,187,3},{6,199,3},{6,211,3},{6,223,3},
829
+ {4,235,1},{4,239,1},{4,243,1} },
830
+ /* copyadd */
831
+ { {4,247,1},{4,248,1},{4,249,1},{4,250,1},{4,251,1},{4,252,1},
832
+ {4,253,1},{4,254,1},{4,255,1} },
833
+ };
834
+
835
+ /* Computes code table entries of TBL using the specified description. */
836
+ static void
837
+ xd3_build_code_table (const xd3_code_table_desc *desc, xd3_dinst *tbl)
838
+ {
839
+ uint8_t size1, size2;
840
+ uint8_t mode;
841
+ usize_t cpy_modes = 2U + desc->near_modes + desc->same_modes;
842
+ xd3_dinst *d = tbl;
843
+
844
+ (d++)->type1 = XD3_RUN;
845
+ (d++)->type1 = XD3_ADD;
846
+
847
+ for (size1 = 1; size1 <= desc->add_sizes; size1 += 1, d += 1)
848
+ {
849
+ d->type1 = XD3_ADD;
850
+ d->size1 = size1;
851
+ }
852
+
853
+ for (mode = 0; mode < cpy_modes; mode += 1)
854
+ {
855
+ (d++)->type1 = XD3_CPY + mode;
856
+
857
+ for (size1 = MIN_MATCH; size1 < MIN_MATCH + desc->cpy_sizes;
858
+ size1 += 1, d += 1)
859
+ {
860
+ d->type1 = XD3_CPY + mode;
861
+ d->size1 = size1;
862
+ }
863
+ }
864
+
865
+ for (mode = 0; mode < cpy_modes; mode += 1)
866
+ {
867
+ for (size1 = 1; size1 <= desc->addcopy_add_max; size1 += 1)
868
+ {
869
+ usize_t max = (mode < 2U + desc->near_modes) ?
870
+ desc->addcopy_near_cpy_max :
871
+ desc->addcopy_same_cpy_max;
872
+
873
+ for (size2 = MIN_MATCH; size2 <= max; size2 += 1, d += 1)
874
+ {
875
+ d->type1 = XD3_ADD;
876
+ d->size1 = size1;
877
+ d->type2 = XD3_CPY + mode;
878
+ d->size2 = size2;
879
+ }
880
+ }
881
+ }
882
+
883
+ for (mode = 0; mode < cpy_modes; mode += 1)
884
+ {
885
+ usize_t max = (mode < 2U + desc->near_modes) ?
886
+ desc->copyadd_near_cpy_max :
887
+ desc->copyadd_same_cpy_max;
888
+
889
+ for (size1 = MIN_MATCH; size1 <= max; size1 += 1)
890
+ {
891
+ for (size2 = 1; size2 <= desc->copyadd_add_max; size2 += 1, d += 1)
892
+ {
893
+ d->type1 = XD3_CPY + mode;
894
+ d->size1 = size1;
895
+ d->type2 = XD3_ADD;
896
+ d->size2 = size2;
897
+ }
898
+ }
899
+ }
900
+
901
+ XD3_ASSERT (d - tbl == 256);
902
+ }
903
+
904
+ /* This function generates the static default code table. */
905
+ static const xd3_dinst*
906
+ xd3_rfc3284_code_table (void)
907
+ {
908
+ static xd3_dinst __rfc3284_code_table[256];
909
+
910
+ if (__rfc3284_code_table[0].type1 != XD3_RUN)
911
+ {
912
+ xd3_build_code_table (& __rfc3284_code_table_desc, __rfc3284_code_table);
913
+ }
914
+
915
+ return __rfc3284_code_table;
916
+ }
917
+
918
+ #if XD3_ENCODER
919
+ /* This version of xd3_choose_instruction is hard-coded for the default
920
+ table. */
921
+ static void
922
+ xd3_choose_instruction (xd3_rinst *prev, xd3_rinst *inst)
923
+ {
924
+ switch (inst->type)
925
+ {
926
+ case XD3_RUN:
927
+ inst->code1 = 0;
928
+ break;
929
+
930
+ case XD3_ADD:
931
+ inst->code1 = 1;
932
+
933
+ if (inst->size <= 17)
934
+ {
935
+ inst->code1 += inst->size;
936
+
937
+ if ( (inst->size == 1) &&
938
+ (prev != NULL) &&
939
+ (prev->size == 4) &&
940
+ (prev->type >= XD3_CPY) )
941
+ {
942
+ prev->code2 = 247 + (prev->type - XD3_CPY);
943
+ }
944
+ }
945
+
946
+ break;
947
+
948
+ default:
949
+ {
950
+ uint8_t mode = inst->type - XD3_CPY;
951
+
952
+ XD3_ASSERT (inst->type >= XD3_CPY && inst->type < 12);
953
+
954
+ inst->code1 = 19 + 16 * mode;
955
+
956
+ if (inst->size <= 18 && inst->size >= 4)
957
+ {
958
+ inst->code1 += inst->size - 3;
959
+
960
+ if ( (prev != NULL) &&
961
+ (prev->type == XD3_ADD) &&
962
+ (prev->size <= 4) )
963
+ {
964
+ if ( (inst->size <= 6) &&
965
+ (mode <= 5) )
966
+ {
967
+ prev->code2 = (uint8_t)(163 + (mode * 12) +
968
+ (3 * (prev->size - 1)) +
969
+ (inst->size - 4));
970
+ XD3_ASSERT (prev->code2 <= 234);
971
+ }
972
+ else if ( (inst->size == 4) &&
973
+ (mode >= 6) )
974
+ {
975
+ prev->code2 = 235 + ((mode - 6) * 4) + (prev->size - 1);
976
+
977
+ XD3_ASSERT (prev->code2 <= 246);
978
+ }
979
+ }
980
+ }
981
+
982
+ XD3_ASSERT (inst->code1 <= 162);
983
+ }
984
+ break;
985
+ }
986
+ }
987
+ #endif /* XD3_ENCODER */
988
+
989
+ /***********************************************************************/
990
+
991
+ static inline void
992
+ xd3_swap_uint8p (uint8_t** p1, uint8_t** p2)
993
+ {
994
+ uint8_t *t = (*p1);
995
+ (*p1) = (*p2);
996
+ (*p2) = t;
997
+ }
998
+
999
+ static inline void
1000
+ xd3_swap_usize_t (usize_t* p1, usize_t* p2)
1001
+ {
1002
+ usize_t t = (*p1);
1003
+ (*p1) = (*p2);
1004
+ (*p2) = t;
1005
+ }
1006
+
1007
+ /* It's not constant time, but it computes the log. */
1008
+ static int
1009
+ xd3_check_pow2 (xoff_t value, usize_t *logof)
1010
+ {
1011
+ xoff_t x = 1;
1012
+ usize_t nolog;
1013
+ if (logof == NULL) {
1014
+ logof = &nolog;
1015
+ }
1016
+
1017
+ *logof = 0;
1018
+
1019
+ for (; x != 0; x <<= 1, *logof += 1)
1020
+ {
1021
+ if (x == value)
1022
+ {
1023
+ return 0;
1024
+ }
1025
+ }
1026
+
1027
+ return XD3_INTERNAL;
1028
+ }
1029
+
1030
+ usize_t
1031
+ xd3_pow2_roundup (usize_t x)
1032
+ {
1033
+ usize_t i = 1;
1034
+ while (x > i) {
1035
+ i <<= 1U;
1036
+ }
1037
+ return i;
1038
+ }
1039
+
1040
+ static xoff_t
1041
+ xd3_xoff_roundup (xoff_t x)
1042
+ {
1043
+ xoff_t i = 1;
1044
+ while (x > i) {
1045
+ i <<= 1U;
1046
+ }
1047
+ return i;
1048
+ }
1049
+
1050
+ static usize_t
1051
+ xd3_round_blksize (usize_t sz, usize_t blksz)
1052
+ {
1053
+ usize_t mod = sz & (blksz-1);
1054
+
1055
+ XD3_ASSERT (xd3_check_pow2 (blksz, NULL) == 0);
1056
+
1057
+ if (mod == 0)
1058
+ {
1059
+ return sz;
1060
+ }
1061
+
1062
+ if (sz > USIZE_T_MAXBLKSZ)
1063
+ {
1064
+ return USIZE_T_MAXBLKSZ;
1065
+ }
1066
+
1067
+ return sz + (blksz - mod);
1068
+ }
1069
+
1070
+ /***********************************************************************
1071
+ Adler32 stream function: code copied from Zlib, defined in RFC1950
1072
+ ***********************************************************************/
1073
+
1074
+ #define A32_BASE 65521L /* Largest prime smaller than 2^16 */
1075
+ #define A32_NMAX 5552 /* NMAX is the largest n such that 255n(n+1)/2
1076
+ + (n+1)(BASE-1) <= 2^32-1 */
1077
+
1078
+ #define A32_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
1079
+ #define A32_DO2(buf,i) A32_DO1(buf,i); A32_DO1(buf,i+1);
1080
+ #define A32_DO4(buf,i) A32_DO2(buf,i); A32_DO2(buf,i+2);
1081
+ #define A32_DO8(buf,i) A32_DO4(buf,i); A32_DO4(buf,i+4);
1082
+ #define A32_DO16(buf) A32_DO8(buf,0); A32_DO8(buf,8);
1083
+
1084
+ static uint32_t adler32 (uint32_t adler, const uint8_t *buf, usize_t len)
1085
+ {
1086
+ uint32_t s1 = adler & 0xffffU;
1087
+ uint32_t s2 = (adler >> 16) & 0xffffU;
1088
+ int k;
1089
+
1090
+ while (len > 0)
1091
+ {
1092
+ k = (len < A32_NMAX) ? len : A32_NMAX;
1093
+ len -= k;
1094
+
1095
+ while (k >= 16)
1096
+ {
1097
+ A32_DO16(buf);
1098
+ buf += 16;
1099
+ k -= 16;
1100
+ }
1101
+
1102
+ if (k != 0)
1103
+ {
1104
+ do
1105
+ {
1106
+ s1 += *buf++;
1107
+ s2 += s1;
1108
+ }
1109
+ while (--k);
1110
+ }
1111
+
1112
+ s1 %= A32_BASE;
1113
+ s2 %= A32_BASE;
1114
+ }
1115
+
1116
+ return (s2 << 16) | s1;
1117
+ }
1118
+
1119
+ /***********************************************************************
1120
+ Run-length function
1121
+ ***********************************************************************/
1122
+
1123
+ #if XD3_ENCODER
1124
+ static usize_t
1125
+ xd3_comprun (const uint8_t *seg, usize_t slook, uint8_t *run_cp)
1126
+ {
1127
+ usize_t i;
1128
+ usize_t run_l = 0;
1129
+ uint8_t run_c = 0;
1130
+
1131
+ for (i = 0; i < slook; i += 1)
1132
+ {
1133
+ NEXTRUN(seg[i]);
1134
+ }
1135
+
1136
+ (*run_cp) = run_c;
1137
+
1138
+ return run_l;
1139
+ }
1140
+ #endif
1141
+
1142
+ /***********************************************************************
1143
+ Basic encoder/decoder functions
1144
+ ***********************************************************************/
1145
+
1146
+ #if XD3_ENCODER
1147
+ inline int
1148
+ xd3_emit_byte (xd3_stream *stream,
1149
+ xd3_output **outputp,
1150
+ uint8_t code)
1151
+ {
1152
+ xd3_output *output = (*outputp);
1153
+
1154
+ if (output->next == output->avail)
1155
+ {
1156
+ xd3_output *aoutput;
1157
+
1158
+ if ((aoutput = xd3_alloc_output (stream, output)) == NULL)
1159
+ {
1160
+ return ENOMEM;
1161
+ }
1162
+
1163
+ output = (*outputp) = aoutput;
1164
+ }
1165
+
1166
+ output->base[output->next++] = code;
1167
+
1168
+ return 0;
1169
+ }
1170
+
1171
+ inline int
1172
+ xd3_emit_bytes (xd3_stream *stream,
1173
+ xd3_output **outputp,
1174
+ const uint8_t *base,
1175
+ usize_t size)
1176
+ {
1177
+ xd3_output *output = (*outputp);
1178
+
1179
+ do
1180
+ {
1181
+ usize_t take;
1182
+
1183
+ if (output->next == output->avail)
1184
+ {
1185
+ xd3_output *aoutput;
1186
+
1187
+ if ((aoutput = xd3_alloc_output (stream, output)) == NULL)
1188
+ {
1189
+ return ENOMEM;
1190
+ }
1191
+
1192
+ output = (*outputp) = aoutput;
1193
+ }
1194
+
1195
+ take = xd3_min (output->avail - output->next, size);
1196
+
1197
+ memcpy (output->base + output->next, base, (size_t) take);
1198
+
1199
+ output->next += take;
1200
+ size -= take;
1201
+ base += take;
1202
+ }
1203
+ while (size > 0);
1204
+
1205
+ return 0;
1206
+ }
1207
+ #endif /* XD3_ENCODER */
1208
+
1209
+ /***********************************************************************
1210
+ Address cache stuff
1211
+ ***********************************************************************/
1212
+
1213
+ static int
1214
+ xd3_alloc_cache (xd3_stream *stream)
1215
+ {
1216
+ if (stream->acache.near_array != NULL)
1217
+ {
1218
+ xd3_free (stream, stream->acache.near_array);
1219
+ }
1220
+
1221
+ if (stream->acache.same_array != NULL)
1222
+ {
1223
+ xd3_free (stream, stream->acache.same_array);
1224
+ }
1225
+
1226
+ if (((stream->acache.s_near > 0) &&
1227
+ (stream->acache.near_array = (usize_t*)
1228
+ xd3_alloc (stream, stream->acache.s_near,
1229
+ (usize_t) sizeof (usize_t)))
1230
+ == NULL) ||
1231
+ ((stream->acache.s_same > 0) &&
1232
+ (stream->acache.same_array = (usize_t*)
1233
+ xd3_alloc (stream, stream->acache.s_same * 256,
1234
+ (usize_t) sizeof (usize_t)))
1235
+ == NULL))
1236
+ {
1237
+ return ENOMEM;
1238
+ }
1239
+
1240
+ return 0;
1241
+ }
1242
+
1243
+ void
1244
+ xd3_init_cache (xd3_addr_cache* acache)
1245
+ {
1246
+ if (acache->s_near > 0)
1247
+ {
1248
+ memset (acache->near_array, 0, acache->s_near * sizeof (usize_t));
1249
+ acache->next_slot = 0;
1250
+ }
1251
+
1252
+ if (acache->s_same > 0)
1253
+ {
1254
+ memset (acache->same_array, 0, acache->s_same * 256 * sizeof (usize_t));
1255
+ }
1256
+ }
1257
+
1258
+ static void
1259
+ xd3_update_cache (xd3_addr_cache* acache, usize_t addr)
1260
+ {
1261
+ if (acache->s_near > 0)
1262
+ {
1263
+ acache->near_array[acache->next_slot] = addr;
1264
+ acache->next_slot = (acache->next_slot + 1) % acache->s_near;
1265
+ }
1266
+
1267
+ if (acache->s_same > 0)
1268
+ {
1269
+ acache->same_array[addr % (acache->s_same*256)] = addr;
1270
+ }
1271
+ }
1272
+
1273
+ #if XD3_ENCODER
1274
+ /* OPT: this gets called a lot, can it be optimized? */
1275
+ static int
1276
+ xd3_encode_address (xd3_stream *stream,
1277
+ usize_t addr,
1278
+ usize_t here,
1279
+ uint8_t* mode)
1280
+ {
1281
+ usize_t d, bestd;
1282
+ usize_t i, bestm;
1283
+ int ret;
1284
+ xd3_addr_cache* acache = & stream->acache;
1285
+
1286
+ #define SMALLEST_INT(x) do { if (((x) & ~127U) == 0) { goto good; } } while (0)
1287
+
1288
+ /* Attempt to find the address mode that yields the smallest integer value
1289
+ * for "d", the encoded address value, thereby minimizing the encoded size
1290
+ * of the address. */
1291
+ bestd = addr;
1292
+ bestm = VCD_SELF;
1293
+
1294
+ XD3_ASSERT (addr < here);
1295
+
1296
+ SMALLEST_INT (bestd);
1297
+
1298
+ if ((d = here-addr) < bestd)
1299
+ {
1300
+ bestd = d;
1301
+ bestm = VCD_HERE;
1302
+
1303
+ SMALLEST_INT (bestd);
1304
+ }
1305
+
1306
+ for (i = 0; i < acache->s_near; i += 1)
1307
+ {
1308
+ /* Note: If we used signed computation here, we'd could compte d
1309
+ * and then check (d >= 0 && d < bestd). */
1310
+ if (addr >= acache->near_array[i])
1311
+ {
1312
+ d = addr - acache->near_array[i];
1313
+
1314
+ if (d < bestd)
1315
+ {
1316
+ bestd = d;
1317
+ bestm = i+2; /* 2 counts the VCD_SELF, VCD_HERE modes */
1318
+
1319
+ SMALLEST_INT (bestd);
1320
+ }
1321
+ }
1322
+ }
1323
+
1324
+ if (acache->s_same > 0 &&
1325
+ acache->same_array[d = addr%(acache->s_same*256)] == addr)
1326
+ {
1327
+ bestd = d%256;
1328
+ /* 2 + s_near offsets past the VCD_NEAR modes */
1329
+ bestm = acache->s_near + 2 + d/256;
1330
+
1331
+ if ((ret = xd3_emit_byte (stream, & ADDR_TAIL (stream), bestd)))
1332
+ {
1333
+ return ret;
1334
+ }
1335
+ }
1336
+ else
1337
+ {
1338
+ good:
1339
+
1340
+ if ((ret = xd3_emit_size (stream, & ADDR_TAIL (stream), bestd)))
1341
+ {
1342
+ return ret;
1343
+ }
1344
+ }
1345
+
1346
+ xd3_update_cache (acache, addr);
1347
+
1348
+ (*mode) += bestm;
1349
+
1350
+ return 0;
1351
+ }
1352
+ #endif
1353
+
1354
+ static int
1355
+ xd3_decode_address (xd3_stream *stream, usize_t here,
1356
+ usize_t mode, const uint8_t **inpp,
1357
+ const uint8_t *max, usize_t *valp)
1358
+ {
1359
+ int ret;
1360
+ usize_t same_start = 2 + stream->acache.s_near;
1361
+
1362
+ if (mode < same_start)
1363
+ {
1364
+ if ((ret = xd3_read_size (stream, inpp, max, valp))) { return ret; }
1365
+
1366
+ switch (mode)
1367
+ {
1368
+ case VCD_SELF:
1369
+ break;
1370
+ case VCD_HERE:
1371
+ (*valp) = here - (*valp);
1372
+ break;
1373
+ default:
1374
+ (*valp) += stream->acache.near_array[mode - 2];
1375
+ break;
1376
+ }
1377
+ }
1378
+ else
1379
+ {
1380
+ if (*inpp == max)
1381
+ {
1382
+ stream->msg = "address underflow";
1383
+ return XD3_INVALID_INPUT;
1384
+ }
1385
+
1386
+ mode -= same_start;
1387
+
1388
+ (*valp) = stream->acache.same_array[mode*256 + (**inpp)];
1389
+
1390
+ (*inpp) += 1;
1391
+ }
1392
+
1393
+ xd3_update_cache (& stream->acache, *valp);
1394
+
1395
+ return 0;
1396
+ }
1397
+
1398
+ /***********************************************************************
1399
+ Alloc/free
1400
+ ***********************************************************************/
1401
+
1402
+ static void*
1403
+ __xd3_alloc_func (void* opaque, size_t items, usize_t size)
1404
+ {
1405
+ return malloc (items * (size_t) size);
1406
+ }
1407
+
1408
+ static void
1409
+ __xd3_free_func (void* opaque, void* address)
1410
+ {
1411
+ free (address);
1412
+ }
1413
+
1414
+ static void*
1415
+ xd3_alloc (xd3_stream *stream,
1416
+ usize_t elts,
1417
+ usize_t size)
1418
+ {
1419
+ void *a = stream->alloc (stream->opaque, elts, size);
1420
+
1421
+ if (a != NULL)
1422
+ {
1423
+ IF_DEBUG (stream->alloc_cnt += 1);
1424
+ IF_DEBUG2 (DP(RINT "[stream %p malloc] size %"W"u ptr %p\n",
1425
+ (void*)stream, elts * size, a));
1426
+ }
1427
+ else
1428
+ {
1429
+ stream->msg = "out of memory";
1430
+ }
1431
+
1432
+ return a;
1433
+ }
1434
+
1435
+ static void
1436
+ xd3_free (xd3_stream *stream,
1437
+ void *ptr)
1438
+ {
1439
+ if (ptr != NULL)
1440
+ {
1441
+ IF_DEBUG (stream->free_cnt += 1);
1442
+ XD3_ASSERT (stream->free_cnt <= stream->alloc_cnt);
1443
+ IF_DEBUG2 (DP(RINT "[stream %p free] %p\n",
1444
+ (void*)stream, ptr));
1445
+ stream->free (stream->opaque, ptr);
1446
+ }
1447
+ }
1448
+
1449
+ #if XD3_ENCODER
1450
+ static void*
1451
+ xd3_alloc0 (xd3_stream *stream,
1452
+ usize_t elts,
1453
+ usize_t size)
1454
+ {
1455
+ void *a = xd3_alloc (stream, elts, size);
1456
+
1457
+ if (a != NULL)
1458
+ {
1459
+ memset (a, 0, (size_t) (elts * size));
1460
+ }
1461
+
1462
+ return a;
1463
+ }
1464
+
1465
+ xd3_output*
1466
+ xd3_alloc_output (xd3_stream *stream,
1467
+ xd3_output *old_output)
1468
+ {
1469
+ xd3_output *output;
1470
+ uint8_t *base;
1471
+
1472
+ if (stream->enc_free != NULL)
1473
+ {
1474
+ output = stream->enc_free;
1475
+ stream->enc_free = output->next_page;
1476
+ }
1477
+ else
1478
+ {
1479
+ if ((output = (xd3_output*) xd3_alloc (stream, 1,
1480
+ (usize_t) sizeof (xd3_output)))
1481
+ == NULL)
1482
+ {
1483
+ return NULL;
1484
+ }
1485
+
1486
+ if ((base = (uint8_t*) xd3_alloc (stream, XD3_ALLOCSIZE,
1487
+ sizeof (uint8_t))) == NULL)
1488
+ {
1489
+ xd3_free (stream, output);
1490
+ return NULL;
1491
+ }
1492
+
1493
+ output->base = base;
1494
+ output->avail = XD3_ALLOCSIZE;
1495
+ }
1496
+
1497
+ output->next = 0;
1498
+
1499
+ if (old_output)
1500
+ {
1501
+ old_output->next_page = output;
1502
+ }
1503
+
1504
+ output->next_page = NULL;
1505
+
1506
+ return output;
1507
+ }
1508
+
1509
+ static usize_t
1510
+ xd3_sizeof_output (xd3_output *output)
1511
+ {
1512
+ usize_t s = 0;
1513
+
1514
+ for (; output; output = output->next_page)
1515
+ {
1516
+ s += output->next;
1517
+ }
1518
+
1519
+ return s;
1520
+ }
1521
+
1522
+ static void
1523
+ xd3_freelist_output (xd3_stream *stream,
1524
+ xd3_output *output)
1525
+ {
1526
+ xd3_output *tmp;
1527
+
1528
+ while (output)
1529
+ {
1530
+ tmp = output;
1531
+ output = output->next_page;
1532
+
1533
+ tmp->next = 0;
1534
+ tmp->next_page = stream->enc_free;
1535
+ stream->enc_free = tmp;
1536
+ }
1537
+ }
1538
+
1539
+ static void
1540
+ xd3_free_output (xd3_stream *stream,
1541
+ xd3_output *output)
1542
+ {
1543
+ xd3_output *next;
1544
+
1545
+ again:
1546
+ if (output == NULL)
1547
+ {
1548
+ return;
1549
+ }
1550
+
1551
+ next = output->next_page;
1552
+
1553
+ xd3_free (stream, output->base);
1554
+ xd3_free (stream, output);
1555
+
1556
+ output = next;
1557
+ goto again;
1558
+ }
1559
+ #endif /* XD3_ENCODER */
1560
+
1561
+ void
1562
+ xd3_free_stream (xd3_stream *stream)
1563
+ {
1564
+ xd3_iopt_buflist *blist = stream->iopt_alloc;
1565
+
1566
+ while (blist != NULL)
1567
+ {
1568
+ xd3_iopt_buflist *tmp = blist;
1569
+ blist = blist->next;
1570
+ xd3_free (stream, tmp->buffer);
1571
+ xd3_free (stream, tmp);
1572
+ }
1573
+
1574
+ #if XD3_ENCODER
1575
+ xd3_free (stream, stream->large_table);
1576
+ xd3_free (stream, stream->small_table);
1577
+ xd3_free (stream, stream->large_hash.powers);
1578
+ xd3_free (stream, stream->small_hash.powers);
1579
+ xd3_free (stream, stream->small_prev);
1580
+
1581
+ {
1582
+ int i;
1583
+ for (i = 0; i < ENC_SECTS; i += 1)
1584
+ {
1585
+ xd3_free_output (stream, stream->enc_heads[i]);
1586
+ }
1587
+ xd3_free_output (stream, stream->enc_free);
1588
+ }
1589
+ #endif
1590
+
1591
+ xd3_free (stream, stream->acache.near_array);
1592
+ xd3_free (stream, stream->acache.same_array);
1593
+
1594
+ xd3_free (stream, stream->inst_sect.copied1);
1595
+ xd3_free (stream, stream->addr_sect.copied1);
1596
+ xd3_free (stream, stream->data_sect.copied1);
1597
+
1598
+ if (stream->dec_lastwin != stream->dec_buffer)
1599
+ {
1600
+ xd3_free (stream, (uint8_t*) stream->dec_lastwin);
1601
+ }
1602
+ xd3_free (stream, stream->dec_buffer);
1603
+
1604
+ xd3_free (stream, stream->buf_in);
1605
+ xd3_free (stream, stream->dec_appheader);
1606
+ xd3_free (stream, stream->dec_codetbl);
1607
+ xd3_free (stream, stream->code_table_alloc);
1608
+
1609
+ #if SECONDARY_ANY
1610
+ xd3_free (stream, stream->inst_sect.copied2);
1611
+ xd3_free (stream, stream->addr_sect.copied2);
1612
+ xd3_free (stream, stream->data_sect.copied2);
1613
+
1614
+ if (stream->sec_type != NULL)
1615
+ {
1616
+ stream->sec_type->destroy (stream, stream->sec_stream_d);
1617
+ stream->sec_type->destroy (stream, stream->sec_stream_i);
1618
+ stream->sec_type->destroy (stream, stream->sec_stream_a);
1619
+ }
1620
+ #endif
1621
+
1622
+ xd3_free (stream, stream->whole_target.adds);
1623
+ xd3_free (stream, stream->whole_target.inst);
1624
+ xd3_free (stream, stream->whole_target.wininfo);
1625
+
1626
+ XD3_ASSERT (stream->alloc_cnt == stream->free_cnt);
1627
+
1628
+ memset (stream, 0, sizeof (xd3_stream));
1629
+ }
1630
+
1631
+ #if (XD3_DEBUG > 1 || VCDIFF_TOOLS)
1632
+ static const char*
1633
+ xd3_rtype_to_string (xd3_rtype type, int print_mode)
1634
+ {
1635
+ switch (type)
1636
+ {
1637
+ case XD3_NOOP:
1638
+ return "NOOP ";
1639
+ case XD3_RUN:
1640
+ return "RUN ";
1641
+ case XD3_ADD:
1642
+ return "ADD ";
1643
+ default: break;
1644
+ }
1645
+ if (! print_mode)
1646
+ {
1647
+ return "CPY ";
1648
+ }
1649
+ switch (type)
1650
+ {
1651
+ case XD3_CPY + 0: return "CPY_0";
1652
+ case XD3_CPY + 1: return "CPY_1";
1653
+ case XD3_CPY + 2: return "CPY_2";
1654
+ case XD3_CPY + 3: return "CPY_3";
1655
+ case XD3_CPY + 4: return "CPY_4";
1656
+ case XD3_CPY + 5: return "CPY_5";
1657
+ case XD3_CPY + 6: return "CPY_6";
1658
+ case XD3_CPY + 7: return "CPY_7";
1659
+ case XD3_CPY + 8: return "CPY_8";
1660
+ case XD3_CPY + 9: return "CPY_9";
1661
+ default: return "CPY>9";
1662
+ }
1663
+ }
1664
+ #endif
1665
+
1666
+ /****************************************************************
1667
+ Stream configuration
1668
+ ******************************************************************/
1669
+
1670
+ int
1671
+ xd3_config_stream(xd3_stream *stream,
1672
+ xd3_config *config)
1673
+ {
1674
+ int ret;
1675
+ xd3_config defcfg;
1676
+ xd3_smatcher *smatcher = &stream->smatcher;
1677
+
1678
+ if (config == NULL)
1679
+ {
1680
+ config = & defcfg;
1681
+ memset (config, 0, sizeof (*config));
1682
+ }
1683
+
1684
+ /* Initial setup: no error checks yet */
1685
+ memset (stream, 0, sizeof (*stream));
1686
+
1687
+ stream->winsize = config->winsize ? config->winsize : XD3_DEFAULT_WINSIZE;
1688
+ stream->sprevsz = config->sprevsz ? config->sprevsz : XD3_DEFAULT_SPREVSZ;
1689
+
1690
+ if (config->iopt_size == 0)
1691
+ {
1692
+ stream->iopt_size = XD3_ALLOCSIZE / sizeof(xd3_rinst);
1693
+ stream->iopt_unlimited = 1;
1694
+ }
1695
+ else
1696
+ {
1697
+ stream->iopt_size = config->iopt_size;
1698
+ }
1699
+
1700
+ stream->getblk = config->getblk;
1701
+ stream->alloc = config->alloc ? config->alloc : __xd3_alloc_func;
1702
+ stream->free = config->freef ? config->freef : __xd3_free_func;
1703
+ stream->opaque = config->opaque;
1704
+ stream->flags = config->flags;
1705
+
1706
+ /* Secondary setup. */
1707
+ stream->sec_data = config->sec_data;
1708
+ stream->sec_inst = config->sec_inst;
1709
+ stream->sec_addr = config->sec_addr;
1710
+
1711
+ stream->sec_data.data_type = DATA_SECTION;
1712
+ stream->sec_inst.data_type = INST_SECTION;
1713
+ stream->sec_addr.data_type = ADDR_SECTION;
1714
+
1715
+ /* Check static sizes. */
1716
+ if (sizeof (usize_t) != SIZEOF_USIZE_T ||
1717
+ sizeof (xoff_t) != SIZEOF_XOFF_T ||
1718
+ (ret = xd3_check_pow2(XD3_ALLOCSIZE, NULL)))
1719
+ {
1720
+ stream->msg = "incorrect compilation: wrong integer sizes";
1721
+ return XD3_INTERNAL;
1722
+ }
1723
+
1724
+ /* Check/set secondary compressor. */
1725
+ switch (stream->flags & XD3_SEC_TYPE)
1726
+ {
1727
+ case 0:
1728
+ if (stream->flags & XD3_SEC_NOALL)
1729
+ {
1730
+ stream->msg = "XD3_SEC flags require a secondary compressor type";
1731
+ return XD3_INTERNAL;
1732
+ }
1733
+ break;
1734
+ case XD3_SEC_FGK:
1735
+ FGK_CASE (stream);
1736
+ case XD3_SEC_DJW:
1737
+ DJW_CASE (stream);
1738
+ case XD3_SEC_LZMA:
1739
+ LZMA_CASE (stream);
1740
+ default:
1741
+ stream->msg = "too many secondary compressor types set";
1742
+ return XD3_INTERNAL;
1743
+ }
1744
+
1745
+ stream->code_table_desc = & __rfc3284_code_table_desc;
1746
+ stream->code_table_func = xd3_rfc3284_code_table;
1747
+
1748
+ /* Check sprevsz */
1749
+ if (smatcher->small_chain == 1 &&
1750
+ smatcher->small_lchain == 1)
1751
+ {
1752
+ stream->sprevsz = 0;
1753
+ }
1754
+ else
1755
+ {
1756
+ if ((ret = xd3_check_pow2 (stream->sprevsz, NULL)))
1757
+ {
1758
+ stream->msg = "sprevsz is required to be a power of two";
1759
+ return XD3_INTERNAL;
1760
+ }
1761
+
1762
+ stream->sprevmask = stream->sprevsz - 1;
1763
+ }
1764
+
1765
+ /* Default scanner settings. */
1766
+ #if XD3_ENCODER
1767
+ switch (config->smatch_cfg)
1768
+ {
1769
+ IF_BUILD_SOFT(case XD3_SMATCH_SOFT:
1770
+ {
1771
+ *smatcher = config->smatcher_soft;
1772
+ smatcher->string_match = __smatcher_soft.string_match;
1773
+ smatcher->name = __smatcher_soft.name;
1774
+ if (smatcher->large_look < MIN_MATCH ||
1775
+ smatcher->large_step < 1 ||
1776
+ smatcher->small_look < MIN_MATCH)
1777
+ {
1778
+ stream->msg = "invalid soft string-match config";
1779
+ return XD3_INVALID;
1780
+ }
1781
+ break;
1782
+ })
1783
+
1784
+ IF_BUILD_DEFAULT(case XD3_SMATCH_DEFAULT:
1785
+ *smatcher = __smatcher_default;
1786
+ break;)
1787
+ IF_BUILD_SLOW(case XD3_SMATCH_SLOW:
1788
+ *smatcher = __smatcher_slow;
1789
+ break;)
1790
+ IF_BUILD_FASTEST(case XD3_SMATCH_FASTEST:
1791
+ *smatcher = __smatcher_fastest;
1792
+ break;)
1793
+ IF_BUILD_FASTER(case XD3_SMATCH_FASTER:
1794
+ *smatcher = __smatcher_faster;
1795
+ break;)
1796
+ IF_BUILD_FAST(case XD3_SMATCH_FAST:
1797
+ *smatcher = __smatcher_fast;
1798
+ break;)
1799
+ default:
1800
+ stream->msg = "invalid string match config type";
1801
+ return XD3_INTERNAL;
1802
+ }
1803
+
1804
+ if (config->smatch_cfg == XD3_SMATCH_DEFAULT &&
1805
+ (stream->flags & XD3_COMPLEVEL_MASK) != 0)
1806
+ {
1807
+ int level = (stream->flags & XD3_COMPLEVEL_MASK) >> XD3_COMPLEVEL_SHIFT;
1808
+
1809
+ switch (level)
1810
+ {
1811
+ case 1:
1812
+ IF_BUILD_FASTEST(*smatcher = __smatcher_fastest;
1813
+ break;)
1814
+ case 2:
1815
+ IF_BUILD_FASTER(*smatcher = __smatcher_faster;
1816
+ break;)
1817
+ case 3: case 4: case 5:
1818
+ IF_BUILD_FAST(*smatcher = __smatcher_fast;
1819
+ break;)
1820
+ case 6:
1821
+ IF_BUILD_DEFAULT(*smatcher = __smatcher_default;
1822
+ break;)
1823
+ default:
1824
+ IF_BUILD_SLOW(*smatcher = __smatcher_slow;
1825
+ break;)
1826
+ IF_BUILD_DEFAULT(*smatcher = __smatcher_default;
1827
+ break;)
1828
+ IF_BUILD_FAST(*smatcher = __smatcher_fast;
1829
+ break;)
1830
+ IF_BUILD_FASTER(*smatcher = __smatcher_faster;
1831
+ break;)
1832
+ IF_BUILD_FASTEST(*smatcher = __smatcher_fastest;
1833
+ break;)
1834
+ }
1835
+ }
1836
+ #endif
1837
+
1838
+ return 0;
1839
+ }
1840
+
1841
+ /***********************************************************
1842
+ Getblk interface
1843
+ ***********************************************************/
1844
+
1845
+ inline
1846
+ xoff_t xd3_source_eof(const xd3_source *src)
1847
+ {
1848
+ xoff_t r = (src->max_blkno << src->shiftby) + (xoff_t)src->onlastblk;
1849
+ return r;
1850
+ }
1851
+
1852
+ inline
1853
+ usize_t xd3_bytes_on_srcblk (xd3_source *src, xoff_t blkno)
1854
+ {
1855
+ usize_t r = (blkno == src->max_blkno ?
1856
+ src->onlastblk :
1857
+ src->blksize);
1858
+ return r;
1859
+ }
1860
+
1861
+ /* This function interfaces with the client getblk function, checks
1862
+ * its results, updates max_blkno, onlastblk, eof_known. */
1863
+ static int
1864
+ xd3_getblk (xd3_stream *stream, xoff_t blkno)
1865
+ {
1866
+ int ret;
1867
+ xd3_source *source = stream->src;
1868
+
1869
+ if (source->curblk == NULL || blkno != source->curblkno)
1870
+ {
1871
+ source->getblkno = blkno;
1872
+
1873
+ if (stream->getblk == NULL)
1874
+ {
1875
+ IF_DEBUG2 (DP(RINT "[getblk] XD3_GETSRCBLK %"Q"u\n", blkno));
1876
+ stream->msg = "getblk source input";
1877
+ return XD3_GETSRCBLK;
1878
+ }
1879
+
1880
+ ret = stream->getblk (stream, source, blkno);
1881
+ if (ret != 0)
1882
+ {
1883
+ IF_DEBUG2 (DP(RINT "[getblk] app error blkno %"Q"u: %s\n",
1884
+ blkno, xd3_strerror (ret)));
1885
+ return ret;
1886
+ }
1887
+
1888
+ IF_DEBUG2 (DP(RINT "[getblk] read source block %"Q"u onblk "
1889
+ "%"W"u blksize %"W"u max_blkno %"Q"u\n", blkno, source->onblk,
1890
+ source->blksize, source->max_blkno));
1891
+ }
1892
+
1893
+ if (blkno > source->max_blkno)
1894
+ {
1895
+ source->max_blkno = blkno;
1896
+
1897
+ if (source->onblk == source->blksize)
1898
+ {
1899
+ IF_DEBUG1 (DP(RINT "[getblk] full source blkno %"Q"u: "
1900
+ "source length unknown %"Q"u\n",
1901
+ blkno,
1902
+ xd3_source_eof (source)));
1903
+ }
1904
+ else if (!source->eof_known)
1905
+ {
1906
+ IF_DEBUG1 (DP(RINT "[getblk] eof block has %"W"u bytes; "
1907
+ "source length known %"Q"u\n",
1908
+ xd3_bytes_on_srcblk (source, blkno),
1909
+ xd3_source_eof (source)));
1910
+ source->eof_known = 1;
1911
+ }
1912
+ }
1913
+
1914
+ XD3_ASSERT (source->curblk != NULL);
1915
+
1916
+ if (blkno == source->max_blkno)
1917
+ {
1918
+ /* In case the application sets the source as 1 block w/ a
1919
+ * preset buffer. */
1920
+ source->onlastblk = source->onblk;
1921
+ }
1922
+ return 0;
1923
+ }
1924
+
1925
+ /***********************************************************
1926
+ Stream open/close
1927
+ ***************************************************************/
1928
+
1929
+ int
1930
+ xd3_set_source (xd3_stream *stream,
1931
+ xd3_source *src)
1932
+ {
1933
+ usize_t shiftby;
1934
+
1935
+ stream->src = src;
1936
+ src->srclen = 0;
1937
+ src->srcbase = 0;
1938
+
1939
+ /* Enforce power-of-two blocksize so that source-block number
1940
+ * calculations are cheap. */
1941
+ if (xd3_check_pow2 (src->blksize, &shiftby) != 0)
1942
+ {
1943
+ src->blksize = xd3_pow2_roundup(src->blksize);
1944
+ xd3_check_pow2 (src->blksize, &shiftby);
1945
+ IF_DEBUG1 (DP(RINT "raising src_blksz to %"W"u\n", src->blksize));
1946
+ }
1947
+
1948
+ src->shiftby = shiftby;
1949
+ src->maskby = (1ULL << shiftby) - 1ULL;
1950
+
1951
+ if (xd3_check_pow2 (src->max_winsize, NULL) != 0)
1952
+ {
1953
+ src->max_winsize = xd3_xoff_roundup(src->max_winsize);
1954
+ IF_DEBUG1 (DP(RINT "raising src_maxsize to %"W"u\n", src->blksize));
1955
+ }
1956
+ src->max_winsize = xd3_max (src->max_winsize, XD3_ALLOCSIZE);
1957
+ return 0;
1958
+ }
1959
+
1960
+ int
1961
+ xd3_set_source_and_size (xd3_stream *stream,
1962
+ xd3_source *user_source,
1963
+ xoff_t source_size) {
1964
+ int ret = xd3_set_source (stream, user_source);
1965
+ if (ret == 0)
1966
+ {
1967
+ stream->src->eof_known = 1;
1968
+ IF_DEBUG2 (DP(RINT "[set source] size known %"Q"u\n",
1969
+ source_size));
1970
+ xd3_blksize_div(source_size,
1971
+ stream->src,
1972
+ &stream->src->max_blkno,
1973
+ &stream->src->onlastblk);
1974
+
1975
+ IF_DEBUG1 (DP(RINT "[set source] size known %"Q"u max_blkno %"Q"u\n",
1976
+ source_size, stream->src->max_blkno));
1977
+ }
1978
+ return ret;
1979
+ }
1980
+
1981
+ void
1982
+ xd3_abort_stream (xd3_stream *stream)
1983
+ {
1984
+ stream->dec_state = DEC_ABORTED;
1985
+ stream->enc_state = ENC_ABORTED;
1986
+ }
1987
+
1988
+ int
1989
+ xd3_close_stream (xd3_stream *stream)
1990
+ {
1991
+ if (stream->enc_state != 0 && stream->enc_state != ENC_ABORTED)
1992
+ {
1993
+ if (stream->buf_leftover != NULL)
1994
+ {
1995
+ stream->msg = "encoding is incomplete";
1996
+ return XD3_INTERNAL;
1997
+ }
1998
+
1999
+ if (stream->enc_state == ENC_POSTWIN)
2000
+ {
2001
+ #if XD3_ENCODER
2002
+ xd3_encode_reset (stream);
2003
+ #endif
2004
+ stream->current_window += 1;
2005
+ stream->enc_state = ENC_INPUT;
2006
+ }
2007
+
2008
+ /* If encoding, should be ready for more input but not actually
2009
+ have any. */
2010
+ if (stream->enc_state != ENC_INPUT || stream->avail_in != 0)
2011
+ {
2012
+ stream->msg = "encoding is incomplete";
2013
+ return XD3_INTERNAL;
2014
+ }
2015
+ }
2016
+ else
2017
+ {
2018
+ switch (stream->dec_state)
2019
+ {
2020
+ case DEC_VCHEAD:
2021
+ case DEC_WININD:
2022
+ /* TODO: Address the zero-byte ambiguity. Does the encoder
2023
+ * emit a window or not? If so, then catch an error here.
2024
+ * If not, need another routine to say
2025
+ * decode_at_least_one_if_empty. */
2026
+ case DEC_ABORTED:
2027
+ break;
2028
+ default:
2029
+ /* If decoding, should be ready for the next window. */
2030
+ stream->msg = "eof in decode";
2031
+ return XD3_INVALID_INPUT;
2032
+ }
2033
+ }
2034
+
2035
+ return 0;
2036
+ }
2037
+
2038
+ /**************************************************************
2039
+ Application header
2040
+ ****************************************************************/
2041
+
2042
+ int
2043
+ xd3_get_appheader (xd3_stream *stream,
2044
+ uint8_t **data,
2045
+ usize_t *size)
2046
+ {
2047
+ if (stream->dec_state < DEC_WININD)
2048
+ {
2049
+ stream->msg = "application header not available";
2050
+ return XD3_INTERNAL;
2051
+ }
2052
+
2053
+ (*data) = stream->dec_appheader;
2054
+ (*size) = stream->dec_appheadsz;
2055
+ return 0;
2056
+ }
2057
+
2058
+ /**********************************************************
2059
+ Decoder stuff
2060
+ *************************************************/
2061
+
2062
+ #include "xdelta3-decode.h"
2063
+
2064
+ /****************************************************************
2065
+ Encoder stuff
2066
+ *****************************************************************/
2067
+
2068
+ #if XD3_ENCODER
2069
+ void
2070
+ xd3_set_appheader (xd3_stream *stream,
2071
+ const uint8_t *data,
2072
+ usize_t size)
2073
+ {
2074
+ stream->enc_appheader = data;
2075
+ stream->enc_appheadsz = size;
2076
+ }
2077
+
2078
+ #if XD3_DEBUG
2079
+ static int
2080
+ xd3_iopt_check (xd3_stream *stream)
2081
+ {
2082
+ usize_t ul = xd3_rlist_length (& stream->iopt_used);
2083
+ usize_t fl = xd3_rlist_length (& stream->iopt_free);
2084
+
2085
+ return (ul + fl + (stream->iout ? 1 : 0)) == stream->iopt_size;
2086
+ }
2087
+ #endif
2088
+
2089
+ static xd3_rinst*
2090
+ xd3_iopt_free (xd3_stream *stream, xd3_rinst *i)
2091
+ {
2092
+ xd3_rinst *n = xd3_rlist_remove (i);
2093
+ xd3_rlist_push_back (& stream->iopt_free, i);
2094
+ return n;
2095
+ }
2096
+
2097
+ static void
2098
+ xd3_iopt_free_nonadd (xd3_stream *stream, xd3_rinst *i)
2099
+ {
2100
+ if (i->type != XD3_ADD)
2101
+ {
2102
+ xd3_rlist_push_back (& stream->iopt_free, i);
2103
+ }
2104
+ }
2105
+
2106
+ /* When an instruction is ready to flush from the iopt buffer, this
2107
+ * function is called to produce an encoding. It writes the
2108
+ * instruction plus size, address, and data to the various encoding
2109
+ * sections. */
2110
+ static int
2111
+ xd3_iopt_finish_encoding (xd3_stream *stream, xd3_rinst *inst)
2112
+ {
2113
+ int ret;
2114
+
2115
+ /* Check for input overflow. */
2116
+ XD3_ASSERT (inst->pos + inst->size <= stream->avail_in);
2117
+
2118
+ switch (inst->type)
2119
+ {
2120
+ case XD3_CPY:
2121
+ {
2122
+ /* the address may have an offset if there is a source window. */
2123
+ usize_t addr;
2124
+ xd3_source *src = stream->src;
2125
+
2126
+ if (src != NULL)
2127
+ {
2128
+ /* If there is a source copy, the source must have its
2129
+ * source window decided before we can encode. This can
2130
+ * be bad -- we have to make this decision even if no
2131
+ * source matches have been found. */
2132
+ if (stream->srcwin_decided == 0)
2133
+ {
2134
+ if ((ret = xd3_srcwin_setup (stream))) { return ret; }
2135
+ }
2136
+ else
2137
+ {
2138
+ stream->srcwin_decided_early = (!stream->src->eof_known ||
2139
+ (stream->srcwin_cksum_pos <
2140
+ xd3_source_eof (stream->src)));
2141
+ }
2142
+
2143
+ /* xtra field indicates the copy is from the source */
2144
+ if (inst->xtra)
2145
+ {
2146
+ XD3_ASSERT (inst->addr >= src->srcbase);
2147
+ XD3_ASSERT (inst->addr + inst->size <=
2148
+ src->srcbase + src->srclen);
2149
+ addr = inst->addr - src->srcbase;
2150
+ stream->n_scpy += 1;
2151
+ stream->l_scpy += inst->size;
2152
+ }
2153
+ else
2154
+ {
2155
+ /* with source window: target copy address is offset
2156
+ * by taroff. */
2157
+ addr = stream->taroff + inst->addr;
2158
+ stream->n_tcpy += 1;
2159
+ stream->l_tcpy += inst->size;
2160
+ }
2161
+ }
2162
+ else
2163
+ {
2164
+ addr = inst->addr;
2165
+ stream->n_tcpy += 1;
2166
+ stream->l_tcpy += inst->size;
2167
+ }
2168
+
2169
+ /* Note: used to assert inst->size >= MIN_MATCH, but not true
2170
+ * for merge operations & identical match heuristics. */
2171
+ /* the "here" position is always offset by taroff */
2172
+ if ((ret = xd3_encode_address (stream, addr, inst->pos + stream->taroff,
2173
+ & inst->type)))
2174
+ {
2175
+ return ret;
2176
+ }
2177
+
2178
+ IF_DEBUG2 ({
2179
+ static int cnt;
2180
+ DP(RINT "[iopt copy:%d] pos %"Q"u-%"Q"u addr %"Q"u-%"Q"u size %"W"u\n",
2181
+ cnt++,
2182
+ stream->total_in + inst->pos,
2183
+ stream->total_in + inst->pos + inst->size,
2184
+ inst->addr, inst->addr + inst->size, inst->size);
2185
+ });
2186
+ break;
2187
+ }
2188
+ case XD3_RUN:
2189
+ {
2190
+ if ((ret = xd3_emit_byte (stream, & DATA_TAIL (stream), inst->xtra))) { return ret; }
2191
+
2192
+ stream->n_run += 1;
2193
+ stream->l_run += inst->size;
2194
+
2195
+ IF_DEBUG2 ({
2196
+ static int cnt;
2197
+ DP(RINT "[iopt run:%d] pos %"Q"u size %"W"u\n", cnt++, stream->total_in + inst->pos, inst->size);
2198
+ });
2199
+ break;
2200
+ }
2201
+ case XD3_ADD:
2202
+ {
2203
+ if ((ret = xd3_emit_bytes (stream, & DATA_TAIL (stream),
2204
+ stream->next_in + inst->pos, inst->size))) { return ret; }
2205
+
2206
+ stream->n_add += 1;
2207
+ stream->l_add += inst->size;
2208
+
2209
+ IF_DEBUG2 ({
2210
+ static int cnt;
2211
+ DP(RINT "[iopt add:%d] pos %"Q"u size %"W"u\n", cnt++, stream->total_in + inst->pos, inst->size);
2212
+ });
2213
+
2214
+ break;
2215
+ }
2216
+ }
2217
+
2218
+ /* This is the only place stream->unencoded_offset is incremented. */
2219
+ XD3_ASSERT (stream->unencoded_offset == inst->pos);
2220
+ stream->unencoded_offset += inst->size;
2221
+
2222
+ inst->code2 = 0;
2223
+
2224
+ XD3_CHOOSE_INSTRUCTION (stream, stream->iout, inst);
2225
+
2226
+ if (stream->iout != NULL)
2227
+ {
2228
+ if (stream->iout->code2 != 0)
2229
+ {
2230
+ if ((ret = xd3_emit_double (stream, stream->iout, inst,
2231
+ stream->iout->code2))) { return ret; }
2232
+
2233
+ xd3_iopt_free_nonadd (stream, stream->iout);
2234
+ xd3_iopt_free_nonadd (stream, inst);
2235
+ stream->iout = NULL;
2236
+ return 0;
2237
+ }
2238
+ else
2239
+ {
2240
+ if ((ret = xd3_emit_single (stream, stream->iout, stream->iout->code1))) { return ret; }
2241
+
2242
+ xd3_iopt_free_nonadd (stream, stream->iout);
2243
+ }
2244
+ }
2245
+
2246
+ stream->iout = inst;
2247
+
2248
+ return 0;
2249
+ }
2250
+
2251
+ /* This possibly encodes an add instruction, iadd, which must remain
2252
+ * on the stack until the following call to
2253
+ * xd3_iopt_finish_encoding. */
2254
+ static int
2255
+ xd3_iopt_add (xd3_stream *stream, usize_t pos, xd3_rinst *iadd)
2256
+ {
2257
+ int ret;
2258
+ usize_t off = stream->unencoded_offset;
2259
+
2260
+ if (pos > off)
2261
+ {
2262
+ iadd->type = XD3_ADD;
2263
+ iadd->pos = off;
2264
+ iadd->size = pos - off;
2265
+
2266
+ if ((ret = xd3_iopt_finish_encoding (stream, iadd))) { return ret; }
2267
+ }
2268
+
2269
+ return 0;
2270
+ }
2271
+
2272
+ /* This function calls xd3_iopt_finish_encoding to finish encoding an
2273
+ * instruction, and it may also produce an add instruction for an
2274
+ * unmatched region. */
2275
+ static int
2276
+ xd3_iopt_add_encoding (xd3_stream *stream, xd3_rinst *inst)
2277
+ {
2278
+ int ret;
2279
+ xd3_rinst iadd;
2280
+
2281
+ if ((ret = xd3_iopt_add (stream, inst->pos, & iadd))) { return ret; }
2282
+
2283
+ if ((ret = xd3_iopt_finish_encoding (stream, inst))) { return ret; }
2284
+
2285
+ return 0;
2286
+ }
2287
+
2288
+ /* Generates a final add instruction to encode the remaining input. */
2289
+ static int
2290
+ xd3_iopt_add_finalize (xd3_stream *stream)
2291
+ {
2292
+ int ret;
2293
+ xd3_rinst iadd;
2294
+
2295
+ if ((ret = xd3_iopt_add (stream, stream->avail_in, & iadd))) { return ret; }
2296
+
2297
+ if (stream->iout)
2298
+ {
2299
+ if ((ret = xd3_emit_single (stream, stream->iout, stream->iout->code1))) { return ret; }
2300
+
2301
+ xd3_iopt_free_nonadd (stream, stream->iout);
2302
+ stream->iout = NULL;
2303
+ }
2304
+
2305
+ return 0;
2306
+ }
2307
+
2308
+ /* Compact the instruction buffer by choosing the best non-overlapping
2309
+ * instructions when lazy string-matching. There are no ADDs in the
2310
+ * iopt buffer because those are synthesized in xd3_iopt_add_encoding
2311
+ * and during xd3_iopt_add_finalize. */
2312
+ static int
2313
+ xd3_iopt_flush_instructions (xd3_stream *stream, int force)
2314
+ {
2315
+ xd3_rinst *r1 = xd3_rlist_front (& stream->iopt_used);
2316
+ xd3_rinst *r2;
2317
+ xd3_rinst *r3;
2318
+ usize_t r1end;
2319
+ usize_t r2end;
2320
+ usize_t r2off;
2321
+ usize_t r2moff;
2322
+ usize_t gap;
2323
+ usize_t flushed;
2324
+ int ret;
2325
+
2326
+ XD3_ASSERT (xd3_iopt_check (stream));
2327
+
2328
+ /* Note: once tried to skip this step if it's possible to assert
2329
+ * there are no overlapping instructions. Doesn't work because
2330
+ * xd3_opt_erase leaves overlapping instructions. */
2331
+ while (! xd3_rlist_end (& stream->iopt_used, r1) &&
2332
+ ! xd3_rlist_end (& stream->iopt_used, r2 = xd3_rlist_next (r1)))
2333
+ {
2334
+ r1end = r1->pos + r1->size;
2335
+
2336
+ /* If the instructions do not overlap, continue. */
2337
+ if (r1end <= r2->pos)
2338
+ {
2339
+ r1 = r2;
2340
+ continue;
2341
+ }
2342
+
2343
+ r2end = r2->pos + r2->size;
2344
+
2345
+ /* The min_match adjustments prevent this. */
2346
+ XD3_ASSERT (r2end > (r1end + LEAST_MATCH_INCR));
2347
+
2348
+ /* If r3 is available... */
2349
+ if (! xd3_rlist_end (& stream->iopt_used, r3 = xd3_rlist_next (r2)))
2350
+ {
2351
+ /* If r3 starts before r1 finishes or just about, r2 is irrelevant */
2352
+ if (r3->pos <= r1end + 1)
2353
+ {
2354
+ xd3_iopt_free (stream, r2);
2355
+ continue;
2356
+ }
2357
+ }
2358
+ else if (! force)
2359
+ {
2360
+ /* Unless force, end the loop when r3 is not available. */
2361
+ break;
2362
+ }
2363
+
2364
+ r2off = r2->pos - r1->pos;
2365
+ r2moff = r2end - r1end;
2366
+ gap = r2end - r1->pos;
2367
+
2368
+ /* If the two matches overlap almost entirely, choose the better match
2369
+ * and discard the other. The else branch can still create inefficient
2370
+ * copies, e.g., a 4-byte copy that takes 4 bytes to encode, which
2371
+ * xd3_smatch() wouldn't allow by its crude efficiency check. However,
2372
+ * in this case there are adjacent copies which mean the add would cost
2373
+ * one extra byte. Allow the inefficiency here. */
2374
+ if (gap < 2*MIN_MATCH || r2moff <= 2 || r2off <= 2)
2375
+ {
2376
+ /* Only one match should be used, choose the longer one. */
2377
+ if (r1->size < r2->size)
2378
+ {
2379
+ xd3_iopt_free (stream, r1);
2380
+ r1 = r2;
2381
+ }
2382
+ else
2383
+ {
2384
+ /* We are guaranteed that r1 does not overlap now, so advance past r2 */
2385
+ r1 = xd3_iopt_free (stream, r2);
2386
+ }
2387
+ continue;
2388
+ }
2389
+ else
2390
+ {
2391
+ /* Shorten one of the instructions -- could be optimized
2392
+ * based on the address cache. */
2393
+ usize_t average;
2394
+ usize_t newsize;
2395
+ usize_t adjust1;
2396
+
2397
+ XD3_ASSERT (r1end > r2->pos && r2end > r1->pos);
2398
+
2399
+ /* Try to balance the length of both instructions, but avoid
2400
+ * making both longer than MAX_MATCH_SPLIT . */
2401
+ average = gap / 2;
2402
+ newsize = xd3_min (MAX_MATCH_SPLIT, gap - average);
2403
+
2404
+ /* Should be possible to simplify this code. */
2405
+ if (newsize > r1->size)
2406
+ {
2407
+ /* shorten r2 */
2408
+ adjust1 = r1end - r2->pos;
2409
+ }
2410
+ else if (newsize > r2->size)
2411
+ {
2412
+ /* shorten r1 */
2413
+ adjust1 = r1end - r2->pos;
2414
+
2415
+ XD3_ASSERT (r1->size > adjust1);
2416
+
2417
+ r1->size -= adjust1;
2418
+
2419
+ /* don't shorten r2 */
2420
+ adjust1 = 0;
2421
+ }
2422
+ else
2423
+ {
2424
+ /* shorten r1 */
2425
+ adjust1 = r1->size - newsize;
2426
+
2427
+ if (r2->pos > r1end - adjust1)
2428
+ {
2429
+ adjust1 -= r2->pos - (r1end - adjust1);
2430
+ }
2431
+
2432
+ XD3_ASSERT (r1->size > adjust1);
2433
+
2434
+ r1->size -= adjust1;
2435
+
2436
+ /* shorten r2 */
2437
+ XD3_ASSERT (r1->pos + r1->size >= r2->pos);
2438
+
2439
+ adjust1 = r1->pos + r1->size - r2->pos;
2440
+ }
2441
+
2442
+ /* Fallthrough above if-else, shorten r2 */
2443
+ XD3_ASSERT (r2->size > adjust1);
2444
+
2445
+ r2->size -= adjust1;
2446
+ r2->pos += adjust1;
2447
+ r2->addr += adjust1;
2448
+
2449
+ XD3_ASSERT (r1->size >= MIN_MATCH);
2450
+ XD3_ASSERT (r2->size >= MIN_MATCH);
2451
+
2452
+ r1 = r2;
2453
+ }
2454
+ }
2455
+
2456
+ XD3_ASSERT (xd3_iopt_check (stream));
2457
+
2458
+ /* If forcing, pick instructions until the list is empty, otherwise
2459
+ * this empties 50% of the queue. */
2460
+ for (flushed = 0; ! xd3_rlist_empty (& stream->iopt_used); )
2461
+ {
2462
+ xd3_rinst *renc = xd3_rlist_pop_front (& stream->iopt_used);
2463
+ if ((ret = xd3_iopt_add_encoding (stream, renc)))
2464
+ {
2465
+ return ret;
2466
+ }
2467
+
2468
+ if (! force)
2469
+ {
2470
+ if (++flushed > stream->iopt_size / 2)
2471
+ {
2472
+ break;
2473
+ }
2474
+
2475
+ /* If there are only two instructions remaining, break,
2476
+ * because they were not optimized. This means there were
2477
+ * more than 50% eliminated by the loop above. */
2478
+ r1 = xd3_rlist_front (& stream->iopt_used);
2479
+ if (xd3_rlist_end(& stream->iopt_used, r1) ||
2480
+ xd3_rlist_end(& stream->iopt_used, r2 = xd3_rlist_next (r1)) ||
2481
+ xd3_rlist_end(& stream->iopt_used, r3 = xd3_rlist_next (r2)))
2482
+ {
2483
+ break;
2484
+ }
2485
+ }
2486
+ }
2487
+
2488
+ XD3_ASSERT (xd3_iopt_check (stream));
2489
+
2490
+ XD3_ASSERT (!force || xd3_rlist_length (& stream->iopt_used) == 0);
2491
+
2492
+ return 0;
2493
+ }
2494
+
2495
+ static int
2496
+ xd3_iopt_get_slot (xd3_stream *stream, xd3_rinst** iptr)
2497
+ {
2498
+ xd3_rinst *i;
2499
+ int ret;
2500
+
2501
+ if (xd3_rlist_empty (& stream->iopt_free))
2502
+ {
2503
+ if (stream->iopt_unlimited)
2504
+ {
2505
+ usize_t elts = XD3_ALLOCSIZE / sizeof(xd3_rinst);
2506
+
2507
+ if ((ret = xd3_alloc_iopt (stream, elts)))
2508
+ {
2509
+ return ret;
2510
+ }
2511
+
2512
+ stream->iopt_size += elts;
2513
+ }
2514
+ else
2515
+ {
2516
+ if ((ret = xd3_iopt_flush_instructions (stream, 0))) { return ret; }
2517
+
2518
+ XD3_ASSERT (! xd3_rlist_empty (& stream->iopt_free));
2519
+ }
2520
+ }
2521
+
2522
+ i = xd3_rlist_pop_back (& stream->iopt_free);
2523
+
2524
+ xd3_rlist_push_back (& stream->iopt_used, i);
2525
+
2526
+ (*iptr) = i;
2527
+
2528
+ ++stream->i_slots_used;
2529
+
2530
+ return 0;
2531
+ }
2532
+
2533
+ /* A copy is about to be emitted that extends backwards to POS,
2534
+ * therefore it may completely cover some existing instructions in the
2535
+ * buffer. If an instruction is completely covered by this new match,
2536
+ * erase it. If the new instruction is covered by the previous one,
2537
+ * return 1 to skip it. */
2538
+ static void
2539
+ xd3_iopt_erase (xd3_stream *stream, usize_t pos, usize_t size)
2540
+ {
2541
+ while (! xd3_rlist_empty (& stream->iopt_used))
2542
+ {
2543
+ xd3_rinst *r = xd3_rlist_back (& stream->iopt_used);
2544
+
2545
+ /* Verify that greedy is working. The previous instruction
2546
+ * should end before the new one begins. */
2547
+ XD3_ASSERT ((stream->flags & XD3_BEGREEDY) == 0 || (r->pos + r->size <= pos));
2548
+ /* Verify that min_match is working. The previous instruction
2549
+ * should end before the new one ends. */
2550
+ XD3_ASSERT ((stream->flags & XD3_BEGREEDY) != 0 || (r->pos + r->size < pos + size));
2551
+
2552
+ /* See if the last instruction starts before the new
2553
+ * instruction. If so, there is nothing to erase. */
2554
+ if (r->pos < pos)
2555
+ {
2556
+ return;
2557
+ }
2558
+
2559
+ /* Otherwise, the new instruction covers the old one, delete it
2560
+ and repeat. */
2561
+ xd3_rlist_remove (r);
2562
+ xd3_rlist_push_back (& stream->iopt_free, r);
2563
+ --stream->i_slots_used;
2564
+ }
2565
+ }
2566
+
2567
+ /* This function tells the last matched input position. */
2568
+ static usize_t
2569
+ xd3_iopt_last_matched (xd3_stream *stream)
2570
+ {
2571
+ xd3_rinst *r;
2572
+
2573
+ if (xd3_rlist_empty (& stream->iopt_used))
2574
+ {
2575
+ return 0;
2576
+ }
2577
+
2578
+ r = xd3_rlist_back (& stream->iopt_used);
2579
+
2580
+ return r->pos + r->size;
2581
+ }
2582
+
2583
+ /*********************************************************
2584
+ Emit routines
2585
+ ***********************************************************/
2586
+
2587
+ static int
2588
+ xd3_emit_single (xd3_stream *stream, xd3_rinst *single, uint8_t code)
2589
+ {
2590
+ int has_size = stream->code_table[code].size1 == 0;
2591
+ int ret;
2592
+
2593
+ IF_DEBUG2 (DP(RINT "[emit1] %"W"u %s (%"W"u) code %u\n",
2594
+ single->pos,
2595
+ xd3_rtype_to_string ((xd3_rtype) single->type, 0),
2596
+ single->size,
2597
+ code));
2598
+
2599
+ if ((ret = xd3_emit_byte (stream, & INST_TAIL (stream), code)))
2600
+ {
2601
+ return ret;
2602
+ }
2603
+
2604
+ if (has_size)
2605
+ {
2606
+ if ((ret = xd3_emit_size (stream, & INST_TAIL (stream), single->size)))
2607
+ {
2608
+ return ret;
2609
+ }
2610
+ }
2611
+
2612
+ return 0;
2613
+ }
2614
+
2615
+ static int
2616
+ xd3_emit_double (xd3_stream *stream, xd3_rinst *first,
2617
+ xd3_rinst *second, uint8_t code)
2618
+ {
2619
+ int ret;
2620
+
2621
+ /* All double instructions use fixed sizes, so all we need to do is
2622
+ * output the instruction code, no sizes. */
2623
+ XD3_ASSERT (stream->code_table[code].size1 != 0 &&
2624
+ stream->code_table[code].size2 != 0);
2625
+
2626
+ if ((ret = xd3_emit_byte (stream, & INST_TAIL (stream), code)))
2627
+ {
2628
+ return ret;
2629
+ }
2630
+
2631
+ IF_DEBUG2 (DP(RINT "[emit2]: %"W"u %s (%"W"u) %s (%"W"u) code %u\n",
2632
+ first->pos,
2633
+ xd3_rtype_to_string ((xd3_rtype) first->type, 0),
2634
+ first->size,
2635
+ xd3_rtype_to_string ((xd3_rtype) second->type, 0),
2636
+ second->size,
2637
+ code));
2638
+
2639
+ return 0;
2640
+ }
2641
+
2642
+ /* This enters a potential run instruction into the iopt buffer. The
2643
+ * position argument is relative to the target window. */
2644
+ static int
2645
+ xd3_emit_run (xd3_stream *stream, usize_t pos, usize_t size, uint8_t *run_c)
2646
+ {
2647
+ xd3_rinst* ri;
2648
+ int ret;
2649
+
2650
+ if ((ret = xd3_iopt_get_slot (stream, & ri))) { return ret; }
2651
+
2652
+ ri->type = XD3_RUN;
2653
+ ri->xtra = *run_c;
2654
+ ri->pos = pos;
2655
+ ri->size = size;
2656
+
2657
+ return 0;
2658
+ }
2659
+
2660
+ /* This enters a potential copy instruction into the iopt buffer. The
2661
+ * position argument is relative to the target window.. */
2662
+ int
2663
+ xd3_found_match (xd3_stream *stream, usize_t pos,
2664
+ usize_t size, xoff_t addr, int is_source)
2665
+ {
2666
+ xd3_rinst* ri;
2667
+ int ret;
2668
+
2669
+ if ((ret = xd3_iopt_get_slot (stream, & ri))) { return ret; }
2670
+
2671
+ ri->type = XD3_CPY;
2672
+ ri->xtra = is_source;
2673
+ ri->pos = pos;
2674
+ ri->size = size;
2675
+ ri->addr = addr;
2676
+
2677
+ return 0;
2678
+ }
2679
+
2680
+ static int
2681
+ xd3_emit_hdr (xd3_stream *stream)
2682
+ {
2683
+ int ret;
2684
+ int use_secondary = stream->sec_type != NULL;
2685
+ int use_adler32 = stream->flags & (XD3_ADLER32 | XD3_ADLER32_RECODE);
2686
+ int vcd_source = xd3_encoder_used_source (stream);
2687
+ uint8_t win_ind = 0;
2688
+ uint8_t del_ind = 0;
2689
+ usize_t enc_len;
2690
+ usize_t tgt_len;
2691
+ usize_t data_len;
2692
+ usize_t inst_len;
2693
+ usize_t addr_len;
2694
+
2695
+ if (stream->current_window == 0)
2696
+ {
2697
+ uint8_t hdr_ind = 0;
2698
+ int use_appheader = stream->enc_appheader != NULL;
2699
+
2700
+ if (use_secondary) { hdr_ind |= VCD_SECONDARY; }
2701
+ if (use_appheader) { hdr_ind |= VCD_APPHEADER; }
2702
+
2703
+ if ((ret = xd3_emit_byte (stream, & HDR_TAIL (stream),
2704
+ VCDIFF_MAGIC1)) != 0 ||
2705
+ (ret = xd3_emit_byte (stream, & HDR_TAIL (stream),
2706
+ VCDIFF_MAGIC2)) != 0 ||
2707
+ (ret = xd3_emit_byte (stream, & HDR_TAIL (stream),
2708
+ VCDIFF_MAGIC3)) != 0 ||
2709
+ (ret = xd3_emit_byte (stream, & HDR_TAIL (stream),
2710
+ VCDIFF_VERSION)) != 0 ||
2711
+ (ret = xd3_emit_byte (stream, & HDR_TAIL (stream), hdr_ind)) != 0)
2712
+ {
2713
+ return ret;
2714
+ }
2715
+
2716
+ /* Secondary compressor ID */
2717
+ #if SECONDARY_ANY
2718
+ if (use_secondary &&
2719
+ (ret = xd3_emit_byte (stream, & HDR_TAIL (stream),
2720
+ stream->sec_type->id)))
2721
+ {
2722
+ return ret;
2723
+ }
2724
+ #endif
2725
+
2726
+ /* Application header */
2727
+ if (use_appheader)
2728
+ {
2729
+ if ((ret = xd3_emit_size (stream, & HDR_TAIL (stream),
2730
+ stream->enc_appheadsz)) ||
2731
+ (ret = xd3_emit_bytes (stream, & HDR_TAIL (stream),
2732
+ stream->enc_appheader,
2733
+ stream->enc_appheadsz)))
2734
+ {
2735
+ return ret;
2736
+ }
2737
+ }
2738
+ }
2739
+
2740
+ /* try to compress this window */
2741
+ #if SECONDARY_ANY
2742
+ if (use_secondary)
2743
+ {
2744
+ int data_sec = 0;
2745
+ int inst_sec = 0;
2746
+ int addr_sec = 0;
2747
+
2748
+ # define ENCODE_SECONDARY_SECTION(UPPER,LOWER) \
2749
+ ((stream->flags & XD3_SEC_NO ## UPPER) == 0 && \
2750
+ (ret = xd3_encode_secondary (stream, \
2751
+ & UPPER ## _HEAD (stream), \
2752
+ & UPPER ## _TAIL (stream), \
2753
+ & xd3_sec_ ## LOWER (stream), \
2754
+ & stream->sec_ ## LOWER, \
2755
+ & LOWER ## _sec)))
2756
+
2757
+ if (ENCODE_SECONDARY_SECTION (DATA, data) ||
2758
+ ENCODE_SECONDARY_SECTION (INST, inst) ||
2759
+ ENCODE_SECONDARY_SECTION (ADDR, addr))
2760
+ {
2761
+ return ret;
2762
+ }
2763
+
2764
+ del_ind |= (data_sec ? VCD_DATACOMP : 0);
2765
+ del_ind |= (inst_sec ? VCD_INSTCOMP : 0);
2766
+ del_ind |= (addr_sec ? VCD_ADDRCOMP : 0);
2767
+ }
2768
+ #endif
2769
+
2770
+ /* if (vcd_target) { win_ind |= VCD_TARGET; } */
2771
+ if (vcd_source) { win_ind |= VCD_SOURCE; }
2772
+ if (use_adler32) { win_ind |= VCD_ADLER32; }
2773
+
2774
+ /* window indicator */
2775
+ if ((ret = xd3_emit_byte (stream, & HDR_TAIL (stream), win_ind)))
2776
+ {
2777
+ return ret;
2778
+ }
2779
+
2780
+ /* source window */
2781
+ if (vcd_source)
2782
+ {
2783
+ /* or (vcd_target) { ... } */
2784
+ if ((ret = xd3_emit_size (stream, & HDR_TAIL (stream),
2785
+ stream->src->srclen)) ||
2786
+ (ret = xd3_emit_offset (stream, & HDR_TAIL (stream),
2787
+ stream->src->srcbase))) { return ret; }
2788
+ }
2789
+
2790
+ tgt_len = stream->avail_in;
2791
+ data_len = xd3_sizeof_output (DATA_HEAD (stream));
2792
+ inst_len = xd3_sizeof_output (INST_HEAD (stream));
2793
+ addr_len = xd3_sizeof_output (ADDR_HEAD (stream));
2794
+
2795
+ /* The enc_len field is a redundency for future extensions. */
2796
+ enc_len = (1 + (xd3_sizeof_size (tgt_len) +
2797
+ xd3_sizeof_size (data_len) +
2798
+ xd3_sizeof_size (inst_len) +
2799
+ xd3_sizeof_size (addr_len)) +
2800
+ data_len +
2801
+ inst_len +
2802
+ addr_len +
2803
+ (use_adler32 ? 4 : 0));
2804
+
2805
+ if ((ret = xd3_emit_size (stream, & HDR_TAIL (stream), enc_len)) ||
2806
+ (ret = xd3_emit_size (stream, & HDR_TAIL (stream), tgt_len)) ||
2807
+ (ret = xd3_emit_byte (stream, & HDR_TAIL (stream), del_ind)) ||
2808
+ (ret = xd3_emit_size (stream, & HDR_TAIL (stream), data_len)) ||
2809
+ (ret = xd3_emit_size (stream, & HDR_TAIL (stream), inst_len)) ||
2810
+ (ret = xd3_emit_size (stream, & HDR_TAIL (stream), addr_len)))
2811
+ {
2812
+ return ret;
2813
+ }
2814
+
2815
+ if (use_adler32)
2816
+ {
2817
+ uint8_t send[4];
2818
+ uint32_t a32;
2819
+
2820
+ if (stream->flags & XD3_ADLER32)
2821
+ {
2822
+ a32 = adler32 (1L, stream->next_in, stream->avail_in);
2823
+ }
2824
+ else
2825
+ {
2826
+ a32 = stream->recode_adler32;
2827
+ }
2828
+
2829
+ /* Four bytes. */
2830
+ send[0] = (uint8_t) (a32 >> 24);
2831
+ send[1] = (uint8_t) (a32 >> 16);
2832
+ send[2] = (uint8_t) (a32 >> 8);
2833
+ send[3] = (uint8_t) (a32 & 0x000000FFU);
2834
+
2835
+ if ((ret = xd3_emit_bytes (stream, & HDR_TAIL (stream), send, 4)))
2836
+ {
2837
+ return ret;
2838
+ }
2839
+ }
2840
+
2841
+ return 0;
2842
+ }
2843
+
2844
+ /****************************************************************
2845
+ Encode routines
2846
+ ****************************************************************/
2847
+
2848
+ static int
2849
+ xd3_encode_buffer_leftover (xd3_stream *stream)
2850
+ {
2851
+ usize_t take;
2852
+ usize_t room;
2853
+
2854
+ /* Allocate the buffer. */
2855
+ if (stream->buf_in == NULL &&
2856
+ (stream->buf_in = (uint8_t*) xd3_alloc (stream, stream->winsize, 1)) == NULL)
2857
+ {
2858
+ return ENOMEM;
2859
+ }
2860
+
2861
+ IF_DEBUG2 (DP(RINT "[leftover] flush?=%s\n", (stream->flags & XD3_FLUSH) ? "yes" : "no"));
2862
+
2863
+ /* Take leftover input first. */
2864
+ if (stream->buf_leftover != NULL)
2865
+ {
2866
+ XD3_ASSERT (stream->buf_avail == 0);
2867
+ XD3_ASSERT (stream->buf_leftavail < stream->winsize);
2868
+
2869
+ IF_DEBUG2 (DP(RINT "[leftover] previous %"W"u avail %"W"u\n",
2870
+ stream->buf_leftavail, stream->avail_in));
2871
+
2872
+ memcpy (stream->buf_in, stream->buf_leftover, stream->buf_leftavail);
2873
+
2874
+ stream->buf_leftover = NULL;
2875
+ stream->buf_avail = stream->buf_leftavail;
2876
+ }
2877
+
2878
+ /* Copy into the buffer. */
2879
+ room = stream->winsize - stream->buf_avail;
2880
+ take = xd3_min (room, stream->avail_in);
2881
+
2882
+ memcpy (stream->buf_in + stream->buf_avail, stream->next_in, take);
2883
+
2884
+ stream->buf_avail += take;
2885
+
2886
+ if (take < stream->avail_in)
2887
+ {
2888
+ /* Buffer is full */
2889
+ stream->buf_leftover = stream->next_in + take;
2890
+ stream->buf_leftavail = stream->avail_in - take;
2891
+ }
2892
+ else if ((stream->buf_avail < stream->winsize) && !(stream->flags & XD3_FLUSH))
2893
+ {
2894
+ /* Buffer has space */
2895
+ IF_DEBUG2 (DP(RINT "[leftover] emptied %"W"u\n", take));
2896
+ return XD3_INPUT;
2897
+ }
2898
+
2899
+ /* Use the buffer: */
2900
+ IF_DEBUG2 (DP(RINT "[leftover] take %"W"u remaining %"W"u\n", take, stream->buf_leftavail));
2901
+ stream->next_in = stream->buf_in;
2902
+ stream->avail_in = stream->buf_avail;
2903
+ stream->buf_avail = 0;
2904
+
2905
+ return 0;
2906
+ }
2907
+
2908
+ /* Allocates one block of xd3_rlist elements */
2909
+ static int
2910
+ xd3_alloc_iopt (xd3_stream *stream, usize_t elts)
2911
+ {
2912
+ usize_t i;
2913
+ xd3_iopt_buflist* last =
2914
+ (xd3_iopt_buflist*) xd3_alloc (stream, sizeof (xd3_iopt_buflist), 1);
2915
+
2916
+ if (last == NULL ||
2917
+ (last->buffer = (xd3_rinst*) xd3_alloc (stream, sizeof (xd3_rinst), elts)) == NULL)
2918
+ {
2919
+ return ENOMEM;
2920
+ }
2921
+
2922
+ last->next = stream->iopt_alloc;
2923
+ stream->iopt_alloc = last;
2924
+
2925
+ for (i = 0; i < elts; i += 1)
2926
+ {
2927
+ xd3_rlist_push_back (& stream->iopt_free, & last->buffer[i]);
2928
+ }
2929
+
2930
+ return 0;
2931
+ }
2932
+
2933
+ /* This function allocates all memory initially used by the encoder. */
2934
+ static int
2935
+ xd3_encode_init (xd3_stream *stream, int full_init)
2936
+ {
2937
+ int ret;
2938
+ int i;
2939
+
2940
+ if (full_init)
2941
+ {
2942
+ int large_comp = (stream->src != NULL);
2943
+ int small_comp = ! (stream->flags & XD3_NOCOMPRESS);
2944
+
2945
+ /* Memory allocations for checksum tables are delayed until
2946
+ * xd3_string_match_init in the first call to string_match--that way
2947
+ * identical or short inputs require no table allocation. */
2948
+ if (large_comp)
2949
+ {
2950
+ /* TODO Need to check for overflow here. */
2951
+ usize_t hash_values = stream->src->max_winsize /
2952
+ stream->smatcher.large_step;
2953
+
2954
+ if ((ret = xd3_size_hashtable (stream,
2955
+ hash_values,
2956
+ stream->smatcher.large_look,
2957
+ & stream->large_hash)))
2958
+ {
2959
+ return ret;
2960
+ }
2961
+ }
2962
+
2963
+ if (small_comp)
2964
+ {
2965
+ /* TODO: This is under devel: used to have min (sprevsz) here, which sort
2966
+ * of makes sense, but observed fast performance w/ larger tables, which
2967
+ * also sort of makes sense. @@@ */
2968
+ usize_t hash_values = stream->winsize;
2969
+
2970
+ if ((ret = xd3_size_hashtable (stream,
2971
+ hash_values,
2972
+ stream->smatcher.small_look,
2973
+ & stream->small_hash)))
2974
+ {
2975
+ return ret;
2976
+ }
2977
+ }
2978
+ }
2979
+
2980
+ /* data buffers */
2981
+ for (i = 0; i < ENC_SECTS; i += 1)
2982
+ {
2983
+ if ((stream->enc_heads[i] =
2984
+ stream->enc_tails[i] =
2985
+ xd3_alloc_output (stream, NULL)) == NULL)
2986
+ {
2987
+ return ENOMEM;
2988
+ }
2989
+ }
2990
+
2991
+ /* iopt buffer */
2992
+ xd3_rlist_init (& stream->iopt_used);
2993
+ xd3_rlist_init (& stream->iopt_free);
2994
+
2995
+ if (xd3_alloc_iopt (stream, stream->iopt_size) != 0) { goto fail; }
2996
+
2997
+ XD3_ASSERT (xd3_rlist_length (& stream->iopt_free) == stream->iopt_size);
2998
+ XD3_ASSERT (xd3_rlist_length (& stream->iopt_used) == 0);
2999
+
3000
+ /* address cache, code table */
3001
+ stream->acache.s_near = stream->code_table_desc->near_modes;
3002
+ stream->acache.s_same = stream->code_table_desc->same_modes;
3003
+ stream->code_table = stream->code_table_func ();
3004
+
3005
+ return xd3_alloc_cache (stream);
3006
+
3007
+ fail:
3008
+
3009
+ return ENOMEM;
3010
+ }
3011
+
3012
+ int
3013
+ xd3_encode_init_full (xd3_stream *stream)
3014
+ {
3015
+ return xd3_encode_init (stream, 1);
3016
+ }
3017
+
3018
+ int
3019
+ xd3_encode_init_partial (xd3_stream *stream)
3020
+ {
3021
+ return xd3_encode_init (stream, 0);
3022
+ }
3023
+
3024
+ /* Called after the ENC_POSTOUT state, this puts the output buffers
3025
+ * back into separate lists and re-initializes some variables. (The
3026
+ * output lists were spliced together during the ENC_FLUSH state.) */
3027
+ static void
3028
+ xd3_encode_reset (xd3_stream *stream)
3029
+ {
3030
+ int i;
3031
+ xd3_output *olist;
3032
+
3033
+ stream->avail_in = 0;
3034
+ stream->small_reset = 1;
3035
+ stream->i_slots_used = 0;
3036
+
3037
+ if (stream->src != NULL)
3038
+ {
3039
+ stream->src->srcbase = 0;
3040
+ stream->src->srclen = 0;
3041
+ stream->srcwin_decided = 0;
3042
+ stream->srcwin_decided_early = 0;
3043
+ stream->match_minaddr = 0;
3044
+ stream->match_maxaddr = 0;
3045
+ stream->taroff = 0;
3046
+ }
3047
+
3048
+ /* Reset output chains. */
3049
+ olist = stream->enc_heads[0];
3050
+
3051
+ for (i = 0; i < ENC_SECTS; i += 1)
3052
+ {
3053
+ XD3_ASSERT (olist != NULL);
3054
+
3055
+ stream->enc_heads[i] = olist;
3056
+ stream->enc_tails[i] = olist;
3057
+ olist = olist->next_page;
3058
+
3059
+ stream->enc_heads[i]->next = 0;
3060
+ stream->enc_heads[i]->next_page = NULL;
3061
+
3062
+ stream->enc_tails[i]->next_page = NULL;
3063
+ stream->enc_tails[i] = stream->enc_heads[i];
3064
+ }
3065
+
3066
+ xd3_freelist_output (stream, olist);
3067
+ }
3068
+
3069
+ /* The main encoding routine. */
3070
+ int
3071
+ xd3_encode_input (xd3_stream *stream)
3072
+ {
3073
+ int ret, i;
3074
+
3075
+ if (stream->dec_state != 0)
3076
+ {
3077
+ stream->msg = "encoder/decoder transition";
3078
+ return XD3_INTERNAL;
3079
+ }
3080
+
3081
+ switch (stream->enc_state)
3082
+ {
3083
+ case ENC_INIT:
3084
+ /* Only reached on first time through: memory setup. */
3085
+ if ((ret = xd3_encode_init_full (stream))) { return ret; }
3086
+
3087
+ stream->enc_state = ENC_INPUT;
3088
+
3089
+ case ENC_INPUT:
3090
+
3091
+ /* If there is no input yet, just return. This checks for
3092
+ * next_in == NULL, not avail_in == 0 since zero bytes is a
3093
+ * valid input. There is an assertion in xd3_avail_input() that
3094
+ * next_in != NULL for this reason. By returning right away we
3095
+ * avoid creating an input buffer before the caller has supplied
3096
+ * its first data. It is possible for xd3_avail_input to be
3097
+ * called both before and after the first call to
3098
+ * xd3_encode_input(). */
3099
+ if (stream->next_in == NULL)
3100
+ {
3101
+ return XD3_INPUT;
3102
+ }
3103
+
3104
+ enc_flush:
3105
+ /* See if we should buffer the input: either if there is already
3106
+ * a leftover buffer, or if the input is short of winsize
3107
+ * without flush. The label at this point is reached by a goto
3108
+ * below, when there is leftover input after postout. */
3109
+ if ((stream->buf_leftover != NULL) ||
3110
+ (stream->buf_avail != 0) ||
3111
+ (stream->avail_in < stream->winsize && ! (stream->flags & XD3_FLUSH)))
3112
+ {
3113
+ if ((ret = xd3_encode_buffer_leftover (stream))) { return ret; }
3114
+ }
3115
+
3116
+ /* Initalize the address cache before each window. */
3117
+ xd3_init_cache (& stream->acache);
3118
+
3119
+ stream->input_position = 0;
3120
+ stream->min_match = MIN_MATCH;
3121
+ stream->unencoded_offset = 0;
3122
+
3123
+ stream->enc_state = ENC_SEARCH;
3124
+
3125
+ IF_DEBUG2 (DP(RINT "[WINSTART:%"Q"u] input bytes %"W"u offset %"Q"u\n",
3126
+ stream->current_window, stream->avail_in,
3127
+ stream->total_in));
3128
+ return XD3_WINSTART;
3129
+
3130
+ case ENC_SEARCH:
3131
+ IF_DEBUG2 (DP(RINT "[SEARCH] match_state %d avail_in %"W"u %s\n",
3132
+ stream->match_state, stream->avail_in,
3133
+ stream->src ? "source" : "no source"));
3134
+
3135
+ /* Reentrant matching. */
3136
+ if (stream->src != NULL)
3137
+ {
3138
+ switch (stream->match_state)
3139
+ {
3140
+ case MATCH_TARGET:
3141
+ /* Try matching forward at the start of the target.
3142
+ * This is entered the first time through, to check for
3143
+ * a perfect match, and whenever there is a source match
3144
+ * that extends to the end of the previous window. The
3145
+ * match_srcpos field is initially zero and later set
3146
+ * during xd3_source_extend_match. */
3147
+
3148
+ if (stream->avail_in > 0)
3149
+ {
3150
+ /* This call can't fail because the source window is
3151
+ * unrestricted. */
3152
+ ret = xd3_source_match_setup (stream, stream->match_srcpos);
3153
+ XD3_ASSERT (ret == 0);
3154
+ stream->match_state = MATCH_FORWARD;
3155
+ }
3156
+ else
3157
+ {
3158
+ stream->match_state = MATCH_SEARCHING;
3159
+ stream->match_fwd = 0;
3160
+ }
3161
+ XD3_ASSERT (stream->match_fwd == 0);
3162
+
3163
+ case MATCH_FORWARD:
3164
+ case MATCH_BACKWARD:
3165
+ if (stream->avail_in != 0)
3166
+ {
3167
+ if ((ret = xd3_source_extend_match (stream)) != 0)
3168
+ {
3169
+ return ret;
3170
+ }
3171
+
3172
+ /* The search has to make forward progress here
3173
+ * or else it can get stuck in a match-backward
3174
+ * (getsrcblk) then match-forward (getsrcblk),
3175
+ * find insufficient match length, then repeat
3176
+
3177
+ * exactly the same search.
3178
+ */
3179
+ stream->input_position += stream->match_fwd;
3180
+ }
3181
+
3182
+ case MATCH_SEARCHING:
3183
+ /* Continue string matching. (It's possible that the
3184
+ * initial match continued through the entire input, in
3185
+ * which case we're still in MATCH_FORWARD and should
3186
+ * remain so for the next input window.) */
3187
+ break;
3188
+ }
3189
+ }
3190
+
3191
+ /* String matching... */
3192
+ if (stream->avail_in != 0 &&
3193
+ (ret = stream->smatcher.string_match (stream)))
3194
+ {
3195
+ return ret;
3196
+ }
3197
+
3198
+ stream->enc_state = ENC_INSTR;
3199
+
3200
+ case ENC_INSTR:
3201
+ /* Note: Jump here to encode VCDIFF deltas w/o using this
3202
+ * string-matching code. Merging code enters here. */
3203
+
3204
+ /* Flush the instrution buffer, then possibly add one more
3205
+ * instruction, then emit the header. */
3206
+ if ((ret = xd3_iopt_flush_instructions (stream, 1)) ||
3207
+ (ret = xd3_iopt_add_finalize (stream)))
3208
+ {
3209
+ return ret;
3210
+ }
3211
+
3212
+ stream->enc_state = ENC_FLUSH;
3213
+
3214
+ case ENC_FLUSH:
3215
+ /* Note: main_recode_func() bypasses string-matching by setting
3216
+ * ENC_FLUSH. */
3217
+ if ((ret = xd3_emit_hdr (stream)))
3218
+ {
3219
+ return ret;
3220
+ }
3221
+
3222
+ /* Begin output. */
3223
+ stream->enc_current = HDR_HEAD (stream);
3224
+
3225
+ /* Chain all the outputs together. After doing this, it looks
3226
+ * as if there is only one section. The other enc_heads are set
3227
+ * to NULL to avoid freeing them more than once. */
3228
+ for (i = 1; i < ENC_SECTS; i += 1)
3229
+ {
3230
+ stream->enc_tails[i-1]->next_page = stream->enc_heads[i];
3231
+ stream->enc_heads[i] = NULL;
3232
+ }
3233
+
3234
+ enc_output:
3235
+
3236
+ stream->enc_state = ENC_POSTOUT;
3237
+ stream->next_out = stream->enc_current->base;
3238
+ stream->avail_out = stream->enc_current->next;
3239
+ stream->total_out += stream->avail_out;
3240
+
3241
+ /* If there is any output in this buffer, return it, otherwise
3242
+ * fall through to handle the next buffer or finish the window
3243
+ * after all buffers have been output. */
3244
+ if (stream->avail_out > 0)
3245
+ {
3246
+ /* This is the only place xd3_encode returns XD3_OUTPUT */
3247
+ return XD3_OUTPUT;
3248
+ }
3249
+
3250
+ case ENC_POSTOUT:
3251
+
3252
+ if (stream->avail_out != 0)
3253
+ {
3254
+ stream->msg = "missed call to consume output";
3255
+ return XD3_INTERNAL;
3256
+ }
3257
+
3258
+ /* Continue outputting one buffer at a time, until the next is NULL. */
3259
+ if ((stream->enc_current = stream->enc_current->next_page) != NULL)
3260
+ {
3261
+ goto enc_output;
3262
+ }
3263
+
3264
+ stream->total_in += stream->avail_in;
3265
+ stream->enc_state = ENC_POSTWIN;
3266
+
3267
+ IF_DEBUG2 (DP(RINT "[WINFINISH:%"Q"u] in=%"Q"u\n",
3268
+ stream->current_window,
3269
+ stream->total_in));
3270
+ return XD3_WINFINISH;
3271
+
3272
+ case ENC_POSTWIN:
3273
+
3274
+ xd3_encode_reset (stream);
3275
+
3276
+ stream->current_window += 1;
3277
+ stream->enc_state = ENC_INPUT;
3278
+
3279
+ /* If there is leftover input to flush, repeat. */
3280
+ if (stream->buf_leftover != NULL)
3281
+ {
3282
+ goto enc_flush;
3283
+ }
3284
+
3285
+ /* Ready for more input. */
3286
+ return XD3_INPUT;
3287
+
3288
+ default:
3289
+ stream->msg = "invalid state";
3290
+ return XD3_INTERNAL;
3291
+ }
3292
+ }
3293
+ #endif /* XD3_ENCODER */
3294
+
3295
+ /*****************************************************************
3296
+ Client convenience functions
3297
+ ******************************************************************/
3298
+
3299
+ int
3300
+ xd3_process_stream (int is_encode,
3301
+ xd3_stream *stream,
3302
+ int (*func) (xd3_stream *),
3303
+ int close_stream,
3304
+ const uint8_t *input,
3305
+ usize_t input_size,
3306
+ uint8_t *output,
3307
+ usize_t *output_size,
3308
+ usize_t output_size_max)
3309
+ {
3310
+ usize_t ipos = 0;
3311
+ usize_t n = xd3_min (stream->winsize, input_size);
3312
+
3313
+ (*output_size) = 0;
3314
+
3315
+ stream->flags |= XD3_FLUSH;
3316
+
3317
+ xd3_avail_input (stream, input + ipos, n);
3318
+ ipos += n;
3319
+
3320
+ for (;;)
3321
+ {
3322
+ int ret;
3323
+ switch ((ret = func (stream)))
3324
+ {
3325
+ case XD3_OUTPUT: { /* memcpy below */ break; }
3326
+ case XD3_INPUT: {
3327
+ n = xd3_min(stream->winsize, input_size - ipos);
3328
+ if (n == 0)
3329
+ {
3330
+ goto done;
3331
+ }
3332
+ xd3_avail_input (stream, input + ipos, n);
3333
+ ipos += n;
3334
+ continue;
3335
+ }
3336
+ case XD3_GOTHEADER: { /* ignore */ continue; }
3337
+ case XD3_WINSTART: { /* ignore */ continue; }
3338
+ case XD3_WINFINISH: { /* ignore */ continue; }
3339
+ case XD3_GETSRCBLK:
3340
+ {
3341
+ /* When the getblk function is NULL, it is necessary to
3342
+ * provide the complete source as a single block using
3343
+ * xd3_set_source_and_size, otherwise this error. The
3344
+ * library should never ask for another source block. */
3345
+ stream->msg = "library requested source block";
3346
+ return XD3_INTERNAL;
3347
+ }
3348
+ case 0:
3349
+ {
3350
+ /* xd3_encode_input/xd3_decode_input never return 0 */
3351
+ stream->msg = "invalid return: 0";
3352
+ return XD3_INTERNAL;
3353
+ }
3354
+ default:
3355
+ return ret;
3356
+ }
3357
+
3358
+ if (*output_size + stream->avail_out > output_size_max)
3359
+ {
3360
+ stream->msg = "insufficient output space";
3361
+ return ENOSPC;
3362
+ }
3363
+
3364
+ memcpy (output + *output_size, stream->next_out, stream->avail_out);
3365
+
3366
+ *output_size += stream->avail_out;
3367
+
3368
+ xd3_consume_output (stream);
3369
+ }
3370
+ done:
3371
+ return (close_stream == 0) ? 0 : xd3_close_stream (stream);
3372
+ }
3373
+
3374
+ static int
3375
+ xd3_process_memory (int is_encode,
3376
+ int (*func) (xd3_stream *),
3377
+ const uint8_t *input,
3378
+ usize_t input_size,
3379
+ const uint8_t *source,
3380
+ usize_t source_size,
3381
+ uint8_t *output,
3382
+ usize_t *output_size,
3383
+ usize_t output_size_max,
3384
+ int flags) {
3385
+ xd3_stream stream;
3386
+ xd3_config config;
3387
+ xd3_source src;
3388
+ int ret;
3389
+
3390
+ memset (& stream, 0, sizeof (stream));
3391
+ memset (& config, 0, sizeof (config));
3392
+
3393
+ if (input == NULL || output == NULL) {
3394
+ stream.msg = "invalid input/output buffer";
3395
+ ret = XD3_INTERNAL;
3396
+ goto exit;
3397
+ }
3398
+
3399
+ config.flags = flags;
3400
+
3401
+ if (is_encode)
3402
+ {
3403
+ config.winsize = xd3_min(input_size, (usize_t) XD3_DEFAULT_WINSIZE);
3404
+ config.sprevsz = xd3_pow2_roundup (config.winsize);
3405
+ }
3406
+
3407
+ if ((ret = xd3_config_stream (&stream, &config)) != 0)
3408
+ {
3409
+ goto exit;
3410
+ }
3411
+
3412
+ if (source != NULL)
3413
+ {
3414
+ memset (& src, 0, sizeof (src));
3415
+
3416
+ src.blksize = source_size;
3417
+ src.onblk = source_size;
3418
+ src.curblk = source;
3419
+ src.curblkno = 0;
3420
+ src.max_winsize = source_size;
3421
+
3422
+ if ((ret = xd3_set_source_and_size (&stream, &src, source_size)) != 0)
3423
+ {
3424
+ goto exit;
3425
+ }
3426
+ }
3427
+
3428
+ if ((ret = xd3_process_stream (is_encode,
3429
+ & stream,
3430
+ func, 1,
3431
+ input, input_size,
3432
+ output,
3433
+ output_size,
3434
+ output_size_max)) != 0)
3435
+ {
3436
+ goto exit;
3437
+ }
3438
+
3439
+ exit:
3440
+ if (ret != 0)
3441
+ {
3442
+ IF_DEBUG2 (DP(RINT "process_memory: %d: %s\n", ret, stream.msg));
3443
+ }
3444
+ xd3_free_stream(&stream);
3445
+ return ret;
3446
+ }
3447
+
3448
+ int
3449
+ xd3_decode_stream (xd3_stream *stream,
3450
+ const uint8_t *input,
3451
+ usize_t input_size,
3452
+ uint8_t *output,
3453
+ usize_t *output_size,
3454
+ usize_t output_size_max)
3455
+ {
3456
+ return xd3_process_stream (0, stream, & xd3_decode_input, 1,
3457
+ input, input_size,
3458
+ output, output_size, output_size_max);
3459
+ }
3460
+
3461
+ int
3462
+ xd3_decode_memory (const uint8_t *input,
3463
+ usize_t input_size,
3464
+ const uint8_t *source,
3465
+ usize_t source_size,
3466
+ uint8_t *output,
3467
+ usize_t *output_size,
3468
+ usize_t output_size_max,
3469
+ int flags) {
3470
+ return xd3_process_memory (0, & xd3_decode_input,
3471
+ input, input_size,
3472
+ source, source_size,
3473
+ output, output_size, output_size_max,
3474
+ flags);
3475
+ }
3476
+
3477
+
3478
+ #if XD3_ENCODER
3479
+ int
3480
+ xd3_encode_stream (xd3_stream *stream,
3481
+ const uint8_t *input,
3482
+ usize_t input_size,
3483
+ uint8_t *output,
3484
+ usize_t *output_size,
3485
+ usize_t output_size_max)
3486
+ {
3487
+ return xd3_process_stream (1, stream, & xd3_encode_input, 1,
3488
+ input, input_size,
3489
+ output, output_size, output_size_max);
3490
+ }
3491
+
3492
+ int
3493
+ xd3_encode_memory (const uint8_t *input,
3494
+ usize_t input_size,
3495
+ const uint8_t *source,
3496
+ usize_t source_size,
3497
+ uint8_t *output,
3498
+ usize_t *output_size,
3499
+ usize_t output_size_max,
3500
+ int flags) {
3501
+ return xd3_process_memory (1, & xd3_encode_input,
3502
+ input, input_size,
3503
+ source, source_size,
3504
+ output, output_size, output_size_max,
3505
+ flags);
3506
+ }
3507
+ #endif
3508
+
3509
+
3510
+ /*************************************************************
3511
+ String matching helpers
3512
+ *************************************************************/
3513
+
3514
+ #if XD3_ENCODER
3515
+ /* Do the initial xd3_string_match() checksum table setup.
3516
+ * Allocations are delayed until first use to avoid allocation
3517
+ * sometimes (e.g., perfect matches, zero-length inputs). */
3518
+ static int
3519
+ xd3_string_match_init (xd3_stream *stream)
3520
+ {
3521
+ const int DO_SMALL = ! (stream->flags & XD3_NOCOMPRESS);
3522
+ const int DO_LARGE = (stream->src != NULL);
3523
+
3524
+ if (DO_LARGE && stream->large_table == NULL)
3525
+ {
3526
+ if ((stream->large_table =
3527
+ (usize_t*) xd3_alloc0 (stream, stream->large_hash.size, sizeof (usize_t))) == NULL)
3528
+ {
3529
+ return ENOMEM;
3530
+ }
3531
+ }
3532
+
3533
+ if (DO_SMALL)
3534
+ {
3535
+ /* Subsequent calls can return immediately after checking reset. */
3536
+ if (stream->small_table != NULL)
3537
+ {
3538
+ /* The target hash table is reinitialized once per window. */
3539
+ /* TODO: This would not have to be reinitialized if absolute
3540
+ * offsets were being stored. */
3541
+ if (stream->small_reset)
3542
+ {
3543
+ stream->small_reset = 0;
3544
+ memset (stream->small_table, 0,
3545
+ sizeof (usize_t) * stream->small_hash.size);
3546
+ }
3547
+
3548
+ return 0;
3549
+ }
3550
+
3551
+ if ((stream->small_table =
3552
+ (usize_t*) xd3_alloc0 (stream,
3553
+ stream->small_hash.size,
3554
+ sizeof (usize_t))) == NULL)
3555
+ {
3556
+ return ENOMEM;
3557
+ }
3558
+
3559
+ /* If there is a previous table needed. */
3560
+ if (stream->smatcher.small_lchain > 1 ||
3561
+ stream->smatcher.small_chain > 1)
3562
+ {
3563
+ if ((stream->small_prev =
3564
+ (xd3_slist*) xd3_alloc (stream,
3565
+ stream->sprevsz,
3566
+ sizeof (xd3_slist))) == NULL)
3567
+ {
3568
+ return ENOMEM;
3569
+ }
3570
+ }
3571
+ }
3572
+
3573
+ return 0;
3574
+ }
3575
+
3576
+ #if XD3_USE_LARGEFILE64 && !XD3_USE_LARGESIZET
3577
+ /* This function handles the 32/64bit ambiguity -- file positions are 64bit
3578
+ * but the hash table for source-offsets is 32bit. */
3579
+ static xoff_t
3580
+ xd3_source_cksum_offset(xd3_stream *stream, usize_t low)
3581
+ {
3582
+ xoff_t scp = stream->srcwin_cksum_pos;
3583
+ xoff_t s0 = scp >> 32;
3584
+
3585
+ usize_t sr = (usize_t) scp;
3586
+
3587
+ if (s0 == 0) {
3588
+ return low;
3589
+ }
3590
+
3591
+ /* This should not be >= because srcwin_cksum_pos is the next
3592
+ * position to index. */
3593
+ if (low > sr) {
3594
+ return (--s0 << 32) | low;
3595
+ }
3596
+
3597
+ return (s0 << 32) | low;
3598
+ }
3599
+ #else
3600
+ static xoff_t
3601
+ xd3_source_cksum_offset(xd3_stream *stream, usize_t low)
3602
+ {
3603
+ return low;
3604
+ }
3605
+ #endif
3606
+
3607
+ /* This function sets up the stream->src fields srcbase, srclen. The
3608
+ * call is delayed until these values are needed to encode a copy
3609
+ * address. At this point the decision has to be made. */
3610
+ static int
3611
+ xd3_srcwin_setup (xd3_stream *stream)
3612
+ {
3613
+ xd3_source *src = stream->src;
3614
+ xoff_t length, x;
3615
+
3616
+ /* Check the undecided state. */
3617
+ XD3_ASSERT (src->srclen == 0 && src->srcbase == 0);
3618
+
3619
+ /* Avoid repeating this call. */
3620
+ stream->srcwin_decided = 1;
3621
+
3622
+ /* If the stream is flushing, then the iopt buffer was able to
3623
+ * contain the complete encoding. If no copies were issued no
3624
+ * source window is actually needed. This prevents the VCDIFF
3625
+ * header from including source base/len. xd3_emit_hdr checks for
3626
+ * srclen == 0. */
3627
+ if (stream->enc_state == ENC_INSTR && stream->match_maxaddr == 0)
3628
+ {
3629
+ goto done;
3630
+ }
3631
+
3632
+ /* Check for overflow, srclen is usize_t - this can't happen unless
3633
+ * XD3_DEFAULT_SRCBACK and related parameters are extreme - should
3634
+ * use smaller windows. */
3635
+ length = stream->match_maxaddr - stream->match_minaddr;
3636
+
3637
+ x = USIZE_T_MAX;
3638
+ if (length > x)
3639
+ {
3640
+ stream->msg = "source window length overflow (not 64bit)";
3641
+ return XD3_INTERNAL;
3642
+ }
3643
+
3644
+ /* If ENC_INSTR, then we know the exact source window to use because
3645
+ * no more copies can be issued. */
3646
+ if (stream->enc_state == ENC_INSTR)
3647
+ {
3648
+ src->srcbase = stream->match_minaddr;
3649
+ src->srclen = (usize_t) length;
3650
+ XD3_ASSERT (src->srclen);
3651
+ goto done;
3652
+ }
3653
+
3654
+ /* Otherwise, we have to make a guess. More copies may still be
3655
+ * issued, but we have to decide the source window base and length
3656
+ * now.
3657
+ * TODO: This may not working well in practice, more testing needed. */
3658
+ src->srcbase = stream->match_minaddr;
3659
+ src->srclen = xd3_max ((usize_t) length,
3660
+ stream->avail_in + (stream->avail_in >> 2));
3661
+
3662
+ if (src->eof_known)
3663
+ {
3664
+ /* Note: if the source size is known, we must reduce srclen or
3665
+ * code that expects to pass a single block w/ getblk == NULL
3666
+ * will not function, as the code will return GETSRCBLK asking
3667
+ * for the second block. */
3668
+ src->srclen = xd3_min (src->srclen, xd3_source_eof(src) - src->srcbase);
3669
+ }
3670
+ IF_DEBUG1 (DP(RINT "[srcwin_setup_constrained] base %"Q"u len %"W"u\n",
3671
+ src->srcbase, src->srclen));
3672
+
3673
+ XD3_ASSERT (src->srclen);
3674
+ done:
3675
+ /* Set the taroff. This convenience variable is used even when
3676
+ stream->src == NULL. */
3677
+ stream->taroff = src->srclen;
3678
+ return 0;
3679
+ }
3680
+
3681
+ /* Sets the bounding region for a newly discovered source match, prior
3682
+ * to calling xd3_source_extend_match(). This sets the match_maxfwd,
3683
+ * match_maxback variables. Note: srcpos is an absolute position
3684
+ * (xoff_t) but the match_maxfwd, match_maxback variables are usize_t.
3685
+ * Returns 0 if the setup succeeds, or 1 if the source position lies
3686
+ * outside an already-decided srcbase/srclen window. */
3687
+ static int
3688
+ xd3_source_match_setup (xd3_stream *stream, xoff_t srcpos)
3689
+ {
3690
+ xd3_source *const src = stream->src;
3691
+ usize_t greedy_or_not;
3692
+
3693
+ stream->match_maxback = 0;
3694
+ stream->match_maxfwd = 0;
3695
+ stream->match_back = 0;
3696
+ stream->match_fwd = 0;
3697
+
3698
+ /* This avoids a non-blocking endless loop caused by scanning
3699
+ * backwards across a block boundary, only to find not enough
3700
+ * matching bytes to beat the current min_match due to a better lazy
3701
+ * target match: the re-entry to xd3_string_match() repeats the same
3702
+ * long match because the input position hasn't changed. TODO: if
3703
+ * ever duplicates are added to the source hash table, this logic
3704
+ * won't suffice to avoid loops. See testing/regtest.cc's
3705
+ * TestNonBlockingProgress test! */
3706
+ if (srcpos != 0 && srcpos == stream->match_last_srcpos)
3707
+ {
3708
+ IF_DEBUG2(DP(RINT "[match_setup] looping failure\n"));
3709
+ goto bad;
3710
+ }
3711
+
3712
+ /* Implement src->max_winsize, which prevents the encoder from seeking
3713
+ * back further than the LRU cache maintaining FIFO discipline, (to
3714
+ * avoid seeking). */
3715
+ if (srcpos < stream->srcwin_cksum_pos &&
3716
+ stream->srcwin_cksum_pos - srcpos > src->max_winsize)
3717
+ {
3718
+ IF_DEBUG2(DP(RINT "[match_setup] rejected due to src->max_winsize "
3719
+ "distance eof=%"Q"u srcpos=%"Q"u max_winsz=%"Q"u\n",
3720
+ xd3_source_eof (src),
3721
+ srcpos, src->max_winsize));
3722
+ goto bad;
3723
+ }
3724
+
3725
+ /* There are cases where the above test does not reject a match that
3726
+ * will experience XD3_TOOFARBACK at the first xd3_getblk call
3727
+ * because the input may have advanced up to one block beyond the
3728
+ * actual EOF. */
3729
+ IF_DEBUG2(DP(RINT "[match_setup] %"Q"u srcpos %"Q"u, "
3730
+ "src->max_winsize %"Q"u\n",
3731
+ stream->total_in + stream->input_position,
3732
+ srcpos, src->max_winsize));
3733
+
3734
+ /* Going backwards, the 1.5-pass algorithm allows some
3735
+ * already-matched input may be covered by a longer source match.
3736
+ * The greedy algorithm does not allow this.
3737
+ * TODO: Measure this. */
3738
+ if (stream->flags & XD3_BEGREEDY)
3739
+ {
3740
+ /* The greedy algorithm allows backward matching to the last
3741
+ * matched position. */
3742
+ greedy_or_not = xd3_iopt_last_matched (stream);
3743
+ }
3744
+ else
3745
+ {
3746
+ /* The 1.5-pass algorithm allows backward matching to go back as
3747
+ * far as the unencoded offset, which is updated as instructions
3748
+ * pass out of the iopt buffer. If this (default) is chosen, it
3749
+ * means xd3_iopt_erase may be called to eliminate instructions
3750
+ * when a covering source match is found. */
3751
+ greedy_or_not = stream->unencoded_offset;
3752
+ }
3753
+
3754
+ /* Backward target match limit. */
3755
+ XD3_ASSERT (stream->input_position >= greedy_or_not);
3756
+ stream->match_maxback = stream->input_position - greedy_or_not;
3757
+
3758
+ /* Forward target match limit. */
3759
+ XD3_ASSERT (stream->avail_in > stream->input_position);
3760
+ stream->match_maxfwd = stream->avail_in - stream->input_position;
3761
+
3762
+ /* Now we take the source position into account. It depends whether
3763
+ * the srclen/srcbase have been decided yet. */
3764
+ if (stream->srcwin_decided == 0)
3765
+ {
3766
+ /* Unrestricted case: the match can cover the entire source,
3767
+ * 0--src->size. We compare the usize_t
3768
+ * match_maxfwd/match_maxback against the xoff_t
3769
+ * src->size/srcpos values and take the min. */
3770
+ /* TODO #if XD3_USE_LARGESIZET ? */
3771
+ if (srcpos < stream->match_maxback)
3772
+ {
3773
+ stream->match_maxback = (usize_t) srcpos;
3774
+ }
3775
+
3776
+ if (src->eof_known)
3777
+ {
3778
+ xoff_t srcavail = xd3_source_eof (src) - srcpos;
3779
+
3780
+ if (srcavail < stream->match_maxfwd)
3781
+ {
3782
+ stream->match_maxfwd = (usize_t) srcavail;
3783
+ }
3784
+ }
3785
+
3786
+ IF_DEBUG2(DP(RINT
3787
+ "[match_setup] srcpos %"Q"u (tgtpos %"Q"u) "
3788
+ "unrestricted maxback %"W"u maxfwd %"W"u\n",
3789
+ srcpos,
3790
+ stream->total_in + stream->input_position,
3791
+ stream->match_maxback,
3792
+ stream->match_maxfwd));
3793
+ goto good;
3794
+ }
3795
+
3796
+ /* Decided some source window. */
3797
+ XD3_ASSERT (src->srclen > 0);
3798
+
3799
+ /* Restricted case: fail if the srcpos lies outside the source window */
3800
+ if ((srcpos < src->srcbase) ||
3801
+ (srcpos > (src->srcbase + src->srclen)))
3802
+ {
3803
+ IF_DEBUG1(DP(RINT "[match_setup] restricted source window failure\n"));
3804
+ goto bad;
3805
+ }
3806
+ else
3807
+ {
3808
+ usize_t srcavail;
3809
+
3810
+ srcavail = (usize_t) (srcpos - src->srcbase);
3811
+ if (srcavail < stream->match_maxback)
3812
+ {
3813
+ stream->match_maxback = srcavail;
3814
+ }
3815
+
3816
+ srcavail = src->srcbase + src->srclen - srcpos;
3817
+ if (srcavail < stream->match_maxfwd)
3818
+ {
3819
+ stream->match_maxfwd = srcavail;
3820
+ }
3821
+
3822
+ IF_DEBUG2(DP(RINT
3823
+ "[match_setup] srcpos %"Q"u (tgtpos %"Q"u) "
3824
+ "restricted maxback %"W"u maxfwd %"W"u\n",
3825
+ srcpos,
3826
+ stream->total_in + stream->input_position,
3827
+ stream->match_maxback,
3828
+ stream->match_maxfwd));
3829
+ goto good;
3830
+ }
3831
+
3832
+ good:
3833
+ stream->match_state = MATCH_BACKWARD;
3834
+ stream->match_srcpos = srcpos;
3835
+ stream->match_last_srcpos = srcpos;
3836
+ return 0;
3837
+
3838
+ bad:
3839
+ stream->match_state = MATCH_SEARCHING;
3840
+ stream->match_last_srcpos = srcpos;
3841
+ return 1;
3842
+ }
3843
+
3844
+ static inline usize_t
3845
+ xd3_forward_match(const uint8_t *s1c, const uint8_t *s2c, usize_t n)
3846
+ {
3847
+ usize_t i = 0;
3848
+ #if UNALIGNED_OK
3849
+ usize_t nint = n / sizeof(int);
3850
+
3851
+ if (nint >> 3)
3852
+ {
3853
+ usize_t j = 0;
3854
+ const int *s1 = (const int*)s1c;
3855
+ const int *s2 = (const int*)s2c;
3856
+ usize_t nint_8 = nint - 8;
3857
+
3858
+ while (i <= nint_8 &&
3859
+ s1[i++] == s2[j++] &&
3860
+ s1[i++] == s2[j++] &&
3861
+ s1[i++] == s2[j++] &&
3862
+ s1[i++] == s2[j++] &&
3863
+ s1[i++] == s2[j++] &&
3864
+ s1[i++] == s2[j++] &&
3865
+ s1[i++] == s2[j++] &&
3866
+ s1[i++] == s2[j++]) { }
3867
+
3868
+ i = (i - 1) * sizeof(int);
3869
+ }
3870
+ #endif
3871
+
3872
+ while (i < n && s1c[i] == s2c[i])
3873
+ {
3874
+ i++;
3875
+ }
3876
+ return i;
3877
+ }
3878
+
3879
+ /* This function expands the source match backward and forward. It is
3880
+ * reentrant, since xd3_getblk may return XD3_GETSRCBLK, so most
3881
+ * variables are kept in xd3_stream. There are two callers of this
3882
+ * function, the string_matching routine when a checksum match is
3883
+ * discovered, and xd3_encode_input whenever a continuing (or initial)
3884
+ * match is suspected. The two callers do different things with the
3885
+ * input_position, thus this function leaves that variable untouched.
3886
+ * If a match is taken the resulting stream->match_fwd is left
3887
+ * non-zero. */
3888
+ static int
3889
+ xd3_source_extend_match (xd3_stream *stream)
3890
+ {
3891
+ int ret;
3892
+ xd3_source *const src = stream->src;
3893
+ xoff_t matchoff; /* matchoff is the current right/left-boundary of
3894
+ the source match being tested. */
3895
+ usize_t streamoff; /* streamoff is the current right/left-boundary
3896
+ of the input match being tested. */
3897
+ xoff_t tryblk; /* tryblk, tryoff are the block, offset position
3898
+ of matchoff */
3899
+ usize_t tryoff;
3900
+ usize_t tryrem; /* tryrem is the number of matchable bytes */
3901
+ usize_t matched;
3902
+
3903
+ IF_DEBUG2(DP(RINT "[extend match] srcpos %"Q"u\n",
3904
+ stream->match_srcpos));
3905
+
3906
+ XD3_ASSERT (src != NULL);
3907
+
3908
+ /* Does it make sense to compute backward match AFTER forward match? */
3909
+ if (stream->match_state == MATCH_BACKWARD)
3910
+ {
3911
+ /* Note: this code is practically duplicated below, substituting
3912
+ * match_fwd/match_back and direction. */
3913
+ matchoff = stream->match_srcpos - stream->match_back;
3914
+ streamoff = stream->input_position - stream->match_back;
3915
+ xd3_blksize_div (matchoff, src, &tryblk, &tryoff);
3916
+
3917
+ /* this loops backward over source blocks */
3918
+ while (stream->match_back < stream->match_maxback)
3919
+ {
3920
+ /* see if we're backing across a source block boundary */
3921
+ if (tryoff == 0)
3922
+ {
3923
+ tryoff = src->blksize;
3924
+ tryblk -= 1;
3925
+ }
3926
+
3927
+ if ((ret = xd3_getblk (stream, tryblk)))
3928
+ {
3929
+ if (ret == XD3_TOOFARBACK)
3930
+ {
3931
+ IF_DEBUG2(DP(RINT "[maxback] %"Q"u TOOFARBACK: %"W"u INP %"Q"u CKSUM %"Q"u\n",
3932
+ tryblk, stream->match_back,
3933
+ stream->total_in + stream->input_position,
3934
+ stream->srcwin_cksum_pos));
3935
+
3936
+ /* the starting position is too far back. */
3937
+ if (stream->match_back == 0)
3938
+ {
3939
+ XD3_ASSERT(stream->match_fwd == 0);
3940
+ goto donefwd;
3941
+ }
3942
+
3943
+ /* search went too far back, continue forward. */
3944
+ goto doneback;
3945
+ }
3946
+
3947
+ /* could be a XD3_GETSRCBLK failure. */
3948
+ return ret;
3949
+ }
3950
+
3951
+ tryrem = xd3_min (tryoff, stream->match_maxback - stream->match_back);
3952
+
3953
+ IF_DEBUG2(DP(RINT "[maxback] maxback %"W"u trysrc %"Q"u/%"W"u tgt %"W"u tryrem %"W"u\n",
3954
+ stream->match_maxback, tryblk, tryoff, streamoff, tryrem));
3955
+
3956
+ /* TODO: This code can be optimized similar to xd3_match_forward() */
3957
+ for (; tryrem != 0; tryrem -= 1, stream->match_back += 1)
3958
+ {
3959
+ if (src->curblk[tryoff-1] != stream->next_in[streamoff-1])
3960
+ {
3961
+ goto doneback;
3962
+ }
3963
+
3964
+ tryoff -= 1;
3965
+ streamoff -= 1;
3966
+ }
3967
+ }
3968
+
3969
+ doneback:
3970
+ stream->match_state = MATCH_FORWARD;
3971
+ }
3972
+
3973
+ XD3_ASSERT (stream->match_state == MATCH_FORWARD);
3974
+
3975
+ matchoff = stream->match_srcpos + stream->match_fwd;
3976
+ streamoff = stream->input_position + stream->match_fwd;
3977
+ xd3_blksize_div (matchoff, src, & tryblk, & tryoff);
3978
+
3979
+ /* Note: practically the same code as backwards case above: same comments */
3980
+ while (stream->match_fwd < stream->match_maxfwd)
3981
+ {
3982
+ if (tryoff == src->blksize)
3983
+ {
3984
+ tryoff = 0;
3985
+ tryblk += 1;
3986
+ }
3987
+
3988
+ if ((ret = xd3_getblk (stream, tryblk)))
3989
+ {
3990
+ if (ret == XD3_TOOFARBACK)
3991
+ {
3992
+ IF_DEBUG2(DP(RINT "[maxfwd] %"Q"u TOOFARBACK: %"W"u INP %"Q"u CKSUM %"Q"u\n",
3993
+ tryblk, stream->match_fwd,
3994
+ stream->total_in + stream->input_position,
3995
+ stream->srcwin_cksum_pos));
3996
+ goto donefwd;
3997
+ }
3998
+
3999
+ /* could be a XD3_GETSRCBLK failure. */
4000
+ return ret;
4001
+ }
4002
+
4003
+ tryrem = xd3_min(stream->match_maxfwd - stream->match_fwd,
4004
+ src->onblk - tryoff);
4005
+
4006
+ if (tryrem == 0)
4007
+ {
4008
+ /* Generally, this means we have a power-of-two size source
4009
+ * and we just found the end-of-file, in this case it's an
4010
+ * empty block. */
4011
+ XD3_ASSERT (src->onblk < src->blksize);
4012
+ break;
4013
+ }
4014
+
4015
+ matched = xd3_forward_match(src->curblk + tryoff,
4016
+ stream->next_in + streamoff,
4017
+ tryrem);
4018
+ tryoff += matched;
4019
+ streamoff += matched;
4020
+ stream->match_fwd += matched;
4021
+
4022
+ if (tryrem != matched)
4023
+ {
4024
+ break;
4025
+ }
4026
+ }
4027
+
4028
+ donefwd:
4029
+ stream->match_state = MATCH_SEARCHING;
4030
+
4031
+ IF_DEBUG2(DP(RINT "[extend match] input %"Q"u srcpos %"Q"u len %"W"u\n",
4032
+ stream->input_position + stream->total_in,
4033
+ stream->match_srcpos,
4034
+ stream->match_fwd));
4035
+
4036
+ /* If the match ends short of the last instruction end, we probably
4037
+ * don't want it. There is the possibility that a copy ends short
4038
+ * of the last copy but also goes further back, in which case we
4039
+ * might want it. This code does not implement such: if so we would
4040
+ * need more complicated xd3_iopt_erase logic. */
4041
+ if (stream->match_fwd < stream->min_match)
4042
+ {
4043
+ stream->match_fwd = 0;
4044
+ }
4045
+ else
4046
+ {
4047
+ usize_t total = stream->match_fwd + stream->match_back;
4048
+
4049
+ /* Correct the variables to remove match_back from the equation. */
4050
+ usize_t target_position = stream->input_position - stream->match_back;
4051
+ usize_t match_length = stream->match_back + stream->match_fwd;
4052
+ xoff_t match_position = stream->match_srcpos - stream->match_back;
4053
+ xoff_t match_end = stream->match_srcpos + stream->match_fwd;
4054
+
4055
+ /* At this point we may have to erase any iopt-buffer
4056
+ * instructions that are fully covered by a backward-extending
4057
+ * copy. */
4058
+ if (stream->match_back > 0)
4059
+ {
4060
+ xd3_iopt_erase (stream, target_position, total);
4061
+ }
4062
+
4063
+ stream->match_back = 0;
4064
+
4065
+ /* Update ranges. The first source match occurs with both
4066
+ values set to 0. */
4067
+ if (stream->match_maxaddr == 0 ||
4068
+ match_position < stream->match_minaddr)
4069
+ {
4070
+ stream->match_minaddr = match_position;
4071
+ }
4072
+
4073
+ if (match_end > stream->match_maxaddr)
4074
+ {
4075
+ /* Note: per-window */
4076
+ stream->match_maxaddr = match_end;
4077
+ }
4078
+
4079
+ if (match_end > stream->maxsrcaddr)
4080
+ {
4081
+ /* Note: across windows */
4082
+ stream->maxsrcaddr = match_end;
4083
+ }
4084
+
4085
+ IF_DEBUG2 ({
4086
+ static int x = 0;
4087
+ DP(RINT "[source match:%d] length %"W"u <inp %"Q"u %"Q"u> <src %"Q"u %"Q"u> (%s)\n",
4088
+ x++,
4089
+ match_length,
4090
+ stream->total_in + target_position,
4091
+ stream->total_in + target_position + match_length,
4092
+ match_position,
4093
+ match_position + match_length,
4094
+ (stream->total_in + target_position == match_position) ? "same" : "diff");
4095
+ });
4096
+
4097
+ if ((ret = xd3_found_match (stream,
4098
+ /* decoder position */ target_position,
4099
+ /* length */ match_length,
4100
+ /* address */ match_position,
4101
+ /* is_source */ 1)))
4102
+ {
4103
+ return ret;
4104
+ }
4105
+
4106
+ /* If the match ends with the available input: */
4107
+ if (target_position + match_length == stream->avail_in)
4108
+ {
4109
+ /* Setup continuing match for the next window. */
4110
+ stream->match_state = MATCH_TARGET;
4111
+ stream->match_srcpos = match_end;
4112
+ }
4113
+ }
4114
+
4115
+ return 0;
4116
+ }
4117
+
4118
+ /* Update the small hash. Values in the small_table are offset by
4119
+ * HASH_CKOFFSET (1) to distinguish empty buckets from real offsets. */
4120
+ static void
4121
+ xd3_scksum_insert (xd3_stream *stream,
4122
+ usize_t inx,
4123
+ usize_t scksum,
4124
+ usize_t pos)
4125
+ {
4126
+ /* If we are maintaining previous duplicates. */
4127
+ if (stream->small_prev)
4128
+ {
4129
+ usize_t last_pos = stream->small_table[inx];
4130
+ xd3_slist *pos_list = & stream->small_prev[pos & stream->sprevmask];
4131
+
4132
+ /* Note last_pos is offset by HASH_CKOFFSET. */
4133
+ pos_list->last_pos = last_pos;
4134
+ }
4135
+
4136
+ /* Enter the new position into the hash bucket. */
4137
+ stream->small_table[inx] = pos + HASH_CKOFFSET;
4138
+ }
4139
+
4140
+ #if XD3_DEBUG
4141
+ static int
4142
+ xd3_check_smatch (const uint8_t *ref0, const uint8_t *inp0,
4143
+ const uint8_t *inp_max, usize_t cmp_len)
4144
+ {
4145
+ usize_t i;
4146
+
4147
+ for (i = 0; i < cmp_len; i += 1)
4148
+ {
4149
+ XD3_ASSERT (ref0[i] == inp0[i]);
4150
+ }
4151
+
4152
+ if (inp0 + cmp_len < inp_max)
4153
+ {
4154
+ XD3_ASSERT (inp0[i] != ref0[i]);
4155
+ }
4156
+
4157
+ return 1;
4158
+ }
4159
+ #endif /* XD3_DEBUG */
4160
+
4161
+ /* When the hash table indicates a possible small string match, it
4162
+ * calls this routine to find the best match. The first matching
4163
+ * position is taken from the small_table, HASH_CKOFFSET is subtracted
4164
+ * to get the actual position. After checking that match, if previous
4165
+ * linked lists are in use (because stream->smatcher.small_chain > 1),
4166
+ * previous matches are tested searching for the longest match. If
4167
+ * (stream->min_match > MIN_MATCH) then a lazy match is in effect.
4168
+ */
4169
+ static usize_t
4170
+ xd3_smatch (xd3_stream *stream,
4171
+ usize_t base,
4172
+ usize_t scksum,
4173
+ usize_t *match_offset)
4174
+ {
4175
+ usize_t cmp_len;
4176
+ usize_t match_length = 0;
4177
+ usize_t chain = (stream->min_match == MIN_MATCH ?
4178
+ stream->smatcher.small_chain :
4179
+ stream->smatcher.small_lchain);
4180
+ const uint8_t *inp_max = stream->next_in + stream->avail_in;
4181
+ const uint8_t *inp;
4182
+ const uint8_t *ref;
4183
+
4184
+ SMALL_HASH_DEBUG1 (stream, stream->next_in + stream->input_position);
4185
+
4186
+ XD3_ASSERT (stream->min_match + stream->input_position <= stream->avail_in);
4187
+
4188
+ base -= HASH_CKOFFSET;
4189
+
4190
+ again:
4191
+
4192
+ IF_DEBUG2 (DP(RINT "smatch at base=%"W"u inp=%"W"u cksum=%"W"u\n", base,
4193
+ stream->input_position, scksum));
4194
+
4195
+ /* For small matches, we can always go to the end-of-input because
4196
+ * the matching position must be less than the input position. */
4197
+ XD3_ASSERT (base < stream->input_position);
4198
+
4199
+ ref = stream->next_in + base;
4200
+ inp = stream->next_in + stream->input_position;
4201
+
4202
+ SMALL_HASH_DEBUG2 (stream, ref);
4203
+
4204
+ /* Expand potential match forward. */
4205
+ while (inp < inp_max && *inp == *ref)
4206
+ {
4207
+ ++inp;
4208
+ ++ref;
4209
+ }
4210
+
4211
+ cmp_len = (usize_t)(inp - (stream->next_in + stream->input_position));
4212
+
4213
+ /* Verify correctness */
4214
+ XD3_ASSERT (xd3_check_smatch (stream->next_in + base,
4215
+ stream->next_in + stream->input_position,
4216
+ inp_max, cmp_len));
4217
+
4218
+ /* Update longest match */
4219
+ if (cmp_len > match_length)
4220
+ {
4221
+ ( match_length) = cmp_len;
4222
+ (*match_offset) = base;
4223
+
4224
+ /* Stop if we match the entire input or have a long_enough match. */
4225
+ if (inp == inp_max || cmp_len >= stream->smatcher.long_enough)
4226
+ {
4227
+ goto done;
4228
+ }
4229
+ }
4230
+
4231
+ /* If we have not reached the chain limit, see if there is another
4232
+ previous position. */
4233
+ while (--chain != 0)
4234
+ {
4235
+ /* Calculate the previous offset. */
4236
+ usize_t prev_pos = stream->small_prev[base & stream->sprevmask].last_pos;
4237
+ usize_t diff_pos;
4238
+
4239
+ if (prev_pos == 0)
4240
+ {
4241
+ break;
4242
+ }
4243
+
4244
+ prev_pos -= HASH_CKOFFSET;
4245
+
4246
+ if (prev_pos > base)
4247
+ {
4248
+ break;
4249
+ }
4250
+
4251
+ base = prev_pos;
4252
+
4253
+ XD3_ASSERT (stream->input_position > base);
4254
+ diff_pos = stream->input_position - base;
4255
+
4256
+ /* Stop searching if we go beyond sprevsz, since those entries
4257
+ * are for unrelated checksum entries. */
4258
+ if (diff_pos & ~stream->sprevmask)
4259
+ {
4260
+ break;
4261
+ }
4262
+
4263
+ goto again;
4264
+ }
4265
+
4266
+ done:
4267
+ /* Crude efficiency test: if the match is very short and very far back, it's
4268
+ * unlikely to help, but the exact calculation requires knowing the state of
4269
+ * the address cache and adjacent instructions, which we can't do here.
4270
+ * Rather than encode a probably inefficient copy here and check it later
4271
+ * (which complicates the code a lot), do this:
4272
+ */
4273
+ if (match_length == 4 && stream->input_position - (*match_offset) >= 1<<14)
4274
+ {
4275
+ /* It probably takes >2 bytes to encode an address >= 2^14 from here */
4276
+ return 0;
4277
+ }
4278
+ if (match_length == 5 && stream->input_position - (*match_offset) >= 1<<21)
4279
+ {
4280
+ /* It probably takes >3 bytes to encode an address >= 2^21 from here */
4281
+ return 0;
4282
+ }
4283
+
4284
+ /* It's unlikely that a window is large enough for the (match_length == 6 &&
4285
+ * address >= 2^28) check */
4286
+ return match_length;
4287
+ }
4288
+
4289
+ #if XD3_DEBUG
4290
+ static void
4291
+ xd3_verify_small_state (xd3_stream *stream,
4292
+ const uint8_t *inp,
4293
+ uint32_t x_cksum)
4294
+ {
4295
+ uint32_t state;
4296
+ uint32_t cksum = xd3_scksum (&state, inp, stream->smatcher.small_look);
4297
+
4298
+ XD3_ASSERT (cksum == x_cksum);
4299
+ }
4300
+
4301
+ static void
4302
+ xd3_verify_large_state (xd3_stream *stream,
4303
+ const uint8_t *inp,
4304
+ usize_t x_cksum)
4305
+ {
4306
+ usize_t cksum = xd3_large_cksum (&stream->large_hash, inp, stream->smatcher.large_look);
4307
+ XD3_ASSERT (cksum == x_cksum);
4308
+ }
4309
+ static void
4310
+ xd3_verify_run_state (xd3_stream *stream,
4311
+ const uint8_t *inp,
4312
+ usize_t x_run_l,
4313
+ uint8_t *x_run_c)
4314
+ {
4315
+ usize_t slook = stream->smatcher.small_look;
4316
+ uint8_t run_c;
4317
+ usize_t run_l = xd3_comprun (inp, slook, &run_c);
4318
+
4319
+ XD3_ASSERT (run_l == 0 || run_c == *x_run_c);
4320
+ XD3_ASSERT (x_run_l > slook || run_l == x_run_l);
4321
+ }
4322
+ #endif /* XD3_DEBUG */
4323
+
4324
+ /* This function computes more source checksums to advance the window.
4325
+ * Called at every entrance to the string-match loop and each time
4326
+ * stream->input_position reaches the value returned as
4327
+ * *next_move_point. NB: this is one of the most expensive functions
4328
+ * in this code and also the most critical for good compression.
4329
+ */
4330
+ static int
4331
+ xd3_srcwin_move_point (xd3_stream *stream, usize_t *next_move_point)
4332
+ {
4333
+ /* the source file is indexed until this point */
4334
+ xoff_t target_cksum_pos;
4335
+ /* the absolute target file input position */
4336
+ xoff_t absolute_input_pos;
4337
+
4338
+ if (stream->src->eof_known)
4339
+ {
4340
+ xoff_t source_size = xd3_source_eof (stream->src);
4341
+ XD3_ASSERT(stream->srcwin_cksum_pos <= source_size);
4342
+
4343
+ if (stream->srcwin_cksum_pos == source_size)
4344
+ {
4345
+ *next_move_point = USIZE_T_MAX;
4346
+ return 0;
4347
+ }
4348
+ }
4349
+
4350
+ absolute_input_pos = stream->total_in + stream->input_position;
4351
+
4352
+ /* Immediately read the entire window.
4353
+ *
4354
+ * Note: this reverses a long held policy, at this point in the
4355
+ * code, of advancing relatively slowly as the input is read, which
4356
+ * results in better compression for very-similar inputs, but worse
4357
+ * compression where data is deleted near the beginning of the file.
4358
+ *
4359
+ * The new policy is simpler, somewhat slower and can benefit, or
4360
+ * slightly worsen, compression performance. */
4361
+ if (absolute_input_pos < stream->src->max_winsize / 2)
4362
+ {
4363
+ target_cksum_pos = stream->src->max_winsize;
4364
+ }
4365
+ else
4366
+ {
4367
+ /* TODO: The addition of 2 blocks here is arbitrary. Do a
4368
+ * better job of stream alignment based on observed source copy
4369
+ * addresses, and when both input sizes are known, the
4370
+ * difference in size. */
4371
+ target_cksum_pos = absolute_input_pos +
4372
+ stream->src->max_winsize / 2 +
4373
+ stream->src->blksize * 2;
4374
+ target_cksum_pos &= ~stream->src->maskby;
4375
+ }
4376
+
4377
+ /* A long match may have extended past srcwin_cksum_pos. Don't
4378
+ * start checksumming already-matched source data. */
4379
+ if (stream->maxsrcaddr > stream->srcwin_cksum_pos)
4380
+ {
4381
+ stream->srcwin_cksum_pos = stream->maxsrcaddr;
4382
+ }
4383
+
4384
+ if (target_cksum_pos < stream->srcwin_cksum_pos)
4385
+ {
4386
+ target_cksum_pos = stream->srcwin_cksum_pos;
4387
+ }
4388
+
4389
+ while (stream->srcwin_cksum_pos < target_cksum_pos &&
4390
+ (!stream->src->eof_known ||
4391
+ stream->srcwin_cksum_pos < xd3_source_eof (stream->src)))
4392
+ {
4393
+ xoff_t blkno;
4394
+ xoff_t blkbaseoffset;
4395
+ usize_t blkrem;
4396
+ ssize_t oldpos; /* Using ssize_t because of a */
4397
+ ssize_t blkpos; /* do { blkpos-- }
4398
+ while (blkpos >= oldpos); */
4399
+ int ret;
4400
+ xd3_blksize_div (stream->srcwin_cksum_pos,
4401
+ stream->src, &blkno, &blkrem);
4402
+ oldpos = blkrem;
4403
+
4404
+ if ((ret = xd3_getblk (stream, blkno)))
4405
+ {
4406
+ /* TOOFARBACK should never occur here, since we read forward. */
4407
+ if (ret == XD3_TOOFARBACK)
4408
+ {
4409
+ ret = XD3_INTERNAL;
4410
+ }
4411
+
4412
+ IF_DEBUG1 (DP(RINT
4413
+ "[srcwin_move_point] async getblk return for %"Q"u: %s\n",
4414
+ blkno, xd3_strerror (ret)));
4415
+ return ret;
4416
+ }
4417
+
4418
+ IF_DEBUG1 (DP(RINT
4419
+ "[srcwin_move_point] block %"Q"u T=%"Q"u S=%"Q"u L=%"Q"u EOF=%"Q"u %s\n",
4420
+ blkno,
4421
+ stream->total_in + stream->input_position,
4422
+ stream->srcwin_cksum_pos,
4423
+ target_cksum_pos,
4424
+ xd3_source_eof (stream->src),
4425
+ stream->src->eof_known ? "known" : "unknown"));
4426
+
4427
+ blkpos = xd3_bytes_on_srcblk (stream->src, blkno);
4428
+
4429
+ if (blkpos < (ssize_t) stream->smatcher.large_look)
4430
+ {
4431
+ stream->srcwin_cksum_pos = (blkno + 1) * stream->src->blksize;
4432
+ IF_DEBUG2 (DP(RINT "[srcwin_move_point] continue (end-of-block): %"Z"d\n", blkpos));
4433
+ continue;
4434
+ }
4435
+
4436
+ /* This inserts checksums for the entire block, in reverse,
4437
+ * starting from the end of the block. This logic does not test
4438
+ * stream->srcwin_cksum_pos because it always advances it to the
4439
+ * start of the next block.
4440
+ *
4441
+ * oldpos is the srcwin_cksum_pos within this block. blkpos is
4442
+ * the number of bytes available. Each iteration inspects
4443
+ * large_look bytes then steps back large_step bytes. The
4444
+ * if-stmt above ensures at least one large_look of data. */
4445
+ blkpos -= stream->smatcher.large_look;
4446
+ blkbaseoffset = stream->src->blksize * blkno;
4447
+
4448
+ do
4449
+ {
4450
+ /* TODO: This would be significantly faster if the compiler
4451
+ * knew stream->smatcher.large_look (which the template for
4452
+ * xd3_string_match_* allows). */
4453
+ usize_t cksum = xd3_large_cksum (&stream->large_hash,
4454
+ stream->src->curblk + blkpos,
4455
+ stream->smatcher.large_look);
4456
+ usize_t hval = xd3_checksum_hash (& stream->large_hash, cksum);
4457
+
4458
+ stream->large_table[hval] =
4459
+ (usize_t) (blkbaseoffset +
4460
+ (xoff_t)(blkpos + HASH_CKOFFSET));
4461
+
4462
+ IF_DEBUG (stream->large_ckcnt += 1);
4463
+
4464
+ blkpos -= stream->smatcher.large_step;
4465
+ }
4466
+ while (blkpos >= oldpos);
4467
+
4468
+ stream->srcwin_cksum_pos = (blkno + 1) * stream->src->blksize;
4469
+ }
4470
+
4471
+ IF_DEBUG1 (DP(RINT
4472
+ "[srcwin_move_point] exited loop T=%"Q"u "
4473
+ "S=%"Q"u EOF=%"Q"u %s\n",
4474
+ stream->total_in + stream->input_position,
4475
+ stream->srcwin_cksum_pos,
4476
+ xd3_source_eof (stream->src),
4477
+ stream->src->eof_known ? "known" : "unknown"));
4478
+
4479
+ if (stream->src->eof_known)
4480
+ {
4481
+ xoff_t source_size = xd3_source_eof (stream->src);
4482
+ if (stream->srcwin_cksum_pos >= source_size)
4483
+ {
4484
+ /* This invariant is needed for xd3_source_cksum_offset() */
4485
+ stream->srcwin_cksum_pos = source_size;
4486
+ *next_move_point = USIZE_T_MAX;
4487
+ IF_DEBUG1 (DP(RINT
4488
+ "[srcwin_move_point] finished with source input\n"));
4489
+ return 0;
4490
+ }
4491
+ }
4492
+
4493
+ /* How long until this function should be called again. */
4494
+ XD3_ASSERT(stream->srcwin_cksum_pos >= target_cksum_pos);
4495
+
4496
+ *next_move_point = stream->input_position +
4497
+ stream->src->blksize -
4498
+ ((stream->srcwin_cksum_pos - target_cksum_pos) & stream->src->maskby);
4499
+
4500
+ IF_DEBUG2 (DP(RINT
4501
+ "[srcwin_move_point] finished T=%"Q"u "
4502
+ "S=%"Q"u L=%"Q"u EOF=%"Q"u %s again in %"W"u\n",
4503
+ stream->total_in + stream->input_position,
4504
+ stream->srcwin_cksum_pos,
4505
+ target_cksum_pos,
4506
+ xd3_source_eof (stream->src),
4507
+ stream->src->eof_known ? "known" : "unknown",
4508
+ *next_move_point - stream->input_position));
4509
+
4510
+ return 0;
4511
+ }
4512
+
4513
+ #endif /* XD3_ENCODER */
4514
+
4515
+ /********************************************************************
4516
+ TEMPLATE pass
4517
+ *********************************************************************/
4518
+
4519
+ #endif /* __XDELTA3_C_INLINE_PASS__ */
4520
+ #ifdef __XDELTA3_C_TEMPLATE_PASS__
4521
+
4522
+ #if XD3_ENCODER
4523
+
4524
+ /********************************************************************
4525
+ Templates
4526
+ *******************************************************************/
4527
+
4528
+ /* Template macros */
4529
+ #define XD3_TEMPLATE(x) XD3_TEMPLATE2(x,TEMPLATE)
4530
+ #define XD3_TEMPLATE2(x,n) XD3_TEMPLATE3(x,n)
4531
+ #define XD3_TEMPLATE3(x,n) x ## n
4532
+ #define XD3_STRINGIFY(x) XD3_STRINGIFY2(x)
4533
+ #define XD3_STRINGIFY2(x) #x
4534
+
4535
+ static int XD3_TEMPLATE(xd3_string_match_) (xd3_stream *stream);
4536
+
4537
+ static const xd3_smatcher XD3_TEMPLATE(__smatcher_) =
4538
+ {
4539
+ XD3_STRINGIFY(TEMPLATE),
4540
+ XD3_TEMPLATE(xd3_string_match_),
4541
+ #if SOFTCFG == 1
4542
+ 0, 0, 0, 0, 0, 0, 0
4543
+ #else
4544
+ LLOOK, LSTEP, SLOOK, SCHAIN, SLCHAIN, MAXLAZY, LONGENOUGH
4545
+ #endif
4546
+ };
4547
+
4548
+ static int
4549
+ XD3_TEMPLATE(xd3_string_match_) (xd3_stream *stream)
4550
+ {
4551
+ const int DO_SMALL = ! (stream->flags & XD3_NOCOMPRESS);
4552
+ const int DO_LARGE = (stream->src != NULL);
4553
+ const int DO_RUN = (1);
4554
+
4555
+ const uint8_t *inp;
4556
+ uint32_t scksum = 0;
4557
+ uint32_t scksum_state = 0;
4558
+ usize_t lcksum = 0;
4559
+ usize_t sinx;
4560
+ usize_t linx;
4561
+ uint8_t run_c;
4562
+ usize_t run_l;
4563
+ int ret;
4564
+ usize_t match_length;
4565
+ usize_t match_offset = 0;
4566
+ usize_t next_move_point = 0;
4567
+
4568
+ IF_DEBUG2(DP(RINT "[string_match] initial entry %"W"u\n", stream->input_position));
4569
+
4570
+ /* If there will be no compression due to settings or short input,
4571
+ * skip it entirely. */
4572
+ if (! (DO_SMALL || DO_LARGE || DO_RUN) ||
4573
+ stream->input_position + SLOOK > stream->avail_in) { goto loopnomore; }
4574
+
4575
+ if ((ret = xd3_string_match_init (stream))) { return ret; }
4576
+
4577
+ /* The restartloop label is reached when the incremental loop state
4578
+ * needs to be reset. */
4579
+ restartloop:
4580
+
4581
+ IF_DEBUG2(DP(RINT "[string_match] restartloop %"W"u\n", stream->input_position));
4582
+
4583
+ /* If there is not enough input remaining for any kind of match,
4584
+ skip it. */
4585
+ if (stream->input_position + SLOOK > stream->avail_in) { goto loopnomore; }
4586
+
4587
+ /* Now reset the incremental loop state: */
4588
+
4589
+ /* The min_match variable is updated to avoid matching the same lazy
4590
+ * match over and over again. For example, if you find a (small)
4591
+ * match of length 9 at one position, you will likely find a match
4592
+ * of length 8 at the next position. */
4593
+ if (xd3_iopt_last_matched (stream) > stream->input_position)
4594
+ {
4595
+ stream->min_match = xd3_max (MIN_MATCH,
4596
+ 1 + xd3_iopt_last_matched(stream) -
4597
+ stream->input_position);
4598
+ }
4599
+ else
4600
+ {
4601
+ stream->min_match = MIN_MATCH;
4602
+ }
4603
+
4604
+ /* The current input byte. */
4605
+ inp = stream->next_in + stream->input_position;
4606
+
4607
+ /* Small match state. */
4608
+ if (DO_SMALL)
4609
+ {
4610
+ scksum = xd3_scksum (&scksum_state, inp, SLOOK);
4611
+ }
4612
+
4613
+ /* Run state. */
4614
+ if (DO_RUN)
4615
+ {
4616
+ run_l = xd3_comprun (inp, SLOOK, & run_c);
4617
+ }
4618
+
4619
+ /* Large match state. We continue the loop even after not enough
4620
+ * bytes for LLOOK remain, so always check stream->input_position in
4621
+ * DO_LARGE code. */
4622
+ if (DO_LARGE && (stream->input_position + LLOOK <= stream->avail_in))
4623
+ {
4624
+ /* Source window: next_move_point is the point that
4625
+ * stream->input_position must reach before computing more
4626
+ * source checksum. Note: this is called unconditionally
4627
+ * the first time after reentry, subsequent calls will be
4628
+ * avoided if next_move_point is > input_position */
4629
+ if ((ret = xd3_srcwin_move_point (stream, & next_move_point)))
4630
+ {
4631
+ return ret;
4632
+ }
4633
+
4634
+ lcksum = xd3_large_cksum (&stream->large_hash, inp, LLOOK);
4635
+ }
4636
+
4637
+ /* TRYLAZYLEN: True if a certain length match should be followed by
4638
+ * lazy search. This checks that LEN is shorter than MAXLAZY and
4639
+ * that there is enough leftover data to consider lazy matching.
4640
+ * "Enough" is set to 2 since the next match will start at the next
4641
+ * offset, it must match two extra characters. */
4642
+ #define TRYLAZYLEN(LEN,POS,MAX) ((MAXLAZY) > 0 && (LEN) < (MAXLAZY) \
4643
+ && (POS) + (LEN) <= (MAX) - 2)
4644
+
4645
+ /* HANDLELAZY: This statement is called each time an instruciton is
4646
+ * emitted (three cases). If the instruction is large enough, the
4647
+ * loop is restarted, otherwise lazy matching may ensue. */
4648
+ #define HANDLELAZY(mlen) \
4649
+ if (TRYLAZYLEN ((mlen), (stream->input_position), (stream->avail_in))) \
4650
+ { stream->min_match = (mlen) + LEAST_MATCH_INCR; goto updateone; } \
4651
+ else \
4652
+ { stream->input_position += (mlen); goto restartloop; }
4653
+
4654
+ /* Now loop over one input byte at a time until a match is found... */
4655
+ for (;; inp += 1, stream->input_position += 1)
4656
+ {
4657
+ /* Now we try three kinds of string match in order of expense:
4658
+ * run, large match, small match. */
4659
+
4660
+ /* Expand the start of a RUN. The test for (run_l == SLOOK)
4661
+ * avoids repeating this check when we pass through a run area
4662
+ * performing lazy matching. The run is only expanded once when
4663
+ * the min_match is first reached. If lazy matching is
4664
+ * performed, the run_l variable will remain inconsistent until
4665
+ * the first non-running input character is reached, at which
4666
+ * time the run_l may then again grow to SLOOK. */
4667
+ if (DO_RUN && run_l == SLOOK)
4668
+ {
4669
+ usize_t max_len = stream->avail_in - stream->input_position;
4670
+
4671
+ IF_DEBUG (xd3_verify_run_state (stream, inp, run_l, &run_c));
4672
+
4673
+ while (run_l < max_len && inp[run_l] == run_c) { run_l += 1; }
4674
+
4675
+ /* Output a RUN instruction. */
4676
+ if (run_l >= stream->min_match && run_l >= MIN_RUN)
4677
+ {
4678
+ if ((ret = xd3_emit_run (stream, stream->input_position,
4679
+ run_l, &run_c))) { return ret; }
4680
+
4681
+ HANDLELAZY (run_l);
4682
+ }
4683
+ }
4684
+
4685
+ /* If there is enough input remaining. */
4686
+ if (DO_LARGE && (stream->input_position + LLOOK <= stream->avail_in))
4687
+ {
4688
+ if ((stream->input_position >= next_move_point) &&
4689
+ (ret = xd3_srcwin_move_point (stream, & next_move_point)))
4690
+ {
4691
+ return ret;
4692
+ }
4693
+
4694
+ linx = xd3_checksum_hash (& stream->large_hash, lcksum);
4695
+
4696
+ IF_DEBUG (xd3_verify_large_state (stream, inp, lcksum));
4697
+
4698
+ if (stream->large_table[linx] != 0)
4699
+ {
4700
+ /* the match_setup will fail if the source window has
4701
+ * been decided and the match lies outside it.
4702
+ * OPT: Consider forcing a window at this point to
4703
+ * permit a new source window. */
4704
+ xoff_t adj_offset =
4705
+ xd3_source_cksum_offset(stream,
4706
+ stream->large_table[linx] -
4707
+ HASH_CKOFFSET);
4708
+ if (xd3_source_match_setup (stream, adj_offset) == 0)
4709
+ {
4710
+ if ((ret = xd3_source_extend_match (stream)))
4711
+ {
4712
+ return ret;
4713
+ }
4714
+
4715
+ /* Update stream position. match_fwd is zero if no
4716
+ * match. */
4717
+ if (stream->match_fwd > 0)
4718
+ {
4719
+ HANDLELAZY (stream->match_fwd);
4720
+ }
4721
+ }
4722
+ }
4723
+ }
4724
+
4725
+ /* Small matches. */
4726
+ if (DO_SMALL)
4727
+ {
4728
+ sinx = xd3_checksum_hash (& stream->small_hash, scksum);
4729
+
4730
+ /* Verify incremental state in debugging mode. */
4731
+ IF_DEBUG (xd3_verify_small_state (stream, inp, scksum));
4732
+
4733
+ /* Search for the longest match */
4734
+ if (stream->small_table[sinx] != 0)
4735
+ {
4736
+ match_length = xd3_smatch (stream,
4737
+ stream->small_table[sinx],
4738
+ scksum,
4739
+ & match_offset);
4740
+ }
4741
+ else
4742
+ {
4743
+ match_length = 0;
4744
+ }
4745
+
4746
+ /* Insert a hash for this string. */
4747
+ xd3_scksum_insert (stream, sinx, scksum, stream->input_position);
4748
+
4749
+ /* Maybe output a COPY instruction */
4750
+ if (match_length >= stream->min_match)
4751
+ {
4752
+ IF_DEBUG2 ({
4753
+ static int x = 0;
4754
+ DP(RINT "[target match:%d] <inp %"W"u %"W"u> <cpy %"W"u %"W"u> "
4755
+ "(-%"W"d) [ %"W"u bytes ]\n",
4756
+ x++,
4757
+ stream->input_position,
4758
+ stream->input_position + match_length,
4759
+ match_offset,
4760
+ match_offset + match_length,
4761
+ stream->input_position - match_offset,
4762
+ match_length);
4763
+ });
4764
+
4765
+ if ((ret = xd3_found_match (stream,
4766
+ /* decoder position */
4767
+ stream->input_position,
4768
+ /* length */ match_length,
4769
+ /* address */ (xoff_t) match_offset,
4770
+ /* is_source */ 0)))
4771
+ {
4772
+ return ret;
4773
+ }
4774
+
4775
+ /* Copy instruction. */
4776
+ HANDLELAZY (match_length);
4777
+ }
4778
+ }
4779
+
4780
+ /* The logic above prevents excess work during lazy matching by
4781
+ * increasing min_match to avoid smaller matches. Each time we
4782
+ * advance stream->input_position by one, the minimum match
4783
+ * shortens as well. */
4784
+ if (stream->min_match > MIN_MATCH)
4785
+ {
4786
+ stream->min_match -= 1;
4787
+ }
4788
+
4789
+ updateone:
4790
+
4791
+ /* See if there are no more incremental cksums to compute. */
4792
+ if (stream->input_position + SLOOK == stream->avail_in)
4793
+ {
4794
+ goto loopnomore;
4795
+ }
4796
+
4797
+ /* Compute next RUN, CKSUM */
4798
+ if (DO_RUN)
4799
+ {
4800
+ NEXTRUN (inp[SLOOK]);
4801
+ }
4802
+
4803
+ if (DO_SMALL)
4804
+ {
4805
+ scksum = xd3_small_cksum_update (&scksum_state, inp, SLOOK);
4806
+ }
4807
+
4808
+ if (DO_LARGE && (stream->input_position + LLOOK < stream->avail_in))
4809
+ {
4810
+ lcksum = xd3_large_cksum_update (&stream->large_hash, lcksum, inp, LLOOK);
4811
+ }
4812
+ }
4813
+
4814
+ loopnomore:
4815
+ return 0;
4816
+ }
4817
+
4818
+ #endif /* XD3_ENCODER */
4819
+ #endif /* __XDELTA3_C_TEMPLATE_PASS__ */