Planeshift

xdelta3.h

Go to the documentation of this file.
00001 /* xdelta 3 - delta compression tools and library
00002  * Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007,
00003  * 2008, 2009, 2010.  Joshua P. MacDonald
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 /* To know more about Xdelta, start by reading xdelta3.c.  If you are
00021  * ready to use the API, continue reading here.  There are two
00022  * interfaces -- xd3_encode_input and xd3_decode_input -- plus a dozen
00023  * or so related calls.  This interface is styled after Zlib. */
00024 
00025 #ifndef _XDELTA3_H_
00026 #define _XDELTA3_H_
00027 
00028 #include <stddef.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <sys/types.h>
00032 
00033 /****************************************************************/
00034 
00035 /* Default configured value of stream->winsize.  If the program
00036  * supplies xd3_encode_input() with data smaller than winsize the
00037  * stream will automatically buffer the input, otherwise the input
00038  * buffer is used directly.
00039  */
00040 #ifndef XD3_DEFAULT_WINSIZE
00041 #define XD3_DEFAULT_WINSIZE (1U << 23)
00042 #endif
00043 
00044 /* Default total size of the source window used in xdelta3-main.h */
00045 #ifndef XD3_DEFAULT_SRCWINSZ
00046 #define XD3_DEFAULT_SRCWINSZ (1U << 26)
00047 #endif
00048 
00049 /* When Xdelta requests a memory allocation for certain buffers, it
00050  * rounds up to units of at least this size.  The code assumes (and
00051  * asserts) that this is a power-of-two. */
00052 #ifndef XD3_ALLOCSIZE
00053 #define XD3_ALLOCSIZE (1U<<14)
00054 #endif
00055 
00056 /* The XD3_HARDMAXWINSIZE parameter is a safety mechanism to protect
00057  * decoders against malicious files.  The decoder will never decode a
00058  * window larger than this.  If the file specifies VCD_TARGET the
00059  * decoder may require two buffers of this size.
00060  *
00061  * 8-16MB is reasonable, probably don't need to go larger. */
00062 #ifndef XD3_HARDMAXWINSIZE
00063 #define XD3_HARDMAXWINSIZE (1U<<24)
00064 #endif
00065 /* The IOPT_SIZE value sets the size of a buffer used to batch
00066  * overlapping copy instructions before they are optimized by picking
00067  * the best non-overlapping ranges.  The larger this buffer, the
00068  * longer a forced xd3_srcwin_setup() decision is held off.  Setting
00069  * this value to 0 causes an unlimited buffer to be used. */
00070 #ifndef XD3_DEFAULT_IOPT_SIZE
00071 #define XD3_DEFAULT_IOPT_SIZE    (1U<<15)
00072 #endif
00073 
00074 /* The maximum distance backward to search for small matches */
00075 #ifndef XD3_DEFAULT_SPREVSZ
00076 #define XD3_DEFAULT_SPREVSZ (1U<<18)
00077 #endif
00078 
00079 /* The default compression level
00080  */
00081 #ifndef XD3_DEFAULT_LEVEL
00082 #define XD3_DEFAULT_LEVEL 3
00083 #endif
00084 
00085 #ifndef XD3_DEFAULT_SECONDARY_LEVEL
00086 #define XD3_DEFAULT_SECONDARY_LEVEL 6
00087 #endif
00088 
00089 #ifndef XD3_USE_LARGEFILE64
00090 #define XD3_USE_LARGEFILE64 1
00091 #endif
00092 
00093 /* Sizes and addresses within VCDIFF windows are represented as usize_t
00094  *
00095  * For source-file offsets and total file sizes, total input and
00096  * output counts, the xoff_t type is used.  The decoder and encoder
00097  * generally check for overflow of the xoff_t size (this is tested at
00098  * the 32bit boundary [xdelta3-test.h]).
00099  */
00100 #ifndef _WIN32
00101 #include <stdint.h>
00102 typedef unsigned int usize_t;
00103 #else
00104 #define WIN32_LEAN_AND_MEAN
00105 #if XD3_USE_LARGEFILE64
00106 /* 64 bit file offsets: uses GetFileSizeEx and SetFilePointerEx.
00107  * requires Win2000 or newer version of WinNT */
00108 #define WINVER          0x0500
00109 #define _WIN32_WINNT    0x0500
00110 #else
00111 /* 32 bit (DWORD) file offsets: uses GetFileSize and
00112  * SetFilePointer. compatible with win9x-me and WinNT4 */
00113 #define WINVER          0x0400
00114 #define _WIN32_WINNT    0x0400
00115 #endif
00116 #include <windows.h>
00117 typedef unsigned int   usize_t;
00118 #ifdef _MSC_VER
00119 #define inline
00120 typedef signed int     ssize_t;
00121 typedef unsigned char  uint8_t;
00122 typedef unsigned short uint16_t;
00123 typedef unsigned long  uint32_t;
00124 typedef ULONGLONG      uint64_t;
00125 #else
00126 /* mingw32, lcc and watcom provide a proper header */
00127 #include <stdint.h>
00128 #endif
00129 #endif
00130 
00131 /* TODO: note that SIZEOF_USIZE_T is never set to 8, although it should be for
00132  * a 64bit platform.  OTOH, may be that using 32bits is appropriate even on a
00133  * 64bit platform because we allocate large arrays of these values. */
00134 #if XD3_USE_LARGEFILE64
00135 #define __USE_FILE_OFFSET64 1 /* GLIBC: for 64bit fileops, ... ? */
00136 #ifndef _LARGEFILE_SOURCE
00137 #define _LARGEFILE_SOURCE
00138 #endif
00139 #ifndef _FILE_OFFSET_BITS
00140 #define _FILE_OFFSET_BITS 64
00141 #endif
00142 
00143 typedef uint64_t xoff_t;
00144 #define SIZEOF_XOFF_T 8
00145 #define SIZEOF_USIZE_T 4
00146 #ifndef WIN32
00147 #define Q "ll"
00148 #else
00149 #define Q "I64"
00150 #endif
00151 #else
00152 typedef uint32_t xoff_t;
00153 #define SIZEOF_XOFF_T 4
00154 #define SIZEOF_USIZE_T 4
00155 #define Q
00156 #endif
00157 
00158 #define USE_UINT32 (SIZEOF_USIZE_T == 4 || \
00159                     SIZEOF_XOFF_T == 4 || REGRESSION_TEST)
00160 #define USE_UINT64 (SIZEOF_USIZE_T == 8 || \
00161                     SIZEOF_XOFF_T == 8 || REGRESSION_TEST)
00162 
00163 /* TODO: probably should do something better here. */
00164 #ifndef UNALIGNED_OK
00165 #if defined(__i386__) || defined(__i486__) || defined(__i586__) || \
00166   defined(__i686__) || defined(_X86_) || defined(__x86_64__)
00167 #define UNALIGNED_OK 1
00168 #else
00169 #define UNALIGNED_OK 0
00170 #endif
00171 #endif
00172 
00173 /**********************************************************************/
00174 
00175 /* Whether to build the encoder, otherwise only build the decoder. */
00176 #ifndef XD3_ENCODER
00177 #define XD3_ENCODER 1
00178 #endif
00179 
00180 /* The code returned when main() fails, also defined in system
00181    includes. */
00182 #ifndef EXIT_FAILURE
00183 #define EXIT_FAILURE 1
00184 #endif
00185 
00186 /* REGRESSION TEST enables the "xdelta3 test" command, which runs a
00187    series of self-tests. */
00188 #ifndef REGRESSION_TEST
00189 #define REGRESSION_TEST 0
00190 #endif
00191 
00192 /* XD3_DEBUG=1 enables assertions and various statistics.  Levels > 1
00193  * enable some additional output only useful during development and
00194  * debugging. */
00195 #ifndef XD3_DEBUG
00196 #define XD3_DEBUG 0
00197 #endif
00198 
00199 #ifndef PYTHON_MODULE
00200 #define PYTHON_MODULE 0
00201 #endif
00202 
00203 #ifndef SWIG_MODULE
00204 #define SWIG_MODULE 0
00205 #endif
00206 
00207 /* There are three string matching functions supplied: one fast, one
00208  * slow (default), and one soft-configurable.  To disable any of
00209  * these, use the following definitions. */
00210 #ifndef XD3_BUILD_SLOW
00211 #define XD3_BUILD_SLOW 1
00212 #endif
00213 #ifndef XD3_BUILD_FAST
00214 #define XD3_BUILD_FAST 1
00215 #endif
00216 #ifndef XD3_BUILD_FASTER
00217 #define XD3_BUILD_FASTER 1
00218 #endif
00219 #ifndef XD3_BUILD_FASTEST
00220 #define XD3_BUILD_FASTEST 1
00221 #endif
00222 #ifndef XD3_BUILD_SOFT
00223 #define XD3_BUILD_SOFT 1
00224 #endif
00225 #ifndef XD3_BUILD_DEFAULT
00226 #define XD3_BUILD_DEFAULT 1
00227 #endif
00228 
00229 #if XD3_DEBUG
00230 #include <stdio.h>
00231 #endif
00232 
00233 /* XPRINT.  Debug output and VCDIFF_TOOLS functions report to stderr.
00234  * I have used an irregular style to abbreviate [fprintf(stderr, "] as
00235  * [DP(RINT "]. */
00236 #define DP   fprintf
00237 #define RINT stderr,
00238 
00239 typedef struct _xd3_stream             xd3_stream;
00240 typedef struct _xd3_source             xd3_source;
00241 typedef struct _xd3_hash_cfg           xd3_hash_cfg;
00242 typedef struct _xd3_smatcher           xd3_smatcher;
00243 typedef struct _xd3_rinst              xd3_rinst;
00244 typedef struct _xd3_dinst              xd3_dinst;
00245 typedef struct _xd3_hinst              xd3_hinst;
00246 typedef struct _xd3_winst              xd3_winst;
00247 typedef struct _xd3_rpage              xd3_rpage;
00248 typedef struct _xd3_addr_cache         xd3_addr_cache;
00249 typedef struct _xd3_output             xd3_output;
00250 typedef struct _xd3_desect             xd3_desect;
00251 typedef struct _xd3_iopt_buflist       xd3_iopt_buflist;
00252 typedef struct _xd3_rlist              xd3_rlist;
00253 typedef struct _xd3_sec_type           xd3_sec_type;
00254 typedef struct _xd3_sec_cfg            xd3_sec_cfg;
00255 typedef struct _xd3_sec_stream         xd3_sec_stream;
00256 typedef struct _xd3_config             xd3_config;
00257 typedef struct _xd3_code_table_desc    xd3_code_table_desc;
00258 typedef struct _xd3_code_table_sizes   xd3_code_table_sizes;
00259 typedef struct _xd3_slist              xd3_slist;
00260 typedef struct _xd3_whole_state        xd3_whole_state;
00261 typedef struct _xd3_wininfo            xd3_wininfo;
00262 
00263 /* The stream configuration has three callbacks functions, all of
00264  * which may be supplied with NULL values.  If config->getblk is
00265  * provided as NULL, the stream returns XD3_GETSRCBLK. */
00266 
00267 typedef void*  (xd3_alloc_func)    (void       *opaque,
00268                                     usize_t      items,
00269                                     usize_t      size);
00270 typedef void   (xd3_free_func)     (void       *opaque,
00271                                     void       *address);
00272 
00273 typedef int    (xd3_getblk_func)   (xd3_stream *stream,
00274                                     xd3_source *source,
00275                                     xoff_t      blkno);
00276 
00277 /* These are internal functions to delay construction of encoding
00278  * tables and support alternate code tables.  See the comments & code
00279  * enabled by GENERIC_ENCODE_TABLES. */
00280 
00281 typedef const xd3_dinst* (xd3_code_table_func) (void);
00282 typedef int              (xd3_comp_table_func) (xd3_stream *stream,
00283                                                 const uint8_t **data,
00284                                                 usize_t *size);
00285 
00286 
00287 
00288 #if XD3_DEBUG
00289 #define XD3_ASSERT(x) \
00290     do { if (! (x)) { DP(RINT "%s:%d: XD3 assertion failed: %s\n", __FILE__, __LINE__, #x); \
00291     abort (); } } while (0)
00292 #else
00293 #define XD3_ASSERT(x) (void)0
00294 #endif  /* XD3_DEBUG */
00295 
00296 #ifdef __GNUC__
00297 #ifndef max
00298 #define max(x,y) ({ \
00299         const typeof(x) _x = (x);       \
00300         const typeof(y) _y = (y);       \
00301         (void) (&_x == &_y);            \
00302         _x > _y ? _x : _y; })
00303 #endif /* __GNUC__ */
00304 
00305 #ifndef min
00306 #define min(x,y) ({ \
00307         const typeof(x) _x = (x);       \
00308         const typeof(y) _y = (y);       \
00309         (void) (&_x == &_y);            \
00310         _x < _y ? _x : _y; })
00311 #endif
00312 #else  /* __GNUC__ */
00313 #ifndef max
00314 #define max(x,y) ((x) < (y) ? (y) : (x))
00315 #endif
00316 #ifndef min
00317 #define min(x,y) ((x) < (y) ? (x) : (y))
00318 #endif
00319 #endif  /* __GNUC__ */
00320 
00321 /****************************************************************
00322  PUBLIC ENUMS
00323  ******************************************************************/
00324 
00325 /* These are the five ordinary status codes returned by the
00326  * xd3_encode_input() and xd3_decode_input() state machines. */
00327 typedef enum {
00328 
00329   /* An application must be prepared to handle these five return
00330    * values from either xd3_encode_input or xd3_decode_input, except
00331    * in the case of no-source compression, in which case XD3_GETSRCBLK
00332    * is never returned.  More detailed comments for these are given in
00333    * xd3_encode_input and xd3_decode_input comments, below. */
00334   XD3_INPUT     = -17703, /* need input */
00335   XD3_OUTPUT    = -17704, /* have output */
00336   XD3_GETSRCBLK = -17705, /* need a block of source input (with no
00337                            * xd3_getblk function), a chance to do
00338                            * non-blocking read. */
00339   XD3_GOTHEADER = -17706, /* (decode-only) after the initial VCDIFF &
00340                              first window header */
00341   XD3_WINSTART  = -17707, /* notification: returned before a window is
00342                            * processed, giving a chance to
00343                            * XD3_SKIP_WINDOW or not XD3_SKIP_EMIT that
00344                            * window. */
00345   XD3_WINFINISH  = -17708, /* notification: returned after
00346                               encode/decode & output for a window */
00347   XD3_TOOFARBACK = -17709, /* (encoder only) may be returned by
00348                               getblk() if the block is too old */
00349   XD3_INTERNAL   = -17710, /* internal error */
00350   XD3_INVALID    = -17711, /* invalid config */
00351   XD3_INVALID_INPUT = -17712, /* invalid input/decoder error */
00352   XD3_NOSECOND    = -17713, /* when secondary compression finds no
00353                                improvement. */
00354   XD3_UNIMPLEMENTED = -17714 /* currently VCD_TARGET */
00355 } xd3_rvalues;
00356 
00357 /* special values in config->flags */
00358 typedef enum
00359 {
00360   XD3_JUST_HDR       = (1 << 1),   /* used by VCDIFF tools, see
00361                                       xdelta3-main.h. */
00362   XD3_SKIP_WINDOW    = (1 << 2),   /* used by VCDIFF tools, see
00363                                       xdelta3-main.h. */
00364   XD3_SKIP_EMIT      = (1 << 3),   /* used by VCDIFF tools, see
00365                                       xdelta3-main.h. */
00366   XD3_FLUSH          = (1 << 4),   /* flush the stream buffer to
00367                                       prepare for
00368                                       xd3_stream_close(). */
00369 
00370   XD3_SEC_DJW        = (1 << 5),   /* use DJW static huffman */
00371   XD3_SEC_FGK        = (1 << 6),   /* use FGK adaptive huffman */
00372   XD3_SEC_TYPE       = (XD3_SEC_DJW | XD3_SEC_FGK),
00373 
00374   XD3_SEC_NODATA     = (1 << 7),   /* disable secondary compression of
00375                                       the data section. */
00376   XD3_SEC_NOINST     = (1 << 8),   /* disable secondary compression of
00377                                       the inst section. */
00378   XD3_SEC_NOADDR     = (1 << 9),   /* disable secondary compression of
00379                                       the addr section. */
00380 
00381   XD3_SEC_NOALL      = (XD3_SEC_NODATA | XD3_SEC_NOINST | XD3_SEC_NOADDR),
00382 
00383   XD3_ADLER32        = (1 << 10),  /* enable checksum computation in
00384                                       the encoder. */
00385   XD3_ADLER32_NOVER  = (1 << 11),  /* disable checksum verification in
00386                                       the decoder. */
00387 
00388   XD3_ALT_CODE_TABLE = (1 << 12),  /* for testing th
00389                                       e alternate code table encoding. */
00390 
00391   XD3_NOCOMPRESS     = (1 << 13),  /* disable ordinary data
00392                                     * compression feature, only search
00393                                     * the source, not the target. */
00394   XD3_BEGREEDY       = (1 << 14),  /* disable the "1.5-pass
00395                                     * algorithm", instead use greedy
00396                                     * matching.  Greedy is off by
00397                                     * default. */
00398   XD3_ADLER32_RECODE = (1 << 15),  /* used by "recode". */
00399 
00400   /* 4 bits to set the compression level the same as the command-line
00401    * setting -1 through -9 (-0 corresponds to the XD3_NOCOMPRESS flag,
00402    * and is independent of compression level).  This is for
00403    * convenience, especially with xd3_encode_memory(). */
00404 
00405   XD3_COMPLEVEL_SHIFT = 20,  /* 20 - 24 */
00406   XD3_COMPLEVEL_MASK = (0xF << XD3_COMPLEVEL_SHIFT),
00407   XD3_COMPLEVEL_1 = (1 << XD3_COMPLEVEL_SHIFT),
00408   XD3_COMPLEVEL_2 = (2 << XD3_COMPLEVEL_SHIFT),
00409   XD3_COMPLEVEL_3 = (3 << XD3_COMPLEVEL_SHIFT),
00410   XD3_COMPLEVEL_6 = (6 << XD3_COMPLEVEL_SHIFT),
00411   XD3_COMPLEVEL_9 = (9 << XD3_COMPLEVEL_SHIFT)
00412 
00413 } xd3_flags;
00414 
00415 /* The values of this enumeration are set in xd3_config using the
00416  * smatch_cfg variable.  It can be set to default, slow, fast, etc.,
00417  * and soft. */
00418 typedef enum
00419 {
00420   XD3_SMATCH_DEFAULT = 0, /* Flags may contain XD3_COMPLEVEL bits,
00421                              else default. */
00422   XD3_SMATCH_SLOW    = 1,
00423   XD3_SMATCH_FAST    = 2,
00424   XD3_SMATCH_FASTER  = 3,
00425   XD3_SMATCH_FASTEST = 4,
00426   XD3_SMATCH_SOFT    = 5
00427 } xd3_smatch_cfg;
00428 
00429 /*********************************************************************
00430  PRIVATE ENUMS
00431 **********************************************************************/
00432 
00433 /* stream->match_state is part of the xd3_encode_input state machine
00434  *  for source matching:
00435  *
00436  *  1. the XD3_GETSRCBLK block-read mechanism means reentrant matching
00437  *  2. this state spans encoder windows: a match and end-of-window
00438  *  will continue in the next 3. the initial target byte and source
00439  *  byte are a presumed match, to avoid some computation in case the
00440  *  inputs are identical.
00441  */
00442 typedef enum {
00443 
00444   MATCH_TARGET    = 0, /* in this state, attempt to match the start of
00445                         * the target with the previously set source
00446                         * address (initially 0). */
00447   MATCH_BACKWARD  = 1, /* currently expanding a match backward in the
00448                           source/target. */
00449   MATCH_FORWARD   = 2, /* currently expanding a match forward in the
00450                           source/target. */
00451   MATCH_SEARCHING = 3 /* currently searching for a match. */
00452 
00453 } xd3_match_state;
00454 
00455 /* The xd3_encode_input state machine steps through these states in
00456  * the following order.  The matcher is reentrant and returns
00457  * XD3_INPUT whenever it requires more data.  After receiving
00458  * XD3_INPUT, if the application reads EOF it should call
00459  * xd3_stream_close().
00460  */
00461 typedef enum {
00462 
00463   ENC_INIT      = 0, /* xd3_encode_input has never been called. */
00464   ENC_INPUT     = 1, /* waiting for xd3_avail_input () to be called. */
00465   ENC_SEARCH    = 2, /* currently searching for matches. */
00466   ENC_INSTR     = 3, /* currently formatting output. */
00467   ENC_FLUSH     = 4, /* currently emitting output. */
00468   ENC_POSTOUT   = 5, /* after an output section. */
00469   ENC_POSTWIN   = 6, /* after all output sections. */
00470   ENC_ABORTED   = 7 /* abort. */
00471 } xd3_encode_state;
00472 
00473 /* The xd3_decode_input state machine steps through these states in
00474  * the following order.  The matcher is reentrant and returns
00475  * XD3_INPUT whenever it requires more data.  After receiving
00476  * XD3_INPUT, if the application reads EOF it should call
00477  * xd3_stream_close().
00478  *
00479  * 0-8:   the VCDIFF header
00480  * 9-18:  the VCDIFF window header
00481  * 19-21: the three primary sections: data, inst, addr
00482  * 22:    producing output: returns XD3_OUTPUT, possibly XD3_GETSRCBLK,
00483  * 23:    return XD3_WINFINISH, set state=9 to decode more input
00484  */
00485 typedef enum {
00486 
00487   DEC_VCHEAD   = 0, /* VCDIFF header */
00488   DEC_HDRIND   = 1, /* header indicator */
00489 
00490   DEC_SECONDID = 2, /* secondary compressor ID */
00491 
00492   DEC_TABLEN   = 3, /* code table length */
00493   DEC_NEAR     = 4, /* code table near */
00494   DEC_SAME     = 5, /* code table same */
00495   DEC_TABDAT   = 6, /* code table data */
00496 
00497   DEC_APPLEN   = 7, /* application data length */
00498   DEC_APPDAT   = 8, /* application data */
00499 
00500   DEC_WININD   = 9, /* window indicator */
00501 
00502   DEC_CPYLEN   = 10, /* copy window length */
00503   DEC_CPYOFF   = 11, /* copy window offset */
00504 
00505   DEC_ENCLEN   = 12, /* length of delta encoding */
00506   DEC_TGTLEN   = 13, /* length of target window */
00507   DEC_DELIND   = 14, /* delta indicator */
00508 
00509   DEC_DATALEN  = 15, /* length of ADD+RUN data */
00510   DEC_INSTLEN  = 16, /* length of instruction data */
00511   DEC_ADDRLEN  = 17, /* length of address data */
00512 
00513   DEC_CKSUM    = 18, /* window checksum */
00514 
00515   DEC_DATA     = 19, /* data section */
00516   DEC_INST     = 20, /* instruction section */
00517   DEC_ADDR     = 21, /* address section */
00518 
00519   DEC_EMIT     = 22, /* producing data */
00520 
00521   DEC_FINISH   = 23, /* window finished */
00522 
00523   DEC_ABORTED  = 24 /* xd3_abort_stream */
00524 } xd3_decode_state;
00525 
00526 /************************************************************
00527  internal types
00528  ************************************************************/
00529 
00530 /* instruction lists used in the IOPT buffer */
00531 struct _xd3_rlist
00532 {
00533   xd3_rlist  *next;
00534   xd3_rlist  *prev;
00535 };
00536 
00537 /* the raw encoding of an instruction used in the IOPT buffer */
00538 struct _xd3_rinst
00539 {
00540   uint8_t     type;
00541   uint8_t     xtra;
00542   uint8_t     code1;
00543   uint8_t     code2;
00544   usize_t      pos;
00545   usize_t      size;
00546   xoff_t      addr;
00547   xd3_rlist   link;
00548 };
00549 
00550 /* the code-table form of an single- or double-instruction */
00551 struct _xd3_dinst
00552 {
00553   uint8_t     type1;
00554   uint8_t     size1;
00555   uint8_t     type2;
00556   uint8_t     size2;
00557 };
00558 
00559 /* the decoded form of a single (half) instruction. */
00560 struct _xd3_hinst
00561 {
00562   uint8_t     type;
00563   uint32_t    size;  /* TODO: why decode breaks if this is usize_t? */
00564   uint32_t    addr;  /* TODO: why decode breaks if this is usize_t? */
00565 };
00566 
00567 /* the form of a whole-file instruction */
00568 struct _xd3_winst
00569 {
00570   uint8_t type;  /* RUN, ADD, COPY */
00571   uint8_t mode;  /* 0, VCD_SOURCE, VCD_TARGET */
00572   usize_t size;
00573   xoff_t  addr;
00574   xoff_t  position;  /* absolute position of this inst */
00575 };
00576 
00577 /* used by the encoder to buffer output in sections.  list of blocks. */
00578 struct _xd3_output
00579 {
00580   uint8_t    *base;
00581   usize_t     next;
00582   usize_t     avail;
00583   xd3_output *next_page;
00584 };
00585 
00586 /* used by the decoder to buffer input in sections. */
00587 struct _xd3_desect
00588 {
00589   const uint8_t *buf;
00590   const uint8_t *buf_max;
00591   uint32_t       size;  /* TODO: why decode breaks if this is usize_t? */
00592   usize_t        pos;
00593 
00594   /* used in xdelta3-decode.h */
00595   uint8_t       *copied1;
00596   usize_t        alloc1;
00597 
00598   /* used in xdelta3-second.h */
00599   uint8_t       *copied2;
00600   usize_t        alloc2;
00601 };
00602 
00603 /* the VCDIFF address cache, see the RFC */
00604 struct _xd3_addr_cache
00605 {
00606   usize_t  s_near;
00607   usize_t  s_same;
00608   usize_t  next_slot;  /* the circular index for near */
00609   usize_t *near_array; /* array of size s_near        */
00610   usize_t *same_array; /* array of size s_same*256    */
00611 };
00612 
00613 /* the IOPT buffer list is just a list of buffers, which may be allocated
00614  * during encode when using an unlimited buffer. */
00615 struct _xd3_iopt_buflist
00616 {
00617   xd3_rinst *buffer;
00618   xd3_iopt_buflist *next;
00619 };
00620 
00621 /* This is the record of a pre-compiled configuration, a subset of
00622    xd3_config. */
00623 struct _xd3_smatcher
00624 {
00625   const char        *name;
00626   int             (*string_match) (xd3_stream  *stream);
00627   usize_t            large_look;
00628   usize_t            large_step;
00629   usize_t            small_look;
00630   usize_t            small_chain;
00631   usize_t            small_lchain;
00632   usize_t            max_lazy;
00633   usize_t            long_enough;
00634 };
00635 
00636 /* hash table size & power-of-two hash function. */
00637 struct _xd3_hash_cfg
00638 {
00639   usize_t           size;
00640   usize_t           shift;
00641   usize_t           mask;
00642 };
00643 
00644 /* the sprev list */
00645 struct _xd3_slist
00646 {
00647   usize_t     last_pos;
00648 };
00649 
00650 /* window info (for whole state) */
00651 struct _xd3_wininfo {
00652   xoff_t offset;
00653   usize_t length;
00654   uint32_t adler32;
00655 };
00656 
00657 /* whole state for, e.g., merge */
00658 struct _xd3_whole_state {
00659   usize_t addslen;
00660   uint8_t *adds;
00661   usize_t  adds_alloc;
00662 
00663   usize_t instlen;
00664   xd3_winst *inst;
00665   usize_t  inst_alloc;
00666 
00667   usize_t wininfolen;
00668   xd3_wininfo *wininfo;
00669   usize_t wininfo_alloc;
00670 
00671   xoff_t length;
00672 };
00673 
00674 /********************************************************************
00675  public types
00676  *******************************************************************/
00677 
00678 /* Settings for the secondary compressor. */
00679 struct _xd3_sec_cfg
00680 {
00681   int                data_type;     /* Which section. (set automatically) */
00682   usize_t            ngroups;       /* Number of DJW Huffman groups. */
00683   usize_t            sector_size;   /* Sector size. */
00684   int                inefficient;   /* If true, ignore efficiency check [avoid XD3_NOSECOND]. */
00685 };
00686 
00687 /* This is the user-visible stream configuration. */
00688 struct _xd3_config
00689 {
00690   usize_t             winsize;       /* The encoder window size. */
00691   usize_t             sprevsz;       /* How far back small string
00692                                         matching goes */
00693   usize_t             iopt_size;     /* entries in the
00694                                         instruction-optimizing
00695                                         buffer */
00696   usize_t             srcwin_maxsz;  /* srcwin_size grows by a factor
00697                                         of 2 when no matches are
00698                                         found.  encoder will not seek
00699                                         back further than this. */
00700 
00701   xd3_getblk_func   *getblk;        /* The three callbacks. */
00702   xd3_alloc_func    *alloc;
00703   xd3_free_func     *freef;
00704   void              *opaque;        /* Not used. */
00705   int                flags;         /* stream->flags are initialized
00706                                      * from xd3_config & never
00707                                      * modified by the library.  Use
00708                                      * xd3_set_flags to modify flags
00709                                      * settings mid-stream. */
00710 
00711   xd3_sec_cfg       sec_data;       /* Secondary compressor config: data */
00712   xd3_sec_cfg       sec_inst;       /* Secondary compressor config: inst */
00713   xd3_sec_cfg       sec_addr;       /* Secondary compressor config: addr */
00714 
00715   xd3_smatch_cfg     smatch_cfg;    /* See enum: use fields below  for
00716                                        soft config */
00717   xd3_smatcher       smatcher_soft;
00718 };
00719 
00720 /* The primary source file object. You create one of these objects and
00721  * initialize the first four fields.  This library maintains the next
00722  * 5 fields.  The configured getblk implementation is responsible for
00723  * setting the final 3 fields when called (and/or when XD3_GETSRCBLK
00724  * is returned).
00725  */
00726 struct _xd3_source
00727 {
00728   /* you set */
00729   usize_t             blksize;       /* block size */
00730   const char         *name;          /* its name, for debug/print
00731                                         purposes */
00732   void               *ioh;           /* opaque handle */
00733 
00734   /* getblk sets */
00735   xoff_t              curblkno;      /* current block number: client
00736                                         sets after getblk request */
00737   usize_t             onblk;         /* number of bytes on current
00738                                         block: client sets,  must be >= 0
00739                                         and <= blksize */
00740   const uint8_t      *curblk;        /* current block array: client
00741                                         sets after getblk request */
00742 
00743   /* xd3 sets */
00744   usize_t             srclen;        /* length of this source window */
00745   xoff_t              srcbase;       /* offset of this source window
00746                                         in the source itself */
00747   int                 shiftby;       /* for power-of-two blocksizes */
00748   int                 maskby;        /* for power-of-two blocksizes */
00749   xoff_t              cpyoff_blocks; /* offset of dec_cpyoff in blocks */
00750   usize_t             cpyoff_blkoff; /* offset of copy window in
00751                                         blocks, remainder */
00752   xoff_t              getblkno;      /* request block number: xd3 sets
00753                                         current getblk request */
00754 
00755   /* See xd3_getblk() */
00756   xoff_t              max_blkno;  /* Maximum block, if eof is known,
00757                                    * otherwise, equals frontier_blkno
00758                                    * (initially 0). */
00759   xoff_t              frontier_blkno;  /* If eof is unknown, the next
00760                                         * source position to be read.
00761                                         * Otherwise, equal to
00762                                         * max_blkno. */
00763   usize_t             onlastblk;  /* Number of bytes on max_blkno */
00764   int                 eof_known;  /* Set to true when the first
00765                                    * partial block is read. */
00766 };
00767 
00768 /* The primary xd3_stream object, used for encoding and decoding.  You
00769  * may access only two fields: avail_out, next_out.  Use the methods
00770  * above to operate on xd3_stream. */
00771 struct _xd3_stream
00772 {
00773   /* input state */
00774   const uint8_t    *next_in;          /* next input byte */
00775   usize_t           avail_in;         /* number of bytes available at
00776                                          next_in */
00777   xoff_t            total_in;         /* how many bytes in */
00778 
00779   /* output state */
00780   uint8_t          *next_out;         /* next output byte */
00781   usize_t           avail_out;        /* number of bytes available at
00782                                          next_out */
00783   usize_t           space_out;        /* total out space */
00784   xoff_t            current_window;   /* number of windows encoded/decoded */
00785   xoff_t            total_out;        /* how many bytes out */
00786 
00787   /* to indicate an error, xd3 sets */
00788   const char       *msg;              /* last error message, NULL if
00789                                          no error */
00790 
00791   /* source configuration */
00792   xd3_source       *src;              /* source array */
00793 
00794   /* encoder memory configuration */
00795   usize_t           winsize;          /* suggested window size */
00796   usize_t           sprevsz;          /* small string, previous window
00797                                          size (power of 2) */
00798   usize_t           sprevmask;        /* small string, previous window
00799                                          size mask */
00800   usize_t           iopt_size;
00801   usize_t           iopt_unlimited;
00802   usize_t           srcwin_maxsz;
00803 
00804   /* general configuration */
00805   xd3_getblk_func  *getblk;           /* set nxtblk, nxtblkno to scanblkno */
00806   xd3_alloc_func   *alloc;            /* malloc function */
00807   xd3_free_func    *free;             /* free function */
00808   void*             opaque;           /* private data object passed to
00809                                          alloc, free, and getblk */
00810   int               flags;            /* various options */
00811 
00812   /* secondary compressor configuration */
00813   xd3_sec_cfg       sec_data;         /* Secondary compressor config: data */
00814   xd3_sec_cfg       sec_inst;         /* Secondary compressor config: inst */
00815   xd3_sec_cfg       sec_addr;         /* Secondary compressor config: addr */
00816 
00817   xd3_smatcher      smatcher;
00818 
00819   usize_t           *large_table;      /* table of large checksums */
00820   xd3_hash_cfg       large_hash;       /* large hash config */
00821 
00822   usize_t           *small_table;      /* table of small checksums */
00823   xd3_slist         *small_prev;       /* table of previous offsets,
00824                                           circular linked list */
00825   int                small_reset;      /* true if small table should
00826                                           be reset */
00827 
00828   xd3_hash_cfg       small_hash;       /* small hash config */
00829   xd3_addr_cache     acache;           /* the vcdiff address cache */
00830   xd3_encode_state   enc_state;        /* state of the encoder */
00831 
00832   usize_t            taroff;           /* base offset of the target input */
00833   usize_t            input_position;   /* current input position */
00834   usize_t            min_match;        /* current minimum match
00835                                           length, avoids redundent
00836                                           matches */
00837   usize_t            unencoded_offset; /* current input, first
00838                                        * unencoded offset. this value
00839                                        * is <= the first instruction's
00840                                        * position in the iopt buffer,
00841                                        * if there is at least one
00842                                        * match in the buffer. */
00843 
00844   // SRCWIN
00845   // these variables plus srcwin_maxsz above (set by config)
00846   int                srcwin_decided;    /* boolean: true if srclen and
00847                                            srcbase have been
00848                                            decided. */
00849   int                srcwin_decided_early;  /* boolean: true if srclen
00850                                                and srcbase were
00851                                                decided early. */
00852   xoff_t             srcwin_cksum_pos;  /* Source checksum position */
00853 
00854   // MATCH
00855   xd3_match_state    match_state;      /* encoder match state */
00856   xoff_t             match_srcpos;     /* current match source
00857                                           position relative to
00858                                           srcbase */
00859   xoff_t             match_last_srcpos;  /* previously attempted
00860                                           * srcpos, to avoid loops. */
00861   xoff_t             match_minaddr;    /* smallest matching address to
00862                                        * set window params (reset each
00863                                        * window xd3_encode_reset) */
00864   xoff_t             match_maxaddr;    /* largest matching address to
00865                                        * set window params (reset each
00866                                        * window xd3_encode_reset) */
00867   usize_t            match_back;       /* match extends back so far */
00868   usize_t            match_maxback;    /* match extends back maximum */
00869   usize_t            match_fwd;        /* match extends forward so far */
00870   usize_t            match_maxfwd;     /* match extends forward maximum */
00871 
00872   xoff_t             maxsrcaddr;      /* address of the last source
00873                                          match (across windows) */
00874 
00875   uint8_t          *buf_in;           /* for saving buffered input */
00876   usize_t           buf_avail;        /* amount of saved input */
00877   const uint8_t    *buf_leftover;     /* leftover content of next_in
00878                                          (i.e., user's buffer) */
00879   usize_t            buf_leftavail;    /* amount of leftover content */
00880 
00881   xd3_output       *enc_current;      /* current output buffer */
00882   xd3_output       *enc_free;         /* free output buffers */
00883   xd3_output       *enc_heads[4];     /* array of encoded outputs:
00884                                          head of chain */
00885   xd3_output       *enc_tails[4];     /* array of encoded outputs:
00886                                          tail of chain */
00887   uint32_t          recode_adler32;   /* set the adler32 checksum
00888                                        * during "recode". */
00889 
00890   xd3_rlist         iopt_used;        /* instruction optimizing buffer */
00891   xd3_rlist         iopt_free;
00892   xd3_rinst        *iout;             /* next single instruction */
00893   xd3_iopt_buflist *iopt_alloc;
00894 
00895   const uint8_t    *enc_appheader;    /* application header to encode */
00896   usize_t            enc_appheadsz;    /* application header size */
00897 
00898   /* decoder stuff */
00899   xd3_decode_state  dec_state;        /* current DEC_XXX value */
00900   usize_t           dec_hdr_ind;      /* VCDIFF header indicator */
00901   usize_t           dec_win_ind;      /* VCDIFF window indicator */
00902   usize_t           dec_del_ind;      /* VCDIFF delta indicator */
00903 
00904   uint8_t           dec_magic[4];     /* First four bytes */
00905   usize_t           dec_magicbytes;   /* Magic position. */
00906 
00907   usize_t           dec_secondid;     /* Optional secondary compressor ID. */
00908 
00909   /* TODO: why decode breaks if this is usize_t? */
00910   uint32_t          dec_codetblsz;    /* Optional code table: length. */
00911   uint8_t          *dec_codetbl;      /* Optional code table: storage. */
00912   usize_t           dec_codetblbytes; /* Optional code table: position. */
00913 
00914   /* TODO: why decode breaks if this is usize_t? */
00915   uint32_t          dec_appheadsz;    /* Optional application header:
00916                                          size. */
00917   uint8_t          *dec_appheader;    /* Optional application header:
00918                                          storage */
00919   usize_t           dec_appheadbytes; /* Optional application header:
00920                                          position. */
00921 
00922   usize_t            dec_cksumbytes;   /* Optional checksum: position. */
00923   uint8_t           dec_cksum[4];     /* Optional checksum: storage. */
00924   uint32_t          dec_adler32;      /* Optional checksum: value. */
00925 
00926   /* TODO: why decode breaks if this is usize_t? */
00927   uint32_t           dec_cpylen;       /* length of copy window
00928                                           (VCD_SOURCE or VCD_TARGET) */
00929   xoff_t             dec_cpyoff;       /* offset of copy window
00930                                           (VCD_SOURCE or VCD_TARGET) */
00931   /* TODO: why decode breaks if this is usize_t? */
00932   uint32_t           dec_enclen;       /* length of delta encoding */
00933   /* TODO: why decode breaks if this is usize_t? */
00934   uint32_t           dec_tgtlen;       /* length of target window */
00935 
00936 #if USE_UINT64
00937   uint64_t          dec_64part;       /* part of a decoded uint64_t */
00938 #endif
00939 #if USE_UINT32
00940   uint32_t          dec_32part;       /* part of a decoded uint32_t */
00941 #endif
00942 
00943   xoff_t            dec_winstart;     /* offset of the start of
00944                                          current target window */
00945   xoff_t            dec_window_count; /* == current_window + 1 in
00946                                          DEC_FINISH */
00947   usize_t            dec_winbytes;     /* bytes of the three sections
00948                                           so far consumed */
00949   usize_t            dec_hdrsize;      /* VCDIFF + app header size */
00950 
00951   const uint8_t    *dec_tgtaddrbase;  /* Base of decoded target
00952                                          addresses (addr >=
00953                                          dec_cpylen). */
00954   const uint8_t    *dec_cpyaddrbase;  /* Base of decoded copy
00955                                          addresses (addr <
00956                                          dec_cpylen). */
00957 
00958   usize_t            dec_position;     /* current decoder position
00959                                           counting the cpylen
00960                                           offset */
00961   usize_t            dec_maxpos;       /* maximum decoder position
00962                                           counting the cpylen
00963                                           offset */
00964   xd3_hinst         dec_current1;     /* current instruction */
00965   xd3_hinst         dec_current2;     /* current instruction */
00966 
00967   uint8_t          *dec_buffer;       /* Decode buffer */
00968   uint8_t          *dec_lastwin;      /* In case of VCD_TARGET, the
00969                                          last target window. */
00970   usize_t            dec_lastlen;      /* length of the last target
00971                                           window */
00972   xoff_t            dec_laststart;    /* offset of the start of last
00973                                          target window */
00974   usize_t            dec_lastspace;    /* allocated space of last
00975                                           target window, for reuse */
00976 
00977   xd3_desect        inst_sect;        /* staging area for decoding
00978                                          window sections */
00979   xd3_desect        addr_sect;
00980   xd3_desect        data_sect;
00981 
00982   xd3_code_table_func       *code_table_func;
00983   xd3_comp_table_func       *comp_table_func;
00984   const xd3_dinst           *code_table;
00985   const xd3_code_table_desc *code_table_desc;
00986   xd3_dinst                 *code_table_alloc;
00987 
00988   /* secondary compression */
00989   const xd3_sec_type *sec_type;
00990   xd3_sec_stream     *sec_stream_d;
00991   xd3_sec_stream     *sec_stream_i;
00992   xd3_sec_stream     *sec_stream_a;
00993 
00994   /* state for reconstructing whole files (e.g., for merge), this only
00995    * supports loading USIZE_T_MAX instructions, adds, etc. */
00996   xd3_whole_state     whole_target;
00997 
00998   /* statistics */
00999   xoff_t            n_scpy;
01000   xoff_t            n_tcpy;
01001   xoff_t            n_add;
01002   xoff_t            n_run;
01003 
01004   xoff_t            l_scpy;
01005   xoff_t            l_tcpy;
01006   xoff_t            l_add;
01007   xoff_t            l_run;
01008 
01009   usize_t           i_slots_used;
01010 
01011 #if XD3_DEBUG
01012   usize_t            large_ckcnt;
01013 
01014   /* memory usage */
01015   usize_t            alloc_cnt;
01016   usize_t            free_cnt;
01017 #endif
01018 };
01019 
01020 /**************************************************************************
01021  PUBLIC FUNCTIONS
01022  **************************************************************************/
01023 
01024 /* This function configures an xd3_stream using the provided in-memory
01025  * input buffer, source buffer, output buffer, and flags.  The output
01026  * array must be large enough or else ENOSPC will be returned.  This
01027  * is the simplest in-memory encoding interface. */
01028 int     xd3_encode_memory (const uint8_t *input,
01029                            usize_t        input_size,
01030                            const uint8_t *source,
01031                            usize_t        source_size,
01032                            uint8_t       *output_buffer,
01033                            usize_t       *output_size,
01034                            usize_t        avail_output,
01035                            int            flags);
01036 
01037 /* The reverse of xd3_encode_memory. */
01038 int     xd3_decode_memory (const uint8_t *input,
01039                            usize_t        input_size,
01040                            const uint8_t *source,
01041                            usize_t        source_size,
01042                            uint8_t       *output_buf,
01043                            usize_t       *output_size,
01044                            usize_t        avail_output,
01045                            int            flags);
01046 
01047 /* This function encodes an in-memory input using a pre-configured
01048  * xd3_stream.  This allows the caller to set a variety of options
01049  * which are not available in the xd3_encode/decode_memory()
01050  * functions.
01051  *
01052  * The output array must be large enough to hold the output or else
01053  * ENOSPC is returned.  The source (if any) should be set using
01054  * xd3_set_source_and_size() with a single-block xd3_source.  This
01055  * calls the underlying non-blocking interfaces,
01056  * xd3_encode/decode_input(), handling the necessary input/output
01057  * states.  This method may be considered a reference for any
01058  * application using xd3_encode_input() directly.
01059  *
01060  *   xd3_stream stream;
01061  *   xd3_config config;
01062  *   xd3_source src;
01063  *
01064  *   memset (& src, 0, sizeof (src));
01065  *   memset (& stream, 0, sizeof (stream));
01066  *   memset (& config, 0, sizeof (config));
01067  *
01068  *   if (source != NULL)
01069  *     {
01070  *       src.size = source_size;
01071  *       src.blksize = source_size;
01072  *       src.curblkno = 0;
01073  *       src.onblk = source_size;
01074  *       src.curblk = source;
01075  *       xd3_set_source(&stream, &src);
01076  *     }
01077  *
01078  *   config.flags = flags;
01079  *   config.srcwin_maxsz = source_size;
01080  *   config.winsize = input_size;
01081  *
01082  *   ... set smatcher, appheader, encoding-table, compression-level, etc.
01083  *
01084  *   xd3_config_stream(&stream, &config);
01085  *   xd3_encode_stream(&stream, ...);
01086  *   xd3_free_stream(&stream);
01087  */
01088 int     xd3_encode_stream (xd3_stream    *stream,
01089                            const uint8_t *input,
01090                            usize_t         input_size,
01091                            uint8_t       *output,
01092                            usize_t        *output_size,
01093                            usize_t         avail_output);
01094 
01095 /* The reverse of xd3_encode_stream. */
01096 int     xd3_decode_stream (xd3_stream    *stream,
01097                            const uint8_t *input,
01098                            usize_t        input_size,
01099                            uint8_t       *output,
01100                            usize_t       *output_size,
01101                            usize_t        avail_size);
01102 
01103 /* This is the non-blocking interface.
01104  *
01105  * Handling input and output states is the same for encoding or
01106  * decoding using the xd3_avail_input() and xd3_consume_output()
01107  * routines, inlined below.
01108  *
01109  * Return values:
01110  *
01111  *   XD3_INPUT: the process requires more input: call
01112  *               xd3_avail_input() then repeat
01113  *
01114  *   XD3_OUTPUT: the process has more output: read stream->next_out,
01115  *               stream->avail_out, then call xd3_consume_output(),
01116  *               then repeat
01117  *
01118  *   XD3_GOTHEADER: (decoder-only) notification returned following the
01119  *               VCDIFF header and first window header.  the decoder
01120  *               may use the header to configure itself.
01121  *
01122  *   XD3_WINSTART: a general notification returned once for each
01123  *               window except the 0-th window, which is implied by
01124  *               XD3_GOTHEADER.  It is recommended to use a
01125  *               switch-stmt such as:
01126  *
01127  *                 ...
01128  *               again:
01129  *                 switch ((ret = xd3_decode_input (stream))) {
01130  *                    case XD3_GOTHEADER: {
01131  *                      assert(stream->current_window == 0);
01132  *                      stuff;
01133  *                    }
01134  *                    // fallthrough
01135  *                    case XD3_WINSTART: {
01136  *                      something(stream->current_window);
01137  *                      goto again;
01138  *                    }
01139  *                    ...
01140  *
01141  *   XD3_WINFINISH: a general notification, following the complete
01142  *               input & output of a window.  at this point,
01143  *               stream->total_in and stream->total_out are consistent
01144  *               for either encoding or decoding.
01145  *
01146  *   XD3_GETSRCBLK: If the xd3_getblk() callback is NULL, this value
01147  *               is returned to initiate a non-blocking source read.
01148  */
01149 int     xd3_decode_input  (xd3_stream    *stream);
01150 int     xd3_encode_input  (xd3_stream    *stream);
01151 
01152 /* The xd3_config structure is used to initialize a stream - all data
01153  * is copied into stream so config may be a temporary variable.  See
01154  * the [documentation] or comments on the xd3_config structure. */
01155 int     xd3_config_stream (xd3_stream    *stream,
01156                            xd3_config    *config);
01157 
01158 /* Since Xdelta3 doesn't open any files, xd3_close_stream is just an
01159  * error check that the stream is in a proper state to be closed: this
01160  * means the encoder is flushed and the decoder is at a window
01161  * boundary.  The application is responsible for freeing any of the
01162  * resources it supplied. */
01163 int     xd3_close_stream (xd3_stream    *stream);
01164 
01165 /* This arranges for closes the stream to succeed.  Does not free the
01166  * stream.*/
01167 void    xd3_abort_stream (xd3_stream    *stream);
01168 
01169 /* xd3_free_stream frees all memory allocated for the stream.  The
01170  * application is responsible for freeing any of the resources it
01171  * supplied. */
01172 void    xd3_free_stream   (xd3_stream    *stream);
01173 
01174 /* This function informs the encoder or decoder that source matching
01175  * (i.e., delta-compression) is possible.  For encoding, this should
01176  * be called before the first xd3_encode_input.  A NULL source is
01177  * ignored.  For decoding, this should be called before the first
01178  * window is decoded, but the appheader may be read first
01179  * (XD3_GOTHEADER).  After decoding the header, call xd3_set_source()
01180  * if you have a source file.  Note: if (stream->dec_win_ind & VCD_SOURCE)
01181  * is true, it means the first window expects there to be a source file.
01182  */
01183 int     xd3_set_source    (xd3_stream    *stream,
01184                            xd3_source    *source);
01185 
01186 /* If the source size is known, call this instead of xd3_set_source().
01187  * to avoid having stream->getblk called (and/or to avoid XD3_GETSRCBLK).
01188  *
01189  * Follow these steps:
01190   xd3_source source;
01191   memset(&source, 0, sizeof(source));
01192   source.blksize  = size;
01193   source.onblk    = size;
01194   source.curblk   = buf;
01195   source.curblkno = 0;
01196   int ret = xd3_set_source_and_size(&stream, &source, size);
01197   ...
01198  */
01199 int     xd3_set_source_and_size (xd3_stream    *stream,
01200                                  xd3_source    *source,
01201                                  xoff_t         source_size);
01202 
01203 /* This should be called before the first call to xd3_encode_input()
01204  * to include application-specific data in the VCDIFF header. */
01205 void    xd3_set_appheader (xd3_stream    *stream,
01206                            const uint8_t *data,
01207                            usize_t        size);
01208 
01209 /* xd3_get_appheader may be called in the decoder after XD3_GOTHEADER.
01210  * For convenience, the decoder always adds a single byte padding to
01211  * the end of the application header, which is set to zero in case the
01212  * application header is a string. */
01213 int     xd3_get_appheader (xd3_stream     *stream,
01214                            uint8_t       **data,
01215                            usize_t        *size);
01216 
01217 /* To generate a VCDIFF encoded delta with xd3_encode_init() from
01218  * another format, use:
01219  *
01220  *   xd3_encode_init_partial() -- initialze encoder state (w/o hash tables)
01221  *   xd3_init_cache() -- reset VCDIFF address cache
01222  *   xd3_found_match() -- to report a copy instruction
01223  *
01224  * set stream->enc_state to ENC_INSTR and call xd3_encode_input as usual.
01225  */
01226 int xd3_encode_init_partial (xd3_stream *stream);
01227 void xd3_init_cache (xd3_addr_cache* acache);
01228 int xd3_found_match (xd3_stream *stream,
01229                      usize_t pos, usize_t size,
01230                      xoff_t addr, int is_source);
01231 
01232 /* Gives an error string for xdelta3-speficic errors, returns NULL for
01233    system errors */
01234 const char* xd3_strerror (int ret);
01235 
01236 /* For convenience, zero & initialize the xd3_config structure with
01237    specified flags. */
01238 static inline
01239 void    xd3_init_config (xd3_config *config,
01240                          int         flags)
01241 {
01242   memset (config, 0, sizeof (*config));
01243   config->flags = flags;
01244 }
01245 
01246 /* This supplies some input to the stream.
01247  *
01248  * For encoding, if the input is larger than the configured window
01249  * size (xd3_config.winsize), the entire input will be consumed and
01250  * encoded anyway.  If you wish to strictly limit the window size,
01251  * limit the buffer passed to xd3_avail_input to the window size.
01252  *
01253  * For encoding, if the input is smaller than the configured window
01254  * size (xd3_config.winsize), the library will create a window-sized
01255  * buffer and accumulate input until a full-sized window can be
01256  * encoded.  XD3_INPUT will be returned.  The input must remain valid
01257  * until the next time xd3_encode_input() returns XD3_INPUT.
01258  *
01259  * For decoding, the input will be consumed entirely before XD3_INPUT
01260  * is returned again.
01261  */
01262 static inline
01263 void    xd3_avail_input  (xd3_stream    *stream,
01264                           const uint8_t *idata,
01265                           usize_t         isize)
01266 {
01267   /* Even if isize is zero, the code expects a non-NULL idata.  Why?
01268    * It uses this value to determine whether xd3_avail_input has ever
01269    * been called.  If xd3_encode_input is called before
01270    * xd3_avail_input it will return XD3_INPUT right away without
01271    * allocating a stream->winsize buffer.  This is to avoid an
01272    * unwanted allocation. */
01273   XD3_ASSERT (idata != NULL || isize == 0);
01274 
01275   stream->next_in  = idata;
01276   stream->avail_in = isize;
01277 }
01278 
01279 /* This acknowledges receipt of output data, must be called after any
01280  * XD3_OUTPUT return. */
01281 static inline
01282 void xd3_consume_output (xd3_stream  *stream)
01283 {
01284   stream->avail_out  = 0;
01285 }
01286 
01287 /* These are set for each XD3_WINFINISH return. */
01288 static inline
01289 int xd3_encoder_used_source (xd3_stream *stream) {
01290   return stream->src != NULL && stream->src->srclen > 0;
01291 }
01292 static inline
01293 xoff_t xd3_encoder_srcbase (xd3_stream *stream) {
01294   return stream->src->srcbase;
01295 }
01296 static inline
01297 usize_t xd3_encoder_srclen (xd3_stream *stream) {
01298   return stream->src->srclen;
01299 }
01300 
01301 /* Checks for legal flag changes. */
01302 static inline
01303 void xd3_set_flags (xd3_stream *stream, int flags)
01304 {
01305   /* The bitwise difference should contain only XD3_FLUSH or
01306      XD3_SKIP_WINDOW */
01307   XD3_ASSERT(((flags ^ stream->flags) & ~(XD3_FLUSH | XD3_SKIP_WINDOW)) == 0);
01308   stream->flags = flags;
01309 }
01310 
01311 /* Gives some extra information about the latest library error, if any
01312  * is known. */
01313 static inline
01314 const char* xd3_errstring (xd3_stream  *stream)
01315 {
01316   return stream->msg ? stream->msg : "";
01317 }
01318 
01319 
01320 /* 64-bit divisions are expensive, which is why we require a
01321  * power-of-two source->blksize.  To relax this restriction is
01322  * relatively easy, see the history for xd3_blksize_div(). */
01323 static inline
01324 void xd3_blksize_div (const xoff_t offset,
01325                       const xd3_source *source,
01326                       xoff_t *blkno,
01327                       usize_t *blkoff) {
01328   *blkno = (xoff_t) (offset >> source->shiftby);
01329   *blkoff = (usize_t) (offset & source->maskby);
01330   XD3_ASSERT (*blkoff < source->blksize);
01331 }
01332 
01333 static inline
01334 void xd3_blksize_add (xoff_t *blkno,
01335                       usize_t *blkoff,
01336                       const xd3_source *source,
01337                       const usize_t add)
01338 {
01339   usize_t blkdiff;
01340 
01341   /* Does not check for overflow, checked in xdelta3-decode.h. */
01342   *blkoff += add;
01343   blkdiff = *blkoff >> source->shiftby;
01344 
01345   if (blkdiff)
01346     {
01347       *blkno += blkdiff;
01348       *blkoff &= source->maskby;
01349     }
01350 
01351   XD3_ASSERT (*blkoff < source->blksize);
01352 }
01353 
01354 #endif /* _XDELTA3_H_ */