root/vowfsc/xdelta3.h

Revision 36, 39.3 kB (checked in by dmajnem2, 10 months ago)

Beginning xdelta stuffs

  • Property svn:executable set to *
Line 
1 /* xdelta 3 - delta compression tools and library
2  * Copyright (C) 2001, 2003, 2004, 2005, 2006.  Joshua P. MacDonald
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 /* Welcome to Xdelta.  If you want to know more about Xdelta, start by reading xdelta3.c.
20  * If you are ready to use the API, continue reading here.  There are two interfaces --
21  * xd3_encode_input and xd3_decode_input -- plus a dozen or so related calls.  This
22  * interface is styled after Zlib. */
23
24 #ifndef _XDELTA3_H_
25 #define _XDELTA3_H_
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30
31 /**********************************************************************/
32
33 /* Default configured value of stream->winsize.  If the program supplies
34  * xd3_encode_input() with data smaller than winsize the stream will
35  * automatically buffer the input, otherwise the input buffer is used directly.
36  */
37 #ifndef XD3_DEFAULT_WINSIZE
38 #define XD3_DEFAULT_WINSIZE (1U << 23)
39 #endif
40
41 /* Default total size of the source window used in xdelta3-main.h */
42 #ifndef XD3_DEFAULT_SRCWINSZ
43 #define XD3_DEFAULT_SRCWINSZ (1U << 26)
44 #endif
45
46 /* When Xdelta requests a memory allocation for certain buffers, it rounds up to units of
47  * at least this size.  The code assumes (and asserts) that this is a power-of-two. */
48 #ifndef XD3_ALLOCSIZE
49 #define XD3_ALLOCSIZE (1U<<14)
50 #endif
51
52 /* The XD3_HARDMAXWINSIZE parameter is a safety mechanism to protect decoders against
53  * malicious files.  The decoder will never decode a window larger than this.  If the file
54  * specifies VCD_TARGET the decoder may require two buffers of this size.
55  *
56  * 8-16MB is reasonable, probably don't need to go larger. */
57 #ifndef XD3_HARDMAXWINSIZE
58 #define XD3_HARDMAXWINSIZE (1U<<24)
59 #endif
60 /* The IOPT_SIZE value sets the size of a buffer used to batch overlapping copy
61  * instructions before they are optimized by picking the best non-overlapping ranges.  The
62  * larger this buffer, the longer a forced xd3_srcwin_setup() decision is held off.
63  * Setting this value to 0 causes an unlimited buffer to be used. */
64 #ifndef XD3_DEFAULT_IOPT_SIZE
65 #define XD3_DEFAULT_IOPT_SIZE    (1U<<15)
66 #endif
67
68 /* The maximum distance backward to search for small matches */
69 #ifndef XD3_DEFAULT_SPREVSZ
70 #define XD3_DEFAULT_SPREVSZ (1U<<18)
71 #endif
72
73 /* The default compression level
74  */
75 #ifndef XD3_DEFAULT_LEVEL
76 #define XD3_DEFAULT_LEVEL 1
77 #endif
78
79 /* Sizes and addresses within VCDIFF windows are represented as usize_t
80  *
81  * For source-file offsets and total file sizes, total input and output counts, the xoff_t
82  * type is used.  The decoder and encoder generally check for overflow of the xoff_t size,
83  * and this is tested at the 32bit boundary [xdelta3-test.h].
84  */
85 #ifndef _WIN32
86 typedef unsigned int    usize_t;
87 typedef u_int8_t        uint8_t;
88 typedef u_int16_t       uint16_t;
89
90 #ifndef __uint32_t_defined  /* Note: Cygwin compat */
91 typedef u_int32_t       uint32_t;
92 #endif
93
94 typedef long long unsigned int uint64_t;
95
96 #else
97 #define WIN32_LEAN_AND_MEAN
98 #include <windows.h>
99 #define inline
100 typedef unsigned int   uint;
101 typedef signed int     ssize_t;
102 typedef unsigned int   usize_t;
103 typedef unsigned char  uint8_t;
104 typedef unsigned short uint16_t;
105 typedef unsigned long  uint32_t;
106 typedef ULONGLONG      uint64_t;
107 #endif
108
109 #define SIZEOF_USIZE_T 4
110
111 #ifndef XD3_USE_LARGEFILE64
112 #define XD3_USE_LARGEFILE64 1
113 #endif
114
115 #if XD3_USE_LARGEFILE64
116 #define __USE_FILE_OFFSET64 1 /* GLIBC: for 64bit fileops, ... ? */
117 typedef uint64_t xoff_t;
118 #define SIZEOF_XOFF_T 8
119 #else
120 typedef uint32_t xoff_t;
121 #define SIZEOF_XOFF_T 4
122 #endif
123
124 #define USE_UINT32 (SIZEOF_USIZE_T == 4 || SIZEOF_XOFF_T == 4 || REGRESSION_TEST)
125 #define USE_UINT64 (SIZEOF_USIZE_T == 8 || SIZEOF_XOFF_T == 8 || REGRESSION_TEST)
126
127 /**********************************************************************/
128
129 #ifndef INLINE
130 #define INLINE inline
131 #endif
132
133 /* Whether to build the encoder, otherwise only build the decoder. */
134 #ifndef XD3_ENCODER
135 #define XD3_ENCODER 1
136 #endif
137
138 /* The code returned when main() fails, also defined in system includes. */
139 #ifndef EXIT_FAILURE
140 #define EXIT_FAILURE 1
141 #endif
142
143 /* REGRESSION TEST enables the "xdelta3 test" command, which runs a series of self-tests. */
144 #ifndef REGRESSION_TEST
145 #define REGRESSION_TEST 0
146 #endif
147
148 /* XD3_DEBUG=1 enables assertions and various statistics.  Levels > 1 enable some
149  * additional output only useful during development and debugging. */
150 #ifndef XD3_DEBUG
151 #define XD3_DEBUG 0
152 #endif
153
154 #ifndef PYTHON_MODULE
155 #define PYTHON_MODULE 0
156 #endif
157
158 #ifndef SWIG_MODULE
159 #define SWIG_MODULE 0
160 #endif
161
162 /* There are three string matching functions supplied: one fast, one slow (default), and
163  * one soft-configurable.  To disable any of these, use the following definitions. */
164 #ifndef XD3_BUILD_SLOW
165 #define XD3_BUILD_SLOW 1
166 #endif
167 #ifndef XD3_BUILD_FAST
168 #define XD3_BUILD_FAST 1
169 #endif
170 #ifndef XD3_BUILD_FASTEST
171 #define XD3_BUILD_FASTEST 1
172 #endif
173 #ifndef XD3_BUILD_SOFT
174 #define XD3_BUILD_SOFT 1
175 #endif
176 #ifndef XD3_BUILD_DEFAULT
177 #define XD3_BUILD_DEFAULT 1
178 #endif
179
180 #if XD3_DEBUG
181 #include <stdio.h>
182 #endif
183
184 /* XPRINT.  Debug output and VCDIFF_TOOLS functions report to stderr.  I have used an
185  * irregular style to abbreviate [fprintf(stderr, "] as [DP(RINT "]. */
186 #define DP   fprintf
187 #define RINT stderr,
188
189 typedef struct _xd3_stream             xd3_stream;
190 typedef struct _xd3_source             xd3_source;
191 typedef struct _xd3_hash_cfg           xd3_hash_cfg;
192 typedef struct _xd3_smatcher           xd3_smatcher;
193 typedef struct _xd3_rinst              xd3_rinst;
194 typedef struct _xd3_dinst              xd3_dinst;
195 typedef struct _xd3_hinst              xd3_hinst;
196 typedef struct _xd3_rpage              xd3_rpage;
197 typedef struct _xd3_addr_cache         xd3_addr_cache;
198 typedef struct _xd3_output             xd3_output;
199 typedef struct _xd3_desect             xd3_desect;
200 typedef struct _xd3_iopt_buflist       xd3_iopt_buflist;
201 typedef struct _xd3_rlist              xd3_rlist;
202 typedef struct _xd3_sec_type           xd3_sec_type;
203 typedef struct _xd3_sec_cfg            xd3_sec_cfg;
204 typedef struct _xd3_sec_stream         xd3_sec_stream;
205 typedef struct _xd3_config             xd3_config;
206 typedef struct _xd3_code_table_desc    xd3_code_table_desc;
207 typedef struct _xd3_code_table_sizes   xd3_code_table_sizes;
208 typedef struct _xd3_slist              xd3_slist;
209
210 /* The stream configuration has three callbacks functions, all of which may be supplied
211  * with NULL values.  If config->getblk is provided as NULL, the stream returns
212  * XD3_GETSRCBLK. */
213
214 typedef void*  (xd3_alloc_func)    (void       *opaque,
215                                     usize_t      items,
216                                     usize_t      size);
217 typedef void   (xd3_free_func)     (void       *opaque,
218                                     void       *address);
219
220 typedef int    (xd3_getblk_func)   (xd3_stream *stream,
221                                     xd3_source *source,
222                                     xoff_t      blkno);
223
224 /* These are internal functions to delay construction of encoding tables and support
225  * alternate code tables.  See the comments & code enabled by GENERIC_ENCODE_TABLES. */
226
227 typedef const xd3_dinst* (xd3_code_table_func) (void);
228 typedef int              (xd3_comp_table_func) (xd3_stream *stream,
229                                                 const uint8_t **data,
230                                                 usize_t *size);
231
232
233 /* Some junk. */
234
235 #ifndef XD3_ASSERT
236 #if XD3_DEBUG
237 #define XD3_ASSERT(x) \
238     do { if (! (x)) { DP(RINT "%s:%d: XD3 assertion failed: %s\n", __FILE__, __LINE__, #x); \
239     abort (); } } while (0)
240 #else
241 #define XD3_ASSERT(x) (void)0
242 #endif
243 #endif
244
245 #ifdef __GNUC__
246 /* As seen on linux-kernel. */
247 #ifndef max
248 #define max(x,y) ({ \
249         const typeof(x) _x = (x);       \
250         const typeof(y) _y = (y);       \
251         (void) (&_x == &_y);            \
252         _x > _y ? _x : _y; })
253 #endif
254
255 #ifndef min
256 #define min(x,y) ({ \
257         const typeof(x) _x = (x);       \
258         const typeof(y) _y = (y);       \
259         (void) (&_x == &_y);            \
260         _x < _y ? _x : _y; })
261 #endif
262 #else
263 #ifndef max
264 #define max(x,y) ((x) < (y) ? (y) : (x))
265 #endif
266 #ifndef min
267 #define min(x,y) ((x) < (y) ? (x) : (y))
268 #endif
269 #endif
270
271 /******************************************************************************************
272  PUBLIC ENUMS
273  ******************************************************************************************/
274
275 /* These are the five ordinary status codes returned by the xd3_encode_input() and
276  * xd3_decode_input() state machines. */
277 typedef enum {
278
279   /* An application must be prepared to handle these five return values from either
280    * xd3_encode_input or xd3_decode_input, except in the case of no-source compression, in
281    * which case XD3_GETSRCBLK is never returned.  More detailed comments for these are
282    * given in xd3_encode_input and xd3_decode_input comments, below. */
283   XD3_INPUT     = -17703, /* need input */
284   XD3_OUTPUT    = -17704, /* have output */
285   XD3_GETSRCBLK = -17705, /* need a block of source input (with no xd3_getblk function),
286                            * a chance to do non-blocking read. */
287   XD3_GOTHEADER = -17706, /* (decode-only) after the initial VCDIFF & first window header */
288   XD3_WINSTART  = -17707, /* notification: returned before a window is processed, giving a
289                            * chance to XD3_SKIP_WINDOW or not XD3_SKIP_EMIT that window. */
290   XD3_WINFINISH  = -17708, /* notification: returned after encode/decode & output for a window */
291   XD3_TOOFARBACK = -17709, /* (encoder only) may be returned by getblk() if the block is too old */
292   XD3_INTERNAL   = -17710, /* internal error */
293   XD3_INVALID    = -17711, /* invalid config */
294   XD3_INVALID_INPUT = -17712, /* invalid input/decoder error */
295   XD3_NOSECOND  = -17713, /* when secondary compression finds no improvement. */
296
297 } xd3_rvalues;
298
299 /* special values in config->flags */
300 typedef enum
301 {
302   XD3_JUST_HDR       = (1 << 1),   /* used by VCDIFF tools, see xdelta3-main.h. */
303   XD3_SKIP_WINDOW    = (1 << 2),   /* used by VCDIFF tools, see xdelta3-main.h. */
304   XD3_SKIP_EMIT      = (1 << 3),   /* used by VCDIFF tools, see xdelta3-main.h. */
305   XD3_FLUSH          = (1 << 4),   /* flush the stream buffer to prepare for xd3_stream_close(). */
306
307   XD3_SEC_DJW        = (1 << 5),   /* use DJW static huffman */
308   XD3_SEC_FGK        = (1 << 6),   /* use FGK adaptive huffman */
309   XD3_SEC_TYPE       = (XD3_SEC_DJW | XD3_SEC_FGK),
310
311   XD3_SEC_NODATA     = (1 << 7),   /* disable secondary compression of the data section. */
312   XD3_SEC_NOINST     = (1 << 8),   /* disable secondary compression of the inst section. */
313   XD3_SEC_NOADDR     = (1 << 9),   /* disable secondary compression of the addr section. */
314
315   XD3_SEC_OTHER      = (XD3_SEC_NODATA | XD3_SEC_NOINST | XD3_SEC_NOADDR),
316
317   XD3_ADLER32        = (1 << 10),  /* enable checksum computation in the encoder. */
318   XD3_ADLER32_NOVER  = (1 << 11),  /* disable checksum verification in the decoder. */
319
320   XD3_ALT_CODE_TABLE = (1 << 12),  /* for testing the alternate code table encoding. */
321
322   XD3_NOCOMPRESS     = (1 << 13),  /* disable ordinary data compression feature,
323                                     * only search the source, not the target. */
324   XD3_BEGREEDY       = (1 << 14),  /* disable the "1.5-pass algorithm", instead use
325                                     * greedy matching.  Greedy is off by default. */
326
327   /* 4 bits to set the compression level the same as the command-line
328    * setting -1 through -9 (-0 corresponds to the XD3_NOCOMPRESS flag,
329    * and is independent of compression level).  This is for
330    * convenience, especially with xd3_encode_memory(). */
331
332   XD3_COMPLEVEL_SHIFT = 20,  /* 20 - 24 */
333   XD3_COMPLEVEL_MASK = (0xF << XD3_COMPLEVEL_SHIFT),
334   XD3_COMPLEVEL_1 = (1 << XD3_COMPLEVEL_SHIFT),
335   XD3_COMPLEVEL_3 = (3 << XD3_COMPLEVEL_SHIFT),
336   XD3_COMPLEVEL_6 = (6 << XD3_COMPLEVEL_SHIFT),
337   XD3_COMPLEVEL_9 = (9 << XD3_COMPLEVEL_SHIFT),
338
339 } xd3_flags;
340
341 /* The values of this enumeration are set in xd3_config using the
342  * smatch_cfg variable.  It can be set to default, slow, fast, etc.,
343  * and soft. */
344 typedef enum
345 {
346   XD3_SMATCH_DEFAULT = 0, /* Flags may contain XD3_COMPLEVEL bits, else default. */
347   XD3_SMATCH_SLOW    = 1,
348   XD3_SMATCH_FAST    = 2,
349   XD3_SMATCH_FASTEST = 3,
350   XD3_SMATCH_SOFT    = 4,
351 } xd3_smatch_cfg;
352
353 /******************************************************************************************
354  PRIVATE ENUMS
355  ******************************************************************************************/
356
357 /* stream->match_state is part of the xd3_encode_input state machine for source matching:
358  *
359  *  1. the XD3_GETSRCBLK block-read mechanism means reentrant matching
360  *  2. this state spans encoder windows: a match and end-of-window will continue in the next
361  *  3. the initial target byte and source byte are a presumed match, to avoid some computation
362  *  in case the inputs are identical.
363  */
364 typedef enum {
365
366   MATCH_TARGET    = 0, /* in this state, attempt to match the start of the target with the
367                         * previously set source address (initially 0). */
368   MATCH_BACKWARD  = 1, /* currently expanding a match backward in the source/target. */
369   MATCH_FORWARD   = 2, /* currently expanding a match forward in the source/target. */
370   MATCH_SEARCHING = 3, /* currently searching for a match. */
371
372 } xd3_match_state;
373
374 /* The xd3_encode_input state machine steps through these states in the following order.
375  * The matcher is reentrant and returns XD3_INPUT whenever it requires more data.  After
376  * receiving XD3_INPUT, if the application reads EOF it should call xd3_stream_close().
377  */
378 typedef enum {
379
380   ENC_INIT      = 0, /* xd3_encode_input has never been called. */
381   ENC_INPUT     = 1, /* waiting for xd3_avail_input () to be called. */
382   ENC_SEARCH    = 2, /* currently searching for matches. */
383   ENC_FLUSH     = 3, /* currently emitting output. */
384   ENC_POSTOUT   = 4, /* after an output section. */
385   ENC_POSTWIN   = 5, /* after all output sections. */
386   ENC_ABORTED   = 6, /* abort. */
387 } xd3_encode_state;
388
389 /* The xd3_decode_input state machine steps through these states in the following order.
390  * The matcher is reentrant and returns XD3_INPUT whenever it requires more data.  After
391  * receiving XD3_INPUT, if the application reads EOF it should call xd3_stream_close().
392  *
393  * 0-8:   the VCDIFF header
394  * 9-18:  the VCDIFF window header
395  * 19-21: the three primary sections: data (which I think should have gone last), inst, addr
396  * 22:    producing output: returns XD3_OUTPUT, possibly XD3_GETSRCBLK,
397  * 23:    return XD3_WINFINISH, set state=9 to decode more input
398  */
399 typedef enum {
400
401   DEC_VCHEAD   = 0, /* VCDIFF header */
402   DEC_HDRIND   = 1, /* header indicator */
403
404   DEC_SECONDID = 2, /* secondary compressor ID */
405
406   DEC_TABLEN   = 3, /* code table length */
407   DEC_NEAR     = 4, /* code table near */
408   DEC_SAME     = 5, /* code table same */
409   DEC_TABDAT   = 6, /* code table data */
410
411   DEC_APPLEN   = 7, /* application data length */
412   DEC_APPDAT   = 8, /* application data */
413
414   DEC_WININD   = 9, /* window indicator */
415
416   DEC_CPYLEN   = 10, /* copy window length */
417   DEC_CPYOFF   = 11, /* copy window offset */
418
419   DEC_ENCLEN   = 12, /* length of delta encoding */
420   DEC_TGTLEN   = 13, /* length of target window */
421   DEC_DELIND   = 14, /* delta indicator */
422
423   DEC_DATALEN  = 15, /* length of ADD+RUN data */
424   DEC_INSTLEN  = 16, /* length of instruction data */
425   DEC_ADDRLEN  = 17, /* length of address data */
426
427   DEC_CKSUM    = 18, /* window checksum */
428
429   DEC_DATA     = 19, /* data section */
430   DEC_INST     = 20, /* instruction section */
431   DEC_ADDR     = 21, /* address section */
432
433   DEC_EMIT     = 22, /* producing data */
434
435   DEC_FINISH   = 23, /* window finished */
436
437   DEC_ABORTED  = 24, /* xd3_abort_stream */
438 } xd3_decode_state;
439
440 /******************************************************************************************
441  internal types
442  ******************************************************************************************/
443
444 /* instruction lists used in the IOPT buffer */
445 struct _xd3_rlist
446 {
447   xd3_rlist  *next;
448   xd3_rlist  *prev;
449 };
450
451 /* the raw encoding of an instruction used in the IOPT buffer */
452 struct _xd3_rinst
453 {
454   uint8_t     type;
455   uint8_t     xtra;
456   uint8_t     code1;
457   uint8_t     code2;
458   usize_t      pos;
459   usize_t      size;
460   xoff_t      addr;
461   xd3_rlist   link;
462 };
463
464 /* the code-table form of an single- or double-instruction */
465 struct _xd3_dinst
466 {
467   uint8_t     type1;
468   uint8_t     size1;
469   uint8_t     type2;
470   uint8_t     size2;
471 };
472
473 /* the decoded form of a single (half) instruction. */
474 struct _xd3_hinst
475 {
476   uint8_t     type;
477   uint32_t    size;
478   uint32_t    addr;
479 };
480
481 /* used by the encoder to buffer output in sections.  list of blocks. */
482 struct _xd3_output
483 {
484   uint8_t    *base;
485   usize_t     next;
486   usize_t     avail;
487   xd3_output *next_page;
488 };
489
490 /* used by the decoder to buffer input in sections. */
491 struct _xd3_desect
492 {
493   const uint8_t *buf;
494   const uint8_t *buf_max;
495   uint32_t       size;
496   usize_t        pos;
497
498   /* used in xdelta3-decode.h */
499   uint8_t       *copied1;
500   usize_t        alloc1;
501
502   /* used in xdelta3-second.h */
503   uint8_t       *copied2;
504   usize_t        alloc2;
505 };
506
507 /* the VCDIFF address cache, see the RFC */
508 struct _xd3_addr_cache
509 {
510   uint     s_near;
511   uint     s_same;
512   usize_t  next_slot;  /* the circular index for near */
513   usize_t *near_array; /* array of size s_near        */
514   usize_t *same_array; /* array of size s_same*256    */
515 };
516
517 /* the IOPT buffer list is just a list of buffers, which may be allocated
518  * during encode when using an unlimited buffer. */
519 struct _xd3_iopt_buflist
520 {
521   xd3_rinst *buffer;
522   xd3_iopt_buflist *next;
523 };
524
525 /* This is the record of a pre-compiled configuration, a subset of xd3_config. */
526 struct _xd3_smatcher
527 {
528   const char        *name;
529   int             (*string_match) (xd3_stream  *stream);
530   uint               large_look;
531   uint               large_step;
532   uint               small_look;
533   uint               small_chain;
534   uint               small_lchain;
535   uint               max_lazy;
536   uint               long_enough;
537 };
538
539 /* hash table size & power-of-two hash function. */
540 struct _xd3_hash_cfg
541 {
542   usize_t           size;
543   usize_t           shift;
544   usize_t           mask;
545 };
546
547 /* the sprev list */
548 struct _xd3_slist
549 {
550   usize_t     last_pos;
551 };
552
553 /******************************************************************************************
554  public types
555  ******************************************************************************************/
556
557 /* Settings for the secondary compressor. */
558 struct _xd3_sec_cfg
559 {
560   int                data_type;     /* Which section. (set automatically) */
561   int                ngroups;       /* Number of DJW Huffman groups. */
562   int                sector_size;   /* Sector size. */
563   int                inefficient;   /* If true, ignore efficiency check [avoid XD3_NOSECOND]. */
564 };
565
566 /* This is the user-visible stream configuration. */
567 struct _xd3_config
568 {
569   usize_t             winsize;       /* The encoder window size. */
570   usize_t             sprevsz;       /* How far back small string matching goes */
571   usize_t             iopt_size;     /* entries in the instruction-optimizing buffer */
572   usize_t             srcwin_maxsz;  /* srcwin_size grows by a factor of 2 when no matches are found */
573
574   xd3_getblk_func   *getblk;        /* The three callbacks. */
575   xd3_alloc_func    *alloc;
576   xd3_free_func     *freef;
577   void              *opaque;        /* Not used. */
578   int                flags;         /* stream->flags are initialized from xd3_config &
579                                      * never modified by the library.  Use xd3_set_flags
580                                      * to modify flags settings mid-stream. */
581
582   xd3_sec_cfg       sec_data;       /* Secondary compressor config: data */
583   xd3_sec_cfg       sec_inst;       /* Secondary compressor config: inst */
584   xd3_sec_cfg       sec_addr;       /* Secondary compressor config: addr */
585
586   xd3_smatch_cfg     smatch_cfg;    /* See enum: use fields below for soft config */
587   xd3_smatcher       smatcher_soft;
588 };
589
590 /* The primary source file object. You create one of these objects and initialize the
591  * first four fields.  This library maintains the next 5 fields.  The configured getblk
592  * implementation is responsible for setting the final 3 fields when called (and/or when
593  * XD3_GETSRCBLK is returned).
594  */
595 struct _xd3_source
596 {
597   /* you set */
598   xoff_t              size;          /* size of this source */
599   usize_t             blksize;       /* block size */
600   const char         *name;          /* its name, for debug/print purposes */
601   void               *ioh;           /* opaque handle */
602
603   /* getblk sets */
604   xoff_t              curblkno;      /* current block number: client sets after getblk request */
605   usize_t             onblk;         /* number of bytes on current block: client sets, xd3 verifies */
606   const uint8_t      *curblk;        /* current block array: client sets after getblk request */
607
608   /* xd3 sets */
609   usize_t             srclen;        /* length of this source window */
610   xoff_t              srcbase;       /* offset of this source window in the source itself */
611   xoff_t              blocks;        /* the total number of blocks in this source */
612   usize_t             cpyoff_blocks; /* offset of copy window in blocks */
613   usize_t             cpyoff_blkoff; /* offset of copy window in blocks, remainder */
614   xoff_t              getblkno;      /* request block number: xd3 sets current getblk request */
615 };
616
617 /* The primary xd3_stream object, used for encoding and decoding.  You may access only two
618  * fields: avail_out, next_out.  Use the methods above to operate on xd3_stream. */
619 struct _xd3_stream
620 {
621   /* input state */
622   const uint8_t    *next_in;          /* next input byte */
623   usize_t           avail_in;         /* number of bytes available at next_in */
624   xoff_t            total_in;         /* how many bytes in */
625
626   /* output state */
627   uint8_t          *next_out;         /* next output byte */
628   usize_t           avail_out;        /* number of bytes available at next_out */
629   usize_t           space_out;        /* total out space */
630   xoff_t            current_window;   /* number of windows encoded/decoded */
631   xoff_t            total_out;        /* how many bytes out */
632
633   /* to indicate an error, xd3 sets */
634   const char       *msg;              /* last error message, NULL if no error */
635
636   /* source configuration */
637   xd3_source       *src;              /* source array */
638
639   /* encoder memory configuration */
640   usize_t           winsize;          /* suggested window size */
641   usize_t           sprevsz;          /* small string, previous window size (power of 2) */
642   usize_t           sprevmask;        /* small string, previous window size mask */
643   uint              iopt_size;
644   uint              iopt_unlimited;
645   uint              srcwin_maxsz;
646
647   /* general configuration */
648   xd3_getblk_func  *getblk;           /* set nxtblk, nxtblkno to scanblkno */
649   xd3_alloc_func   *alloc;            /* malloc function */
650   xd3_free_func    *free;             /* free function */
651   void*             opaque;           /* private data object passed to alloc, free, and getblk */
652   int               flags;            /* various options */
653  
654   /* secondary compressor configuration */
655   xd3_sec_cfg       sec_data;         /* Secondary compressor config: data */
656   xd3_sec_cfg       sec_inst;         /* Secondary compressor config: inst */
657   xd3_sec_cfg       sec_addr;         /* Secondary compressor config: addr */
658
659   xd3_smatcher      smatcher;
660
661   usize_t           *large_table;      /* table of large checksums */
662   xd3_hash_cfg       large_hash;       /* large hash config */
663
664   usize_t           *small_table;      /* table of small checksums */
665   xd3_slist         *small_prev;       /* table of previous offsets, circular linked list */
666   int                small_reset;      /* true if small table should be reset */
667
668   xd3_hash_cfg       small_hash;       /* small hash config */
669   xd3_addr_cache     acache;           /* the vcdiff address cache */
670   xd3_encode_state   enc_state;        /* state of the encoder */
671
672   usize_t            taroff;           /* base offset of the target input */
673   usize_t            input_position;   /* current input position */
674   usize_t            min_match;        /* current minimum match length, avoids redundent matches */
675   usize_t            unencoded_offset; /* current input, first unencoded offset. this value is <= the first
676                                        * instruction's position in the iopt buffer, if there is at least one
677                                        * match in the buffer. */
678
679   // SRCWIN
680   // these variables plus srcwin_maxsz above (set by config)
681   int