@gfean/react-native-bundle-drop 0.3.9 → 0.4.2

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 (80) hide show
  1. package/BundleDrop.podspec +9 -2
  2. package/README.md +19 -0
  3. package/android/build.gradle +43 -4
  4. package/android/src/main/cpp/CMakeLists.txt +45 -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 +98 -36
  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 +82 -75
  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 -68
  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 +197 -43
  60. package/lib/scripts/download-bundle.js +18 -7
  61. package/package.json +44 -6
  62. package/third_party/xdelta/NOTICE +7 -0
  63. package/third_party/xdelta/PROVENANCE.md +10 -0
  64. package/third_party/xdelta/bundle_drop_xdelta.c +252 -0
  65. package/third_party/xdelta/bundle_drop_xdelta.h +26 -0
  66. package/third_party/xdelta/xdelta3/LICENSE +176 -0
  67. package/third_party/xdelta/xdelta3/config.h +34 -0
  68. package/third_party/xdelta/xdelta3/xdelta3-cfgs.h +171 -0
  69. package/third_party/xdelta/xdelta3/xdelta3-decode.h +1219 -0
  70. package/third_party/xdelta/xdelta3/xdelta3-hash.h +159 -0
  71. package/third_party/xdelta/xdelta3/xdelta3-internal.h +385 -0
  72. package/third_party/xdelta/xdelta3/xdelta3-list.h +127 -0
  73. package/third_party/xdelta/xdelta3/xdelta3.c +4819 -0
  74. package/third_party/xdelta/xdelta3/xdelta3.h +1476 -0
  75. package/lib/CLI/scripts/init-native-config.d.ts +0 -18
  76. package/lib/CLI/scripts/init-native-config.js +0 -539
  77. package/lib/assetExtensions.d.ts +0 -4
  78. package/lib/assetExtensions.js +0 -35
  79. package/lib/scripts/upload-bundle.d.ts +0 -9
  80. package/lib/scripts/upload-bundle.js +0 -320
