inc/xdelta3.h

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