"Fossies" - the Fresh Open Source Software Archive

Member "xdelta3-3.0.11/xdelta3.h" (8 Jan 2016, 47764 Bytes) of package /linux/misc/xdelta3-3.0.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "xdelta3.h" see the Fossies "Dox" file reference documentation.

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