@gfean/react-native-bundle-drop 0.3.9 → 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 (79) 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 +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 +184 -41
  60. package/lib/scripts/download-bundle.js +18 -7
  61. package/lib/scripts/upload-bundle.js +25 -28
  62. package/package.json +43 -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
  78. package/lib/assetExtensions.d.ts +0 -4
  79. package/lib/assetExtensions.js +0 -35
@@ -0,0 +1,1219 @@
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
+ #ifndef _XDELTA3_DECODE_H_
18
+ #define _XDELTA3_DECODE_H_
19
+
20
+ #include "xdelta3-internal.h"
21
+
22
+ #define SRCORTGT(x) ((((x) & VCD_SRCORTGT) == VCD_SOURCE) ? \
23
+ VCD_SOURCE : ((((x) & VCD_SRCORTGT) == \
24
+ VCD_TARGET) ? VCD_TARGET : 0))
25
+
26
+ static inline int
27
+ xd3_decode_byte (xd3_stream *stream, usize_t *val)
28
+ {
29
+ if (stream->avail_in == 0)
30
+ {
31
+ stream->msg = "further input required";
32
+ return XD3_INPUT;
33
+ }
34
+
35
+ (*val) = stream->next_in[0];
36
+
37
+ DECODE_INPUT (1);
38
+ return 0;
39
+ }
40
+
41
+ static inline int
42
+ xd3_decode_bytes (xd3_stream *stream, uint8_t *buf, usize_t *pos, usize_t size)
43
+ {
44
+ usize_t want;
45
+ usize_t take;
46
+
47
+ /* Note: The case where (*pos == size) happens when a zero-length
48
+ * appheader or code table is transmitted, but there is nothing in
49
+ * the standard against that. */
50
+ while (*pos < size)
51
+ {
52
+ if (stream->avail_in == 0)
53
+ {
54
+ stream->msg = "further input required";
55
+ return XD3_INPUT;
56
+ }
57
+
58
+ want = size - *pos;
59
+ take = xd3_min (want, stream->avail_in);
60
+
61
+ memcpy (buf + *pos, stream->next_in, (size_t) take);
62
+
63
+ DECODE_INPUT (take);
64
+ (*pos) += take;
65
+ }
66
+
67
+ return 0;
68
+ }
69
+
70
+ /* Initialize the decoder for a new window. The dec_tgtlen value is
71
+ * preserved across successive window decodings, and the update to
72
+ * dec_winstart is delayed until a new window actually starts. This
73
+ * is to avoid throwing an error due to overflow until the last
74
+ * possible moment. This makes it possible to encode exactly 4GB
75
+ * through a 32-bit encoder. */
76
+ static int
77
+ xd3_decode_init_window (xd3_stream *stream)
78
+ {
79
+ stream->dec_cpylen = 0;
80
+ stream->dec_cpyoff = 0;
81
+ stream->dec_cksumbytes = 0;
82
+
83
+ xd3_init_cache (& stream->acache);
84
+
85
+ return 0;
86
+ }
87
+
88
+ /* Allocates buffer space for the target window and possibly the
89
+ * VCD_TARGET copy-window. Also sets the base of the two copy
90
+ * segments. */
91
+ static int
92
+ xd3_decode_setup_buffers (xd3_stream *stream)
93
+ {
94
+ /* If VCD_TARGET is set then the previous buffer may be reused. */
95
+ if (stream->dec_win_ind & VCD_TARGET)
96
+ {
97
+ /* Note: this implementation is untested, since Xdelta3 itself
98
+ * does not implement an encoder for VCD_TARGET mode. Thus, mark
99
+ * unimplemented until needed. */
100
+ if (1)
101
+ {
102
+ stream->msg = "VCD_TARGET not implemented";
103
+ return XD3_UNIMPLEMENTED;
104
+ }
105
+
106
+ /* But this implementation only supports copying from the last
107
+ * target window. If the offset is outside that range, it can't
108
+ * be done. */
109
+ if (stream->dec_cpyoff < stream->dec_laststart)
110
+ {
111
+ stream->msg = "unsupported VCD_TARGET offset";
112
+ return XD3_INVALID_INPUT;
113
+ }
114
+
115
+ /* See if the two windows are the same. This indicates the
116
+ * first time VCD_TARGET is used. This causes a second buffer
117
+ * to be allocated, after that the two are swapped in the
118
+ * DEC_FINISH case. */
119
+ if (stream->dec_lastwin == stream->next_out)
120
+ {
121
+ stream->next_out = NULL;
122
+ stream->space_out = 0;
123
+ }
124
+
125
+ /* TODO: (See note above, this looks incorrect) */
126
+ stream->dec_cpyaddrbase = stream->dec_lastwin +
127
+ (usize_t) (stream->dec_cpyoff - stream->dec_laststart);
128
+ }
129
+
130
+ /* See if the current output window is large enough. */
131
+ if (stream->space_out < stream->dec_tgtlen)
132
+ {
133
+ xd3_free (stream, stream->dec_buffer);
134
+
135
+ stream->space_out =
136
+ xd3_round_blksize (stream->dec_tgtlen, XD3_ALLOCSIZE);
137
+
138
+ if ((stream->dec_buffer =
139
+ (uint8_t*) xd3_alloc (stream, stream->space_out, 1)) == NULL)
140
+ {
141
+ return ENOMEM;
142
+ }
143
+
144
+ stream->next_out = stream->dec_buffer;
145
+ }
146
+
147
+ /* dec_tgtaddrbase refers to an invalid base address, but it is
148
+ * always used with a sufficiently large instruction offset (i.e.,
149
+ * beyond the copy window). This condition is enforced by
150
+ * xd3_decode_output_halfinst. */
151
+ stream->dec_tgtaddrbase = stream->next_out - stream->dec_cpylen;
152
+
153
+ return 0;
154
+ }
155
+
156
+ static int
157
+ xd3_decode_allocate (xd3_stream *stream,
158
+ usize_t size,
159
+ uint8_t **buf_ptr,
160
+ usize_t *buf_alloc)
161
+ {
162
+ IF_DEBUG2 (DP(RINT "[xd3_decode_allocate] size %"W"u alloc %"W"u\n",
163
+ size, *buf_alloc));
164
+
165
+ if (*buf_ptr != NULL && *buf_alloc < size)
166
+ {
167
+ xd3_free (stream, *buf_ptr);
168
+ *buf_ptr = NULL;
169
+ }
170
+
171
+ if (*buf_ptr == NULL)
172
+ {
173
+ *buf_alloc = xd3_round_blksize (size, XD3_ALLOCSIZE);
174
+
175
+ if ((*buf_ptr = (uint8_t*) xd3_alloc (stream, *buf_alloc, 1)) == NULL)
176
+ {
177
+ return ENOMEM;
178
+ }
179
+ }
180
+
181
+ return 0;
182
+ }
183
+
184
+ static int
185
+ xd3_decode_section (xd3_stream *stream,
186
+ xd3_desect *section,
187
+ xd3_decode_state nstate,
188
+ int copy)
189
+ {
190
+ XD3_ASSERT (section->pos <= section->size);
191
+ XD3_ASSERT (stream->dec_state != nstate);
192
+
193
+ if (section->pos < section->size)
194
+ {
195
+ usize_t sect_take;
196
+
197
+ if (stream->avail_in == 0)
198
+ {
199
+ return XD3_INPUT;
200
+ }
201
+
202
+ if ((copy == 0) && (section->pos == 0))
203
+ {
204
+ /* No allocation/copy needed */
205
+ section->buf = stream->next_in;
206
+ sect_take = section->size;
207
+ IF_DEBUG1 (DP(RINT "[xd3_decode_section] zerocopy %"W"u @ %"W"u avail %"W"u\n",
208
+ sect_take, section->pos, stream->avail_in));
209
+ }
210
+ else
211
+ {
212
+ usize_t sect_need = section->size - section->pos;
213
+
214
+ /* Allocate and copy */
215
+ sect_take = xd3_min (sect_need, stream->avail_in);
216
+
217
+ if (section->pos == 0)
218
+ {
219
+ int ret;
220
+
221
+ if ((ret = xd3_decode_allocate (stream,
222
+ section->size,
223
+ & section->copied1,
224
+ & section->alloc1)))
225
+ {
226
+ return ret;
227
+ }
228
+
229
+ section->buf = section->copied1;
230
+ }
231
+
232
+ IF_DEBUG2 (DP(RINT "[xd3_decode_section] take %"W"u @ %"W"u [need %"W"u] avail %"W"u\n",
233
+ sect_take, section->pos, sect_need, stream->avail_in));
234
+ XD3_ASSERT (section->pos + sect_take <= section->alloc1);
235
+
236
+ memcpy (section->copied1 + section->pos,
237
+ stream->next_in,
238
+ sect_take);
239
+ }
240
+
241
+ section->pos += sect_take;
242
+
243
+ stream->dec_winbytes += sect_take;
244
+
245
+ DECODE_INPUT (sect_take);
246
+ }
247
+
248
+ if (section->pos < section->size)
249
+ {
250
+ IF_DEBUG1 (DP(RINT "[xd3_decode_section] further input required %"W"u\n",
251
+ section->size - section->pos));
252
+ stream->msg = "further input required";
253
+ return XD3_INPUT;
254
+ }
255
+
256
+ XD3_ASSERT (section->pos == section->size);
257
+
258
+ stream->dec_state = nstate;
259
+ section->buf_max = section->buf + section->size;
260
+ section->pos = 0;
261
+ return 0;
262
+ }
263
+
264
+ /* Decode the size and address for half of an instruction (i.e., a
265
+ * single opcode). This updates the stream->dec_position, which are
266
+ * bytes already output prior to processing this instruction. Perform
267
+ * bounds checking for sizes and copy addresses, which uses the
268
+ * dec_position (which is why these checks are done here). */
269
+ static int
270
+ xd3_decode_parse_halfinst (xd3_stream *stream, xd3_hinst *inst)
271
+ {
272
+ int ret;
273
+
274
+ /* If the size from the instruction table is zero then read a size value. */
275
+ if ((inst->size == 0) &&
276
+ (ret = xd3_read_size (stream,
277
+ & stream->inst_sect.buf,
278
+ stream->inst_sect.buf_max,
279
+ & inst->size)))
280
+ {
281
+ return XD3_INVALID_INPUT;
282
+ }
283
+
284
+ /* For copy instructions, read address. */
285
+ if (inst->type >= XD3_CPY)
286
+ {
287
+ IF_DEBUG2 ({
288
+ static int cnt = 0;
289
+ XPR(NT "DECODE:%u: COPY at %"Q"u (winoffset %"W"u) "
290
+ "size %"W"u winaddr %"W"u\n",
291
+ cnt++,
292
+ stream->total_out + (stream->dec_position -
293
+ stream->dec_cpylen),
294
+ (stream->dec_position - stream->dec_cpylen),
295
+ inst->size,
296
+ inst->addr);
297
+ });
298
+
299
+ if ((ret = xd3_decode_address (stream,
300
+ stream->dec_position,
301
+ inst->type - XD3_CPY,
302
+ & stream->addr_sect.buf,
303
+ stream->addr_sect.buf_max,
304
+ & inst->addr)))
305
+ {
306
+ return ret;
307
+ }
308
+
309
+ /* Cannot copy an address before it is filled-in. */
310
+ if (inst->addr >= stream->dec_position)
311
+ {
312
+ stream->msg = "address too large";
313
+ return XD3_INVALID_INPUT;
314
+ }
315
+
316
+ /* Check: a VCD_TARGET or VCD_SOURCE copy cannot exceed the remaining
317
+ * buffer space in its own segment. */
318
+ if (inst->addr < stream->dec_cpylen &&
319
+ inst->addr + inst->size > stream->dec_cpylen)
320
+ {
321
+ stream->msg = "size too large";
322
+ return XD3_INVALID_INPUT;
323
+ }
324
+ }
325
+ else
326
+ {
327
+ IF_DEBUG2 ({
328
+ if (inst->type == XD3_ADD)
329
+ {
330
+ static int cnt;
331
+ XPR(NT "DECODE:%d: ADD at %"Q"u (winoffset %"W"u) size %"W"u\n",
332
+ cnt++,
333
+ (stream->total_out + stream->dec_position - stream->dec_cpylen),
334
+ stream->dec_position - stream->dec_cpylen,
335
+ inst->size);
336
+ }
337
+ else
338
+ {
339
+ static int cnt;
340
+ XD3_ASSERT (inst->type == XD3_RUN);
341
+ XPR(NT "DECODE:%d: RUN at %"Q"u (winoffset %"W"u) size %"W"u\n",
342
+ cnt++,
343
+ stream->total_out + stream->dec_position - stream->dec_cpylen,
344
+ stream->dec_position - stream->dec_cpylen,
345
+ inst->size);
346
+ }
347
+ });
348
+ }
349
+
350
+ /* Check: The instruction will not overflow the output buffer. */
351
+ if (stream->dec_position + inst->size > stream->dec_maxpos)
352
+ {
353
+ stream->msg = "size too large";
354
+ return XD3_INVALID_INPUT;
355
+ }
356
+
357
+ stream->dec_position += inst->size;
358
+ return 0;
359
+ }
360
+
361
+ /* Decode a single opcode and then decode the two half-instructions. */
362
+ static int
363
+ xd3_decode_instruction (xd3_stream *stream)
364
+ {
365
+ int ret;
366
+ const xd3_dinst *inst;
367
+
368
+ if (stream->inst_sect.buf == stream->inst_sect.buf_max)
369
+ {
370
+ stream->msg = "instruction underflow";
371
+ return XD3_INVALID_INPUT;
372
+ }
373
+
374
+ inst = &stream->code_table[*stream->inst_sect.buf++];
375
+
376
+ stream->dec_current1.type = inst->type1;
377
+ stream->dec_current2.type = inst->type2;
378
+ stream->dec_current1.size = inst->size1;
379
+ stream->dec_current2.size = inst->size2;
380
+
381
+ /* For each instruction with a real operation, decode the
382
+ * corresponding size and addresses if necessary. Assume a
383
+ * code-table may have NOOP in either position, although this is
384
+ * unlikely. */
385
+ if (inst->type1 != XD3_NOOP &&
386
+ (ret = xd3_decode_parse_halfinst (stream, & stream->dec_current1)))
387
+ {
388
+ return ret;
389
+ }
390
+ if (inst->type2 != XD3_NOOP &&
391
+ (ret = xd3_decode_parse_halfinst (stream, & stream->dec_current2)))
392
+ {
393
+ return ret;
394
+ }
395
+ return 0;
396
+ }
397
+
398
+ /* Output the result of a single half-instruction. OPT: This the
399
+ decoder hotspot. Modifies "hinst", see below. */
400
+ static int
401
+ xd3_decode_output_halfinst (xd3_stream *stream, xd3_hinst *inst)
402
+ {
403
+ /* This method is reentrant for copy instructions which may return
404
+ * XD3_GETSRCBLK to the caller. Each time through a copy takes the
405
+ * minimum of inst->size and the available space on whichever block
406
+ * supplies the data */
407
+ usize_t take = inst->size;
408
+
409
+ if (USIZE_T_OVERFLOW (stream->avail_out, take) ||
410
+ stream->avail_out + take > stream->space_out)
411
+ {
412
+ stream->msg = "overflow while decoding";
413
+ return XD3_INVALID_INPUT;
414
+ }
415
+
416
+ XD3_ASSERT (inst->type != XD3_NOOP);
417
+
418
+ switch (inst->type)
419
+ {
420
+ case XD3_RUN:
421
+ {
422
+ /* Only require a single data byte. */
423
+ if (stream->data_sect.buf == stream->data_sect.buf_max)
424
+ {
425
+ stream->msg = "data underflow";
426
+ return XD3_INVALID_INPUT;
427
+ }
428
+
429
+ memset (stream->next_out + stream->avail_out,
430
+ stream->data_sect.buf[0],
431
+ take);
432
+
433
+ stream->data_sect.buf += 1;
434
+ stream->avail_out += take;
435
+ inst->type = XD3_NOOP;
436
+ break;
437
+ }
438
+ case XD3_ADD:
439
+ {
440
+ /* Require at least TAKE data bytes. */
441
+ if (stream->data_sect.buf + take > stream->data_sect.buf_max)
442
+ {
443
+ stream->msg = "data underflow";
444
+ return XD3_INVALID_INPUT;
445
+ }
446
+
447
+ memcpy (stream->next_out + stream->avail_out,
448
+ stream->data_sect.buf,
449
+ take);
450
+
451
+ stream->data_sect.buf += take;
452
+ stream->avail_out += take;
453
+ inst->type = XD3_NOOP;
454
+ break;
455
+ }
456
+ default:
457
+ {
458
+ usize_t i;
459
+ const uint8_t *src;
460
+ uint8_t *dst;
461
+ int overlap;
462
+
463
+ /* See if it copies from the VCD_TARGET/VCD_SOURCE window or
464
+ * the target window. Out-of-bounds checks for the addresses
465
+ * and sizes are performed in xd3_decode_parse_halfinst. This
466
+ * if/else must set "overlap", "src", and "dst". */
467
+ if (inst->addr < stream->dec_cpylen)
468
+ {
469
+ /* In both branches we are copying from outside the
470
+ * current decoder window, the first (VCD_TARGET) is
471
+ * unimplemented. */
472
+ overlap = 0;
473
+
474
+ /* This branch sets "src". As a side-effect, we modify
475
+ * "inst" so that if we reenter this method after a
476
+ * XD3_GETSRCBLK response the state is correct. So if the
477
+ * instruction can be fulfilled by a contiguous block of
478
+ * memory then we will set:
479
+ *
480
+ * inst->type = XD3_NOOP;
481
+ * inst->size = 0;
482
+ */
483
+ if (stream->dec_win_ind & VCD_TARGET)
484
+ {
485
+ /* TODO: Users have requested long-distance copies of
486
+ * similar material within a target (e.g., for dup
487
+ * supression in backups). This code path is probably
488
+ * dead due to XD3_UNIMPLEMENTED in xd3_decode_setup_buffers */
489
+ inst->size = 0;
490
+ inst->type = XD3_NOOP;
491
+ stream->msg = "VCD_TARGET not implemented";
492
+ return XD3_UNIMPLEMENTED;
493
+ }
494
+ else
495
+ {
496
+ /* In this case we have to read a source block, which
497
+ * could return control to the caller. We need to
498
+ * know the first block number needed for this
499
+ * copy. */
500
+ xd3_source *source = stream->src;
501
+ xoff_t block = source->cpyoff_blocks;
502
+ usize_t blkoff = source->cpyoff_blkoff;
503
+ const usize_t blksize = source->blksize;
504
+ int ret;
505
+
506
+ xd3_blksize_add (&block, &blkoff, source, inst->addr);
507
+ XD3_ASSERT (blkoff < blksize);
508
+
509
+ if ((ret = xd3_getblk (stream, block)))
510
+ {
511
+ /* could be a XD3_GETSRCBLK failure. */
512
+ if (ret == XD3_TOOFARBACK)
513
+ {
514
+ stream->msg = "non-seekable source in decode";
515
+ ret = XD3_INTERNAL;
516
+ }
517
+ return ret;
518
+ }
519
+
520
+ src = source->curblk + blkoff;
521
+
522
+ /* This block is either full, or a partial block that
523
+ * must contain enough bytes. */
524
+ if ((source->onblk != blksize) &&
525
+ (blkoff + take > source->onblk))
526
+ {
527
+ IF_DEBUG1 (XPR(NT "[srcfile] short at blkno %"Q"u onblk "
528
+ "%"W"u blksize %"W"u blkoff %"W"u take %"W"u\n",
529
+ block,
530
+ source->onblk,
531
+ blksize,
532
+ blkoff,
533
+ take));
534
+ stream->msg = "source file too short";
535
+ return XD3_INVALID_INPUT;
536
+ }
537
+
538
+ XD3_ASSERT (blkoff != blksize);
539
+
540
+ /* Check if we have enough data on this block to
541
+ * finish the instruction. */
542
+ if (blkoff + take <= blksize)
543
+ {
544
+ inst->type = XD3_NOOP;
545
+ inst->size = 0;
546
+ }
547
+ else
548
+ {
549
+ take = blksize - blkoff;
550
+ inst->size -= take;
551
+ inst->addr += take;
552
+
553
+ /* because (blkoff + take > blksize), above */
554
+ XD3_ASSERT (inst->size != 0);
555
+ }
556
+ }
557
+ }
558
+ else
559
+ {
560
+ /* TODO: the memcpy/overlap optimization, etc. Overlap
561
+ * here could be more specific, it's whether (inst->addr -
562
+ * srclen) + inst->size > input_pos ? And is the system
563
+ * memcpy really any good? */
564
+ overlap = 1;
565
+
566
+ /* For a target-window copy, we know the entire range is
567
+ * in-memory. The dec_tgtaddrbase is negatively offset by
568
+ * dec_cpylen because the addresses start beyond that
569
+ * point. */
570
+ src = stream->dec_tgtaddrbase + inst->addr;
571
+ inst->type = XD3_NOOP;
572
+ inst->size = 0;
573
+ }
574
+
575
+ dst = stream->next_out + stream->avail_out;
576
+
577
+ stream->avail_out += take;
578
+
579
+ if (overlap)
580
+ {
581
+ /* Can't just memcpy here due to possible overlap. */
582
+ for (i = take; i != 0; i -= 1)
583
+ {
584
+ *dst++ = *src++;
585
+ }
586
+ }
587
+ else
588
+ {
589
+ memcpy (dst, src, take);
590
+ }
591
+ }
592
+ }
593
+
594
+ return 0;
595
+ }
596
+
597
+ static int
598
+ xd3_decode_finish_window (xd3_stream *stream)
599
+ {
600
+ stream->dec_winbytes = 0;
601
+ stream->dec_state = DEC_FINISH;
602
+
603
+ stream->data_sect.pos = 0;
604
+ stream->inst_sect.pos = 0;
605
+ stream->addr_sect.pos = 0;
606
+
607
+ return XD3_OUTPUT;
608
+ }
609
+
610
+ static int
611
+ xd3_decode_secondary_sections (xd3_stream *secondary_stream)
612
+ {
613
+ #if SECONDARY_ANY
614
+ int ret;
615
+ #define DECODE_SECONDARY_SECTION(UPPER,LOWER) \
616
+ ((secondary_stream->dec_del_ind & VCD_ ## UPPER ## COMP) && \
617
+ (ret = xd3_decode_secondary (secondary_stream, \
618
+ & secondary_stream-> LOWER ## _sect, \
619
+ & xd3_sec_ ## LOWER (secondary_stream))))
620
+
621
+ if (DECODE_SECONDARY_SECTION (DATA, data) ||
622
+ DECODE_SECONDARY_SECTION (INST, inst) ||
623
+ DECODE_SECONDARY_SECTION (ADDR, addr))
624
+ {
625
+ return ret;
626
+ }
627
+ #undef DECODE_SECONDARY_SECTION
628
+ #endif
629
+ return 0;
630
+ }
631
+
632
+ static int
633
+ xd3_decode_sections (xd3_stream *stream)
634
+ {
635
+ usize_t need, more, take;
636
+ int copy, ret;
637
+
638
+ if ((stream->flags & XD3_JUST_HDR) != 0)
639
+ {
640
+ /* Nothing left to do. */
641
+ return xd3_decode_finish_window (stream);
642
+ }
643
+
644
+ /* To avoid extra copying, allocate three sections at once (but
645
+ * check for overflow). */
646
+ need = stream->inst_sect.size;
647
+
648
+ if (USIZE_T_OVERFLOW (need, stream->addr_sect.size))
649
+ {
650
+ stream->msg = "decoder section size overflow";
651
+ return XD3_INTERNAL;
652
+ }
653
+ need += stream->addr_sect.size;
654
+
655
+ if (USIZE_T_OVERFLOW (need, stream->data_sect.size))
656
+ {
657
+ stream->msg = "decoder section size overflow";
658
+ return XD3_INTERNAL;
659
+ }
660
+ need += stream->data_sect.size;
661
+
662
+ /* The window may be entirely processed. */
663
+ XD3_ASSERT (stream->dec_winbytes <= need);
664
+
665
+ /* Compute how much more input is needed. */
666
+ more = (need - stream->dec_winbytes);
667
+
668
+ /* How much to consume. */
669
+ take = xd3_min (more, stream->avail_in);
670
+
671
+ /* See if the input is completely available, to avoid copy. */
672
+ copy = (take != more);
673
+
674
+ /* If the window is skipped... */
675
+ if ((stream->flags & XD3_SKIP_WINDOW) != 0)
676
+ {
677
+ /* Skip the available input. */
678
+ DECODE_INPUT (take);
679
+
680
+ stream->dec_winbytes += take;
681
+
682
+ if (copy)
683
+ {
684
+ stream->msg = "further input required";
685
+ return XD3_INPUT;
686
+ }
687
+
688
+ return xd3_decode_finish_window (stream);
689
+ }
690
+
691
+ /* Process all but the DATA section. */
692
+ switch (stream->dec_state)
693
+ {
694
+ default:
695
+ stream->msg = "internal error";
696
+ return XD3_INVALID_INPUT;
697
+
698
+ case DEC_DATA:
699
+ if ((ret = xd3_decode_section (stream, & stream->data_sect,
700
+ DEC_INST, copy))) { return ret; }
701
+ case DEC_INST:
702
+ if ((ret = xd3_decode_section (stream, & stream->inst_sect,
703
+ DEC_ADDR, copy))) { return ret; }
704
+ case DEC_ADDR:
705
+ if ((ret = xd3_decode_section (stream, & stream->addr_sect,
706
+ DEC_EMIT, copy))) { return ret; }
707
+ }
708
+
709
+ XD3_ASSERT (stream->dec_winbytes == need);
710
+
711
+ if ((ret = xd3_decode_secondary_sections (stream))) { return ret; }
712
+
713
+ if (stream->flags & XD3_SKIP_EMIT)
714
+ {
715
+ return xd3_decode_finish_window (stream);
716
+ }
717
+
718
+ /* OPT: A possible optimization is to avoid allocating memory in
719
+ * decode_setup_buffers and to avoid a large memcpy when the window
720
+ * consists of a single VCD_SOURCE copy instruction. */
721
+ if ((ret = xd3_decode_setup_buffers (stream))) { return ret; }
722
+
723
+ return 0;
724
+ }
725
+
726
+ static int
727
+ xd3_decode_emit (xd3_stream *stream)
728
+ {
729
+ int ret;
730
+
731
+ /* Produce output: originally structured to allow reentrant code
732
+ * that fills as much of the output buffer as possible, but VCDIFF
733
+ * semantics allows to copy from anywhere from the target window, so
734
+ * instead allocate a sufficiently sized buffer after the target
735
+ * window length is decoded.
736
+ *
737
+ * This code still needs to be reentrant to allow XD3_GETSRCBLK to
738
+ * return control. This is handled by setting the
739
+ * stream->dec_currentN instruction types to XD3_NOOP after they
740
+ * have been processed. */
741
+ XD3_ASSERT (! (stream->flags & XD3_SKIP_EMIT));
742
+ XD3_ASSERT (stream->dec_tgtlen <= stream->space_out);
743
+
744
+ while (stream->inst_sect.buf != stream->inst_sect.buf_max ||
745
+ stream->dec_current1.type != XD3_NOOP ||
746
+ stream->dec_current2.type != XD3_NOOP)
747
+ {
748
+ /* Decode next instruction pair. */
749
+ if ((stream->dec_current1.type == XD3_NOOP) &&
750
+ (stream->dec_current2.type == XD3_NOOP) &&
751
+ (ret = xd3_decode_instruction (stream))) { return ret; }
752
+
753
+ /* Output dec_current1 */
754
+ while ((stream->dec_current1.type != XD3_NOOP))
755
+ {
756
+ if ((ret = xd3_decode_output_halfinst (stream, & stream->dec_current1)))
757
+ {
758
+ return ret;
759
+ }
760
+ }
761
+ /* Output dec_current2 */
762
+ while (stream->dec_current2.type != XD3_NOOP)
763
+ {
764
+ if ((ret = xd3_decode_output_halfinst (stream, & stream->dec_current2)))
765
+ {
766
+ return ret;
767
+ }
768
+ }
769
+ }
770
+
771
+ if (stream->avail_out != stream->dec_tgtlen)
772
+ {
773
+ IF_DEBUG2 (DP(RINT "AVAIL_OUT(%"W"u) != DEC_TGTLEN(%"W"u)\n",
774
+ stream->avail_out, stream->dec_tgtlen));
775
+ stream->msg = "wrong window length";
776
+ return XD3_INVALID_INPUT;
777
+ }
778
+
779
+ if (stream->data_sect.buf != stream->data_sect.buf_max)
780
+ {
781
+ stream->msg = "extra data section";
782
+ return XD3_INVALID_INPUT;
783
+ }
784
+
785
+ if (stream->addr_sect.buf != stream->addr_sect.buf_max)
786
+ {
787
+ stream->msg = "extra address section";
788
+ return XD3_INVALID_INPUT;
789
+ }
790
+
791
+ /* OPT: Should cksum computation be combined with the above loop? */
792
+ if ((stream->dec_win_ind & VCD_ADLER32) != 0 &&
793
+ (stream->flags & XD3_ADLER32_NOVER) == 0)
794
+ {
795
+ uint32_t a32 = adler32 (1L, stream->next_out, stream->avail_out);
796
+
797
+ if (a32 != stream->dec_adler32)
798
+ {
799
+ stream->msg = "target window checksum mismatch";
800
+ return XD3_INVALID_INPUT;
801
+ }
802
+ }
803
+
804
+ /* Finished with a window. */
805
+ return xd3_decode_finish_window (stream);
806
+ }
807
+
808
+ int
809
+ xd3_decode_input (xd3_stream *stream)
810
+ {
811
+ int ret;
812
+
813
+ if (stream->enc_state != 0)
814
+ {
815
+ stream->msg = "encoder/decoder transition";
816
+ return XD3_INVALID_INPUT;
817
+ }
818
+
819
+ #define BYTE_CASE(expr,x,nstate) \
820
+ do { \
821
+ if ( (expr) && \
822
+ ((ret = xd3_decode_byte (stream, & (x))) != 0) ) { return ret; } \
823
+ stream->dec_state = (nstate); \
824
+ } while (0)
825
+
826
+ #define OFFSET_CASE(expr,x,nstate) \
827
+ do { \
828
+ if ( (expr) && \
829
+ ((ret = xd3_decode_offset (stream, & (x))) != 0) ) { return ret; } \
830
+ stream->dec_state = (nstate); \
831
+ } while (0)
832
+
833
+ #define SIZE_CASE(expr,x,nstate) \
834
+ do { \
835
+ if ( (expr) && \
836
+ ((ret = xd3_decode_size (stream, & (x))) != 0) ) { return ret; } \
837
+ stream->dec_state = (nstate); \
838
+ } while (0)
839
+
840
+ switch (stream->dec_state)
841
+ {
842
+ case DEC_VCHEAD:
843
+ {
844
+ if ((ret = xd3_decode_bytes (stream, stream->dec_magic,
845
+ & stream->dec_magicbytes, 4)))
846
+ {
847
+ return ret;
848
+ }
849
+
850
+ if (stream->dec_magic[0] != VCDIFF_MAGIC1 ||
851
+ stream->dec_magic[1] != VCDIFF_MAGIC2 ||
852
+ stream->dec_magic[2] != VCDIFF_MAGIC3)
853
+ {
854
+ stream->msg = "not a VCDIFF input";
855
+ return XD3_INVALID_INPUT;
856
+ }
857
+
858
+ if (stream->dec_magic[3] != 0)
859
+ {
860
+ stream->msg = "VCDIFF input version > 0 is not supported";
861
+ return XD3_INVALID_INPUT;
862
+ }
863
+
864
+ stream->dec_state = DEC_HDRIND;
865
+ }
866
+ case DEC_HDRIND:
867
+ {
868
+ if ((ret = xd3_decode_byte (stream, & stream->dec_hdr_ind)))
869
+ {
870
+ return ret;
871
+ }
872
+
873
+ if ((stream->dec_hdr_ind & VCD_INVHDR) != 0)
874
+ {
875
+ stream->msg = "unrecognized header indicator bits set";
876
+ return XD3_INVALID_INPUT;
877
+ }
878
+
879
+ stream->dec_state = DEC_SECONDID;
880
+ }
881
+
882
+ case DEC_SECONDID:
883
+ /* Secondary compressor ID: only if VCD_SECONDARY is set */
884
+ if ((stream->dec_hdr_ind & VCD_SECONDARY) != 0)
885
+ {
886
+ BYTE_CASE (1, stream->dec_secondid, DEC_TABLEN);
887
+
888
+ switch (stream->dec_secondid)
889
+ {
890
+ case VCD_FGK_ID:
891
+ FGK_CASE (stream);
892
+ case VCD_DJW_ID:
893
+ DJW_CASE (stream);
894
+ case VCD_LZMA_ID:
895
+ LZMA_CASE (stream);
896
+ default:
897
+ stream->msg = "unknown secondary compressor ID";
898
+ return XD3_INVALID_INPUT;
899
+ }
900
+ }
901
+
902
+ case DEC_TABLEN:
903
+ /* Length of code table data: only if VCD_CODETABLE is set */
904
+ SIZE_CASE ((stream->dec_hdr_ind & VCD_CODETABLE) != 0,
905
+ stream->dec_codetblsz, DEC_NEAR);
906
+
907
+ /* The codetblsz counts the two NEAR/SAME bytes */
908
+ if ((stream->dec_hdr_ind & VCD_CODETABLE) != 0) {
909
+ if (stream->dec_codetblsz <= 2) {
910
+ stream->msg = "invalid code table size";
911
+ return ENOMEM;
912
+ }
913
+ stream->dec_codetblsz -= 2;
914
+ }
915
+ case DEC_NEAR:
916
+ /* Near modes: only if VCD_CODETABLE is set */
917
+ BYTE_CASE((stream->dec_hdr_ind & VCD_CODETABLE) != 0,
918
+ stream->acache.s_near, DEC_SAME);
919
+ case DEC_SAME:
920
+ /* Same modes: only if VCD_CODETABLE is set */
921
+ BYTE_CASE((stream->dec_hdr_ind & VCD_CODETABLE) != 0,
922
+ stream->acache.s_same, DEC_TABDAT);
923
+ case DEC_TABDAT:
924
+ /* Compressed code table data */
925
+
926
+ if ((stream->dec_hdr_ind & VCD_CODETABLE) != 0)
927
+ {
928
+ stream->msg = "VCD_CODETABLE support was removed";
929
+ return XD3_UNIMPLEMENTED;
930
+ }
931
+ else
932
+ {
933
+ /* Use the default table. */
934
+ stream->acache.s_near = __rfc3284_code_table_desc.near_modes;
935
+ stream->acache.s_same = __rfc3284_code_table_desc.same_modes;
936
+ stream->code_table = xd3_rfc3284_code_table ();
937
+ }
938
+
939
+ if ((ret = xd3_alloc_cache (stream))) { return ret; }
940
+
941
+ stream->dec_state = DEC_APPLEN;
942
+
943
+ case DEC_APPLEN:
944
+ /* Length of application data */
945
+ SIZE_CASE((stream->dec_hdr_ind & VCD_APPHEADER) != 0,
946
+ stream->dec_appheadsz, DEC_APPDAT);
947
+
948
+ case DEC_APPDAT:
949
+ /* Application data */
950
+ if (stream->dec_hdr_ind & VCD_APPHEADER)
951
+ {
952
+ /* Note: we add an additional byte for padding, to allow
953
+ 0-termination. Check for overflow: */
954
+ if (USIZE_T_OVERFLOW(stream->dec_appheadsz, 1))
955
+ {
956
+ stream->msg = "exceptional appheader size";
957
+ return XD3_INVALID_INPUT;
958
+ }
959
+
960
+ if ((stream->dec_appheader == NULL) &&
961
+ (stream->dec_appheader =
962
+ (uint8_t*) xd3_alloc (stream,
963
+ stream->dec_appheadsz+1, 1)) == NULL)
964
+ {
965
+ return ENOMEM;
966
+ }
967
+
968
+ stream->dec_appheader[stream->dec_appheadsz] = 0;
969
+
970
+ if ((ret = xd3_decode_bytes (stream, stream->dec_appheader,
971
+ & stream->dec_appheadbytes,
972
+ stream->dec_appheadsz)))
973
+ {
974
+ return ret;
975
+ }
976
+ }
977
+
978
+ /* xoff_t -> usize_t is safe because this is the first block. */
979
+ stream->dec_hdrsize = (usize_t) stream->total_in;
980
+ stream->dec_state = DEC_WININD;
981
+
982
+ case DEC_WININD:
983
+ {
984
+ /* Start of a window: the window indicator */
985
+ if ((ret = xd3_decode_byte (stream, & stream->dec_win_ind)))
986
+ {
987
+ return ret;
988
+ }
989
+
990
+ stream->current_window = stream->dec_window_count;
991
+
992
+ if (XOFF_T_OVERFLOW (stream->dec_winstart, stream->dec_tgtlen))
993
+ {
994
+ stream->msg = "decoder file offset overflow";
995
+ return XD3_INVALID_INPUT;
996
+ }
997
+
998
+ stream->dec_winstart += stream->dec_tgtlen;
999
+
1000
+ if ((stream->dec_win_ind & VCD_INVWIN) != 0)
1001
+ {
1002
+ stream->msg = "unrecognized window indicator bits set";
1003
+ return XD3_INVALID_INPUT;
1004
+ }
1005
+
1006
+ if ((ret = xd3_decode_init_window (stream))) { return ret; }
1007
+
1008
+ stream->dec_state = DEC_CPYLEN;
1009
+
1010
+ IF_DEBUG2 (DP(RINT "--------- TARGET WINDOW %"Q"u -----------\n",
1011
+ stream->current_window));
1012
+ }
1013
+
1014
+ case DEC_CPYLEN:
1015
+ /* Copy window length: only if VCD_SOURCE or VCD_TARGET is set */
1016
+ SIZE_CASE(SRCORTGT (stream->dec_win_ind), stream->dec_cpylen,
1017
+ DEC_CPYOFF);
1018
+
1019
+ /* Set the initial, logical decoder position (HERE address) in
1020
+ * dec_position. This is set to just after the source/copy
1021
+ * window, as we are just about to output the first byte of
1022
+ * target window. */
1023
+ stream->dec_position = stream->dec_cpylen;
1024
+
1025
+ case DEC_CPYOFF:
1026
+ /* Copy window offset: only if VCD_SOURCE or VCD_TARGET is set */
1027
+ OFFSET_CASE(SRCORTGT (stream->dec_win_ind), stream->dec_cpyoff,
1028
+ DEC_ENCLEN);
1029
+
1030
+ /* Copy offset and copy length may not overflow. */
1031
+ if (XOFF_T_OVERFLOW (stream->dec_cpyoff, stream->dec_cpylen))
1032
+ {
1033
+ stream->msg = "decoder copy window overflows a file offset";
1034
+ return XD3_INVALID_INPUT;
1035
+ }
1036
+
1037
+ /* Check copy window bounds: VCD_TARGET window may not exceed
1038
+ current position. */
1039
+ if ((stream->dec_win_ind & VCD_TARGET) &&
1040
+ (stream->dec_cpyoff + stream->dec_cpylen >
1041
+ stream->dec_winstart))
1042
+ {
1043
+ stream->msg = "VCD_TARGET window out of bounds";
1044
+ return XD3_INVALID_INPUT;
1045
+ }
1046
+
1047
+ case DEC_ENCLEN:
1048
+ /* Length of the delta encoding */
1049
+ SIZE_CASE(1, stream->dec_enclen, DEC_TGTLEN);
1050
+ case DEC_TGTLEN:
1051
+ /* Length of target window */
1052
+ SIZE_CASE(1, stream->dec_tgtlen, DEC_DELIND);
1053
+
1054
+ /* Set the maximum decoder position, beyond which we should not
1055
+ * decode any data. This is the maximum value for dec_position.
1056
+ * This may not exceed the size of a usize_t. */
1057
+ if (USIZE_T_OVERFLOW (stream->dec_cpylen, stream->dec_tgtlen))
1058
+ {
1059
+ stream->msg = "decoder target window overflows a usize_t";
1060
+ return XD3_INVALID_INPUT;
1061
+ }
1062
+
1063
+ /* Check for malicious files. */
1064
+ if (stream->dec_tgtlen > XD3_HARDMAXWINSIZE)
1065
+ {
1066
+ stream->msg = "hard window size exceeded";
1067
+ return XD3_INVALID_INPUT;
1068
+ }
1069
+
1070
+ stream->dec_maxpos = stream->dec_cpylen + stream->dec_tgtlen;
1071
+
1072
+ case DEC_DELIND:
1073
+ /* Delta indicator */
1074
+ BYTE_CASE(1, stream->dec_del_ind, DEC_DATALEN);
1075
+
1076
+ if ((stream->dec_del_ind & VCD_INVDEL) != 0)
1077
+ {
1078
+ stream->msg = "unrecognized delta indicator bits set";
1079
+ return XD3_INVALID_INPUT;
1080
+ }
1081
+
1082
+ /* Delta indicator is only used with secondary compression. */
1083
+ if ((stream->dec_del_ind != 0) && (stream->sec_type == NULL))
1084
+ {
1085
+ stream->msg = "invalid delta indicator bits set";
1086
+ return XD3_INVALID_INPUT;
1087
+ }
1088
+
1089
+ /* Section lengths */
1090
+ case DEC_DATALEN:
1091
+ SIZE_CASE(1, stream->data_sect.size, DEC_INSTLEN);
1092
+ case DEC_INSTLEN:
1093
+ SIZE_CASE(1, stream->inst_sect.size, DEC_ADDRLEN);
1094
+ case DEC_ADDRLEN:
1095
+ SIZE_CASE(1, stream->addr_sect.size, DEC_CKSUM);
1096
+
1097
+ case DEC_CKSUM:
1098
+ /* Window checksum. */
1099
+ if ((stream->dec_win_ind & VCD_ADLER32) != 0)
1100
+ {
1101
+ int i;
1102
+
1103
+ if ((ret = xd3_decode_bytes (stream, stream->dec_cksum,
1104
+ & stream->dec_cksumbytes, 4)))
1105
+ {
1106
+ return ret;
1107
+ }
1108
+
1109
+ for (i = 0; i < 4; i += 1)
1110
+ {
1111
+ stream->dec_adler32 =
1112
+ (stream->dec_adler32 << 8) | stream->dec_cksum[i];
1113
+ }
1114
+ }
1115
+
1116
+ stream->dec_state = DEC_DATA;
1117
+
1118
+ /* Check dec_enclen for redundency, otherwise it is not really used. */
1119
+ {
1120
+ usize_t enclen_check =
1121
+ (1 + (xd3_sizeof_size (stream->dec_tgtlen) +
1122
+ xd3_sizeof_size (stream->data_sect.size) +
1123
+ xd3_sizeof_size (stream->inst_sect.size) +
1124
+ xd3_sizeof_size (stream->addr_sect.size)) +
1125
+ stream->data_sect.size +
1126
+ stream->inst_sect.size +
1127
+ stream->addr_sect.size +
1128
+ ((stream->dec_win_ind & VCD_ADLER32) ? 4 : 0));
1129
+
1130
+ if (stream->dec_enclen != enclen_check)
1131
+ {
1132
+ stream->msg = "incorrect encoding length (redundent)";
1133
+ return XD3_INVALID_INPUT;
1134
+ }
1135
+ }
1136
+
1137
+ /* Returning here gives the application a chance to inspect the
1138
+ * header, skip the window, etc. */
1139
+ if (stream->current_window == 0) { return XD3_GOTHEADER; }
1140
+ else { return XD3_WINSTART; }
1141
+
1142
+ case DEC_DATA:
1143
+ case DEC_INST:
1144
+ case DEC_ADDR:
1145
+ /* Next read the three sections. */
1146
+ if ((ret = xd3_decode_sections (stream))) { return ret; }
1147
+
1148
+ case DEC_EMIT:
1149
+
1150
+ /* To speed VCD_SOURCE block-address calculations, the source
1151
+ * cpyoff_blocks and cpyoff_blkoff are pre-computed. */
1152
+ if (stream->dec_win_ind & VCD_SOURCE)
1153
+ {
1154
+ xd3_source *src = stream->src;
1155
+
1156
+ if (src == NULL)
1157
+ {
1158
+ stream->msg = "source input required";
1159
+ return XD3_INVALID_INPUT;
1160
+ }
1161
+
1162
+ xd3_blksize_div(stream->dec_cpyoff, src,
1163
+ &src->cpyoff_blocks,
1164
+ &src->cpyoff_blkoff);
1165
+
1166
+ IF_DEBUG2(DP(RINT
1167
+ "[decode_cpyoff] %"Q"u "
1168
+ "cpyblkno %"Q"u "
1169
+ "cpyblkoff %"W"u "
1170
+ "blksize %"W"u\n",
1171
+ stream->dec_cpyoff,
1172
+ src->cpyoff_blocks,
1173
+ src->cpyoff_blkoff,
1174
+ src->blksize));
1175
+ }
1176
+
1177
+ /* xd3_decode_emit returns XD3_OUTPUT on every success. */
1178
+ if ((ret = xd3_decode_emit (stream)) == XD3_OUTPUT)
1179
+ {
1180
+ stream->total_out += stream->avail_out;
1181
+ }
1182
+
1183
+ return ret;
1184
+
1185
+ case DEC_FINISH:
1186
+ {
1187
+ if (stream->dec_win_ind & VCD_TARGET)
1188
+ {
1189
+ if (stream->dec_lastwin == NULL)
1190
+ {
1191
+ stream->dec_lastwin = stream->next_out;
1192
+ stream->dec_lastspace = stream->space_out;
1193
+ }
1194
+ else
1195
+ {
1196
+ xd3_swap_uint8p (& stream->dec_lastwin,
1197
+ & stream->next_out);
1198
+ xd3_swap_usize_t (& stream->dec_lastspace,
1199
+ & stream->space_out);
1200
+ }
1201
+ }
1202
+
1203
+ stream->dec_lastlen = stream->dec_tgtlen;
1204
+ stream->dec_laststart = stream->dec_winstart;
1205
+ stream->dec_window_count += 1;
1206
+
1207
+ /* Note: the updates to dec_winstart & current_window are
1208
+ * deferred until after the next DEC_WININD byte is read. */
1209
+ stream->dec_state = DEC_WININD;
1210
+ return XD3_WINFINISH;
1211
+ }
1212
+
1213
+ default:
1214
+ stream->msg = "invalid state";
1215
+ return XD3_INVALID_INPUT;
1216
+ }
1217
+ }
1218
+
1219
+ #endif // _XDELTA3_DECODE_H_