@@ -0,0 +1,1476 @@
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
+ /* To learn more about Xdelta, start by reading xdelta3.c. If you are
18
+ * ready to use the API, continue reading here. There are two
19
+ * interfaces -- xd3_encode_input and xd3_decode_input -- plus a dozen
20
+ * or so related calls. This interface is styled after Zlib. */
21
+
22
+ #ifndef _XDELTA3_H_
23
+ #define _XDELTA3_H_
24
+
25
+ #define _POSIX_SOURCE 200112L
26
+ #define _ISOC99_SOURCE
27
+ #define _C99_SOURCE
28
+
29
+ #if HAVE_CONFIG_H
30
+ #include "config.h"
31
+ #endif
32
+
33
+ #include <errno.h>
34
+ #include <stdarg.h>
35
+ #include <stddef.h>
36
+ #include <stdio.h>
37
+ #include <stdlib.h>
38
+ #include <string.h>
39
+ #include <sys/types.h>
40
+
41
+ /****************************************************************/
42
+
43
+ /* Default configured value of stream->winsize. If the program
44
+ * supplies xd3_encode_input() with data smaller than winsize the
45
+ * stream will automatically buffer the input, otherwise the input
46
+ * buffer is used directly.
47
+ */
48
+ #ifndef XD3_DEFAULT_WINSIZE
49
+ #define XD3_DEFAULT_WINSIZE (1U << 23)
50
+ #endif
51
+
52
+ /* Default total size of the source window used in xdelta3-main.h */
53
+ #ifndef XD3_DEFAULT_SRCWINSZ
54
+ #define XD3_DEFAULT_SRCWINSZ (1U << 26)
55
+ #endif
56
+
57
+ /* When Xdelta requests a memory allocation for certain buffers, it
58
+ * rounds up to units of at least this size. The code assumes (and
59
+ * asserts) that this is a power-of-two. */
60
+ #ifndef XD3_ALLOCSIZE
61
+ #define XD3_ALLOCSIZE (1U<<14)
62
+ #endif
63
+
64
+ /* The XD3_HARDMAXWINSIZE parameter is a safety mechanism to protect
65
+ * decoders against malicious files. The decoder will never decode a
66
+ * window larger than this. If the file specifies VCD_TARGET the
67
+ * decoder may require two buffers of this size.
68
+ *
69
+ * 8-16MB is reasonable, probably don't need to go larger. */
70
+ #ifndef XD3_HARDMAXWINSIZE
71
+ #define XD3_HARDMAXWINSIZE (1U<<26)
72
+ #endif
73
+ /* The IOPT_SIZE value sets the size of a buffer used to batch
74
+ * overlapping copy instructions before they are optimized by picking
75
+ * the best non-overlapping ranges. The larger this buffer, the
76
+ * longer a forced xd3_srcwin_setup() decision is held off. Setting
77
+ * this value to 0 causes an unlimited buffer to be used. */
78
+ #ifndef XD3_DEFAULT_IOPT_SIZE
79
+ #define XD3_DEFAULT_IOPT_SIZE (1U<<15)
80
+ #endif
81
+
82
+ /* The maximum distance backward to search for small matches */
83
+ #ifndef XD3_DEFAULT_SPREVSZ
84
+ #define XD3_DEFAULT_SPREVSZ (1U<<18)
85
+ #endif
86
+
87
+ /* The default compression level */
88
+ #ifndef XD3_DEFAULT_LEVEL
89
+ #define XD3_DEFAULT_LEVEL 3
90
+ #endif
91
+
92
+ #ifndef XD3_DEFAULT_SECONDARY_LEVEL
93
+ #define XD3_DEFAULT_SECONDARY_LEVEL 6
94
+ #endif
95
+
96
+ #ifndef XD3_USE_LARGEFILE64
97
+ #define XD3_USE_LARGEFILE64 1
98
+ #endif
99
+
100
+ /* The source window size is limited to 2GB unless
101
+ * XD3_USE_LARGESIZET is defined to 1. */
102
+ #ifndef XD3_USE_LARGESIZET
103
+ #define XD3_USE_LARGESIZET 1
104
+ #endif
105
+
106
+ /* Sizes and addresses within VCDIFF windows are represented as usize_t
107
+ *
108
+ * For source-file offsets and total file sizes, total input and
109
+ * output counts, the xoff_t type is used. The decoder and encoder
110
+ * generally check for overflow of the xoff_t size (this is tested at
111
+ * the 32bit boundary [xdelta3-test.h]).
112
+ */
113
+ #ifndef _WIN32
114
+ #define __STDC_FORMAT_MACROS
115
+ #include <inttypes.h>
116
+ #include <stdint.h>
117
+ #else /* WIN32 case */
118
+ #ifndef WIN32_LEAN_AND_MEAN
119
+ #define WIN32_LEAN_AND_MEAN
120
+ #endif
121
+
122
+ #ifndef WINVER
123
+ #if XD3_USE_LARGEFILE64
124
+ /* 64 bit file offsets: uses GetFileSizeEx and SetFilePointerEx. */
125
+ #define WINVER 0x0500
126
+ #define _WIN32_WINNT 0x0500
127
+ #else /* xoff_t is 32bit */
128
+ /* 32 bit file offsets: uses GetFileSize and SetFilePointer. */
129
+ #define WINVER 0x0400
130
+ #define _WIN32_WINNT 0x0400
131
+ #endif /* if XD3_USE_LARGEFILE64 */
132
+ #endif /* ifndef WINVER */
133
+
134
+ #include <windows.h>
135
+
136
+ /* _MSV_VER is defined by Microsoft tools, not by Mingw32 */
137
+ #ifdef _MSC_VER
138
+ typedef signed int ssize_t;
139
+ typedef int pid_t;
140
+ #if _MSC_VER < 1600
141
+ typedef unsigned char uint8_t;
142
+ typedef unsigned short uint16_t;
143
+ typedef unsigned long uint32_t;
144
+ typedef ULONGLONG uint64_t;
145
+ #else /* _MSC_VER >= 1600 */
146
+ /* For MSVC10 and above */
147
+ #include <stdint.h>
148
+ #define inline __inline
149
+ #endif /* _MSC_VER < 1600 */
150
+ #else /* _MSC_VER not defined */
151
+ /* Mingw32 */
152
+ #include <stdint.h>
153
+ #endif /* _MSC_VER defined */
154
+
155
+ #endif /* _WIN32 defined */
156
+
157
+ /* Settings based on the size of xoff_t (32 vs 64 file offsets) */
158
+ #if XD3_USE_LARGEFILE64
159
+ /* xoff_t is a 64-bit type */
160
+ #define __USE_FILE_OFFSET64 1 /* GLIBC: for 64bit fileops. */
161
+
162
+ #ifndef _LARGEFILE_SOURCE
163
+ #define _LARGEFILE_SOURCE
164
+ #endif
165
+
166
+ #ifndef _FILE_OFFSET_BITS
167
+ #define _FILE_OFFSET_BITS 64
168
+ #endif
169
+
170
+ static_assert(SIZEOF_SIZE_T == sizeof(size_t), "SIZEOF_SIZE_T not correctly set");
171
+ static_assert(SIZEOF_UNSIGNED_LONG_LONG == sizeof(unsigned long long), "SIZEOF_UNSIGNED_LONG_LONG not correctly set");
172
+
173
+ /* Set a xoff_t typedef and the "Q" printf insert. */
174
+ #if defined(_WIN32)
175
+ typedef uint64_t xoff_t;
176
+ #define Q "I64"
177
+ #elif SIZEOF_UNSIGNED_LONG == 8
178
+ typedef unsigned long xoff_t;
179
+ #define Q "l"
180
+ #elif SIZEOF_SIZE_T == 8
181
+ typedef size_t xoff_t;
182
+ #define Q "z"
183
+ #elif SIZEOF_UNSIGNED_LONG_LONG == 8
184
+ typedef unsigned long long xoff_t;
185
+ #define Q "ll"
186
+ #endif /* typedef and #define Q */
187
+
188
+ #define SIZEOF_XOFF_T 8
189
+
190
+ #else /* XD3_USE_LARGEFILE64 == 0 */
191
+
192
+ #if SIZEOF_UNSIGNED_INT == 4
193
+ typedef unsigned int xoff_t;
194
+ #elif SIZEOF_UNSIGNED_LONG == 4
195
+ typedef unsigned long xoff_t;
196
+ #else
197
+ typedef uint32_t xoff_t;
198
+ #endif /* xoff_t is 32 bits */
199
+
200
+ #define SIZEOF_XOFF_T 4
201
+ #define Q
202
+ #endif /* 64 vs 32 bit xoff_t */
203
+
204
+ /* Settings based on the size of usize_t (32 and 64 bit window size) */
205
+ #if XD3_USE_LARGESIZET
206
+
207
+ /* Set a usize_ttypedef and the "W" printf insert. */
208
+ #if defined(_WIN32)
209
+ typedef uint64_t usize_t;
210
+ #define W "I64"
211
+ #elif SIZEOF_UNSIGNED_LONG == 8
212
+ typedef unsigned long usize_t;
213
+ #define W "l"
214
+ #elif SIZEOF_SIZE_T == 8
215
+ typedef size_t usize_t;
216
+ #define W "z"
217
+ #elif SIZEOF_UNSIGNED_LONG_LONG == 8
218
+ typedef unsigned long long usize_t;
219
+ #define W "ll"
220
+ #endif /* typedef and #define W */
221
+
222
+ #define SIZEOF_USIZE_T 8
223
+
224
+ #else /* XD3_USE_LARGESIZET == 0 */
225
+
226
+ #if SIZEOF_UNSIGNED_INT == 4
227
+ typedef unsigned int usize_t;
228
+ #elif SIZEOF_UNSIGNED_LONG == 4
229
+ typedef unsigned long usize_t;
230
+ #else
231
+ typedef uint32_t usize_t;
232
+ #endif /* usize_t is 32 bits */
233
+
234
+ #define SIZEOF_USIZE_T 4
235
+ #define W
236
+
237
+ #endif /* 64 vs 32 bit usize_t */
238
+
239
+ /* Settings based on the size of size_t (the system-provided,
240
+ * usually-but-maybe-not an unsigned type) */
241
+ #if SIZEOF_SIZE_T == 4
242
+ #define Z "z"
243
+ #elif SIZEOF_SIZE_T == 8
244
+ #ifdef _WIN32
245
+ #define Z "I64"
246
+ #else /* !_WIN32 */
247
+ #define Z "z"
248
+ #endif /* Windows or not */
249
+ #else
250
+ #error Bad configure script
251
+ #endif /* size_t printf flags */
252
+
253
+ #define USE_UINT32 (SIZEOF_USIZE_T == 4 || \
254
+ SIZEOF_XOFF_T == 4 || REGRESSION_TEST)
255
+ #define USE_UINT64 (SIZEOF_USIZE_T == 8 || \
256
+ SIZEOF_XOFF_T == 8 || REGRESSION_TEST)
257
+
258
+ #ifndef UNALIGNED_OK
259
+ #ifdef HAVE_ALIGNED_ACCESS_REQUIRED
260
+ #define UNALIGNED_OK 0
261
+ #else
262
+ /* This generally includes all Windows builds. */
263
+ #define UNALIGNED_OK 1
264
+ #endif
265
+ #endif
266
+
267
+ /**********************************************************************/
268
+
269
+ /* Whether to build the encoder, otherwise only build the decoder. */
270
+ #ifndef XD3_ENCODER
271
+ #define XD3_ENCODER 1
272
+ #endif
273
+
274
+ /* The code returned when main() fails, also defined in system
275
+ includes. */
276
+ #ifndef EXIT_FAILURE
277
+ #define EXIT_FAILURE 1
278
+ #endif
279
+
280
+ /* REGRESSION TEST enables the "xdelta3 test" command, which runs a
281
+ series of self-tests. */
282
+ #ifndef REGRESSION_TEST
283
+ #define REGRESSION_TEST 0
284
+ #endif
285
+
286
+ /* XD3_DEBUG=1 enables assertions and various statistics. Levels > 1
287
+ * enable some additional output only useful during development and
288
+ * debugging. */
289
+ #ifndef XD3_DEBUG
290
+ #define XD3_DEBUG 0
291
+ #endif
292
+
293
+ #ifndef PYTHON_MODULE
294
+ #define PYTHON_MODULE 0
295
+ #endif
296
+
297
+ #ifndef SWIG_MODULE
298
+ #define SWIG_MODULE 0
299
+ #endif
300
+
301
+ #ifndef NOT_MAIN
302
+ #define NOT_MAIN 0
303
+ #endif
304
+
305
+ /* There are three string matching functions supplied: one fast, one
306
+ * slow (default), and one soft-configurable. To disable any of
307
+ * these, use the following definitions. */
308
+ #ifndef XD3_BUILD_SLOW
309
+ #define XD3_BUILD_SLOW 1
310
+ #endif
311
+ #ifndef XD3_BUILD_FAST
312
+ #define XD3_BUILD_FAST 1
313
+ #endif
314
+ #ifndef XD3_BUILD_FASTER
315
+ #define XD3_BUILD_FASTER 1
316
+ #endif
317
+ #ifndef XD3_BUILD_FASTEST
318
+ #define XD3_BUILD_FASTEST 1
319
+ #endif
320
+ #ifndef XD3_BUILD_SOFT
321
+ #define XD3_BUILD_SOFT 1
322
+ #endif
323
+ #ifndef XD3_BUILD_DEFAULT
324
+ #define XD3_BUILD_DEFAULT 1
325
+ #endif
326
+
327
+ #if XD3_DEBUG
328
+ #include <stdio.h>
329
+ #endif
330
+
331
+ typedef struct _xd3_stream xd3_stream;
332
+ typedef struct _xd3_source xd3_source;
333
+ typedef struct _xd3_hash_cfg xd3_hash_cfg;
334
+ typedef struct _xd3_smatcher xd3_smatcher;
335
+ typedef struct _xd3_rinst xd3_rinst;
336
+ typedef struct _xd3_dinst xd3_dinst;
337
+ typedef struct _xd3_hinst xd3_hinst;
338
+ typedef struct _xd3_winst xd3_winst;
339
+ typedef struct _xd3_rpage xd3_rpage;
340
+ typedef struct _xd3_addr_cache xd3_addr_cache;
341
+ typedef struct _xd3_output xd3_output;
342
+ typedef struct _xd3_desect xd3_desect;
343
+ typedef struct _xd3_iopt_buflist xd3_iopt_buflist;
344
+ typedef struct _xd3_rlist xd3_rlist;
345
+ typedef struct _xd3_sec_type xd3_sec_type;
346
+ typedef struct _xd3_sec_cfg xd3_sec_cfg;
347
+ typedef struct _xd3_sec_stream xd3_sec_stream;
348
+ typedef struct _xd3_config xd3_config;
349
+ typedef struct _xd3_code_table_desc xd3_code_table_desc;
350
+ typedef struct _xd3_code_table_sizes xd3_code_table_sizes;
351
+ typedef struct _xd3_slist xd3_slist;
352
+ typedef struct _xd3_whole_state xd3_whole_state;
353
+ typedef struct _xd3_wininfo xd3_wininfo;
354
+
355
+ /* The stream configuration has three callbacks functions, all of
356
+ * which may be supplied with NULL values. If config->getblk is
357
+ * provided as NULL, the stream returns XD3_GETSRCBLK. */
358
+
359
+ typedef void* (xd3_alloc_func) (void *opaque,
360
+ size_t items,
361
+ usize_t size);
362
+ typedef void (xd3_free_func) (void *opaque,
363
+ void *address);
364
+
365
+ typedef int (xd3_getblk_func) (xd3_stream *stream,
366
+ xd3_source *source,
367
+ xoff_t blkno);
368
+
369
+ typedef const xd3_dinst* (xd3_code_table_func) (void);
370
+
371
+
372
+ #ifdef _WIN32
373
+ #define vsnprintf_func _vsnprintf
374
+ #define snprintf_func _snprintf
375
+ #else
376
+ #define vsnprintf_func vsnprintf
377
+ #define snprintf_func snprintf
378
+ #endif
379
+ #define short_sprintf(sb,fmt,...) \
380
+ snprintf_func((sb).buf,sizeof((sb).buf),fmt,__VA_ARGS__)
381
+
382
+ /* Type used for short snprintf calls. */
383
+ typedef struct {
384
+ char buf[48];
385
+ } shortbuf;
386
+
387
+ #ifndef PRINTF_ATTRIBUTE
388
+ #ifdef __GNUC__
389
+ #define PRINTF_ATTRIBUTE(x,y) __attribute__ ((__format__ (__printf__, x, y)))
390
+ #else
391
+ #define PRINTF_ATTRIBUTE(x,y)
392
+ #endif
393
+ #endif
394
+
395
+ /* Underlying xprintf() */
396
+ int xsnprintf_func (char *str, size_t n, const char *fmt, ...)
397
+ PRINTF_ATTRIBUTE(3,4);
398
+
399
+ /* XPR(NT "", ...) (used by main) prefixes an "xdelta3: " to the output. */
400
+ void xprintf(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
401
+ #define XPR xprintf
402
+ #define NT "xdelta3: "
403
+ #define NTR ""
404
+ /* DP(RINT ...) */
405
+ #define DP xprintf
406
+ #define RINT ""
407
+
408
+ #if XD3_DEBUG
409
+ #define XD3_ASSERT(x) \
410
+ do { \
411
+ if (! (x)) { \
412
+ DP(RINT "%s:%d: XD3 assertion failed: %s\n", \
413
+ __FILE__, __LINE__, #x); \
414
+ abort (); } } while (0)
415
+ #else
416
+ #define XD3_ASSERT(x) (void)0
417
+ #endif /* XD3_DEBUG */
418
+
419
+ #define xd3_max(x,y) ((x) < (y) ? (y) : (x))
420
+ #define xd3_min(x,y) ((x) < (y) ? (x) : (y))
421
+
422
+ /****************************************************************
423
+ PUBLIC ENUMS
424
+ ******************************************************************/
425
+
426
+ /* These are the five ordinary status codes returned by the
427
+ * xd3_encode_input() and xd3_decode_input() state machines. */
428
+ typedef enum {
429
+
430
+ /* An application must be prepared to handle these five return
431
+ * values from either xd3_encode_input or xd3_decode_input, except
432
+ * in the case of no-source compression, in which case XD3_GETSRCBLK
433
+ * is never returned. More detailed comments for these are given in
434
+ * xd3_encode_input and xd3_decode_input comments, below. */
435
+ XD3_INPUT = -17703, /* need input */
436
+ XD3_OUTPUT = -17704, /* have output */
437
+ XD3_GETSRCBLK = -17705, /* need a block of source input (with no
438
+ * xd3_getblk function), a chance to do
439
+ * non-blocking read. */
440
+ XD3_GOTHEADER = -17706, /* (decode-only) after the initial VCDIFF &
441
+ first window header */
442
+ XD3_WINSTART = -17707, /* notification: returned before a window is
443
+ * processed, giving a chance to
444
+ * XD3_SKIP_WINDOW or not XD3_SKIP_EMIT that
445
+ * window. */
446
+ XD3_WINFINISH = -17708, /* notification: returned after
447
+ encode/decode & output for a window */
448
+ XD3_TOOFARBACK = -17709, /* (encoder only) may be returned by
449
+ getblk() if the block is too old */
450
+ XD3_INTERNAL = -17710, /* internal error */
451
+ XD3_INVALID = -17711, /* invalid config */
452
+ XD3_INVALID_INPUT = -17712, /* invalid input/decoder error */
453
+ XD3_NOSECOND = -17713, /* when secondary compression finds no
454
+ improvement. */
455
+ XD3_UNIMPLEMENTED = -17714 /* currently VCD_TARGET, VCD_CODETABLE */
456
+ } xd3_rvalues;
457
+
458
+ /* special values in config->flags */
459
+ typedef enum
460
+ {
461
+ XD3_JUST_HDR = (1 << 1), /* used by VCDIFF tools, see
462
+ xdelta3-main.h. */
463
+ XD3_SKIP_WINDOW = (1 << 2), /* used by VCDIFF tools, see
464
+ xdelta3-main.h. */
465
+ XD3_SKIP_EMIT = (1 << 3), /* used by VCDIFF tools, see
466
+ xdelta3-main.h. */
467
+ XD3_FLUSH = (1 << 4), /* flush the stream buffer to
468
+ prepare for
469
+ xd3_stream_close(). */
470
+
471
+ XD3_SEC_DJW = (1 << 5), /* use DJW static huffman */
472
+ XD3_SEC_FGK = (1 << 6), /* use FGK adaptive huffman */
473
+ XD3_SEC_LZMA = (1 << 24), /* use LZMA secondary */
474
+
475
+ XD3_SEC_TYPE = (XD3_SEC_DJW | XD3_SEC_FGK | XD3_SEC_LZMA),
476
+
477
+ XD3_SEC_NODATA = (1 << 7), /* disable secondary compression of
478
+ the data section. */
479
+ XD3_SEC_NOINST = (1 << 8), /* disable secondary compression of
480
+ the inst section. */
481
+ XD3_SEC_NOADDR = (1 << 9), /* disable secondary compression of
482
+ the addr section. */
483
+
484
+ XD3_SEC_NOALL = (XD3_SEC_NODATA | XD3_SEC_NOINST | XD3_SEC_NOADDR),
485
+
486
+ XD3_ADLER32 = (1 << 10), /* enable checksum computation in
487
+ the encoder. */
488
+ XD3_ADLER32_NOVER = (1 << 11), /* disable checksum verification in
489
+ the decoder. */
490
+
491
+ XD3_NOCOMPRESS = (1 << 13), /* disable ordinary data
492
+ * compression feature, only search
493
+ * the source, not the target. */
494
+ XD3_BEGREEDY = (1 << 14), /* disable the "1.5-pass
495
+ * algorithm", instead use greedy
496
+ * matching. Greedy is off by
497
+ * default. */
498
+ XD3_ADLER32_RECODE = (1 << 15), /* used by "recode". */
499
+
500
+ /* 4 bits to set the compression level the same as the command-line
501
+ * setting -1 through -9 (-0 corresponds to the XD3_NOCOMPRESS flag,
502
+ * and is independent of compression level). This is for
503
+ * convenience, especially with xd3_encode_memory(). */
504
+
505
+ XD3_COMPLEVEL_SHIFT = 20, /* 20 - 23 */
506
+ XD3_COMPLEVEL_MASK = (0xF << XD3_COMPLEVEL_SHIFT),
507
+ XD3_COMPLEVEL_1 = (1 << XD3_COMPLEVEL_SHIFT),
508
+ XD3_COMPLEVEL_2 = (2 << XD3_COMPLEVEL_SHIFT),
509
+ XD3_COMPLEVEL_3 = (3 << XD3_COMPLEVEL_SHIFT),
510
+ XD3_COMPLEVEL_6 = (6 << XD3_COMPLEVEL_SHIFT),
511
+ XD3_COMPLEVEL_9 = (9 << XD3_COMPLEVEL_SHIFT)
512
+
513
+ } xd3_flags;
514
+
515
+ /* The values of this enumeration are set in xd3_config using the
516
+ * smatch_cfg variable. It can be set to default, slow, fast, etc.,
517
+ * and soft. */
518
+ typedef enum
519
+ {
520
+ XD3_SMATCH_DEFAULT = 0, /* Flags may contain XD3_COMPLEVEL bits,
521
+ else default. */
522
+ XD3_SMATCH_SLOW = 1,
523
+ XD3_SMATCH_FAST = 2,
524
+ XD3_SMATCH_FASTER = 3,
525
+ XD3_SMATCH_FASTEST = 4,
526
+ XD3_SMATCH_SOFT = 5
527
+ } xd3_smatch_cfg;
528
+
529
+ /*********************************************************************
530
+ PRIVATE ENUMS
531
+ **********************************************************************/
532
+
533
+ /* stream->match_state is part of the xd3_encode_input state machine
534
+ * for source matching:
535
+ *
536
+ * 1. the XD3_GETSRCBLK block-read mechanism means reentrant matching
537
+ * 2. this state spans encoder windows: a match and end-of-window
538
+ * will continue in the next 3. the initial target byte and source
539
+ * byte are a presumed match, to avoid some computation in case the
540
+ * inputs are identical.
541
+ */
542
+ typedef enum {
543
+
544
+ MATCH_TARGET = 0, /* in this state, attempt to match the start of
545
+ * the target with the previously set source
546
+ * address (initially 0). */
547
+ MATCH_BACKWARD = 1, /* currently expanding a match backward in the
548
+ source/target. */
549
+ MATCH_FORWARD = 2, /* currently expanding a match forward in the
550
+ source/target. */
551
+ MATCH_SEARCHING = 3 /* currently searching for a match. */
552
+
553
+ } xd3_match_state;
554
+
555
+ /* The xd3_encode_input state machine steps through these states in
556
+ * the following order. The matcher is reentrant and returns
557
+ * XD3_INPUT whenever it requires more data. After receiving
558
+ * XD3_INPUT, if the application reads EOF it should call
559
+ * xd3_stream_close().
560
+ */
561
+ typedef enum {
562
+
563
+ ENC_INIT = 0, /* xd3_encode_input has never been called. */
564
+ ENC_INPUT = 1, /* waiting for xd3_avail_input () to be called. */
565
+ ENC_SEARCH = 2, /* currently searching for matches. */
566
+ ENC_INSTR = 3, /* currently formatting output. */
567
+ ENC_FLUSH = 4, /* currently emitting output. */
568
+ ENC_POSTOUT = 5, /* after an output section. */
569
+ ENC_POSTWIN = 6, /* after all output sections. */
570
+ ENC_ABORTED = 7 /* abort. */
571
+ } xd3_encode_state;
572
+
573
+ /* The xd3_decode_input state machine steps through these states in
574
+ * the following order. The matcher is reentrant and returns
575
+ * XD3_INPUT whenever it requires more data. After receiving
576
+ * XD3_INPUT, if the application reads EOF it should call
577
+ * xd3_stream_close().
578
+ *
579
+ * 0-8: the VCDIFF header
580
+ * 9-18: the VCDIFF window header
581
+ * 19-21: the three primary sections: data, inst, addr
582
+ * 22: producing output: returns XD3_OUTPUT, possibly XD3_GETSRCBLK,
583
+ * 23: return XD3_WINFINISH, set state=9 to decode more input
584
+ */
585
+ typedef enum {
586
+
587
+ DEC_VCHEAD = 0, /* VCDIFF header */
588
+ DEC_HDRIND = 1, /* header indicator */
589
+
590
+ DEC_SECONDID = 2, /* secondary compressor ID */
591
+
592
+ DEC_TABLEN = 3, /* code table length */
593
+ DEC_NEAR = 4, /* code table near */
594
+ DEC_SAME = 5, /* code table same */
595
+ DEC_TABDAT = 6, /* code table data */
596
+
597
+ DEC_APPLEN = 7, /* application data length */
598
+ DEC_APPDAT = 8, /* application data */
599
+
600
+ DEC_WININD = 9, /* window indicator */
601
+
602
+ DEC_CPYLEN = 10, /* copy window length */
603
+ DEC_CPYOFF = 11, /* copy window offset */
604
+
605
+ DEC_ENCLEN = 12, /* length of delta encoding */
606
+ DEC_TGTLEN = 13, /* length of target window */
607
+ DEC_DELIND = 14, /* delta indicator */
608
+
609
+ DEC_DATALEN = 15, /* length of ADD+RUN data */
610
+ DEC_INSTLEN = 16, /* length of instruction data */
611
+ DEC_ADDRLEN = 17, /* length of address data */
612
+
613
+ DEC_CKSUM = 18, /* window checksum */
614
+
615
+ DEC_DATA = 19, /* data section */
616
+ DEC_INST = 20, /* instruction section */
617
+ DEC_ADDR = 21, /* address section */
618
+
619
+ DEC_EMIT = 22, /* producing data */
620
+
621
+ DEC_FINISH = 23, /* window finished */
622
+
623
+ DEC_ABORTED = 24 /* xd3_abort_stream */
624
+ } xd3_decode_state;
625
+
626
+ /************************************************************
627
+ internal types
628
+ ************************************************************/
629
+
630
+ /* instruction lists used in the IOPT buffer */
631
+ struct _xd3_rlist
632
+ {
633
+ xd3_rlist *next;
634
+ xd3_rlist *prev;
635
+ };
636
+
637
+ /* the raw encoding of an instruction used in the IOPT buffer */
638
+ struct _xd3_rinst
639
+ {
640
+ uint8_t type;
641
+ uint8_t xtra;
642
+ uint8_t code1;
643
+ uint8_t code2;
644
+ usize_t pos;
645
+ usize_t size;
646
+ xoff_t addr;
647
+ xd3_rlist link;
648
+ };
649
+
650
+ /* the code-table form of an single- or double-instruction */
651
+ struct _xd3_dinst
652
+ {
653
+ uint8_t type1;
654
+ uint8_t size1;
655
+ uint8_t type2;
656
+ uint8_t size2;
657
+ };
658
+
659
+ /* the decoded form of a single (half) instruction. */
660
+ struct _xd3_hinst
661
+ {
662
+ uint8_t type;
663
+ usize_t size;
664
+ usize_t addr;
665
+ };
666
+
667
+ /* the form of a whole-file instruction */
668
+ struct _xd3_winst
669
+ {
670
+ uint8_t type; /* RUN, ADD, COPY */
671
+ uint8_t mode; /* 0, VCD_SOURCE, VCD_TARGET */
672
+ usize_t size;
673
+ xoff_t addr;
674
+ xoff_t position; /* absolute position of this inst */
675
+ };
676
+
677
+ /* used by the encoder to buffer output in sections. list of blocks. */
678
+ struct _xd3_output
679
+ {
680
+ uint8_t *base;
681
+ usize_t next;
682
+ usize_t avail;
683
+ xd3_output *next_page;
684
+ };
685
+
686
+ /* used by the decoder to buffer input in sections. */
687
+ struct _xd3_desect
688
+ {
689
+ const uint8_t *buf;
690
+ const uint8_t *buf_max;
691
+ usize_t size;
692
+ usize_t pos;
693
+
694
+ /* used in xdelta3-decode.h */
695
+ uint8_t *copied1;
696
+ usize_t alloc1;
697
+
698
+ /* used in xdelta3-second.h */
699
+ uint8_t *copied2;
700
+ usize_t alloc2;
701
+ };
702
+
703
+ /* the VCDIFF address cache, see the RFC */
704
+ struct _xd3_addr_cache
705
+ {
706
+ usize_t s_near;
707
+ usize_t s_same;
708
+ usize_t next_slot; /* the circular index for near */
709
+ usize_t *near_array; /* array of size s_near */
710
+ usize_t *same_array; /* array of size s_same*256 */
711
+ };
712
+
713
+ /* the IOPT buffer list is just a list of buffers, which may be allocated
714
+ * during encode when using an unlimited buffer. */
715
+ struct _xd3_iopt_buflist
716
+ {
717
+ xd3_rinst *buffer;
718
+ xd3_iopt_buflist *next;
719
+ };
720
+
721
+ /* This is the record of a pre-compiled configuration, a subset of
722
+ xd3_config. */
723
+ struct _xd3_smatcher
724
+ {
725
+ const char *name;
726
+ int (*string_match) (xd3_stream *stream);
727
+ usize_t large_look;
728
+ usize_t large_step;
729
+ usize_t small_look;
730
+ usize_t small_chain;
731
+ usize_t small_lchain;
732
+ usize_t max_lazy;
733
+ usize_t long_enough;
734
+ };
735
+
736
+ /* hash table size & power-of-two hash function. */
737
+ struct _xd3_hash_cfg
738
+ {
739
+ usize_t size; // Number of buckets
740
+ usize_t shift;
741
+ usize_t mask;
742
+ usize_t look; // How wide is this checksum
743
+ usize_t multiplier; // K * powers[0]
744
+ usize_t *powers; // Array of [0,look) where powers[look-1] == 1
745
+ // and powers[N] = powers[N+1]*K (Rabin-Karp)
746
+ };
747
+
748
+ /* the sprev list */
749
+ struct _xd3_slist
750
+ {
751
+ usize_t last_pos;
752
+ };
753
+
754
+ /* window info (for whole state) */
755
+ struct _xd3_wininfo {
756
+ xoff_t offset;
757
+ usize_t length;
758
+ uint32_t adler32;
759
+ };
760
+
761
+ /* whole state for, e.g., merge */
762
+ struct _xd3_whole_state {
763
+ usize_t addslen;
764
+ uint8_t *adds;
765
+ usize_t adds_alloc;
766
+
767
+ usize_t instlen;
768
+ xd3_winst *inst;
769
+ usize_t inst_alloc;
770
+
771
+ usize_t wininfolen;
772
+ xd3_wininfo *wininfo;
773
+ usize_t wininfo_alloc;
774
+
775
+ xoff_t length;
776
+ };
777
+
778
+ /********************************************************************
779
+ public types
780
+ *******************************************************************/
781
+
782
+ /* Settings for the secondary compressor. */
783
+ struct _xd3_sec_cfg
784
+ {
785
+ int data_type; /* Which section. (set automatically) */
786
+ usize_t ngroups; /* Number of DJW Huffman groups. */
787
+ usize_t sector_size; /* Sector size. */
788
+ int inefficient; /* If true, ignore efficiency check [avoid XD3_NOSECOND]. */
789
+ };
790
+
791
+ /* This is the user-visible stream configuration. */
792
+ struct _xd3_config
793
+ {
794
+ usize_t winsize; /* The encoder window size. */
795
+ usize_t sprevsz; /* How far back small string
796
+ matching goes */
797
+ usize_t iopt_size; /* entries in the
798
+ instruction-optimizing
799
+ buffer */
800
+
801
+ xd3_getblk_func *getblk; /* The three callbacks. */
802
+ xd3_alloc_func *alloc;
803
+ xd3_free_func *freef;
804
+ void *opaque; /* Not used. */
805
+ uint32_t flags; /* stream->flags are initialized
806
+ * from xd3_config & never
807
+ * modified by the library. Use
808
+ * xd3_set_flags to modify flags
809
+ * settings mid-stream. */
810
+
811
+ xd3_sec_cfg sec_data; /* Secondary compressor config: data */
812
+ xd3_sec_cfg sec_inst; /* Secondary compressor config: inst */
813
+ xd3_sec_cfg sec_addr; /* Secondary compressor config: addr */
814
+
815
+ xd3_smatch_cfg smatch_cfg; /* See enum: use fields below for
816
+ soft config */
817
+ xd3_smatcher smatcher_soft;
818
+ };
819
+
820
+ /* The primary source file object. You create one of these objects and
821
+ * initialize the first four fields. This library maintains the next
822
+ * 5 fields. The configured getblk implementation is responsible for
823
+ * setting the final 3 fields when called (and/or when XD3_GETSRCBLK
824
+ * is returned).
825
+ */
826
+ struct _xd3_source
827
+ {
828
+ /* you set */
829
+ usize_t blksize; /* block size */
830
+ const char *name; /* its name, for debug/print
831
+ purposes */
832
+ void *ioh; /* opaque handle */
833
+ xoff_t max_winsize; /* maximum visible buffer */
834
+
835
+ /* getblk sets */
836
+ xoff_t curblkno; /* current block number: client
837
+ sets after getblk request */
838
+ usize_t onblk; /* number of bytes on current
839
+ block: client sets, must be >= 0
840
+ and <= blksize */
841
+ const uint8_t *curblk; /* current block array: client
842
+ sets after getblk request */
843
+
844
+ /* xd3 sets */
845
+ usize_t srclen; /* length of this source window */
846
+ xoff_t srcbase; /* offset of this source window
847
+ in the source itself */
848
+ usize_t shiftby; /* for power-of-two blocksizes */
849
+ usize_t maskby; /* for power-of-two blocksizes */
850
+ xoff_t cpyoff_blocks; /* offset of dec_cpyoff in blocks */
851
+ usize_t cpyoff_blkoff; /* offset of copy window in
852
+ blocks, remainder */
853
+ xoff_t getblkno; /* request block number: xd3 sets
854
+ current getblk request */
855
+
856
+ /* See xd3_getblk() */
857
+ xoff_t max_blkno; /* Maximum block, if eof is known,
858
+ * otherwise, equals frontier_blkno
859
+ * (initially 0). */
860
+ usize_t onlastblk; /* Number of bytes on max_blkno */
861
+ int eof_known; /* Set to true when the first
862
+ * partial block is read. */
863
+ };
864
+
865
+ /* The primary xd3_stream object, used for encoding and decoding. You
866
+ * may access only two fields: avail_out, next_out. Use the methods
867
+ * above to operate on xd3_stream. */
868
+ struct _xd3_stream
869
+ {
870
+ /* input state */
871
+ const uint8_t *next_in; /* next input byte */
872
+ usize_t avail_in; /* number of bytes available at
873
+ next_in */
874
+ xoff_t total_in; /* how many bytes in */
875
+
876
+ /* output state */
877
+ uint8_t *next_out; /* next output byte */
878
+ usize_t avail_out; /* number of bytes available at
879
+ next_out */
880
+ usize_t space_out; /* total out space */
881
+ xoff_t current_window; /* number of windows encoded/decoded */
882
+ xoff_t total_out; /* how many bytes out */
883
+
884
+ /* to indicate an error, xd3 sets */
885
+ const char *msg; /* last error message, NULL if
886
+ no error */
887
+
888
+ /* source configuration */
889
+ xd3_source *src; /* source array */
890
+
891
+ /* encoder memory configuration */
892
+ usize_t winsize; /* suggested window size */
893
+ usize_t sprevsz; /* small string, previous window
894
+ size (power of 2) */
895
+ usize_t sprevmask; /* small string, previous window
896
+ size mask */
897
+ usize_t iopt_size;
898
+ usize_t iopt_unlimited;
899
+
900
+ /* general configuration */
901
+ xd3_getblk_func *getblk; /* set nxtblk, nxtblkno to scanblkno */
902
+ xd3_alloc_func *alloc; /* malloc function */
903
+ xd3_free_func *free; /* free function */
904
+ void* opaque; /* private data object passed to
905
+ alloc, free, and getblk */
906
+ uint32_t flags; /* various options */
907
+
908
+ /* secondary compressor configuration */
909
+ xd3_sec_cfg sec_data; /* Secondary compressor config: data */
910
+ xd3_sec_cfg sec_inst; /* Secondary compressor config: inst */
911
+ xd3_sec_cfg sec_addr; /* Secondary compressor config: addr */
912
+
913
+ xd3_smatcher smatcher;
914
+
915
+ usize_t *large_table; /* table of large checksums */
916
+ xd3_hash_cfg large_hash; /* large hash config */
917
+
918
+ usize_t *small_table; /* table of small checksums */
919
+ xd3_slist *small_prev; /* table of previous offsets,
920
+ circular linked list */
921
+ int small_reset; /* true if small table should
922
+ be reset */
923
+
924
+ xd3_hash_cfg small_hash; /* small hash config */
925
+ xd3_addr_cache acache; /* the vcdiff address cache */
926
+ xd3_encode_state enc_state; /* state of the encoder */
927
+
928
+ usize_t taroff; /* base offset of the target input */
929
+ usize_t input_position; /* current input position */
930
+ usize_t min_match; /* current minimum match
931
+ length, avoids redundent
932
+ matches */
933
+ usize_t unencoded_offset; /* current input, first
934
+ * unencoded offset. this value
935
+ * is <= the first instruction's
936
+ * position in the iopt buffer,
937
+ * if there is at least one
938
+ * match in the buffer. */
939
+
940
+ /* SRCWIN */
941
+ int srcwin_decided; /* boolean: true if srclen and
942
+ srcbase have been
943
+ decided. */
944
+ int srcwin_decided_early; /* boolean: true if srclen
945
+ and srcbase were
946
+ decided early. */
947
+ xoff_t srcwin_cksum_pos; /* Source checksum position */
948
+
949
+ /* MATCH */
950
+ xd3_match_state match_state; /* encoder match state */
951
+ xoff_t match_srcpos; /* current match source
952
+ position relative to
953
+ srcbase */
954
+ xoff_t match_last_srcpos; /* previously attempted
955
+ * srcpos, to avoid loops. */
956
+ xoff_t match_minaddr; /* smallest matching address to
957
+ * set window params (reset each
958
+ * window xd3_encode_reset) */
959
+ xoff_t match_maxaddr; /* largest matching address to
960
+ * set window params (reset each
961
+ * window xd3_encode_reset) */
962
+ usize_t match_back; /* match extends back so far */
963
+ usize_t match_maxback; /* match extends back maximum */
964
+ usize_t match_fwd; /* match extends forward so far */
965
+ usize_t match_maxfwd; /* match extends forward maximum */
966
+
967
+ xoff_t maxsrcaddr; /* address of the last source
968
+ match (across windows) */
969
+
970
+ uint8_t *buf_in; /* for saving buffered input */
971
+ usize_t buf_avail; /* amount of saved input */
972
+ const uint8_t *buf_leftover; /* leftover content of next_in
973
+ (i.e., user's buffer) */
974
+ usize_t buf_leftavail; /* amount of leftover content */
975
+
976
+ xd3_output *enc_current; /* current output buffer */
977
+ xd3_output *enc_free; /* free output buffers */
978
+ xd3_output *enc_heads[4]; /* array of encoded outputs:
979
+ head of chain */
980
+ xd3_output *enc_tails[4]; /* array of encoded outputs:
981
+ tail of chain */
982
+ uint32_t recode_adler32; /* set the adler32 checksum
983
+ * during "recode". */
984
+
985
+ xd3_rlist iopt_used; /* instruction optimizing buffer */
986
+ xd3_rlist iopt_free;
987
+ xd3_rinst *iout; /* next single instruction */
988
+ xd3_iopt_buflist *iopt_alloc;
989
+
990
+ const uint8_t *enc_appheader; /* application header to encode */
991
+ usize_t enc_appheadsz; /* application header size */
992
+
993
+ /* decoder stuff */
994
+ xd3_decode_state dec_state; /* current DEC_XXX value */
995
+ usize_t dec_hdr_ind; /* VCDIFF header indicator */
996
+ usize_t dec_win_ind; /* VCDIFF window indicator */
997
+ usize_t dec_del_ind; /* VCDIFF delta indicator */
998
+
999
+ uint8_t dec_magic[4]; /* First four bytes */
1000
+ usize_t dec_magicbytes; /* Magic position. */
1001
+
1002
+ usize_t dec_secondid; /* Optional secondary compressor ID. */
1003
+
1004
+ usize_t dec_codetblsz; /* Optional code table: length. */
1005
+ uint8_t *dec_codetbl; /* Optional code table: storage. */
1006
+ usize_t dec_codetblbytes; /* Optional code table: position. */
1007
+
1008
+ usize_t dec_appheadsz; /* Optional application header:
1009
+ size. */
1010
+ uint8_t *dec_appheader; /* Optional application header:
1011
+ storage */
1012
+ usize_t dec_appheadbytes; /* Optional application header:
1013
+ position. */
1014
+
1015
+ usize_t dec_cksumbytes; /* Optional checksum: position. */
1016
+ uint8_t dec_cksum[4]; /* Optional checksum: storage. */
1017
+ uint32_t dec_adler32; /* Optional checksum: value. */
1018
+
1019
+ usize_t dec_cpylen; /* length of copy window
1020
+ (VCD_SOURCE or VCD_TARGET) */
1021
+ xoff_t dec_cpyoff; /* offset of copy window
1022
+ (VCD_SOURCE or VCD_TARGET) */
1023
+ usize_t dec_enclen; /* length of delta encoding */
1024
+ usize_t dec_tgtlen; /* length of target window */
1025
+
1026
+ #if USE_UINT64
1027
+ uint64_t dec_64part; /* part of a decoded uint64_t */
1028
+ #endif
1029
+ #if USE_UINT32
1030
+ uint32_t dec_32part; /* part of a decoded uint32_t */
1031
+ #endif
1032
+
1033
+ xoff_t dec_winstart; /* offset of the start of
1034
+ current target window */
1035
+ xoff_t dec_window_count; /* == current_window + 1 in
1036
+ DEC_FINISH */
1037
+ usize_t dec_winbytes; /* bytes of the three sections
1038
+ so far consumed */
1039
+ usize_t dec_hdrsize; /* VCDIFF + app header size */
1040
+
1041
+ const uint8_t *dec_tgtaddrbase; /* Base of decoded target
1042
+ addresses (addr >=
1043
+ dec_cpylen). */
1044
+ const uint8_t *dec_cpyaddrbase; /* Base of decoded copy
1045
+ addresses (addr <
1046
+ dec_cpylen). */
1047
+
1048
+ usize_t dec_position; /* current decoder position
1049
+ counting the cpylen
1050
+ offset */
1051
+ usize_t dec_maxpos; /* maximum decoder position
1052
+ counting the cpylen
1053
+ offset */
1054
+ xd3_hinst dec_current1; /* current instruction */
1055
+ xd3_hinst dec_current2; /* current instruction */
1056
+
1057
+ uint8_t *dec_buffer; /* Decode buffer */
1058
+ uint8_t *dec_lastwin; /* In case of VCD_TARGET, the
1059
+ last target window. */
1060
+ usize_t dec_lastlen; /* length of the last target
1061
+ window */
1062
+ xoff_t dec_laststart; /* offset of the start of last
1063
+ target window */
1064
+ usize_t dec_lastspace; /* allocated space of last
1065
+ target window, for reuse */
1066
+
1067
+ xd3_desect inst_sect; /* staging area for decoding
1068
+ window sections */
1069
+ xd3_desect addr_sect;
1070
+ xd3_desect data_sect;
1071
+
1072
+ xd3_code_table_func *code_table_func;
1073
+ const xd3_dinst *code_table;
1074
+ const xd3_code_table_desc *code_table_desc;
1075
+ xd3_dinst *code_table_alloc;
1076
+
1077
+ /* secondary compression */
1078
+ const xd3_sec_type *sec_type;
1079
+ xd3_sec_stream *sec_stream_d;
1080
+ xd3_sec_stream *sec_stream_i;
1081
+ xd3_sec_stream *sec_stream_a;
1082
+
1083
+ /* state for reconstructing whole files (e.g., for merge), this only
1084
+ * supports loading USIZE_T_MAX instructions, adds, etc. */
1085
+ xd3_whole_state whole_target;
1086
+
1087
+ /* statistics */
1088
+ xoff_t n_scpy;
1089
+ xoff_t n_tcpy;
1090
+ xoff_t n_add;
1091
+ xoff_t n_run;
1092
+
1093
+ xoff_t l_scpy;
1094
+ xoff_t l_tcpy;
1095
+ xoff_t l_add;
1096
+ xoff_t l_run;
1097
+
1098
+ usize_t i_slots_used;
1099
+
1100
+ #if XD3_DEBUG
1101
+ usize_t large_ckcnt;
1102
+
1103
+ /* memory usage */
1104
+ usize_t alloc_cnt;
1105
+ usize_t free_cnt;
1106
+ #endif
1107
+ };
1108
+
1109
+ /**************************************************************************
1110
+ PUBLIC FUNCTIONS
1111
+ **************************************************************************/
1112
+
1113
+ #ifdef __cplusplus
1114
+ extern "C" {
1115
+ #endif // __cplusplus
1116
+
1117
+
1118
+ /* This function configures an xd3_stream using the provided in-memory
1119
+ * input buffer, source buffer, output buffer, and flags. The output
1120
+ * array must be large enough or else ENOSPC will be returned. This
1121
+ * is the simplest in-memory encoding interface. */
1122
+ int xd3_encode_memory (const uint8_t *input,
1123
+ usize_t input_size,
1124
+ const uint8_t *source,
1125
+ usize_t source_size,
1126
+ uint8_t *output_buffer,
1127
+ usize_t *output_size,
1128
+ usize_t avail_output,
1129
+ int flags);
1130
+
1131
+ /* The reverse of xd3_encode_memory. */
1132
+ int xd3_decode_memory (const uint8_t *input,
1133
+ usize_t input_size,
1134
+ const uint8_t *source,
1135
+ usize_t source_size,
1136
+ uint8_t *output_buf,
1137
+ usize_t *output_size,
1138
+ usize_t avail_output,
1139
+ int flags);
1140
+
1141
+ /* This function encodes an in-memory input using a pre-configured
1142
+ * xd3_stream. This allows the caller to set a variety of options
1143
+ * which are not available in the xd3_encode/decode_memory()
1144
+ * functions.
1145
+ *
1146
+ * The output array must be large enough to hold the output or else
1147
+ * ENOSPC is returned. The source (if any) should be set using
1148
+ * xd3_set_source_and_size() with a single-block xd3_source. This
1149
+ * calls the underlying non-blocking interfaces,
1150
+ * xd3_encode/decode_input(), handling the necessary input/output
1151
+ * states. This method may be considered a reference for any
1152
+ * application using xd3_encode_input() directly.
1153
+ *
1154
+ * xd3_stream stream;
1155
+ * xd3_config config;
1156
+ * xd3_source src;
1157
+ *
1158
+ * memset (& src, 0, sizeof (src));
1159
+ * memset (& stream, 0, sizeof (stream));
1160
+ * memset (& config, 0, sizeof (config));
1161
+ *
1162
+ * if (source != NULL)
1163
+ * {
1164
+ * src.size = source_size;
1165
+ * src.blksize = source_size;
1166
+ * src.curblkno = 0;
1167
+ * src.onblk = source_size;
1168
+ * src.curblk = source;
1169
+ * src.max_winsize = source_size;
1170
+ * xd3_set_source(&stream, &src);
1171
+ * }
1172
+ *
1173
+ * config.flags = flags;
1174
+ * config.winsize = input_size;
1175
+ *
1176
+ * ... set smatcher, appheader, encoding-table, compression-level, etc.
1177
+ *
1178
+ * xd3_config_stream(&stream, &config);
1179
+ * xd3_encode_stream(&stream, ...);
1180
+ * xd3_free_stream(&stream);
1181
+ */
1182
+ int xd3_encode_stream (xd3_stream *stream,
1183
+ const uint8_t *input,
1184
+ usize_t input_size,
1185
+ uint8_t *output,
1186
+ usize_t *output_size,
1187
+ usize_t avail_output);
1188
+
1189
+ /* The reverse of xd3_encode_stream. */
1190
+ int xd3_decode_stream (xd3_stream *stream,
1191
+ const uint8_t *input,
1192
+ usize_t input_size,
1193
+ uint8_t *output,
1194
+ usize_t *output_size,
1195
+ usize_t avail_size);
1196
+
1197
+ /* This is the non-blocking interface.
1198
+ *
1199
+ * Handling input and output states is the same for encoding or
1200
+ * decoding using the xd3_avail_input() and xd3_consume_output()
1201
+ * routines, inlined below.
1202
+ *
1203
+ * Return values:
1204
+ *
1205
+ * XD3_INPUT: the process requires more input: call
1206
+ * xd3_avail_input() then repeat
1207
+ *
1208
+ * XD3_OUTPUT: the process has more output: read stream->next_out,
1209
+ * stream->avail_out, then call xd3_consume_output(),
1210
+ * then repeat
1211
+ *
1212
+ * XD3_GOTHEADER: (decoder-only) notification returned following the
1213
+ * VCDIFF header and first window header. the decoder
1214
+ * may use the header to configure itself.
1215
+ *
1216
+ * XD3_WINSTART: a general notification returned once for each
1217
+ * window except the 0-th window, which is implied by
1218
+ * XD3_GOTHEADER. It is recommended to use a
1219
+ * switch-stmt such as:
1220
+ *
1221
+ * ...
1222
+ * again:
1223
+ * switch ((ret = xd3_decode_input (stream))) {
1224
+ * case XD3_GOTHEADER: {
1225
+ * assert(stream->current_window == 0);
1226
+ * stuff;
1227
+ * }
1228
+ * // fallthrough
1229
+ * case XD3_WINSTART: {
1230
+ * something(stream->current_window);
1231
+ * goto again;
1232
+ * }
1233
+ * ...
1234
+ *
1235
+ * XD3_WINFINISH: a general notification, following the complete
1236
+ * input & output of a window. at this point,
1237
+ * stream->total_in and stream->total_out are consistent
1238
+ * for either encoding or decoding.
1239
+ *
1240
+ * XD3_GETSRCBLK: If the xd3_getblk() callback is NULL, this value
1241
+ * is returned to initiate a non-blocking source read.
1242
+ */
1243
+ int xd3_decode_input (xd3_stream *stream);
1244
+ int xd3_encode_input (xd3_stream *stream);
1245
+
1246
+ /* The xd3_config structure is used to initialize a stream - all data
1247
+ * is copied into stream so config may be a temporary variable. See
1248
+ * the [documentation] or comments on the xd3_config structure. */
1249
+ int xd3_config_stream (xd3_stream *stream,
1250
+ xd3_config *config);
1251
+
1252
+ /* Since Xdelta3 doesn't open any files, xd3_close_stream is just an
1253
+ * error check that the stream is in a proper state to be closed: this
1254
+ * means the encoder is flushed and the decoder is at a window
1255
+ * boundary. The application is responsible for freeing any of the
1256
+ * resources it supplied. */
1257
+ int xd3_close_stream (xd3_stream *stream);
1258
+
1259
+ /* This arranges for closes the stream to succeed. Does not free the
1260
+ * stream.*/
1261
+ void xd3_abort_stream (xd3_stream *stream);
1262
+
1263
+ /* xd3_free_stream frees all memory allocated for the stream. The
1264
+ * application is responsible for freeing any of the resources it
1265
+ * supplied. */
1266
+ void xd3_free_stream (xd3_stream *stream);
1267
+
1268
+ /* This function informs the encoder or decoder that source matching
1269
+ * (i.e., delta-compression) is possible. For encoding, this should
1270
+ * be called before the first xd3_encode_input. A NULL source is
1271
+ * ignored. For decoding, this should be called before the first
1272
+ * window is decoded, but the appheader may be read first
1273
+ * (XD3_GOTHEADER). After decoding the header, call xd3_set_source()
1274
+ * if you have a source file. Note: if (stream->dec_win_ind & VCD_SOURCE)
1275
+ * is true, it means the first window expects there to be a source file.
1276
+ */
1277
+ int xd3_set_source (xd3_stream *stream,
1278
+ xd3_source *source);
1279
+
1280
+ /* If the source size is known, call this instead of xd3_set_source().
1281
+ * to avoid having stream->getblk called (and/or to avoid XD3_GETSRCBLK).
1282
+ *
1283
+ * Follow these steps:
1284
+ xd3_source source;
1285
+ memset(&source, 0, sizeof(source));
1286
+ source.blksize = size;
1287
+ source.onblk = size;
1288
+ source.curblk = buf;
1289
+ source.curblkno = 0;
1290
+ int ret = xd3_set_source_and_size(&stream, &source, size);
1291
+ ...
1292
+ */
1293
+ int xd3_set_source_and_size (xd3_stream *stream,
1294
+ xd3_source *source,
1295
+ xoff_t source_size);
1296
+
1297
+ /* This should be called before the first call to xd3_encode_input()
1298
+ * to include application-specific data in the VCDIFF header. */
1299
+ void xd3_set_appheader (xd3_stream *stream,
1300
+ const uint8_t *data,
1301
+ usize_t size);
1302
+
1303
+ /* xd3_get_appheader may be called in the decoder after XD3_GOTHEADER.
1304
+ * For convenience, the decoder always adds a single byte padding to
1305
+ * the end of the application header, which is set to zero in case the
1306
+ * application header is a string. */
1307
+ int xd3_get_appheader (xd3_stream *stream,
1308
+ uint8_t **data,
1309
+ usize_t *size);
1310
+
1311
+ /* To generate a VCDIFF encoded delta with xd3_encode_init() from
1312
+ * another format, use:
1313
+ *
1314
+ * xd3_encode_init_partial() -- initialze encoder state (w/o hash tables)
1315
+ * xd3_init_cache() -- reset VCDIFF address cache
1316
+ * xd3_found_match() -- to report a copy instruction
1317
+ *
1318
+ * set stream->enc_state to ENC_INSTR and call xd3_encode_input as usual.
1319
+ */
1320
+ int xd3_encode_init_partial (xd3_stream *stream);
1321
+ void xd3_init_cache (xd3_addr_cache* acache);
1322
+ int xd3_found_match (xd3_stream *stream,
1323
+ usize_t pos, usize_t size,
1324
+ xoff_t addr, int is_source);
1325
+
1326
+ /* Gives an error string for xdelta3-speficic errors, returns NULL for
1327
+ system errors */
1328
+ const char* xd3_strerror (int ret);
1329
+
1330
+ /* For convenience, zero & initialize the xd3_config structure with
1331
+ specified flags. */
1332
+ static inline
1333
+ void xd3_init_config (xd3_config *config,
1334
+ uint32_t flags)
1335
+ {
1336
+ memset (config, 0, sizeof (*config));
1337
+ config->flags = flags;
1338
+ }
1339
+
1340
+ /* This supplies some input to the stream.
1341
+ *
1342
+ * For encoding, if the input is larger than the configured window
1343
+ * size (xd3_config.winsize), the entire input will be consumed and
1344
+ * encoded anyway. If you wish to strictly limit the window size,
1345
+ * limit the buffer passed to xd3_avail_input to the window size.
1346
+ *
1347
+ * For encoding, if the input is smaller than the configured window
1348
+ * size (xd3_config.winsize), the library will create a window-sized
1349
+ * buffer and accumulate input until a full-sized window can be
1350
+ * encoded. XD3_INPUT will be returned. The input must remain valid
1351
+ * until the next time xd3_encode_input() returns XD3_INPUT.
1352
+ *
1353
+ * For decoding, the input will be consumed entirely before XD3_INPUT
1354
+ * is returned again.
1355
+ */
1356
+ static inline
1357
+ void xd3_avail_input (xd3_stream *stream,
1358
+ const uint8_t *idata,
1359
+ usize_t isize)
1360
+ {
1361
+ /* Even if isize is zero, the code expects a non-NULL idata. Why?
1362
+ * It uses this value to determine whether xd3_avail_input has ever
1363
+ * been called. If xd3_encode_input is called before
1364
+ * xd3_avail_input it will return XD3_INPUT right away without
1365
+ * allocating a stream->winsize buffer. This is to avoid an
1366
+ * unwanted allocation. */
1367
+ XD3_ASSERT (idata != NULL || isize == 0);
1368
+
1369
+ stream->next_in = idata;
1370
+ stream->avail_in = isize;
1371
+ }
1372
+
1373
+ /* This acknowledges receipt of output data, must be called after any
1374
+ * XD3_OUTPUT return. */
1375
+ static inline
1376
+ void xd3_consume_output (xd3_stream *stream)
1377
+ {
1378
+ stream->avail_out = 0;
1379
+ }
1380
+
1381
+ /* These are set for each XD3_WINFINISH return. */
1382
+ static inline
1383
+ int xd3_encoder_used_source (xd3_stream *stream) {
1384
+ return stream->src != NULL && stream->src->srclen > 0;
1385
+ }
1386
+ static inline
1387
+ xoff_t xd3_encoder_srcbase (xd3_stream *stream) {
1388
+ return stream->src->srcbase;
1389
+ }
1390
+ static inline
1391
+ usize_t xd3_encoder_srclen (xd3_stream *stream) {
1392
+ return stream->src->srclen;
1393
+ }
1394
+
1395
+ /* Checks for legal flag changes. */
1396
+ static inline
1397
+ void xd3_set_flags (xd3_stream *stream, uint32_t flags)
1398
+ {
1399
+ /* The bitwise difference should contain only XD3_FLUSH or
1400
+ XD3_SKIP_WINDOW */
1401
+ XD3_ASSERT(((flags ^ stream->flags) & ~(XD3_FLUSH | XD3_SKIP_WINDOW)) == 0);
1402
+ stream->flags = flags;
1403
+ }
1404
+
1405
+ /* Gives some extra information about the latest library error, if any
1406
+ * is known. */
1407
+ static inline
1408
+ const char* xd3_errstring (xd3_stream *stream)
1409
+ {
1410
+ return stream->msg ? stream->msg : "";
1411
+ }
1412
+
1413
+
1414
+ /* 64-bit divisions are expensive, which is why we require a
1415
+ * power-of-two source->blksize. To relax this restriction is
1416
+ * relatively easy, see the history for xd3_blksize_div(). */
1417
+ static inline
1418
+ void xd3_blksize_div (const xoff_t offset,
1419
+ const xd3_source *source,
1420
+ xoff_t *blkno,
1421
+ usize_t *blkoff) {
1422
+ *blkno = offset >> source->shiftby;
1423
+ *blkoff = offset & source->maskby;
1424
+ XD3_ASSERT (*blkoff < source->blksize);
1425
+ }
1426
+
1427
+ static inline
1428
+ void xd3_blksize_add (xoff_t *blkno,
1429
+ usize_t *blkoff,
1430
+ const xd3_source *source,
1431
+ const usize_t add)
1432
+ {
1433
+ usize_t blkdiff;
1434
+
1435
+ /* Does not check for overflow, checked in xdelta3-decode.h. */
1436
+ *blkoff += add;
1437
+ blkdiff = *blkoff >> source->shiftby;
1438
+
1439
+ if (blkdiff)
1440
+ {
1441
+ *blkno += blkdiff;
1442
+ *blkoff &= source->maskby;
1443
+ }
1444
+
1445
+ XD3_ASSERT (*blkoff < source->blksize);
1446
+ }
1447
+
1448
+ #ifdef __cplusplus
1449
+ } // extern "C"
1450
+ #endif
1451
+
1452
+ #define XD3_NOOP 0U
1453
+ #define XD3_ADD 1U
1454
+ #define XD3_RUN 2U
1455
+ #define XD3_CPY 3U /* XD3_CPY rtypes are represented as (XD3_CPY +
1456
+ * copy-mode value) */
1457
+
1458
+ #if XD3_DEBUG
1459
+ #define IF_DEBUG(x) x
1460
+ #else
1461
+ #define IF_DEBUG(x)
1462
+ #endif
1463
+ #if XD3_DEBUG > 1
1464
+ #define IF_DEBUG1(x) x
1465
+ #else
1466
+ #define IF_DEBUG1(x)
1467
+ #endif
1468
+ #if XD3_DEBUG > 2
1469
+ #define IF_DEBUG2(x) x
1470
+ #else
1471
+ #define IF_DEBUG2(x)
1472
+ #endif
1473
+
1474
+ #define SIZEOF_ARRAY(x) (sizeof(x) / sizeof(x[0]))
1475
+
1476
+ #endif /* _XDELTA3_H_ */