xdelta3.h 48 KB

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