1From 2b29ec569f8438a0307debd29873859ca6d407fc Mon Sep 17 00:00:00 2001
2From: Nick Terrell <terrelln@fb.com>
3Date: Mon, 17 Jul 2017 17:08:19 -0700
4Subject: [PATCH v5 2/5] lib: Add zstd modules
5
6Add zstd compression and decompression kernel modules.
7zstd offers a wide variety of compression speed and quality trade-offs.
8It can compress at speeds approaching lz4, and quality approaching lzma.
9zstd decompressions at speeds more than twice as fast as zlib, and
10decompression speed remains roughly the same across all compression levels.
11
12The code was ported from the upstream zstd source repository. The
13`linux/zstd.h` header was modified to match linux kernel style.
14The cross-platform and allocation code was stripped out. Instead zstd
15requires the caller to pass a preallocated workspace. The source files
16were clang-formatted [1] to match the Linux Kernel style as much as
17possible. Otherwise, the code was unmodified. We would like to avoid
18as much further manual modification to the source code as possible, so it
19will be easier to keep the kernel zstd up to date.
20
21I benchmarked zstd compression as a special character device. I ran zstd
22and zlib compression at several levels, as well as performing no
23compression, which measure the time spent copying the data to kernel space.
24Data is passed to the compressor 4096 B at a time. The benchmark file is
25located in the upstream zstd source repository under
26`contrib/linux-kernel/zstd_compress_test.c` [2].
27
28I ran the benchmarks on a Ubuntu 14.04 VM with 2 cores and 4 GiB of RAM.
29The VM is running on a MacBook Pro with a 3.1 GHz Intel Core i7 processor,
3016 GB of RAM, and a SSD. I benchmarked using `silesia.tar` [3], which is
31211,988,480 B large. Run the following commands for the benchmark:
32
33    sudo modprobe zstd_compress_test
34    sudo mknod zstd_compress_test c 245 0
35    sudo cp silesia.tar zstd_compress_test
36
37The time is reported by the time of the userland `cp`.
38The MB/s is computed with
39
40    1,536,217,008 B / time(buffer size, hash)
41
42which includes the time to copy from userland.
43The Adjusted MB/s is computed with
44
45    1,536,217,088 B / (time(buffer size, hash) - time(buffer size, none)).
46
47The memory reported is the amount of memory the compressor requests.
48
49| Method   | Size (B) | Time (s) | Ratio | MB/s    | Adj MB/s | Mem (MB) |
50|----------|----------|----------|-------|---------|----------|----------|
51| none     | 11988480 |    0.100 |     1 | 2119.88 |        - |        - |
52| zstd -1  | 73645762 |    1.044 | 2.878 |  203.05 |   224.56 |     1.23 |
53| zstd -3  | 66988878 |    1.761 | 3.165 |  120.38 |   127.63 |     2.47 |
54| zstd -5  | 65001259 |    2.563 | 3.261 |   82.71 |    86.07 |     2.86 |
55| zstd -10 | 60165346 |   13.242 | 3.523 |   16.01 |    16.13 |    13.22 |
56| zstd -15 | 58009756 |   47.601 | 3.654 |    4.45 |     4.46 |    21.61 |
57| zstd -19 | 54014593 |  102.835 | 3.925 |    2.06 |     2.06 |    60.15 |
58| zlib -1  | 77260026 |    2.895 | 2.744 |   73.23 |    75.85 |     0.27 |
59| zlib -3  | 72972206 |    4.116 | 2.905 |   51.50 |    52.79 |     0.27 |
60| zlib -6  | 68190360 |    9.633 | 3.109 |   22.01 |    22.24 |     0.27 |
61| zlib -9  | 67613382 |   22.554 | 3.135 |    9.40 |     9.44 |     0.27 |
62
63I benchmarked zstd decompression using the same method on the same machine.
64The benchmark file is located in the upstream zstd repo under
65`contrib/linux-kernel/zstd_decompress_test.c` [4]. The memory reported is
66the amount of memory required to decompress data compressed with the given
67compression level. If you know the maximum size of your input, you can
68reduce the memory usage of decompression irrespective of the compression
69level.
70
71| Method   | Time (s) | MB/s    | Adjusted MB/s | Memory (MB) |
72|----------|----------|---------|---------------|-------------|
73| none     |    0.025 | 8479.54 |             - |           - |
74| zstd -1  |    0.358 |  592.15 |        636.60 |        0.84 |
75| zstd -3  |    0.396 |  535.32 |        571.40 |        1.46 |
76| zstd -5  |    0.396 |  535.32 |        571.40 |        1.46 |
77| zstd -10 |    0.374 |  566.81 |        607.42 |        2.51 |
78| zstd -15 |    0.379 |  559.34 |        598.84 |        4.61 |
79| zstd -19 |    0.412 |  514.54 |        547.77 |        8.80 |
80| zlib -1  |    0.940 |  225.52 |        231.68 |        0.04 |
81| zlib -3  |    0.883 |  240.08 |        247.07 |        0.04 |
82| zlib -6  |    0.844 |  251.17 |        258.84 |        0.04 |
83| zlib -9  |    0.837 |  253.27 |        287.64 |        0.04 |
84
85Tested in userland using the test-suite in the zstd repo under
86`contrib/linux-kernel/test/UserlandTest.cpp` [5] by mocking the kernel
87functions. Fuzz tested using libfuzzer [6] with the fuzz harnesses under
88`contrib/linux-kernel/test/{RoundTripCrash.c,DecompressCrash.c}` [7] [8]
89with ASAN, UBSAN, and MSAN. Additionally, it was tested while testing the
90BtrFS and SquashFS patches coming next.
91
92[1] https://clang.llvm.org/docs/ClangFormat.html
93[2] https://github.com/facebook/zstd/blob/dev/contrib/linux-kernel/zstd_compress_test.c
94[3] http://sun.aei.polsl.pl/~sdeor/index.php?page=silesia
95[4] https://github.com/facebook/zstd/blob/dev/contrib/linux-kernel/zstd_decompress_test.c
96[5] https://github.com/facebook/zstd/blob/dev/contrib/linux-kernel/test/UserlandTest.cpp
97[6] http://llvm.org/docs/LibFuzzer.html
98[7] https://github.com/facebook/zstd/blob/dev/contrib/linux-kernel/test/RoundTripCrash.c
99[8] https://github.com/facebook/zstd/blob/dev/contrib/linux-kernel/test/DecompressCrash.c
100
101zstd source repository: https://github.com/facebook/zstd
102
103Signed-off-by: Nick Terrell <terrelln@fb.com>
104---
105v1 -> v2:
106- Use div_u64() for division of u64s
107- Reduce stack usage of ZSTD_compressSequences(), ZSTD_buildSeqTable(),
108  ZSTD_decompressSequencesLong(), FSE_buildDTable(), FSE_decompress_wksp(),
109  HUF_writeCTable(), HUF_readStats(), HUF_readCTable(),
110  HUF_compressWeights(), HUF_readDTableX2(), and HUF_readDTableX4()
111- No function uses more than 400 B of stack space
112
113v2 -> v3:
114- Work around gcc-7 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81388
115- Fix bug in dictionary compression from upstream commit cc1522351f
116
117v3 -> v4:
118- Fix minor compiler warnings
119
120v4 -> v5:
121- Fix rare compression bug from upstream commit 308047eb5d
122- Fix bug introduced in v3 when working around the gcc-7 bug
123
124 include/linux/zstd.h      | 1155 +++++++++++++++
125 lib/Kconfig               |    8 +
126 lib/Makefile              |    2 +
127 lib/zstd/Makefile         |   18 +
128 lib/zstd/bitstream.h      |  374 +++++
129 lib/zstd/compress.c       | 3482 +++++++++++++++++++++++++++++++++++++++++++++
130 lib/zstd/decompress.c     | 2526 ++++++++++++++++++++++++++++++++
131 lib/zstd/entropy_common.c |  243 ++++
132 lib/zstd/error_private.h  |   51 +
133 lib/zstd/fse.h            |  575 ++++++++
134 lib/zstd/fse_compress.c   |  795 +++++++++++
135 lib/zstd/fse_decompress.c |  332 +++++
136 lib/zstd/huf.h            |  212 +++
137 lib/zstd/huf_compress.c   |  770 ++++++++++
138 lib/zstd/huf_decompress.c |  960 +++++++++++++
139 lib/zstd/mem.h            |  149 ++
140 lib/zstd/zstd_common.c    |   73 +
141 lib/zstd/zstd_internal.h  |  261 ++++
142 lib/zstd/zstd_opt.h       | 1012 +++++++++++++
143 19 files changed, 12998 insertions(+)
144 create mode 100644 include/linux/zstd.h
145 create mode 100644 lib/zstd/Makefile
146 create mode 100644 lib/zstd/bitstream.h
147 create mode 100644 lib/zstd/compress.c
148 create mode 100644 lib/zstd/decompress.c
149 create mode 100644 lib/zstd/entropy_common.c
150 create mode 100644 lib/zstd/error_private.h
151 create mode 100644 lib/zstd/fse.h
152 create mode 100644 lib/zstd/fse_compress.c
153 create mode 100644 lib/zstd/fse_decompress.c
154 create mode 100644 lib/zstd/huf.h
155 create mode 100644 lib/zstd/huf_compress.c
156 create mode 100644 lib/zstd/huf_decompress.c
157 create mode 100644 lib/zstd/mem.h
158 create mode 100644 lib/zstd/zstd_common.c
159 create mode 100644 lib/zstd/zstd_internal.h
160 create mode 100644 lib/zstd/zstd_opt.h
161
162diff --git a/include/linux/zstd.h b/include/linux/zstd.h
163new file mode 100644
164index 0000000..305efd0
165--- /dev/null
166+++ b/include/linux/zstd.h
167@@ -0,0 +1,1155 @@
168+/*
169+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
170+ * All rights reserved.
171+ *
172+ * This source code is licensed under the BSD-style license found in the
173+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
174+ *
175+ * This program is free software; you can redistribute it and/or modify it under
176+ * the terms of the GNU General Public License version 2 as published by the
177+ * Free Software Foundation. This program is dual-licensed; you may select
178+ * either version 2 of the GNU General Public License ("GPL") or BSD license
179+ * ("BSD").
180+ */
181+
182+#ifndef ZSTD_H
183+#define ZSTD_H
184+
185+/* ======   Dependency   ======*/
186+#include <linux/types.h>   /* size_t */
187+
188+
189+/*-*****************************************************************************
190+ * Introduction
191+ *
192+ * zstd, short for Zstandard, is a fast lossless compression algorithm,
193+ * targeting real-time compression scenarios at zlib-level and better
194+ * compression ratios. The zstd compression library provides in-memory
195+ * compression and decompression functions. The library supports compression
196+ * levels from 1 up to ZSTD_maxCLevel() which is 22. Levels >= 20, labeled
197+ * ultra, should be used with caution, as they require more memory.
198+ * Compression can be done in:
199+ *  - a single step, reusing a context (described as Explicit memory management)
200+ *  - unbounded multiple steps (described as Streaming compression)
201+ * The compression ratio achievable on small data can be highly improved using
202+ * compression with a dictionary in:
203+ *  - a single step (described as Simple dictionary API)
204+ *  - a single step, reusing a dictionary (described as Fast dictionary API)
205+ ******************************************************************************/
206+
207+/*======  Helper functions  ======*/
208+
209+/**
210+ * enum ZSTD_ErrorCode - zstd error codes
211+ *
212+ * Functions that return size_t can be checked for errors using ZSTD_isError()
213+ * and the ZSTD_ErrorCode can be extracted using ZSTD_getErrorCode().
214+ */
215+typedef enum {
216+	ZSTD_error_no_error,
217+	ZSTD_error_GENERIC,
218+	ZSTD_error_prefix_unknown,
219+	ZSTD_error_version_unsupported,
220+	ZSTD_error_parameter_unknown,
221+	ZSTD_error_frameParameter_unsupported,
222+	ZSTD_error_frameParameter_unsupportedBy32bits,
223+	ZSTD_error_frameParameter_windowTooLarge,
224+	ZSTD_error_compressionParameter_unsupported,
225+	ZSTD_error_init_missing,
226+	ZSTD_error_memory_allocation,
227+	ZSTD_error_stage_wrong,
228+	ZSTD_error_dstSize_tooSmall,
229+	ZSTD_error_srcSize_wrong,
230+	ZSTD_error_corruption_detected,
231+	ZSTD_error_checksum_wrong,
232+	ZSTD_error_tableLog_tooLarge,
233+	ZSTD_error_maxSymbolValue_tooLarge,
234+	ZSTD_error_maxSymbolValue_tooSmall,
235+	ZSTD_error_dictionary_corrupted,
236+	ZSTD_error_dictionary_wrong,
237+	ZSTD_error_dictionaryCreation_failed,
238+	ZSTD_error_maxCode
239+} ZSTD_ErrorCode;
240+
241+/**
242+ * ZSTD_maxCLevel() - maximum compression level available
243+ *
244+ * Return: Maximum compression level available.
245+ */
246+int ZSTD_maxCLevel(void);
247+/**
248+ * ZSTD_compressBound() - maximum compressed size in worst case scenario
249+ * @srcSize: The size of the data to compress.
250+ *
251+ * Return:   The maximum compressed size in the worst case scenario.
252+ */
253+size_t ZSTD_compressBound(size_t srcSize);
254+/**
255+ * ZSTD_isError() - tells if a size_t function result is an error code
256+ * @code:  The function result to check for error.
257+ *
258+ * Return: Non-zero iff the code is an error.
259+ */
260+static __attribute__((unused)) unsigned int ZSTD_isError(size_t code)
261+{
262+	return code > (size_t)-ZSTD_error_maxCode;
263+}
264+/**
265+ * ZSTD_getErrorCode() - translates an error function result to a ZSTD_ErrorCode
266+ * @functionResult: The result of a function for which ZSTD_isError() is true.
267+ *
268+ * Return:          The ZSTD_ErrorCode corresponding to the functionResult or 0
269+ *                  if the functionResult isn't an error.
270+ */
271+static __attribute__((unused)) ZSTD_ErrorCode ZSTD_getErrorCode(
272+	size_t functionResult)
273+{
274+	if (!ZSTD_isError(functionResult))
275+		return (ZSTD_ErrorCode)0;
276+	return (ZSTD_ErrorCode)(0 - functionResult);
277+}
278+
279+/**
280+ * enum ZSTD_strategy - zstd compression search strategy
281+ *
282+ * From faster to stronger.
283+ */
284+typedef enum {
285+	ZSTD_fast,
286+	ZSTD_dfast,
287+	ZSTD_greedy,
288+	ZSTD_lazy,
289+	ZSTD_lazy2,
290+	ZSTD_btlazy2,
291+	ZSTD_btopt,
292+	ZSTD_btopt2
293+} ZSTD_strategy;
294+
295+/**
296+ * struct ZSTD_compressionParameters - zstd compression parameters
297+ * @windowLog:    Log of the largest match distance. Larger means more
298+ *                compression, and more memory needed during decompression.
299+ * @chainLog:     Fully searched segment. Larger means more compression, slower,
300+ *                and more memory (useless for fast).
301+ * @hashLog:      Dispatch table. Larger means more compression,
302+ *                slower, and more memory.
303+ * @searchLog:    Number of searches. Larger means more compression and slower.
304+ * @searchLength: Match length searched. Larger means faster decompression,
305+ *                sometimes less compression.
306+ * @targetLength: Acceptable match size for optimal parser (only). Larger means
307+ *                more compression, and slower.
308+ * @strategy:     The zstd compression strategy.
309+ */
310+typedef struct {
311+	unsigned int windowLog;
312+	unsigned int chainLog;
313+	unsigned int hashLog;
314+	unsigned int searchLog;
315+	unsigned int searchLength;
316+	unsigned int targetLength;
317+	ZSTD_strategy strategy;
318+} ZSTD_compressionParameters;
319+
320+/**
321+ * struct ZSTD_frameParameters - zstd frame parameters
322+ * @contentSizeFlag: Controls whether content size will be present in the frame
323+ *                   header (when known).
324+ * @checksumFlag:    Controls whether a 32-bit checksum is generated at the end
325+ *                   of the frame for error detection.
326+ * @noDictIDFlag:    Controls whether dictID will be saved into the frame header
327+ *                   when using dictionary compression.
328+ *
329+ * The default value is all fields set to 0.
330+ */
331+typedef struct {
332+	unsigned int contentSizeFlag;
333+	unsigned int checksumFlag;
334+	unsigned int noDictIDFlag;
335+} ZSTD_frameParameters;
336+
337+/**
338+ * struct ZSTD_parameters - zstd parameters
339+ * @cParams: The compression parameters.
340+ * @fParams: The frame parameters.
341+ */
342+typedef struct {
343+	ZSTD_compressionParameters cParams;
344+	ZSTD_frameParameters fParams;
345+} ZSTD_parameters;
346+
347+/**
348+ * ZSTD_getCParams() - returns ZSTD_compressionParameters for selected level
349+ * @compressionLevel: The compression level from 1 to ZSTD_maxCLevel().
350+ * @estimatedSrcSize: The estimated source size to compress or 0 if unknown.
351+ * @dictSize:         The dictionary size or 0 if a dictionary isn't being used.
352+ *
353+ * Return:            The selected ZSTD_compressionParameters.
354+ */
355+ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel,
356+	unsigned long long estimatedSrcSize, size_t dictSize);
357+
358+/**
359+ * ZSTD_getParams() - returns ZSTD_parameters for selected level
360+ * @compressionLevel: The compression level from 1 to ZSTD_maxCLevel().
361+ * @estimatedSrcSize: The estimated source size to compress or 0 if unknown.
362+ * @dictSize:         The dictionary size or 0 if a dictionary isn't being used.
363+ *
364+ * The same as ZSTD_getCParams() except also selects the default frame
365+ * parameters (all zero).
366+ *
367+ * Return:            The selected ZSTD_parameters.
368+ */
369+ZSTD_parameters ZSTD_getParams(int compressionLevel,
370+	unsigned long long estimatedSrcSize, size_t dictSize);
371+
372+/*-*************************************
373+ * Explicit memory management
374+ **************************************/
375+
376+/**
377+ * ZSTD_CCtxWorkspaceBound() - amount of memory needed to initialize a ZSTD_CCtx
378+ * @cParams: The compression parameters to be used for compression.
379+ *
380+ * If multiple compression parameters might be used, the caller must call
381+ * ZSTD_CCtxWorkspaceBound() for each set of parameters and use the maximum
382+ * size.
383+ *
384+ * Return:   A lower bound on the size of the workspace that is passed to
385+ *           ZSTD_initCCtx().
386+ */
387+size_t ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters cParams);
388+
389+/**
390+ * struct ZSTD_CCtx - the zstd compression context
391+ *
392+ * When compressing many times it is recommended to allocate a context just once
393+ * and reuse it for each successive compression operation.
394+ */
395+typedef struct ZSTD_CCtx_s ZSTD_CCtx;
396+/**
397+ * ZSTD_initCCtx() - initialize a zstd compression context
398+ * @workspace:     The workspace to emplace the context into. It must outlive
399+ *                 the returned context.
400+ * @workspaceSize: The size of workspace. Use ZSTD_CCtxWorkspaceBound() to
401+ *                 determine how large the workspace must be.
402+ *
403+ * Return:         A compression context emplaced into workspace.
404+ */
405+ZSTD_CCtx *ZSTD_initCCtx(void *workspace, size_t workspaceSize);
406+
407+/**
408+ * ZSTD_compressCCtx() - compress src into dst
409+ * @ctx:         The context. Must have been initialized with a workspace at
410+ *               least as large as ZSTD_CCtxWorkspaceBound(params.cParams).
411+ * @dst:         The buffer to compress src into.
412+ * @dstCapacity: The size of the destination buffer. May be any size, but
413+ *               ZSTD_compressBound(srcSize) is guaranteed to be large enough.
414+ * @src:         The data to compress.
415+ * @srcSize:     The size of the data to compress.
416+ * @params:      The parameters to use for compression. See ZSTD_getParams().
417+ *
418+ * Return:       The compressed size or an error, which can be checked using
419+ *               ZSTD_isError().
420+ */
421+size_t ZSTD_compressCCtx(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity,
422+	const void *src, size_t srcSize, ZSTD_parameters params);
423+
424+/**
425+ * ZSTD_DCtxWorkspaceBound() - amount of memory needed to initialize a ZSTD_DCtx
426+ *
427+ * Return: A lower bound on the size of the workspace that is passed to
428+ *         ZSTD_initDCtx().
429+ */
430+size_t ZSTD_DCtxWorkspaceBound(void);
431+
432+/**
433+ * struct ZSTD_DCtx - the zstd decompression context
434+ *
435+ * When decompressing many times it is recommended to allocate a context just
436+ * once and reuse it for each successive decompression operation.
437+ */
438+typedef struct ZSTD_DCtx_s ZSTD_DCtx;
439+/**
440+ * ZSTD_initDCtx() - initialize a zstd decompression context
441+ * @workspace:     The workspace to emplace the context into. It must outlive
442+ *                 the returned context.
443+ * @workspaceSize: The size of workspace. Use ZSTD_DCtxWorkspaceBound() to
444+ *                 determine how large the workspace must be.
445+ *
446+ * Return:         A decompression context emplaced into workspace.
447+ */
448+ZSTD_DCtx *ZSTD_initDCtx(void *workspace, size_t workspaceSize);
449+
450+/**
451+ * ZSTD_decompressDCtx() - decompress zstd compressed src into dst
452+ * @ctx:         The decompression context.
453+ * @dst:         The buffer to decompress src into.
454+ * @dstCapacity: The size of the destination buffer. Must be at least as large
455+ *               as the decompressed size. If the caller cannot upper bound the
456+ *               decompressed size, then it's better to use the streaming API.
457+ * @src:         The zstd compressed data to decompress. Multiple concatenated
458+ *               frames and skippable frames are allowed.
459+ * @srcSize:     The exact size of the data to decompress.
460+ *
461+ * Return:       The decompressed size or an error, which can be checked using
462+ *               ZSTD_isError().
463+ */
464+size_t ZSTD_decompressDCtx(ZSTD_DCtx *ctx, void *dst, size_t dstCapacity,
465+	const void *src, size_t srcSize);
466+
467+/*-************************
468+ * Simple dictionary API
469+ **************************/
470+
471+/**
472+ * ZSTD_compress_usingDict() - compress src into dst using a dictionary
473+ * @ctx:         The context. Must have been initialized with a workspace at
474+ *               least as large as ZSTD_CCtxWorkspaceBound(params.cParams).
475+ * @dst:         The buffer to compress src into.
476+ * @dstCapacity: The size of the destination buffer. May be any size, but
477+ *               ZSTD_compressBound(srcSize) is guaranteed to be large enough.
478+ * @src:         The data to compress.
479+ * @srcSize:     The size of the data to compress.
480+ * @dict:        The dictionary to use for compression.
481+ * @dictSize:    The size of the dictionary.
482+ * @params:      The parameters to use for compression. See ZSTD_getParams().
483+ *
484+ * Compression using a predefined dictionary. The same dictionary must be used
485+ * during decompression.
486+ *
487+ * Return:       The compressed size or an error, which can be checked using
488+ *               ZSTD_isError().
489+ */
490+size_t ZSTD_compress_usingDict(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity,
491+	const void *src, size_t srcSize, const void *dict, size_t dictSize,
492+	ZSTD_parameters params);
493+
494+/**
495+ * ZSTD_decompress_usingDict() - decompress src into dst using a dictionary
496+ * @ctx:         The decompression context.
497+ * @dst:         The buffer to decompress src into.
498+ * @dstCapacity: The size of the destination buffer. Must be at least as large
499+ *               as the decompressed size. If the caller cannot upper bound the
500+ *               decompressed size, then it's better to use the streaming API.
501+ * @src:         The zstd compressed data to decompress. Multiple concatenated
502+ *               frames and skippable frames are allowed.
503+ * @srcSize:     The exact size of the data to decompress.
504+ * @dict:        The dictionary to use for decompression. The same dictionary
505+ *               must've been used to compress the data.
506+ * @dictSize:    The size of the dictionary.
507+ *
508+ * Return:       The decompressed size or an error, which can be checked using
509+ *               ZSTD_isError().
510+ */
511+size_t ZSTD_decompress_usingDict(ZSTD_DCtx *ctx, void *dst, size_t dstCapacity,
512+	const void *src, size_t srcSize, const void *dict, size_t dictSize);
513+
514+/*-**************************
515+ * Fast dictionary API
516+ ***************************/
517+
518+/**
519+ * ZSTD_CDictWorkspaceBound() - memory needed to initialize a ZSTD_CDict
520+ * @cParams: The compression parameters to be used for compression.
521+ *
522+ * Return:   A lower bound on the size of the workspace that is passed to
523+ *           ZSTD_initCDict().
524+ */
525+size_t ZSTD_CDictWorkspaceBound(ZSTD_compressionParameters cParams);
526+
527+/**
528+ * struct ZSTD_CDict - a digested dictionary to be used for compression
529+ */
530+typedef struct ZSTD_CDict_s ZSTD_CDict;
531+
532+/**
533+ * ZSTD_initCDict() - initialize a digested dictionary for compression
534+ * @dictBuffer:    The dictionary to digest. The buffer is referenced by the
535+ *                 ZSTD_CDict so it must outlive the returned ZSTD_CDict.
536+ * @dictSize:      The size of the dictionary.
537+ * @params:        The parameters to use for compression. See ZSTD_getParams().
538+ * @workspace:     The workspace. It must outlive the returned ZSTD_CDict.
539+ * @workspaceSize: The workspace size. Must be at least
540+ *                 ZSTD_CDictWorkspaceBound(params.cParams).
541+ *
542+ * When compressing multiple messages / blocks with the same dictionary it is
543+ * recommended to load it just once. The ZSTD_CDict merely references the
544+ * dictBuffer, so it must outlive the returned ZSTD_CDict.
545+ *
546+ * Return:         The digested dictionary emplaced into workspace.
547+ */
548+ZSTD_CDict *ZSTD_initCDict(const void *dictBuffer, size_t dictSize,
549+	ZSTD_parameters params, void *workspace, size_t workspaceSize);
550+
551+/**
552+ * ZSTD_compress_usingCDict() - compress src into dst using a ZSTD_CDict
553+ * @ctx:         The context. Must have been initialized with a workspace at
554+ *               least as large as ZSTD_CCtxWorkspaceBound(cParams) where
555+ *               cParams are the compression parameters used to initialize the
556+ *               cdict.
557+ * @dst:         The buffer to compress src into.
558+ * @dstCapacity: The size of the destination buffer. May be any size, but
559+ *               ZSTD_compressBound(srcSize) is guaranteed to be large enough.
560+ * @src:         The data to compress.
561+ * @srcSize:     The size of the data to compress.
562+ * @cdict:       The digested dictionary to use for compression.
563+ * @params:      The parameters to use for compression. See ZSTD_getParams().
564+ *
565+ * Compression using a digested dictionary. The same dictionary must be used
566+ * during decompression.
567+ *
568+ * Return:       The compressed size or an error, which can be checked using
569+ *               ZSTD_isError().
570+ */
571+size_t ZSTD_compress_usingCDict(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
572+	const void *src, size_t srcSize, const ZSTD_CDict *cdict);
573+
574+
575+/**
576+ * ZSTD_DDictWorkspaceBound() - memory needed to initialize a ZSTD_DDict
577+ *
578+ * Return:  A lower bound on the size of the workspace that is passed to
579+ *          ZSTD_initDDict().
580+ */
581+size_t ZSTD_DDictWorkspaceBound(void);
582+
583+/**
584+ * struct ZSTD_DDict - a digested dictionary to be used for decompression
585+ */
586+typedef struct ZSTD_DDict_s ZSTD_DDict;
587+
588+/**
589+ * ZSTD_initDDict() - initialize a digested dictionary for decompression
590+ * @dictBuffer:    The dictionary to digest. The buffer is referenced by the
591+ *                 ZSTD_DDict so it must outlive the returned ZSTD_DDict.
592+ * @dictSize:      The size of the dictionary.
593+ * @workspace:     The workspace. It must outlive the returned ZSTD_DDict.
594+ * @workspaceSize: The workspace size. Must be at least
595+ *                 ZSTD_DDictWorkspaceBound().
596+ *
597+ * When decompressing multiple messages / blocks with the same dictionary it is
598+ * recommended to load it just once. The ZSTD_DDict merely references the
599+ * dictBuffer, so it must outlive the returned ZSTD_DDict.
600+ *
601+ * Return:         The digested dictionary emplaced into workspace.
602+ */
603+ZSTD_DDict *ZSTD_initDDict(const void *dictBuffer, size_t dictSize,
604+	void *workspace, size_t workspaceSize);
605+
606+/**
607+ * ZSTD_decompress_usingDDict() - decompress src into dst using a ZSTD_DDict
608+ * @ctx:         The decompression context.
609+ * @dst:         The buffer to decompress src into.
610+ * @dstCapacity: The size of the destination buffer. Must be at least as large
611+ *               as the decompressed size. If the caller cannot upper bound the
612+ *               decompressed size, then it's better to use the streaming API.
613+ * @src:         The zstd compressed data to decompress. Multiple concatenated
614+ *               frames and skippable frames are allowed.
615+ * @srcSize:     The exact size of the data to decompress.
616+ * @ddict:       The digested dictionary to use for decompression. The same
617+ *               dictionary must've been used to compress the data.
618+ *
619+ * Return:       The decompressed size or an error, which can be checked using
620+ *               ZSTD_isError().
621+ */
622+size_t ZSTD_decompress_usingDDict(ZSTD_DCtx *dctx, void *dst,
623+	size_t dstCapacity, const void *src, size_t srcSize,
624+	const ZSTD_DDict *ddict);
625+
626+
627+/*-**************************
628+ * Streaming
629+ ***************************/
630+
631+/**
632+ * struct ZSTD_inBuffer - input buffer for streaming
633+ * @src:  Start of the input buffer.
634+ * @size: Size of the input buffer.
635+ * @pos:  Position where reading stopped. Will be updated.
636+ *        Necessarily 0 <= pos <= size.
637+ */
638+typedef struct ZSTD_inBuffer_s {
639+	const void *src;
640+	size_t size;
641+	size_t pos;
642+} ZSTD_inBuffer;
643+
644+/**
645+ * struct ZSTD_outBuffer - output buffer for streaming
646+ * @dst:  Start of the output buffer.
647+ * @size: Size of the output buffer.
648+ * @pos:  Position where writing stopped. Will be updated.
649+ *        Necessarily 0 <= pos <= size.
650+ */
651+typedef struct ZSTD_outBuffer_s {
652+	void *dst;
653+	size_t size;
654+	size_t pos;
655+} ZSTD_outBuffer;
656+
657+
658+
659+/*-*****************************************************************************
660+ * Streaming compression - HowTo
661+ *
662+ * A ZSTD_CStream object is required to track streaming operation.
663+ * Use ZSTD_initCStream() to initialize a ZSTD_CStream object.
664+ * ZSTD_CStream objects can be reused multiple times on consecutive compression
665+ * operations. It is recommended to re-use ZSTD_CStream in situations where many
666+ * streaming operations will be achieved consecutively. Use one separate
667+ * ZSTD_CStream per thread for parallel execution.
668+ *
669+ * Use ZSTD_compressStream() repetitively to consume input stream.
670+ * The function will automatically update both `pos` fields.
671+ * Note that it may not consume the entire input, in which case `pos < size`,
672+ * and it's up to the caller to present again remaining data.
673+ * It returns a hint for the preferred number of bytes to use as an input for
674+ * the next function call.
675+ *
676+ * At any moment, it's possible to flush whatever data remains within internal
677+ * buffer, using ZSTD_flushStream(). `output->pos` will be updated. There might
678+ * still be some content left within the internal buffer if `output->size` is
679+ * too small. It returns the number of bytes left in the internal buffer and
680+ * must be called until it returns 0.
681+ *
682+ * ZSTD_endStream() instructs to finish a frame. It will perform a flush and
683+ * write frame epilogue. The epilogue is required for decoders to consider a
684+ * frame completed. Similar to ZSTD_flushStream(), it may not be able to flush
685+ * the full content if `output->size` is too small. In which case, call again
686+ * ZSTD_endStream() to complete the flush. It returns the number of bytes left
687+ * in the internal buffer and must be called until it returns 0.
688+ ******************************************************************************/
689+
690+/**
691+ * ZSTD_CStreamWorkspaceBound() - memory needed to initialize a ZSTD_CStream
692+ * @cParams: The compression parameters to be used for compression.
693+ *
694+ * Return:   A lower bound on the size of the workspace that is passed to
695+ *           ZSTD_initCStream() and ZSTD_initCStream_usingCDict().
696+ */
697+size_t ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters cParams);
698+
699+/**
700+ * struct ZSTD_CStream - the zstd streaming compression context
701+ */
702+typedef struct ZSTD_CStream_s ZSTD_CStream;
703+
704+/*===== ZSTD_CStream management functions =====*/
705+/**
706+ * ZSTD_initCStream() - initialize a zstd streaming compression context
707+ * @params:         The zstd compression parameters.
708+ * @pledgedSrcSize: If params.fParams.contentSizeFlag == 1 then the caller must
709+ *                  pass the source size (zero means empty source). Otherwise,
710+ *                  the caller may optionally pass the source size, or zero if
711+ *                  unknown.
712+ * @workspace:      The workspace to emplace the context into. It must outlive
713+ *                  the returned context.
714+ * @workspaceSize:  The size of workspace.
715+ *                  Use ZSTD_CStreamWorkspaceBound(params.cParams) to determine
716+ *                  how large the workspace must be.
717+ *
718+ * Return:          The zstd streaming compression context.
719+ */
720+ZSTD_CStream *ZSTD_initCStream(ZSTD_parameters params,
721+	unsigned long long pledgedSrcSize, void *workspace,
722+	size_t workspaceSize);
723+
724+/**
725+ * ZSTD_initCStream_usingCDict() - initialize a streaming compression context
726+ * @cdict:          The digested dictionary to use for compression.
727+ * @pledgedSrcSize: Optionally the source size, or zero if unknown.
728+ * @workspace:      The workspace to emplace the context into. It must outlive
729+ *                  the returned context.
730+ * @workspaceSize:  The size of workspace. Call ZSTD_CStreamWorkspaceBound()
731+ *                  with the cParams used to initialize the cdict to determine
732+ *                  how large the workspace must be.
733+ *
734+ * Return:          The zstd streaming compression context.
735+ */
736+ZSTD_CStream *ZSTD_initCStream_usingCDict(const ZSTD_CDict *cdict,
737+	unsigned long long pledgedSrcSize, void *workspace,
738+	size_t workspaceSize);
739+
740+/*===== Streaming compression functions =====*/
741+/**
742+ * ZSTD_resetCStream() - reset the context using parameters from creation
743+ * @zcs:            The zstd streaming compression context to reset.
744+ * @pledgedSrcSize: Optionally the source size, or zero if unknown.
745+ *
746+ * Resets the context using the parameters from creation. Skips dictionary
747+ * loading, since it can be reused. If `pledgedSrcSize` is non-zero the frame
748+ * content size is always written into the frame header.
749+ *
750+ * Return:          Zero or an error, which can be checked using ZSTD_isError().
751+ */
752+size_t ZSTD_resetCStream(ZSTD_CStream *zcs, unsigned long long pledgedSrcSize);
753+/**
754+ * ZSTD_compressStream() - streaming compress some of input into output
755+ * @zcs:    The zstd streaming compression context.
756+ * @output: Destination buffer. `output->pos` is updated to indicate how much
757+ *          compressed data was written.
758+ * @input:  Source buffer. `input->pos` is updated to indicate how much data was
759+ *          read. Note that it may not consume the entire input, in which case
760+ *          `input->pos < input->size`, and it's up to the caller to present
761+ *          remaining data again.
762+ *
763+ * The `input` and `output` buffers may be any size. Guaranteed to make some
764+ * forward progress if `input` and `output` are not empty.
765+ *
766+ * Return:  A hint for the number of bytes to use as the input for the next
767+ *          function call or an error, which can be checked using
768+ *          ZSTD_isError().
769+ */
770+size_t ZSTD_compressStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output,
771+	ZSTD_inBuffer *input);
772+/**
773+ * ZSTD_flushStream() - flush internal buffers into output
774+ * @zcs:    The zstd streaming compression context.
775+ * @output: Destination buffer. `output->pos` is updated to indicate how much
776+ *          compressed data was written.
777+ *
778+ * ZSTD_flushStream() must be called until it returns 0, meaning all the data
779+ * has been flushed. Since ZSTD_flushStream() causes a block to be ended,
780+ * calling it too often will degrade the compression ratio.
781+ *
782+ * Return:  The number of bytes still present within internal buffers or an
783+ *          error, which can be checked using ZSTD_isError().
784+ */
785+size_t ZSTD_flushStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output);
786+/**
787+ * ZSTD_endStream() - flush internal buffers into output and end the frame
788+ * @zcs:    The zstd streaming compression context.
789+ * @output: Destination buffer. `output->pos` is updated to indicate how much
790+ *          compressed data was written.
791+ *
792+ * ZSTD_endStream() must be called until it returns 0, meaning all the data has
793+ * been flushed and the frame epilogue has been written.
794+ *
795+ * Return:  The number of bytes still present within internal buffers or an
796+ *          error, which can be checked using ZSTD_isError().
797+ */
798+size_t ZSTD_endStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output);
799+
800+/**
801+ * ZSTD_CStreamInSize() - recommended size for the input buffer
802+ *
803+ * Return: The recommended size for the input buffer.
804+ */
805+size_t ZSTD_CStreamInSize(void);
806+/**
807+ * ZSTD_CStreamOutSize() - recommended size for the output buffer
808+ *
809+ * When the output buffer is at least this large, it is guaranteed to be large
810+ * enough to flush at least one complete compressed block.
811+ *
812+ * Return: The recommended size for the output buffer.
813+ */
814+size_t ZSTD_CStreamOutSize(void);
815+
816+
817+
818+/*-*****************************************************************************
819+ * Streaming decompression - HowTo
820+ *
821+ * A ZSTD_DStream object is required to track streaming operations.
822+ * Use ZSTD_initDStream() to initialize a ZSTD_DStream object.
823+ * ZSTD_DStream objects can be re-used multiple times.
824+ *
825+ * Use ZSTD_decompressStream() repetitively to consume your input.
826+ * The function will update both `pos` fields.
827+ * If `input->pos < input->size`, some input has not been consumed.
828+ * It's up to the caller to present again remaining data.
829+ * If `output->pos < output->size`, decoder has flushed everything it could.
830+ * Returns 0 iff a frame is completely decoded and fully flushed.
831+ * Otherwise it returns a suggested next input size that will never load more
832+ * than the current frame.
833+ ******************************************************************************/
834+
835+/**
836+ * ZSTD_DStreamWorkspaceBound() - memory needed to initialize a ZSTD_DStream
837+ * @maxWindowSize: The maximum window size allowed for compressed frames.
838+ *
839+ * Return:         A lower bound on the size of the workspace that is passed to
840+ *                 ZSTD_initDStream() and ZSTD_initDStream_usingDDict().
841+ */
842+size_t ZSTD_DStreamWorkspaceBound(size_t maxWindowSize);
843+
844+/**
845+ * struct ZSTD_DStream - the zstd streaming decompression context
846+ */
847+typedef struct ZSTD_DStream_s ZSTD_DStream;
848+/*===== ZSTD_DStream management functions =====*/
849+/**
850+ * ZSTD_initDStream() - initialize a zstd streaming decompression context
851+ * @maxWindowSize: The maximum window size allowed for compressed frames.
852+ * @workspace:     The workspace to emplace the context into. It must outlive
853+ *                 the returned context.
854+ * @workspaceSize: The size of workspace.
855+ *                 Use ZSTD_DStreamWorkspaceBound(maxWindowSize) to determine
856+ *                 how large the workspace must be.
857+ *
858+ * Return:         The zstd streaming decompression context.
859+ */
860+ZSTD_DStream *ZSTD_initDStream(size_t maxWindowSize, void *workspace,
861+	size_t workspaceSize);
862+/**
863+ * ZSTD_initDStream_usingDDict() - initialize streaming decompression context
864+ * @maxWindowSize: The maximum window size allowed for compressed frames.
865+ * @ddict:         The digested dictionary to use for decompression.
866+ * @workspace:     The workspace to emplace the context into. It must outlive
867+ *                 the returned context.
868+ * @workspaceSize: The size of workspace.
869+ *                 Use ZSTD_DStreamWorkspaceBound(maxWindowSize) to determine
870+ *                 how large the workspace must be.
871+ *
872+ * Return:         The zstd streaming decompression context.
873+ */
874+ZSTD_DStream *ZSTD_initDStream_usingDDict(size_t maxWindowSize,
875+	const ZSTD_DDict *ddict, void *workspace, size_t workspaceSize);
876+
877+/*===== Streaming decompression functions =====*/
878+/**
879+ * ZSTD_resetDStream() - reset the context using parameters from creation
880+ * @zds:   The zstd streaming decompression context to reset.
881+ *
882+ * Resets the context using the parameters from creation. Skips dictionary
883+ * loading, since it can be reused.
884+ *
885+ * Return: Zero or an error, which can be checked using ZSTD_isError().
886+ */
887+size_t ZSTD_resetDStream(ZSTD_DStream *zds);
888+/**
889+ * ZSTD_decompressStream() - streaming decompress some of input into output
890+ * @zds:    The zstd streaming decompression context.
891+ * @output: Destination buffer. `output.pos` is updated to indicate how much
892+ *          decompressed data was written.
893+ * @input:  Source buffer. `input.pos` is updated to indicate how much data was
894+ *          read. Note that it may not consume the entire input, in which case
895+ *          `input.pos < input.size`, and it's up to the caller to present
896+ *          remaining data again.
897+ *
898+ * The `input` and `output` buffers may be any size. Guaranteed to make some
899+ * forward progress if `input` and `output` are not empty.
900+ * ZSTD_decompressStream() will not consume the last byte of the frame until
901+ * the entire frame is flushed.
902+ *
903+ * Return:  Returns 0 iff a frame is completely decoded and fully flushed.
904+ *          Otherwise returns a hint for the number of bytes to use as the input
905+ *          for the next function call or an error, which can be checked using
906+ *          ZSTD_isError(). The size hint will never load more than the frame.
907+ */
908+size_t ZSTD_decompressStream(ZSTD_DStream *zds, ZSTD_outBuffer *output,
909+	ZSTD_inBuffer *input);
910+
911+/**
912+ * ZSTD_DStreamInSize() - recommended size for the input buffer
913+ *
914+ * Return: The recommended size for the input buffer.
915+ */
916+size_t ZSTD_DStreamInSize(void);
917+/**
918+ * ZSTD_DStreamOutSize() - recommended size for the output buffer
919+ *
920+ * When the output buffer is at least this large, it is guaranteed to be large
921+ * enough to flush at least one complete decompressed block.
922+ *
923+ * Return: The recommended size for the output buffer.
924+ */
925+size_t ZSTD_DStreamOutSize(void);
926+
927+
928+/* --- Constants ---*/
929+#define ZSTD_MAGICNUMBER            0xFD2FB528   /* >= v0.8.0 */
930+#define ZSTD_MAGIC_SKIPPABLE_START  0x184D2A50U
931+
932+#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
933+#define ZSTD_CONTENTSIZE_ERROR   (0ULL - 2)
934+
935+#define ZSTD_WINDOWLOG_MAX_32  27
936+#define ZSTD_WINDOWLOG_MAX_64  27
937+#define ZSTD_WINDOWLOG_MAX \
938+	((unsigned int)(sizeof(size_t) == 4 \
939+		? ZSTD_WINDOWLOG_MAX_32 \
940+		: ZSTD_WINDOWLOG_MAX_64))
941+#define ZSTD_WINDOWLOG_MIN 10
942+#define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX
943+#define ZSTD_HASHLOG_MIN        6
944+#define ZSTD_CHAINLOG_MAX     (ZSTD_WINDOWLOG_MAX+1)
945+#define ZSTD_CHAINLOG_MIN      ZSTD_HASHLOG_MIN
946+#define ZSTD_HASHLOG3_MAX      17
947+#define ZSTD_SEARCHLOG_MAX    (ZSTD_WINDOWLOG_MAX-1)
948+#define ZSTD_SEARCHLOG_MIN      1
949+/* only for ZSTD_fast, other strategies are limited to 6 */
950+#define ZSTD_SEARCHLENGTH_MAX   7
951+/* only for ZSTD_btopt, other strategies are limited to 4 */
952+#define ZSTD_SEARCHLENGTH_MIN   3
953+#define ZSTD_TARGETLENGTH_MIN   4
954+#define ZSTD_TARGETLENGTH_MAX 999
955+
956+/* for static allocation */
957+#define ZSTD_FRAMEHEADERSIZE_MAX 18
958+#define ZSTD_FRAMEHEADERSIZE_MIN  6
959+static const size_t ZSTD_frameHeaderSize_prefix = 5;
960+static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN;
961+static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
962+/* magic number + skippable frame length */
963+static const size_t ZSTD_skippableHeaderSize = 8;
964+
965+
966+/*-*************************************
967+ * Compressed size functions
968+ **************************************/
969+
970+/**
971+ * ZSTD_findFrameCompressedSize() - returns the size of a compressed frame
972+ * @src:     Source buffer. It should point to the start of a zstd encoded frame
973+ *           or a skippable frame.
974+ * @srcSize: The size of the source buffer. It must be at least as large as the
975+ *           size of the frame.
976+ *
977+ * Return:   The compressed size of the frame pointed to by `src` or an error,
978+ *           which can be check with ZSTD_isError().
979+ *           Suitable to pass to ZSTD_decompress() or similar functions.
980+ */
981+size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize);
982+
983+/*-*************************************
984+ * Decompressed size functions
985+ **************************************/
986+/**
987+ * ZSTD_getFrameContentSize() - returns the content size in a zstd frame header
988+ * @src:     It should point to the start of a zstd encoded frame.
989+ * @srcSize: The size of the source buffer. It must be at least as large as the
990+ *           frame header. `ZSTD_frameHeaderSize_max` is always large enough.
991+ *
992+ * Return:   The frame content size stored in the frame header if known.
993+ *           `ZSTD_CONTENTSIZE_UNKNOWN` if the content size isn't stored in the
994+ *           frame header. `ZSTD_CONTENTSIZE_ERROR` on invalid input.
995+ */
996+unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
997+
998+/**
999+ * ZSTD_findDecompressedSize() - returns decompressed size of a series of frames
1000+ * @src:     It should point to the start of a series of zstd encoded and/or
1001+ *           skippable frames.
1002+ * @srcSize: The exact size of the series of frames.
1003+ *
1004+ * If any zstd encoded frame in the series doesn't have the frame content size
1005+ * set, `ZSTD_CONTENTSIZE_UNKNOWN` is returned. But frame content size is always
1006+ * set when using ZSTD_compress(). The decompressed size can be very large.
1007+ * If the source is untrusted, the decompressed size could be wrong or
1008+ * intentionally modified. Always ensure the result fits within the
1009+ * application's authorized limits. ZSTD_findDecompressedSize() handles multiple
1010+ * frames, and so it must traverse the input to read each frame header. This is
1011+ * efficient as most of the data is skipped, however it does mean that all frame
1012+ * data must be present and valid.
1013+ *
1014+ * Return:   Decompressed size of all the data contained in the frames if known.
1015+ *           `ZSTD_CONTENTSIZE_UNKNOWN` if the decompressed size is unknown.
1016+ *           `ZSTD_CONTENTSIZE_ERROR` if an error occurred.
1017+ */
1018+unsigned long long ZSTD_findDecompressedSize(const void *src, size_t srcSize);
1019+
1020+/*-*************************************
1021+ * Advanced compression functions
1022+ **************************************/
1023+/**
1024+ * ZSTD_checkCParams() - ensure parameter values remain within authorized range
1025+ * @cParams: The zstd compression parameters.
1026+ *
1027+ * Return:   Zero or an error, which can be checked using ZSTD_isError().
1028+ */
1029+size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams);
1030+
1031+/**
1032+ * ZSTD_adjustCParams() - optimize parameters for a given srcSize and dictSize
1033+ * @srcSize:  Optionally the estimated source size, or zero if unknown.
1034+ * @dictSize: Optionally the estimated dictionary size, or zero if unknown.
1035+ *
1036+ * Return:    The optimized parameters.
1037+ */
1038+ZSTD_compressionParameters ZSTD_adjustCParams(
1039+	ZSTD_compressionParameters cParams, unsigned long long srcSize,
1040+	size_t dictSize);
1041+
1042+/*--- Advanced decompression functions ---*/
1043+
1044+/**
1045+ * ZSTD_isFrame() - returns true iff the buffer starts with a valid frame
1046+ * @buffer: The source buffer to check.
1047+ * @size:   The size of the source buffer, must be at least 4 bytes.
1048+ *
1049+ * Return: True iff the buffer starts with a zstd or skippable frame identifier.
1050+ */
1051+unsigned int ZSTD_isFrame(const void *buffer, size_t size);
1052+
1053+/**
1054+ * ZSTD_getDictID_fromDict() - returns the dictionary id stored in a dictionary
1055+ * @dict:     The dictionary buffer.
1056+ * @dictSize: The size of the dictionary buffer.
1057+ *
1058+ * Return:    The dictionary id stored within the dictionary or 0 if the
1059+ *            dictionary is not a zstd dictionary. If it returns 0 the
1060+ *            dictionary can still be loaded as a content-only dictionary.
1061+ */
1062+unsigned int ZSTD_getDictID_fromDict(const void *dict, size_t dictSize);
1063+
1064+/**
1065+ * ZSTD_getDictID_fromDDict() - returns the dictionary id stored in a ZSTD_DDict
1066+ * @ddict: The ddict to find the id of.
1067+ *
1068+ * Return: The dictionary id stored within `ddict` or 0 if the dictionary is not
1069+ *         a zstd dictionary. If it returns 0 `ddict` will be loaded as a
1070+ *         content-only dictionary.
1071+ */
1072+unsigned int ZSTD_getDictID_fromDDict(const ZSTD_DDict *ddict);
1073+
1074+/**
1075+ * ZSTD_getDictID_fromFrame() - returns the dictionary id stored in a zstd frame
1076+ * @src:     Source buffer. It must be a zstd encoded frame.
1077+ * @srcSize: The size of the source buffer. It must be at least as large as the
1078+ *           frame header. `ZSTD_frameHeaderSize_max` is always large enough.
1079+ *
1080+ * Return:   The dictionary id required to decompress the frame stored within
1081+ *           `src` or 0 if the dictionary id could not be decoded. It can return
1082+ *           0 if the frame does not require a dictionary, the dictionary id
1083+ *           wasn't stored in the frame, `src` is not a zstd frame, or `srcSize`
1084+ *           is too small.
1085+ */
1086+unsigned int ZSTD_getDictID_fromFrame(const void *src, size_t srcSize);
1087+
1088+/**
1089+ * struct ZSTD_frameParams - zstd frame parameters stored in the frame header
1090+ * @frameContentSize: The frame content size, or 0 if not present.
1091+ * @windowSize:       The window size, or 0 if the frame is a skippable frame.
1092+ * @dictID:           The dictionary id, or 0 if not present.
1093+ * @checksumFlag:     Whether a checksum was used.
1094+ */
1095+typedef struct {
1096+	unsigned long long frameContentSize;
1097+	unsigned int windowSize;
1098+	unsigned int dictID;
1099+	unsigned int checksumFlag;
1100+} ZSTD_frameParams;
1101+
1102+/**
1103+ * ZSTD_getFrameParams() - extracts parameters from a zstd or skippable frame
1104+ * @fparamsPtr: On success the frame parameters are written here.
1105+ * @src:        The source buffer. It must point to a zstd or skippable frame.
1106+ * @srcSize:    The size of the source buffer. `ZSTD_frameHeaderSize_max` is
1107+ *              always large enough to succeed.
1108+ *
1109+ * Return:      0 on success. If more data is required it returns how many bytes
1110+ *              must be provided to make forward progress. Otherwise it returns
1111+ *              an error, which can be checked using ZSTD_isError().
1112+ */
1113+size_t ZSTD_getFrameParams(ZSTD_frameParams *fparamsPtr, const void *src,
1114+	size_t srcSize);
1115+
1116+/*-*****************************************************************************
1117+ * Buffer-less and synchronous inner streaming functions
1118+ *
1119+ * This is an advanced API, giving full control over buffer management, for
1120+ * users which need direct control over memory.
1121+ * But it's also a complex one, with many restrictions (documented below).
1122+ * Prefer using normal streaming API for an easier experience
1123+ ******************************************************************************/
1124+
1125+/*-*****************************************************************************
1126+ * Buffer-less streaming compression (synchronous mode)
1127+ *
1128+ * A ZSTD_CCtx object is required to track streaming operations.
1129+ * Use ZSTD_initCCtx() to initialize a context.
1130+ * ZSTD_CCtx object can be re-used multiple times within successive compression
1131+ * operations.
1132+ *
1133+ * Start by initializing a context.
1134+ * Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary
1135+ * compression,
1136+ * or ZSTD_compressBegin_advanced(), for finer parameter control.
1137+ * It's also possible to duplicate a reference context which has already been
1138+ * initialized, using ZSTD_copyCCtx()
1139+ *
1140+ * Then, consume your input using ZSTD_compressContinue().
1141+ * There are some important considerations to keep in mind when using this
1142+ * advanced function :
1143+ * - ZSTD_compressContinue() has no internal buffer. It uses externally provided
1144+ *   buffer only.
1145+ * - Interface is synchronous : input is consumed entirely and produce 1+
1146+ *   (or more) compressed blocks.
1147+ * - Caller must ensure there is enough space in `dst` to store compressed data
1148+ *   under worst case scenario. Worst case evaluation is provided by
1149+ *   ZSTD_compressBound().
1150+ *   ZSTD_compressContinue() doesn't guarantee recover after a failed
1151+ *   compression.
1152+ * - ZSTD_compressContinue() presumes prior input ***is still accessible and
1153+ *   unmodified*** (up to maximum distance size, see WindowLog).
1154+ *   It remembers all previous contiguous blocks, plus one separated memory
1155+ *   segment (which can itself consists of multiple contiguous blocks)
1156+ * - ZSTD_compressContinue() detects that prior input has been overwritten when
1157+ *   `src` buffer overlaps. In which case, it will "discard" the relevant memory
1158+ *   section from its history.
1159+ *
1160+ * Finish a frame with ZSTD_compressEnd(), which will write the last block(s)
1161+ * and optional checksum. It's possible to use srcSize==0, in which case, it
1162+ * will write a final empty block to end the frame. Without last block mark,
1163+ * frames will be considered unfinished (corrupted) by decoders.
1164+ *
1165+ * `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress some new
1166+ * frame.
1167+ ******************************************************************************/
1168+
1169+/*=====   Buffer-less streaming compression functions  =====*/
1170+size_t ZSTD_compressBegin(ZSTD_CCtx *cctx, int compressionLevel);
1171+size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx *cctx, const void *dict,
1172+	size_t dictSize, int compressionLevel);
1173+size_t ZSTD_compressBegin_advanced(ZSTD_CCtx *cctx, const void *dict,
1174+	size_t dictSize, ZSTD_parameters params,
1175+	unsigned long long pledgedSrcSize);
1176+size_t ZSTD_copyCCtx(ZSTD_CCtx *cctx, const ZSTD_CCtx *preparedCCtx,
1177+	unsigned long long pledgedSrcSize);
1178+size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx *cctx, const ZSTD_CDict *cdict,
1179+	unsigned long long pledgedSrcSize);
1180+size_t ZSTD_compressContinue(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
1181+	const void *src, size_t srcSize);
1182+size_t ZSTD_compressEnd(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
1183+	const void *src, size_t srcSize);
1184+
1185+
1186+
1187+/*-*****************************************************************************
1188+ * Buffer-less streaming decompression (synchronous mode)
1189+ *
1190+ * A ZSTD_DCtx object is required to track streaming operations.
1191+ * Use ZSTD_initDCtx() to initialize a context.
1192+ * A ZSTD_DCtx object can be re-used multiple times.
1193+ *
1194+ * First typical operation is to retrieve frame parameters, using
1195+ * ZSTD_getFrameParams(). It fills a ZSTD_frameParams structure which provide
1196+ * important information to correctly decode the frame, such as the minimum
1197+ * rolling buffer size to allocate to decompress data (`windowSize`), and the
1198+ * dictionary ID used.
1199+ * Note: content size is optional, it may not be present. 0 means unknown.
1200+ * Note that these values could be wrong, either because of data malformation,
1201+ * or because an attacker is spoofing deliberate false information. As a
1202+ * consequence, check that values remain within valid application range,
1203+ * especially `windowSize`, before allocation. Each application can set its own
1204+ * limit, depending on local restrictions. For extended interoperability, it is
1205+ * recommended to support at least 8 MB.
1206+ * Frame parameters are extracted from the beginning of the compressed frame.
1207+ * Data fragment must be large enough to ensure successful decoding, typically
1208+ * `ZSTD_frameHeaderSize_max` bytes.
1209+ * Result: 0: successful decoding, the `ZSTD_frameParams` structure is filled.
1210+ *        >0: `srcSize` is too small, provide at least this many bytes.
1211+ *        errorCode, which can be tested using ZSTD_isError().
1212+ *
1213+ * Start decompression, with ZSTD_decompressBegin() or
1214+ * ZSTD_decompressBegin_usingDict(). Alternatively, you can copy a prepared
1215+ * context, using ZSTD_copyDCtx().
1216+ *
1217+ * Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue()
1218+ * alternatively.
1219+ * ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize'
1220+ * to ZSTD_decompressContinue().
1221+ * ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will
1222+ * fail.
1223+ *
1224+ * The result of ZSTD_decompressContinue() is the number of bytes regenerated
1225+ * within 'dst' (necessarily <= dstCapacity). It can be zero, which is not an
1226+ * error; it just means ZSTD_decompressContinue() has decoded some metadata
1227+ * item. It can also be an error code, which can be tested with ZSTD_isError().
1228+ *
1229+ * ZSTD_decompressContinue() needs previous data blocks during decompression, up
1230+ * to `windowSize`. They should preferably be located contiguously, prior to
1231+ * current block. Alternatively, a round buffer of sufficient size is also
1232+ * possible. Sufficient size is determined by frame parameters.
1233+ * ZSTD_decompressContinue() is very sensitive to contiguity, if 2 blocks don't
1234+ * follow each other, make sure that either the compressor breaks contiguity at
1235+ * the same place, or that previous contiguous segment is large enough to
1236+ * properly handle maximum back-reference.
1237+ *
1238+ * A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
1239+ * Context can then be reset to start a new decompression.
1240+ *
1241+ * Note: it's possible to know if next input to present is a header or a block,
1242+ * using ZSTD_nextInputType(). This information is not required to properly
1243+ * decode a frame.
1244+ *
1245+ * == Special case: skippable frames ==
1246+ *
1247+ * Skippable frames allow integration of user-defined data into a flow of
1248+ * concatenated frames. Skippable frames will be ignored (skipped) by a
1249+ * decompressor. The format of skippable frames is as follows:
1250+ * a) Skippable frame ID - 4 Bytes, Little endian format, any value from
1251+ *    0x184D2A50 to 0x184D2A5F
1252+ * b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
1253+ * c) Frame Content - any content (User Data) of length equal to Frame Size
1254+ * For skippable frames ZSTD_decompressContinue() always returns 0.
1255+ * For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0
1256+ * what means that a frame is skippable.
1257+ * Note: If fparamsPtr->frameContentSize==0, it is ambiguous: the frame might
1258+ *       actually be a zstd encoded frame with no content. For purposes of
1259+ *       decompression, it is valid in both cases to skip the frame using
1260+ *       ZSTD_findFrameCompressedSize() to find its size in bytes.
1261+ * It also returns frame size as fparamsPtr->frameContentSize.
1262+ ******************************************************************************/
1263+
1264+/*=====   Buffer-less streaming decompression functions  =====*/
1265+size_t ZSTD_decompressBegin(ZSTD_DCtx *dctx);
1266+size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx *dctx, const void *dict,
1267+	size_t dictSize);
1268+void   ZSTD_copyDCtx(ZSTD_DCtx *dctx, const ZSTD_DCtx *preparedDCtx);
1269+size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx *dctx);
1270+size_t ZSTD_decompressContinue(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity,
1271+	const void *src, size_t srcSize);
1272+typedef enum {
1273+	ZSTDnit_frameHeader,
1274+	ZSTDnit_blockHeader,
1275+	ZSTDnit_block,
1276+	ZSTDnit_lastBlock,
1277+	ZSTDnit_checksum,
1278+	ZSTDnit_skippableFrame
1279+} ZSTD_nextInputType_e;
1280+ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx *dctx);
1281+
1282+/*-*****************************************************************************
1283+ * Block functions
1284+ *
1285+ * Block functions produce and decode raw zstd blocks, without frame metadata.
1286+ * Frame metadata cost is typically ~18 bytes, which can be non-negligible for
1287+ * very small blocks (< 100 bytes). User will have to take in charge required
1288+ * information to regenerate data, such as compressed and content sizes.
1289+ *
1290+ * A few rules to respect:
1291+ * - Compressing and decompressing require a context structure
1292+ *   + Use ZSTD_initCCtx() and ZSTD_initDCtx()
1293+ * - It is necessary to init context before starting
1294+ *   + compression : ZSTD_compressBegin()
1295+ *   + decompression : ZSTD_decompressBegin()
1296+ *   + variants _usingDict() are also allowed
1297+ *   + copyCCtx() and copyDCtx() work too
1298+ * - Block size is limited, it must be <= ZSTD_getBlockSizeMax()
1299+ *   + If you need to compress more, cut data into multiple blocks
1300+ *   + Consider using the regular ZSTD_compress() instead, as frame metadata
1301+ *     costs become negligible when source size is large.
1302+ * - When a block is considered not compressible enough, ZSTD_compressBlock()
1303+ *   result will be zero. In which case, nothing is produced into `dst`.
1304+ *   + User must test for such outcome and deal directly with uncompressed data
1305+ *   + ZSTD_decompressBlock() doesn't accept uncompressed data as input!!!
1306+ *   + In case of multiple successive blocks, decoder must be informed of
1307+ *     uncompressed block existence to follow proper history. Use
1308+ *     ZSTD_insertBlock() in such a case.
1309+ ******************************************************************************/
1310+
1311+/* Define for static allocation */
1312+#define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024)
1313+/*=====   Raw zstd block functions  =====*/
1314+size_t ZSTD_getBlockSizeMax(ZSTD_CCtx *cctx);
1315+size_t ZSTD_compressBlock(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
1316+	const void *src, size_t srcSize);
1317+size_t ZSTD_decompressBlock(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity,
1318+	const void *src, size_t srcSize);
1319+size_t ZSTD_insertBlock(ZSTD_DCtx *dctx, const void *blockStart,
1320+	size_t blockSize);
1321+
1322+#endif  /* ZSTD_H */
1323diff --git a/lib/Kconfig b/lib/Kconfig
1324index 5e7541f..0d49ed0 100644
1325--- a/lib/Kconfig
1326+++ b/lib/Kconfig
1327@@ -249,6 +249,14 @@ config LZ4HC_COMPRESS
1328 config LZ4_DECOMPRESS
1329 	tristate
1330
1331+config ZSTD_COMPRESS
1332+	select XXHASH
1333+	tristate
1334+
1335+config ZSTD_DECOMPRESS
1336+	select XXHASH
1337+	tristate
1338+
1339 source "lib/xz/Kconfig"
1340
1341 #
1342diff --git a/lib/Makefile b/lib/Makefile
1343index d06b68a..d5c8a4f 100644
1344--- a/lib/Makefile
1345+++ b/lib/Makefile
1346@@ -116,6 +116,8 @@ obj-$(CONFIG_LZO_DECOMPRESS) += lzo/
1347 obj-$(CONFIG_LZ4_COMPRESS) += lz4/
1348 obj-$(CONFIG_LZ4HC_COMPRESS) += lz4/
1349 obj-$(CONFIG_LZ4_DECOMPRESS) += lz4/
1350+obj-$(CONFIG_ZSTD_COMPRESS) += zstd/
1351+obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd/
1352 obj-$(CONFIG_XZ_DEC) += xz/
1353 obj-$(CONFIG_RAID6_PQ) += raid6/
1354
1355diff --git a/lib/zstd/Makefile b/lib/zstd/Makefile
1356new file mode 100644
1357index 0000000..dd0a359
1358--- /dev/null
1359+++ b/lib/zstd/Makefile
1360@@ -0,0 +1,18 @@
1361+obj-$(CONFIG_ZSTD_COMPRESS) += zstd_compress.o
1362+obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd_decompress.o
1363+
1364+ccflags-y += -O3
1365+
1366+# Object files unique to zstd_compress and zstd_decompress
1367+zstd_compress-y := fse_compress.o huf_compress.o compress.o
1368+zstd_decompress-y := huf_decompress.o decompress.o
1369+
1370+# These object files are shared between the modules.
1371+# Always add them to zstd_compress.
1372+# Unless both zstd_compress and zstd_decompress are built in
1373+# then also add them to zstd_decompress.
1374+zstd_compress-y += entropy_common.o fse_decompress.o zstd_common.o
1375+
1376+ifneq ($(CONFIG_ZSTD_COMPRESS)$(CONFIG_ZSTD_DECOMPRESS),yy)
1377+	zstd_decompress-y += entropy_common.o fse_decompress.o zstd_common.o
1378+endif
1379diff --git a/lib/zstd/bitstream.h b/lib/zstd/bitstream.h
1380new file mode 100644
1381index 0000000..a826b99
1382--- /dev/null
1383+++ b/lib/zstd/bitstream.h
1384@@ -0,0 +1,374 @@
1385+/*
1386+ * bitstream
1387+ * Part of FSE library
1388+ * header file (to include)
1389+ * Copyright (C) 2013-2016, Yann Collet.
1390+ *
1391+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1392+ *
1393+ * Redistribution and use in source and binary forms, with or without
1394+ * modification, are permitted provided that the following conditions are
1395+ * met:
1396+ *
1397+ *   * Redistributions of source code must retain the above copyright
1398+ * notice, this list of conditions and the following disclaimer.
1399+ *   * Redistributions in binary form must reproduce the above
1400+ * copyright notice, this list of conditions and the following disclaimer
1401+ * in the documentation and/or other materials provided with the
1402+ * distribution.
1403+ *
1404+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1405+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1406+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1407+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1408+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1409+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1410+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1411+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1412+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1413+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1414+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1415+ *
1416+ * This program is free software; you can redistribute it and/or modify it under
1417+ * the terms of the GNU General Public License version 2 as published by the
1418+ * Free Software Foundation. This program is dual-licensed; you may select
1419+ * either version 2 of the GNU General Public License ("GPL") or BSD license
1420+ * ("BSD").
1421+ *
1422+ * You can contact the author at :
1423+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
1424+ */
1425+#ifndef BITSTREAM_H_MODULE
1426+#define BITSTREAM_H_MODULE
1427+
1428+/*
1429+*  This API consists of small unitary functions, which must be inlined for best performance.
1430+*  Since link-time-optimization is not available for all compilers,
1431+*  these functions are defined into a .h to be included.
1432+*/
1433+
1434+/*-****************************************
1435+*  Dependencies
1436+******************************************/
1437+#include "error_private.h" /* error codes and messages */
1438+#include "mem.h"	   /* unaligned access routines */
1439+
1440+/*=========================================
1441+*  Target specific
1442+=========================================*/
1443+#define STREAM_ACCUMULATOR_MIN_32 25
1444+#define STREAM_ACCUMULATOR_MIN_64 57
1445+#define STREAM_ACCUMULATOR_MIN ((U32)(ZSTD_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))
1446+
1447+/*-******************************************
1448+*  bitStream encoding API (write forward)
1449+********************************************/
1450+/* bitStream can mix input from multiple sources.
1451+*  A critical property of these streams is that they encode and decode in **reverse** direction.
1452+*  So the first bit sequence you add will be the last to be read, like a LIFO stack.
1453+*/
1454+typedef struct {
1455+	size_t bitContainer;
1456+	int bitPos;
1457+	char *startPtr;
1458+	char *ptr;
1459+	char *endPtr;
1460+} BIT_CStream_t;
1461+
1462+ZSTD_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *dstBuffer, size_t dstCapacity);
1463+ZSTD_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits);
1464+ZSTD_STATIC void BIT_flushBits(BIT_CStream_t *bitC);
1465+ZSTD_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC);
1466+
1467+/* Start with initCStream, providing the size of buffer to write into.
1468+*  bitStream will never write outside of this buffer.
1469+*  `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.
1470+*
1471+*  bits are first added to a local register.
1472+*  Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
1473+*  Writing data into memory is an explicit operation, performed by the flushBits function.
1474+*  Hence keep track how many bits are potentially stored into local register to avoid register overflow.
1475+*  After a flushBits, a maximum of 7 bits might still be stored into local register.
1476+*
1477+*  Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
1478+*
1479+*  Last operation is to close the bitStream.
1480+*  The function returns the final size of CStream in bytes.
1481+*  If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)
1482+*/
1483+
1484+/*-********************************************
1485+*  bitStream decoding API (read backward)
1486+**********************************************/
1487+typedef struct {
1488+	size_t bitContainer;
1489+	unsigned bitsConsumed;
1490+	const char *ptr;
1491+	const char *start;
1492+} BIT_DStream_t;
1493+
1494+typedef enum {
1495+	BIT_DStream_unfinished = 0,
1496+	BIT_DStream_endOfBuffer = 1,
1497+	BIT_DStream_completed = 2,
1498+	BIT_DStream_overflow = 3
1499+} BIT_DStream_status; /* result of BIT_reloadDStream() */
1500+/* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
1501+
1502+ZSTD_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize);
1503+ZSTD_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, unsigned nbBits);
1504+ZSTD_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD);
1505+ZSTD_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t *bitD);
1506+
1507+/* Start by invoking BIT_initDStream().
1508+*  A chunk of the bitStream is then stored into a local register.
1509+*  Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
1510+*  You can then retrieve bitFields stored into the local register, **in reverse order**.
1511+*  Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
1512+*  A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished.
1513+*  Otherwise, it can be less than that, so proceed accordingly.
1514+*  Checking if DStream has reached its end can be performed with BIT_endOfDStream().
1515+*/
1516+
1517+/*-****************************************
1518+*  unsafe API
1519+******************************************/
1520+ZSTD_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits);
1521+/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */
1522+
1523+ZSTD_STATIC void BIT_flushBitsFast(BIT_CStream_t *bitC);
1524+/* unsafe version; does not check buffer overflow */
1525+
1526+ZSTD_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, unsigned nbBits);
1527+/* faster, but works only if nbBits >= 1 */
1528+
1529+/*-**************************************************************
1530+*  Internal functions
1531+****************************************************************/
1532+ZSTD_STATIC unsigned BIT_highbit32(register U32 val) { return 31 - __builtin_clz(val); }
1533+
1534+/*=====    Local Constants   =====*/
1535+static const unsigned BIT_mask[] = {0,       1,       3,       7,	0xF,      0x1F,     0x3F,     0x7F,      0xFF,
1536+				    0x1FF,   0x3FF,   0x7FF,   0xFFF,    0x1FFF,   0x3FFF,   0x7FFF,   0xFFFF,    0x1FFFF,
1537+				    0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF}; /* up to 26 bits */
1538+
1539+/*-**************************************************************
1540+*  bitStream encoding
1541+****************************************************************/
1542+/*! BIT_initCStream() :
1543+ *  `dstCapacity` must be > sizeof(void*)
1544+ *  @return : 0 if success,
1545+			  otherwise an error code (can be tested using ERR_isError() ) */
1546+ZSTD_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *startPtr, size_t dstCapacity)
1547+{
1548+	bitC->bitContainer = 0;
1549+	bitC->bitPos = 0;
1550+	bitC->startPtr = (char *)startPtr;
1551+	bitC->ptr = bitC->startPtr;
1552+	bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr);
1553+	if (dstCapacity <= sizeof(bitC->ptr))
1554+		return ERROR(dstSize_tooSmall);
1555+	return 0;
1556+}
1557+
1558+/*! BIT_addBits() :
1559+	can add up to 26 bits into `bitC`.
1560+	Does not check for register overflow ! */
1561+ZSTD_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
1562+{
1563+	bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
1564+	bitC->bitPos += nbBits;
1565+}
1566+
1567+/*! BIT_addBitsFast() :
1568+ *  works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
1569+ZSTD_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits)
1570+{
1571+	bitC->bitContainer |= value << bitC->bitPos;
1572+	bitC->bitPos += nbBits;
1573+}
1574+
1575+/*! BIT_flushBitsFast() :
1576+ *  unsafe version; does not check buffer overflow */
1577+ZSTD_STATIC void BIT_flushBitsFast(BIT_CStream_t *bitC)
1578+{
1579+	size_t const nbBytes = bitC->bitPos >> 3;
1580+	ZSTD_writeLEST(bitC->ptr, bitC->bitContainer);
1581+	bitC->ptr += nbBytes;
1582+	bitC->bitPos &= 7;
1583+	bitC->bitContainer >>= nbBytes * 8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
1584+}
1585+
1586+/*! BIT_flushBits() :
1587+ *  safe version; check for buffer overflow, and prevents it.
1588+ *  note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */
1589+ZSTD_STATIC void BIT_flushBits(BIT_CStream_t *bitC)
1590+{
1591+	size_t const nbBytes = bitC->bitPos >> 3;
1592+	ZSTD_writeLEST(bitC->ptr, bitC->bitContainer);
1593+	bitC->ptr += nbBytes;
1594+	if (bitC->ptr > bitC->endPtr)
1595+		bitC->ptr = bitC->endPtr;
1596+	bitC->bitPos &= 7;
1597+	bitC->bitContainer >>= nbBytes * 8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
1598+}
1599+
1600+/*! BIT_closeCStream() :
1601+ *  @return : size of CStream, in bytes,
1602+			  or 0 if it could not fit into dstBuffer */
1603+ZSTD_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC)
1604+{
1605+	BIT_addBitsFast(bitC, 1, 1); /* endMark */
1606+	BIT_flushBits(bitC);
1607+
1608+	if (bitC->ptr >= bitC->endPtr)
1609+		return 0; /* doesn't fit within authorized budget : cancel */
1610+
1611+	return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
1612+}
1613+
1614+/*-********************************************************
1615+* bitStream decoding
1616+**********************************************************/
1617+/*! BIT_initDStream() :
1618+*   Initialize a BIT_DStream_t.
1619+*   `bitD` : a pointer to an already allocated BIT_DStream_t structure.
1620+*   `srcSize` must be the *exact* size of the bitStream, in bytes.
1621+*   @return : size of stream (== srcSize) or an errorCode if a problem is detected
1622+*/
1623+ZSTD_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize)
1624+{
1625+	if (srcSize < 1) {
1626+		memset(bitD, 0, sizeof(*bitD));
1627+		return ERROR(srcSize_wrong);
1628+	}
1629+
1630+	if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
1631+		bitD->start = (const char *)srcBuffer;
1632+		bitD->ptr = (const char *)srcBuffer + srcSize - sizeof(bitD->bitContainer);
1633+		bitD->bitContainer = ZSTD_readLEST(bitD->ptr);
1634+		{
1635+			BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1];
1636+			bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
1637+			if (lastByte == 0)
1638+				return ERROR(GENERIC); /* endMark not present */
1639+		}
1640+	} else {
1641+		bitD->start = (const char *)srcBuffer;
1642+		bitD->ptr = bitD->start;
1643+		bitD->bitContainer = *(const BYTE *)(bitD->start);
1644+		switch (srcSize) {
1645+		case 7: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[6]) << (sizeof(bitD->bitContainer) * 8 - 16);
1646+		case 6: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[5]) << (sizeof(bitD->bitContainer) * 8 - 24);
1647+		case 5: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[4]) << (sizeof(bitD->bitContainer) * 8 - 32);
1648+		case 4: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[3]) << 24;
1649+		case 3: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[2]) << 16;
1650+		case 2: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[1]) << 8;
1651+		default:;
1652+		}
1653+		{
1654+			BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1];
1655+			bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
1656+			if (lastByte == 0)
1657+				return ERROR(GENERIC); /* endMark not present */
1658+		}
1659+		bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize) * 8;
1660+	}
1661+
1662+	return srcSize;
1663+}
1664+
1665+ZSTD_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start) { return bitContainer >> start; }
1666+
1667+ZSTD_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits) { return (bitContainer >> start) & BIT_mask[nbBits]; }
1668+
1669+ZSTD_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) { return bitContainer & BIT_mask[nbBits]; }
1670+
1671+/*! BIT_lookBits() :
1672+ *  Provides next n bits from local register.
1673+ *  local register is not modified.
1674+ *  On 32-bits, maxNbBits==24.
1675+ *  On 64-bits, maxNbBits==56.
1676+ *  @return : value extracted
1677+ */
1678+ZSTD_STATIC size_t BIT_lookBits(const BIT_DStream_t *bitD, U32 nbBits)
1679+{
1680+	U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1;
1681+	return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask - nbBits) & bitMask);
1682+}
1683+
1684+/*! BIT_lookBitsFast() :
1685+*   unsafe version; only works only if nbBits >= 1 */
1686+ZSTD_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t *bitD, U32 nbBits)
1687+{
1688+	U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1;
1689+	return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask + 1) - nbBits) & bitMask);
1690+}
1691+
1692+ZSTD_STATIC void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits) { bitD->bitsConsumed += nbBits; }
1693+
1694+/*! BIT_readBits() :
1695+ *  Read (consume) next n bits from local register and update.
1696+ *  Pay attention to not read more than nbBits contained into local register.
1697+ *  @return : extracted value.
1698+ */
1699+ZSTD_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, U32 nbBits)
1700+{
1701+	size_t const value = BIT_lookBits(bitD, nbBits);
1702+	BIT_skipBits(bitD, nbBits);
1703+	return value;
1704+}
1705+
1706+/*! BIT_readBitsFast() :
1707+*   unsafe version; only works only if nbBits >= 1 */
1708+ZSTD_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, U32 nbBits)
1709+{
1710+	size_t const value = BIT_lookBitsFast(bitD, nbBits);
1711+	BIT_skipBits(bitD, nbBits);
1712+	return value;
1713+}
1714+
1715+/*! BIT_reloadDStream() :
1716+*   Refill `bitD` from buffer previously set in BIT_initDStream() .
1717+*   This function is safe, it guarantees it will not read beyond src buffer.
1718+*   @return : status of `BIT_DStream_t` internal register.
1719+			  if status == BIT_DStream_unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */
1720+ZSTD_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD)
1721+{
1722+	if (bitD->bitsConsumed > (sizeof(bitD->bitContainer) * 8)) /* should not happen => corruption detected */
1723+		return BIT_DStream_overflow;
1724+
1725+	if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
1726+		bitD->ptr -= bitD->bitsConsumed >> 3;
1727+		bitD->bitsConsumed &= 7;
1728+		bitD->bitContainer = ZSTD_readLEST(bitD->ptr);
1729+		return BIT_DStream_unfinished;
1730+	}
1731+	if (bitD->ptr == bitD->start) {
1732+		if (bitD->bitsConsumed < sizeof(bitD->bitContainer) * 8)
1733+			return BIT_DStream_endOfBuffer;
1734+		return BIT_DStream_completed;
1735+	}
1736+	{
1737+		U32 nbBytes = bitD->bitsConsumed >> 3;
1738+		BIT_DStream_status result = BIT_DStream_unfinished;
1739+		if (bitD->ptr - nbBytes < bitD->start) {
1740+			nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
1741+			result = BIT_DStream_endOfBuffer;
1742+		}
1743+		bitD->ptr -= nbBytes;
1744+		bitD->bitsConsumed -= nbBytes * 8;
1745+		bitD->bitContainer = ZSTD_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
1746+		return result;
1747+	}
1748+}
1749+
1750+/*! BIT_endOfDStream() :
1751+*   @return Tells if DStream has exactly reached its end (all bits consumed).
1752+*/
1753+ZSTD_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t *DStream)
1754+{
1755+	return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer) * 8));
1756+}
1757+
1758+#endif /* BITSTREAM_H_MODULE */
1759diff --git a/lib/zstd/compress.c b/lib/zstd/compress.c
1760new file mode 100644
1761index 0000000..ff18ae6
1762--- /dev/null
1763+++ b/lib/zstd/compress.c
1764@@ -0,0 +1,3482 @@
1765+/**
1766+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
1767+ * All rights reserved.
1768+ *
1769+ * This source code is licensed under the BSD-style license found in the
1770+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
1771+ *
1772+ * This program is free software; you can redistribute it and/or modify it under
1773+ * the terms of the GNU General Public License version 2 as published by the
1774+ * Free Software Foundation. This program is dual-licensed; you may select
1775+ * either version 2 of the GNU General Public License ("GPL") or BSD license
1776+ * ("BSD").
1777+ */
1778+
1779+/*-*************************************
1780+*  Dependencies
1781+***************************************/
1782+#include "fse.h"
1783+#include "huf.h"
1784+#include "mem.h"
1785+#include "zstd_internal.h" /* includes zstd.h */
1786+#include <linux/kernel.h>
1787+#include <linux/module.h>
1788+#include <linux/string.h> /* memset */
1789+
1790+/*-*************************************
1791+*  Constants
1792+***************************************/
1793+static const U32 g_searchStrength = 8; /* control skip over incompressible data */
1794+#define HASH_READ_SIZE 8
1795+typedef enum { ZSTDcs_created = 0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e;
1796+
1797+/*-*************************************
1798+*  Helper functions
1799+***************************************/
1800+size_t ZSTD_compressBound(size_t srcSize) { return FSE_compressBound(srcSize) + 12; }
1801+
1802+/*-*************************************
1803+*  Sequence storage
1804+***************************************/
1805+static void ZSTD_resetSeqStore(seqStore_t *ssPtr)
1806+{
1807+	ssPtr->lit = ssPtr->litStart;
1808+	ssPtr->sequences = ssPtr->sequencesStart;
1809+	ssPtr->longLengthID = 0;
1810+}
1811+
1812+/*-*************************************
1813+*  Context memory management
1814+***************************************/
1815+struct ZSTD_CCtx_s {
1816+	const BYTE *nextSrc;  /* next block here to continue on curr prefix */
1817+	const BYTE *base;     /* All regular indexes relative to this position */
1818+	const BYTE *dictBase; /* extDict indexes relative to this position */
1819+	U32 dictLimit;	/* below that point, need extDict */
1820+	U32 lowLimit;	 /* below that point, no more data */
1821+	U32 nextToUpdate;     /* index from which to continue dictionary update */
1822+	U32 nextToUpdate3;    /* index from which to continue dictionary update */
1823+	U32 hashLog3;	 /* dispatch table : larger == faster, more memory */
1824+	U32 loadedDictEnd;    /* index of end of dictionary */
1825+	U32 forceWindow;      /* force back-references to respect limit of 1<<wLog, even for dictionary */
1826+	U32 forceRawDict;     /* Force loading dictionary in "content-only" mode (no header analysis) */
1827+	ZSTD_compressionStage_e stage;
1828+	U32 rep[ZSTD_REP_NUM];
1829+	U32 repToConfirm[ZSTD_REP_NUM];
1830+	U32 dictID;
1831+	ZSTD_parameters params;
1832+	void *workSpace;
1833+	size_t workSpaceSize;
1834+	size_t blockSize;
1835+	U64 frameContentSize;
1836+	struct xxh64_state xxhState;
1837+	ZSTD_customMem customMem;
1838+
1839+	seqStore_t seqStore; /* sequences storage ptrs */
1840+	U32 *hashTable;
1841+	U32 *hashTable3;
1842+	U32 *chainTable;
1843+	HUF_CElt *hufTable;
1844+	U32 flagStaticTables;
1845+	HUF_repeat flagStaticHufTable;
1846+	FSE_CTable offcodeCTable[FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)];
1847+	FSE_CTable matchlengthCTable[FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)];
1848+	FSE_CTable litlengthCTable[FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)];
1849+	unsigned tmpCounters[HUF_COMPRESS_WORKSPACE_SIZE_U32];
1850+};
1851+
1852+size_t ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters cParams)
1853+{
1854+	size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << cParams.windowLog);
1855+	U32 const divider = (cParams.searchLength == 3) ? 3 : 4;
1856+	size_t const maxNbSeq = blockSize / divider;
1857+	size_t const tokenSpace = blockSize + 11 * maxNbSeq;
1858+	size_t const chainSize = (cParams.strategy == ZSTD_fast) ? 0 : (1 << cParams.chainLog);
1859+	size_t const hSize = ((size_t)1) << cParams.hashLog;
1860+	U32 const hashLog3 = (cParams.searchLength > 3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog);
1861+	size_t const h3Size = ((size_t)1) << hashLog3;
1862+	size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
1863+	size_t const optSpace =
1864+	    ((MaxML + 1) + (MaxLL + 1) + (MaxOff + 1) + (1 << Litbits)) * sizeof(U32) + (ZSTD_OPT_NUM + 1) * (sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t));
1865+	size_t const workspaceSize = tableSpace + (256 * sizeof(U32)) /* huffTable */ + tokenSpace +
1866+				     (((cParams.strategy == ZSTD_btopt) || (cParams.strategy == ZSTD_btopt2)) ? optSpace : 0);
1867+
1868+	return ZSTD_ALIGN(sizeof(ZSTD_stack)) + ZSTD_ALIGN(sizeof(ZSTD_CCtx)) + ZSTD_ALIGN(workspaceSize);
1869+}
1870+
1871+static ZSTD_CCtx *ZSTD_createCCtx_advanced(ZSTD_customMem customMem)
1872+{
1873+	ZSTD_CCtx *cctx;
1874+	if (!customMem.customAlloc || !customMem.customFree)
1875+		return NULL;
1876+	cctx = (ZSTD_CCtx *)ZSTD_malloc(sizeof(ZSTD_CCtx), customMem);
1877+	if (!cctx)
1878+		return NULL;
1879+	memset(cctx, 0, sizeof(ZSTD_CCtx));
1880+	cctx->customMem = customMem;
1881+	return cctx;
1882+}
1883+
1884+ZSTD_CCtx *ZSTD_initCCtx(void *workspace, size_t workspaceSize)
1885+{
1886+	ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
1887+	ZSTD_CCtx *cctx = ZSTD_createCCtx_advanced(stackMem);
1888+	if (cctx) {
1889+		cctx->workSpace = ZSTD_stackAllocAll(cctx->customMem.opaque, &cctx->workSpaceSize);
1890+	}
1891+	return cctx;
1892+}
1893+
1894+size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx)
1895+{
1896+	if (cctx == NULL)
1897+		return 0; /* support free on NULL */
1898+	ZSTD_free(cctx->workSpace, cctx->customMem);
1899+	ZSTD_free(cctx, cctx->customMem);
1900+	return 0; /* reserved as a potential error code in the future */
1901+}
1902+
1903+const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx) /* hidden interface */ { return &(ctx->seqStore); }
1904+
1905+static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx *cctx) { return cctx->params; }
1906+
1907+/** ZSTD_checkParams() :
1908+	ensure param values remain within authorized range.
1909+	@return : 0, or an error code if one value is beyond authorized range */
1910+size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams)
1911+{
1912+#define CLAMPCHECK(val, min, max)                                       \
1913+	{                                                               \
1914+		if ((val < min) | (val > max))                          \
1915+			return ERROR(compressionParameter_unsupported); \
1916+	}
1917+	CLAMPCHECK(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX);
1918+	CLAMPCHECK(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX);
1919+	CLAMPCHECK(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX);
1920+	CLAMPCHECK(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX);
1921+	CLAMPCHECK(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX);
1922+	CLAMPCHECK(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX);
1923+	if ((U32)(cParams.strategy) > (U32)ZSTD_btopt2)
1924+		return ERROR(compressionParameter_unsupported);
1925+	return 0;
1926+}
1927+
1928+/** ZSTD_cycleLog() :
1929+ *  condition for correct operation : hashLog > 1 */
1930+static U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat)
1931+{
1932+	U32 const btScale = ((U32)strat >= (U32)ZSTD_btlazy2);
1933+	return hashLog - btScale;
1934+}
1935+
1936+/** ZSTD_adjustCParams() :
1937+	optimize `cPar` for a given input (`srcSize` and `dictSize`).
1938+	mostly downsizing to reduce memory consumption and initialization.
1939+	Both `srcSize` and `dictSize` are optional (use 0 if unknown),
1940+	but if both are 0, no optimization can be done.
1941+	Note : cPar is considered validated at this stage. Use ZSTD_checkParams() to ensure that. */
1942+ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize)
1943+{
1944+	if (srcSize + dictSize == 0)
1945+		return cPar; /* no size information available : no adjustment */
1946+
1947+	/* resize params, to use less memory when necessary */
1948+	{
1949+		U32 const minSrcSize = (srcSize == 0) ? 500 : 0;
1950+		U64 const rSize = srcSize + dictSize + minSrcSize;
1951+		if (rSize < ((U64)1 << ZSTD_WINDOWLOG_MAX)) {
1952+			U32 const srcLog = MAX(ZSTD_HASHLOG_MIN, ZSTD_highbit32((U32)(rSize)-1) + 1);
1953+			if (cPar.windowLog > srcLog)
1954+				cPar.windowLog = srcLog;
1955+		}
1956+	}
1957+	if (cPar.hashLog > cPar.windowLog)
1958+		cPar.hashLog = cPar.windowLog;
1959+	{
1960+		U32 const cycleLog = ZSTD_cycleLog(cPar.chainLog, cPar.strategy);
1961+		if (cycleLog > cPar.windowLog)
1962+			cPar.chainLog -= (cycleLog - cPar.windowLog);
1963+	}
1964+
1965+	if (cPar.windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN)
1966+		cPar.windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */
1967+
1968+	return cPar;
1969+}
1970+
1971+static U32 ZSTD_equivalentParams(ZSTD_parameters param1, ZSTD_parameters param2)
1972+{
1973+	return (param1.cParams.hashLog == param2.cParams.hashLog) & (param1.cParams.chainLog == param2.cParams.chainLog) &
1974+	       (param1.cParams.strategy == param2.cParams.strategy) & ((param1.cParams.searchLength == 3) == (param2.cParams.searchLength == 3));
1975+}
1976+
1977+/*! ZSTD_continueCCtx() :
1978+	reuse CCtx without reset (note : requires no dictionary) */
1979+static size_t ZSTD_continueCCtx(ZSTD_CCtx *cctx, ZSTD_parameters params, U64 frameContentSize)
1980+{
1981+	U32 const end = (U32)(cctx->nextSrc - cctx->base);
1982+	cctx->params = params;
1983+	cctx->frameContentSize = frameContentSize;
1984+	cctx->lowLimit = end;
1985+	cctx->dictLimit = end;
1986+	cctx->nextToUpdate = end + 1;
1987+	cctx->stage = ZSTDcs_init;
1988+	cctx->dictID = 0;
1989+	cctx->loadedDictEnd = 0;
1990+	{
1991+		int i;
1992+		for (i = 0; i < ZSTD_REP_NUM; i++)
1993+			cctx->rep[i] = repStartValue[i];
1994+	}
1995+	cctx->seqStore.litLengthSum = 0; /* force reset of btopt stats */
1996+	xxh64_reset(&cctx->xxhState, 0);
1997+	return 0;
1998+}
1999+
2000+typedef enum { ZSTDcrp_continue, ZSTDcrp_noMemset, ZSTDcrp_fullReset } ZSTD_compResetPolicy_e;
2001+
2002+/*! ZSTD_resetCCtx_advanced() :
2003+	note : `params` must be validated */
2004+static size_t ZSTD_resetCCtx_advanced(ZSTD_CCtx *zc, ZSTD_parameters params, U64 frameContentSize, ZSTD_compResetPolicy_e const crp)
2005+{
2006+	if (crp == ZSTDcrp_continue)
2007+		if (ZSTD_equivalentParams(params, zc->params)) {
2008+			zc->flagStaticTables = 0;
2009+			zc->flagStaticHufTable = HUF_repeat_none;
2010+			return ZSTD_continueCCtx(zc, params, frameContentSize);
2011+		}
2012+
2013+	{
2014+		size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << params.cParams.windowLog);
2015+		U32 const divider = (params.cParams.searchLength == 3) ? 3 : 4;
2016+		size_t const maxNbSeq = blockSize / divider;
2017+		size_t const tokenSpace = blockSize + 11 * maxNbSeq;
2018+		size_t const chainSize = (params.cParams.strategy == ZSTD_fast) ? 0 : (1 << params.cParams.chainLog);
2019+		size_t const hSize = ((size_t)1) << params.cParams.hashLog;
2020+		U32 const hashLog3 = (params.cParams.searchLength > 3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, params.cParams.windowLog);
2021+		size_t const h3Size = ((size_t)1) << hashLog3;
2022+		size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
2023+		void *ptr;
2024+
2025+		/* Check if workSpace is large enough, alloc a new one if needed */
2026+		{
2027+			size_t const optSpace = ((MaxML + 1) + (MaxLL + 1) + (MaxOff + 1) + (1 << Litbits)) * sizeof(U32) +
2028+						(ZSTD_OPT_NUM + 1) * (sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t));
2029+			size_t const neededSpace = tableSpace + (256 * sizeof(U32)) /* huffTable */ + tokenSpace +
2030+						   (((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) ? optSpace : 0);
2031+			if (zc->workSpaceSize < neededSpace) {
2032+				ZSTD_free(zc->workSpace, zc->customMem);
2033+				zc->workSpace = ZSTD_malloc(neededSpace, zc->customMem);
2034+				if (zc->workSpace == NULL)
2035+					return ERROR(memory_allocation);
2036+				zc->workSpaceSize = neededSpace;
2037+			}
2038+		}
2039+
2040+		if (crp != ZSTDcrp_noMemset)
2041+			memset(zc->workSpace, 0, tableSpace); /* reset tables only */
2042+		xxh64_reset(&zc->xxhState, 0);
2043+		zc->hashLog3 = hashLog3;
2044+		zc->hashTable = (U32 *)(zc->workSpace);
2045+		zc->chainTable = zc->hashTable + hSize;
2046+		zc->hashTable3 = zc->chainTable + chainSize;
2047+		ptr = zc->hashTable3 + h3Size;
2048+		zc->hufTable = (HUF_CElt *)ptr;
2049+		zc->flagStaticTables = 0;
2050+		zc->flagStaticHufTable = HUF_repeat_none;
2051+		ptr = ((U32 *)ptr) + 256; /* note : HUF_CElt* is incomplete type, size is simulated using U32 */
2052+
2053+		zc->nextToUpdate = 1;
2054+		zc->nextSrc = NULL;
2055+		zc->base = NULL;
2056+		zc->dictBase = NULL;
2057+		zc->dictLimit = 0;
2058+		zc->lowLimit = 0;
2059+		zc->params = params;
2060+		zc->blockSize = blockSize;
2061+		zc->frameContentSize = frameContentSize;
2062+		{
2063+			int i;
2064+			for (i = 0; i < ZSTD_REP_NUM; i++)
2065+				zc->rep[i] = repStartValue[i];
2066+		}
2067+
2068+		if ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) {
2069+			zc->seqStore.litFreq = (U32 *)ptr;
2070+			zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1 << Litbits);
2071+			zc->seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (MaxLL + 1);
2072+			zc->seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (MaxML + 1);
2073+			ptr = zc->seqStore.offCodeFreq + (MaxOff + 1);
2074+			zc->seqStore.matchTable = (ZSTD_match_t *)ptr;
2075+			ptr = zc->seqStore.matchTable + ZSTD_OPT_NUM + 1;
2076+			zc->seqStore.priceTable = (ZSTD_optimal_t *)ptr;
2077+			ptr = zc->seqStore.priceTable + ZSTD_OPT_NUM + 1;
2078+			zc->seqStore.litLengthSum = 0;
2079+		}
2080+		zc->seqStore.sequencesStart = (seqDef *)ptr;
2081+		ptr = zc->seqStore.sequencesStart + maxNbSeq;
2082+		zc->seqStore.llCode = (BYTE *)ptr;
2083+		zc->seqStore.mlCode = zc->seqStore.llCode + maxNbSeq;
2084+		zc->seqStore.ofCode = zc->seqStore.mlCode + maxNbSeq;
2085+		zc->seqStore.litStart = zc->seqStore.ofCode + maxNbSeq;
2086+
2087+		zc->stage = ZSTDcs_init;
2088+		zc->dictID = 0;
2089+		zc->loadedDictEnd = 0;
2090+
2091+		return 0;
2092+	}
2093+}
2094+
2095+/* ZSTD_invalidateRepCodes() :
2096+ * ensures next compression will not use repcodes from previous block.
2097+ * Note : only works with regular variant;
2098+ *        do not use with extDict variant ! */
2099+void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx)
2100+{
2101+	int i;
2102+	for (i = 0; i < ZSTD_REP_NUM; i++)
2103+		cctx->rep[i] = 0;
2104+}
2105+
2106+/*! ZSTD_copyCCtx() :
2107+*   Duplicate an existing context `srcCCtx` into another one `dstCCtx`.
2108+*   Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()).
2109+*   @return : 0, or an error code */
2110+size_t ZSTD_copyCCtx(ZSTD_CCtx *dstCCtx, const ZSTD_CCtx *srcCCtx, unsigned long long pledgedSrcSize)
2111+{
2112+	if (srcCCtx->stage != ZSTDcs_init)
2113+		return ERROR(stage_wrong);
2114+
2115+	memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem));
2116+	{
2117+		ZSTD_parameters params = srcCCtx->params;
2118+		params.fParams.contentSizeFlag = (pledgedSrcSize > 0);
2119+		ZSTD_resetCCtx_advanced(dstCCtx, params, pledgedSrcSize, ZSTDcrp_noMemset);
2120+	}
2121+
2122+	/* copy tables */
2123+	{
2124+		size_t const chainSize = (srcCCtx->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << srcCCtx->params.cParams.chainLog);
2125+		size_t const hSize = ((size_t)1) << srcCCtx->params.cParams.hashLog;
2126+		size_t const h3Size = (size_t)1 << srcCCtx->hashLog3;
2127+		size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32);
2128+		memcpy(dstCCtx->workSpace, srcCCtx->workSpace, tableSpace);
2129+	}
2130+
2131+	/* copy dictionary offsets */
2132+	dstCCtx->nextToUpdate = srcCCtx->nextToUpdate;
2133+	dstCCtx->nextToUpdate3 = srcCCtx->nextToUpdate3;
2134+	dstCCtx->nextSrc = srcCCtx->nextSrc;
2135+	dstCCtx->base = srcCCtx->base;
2136+	dstCCtx->dictBase = srcCCtx->dictBase;
2137+	dstCCtx->dictLimit = srcCCtx->dictLimit;
2138+	dstCCtx->lowLimit = srcCCtx->lowLimit;
2139+	dstCCtx->loadedDictEnd = srcCCtx->loadedDictEnd;
2140+	dstCCtx->dictID = srcCCtx->dictID;
2141+
2142+	/* copy entropy tables */
2143+	dstCCtx->flagStaticTables = srcCCtx->flagStaticTables;
2144+	dstCCtx->flagStaticHufTable = srcCCtx->flagStaticHufTable;
2145+	if (srcCCtx->flagStaticTables) {
2146+		memcpy(dstCCtx->litlengthCTable, srcCCtx->litlengthCTable, sizeof(dstCCtx->litlengthCTable));
2147+		memcpy(dstCCtx->matchlengthCTable, srcCCtx->matchlengthCTable, sizeof(dstCCtx->matchlengthCTable));
2148+		memcpy(dstCCtx->offcodeCTable, srcCCtx->offcodeCTable, sizeof(dstCCtx->offcodeCTable));
2149+	}
2150+	if (srcCCtx->flagStaticHufTable) {
2151+		memcpy(dstCCtx->hufTable, srcCCtx->hufTable, 256 * 4);
2152+	}
2153+
2154+	return 0;
2155+}
2156+
2157+/*! ZSTD_reduceTable() :
2158+*   reduce table indexes by `reducerValue` */
2159+static void ZSTD_reduceTable(U32 *const table, U32 const size, U32 const reducerValue)
2160+{
2161+	U32 u;
2162+	for (u = 0; u < size; u++) {
2163+		if (table[u] < reducerValue)
2164+			table[u] = 0;
2165+		else
2166+			table[u] -= reducerValue;
2167+	}
2168+}
2169+
2170+/*! ZSTD_reduceIndex() :
2171+*   rescale all indexes to avoid future overflow (indexes are U32) */
2172+static void ZSTD_reduceIndex(ZSTD_CCtx *zc, const U32 reducerValue)
2173+{
2174+	{
2175+		U32 const hSize = 1 << zc->params.cParams.hashLog;
2176+		ZSTD_reduceTable(zc->hashTable, hSize, reducerValue);
2177+	}
2178+
2179+	{
2180+		U32 const chainSize = (zc->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << zc->params.cParams.chainLog);
2181+		ZSTD_reduceTable(zc->chainTable, chainSize, reducerValue);
2182+	}
2183+
2184+	{
2185+		U32 const h3Size = (zc->hashLog3) ? 1 << zc->hashLog3 : 0;
2186+		ZSTD_reduceTable(zc->hashTable3, h3Size, reducerValue);
2187+	}
2188+}
2189+
2190+/*-*******************************************************
2191+*  Block entropic compression
2192+*********************************************************/
2193+
2194+/* See doc/zstd_compression_format.md for detailed format description */
2195+
2196+size_t ZSTD_noCompressBlock(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2197+{
2198+	if (srcSize + ZSTD_blockHeaderSize > dstCapacity)
2199+		return ERROR(dstSize_tooSmall);
2200+	memcpy((BYTE *)dst + ZSTD_blockHeaderSize, src, srcSize);
2201+	ZSTD_writeLE24(dst, (U32)(srcSize << 2) + (U32)bt_raw);
2202+	return ZSTD_blockHeaderSize + srcSize;
2203+}
2204+
2205+static size_t ZSTD_noCompressLiterals(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2206+{
2207+	BYTE *const ostart = (BYTE * const)dst;
2208+	U32 const flSize = 1 + (srcSize > 31) + (srcSize > 4095);
2209+
2210+	if (srcSize + flSize > dstCapacity)
2211+		return ERROR(dstSize_tooSmall);
2212+
2213+	switch (flSize) {
2214+	case 1: /* 2 - 1 - 5 */ ostart[0] = (BYTE)((U32)set_basic + (srcSize << 3)); break;
2215+	case 2: /* 2 - 2 - 12 */ ZSTD_writeLE16(ostart, (U16)((U32)set_basic + (1 << 2) + (srcSize << 4))); break;
2216+	default: /*note : should not be necessary : flSize is within {1,2,3} */
2217+	case 3: /* 2 - 2 - 20 */ ZSTD_writeLE32(ostart, (U32)((U32)set_basic + (3 << 2) + (srcSize << 4))); break;
2218+	}
2219+
2220+	memcpy(ostart + flSize, src, srcSize);
2221+	return srcSize + flSize;
2222+}
2223+
2224+static size_t ZSTD_compressRleLiteralsBlock(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2225+{
2226+	BYTE *const ostart = (BYTE * const)dst;
2227+	U32 const flSize = 1 + (srcSize > 31) + (srcSize > 4095);
2228+
2229+	(void)dstCapacity; /* dstCapacity already guaranteed to be >=4, hence large enough */
2230+
2231+	switch (flSize) {
2232+	case 1: /* 2 - 1 - 5 */ ostart[0] = (BYTE)((U32)set_rle + (srcSize << 3)); break;
2233+	case 2: /* 2 - 2 - 12 */ ZSTD_writeLE16(ostart, (U16)((U32)set_rle + (1 << 2) + (srcSize << 4))); break;
2234+	default: /*note : should not be necessary : flSize is necessarily within {1,2,3} */
2235+	case 3: /* 2 - 2 - 20 */ ZSTD_writeLE32(ostart, (U32)((U32)set_rle + (3 << 2) + (srcSize << 4))); break;
2236+	}
2237+
2238+	ostart[flSize] = *(const BYTE *)src;
2239+	return flSize + 1;
2240+}
2241+
2242+static size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 2; }
2243+
2244+static size_t ZSTD_compressLiterals(ZSTD_CCtx *zc, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
2245+{
2246+	size_t const minGain = ZSTD_minGain(srcSize);
2247+	size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB);
2248+	BYTE *const ostart = (BYTE *)dst;
2249+	U32 singleStream = srcSize < 256;
2250+	symbolEncodingType_e hType = set_compressed;
2251+	size_t cLitSize;
2252+
2253+/* small ? don't even attempt compression (speed opt) */
2254+#define LITERAL_NOENTROPY 63
2255+	{
2256+		size_t const minLitSize = zc->flagStaticHufTable == HUF_repeat_valid ? 6 : LITERAL_NOENTROPY;
2257+		if (srcSize <= minLitSize)
2258+			return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
2259+	}
2260+
2261+	if (dstCapacity < lhSize + 1)
2262+		return ERROR(dstSize_tooSmall); /* not enough space for compression */
2263+	{
2264+		HUF_repeat repeat = zc->flagStaticHufTable;
2265+		int const preferRepeat = zc->params.cParams.strategy < ZSTD_lazy ? srcSize <= 1024 : 0;
2266+		if (repeat == HUF_repeat_valid && lhSize == 3)
2267+			singleStream = 1;
2268+		cLitSize = singleStream ? HUF_compress1X_repeat(ostart + lhSize, dstCapacity - lhSize, src, srcSize, 255, 11, zc->tmpCounters,
2269+								sizeof(zc->tmpCounters), zc->hufTable, &repeat, preferRepeat)
2270+					: HUF_compress4X_repeat(ostart + lhSize, dstCapacity - lhSize, src, srcSize, 255, 11, zc->tmpCounters,
2271+								sizeof(zc->tmpCounters), zc->hufTable, &repeat, preferRepeat);
2272+		if (repeat != HUF_repeat_none) {
2273+			hType = set_repeat;
2274+		} /* reused the existing table */
2275+		else {
2276+			zc->flagStaticHufTable = HUF_repeat_check;
2277+		} /* now have a table to reuse */
2278+	}
2279+
2280+	if ((cLitSize == 0) | (cLitSize >= srcSize - minGain)) {
2281+		zc->flagStaticHufTable = HUF_repeat_none;
2282+		return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize);
2283+	}
2284+	if (cLitSize == 1) {
2285+		zc->flagStaticHufTable = HUF_repeat_none;
2286+		return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize);
2287+	}
2288+
2289+	/* Build header */
2290+	switch (lhSize) {
2291+	case 3: /* 2 - 2 - 10 - 10 */
2292+	{
2293+		U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize << 4) + ((U32)cLitSize << 14);
2294+		ZSTD_writeLE24(ostart, lhc);
2295+		break;
2296+	}
2297+	case 4: /* 2 - 2 - 14 - 14 */
2298+	{
2299+		U32 const lhc = hType + (2 << 2) + ((U32)srcSize << 4) + ((U32)cLitSize << 18);
2300+		ZSTD_writeLE32(ostart, lhc);
2301+		break;
2302+	}
2303+	default: /* should not be necessary, lhSize is only {3,4,5} */
2304+	case 5:  /* 2 - 2 - 18 - 18 */
2305+	{
2306+		U32 const lhc = hType + (3 << 2) + ((U32)srcSize << 4) + ((U32)cLitSize << 22);
2307+		ZSTD_writeLE32(ostart, lhc);
2308+		ostart[4] = (BYTE)(cLitSize >> 10);
2309+		break;
2310+	}
2311+	}
2312+	return lhSize + cLitSize;
2313+}
2314+
2315+static const BYTE LL_Code[64] = {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 16, 17, 17, 18, 18,
2316+				 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23,
2317+				 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24};
2318+
2319+static const BYTE ML_Code[128] = {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
2320+				  26, 27, 28, 29, 30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38,
2321+				  38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
2322+				  40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42,
2323+				  42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
2324+
2325+void ZSTD_seqToCodes(const seqStore_t *seqStorePtr)
2326+{
2327+	BYTE const LL_deltaCode = 19;
2328+	BYTE const ML_deltaCode = 36;
2329+	const seqDef *const sequences = seqStorePtr->sequencesStart;
2330+	BYTE *const llCodeTable = seqStorePtr->llCode;
2331+	BYTE *const ofCodeTable = seqStorePtr->ofCode;
2332+	BYTE *const mlCodeTable = seqStorePtr->mlCode;
2333+	U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
2334+	U32 u;
2335+	for (u = 0; u < nbSeq; u++) {
2336+		U32 const llv = sequences[u].litLength;
2337+		U32 const mlv = sequences[u].matchLength;
2338+		llCodeTable[u] = (llv > 63) ? (BYTE)ZSTD_highbit32(llv) + LL_deltaCode : LL_Code[llv];
2339+		ofCodeTable[u] = (BYTE)ZSTD_highbit32(sequences[u].offset);
2340+		mlCodeTable[u] = (mlv > 127) ? (BYTE)ZSTD_highbit32(mlv) + ML_deltaCode : ML_Code[mlv];
2341+	}
2342+	if (seqStorePtr->longLengthID == 1)
2343+		llCodeTable[seqStorePtr->longLengthPos] = MaxLL;
2344+	if (seqStorePtr->longLengthID == 2)
2345+		mlCodeTable[seqStorePtr->longLengthPos] = MaxML;
2346+}
2347+
2348+ZSTD_STATIC size_t ZSTD_compressSequences_internal(ZSTD_CCtx *zc, void *dst, size_t dstCapacity)
2349+{
2350+	const int longOffsets = zc->params.cParams.windowLog > STREAM_ACCUMULATOR_MIN;
2351+	const seqStore_t *seqStorePtr = &(zc->seqStore);
2352+	FSE_CTable *CTable_LitLength = zc->litlengthCTable;
2353+	FSE_CTable *CTable_OffsetBits = zc->offcodeCTable;
2354+	FSE_CTable *CTable_MatchLength = zc->matchlengthCTable;
2355+	U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */
2356+	const seqDef *const sequences = seqStorePtr->sequencesStart;
2357+	const BYTE *const ofCodeTable = seqStorePtr->ofCode;
2358+	const BYTE *const llCodeTable = seqStorePtr->llCode;
2359+	const BYTE *const mlCodeTable = seqStorePtr->mlCode;
2360+	BYTE *const ostart = (BYTE *)dst;
2361+	BYTE *const oend = ostart + dstCapacity;
2362+	BYTE *op = ostart;
2363+	size_t const nbSeq = seqStorePtr->sequences - seqStorePtr->sequencesStart;
2364+	BYTE *seqHead;
2365+
2366+	U32 *count;
2367+	S16 *norm;
2368+	U32 *workspace;
2369+	size_t workspaceSize = sizeof(zc->tmpCounters);
2370+	{
2371+		size_t spaceUsed32 = 0;
2372+		count = (U32 *)zc->tmpCounters + spaceUsed32;
2373+		spaceUsed32 += MaxSeq + 1;
2374+		norm = (S16 *)((U32 *)zc->tmpCounters + spaceUsed32);
2375+		spaceUsed32 += ALIGN(sizeof(S16) * (MaxSeq + 1), sizeof(U32)) >> 2;
2376+
2377+		workspace = (U32 *)zc->tmpCounters + spaceUsed32;
2378+		workspaceSize -= (spaceUsed32 << 2);
2379+	}
2380+
2381+	/* Compress literals */
2382+	{
2383+		const BYTE *const literals = seqStorePtr->litStart;
2384+		size_t const litSize = seqStorePtr->lit - literals;
2385+		size_t const cSize = ZSTD_compressLiterals(zc, op, dstCapacity, literals, litSize);
2386+		if (ZSTD_isError(cSize))
2387+			return cSize;
2388+		op += cSize;
2389+	}
2390+
2391+	/* Sequences Header */
2392+	if ((oend - op) < 3 /*max nbSeq Size*/ + 1 /*seqHead */)
2393+		return ERROR(dstSize_tooSmall);
2394+	if (nbSeq < 0x7F)
2395+		*op++ = (BYTE)nbSeq;
2396+	else if (nbSeq < LONGNBSEQ)
2397+		op[0] = (BYTE)((nbSeq >> 8) + 0x80), op[1] = (BYTE)nbSeq, op += 2;
2398+	else
2399+		op[0] = 0xFF, ZSTD_writeLE16(op + 1, (U16)(nbSeq - LONGNBSEQ)), op += 3;
2400+	if (nbSeq == 0)
2401+		return op - ostart;
2402+
2403+	/* seqHead : flags for FSE encoding type */
2404+	seqHead = op++;
2405+
2406+#define MIN_SEQ_FOR_DYNAMIC_FSE 64
2407+#define MAX_SEQ_FOR_STATIC_FSE 1000
2408+
2409+	/* convert length/distances into codes */
2410+	ZSTD_seqToCodes(seqStorePtr);
2411+
2412+	/* CTable for Literal Lengths */
2413+	{
2414+		U32 max = MaxLL;
2415+		size_t const mostFrequent = FSE_countFast_wksp(count, &max, llCodeTable, nbSeq, workspace);
2416+		if ((mostFrequent == nbSeq) && (nbSeq > 2)) {
2417+			*op++ = llCodeTable[0];
2418+			FSE_buildCTable_rle(CTable_LitLength, (BYTE)max);
2419+			LLtype = set_rle;
2420+		} else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) {
2421+			LLtype = set_repeat;
2422+		} else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (LL_defaultNormLog - 1)))) {
2423+			FSE_buildCTable_wksp(CTable_LitLength, LL_defaultNorm, MaxLL, LL_defaultNormLog, workspace, workspaceSize);
2424+			LLtype = set_basic;
2425+		} else {
2426+			size_t nbSeq_1 = nbSeq;
2427+			const U32 tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max);
2428+			if (count[llCodeTable[nbSeq - 1]] > 1) {
2429+				count[llCodeTable[nbSeq - 1]]--;
2430+				nbSeq_1--;
2431+			}
2432+			FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max);
2433+			{
2434+				size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */
2435+				if (FSE_isError(NCountSize))
2436+					return NCountSize;
2437+				op += NCountSize;
2438+			}
2439+			FSE_buildCTable_wksp(CTable_LitLength, norm, max, tableLog, workspace, workspaceSize);
2440+			LLtype = set_compressed;
2441+		}
2442+	}
2443+
2444+	/* CTable for Offsets */
2445+	{
2446+		U32 max = MaxOff;
2447+		size_t const mostFrequent = FSE_countFast_wksp(count, &max, ofCodeTable, nbSeq, workspace);
2448+		if ((mostFrequent == nbSeq) && (nbSeq > 2)) {
2449+			*op++ = ofCodeTable[0];
2450+			FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max);
2451+			Offtype = set_rle;
2452+		} else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) {
2453+			Offtype = set_repeat;
2454+		} else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (OF_defaultNormLog - 1)))) {
2455+			FSE_buildCTable_wksp(CTable_OffsetBits, OF_defaultNorm, MaxOff, OF_defaultNormLog, workspace, workspaceSize);
2456+			Offtype = set_basic;
2457+		} else {
2458+			size_t nbSeq_1 = nbSeq;
2459+			const U32 tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max);
2460+			if (count[ofCodeTable[nbSeq - 1]] > 1) {
2461+				count[ofCodeTable[nbSeq - 1]]--;
2462+				nbSeq_1--;
2463+			}
2464+			FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max);
2465+			{
2466+				size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */
2467+				if (FSE_isError(NCountSize))
2468+					return NCountSize;
2469+				op += NCountSize;
2470+			}
2471+			FSE_buildCTable_wksp(CTable_OffsetBits, norm, max, tableLog, workspace, workspaceSize);
2472+			Offtype = set_compressed;
2473+		}
2474+	}
2475+
2476+	/* CTable for MatchLengths */
2477+	{
2478+		U32 max = MaxML;
2479+		size_t const mostFrequent = FSE_countFast_wksp(count, &max, mlCodeTable, nbSeq, workspace);
2480+		if ((mostFrequent == nbSeq) && (nbSeq > 2)) {
2481+			*op++ = *mlCodeTable;
2482+			FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max);
2483+			MLtype = set_rle;
2484+		} else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) {
2485+			MLtype = set_repeat;
2486+		} else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (ML_defaultNormLog - 1)))) {
2487+			FSE_buildCTable_wksp(CTable_MatchLength, ML_defaultNorm, MaxML, ML_defaultNormLog, workspace, workspaceSize);
2488+			MLtype = set_basic;
2489+		} else {
2490+			size_t nbSeq_1 = nbSeq;
2491+			const U32 tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max);
2492+			if (count[mlCodeTable[nbSeq - 1]] > 1) {
2493+				count[mlCodeTable[nbSeq - 1]]--;
2494+				nbSeq_1--;
2495+			}
2496+			FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max);
2497+			{
2498+				size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */
2499+				if (FSE_isError(NCountSize))
2500+					return NCountSize;
2501+				op += NCountSize;
2502+			}
2503+			FSE_buildCTable_wksp(CTable_MatchLength, norm, max, tableLog, workspace, workspaceSize);
2504+			MLtype = set_compressed;
2505+		}
2506+	}
2507+
2508+	*seqHead = (BYTE)((LLtype << 6) + (Offtype << 4) + (MLtype << 2));
2509+	zc->flagStaticTables = 0;
2510+
2511+	/* Encoding Sequences */
2512+	{
2513+		BIT_CStream_t blockStream;
2514+		FSE_CState_t stateMatchLength;
2515+		FSE_CState_t stateOffsetBits;
2516+		FSE_CState_t stateLitLength;
2517+
2518+		CHECK_E(BIT_initCStream(&blockStream, op, oend - op), dstSize_tooSmall); /* not enough space remaining */
2519+
2520+		/* first symbols */
2521+		FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq - 1]);
2522+		FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq - 1]);
2523+		FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq - 1]);
2524+		BIT_addBits(&blockStream, sequences[nbSeq - 1].litLength, LL_bits[llCodeTable[nbSeq - 1]]);
2525+		if (ZSTD_32bits())
2526+			BIT_flushBits(&blockStream);
2527+		BIT_addBits(&blockStream, sequences[nbSeq - 1].matchLength, ML_bits[mlCodeTable[nbSeq - 1]]);
2528+		if (ZSTD_32bits())
2529+			BIT_flushBits(&blockStream);
2530+		if (longOffsets) {
2531+			U32 const ofBits = ofCodeTable[nbSeq - 1];
2532+			int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN - 1);
2533+			if (extraBits) {
2534+				BIT_addBits(&blockStream, sequences[nbSeq - 1].offset, extraBits);
2535+				BIT_flushBits(&blockStream);
2536+			}
2537+			BIT_addBits(&blockStream, sequences[nbSeq - 1].offset >> extraBits, ofBits - extraBits);
2538+		} else {
2539+			BIT_addBits(&blockStream, sequences[nbSeq - 1].offset, ofCodeTable[nbSeq - 1]);
2540+		}
2541+		BIT_flushBits(&blockStream);
2542+
2543+		{
2544+			size_t n;
2545+			for (n = nbSeq - 2; n < nbSeq; n--) { /* intentional underflow */
2546+				BYTE const llCode = llCodeTable[n];
2547+				BYTE const ofCode = ofCodeTable[n];
2548+				BYTE const mlCode = mlCodeTable[n];
2549+				U32 const llBits = LL_bits[llCode];
2550+				U32 const ofBits = ofCode; /* 32b*/ /* 64b*/
2551+				U32 const mlBits = ML_bits[mlCode];
2552+				/* (7)*/							    /* (7)*/
2553+				FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */  /* 15 */
2554+				FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */
2555+				if (ZSTD_32bits())
2556+					BIT_flushBits(&blockStream);				  /* (7)*/
2557+				FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */
2558+				if (ZSTD_32bits() || (ofBits + mlBits + llBits >= 64 - 7 - (LLFSELog + MLFSELog + OffFSELog)))
2559+					BIT_flushBits(&blockStream); /* (7)*/
2560+				BIT_addBits(&blockStream, sequences[n].litLength, llBits);
2561+				if (ZSTD_32bits() && ((llBits + mlBits) > 24))
2562+					BIT_flushBits(&blockStream);
2563+				BIT_addBits(&blockStream, sequences[n].matchLength, mlBits);
2564+				if (ZSTD_32bits())
2565+					BIT_flushBits(&blockStream); /* (7)*/
2566+				if (longOffsets) {
2567+					int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN - 1);
2568+					if (extraBits) {
2569+						BIT_addBits(&blockStream, sequences[n].offset, extraBits);
2570+						BIT_flushBits(&blockStream); /* (7)*/
2571+					}
2572+					BIT_addBits(&blockStream, sequences[n].offset >> extraBits, ofBits - extraBits); /* 31 */
2573+				} else {
2574+					BIT_addBits(&blockStream, sequences[n].offset, ofBits); /* 31 */
2575+				}
2576+				BIT_flushBits(&blockStream); /* (7)*/
2577+			}
2578+		}
2579+
2580+		FSE_flushCState(&blockStream, &stateMatchLength);
2581+		FSE_flushCState(&blockStream, &stateOffsetBits);
2582+		FSE_flushCState(&blockStream, &stateLitLength);
2583+
2584+		{
2585+			size_t const streamSize = BIT_closeCStream(&blockStream);
2586+			if (streamSize == 0)
2587+				return ERROR(dstSize_tooSmall); /* not enough space */
2588+			op += streamSize;
2589+		}
2590+	}
2591+	return op - ostart;
2592+}
2593+
2594+ZSTD_STATIC size_t ZSTD_compressSequences(ZSTD_CCtx *zc, void *dst, size_t dstCapacity, size_t srcSize)
2595+{
2596+	size_t const cSize = ZSTD_compressSequences_internal(zc, dst, dstCapacity);
2597+	size_t const minGain = ZSTD_minGain(srcSize);
2598+	size_t const maxCSize = srcSize - minGain;
2599+	/* If the srcSize <= dstCapacity, then there is enough space to write a
2600+	 * raw uncompressed block. Since we ran out of space, the block must not
2601+	 * be compressible, so fall back to a raw uncompressed block.
2602+	 */
2603+	int const uncompressibleError = cSize == ERROR(dstSize_tooSmall) && srcSize <= dstCapacity;
2604+	int i;
2605+
2606+	if (ZSTD_isError(cSize) && !uncompressibleError)
2607+		return cSize;
2608+	if (cSize >= maxCSize || uncompressibleError) {
2609+		zc->flagStaticHufTable = HUF_repeat_none;
2610+		return 0;
2611+	}
2612+	/* confirm repcodes */
2613+	for (i = 0; i < ZSTD_REP_NUM; i++)
2614+		zc->rep[i] = zc->repToConfirm[i];
2615+	return cSize;
2616+}
2617+
2618+/*! ZSTD_storeSeq() :
2619+	Store a sequence (literal length, literals, offset code and match length code) into seqStore_t.
2620+	`offsetCode` : distance to match, or 0 == repCode.
2621+	`matchCode` : matchLength - MINMATCH
2622+*/
2623+ZSTD_STATIC void ZSTD_storeSeq(seqStore_t *seqStorePtr, size_t litLength, const void *literals, U32 offsetCode, size_t matchCode)
2624+{
2625+	/* copy Literals */
2626+	ZSTD_wildcopy(seqStorePtr->lit, literals, litLength);
2627+	seqStorePtr->lit += litLength;
2628+
2629+	/* literal Length */
2630+	if (litLength > 0xFFFF) {
2631+		seqStorePtr->longLengthID = 1;
2632+		seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
2633+	}
2634+	seqStorePtr->sequences[0].litLength = (U16)litLength;
2635+
2636+	/* match offset */
2637+	seqStorePtr->sequences[0].offset = offsetCode + 1;
2638+
2639+	/* match Length */
2640+	if (matchCode > 0xFFFF) {
2641+		seqStorePtr->longLengthID = 2;
2642+		seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart);
2643+	}
2644+	seqStorePtr->sequences[0].matchLength = (U16)matchCode;
2645+
2646+	seqStorePtr->sequences++;
2647+}
2648+
2649+/*-*************************************
2650+*  Match length counter
2651+***************************************/
2652+static unsigned ZSTD_NbCommonBytes(register size_t val)
2653+{
2654+	if (ZSTD_isLittleEndian()) {
2655+		if (ZSTD_64bits()) {
2656+			return (__builtin_ctzll((U64)val) >> 3);
2657+		} else { /* 32 bits */
2658+			return (__builtin_ctz((U32)val) >> 3);
2659+		}
2660+	} else { /* Big Endian CPU */
2661+		if (ZSTD_64bits()) {
2662+			return (__builtin_clzll(val) >> 3);
2663+		} else { /* 32 bits */
2664+			return (__builtin_clz((U32)val) >> 3);
2665+		}
2666+	}
2667+}
2668+
2669+static size_t ZSTD_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *const pInLimit)
2670+{
2671+	const BYTE *const pStart = pIn;
2672+	const BYTE *const pInLoopLimit = pInLimit - (sizeof(size_t) - 1);
2673+
2674+	while (pIn < pInLoopLimit) {
2675+		size_t const diff = ZSTD_readST(pMatch) ^ ZSTD_readST(pIn);
2676+		if (!diff) {
2677+			pIn += sizeof(size_t);
2678+			pMatch += sizeof(size_t);
2679+			continue;
2680+		}
2681+		pIn += ZSTD_NbCommonBytes(diff);
2682+		return (size_t)(pIn - pStart);
2683+	}
2684+	if (ZSTD_64bits())
2685+		if ((pIn < (pInLimit - 3)) && (ZSTD_read32(pMatch) == ZSTD_read32(pIn))) {
2686+			pIn += 4;
2687+			pMatch += 4;
2688+		}
2689+	if ((pIn < (pInLimit - 1)) && (ZSTD_read16(pMatch) == ZSTD_read16(pIn))) {
2690+		pIn += 2;
2691+		pMatch += 2;
2692+	}
2693+	if ((pIn < pInLimit) && (*pMatch == *pIn))
2694+		pIn++;
2695+	return (size_t)(pIn - pStart);
2696+}
2697+
2698+/** ZSTD_count_2segments() :
2699+*   can count match length with `ip` & `match` in 2 different segments.
2700+*   convention : on reaching mEnd, match count continue starting from iStart
2701+*/
2702+static size_t ZSTD_count_2segments(const BYTE *ip, const BYTE *match, const BYTE *iEnd, const BYTE *mEnd, const BYTE *iStart)
2703+{
2704+	const BYTE *const vEnd = MIN(ip + (mEnd - match), iEnd);
2705+	size_t const matchLength = ZSTD_count(ip, match, vEnd);
2706+	if (match + matchLength != mEnd)
2707+		return matchLength;
2708+	return matchLength + ZSTD_count(ip + matchLength, iStart, iEnd);
2709+}
2710+
2711+/*-*************************************
2712+*  Hashes
2713+***************************************/
2714+static const U32 prime3bytes = 506832829U;
2715+static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32 - 24)) * prime3bytes) >> (32 - h); }
2716+ZSTD_STATIC size_t ZSTD_hash3Ptr(const void *ptr, U32 h) { return ZSTD_hash3(ZSTD_readLE32(ptr), h); } /* only in zstd_opt.h */
2717+
2718+static const U32 prime4bytes = 2654435761U;
2719+static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32 - h); }
2720+static size_t ZSTD_hash4Ptr(const void *ptr, U32 h) { return ZSTD_hash4(ZSTD_read32(ptr), h); }
2721+
2722+static const U64 prime5bytes = 889523592379ULL;
2723+static size_t ZSTD_hash5(U64 u, U32 h) { return (size_t)(((u << (64 - 40)) * prime5bytes) >> (64 - h)); }
2724+static size_t ZSTD_hash5Ptr(const void *p, U32 h) { return ZSTD_hash5(ZSTD_readLE64(p), h); }
2725+
2726+static const U64 prime6bytes = 227718039650203ULL;
2727+static size_t ZSTD_hash6(U64 u, U32 h) { return (size_t)(((u << (64 - 48)) * prime6bytes) >> (64 - h)); }
2728+static size_t ZSTD_hash6Ptr(const void *p, U32 h) { return ZSTD_hash6(ZSTD_readLE64(p), h); }
2729+
2730+static const U64 prime7bytes = 58295818150454627ULL;
2731+static size_t ZSTD_hash7(U64 u, U32 h) { return (size_t)(((u << (64 - 56)) * prime7bytes) >> (64 - h)); }
2732+static size_t ZSTD_hash7Ptr(const void *p, U32 h) { return ZSTD_hash7(ZSTD_readLE64(p), h); }
2733+
2734+static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL;
2735+static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u)*prime8bytes) >> (64 - h)); }
2736+static size_t ZSTD_hash8Ptr(const void *p, U32 h) { return ZSTD_hash8(ZSTD_readLE64(p), h); }
2737+
2738+static size_t ZSTD_hashPtr(const void *p, U32 hBits, U32 mls)
2739+{
2740+	switch (mls) {
2741+	// case 3: return ZSTD_hash3Ptr(p, hBits);
2742+	default:
2743+	case 4: return ZSTD_hash4Ptr(p, hBits);
2744+	case 5: return ZSTD_hash5Ptr(p, hBits);
2745+	case 6: return ZSTD_hash6Ptr(p, hBits);
2746+	case 7: return ZSTD_hash7Ptr(p, hBits);
2747+	case 8: return ZSTD_hash8Ptr(p, hBits);
2748+	}
2749+}
2750+
2751+/*-*************************************
2752+*  Fast Scan
2753+***************************************/
2754+static void ZSTD_fillHashTable(ZSTD_CCtx *zc, const void *end, const U32 mls)
2755+{
2756+	U32 *const hashTable = zc->hashTable;
2757+	U32 const hBits = zc->params.cParams.hashLog;
2758+	const BYTE *const base = zc->base;
2759+	const BYTE *ip = base + zc->nextToUpdate;
2760+	const BYTE *const iend = ((const BYTE *)end) - HASH_READ_SIZE;
2761+	const size_t fastHashFillStep = 3;
2762+
2763+	while (ip <= iend) {
2764+		hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base);
2765+		ip += fastHashFillStep;
2766+	}
2767+}
2768+
2769+FORCE_INLINE
2770+void ZSTD_compressBlock_fast_generic(ZSTD_CCtx *cctx, const void *src, size_t srcSize, const U32 mls)
2771+{
2772+	U32 *const hashTable = cctx->hashTable;
2773+	U32 const hBits = cctx->params.cParams.hashLog;
2774+	seqStore_t *seqStorePtr = &(cctx->seqStore);
2775+	const BYTE *const base = cctx->base;
2776+	const BYTE *const istart = (const BYTE *)src;
2777+	const BYTE *ip = istart;
2778+	const BYTE *anchor = istart;
2779+	const U32 lowestIndex = cctx->dictLimit;
2780+	const BYTE *const lowest = base + lowestIndex;
2781+	const BYTE *const iend = istart + srcSize;
2782+	const BYTE *const ilimit = iend - HASH_READ_SIZE;
2783+	U32 offset_1 = cctx->rep[0], offset_2 = cctx->rep[1];
2784+	U32 offsetSaved = 0;
2785+
2786+	/* init */
2787+	ip += (ip == lowest);
2788+	{
2789+		U32 const maxRep = (U32)(ip - lowest);
2790+		if (offset_2 > maxRep)
2791+			offsetSaved = offset_2, offset_2 = 0;
2792+		if (offset_1 > maxRep)
2793+			offsetSaved = offset_1, offset_1 = 0;
2794+	}
2795+
2796+	/* Main Search Loop */
2797+	while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
2798+		size_t mLength;
2799+		size_t const h = ZSTD_hashPtr(ip, hBits, mls);
2800+		U32 const curr = (U32)(ip - base);
2801+		U32 const matchIndex = hashTable[h];
2802+		const BYTE *match = base + matchIndex;
2803+		hashTable[h] = curr; /* update hash table */
2804+
2805+		if ((offset_1 > 0) & (ZSTD_read32(ip + 1 - offset_1) == ZSTD_read32(ip + 1))) {
2806+			mLength = ZSTD_count(ip + 1 + 4, ip + 1 + 4 - offset_1, iend) + 4;
2807+			ip++;
2808+			ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
2809+		} else {
2810+			U32 offset;
2811+			if ((matchIndex <= lowestIndex) || (ZSTD_read32(match) != ZSTD_read32(ip))) {
2812+				ip += ((ip - anchor) >> g_searchStrength) + 1;
2813+				continue;
2814+			}
2815+			mLength = ZSTD_count(ip + 4, match + 4, iend) + 4;
2816+			offset = (U32)(ip - match);
2817+			while (((ip > anchor) & (match > lowest)) && (ip[-1] == match[-1])) {
2818+				ip--;
2819+				match--;
2820+				mLength++;
2821+			} /* catch up */
2822+			offset_2 = offset_1;
2823+			offset_1 = offset;
2824+
2825+			ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
2826+		}
2827+
2828+		/* match found */
2829+		ip += mLength;
2830+		anchor = ip;
2831+
2832+		if (ip <= ilimit) {
2833+			/* Fill Table */
2834+			hashTable[ZSTD_hashPtr(base + curr + 2, hBits, mls)] = curr + 2; /* here because curr+2 could be > iend-8 */
2835+			hashTable[ZSTD_hashPtr(ip - 2, hBits, mls)] = (U32)(ip - 2 - base);
2836+			/* check immediate repcode */
2837+			while ((ip <= ilimit) && ((offset_2 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_2)))) {
2838+				/* store sequence */
2839+				size_t const rLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4;
2840+				{
2841+					U32 const tmpOff = offset_2;
2842+					offset_2 = offset_1;
2843+					offset_1 = tmpOff;
2844+				} /* swap offset_2 <=> offset_1 */
2845+				hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base);
2846+				ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength - MINMATCH);
2847+				ip += rLength;
2848+				anchor = ip;
2849+				continue; /* faster when present ... (?) */
2850+			}
2851+		}
2852+	}
2853+
2854+	/* save reps for next block */
2855+	cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved;
2856+	cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved;
2857+
2858+	/* Last Literals */
2859+	{
2860+		size_t const lastLLSize = iend - anchor;
2861+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
2862+		seqStorePtr->lit += lastLLSize;
2863+	}
2864+}
2865+
2866+static void ZSTD_compressBlock_fast(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2867+{
2868+	const U32 mls = ctx->params.cParams.searchLength;
2869+	switch (mls) {
2870+	default: /* includes case 3 */
2871+	case 4: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 4); return;
2872+	case 5: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 5); return;
2873+	case 6: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 6); return;
2874+	case 7: ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 7); return;
2875+	}
2876+}
2877+
2878+static void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 mls)
2879+{
2880+	U32 *hashTable = ctx->hashTable;
2881+	const U32 hBits = ctx->params.cParams.hashLog;
2882+	seqStore_t *seqStorePtr = &(ctx->seqStore);
2883+	const BYTE *const base = ctx->base;
2884+	const BYTE *const dictBase = ctx->dictBase;
2885+	const BYTE *const istart = (const BYTE *)src;
2886+	const BYTE *ip = istart;
2887+	const BYTE *anchor = istart;
2888+	const U32 lowestIndex = ctx->lowLimit;
2889+	const BYTE *const dictStart = dictBase + lowestIndex;
2890+	const U32 dictLimit = ctx->dictLimit;
2891+	const BYTE *const lowPrefixPtr = base + dictLimit;
2892+	const BYTE *const dictEnd = dictBase + dictLimit;
2893+	const BYTE *const iend = istart + srcSize;
2894+	const BYTE *const ilimit = iend - 8;
2895+	U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1];
2896+
2897+	/* Search Loop */
2898+	while (ip < ilimit) { /* < instead of <=, because (ip+1) */
2899+		const size_t h = ZSTD_hashPtr(ip, hBits, mls);
2900+		const U32 matchIndex = hashTable[h];
2901+		const BYTE *matchBase = matchIndex < dictLimit ? dictBase : base;
2902+		const BYTE *match = matchBase + matchIndex;
2903+		const U32 curr = (U32)(ip - base);
2904+		const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
2905+		const BYTE *repBase = repIndex < dictLimit ? dictBase : base;
2906+		const BYTE *repMatch = repBase + repIndex;
2907+		size_t mLength;
2908+		hashTable[h] = curr; /* update hash table */
2909+
2910+		if ((((U32)((dictLimit - 1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) &&
2911+		    (ZSTD_read32(repMatch) == ZSTD_read32(ip + 1))) {
2912+			const BYTE *repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
2913+			mLength = ZSTD_count_2segments(ip + 1 + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repMatchEnd, lowPrefixPtr) + EQUAL_READ32;
2914+			ip++;
2915+			ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
2916+		} else {
2917+			if ((matchIndex < lowestIndex) || (ZSTD_read32(match) != ZSTD_read32(ip))) {
2918+				ip += ((ip - anchor) >> g_searchStrength) + 1;
2919+				continue;
2920+			}
2921+			{
2922+				const BYTE *matchEnd = matchIndex < dictLimit ? dictEnd : iend;
2923+				const BYTE *lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
2924+				U32 offset;
2925+				mLength = ZSTD_count_2segments(ip + EQUAL_READ32, match + EQUAL_READ32, iend, matchEnd, lowPrefixPtr) + EQUAL_READ32;
2926+				while (((ip > anchor) & (match > lowMatchPtr)) && (ip[-1] == match[-1])) {
2927+					ip--;
2928+					match--;
2929+					mLength++;
2930+				} /* catch up */
2931+				offset = curr - matchIndex;
2932+				offset_2 = offset_1;
2933+				offset_1 = offset;
2934+				ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
2935+			}
2936+		}
2937+
2938+		/* found a match : store it */
2939+		ip += mLength;
2940+		anchor = ip;
2941+
2942+		if (ip <= ilimit) {
2943+			/* Fill Table */
2944+			hashTable[ZSTD_hashPtr(base + curr + 2, hBits, mls)] = curr + 2;
2945+			hashTable[ZSTD_hashPtr(ip - 2, hBits, mls)] = (U32)(ip - 2 - base);
2946+			/* check immediate repcode */
2947+			while (ip <= ilimit) {
2948+				U32 const curr2 = (U32)(ip - base);
2949+				U32 const repIndex2 = curr2 - offset_2;
2950+				const BYTE *repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
2951+				if ((((U32)((dictLimit - 1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
2952+				    && (ZSTD_read32(repMatch2) == ZSTD_read32(ip))) {
2953+					const BYTE *const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
2954+					size_t repLength2 =
2955+					    ZSTD_count_2segments(ip + EQUAL_READ32, repMatch2 + EQUAL_READ32, iend, repEnd2, lowPrefixPtr) + EQUAL_READ32;
2956+					U32 tmpOffset = offset_2;
2957+					offset_2 = offset_1;
2958+					offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
2959+					ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2 - MINMATCH);
2960+					hashTable[ZSTD_hashPtr(ip, hBits, mls)] = curr2;
2961+					ip += repLength2;
2962+					anchor = ip;
2963+					continue;
2964+				}
2965+				break;
2966+			}
2967+		}
2968+	}
2969+
2970+	/* save reps for next block */
2971+	ctx->repToConfirm[0] = offset_1;
2972+	ctx->repToConfirm[1] = offset_2;
2973+
2974+	/* Last Literals */
2975+	{
2976+		size_t const lastLLSize = iend - anchor;
2977+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
2978+		seqStorePtr->lit += lastLLSize;
2979+	}
2980+}
2981+
2982+static void ZSTD_compressBlock_fast_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
2983+{
2984+	U32 const mls = ctx->params.cParams.searchLength;
2985+	switch (mls) {
2986+	default: /* includes case 3 */
2987+	case 4: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 4); return;
2988+	case 5: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 5); return;
2989+	case 6: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 6); return;
2990+	case 7: ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 7); return;
2991+	}
2992+}
2993+
2994+/*-*************************************
2995+*  Double Fast
2996+***************************************/
2997+static void ZSTD_fillDoubleHashTable(ZSTD_CCtx *cctx, const void *end, const U32 mls)
2998+{
2999+	U32 *const hashLarge = cctx->hashTable;
3000+	U32 const hBitsL = cctx->params.cParams.hashLog;
3001+	U32 *const hashSmall = cctx->chainTable;
3002+	U32 const hBitsS = cctx->params.cParams.chainLog;
3003+	const BYTE *const base = cctx->base;
3004+	const BYTE *ip = base + cctx->nextToUpdate;
3005+	const BYTE *const iend = ((const BYTE *)end) - HASH_READ_SIZE;
3006+	const size_t fastHashFillStep = 3;
3007+
3008+	while (ip <= iend) {
3009+		hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base);
3010+		hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base);
3011+		ip += fastHashFillStep;
3012+	}
3013+}
3014+
3015+FORCE_INLINE
3016+void ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx *cctx, const void *src, size_t srcSize, const U32 mls)
3017+{
3018+	U32 *const hashLong = cctx->hashTable;
3019+	const U32 hBitsL = cctx->params.cParams.hashLog;
3020+	U32 *const hashSmall = cctx->chainTable;
3021+	const U32 hBitsS = cctx->params.cParams.chainLog;
3022+	seqStore_t *seqStorePtr = &(cctx->seqStore);
3023+	const BYTE *const base = cctx->base;
3024+	const BYTE *const istart = (const BYTE *)src;
3025+	const BYTE *ip = istart;
3026+	const BYTE *anchor = istart;
3027+	const U32 lowestIndex = cctx->dictLimit;
3028+	const BYTE *const lowest = base + lowestIndex;
3029+	const BYTE *const iend = istart + srcSize;
3030+	const BYTE *const ilimit = iend - HASH_READ_SIZE;
3031+	U32 offset_1 = cctx->rep[0], offset_2 = cctx->rep[1];
3032+	U32 offsetSaved = 0;
3033+
3034+	/* init */
3035+	ip += (ip == lowest);
3036+	{
3037+		U32 const maxRep = (U32)(ip - lowest);
3038+		if (offset_2 > maxRep)
3039+			offsetSaved = offset_2, offset_2 = 0;
3040+		if (offset_1 > maxRep)
3041+			offsetSaved = offset_1, offset_1 = 0;
3042+	}
3043+
3044+	/* Main Search Loop */
3045+	while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
3046+		size_t mLength;
3047+		size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
3048+		size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
3049+		U32 const curr = (U32)(ip - base);
3050+		U32 const matchIndexL = hashLong[h2];
3051+		U32 const matchIndexS = hashSmall[h];
3052+		const BYTE *matchLong = base + matchIndexL;
3053+		const BYTE *match = base + matchIndexS;
3054+		hashLong[h2] = hashSmall[h] = curr; /* update hash tables */
3055+
3056+		if ((offset_1 > 0) & (ZSTD_read32(ip + 1 - offset_1) == ZSTD_read32(ip + 1))) { /* note : by construction, offset_1 <= curr */
3057+			mLength = ZSTD_count(ip + 1 + 4, ip + 1 + 4 - offset_1, iend) + 4;
3058+			ip++;
3059+			ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
3060+		} else {
3061+			U32 offset;
3062+			if ((matchIndexL > lowestIndex) && (ZSTD_read64(matchLong) == ZSTD_read64(ip))) {
3063+				mLength = ZSTD_count(ip + 8, matchLong + 8, iend) + 8;
3064+				offset = (U32)(ip - matchLong);
3065+				while (((ip > anchor) & (matchLong > lowest)) && (ip[-1] == matchLong[-1])) {
3066+					ip--;
3067+					matchLong--;
3068+					mLength++;
3069+				} /* catch up */
3070+			} else if ((matchIndexS > lowestIndex) && (ZSTD_read32(match) == ZSTD_read32(ip))) {
3071+				size_t const h3 = ZSTD_hashPtr(ip + 1, hBitsL, 8);
3072+				U32 const matchIndex3 = hashLong[h3];
3073+				const BYTE *match3 = base + matchIndex3;
3074+				hashLong[h3] = curr + 1;
3075+				if ((matchIndex3 > lowestIndex) && (ZSTD_read64(match3) == ZSTD_read64(ip + 1))) {
3076+					mLength = ZSTD_count(ip + 9, match3 + 8, iend) + 8;
3077+					ip++;
3078+					offset = (U32)(ip - match3);
3079+					while (((ip > anchor) & (match3 > lowest)) && (ip[-1] == match3[-1])) {
3080+						ip--;
3081+						match3--;
3082+						mLength++;
3083+					} /* catch up */
3084+				} else {
3085+					mLength = ZSTD_count(ip + 4, match + 4, iend) + 4;
3086+					offset = (U32)(ip - match);
3087+					while (((ip > anchor) & (match > lowest)) && (ip[-1] == match[-1])) {
3088+						ip--;
3089+						match--;
3090+						mLength++;
3091+					} /* catch up */
3092+				}
3093+			} else {
3094+				ip += ((ip - anchor) >> g_searchStrength) + 1;
3095+				continue;
3096+			}
3097+
3098+			offset_2 = offset_1;
3099+			offset_1 = offset;
3100+
3101+			ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
3102+		}
3103+
3104+		/* match found */
3105+		ip += mLength;
3106+		anchor = ip;
3107+
3108+		if (ip <= ilimit) {
3109+			/* Fill Table */
3110+			hashLong[ZSTD_hashPtr(base + curr + 2, hBitsL, 8)] = hashSmall[ZSTD_hashPtr(base + curr + 2, hBitsS, mls)] =
3111+			    curr + 2; /* here because curr+2 could be > iend-8 */
3112+			hashLong[ZSTD_hashPtr(ip - 2, hBitsL, 8)] = hashSmall[ZSTD_hashPtr(ip - 2, hBitsS, mls)] = (U32)(ip - 2 - base);
3113+
3114+			/* check immediate repcode */
3115+			while ((ip <= ilimit) && ((offset_2 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_2)))) {
3116+				/* store sequence */
3117+				size_t const rLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4;
3118+				{
3119+					U32 const tmpOff = offset_2;
3120+					offset_2 = offset_1;
3121+					offset_1 = tmpOff;
3122+				} /* swap offset_2 <=> offset_1 */
3123+				hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base);
3124+				hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base);
3125+				ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength - MINMATCH);
3126+				ip += rLength;
3127+				anchor = ip;
3128+				continue; /* faster when present ... (?) */
3129+			}
3130+		}
3131+	}
3132+
3133+	/* save reps for next block */
3134+	cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved;
3135+	cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved;
3136+
3137+	/* Last Literals */
3138+	{
3139+		size_t const lastLLSize = iend - anchor;
3140+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
3141+		seqStorePtr->lit += lastLLSize;
3142+	}
3143+}
3144+
3145+static void ZSTD_compressBlock_doubleFast(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
3146+{
3147+	const U32 mls = ctx->params.cParams.searchLength;
3148+	switch (mls) {
3149+	default: /* includes case 3 */
3150+	case 4: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4); return;
3151+	case 5: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5); return;
3152+	case 6: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6); return;
3153+	case 7: ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7); return;
3154+	}
3155+}
3156+
3157+static void ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 mls)
3158+{
3159+	U32 *const hashLong = ctx->hashTable;
3160+	U32 const hBitsL = ctx->params.cParams.hashLog;
3161+	U32 *const hashSmall = ctx->chainTable;
3162+	U32 const hBitsS = ctx->params.cParams.chainLog;
3163+	seqStore_t *seqStorePtr = &(ctx->seqStore);
3164+	const BYTE *const base = ctx->base;
3165+	const BYTE *const dictBase = ctx->dictBase;
3166+	const BYTE *const istart = (const BYTE *)src;
3167+	const BYTE *ip = istart;
3168+	const BYTE *anchor = istart;
3169+	const U32 lowestIndex = ctx->lowLimit;
3170+	const BYTE *const dictStart = dictBase + lowestIndex;
3171+	const U32 dictLimit = ctx->dictLimit;
3172+	const BYTE *const lowPrefixPtr = base + dictLimit;
3173+	const BYTE *const dictEnd = dictBase + dictLimit;
3174+	const BYTE *const iend = istart + srcSize;
3175+	const BYTE *const ilimit = iend - 8;
3176+	U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1];
3177+
3178+	/* Search Loop */
3179+	while (ip < ilimit) { /* < instead of <=, because (ip+1) */
3180+		const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
3181+		const U32 matchIndex = hashSmall[hSmall];
3182+		const BYTE *matchBase = matchIndex < dictLimit ? dictBase : base;
3183+		const BYTE *match = matchBase + matchIndex;
3184+
3185+		const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
3186+		const U32 matchLongIndex = hashLong[hLong];
3187+		const BYTE *matchLongBase = matchLongIndex < dictLimit ? dictBase : base;
3188+		const BYTE *matchLong = matchLongBase + matchLongIndex;
3189+
3190+		const U32 curr = (U32)(ip - base);
3191+		const U32 repIndex = curr + 1 - offset_1; /* offset_1 expected <= curr +1 */
3192+		const BYTE *repBase = repIndex < dictLimit ? dictBase : base;
3193+		const BYTE *repMatch = repBase + repIndex;
3194+		size_t mLength;
3195+		hashSmall[hSmall] = hashLong[hLong] = curr; /* update hash table */
3196+
3197+		if ((((U32)((dictLimit - 1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) &&
3198+		    (ZSTD_read32(repMatch) == ZSTD_read32(ip + 1))) {
3199+			const BYTE *repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
3200+			mLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, lowPrefixPtr) + 4;
3201+			ip++;
3202+			ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, 0, mLength - MINMATCH);
3203+		} else {
3204+			if ((matchLongIndex > lowestIndex) && (ZSTD_read64(matchLong) == ZSTD_read64(ip))) {
3205+				const BYTE *matchEnd = matchLongIndex < dictLimit ? dictEnd : iend;
3206+				const BYTE *lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr;
3207+				U32 offset;
3208+				mLength = ZSTD_count_2segments(ip + 8, matchLong + 8, iend, matchEnd, lowPrefixPtr) + 8;
3209+				offset = curr - matchLongIndex;
3210+				while (((ip > anchor) & (matchLong > lowMatchPtr)) && (ip[-1] == matchLong[-1])) {
3211+					ip--;
3212+					matchLong--;
3213+					mLength++;
3214+				} /* catch up */
3215+				offset_2 = offset_1;
3216+				offset_1 = offset;
3217+				ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
3218+
3219+			} else if ((matchIndex > lowestIndex) && (ZSTD_read32(match) == ZSTD_read32(ip))) {
3220+				size_t const h3 = ZSTD_hashPtr(ip + 1, hBitsL, 8);
3221+				U32 const matchIndex3 = hashLong[h3];
3222+				const BYTE *const match3Base = matchIndex3 < dictLimit ? dictBase : base;
3223+				const BYTE *match3 = match3Base + matchIndex3;
3224+				U32 offset;
3225+				hashLong[h3] = curr + 1;
3226+				if ((matchIndex3 > lowestIndex) && (ZSTD_read64(match3) == ZSTD_read64(ip + 1))) {
3227+					const BYTE *matchEnd = matchIndex3 < dictLimit ? dictEnd : iend;
3228+					const BYTE *lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr;
3229+					mLength = ZSTD_count_2segments(ip + 9, match3 + 8, iend, matchEnd, lowPrefixPtr) + 8;
3230+					ip++;
3231+					offset = curr + 1 - matchIndex3;
3232+					while (((ip > anchor) & (match3 > lowMatchPtr)) && (ip[-1] == match3[-1])) {
3233+						ip--;
3234+						match3--;
3235+						mLength++;
3236+					} /* catch up */
3237+				} else {
3238+					const BYTE *matchEnd = matchIndex < dictLimit ? dictEnd : iend;
3239+					const BYTE *lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
3240+					mLength = ZSTD_count_2segments(ip + 4, match + 4, iend, matchEnd, lowPrefixPtr) + 4;
3241+					offset = curr - matchIndex;
3242+					while (((ip > anchor) & (match > lowMatchPtr)) && (ip[-1] == match[-1])) {
3243+						ip--;
3244+						match--;
3245+						mLength++;
3246+					} /* catch up */
3247+				}
3248+				offset_2 = offset_1;
3249+				offset_1 = offset;
3250+				ZSTD_storeSeq(seqStorePtr, ip - anchor, anchor, offset + ZSTD_REP_MOVE, mLength - MINMATCH);
3251+
3252+			} else {
3253+				ip += ((ip - anchor) >> g_searchStrength) + 1;
3254+				continue;
3255+			}
3256+		}
3257+
3258+		/* found a match : store it */
3259+		ip += mLength;
3260+		anchor = ip;
3261+
3262+		if (ip <= ilimit) {
3263+			/* Fill Table */
3264+			hashSmall[ZSTD_hashPtr(base + curr + 2, hBitsS, mls)] = curr + 2;
3265+			hashLong[ZSTD_hashPtr(base + curr + 2, hBitsL, 8)] = curr + 2;
3266+			hashSmall[ZSTD_hashPtr(ip - 2, hBitsS, mls)] = (U32)(ip - 2 - base);
3267+			hashLong[ZSTD_hashPtr(ip - 2, hBitsL, 8)] = (U32)(ip - 2 - base);
3268+			/* check immediate repcode */
3269+			while (ip <= ilimit) {
3270+				U32 const curr2 = (U32)(ip - base);
3271+				U32 const repIndex2 = curr2 - offset_2;
3272+				const BYTE *repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
3273+				if ((((U32)((dictLimit - 1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
3274+				    && (ZSTD_read32(repMatch2) == ZSTD_read32(ip))) {
3275+					const BYTE *const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
3276+					size_t const repLength2 =
3277+					    ZSTD_count_2segments(ip + EQUAL_READ32, repMatch2 + EQUAL_READ32, iend, repEnd2, lowPrefixPtr) + EQUAL_READ32;
3278+					U32 tmpOffset = offset_2;
3279+					offset_2 = offset_1;
3280+					offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
3281+					ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2 - MINMATCH);
3282+					hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = curr2;
3283+					hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = curr2;
3284+					ip += repLength2;
3285+					anchor = ip;
3286+					continue;
3287+				}
3288+				break;
3289+			}
3290+		}
3291+	}
3292+
3293+	/* save reps for next block */
3294+	ctx->repToConfirm[0] = offset_1;
3295+	ctx->repToConfirm[1] = offset_2;
3296+
3297+	/* Last Literals */
3298+	{
3299+		size_t const lastLLSize = iend - anchor;
3300+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
3301+		seqStorePtr->lit += lastLLSize;
3302+	}
3303+}
3304+
3305+static void ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
3306+{
3307+	U32 const mls = ctx->params.cParams.searchLength;
3308+	switch (mls) {
3309+	default: /* includes case 3 */
3310+	case 4: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4); return;
3311+	case 5: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5); return;
3312+	case 6: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6); return;
3313+	case 7: ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7); return;
3314+	}
3315+}
3316+
3317+/*-*************************************
3318+*  Binary Tree search
3319+***************************************/
3320+/** ZSTD_insertBt1() : add one or multiple positions to tree.
3321+*   ip : assumed <= iend-8 .
3322+*   @return : nb of positions added */
3323+static U32 ZSTD_insertBt1(ZSTD_CCtx *zc, const BYTE *const ip, const U32 mls, const BYTE *const iend, U32 nbCompares, U32 extDict)
3324+{
3325+	U32 *const hashTable = zc->hashTable;
3326+	U32 const hashLog = zc->params.cParams.hashLog;
3327+	size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
3328+	U32 *const bt = zc->chainTable;
3329+	U32 const btLog = zc->params.cParams.chainLog - 1;
3330+	U32 const btMask = (1 << btLog) - 1;
3331+	U32 matchIndex = hashTable[h];
3332+	size_t commonLengthSmaller = 0, commonLengthLarger = 0;
3333+	const BYTE *const base = zc->base;
3334+	const BYTE *const dictBase = zc->dictBase;
3335+	const U32 dictLimit = zc->dictLimit;
3336+	const BYTE *const dictEnd = dictBase + dictLimit;
3337+	const BYTE *const prefixStart = base + dictLimit;
3338+	const BYTE *match;
3339+	const U32 curr = (U32)(ip - base);
3340+	const U32 btLow = btMask >= curr ? 0 : curr - btMask;
3341+	U32 *smallerPtr = bt + 2 * (curr & btMask);
3342+	U32 *largerPtr = smallerPtr + 1;
3343+	U32 dummy32; /* to be nullified at the end */
3344+	U32 const windowLow = zc->lowLimit;
3345+	U32 matchEndIdx = curr + 8;
3346+	size_t bestLength = 8;
3347+
3348+	hashTable[h] = curr; /* Update Hash Table */
3349+
3350+	while (nbCompares-- && (matchIndex > windowLow)) {
3351+		U32 *const nextPtr = bt + 2 * (matchIndex & btMask);
3352+		size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
3353+
3354+		if ((!extDict) || (matchIndex + matchLength >= dictLimit)) {
3355+			match = base + matchIndex;
3356+			if (match[matchLength] == ip[matchLength])
3357+				matchLength += ZSTD_count(ip + matchLength + 1, match + matchLength + 1, iend) + 1;
3358+		} else {
3359+			match = dictBase + matchIndex;
3360+			matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart);
3361+			if (matchIndex + matchLength >= dictLimit)
3362+				match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
3363+		}
3364+
3365+		if (matchLength > bestLength) {
3366+			bestLength = matchLength;
3367+			if (matchLength > matchEndIdx - matchIndex)
3368+				matchEndIdx = matchIndex + (U32)matchLength;
3369+		}
3370+
3371+		if (ip + matchLength == iend) /* equal : no way to know if inf or sup */
3372+			break;		      /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt the tree */
3373+
3374+		if (match[matchLength] < ip[matchLength]) { /* necessarily within correct buffer */
3375+			/* match is smaller than curr */
3376+			*smallerPtr = matchIndex;	  /* update smaller idx */
3377+			commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
3378+			if (matchIndex <= btLow) {
3379+				smallerPtr = &dummy32;
3380+				break;
3381+			}			  /* beyond tree size, stop the search */
3382+			smallerPtr = nextPtr + 1; /* new "smaller" => larger of match */
3383+			matchIndex = nextPtr[1];  /* new matchIndex larger than previous (closer to curr) */
3384+		} else {
3385+			/* match is larger than curr */
3386+			*largerPtr = matchIndex;
3387+			commonLengthLarger = matchLength;
3388+			if (matchIndex <= btLow) {
3389+				largerPtr = &dummy32;
3390+				break;
3391+			} /* beyond tree size, stop the search */
3392+			largerPtr = nextPtr;
3393+			matchIndex = nextPtr[0];
3394+		}
3395+	}
3396+
3397+	*smallerPtr = *largerPtr = 0;
3398+	if (bestLength > 384)
3399+		return MIN(192, (U32)(bestLength - 384)); /* speed optimization */
3400+	if (matchEndIdx > curr + 8)
3401+		return matchEndIdx - curr - 8;
3402+	return 1;
3403+}
3404+
3405+static size_t ZSTD_insertBtAndFindBestMatch(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iend, size_t *offsetPtr, U32 nbCompares, const U32 mls,
3406+					    U32 extDict)
3407+{
3408+	U32 *const hashTable = zc->hashTable;
3409+	U32 const hashLog = zc->params.cParams.hashLog;
3410+	size_t const h = ZSTD_hashPtr(ip, hashLog, mls);
3411+	U32 *const bt = zc->chainTable;
3412+	U32 const btLog = zc->params.cParams.chainLog - 1;
3413+	U32 const btMask = (1 << btLog) - 1;
3414+	U32 matchIndex = hashTable[h];
3415+	size_t commonLengthSmaller = 0, commonLengthLarger = 0;
3416+	const BYTE *const base = zc->base;
3417+	const BYTE *const dictBase = zc->dictBase;
3418+	const U32 dictLimit = zc->dictLimit;
3419+	const BYTE *const dictEnd = dictBase + dictLimit;
3420+	const BYTE *const prefixStart = base + dictLimit;
3421+	const U32 curr = (U32)(ip - base);
3422+	const U32 btLow = btMask >= curr ? 0 : curr - btMask;
3423+	const U32 windowLow = zc->lowLimit;
3424+	U32 *smallerPtr = bt + 2 * (curr & btMask);
3425+	U32 *largerPtr = bt + 2 * (curr & btMask) + 1;
3426+	U32 matchEndIdx = curr + 8;
3427+	U32 dummy32; /* to be nullified at the end */
3428+	size_t bestLength = 0;
3429+
3430+	hashTable[h] = curr; /* Update Hash Table */
3431+
3432+	while (nbCompares-- && (matchIndex > windowLow)) {
3433+		U32 *const nextPtr = bt + 2 * (matchIndex & btMask);
3434+		size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
3435+		const BYTE *match;
3436+
3437+		if ((!extDict) || (matchIndex + matchLength >= dictLimit)) {
3438+			match = base + matchIndex;
3439+			if (match[matchLength] == ip[matchLength])
3440+				matchLength += ZSTD_count(ip + matchLength + 1, match + matchLength + 1, iend) + 1;
3441+		} else {
3442+			match = dictBase + matchIndex;
3443+			matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart);
3444+			if (matchIndex + matchLength >= dictLimit)
3445+				match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
3446+		}
3447+
3448+		if (matchLength > bestLength) {
3449+			if (matchLength > matchEndIdx - matchIndex)
3450+				matchEndIdx = matchIndex + (U32)matchLength;
3451+			if ((4 * (int)(matchLength - bestLength)) > (int)(ZSTD_highbit32(curr - matchIndex + 1) - ZSTD_highbit32((U32)offsetPtr[0] + 1)))
3452+				bestLength = matchLength, *offsetPtr = ZSTD_REP_MOVE + curr - matchIndex;
3453+			if (ip + matchLength == iend) /* equal : no way to know if inf or sup */
3454+				break;		      /* drop, to guarantee consistency (miss a little bit of compression) */
3455+		}
3456+
3457+		if (match[matchLength] < ip[matchLength]) {
3458+			/* match is smaller than curr */
3459+			*smallerPtr = matchIndex;	  /* update smaller idx */
3460+			commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
3461+			if (matchIndex <= btLow) {
3462+				smallerPtr = &dummy32;
3463+				break;
3464+			}			  /* beyond tree size, stop the search */
3465+			smallerPtr = nextPtr + 1; /* new "smaller" => larger of match */
3466+			matchIndex = nextPtr[1];  /* new matchIndex larger than previous (closer to curr) */
3467+		} else {
3468+			/* match is larger than curr */
3469+			*largerPtr = matchIndex;
3470+			commonLengthLarger = matchLength;
3471+			if (matchIndex <= btLow) {
3472+				largerPtr = &dummy32;
3473+				break;
3474+			} /* beyond tree size, stop the search */
3475+			largerPtr = nextPtr;
3476+			matchIndex = nextPtr[0];
3477+		}
3478+	}
3479+
3480+	*smallerPtr = *largerPtr = 0;
3481+
3482+	zc->nextToUpdate = (matchEndIdx > curr + 8) ? matchEndIdx - 8 : curr + 1;
3483+	return bestLength;
3484+}
3485+
3486+static void ZSTD_updateTree(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iend, const U32 nbCompares, const U32 mls)
3487+{
3488+	const BYTE *const base = zc->base;
3489+	const U32 target = (U32)(ip - base);
3490+	U32 idx = zc->nextToUpdate;
3491+
3492+	while (idx < target)
3493+		idx += ZSTD_insertBt1(zc, base + idx, mls, iend, nbCompares, 0);
3494+}
3495+
3496+/** ZSTD_BtFindBestMatch() : Tree updater, providing best match */
3497+static size_t ZSTD_BtFindBestMatch(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts, const U32 mls)
3498+{
3499+	if (ip < zc->base + zc->nextToUpdate)
3500+		return 0; /* skipped area */
3501+	ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls);
3502+	return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 0);
3503+}
3504+
3505+static size_t ZSTD_BtFindBestMatch_selectMLS(ZSTD_CCtx *zc, /* Index table will be updated */
3506+					     const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts, const U32 matchLengthSearch)
3507+{
3508+	switch (matchLengthSearch) {
3509+	default: /* includes case 3 */
3510+	case 4: return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4);
3511+	case 5: return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5);
3512+	case 7:
3513+	case 6: return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6);
3514+	}
3515+}
3516+
3517+static void ZSTD_updateTree_extDict(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iend, const U32 nbCompares, const U32 mls)
3518+{
3519+	const BYTE *const base = zc->base;
3520+	const U32 target = (U32)(ip - base);
3521+	U32 idx = zc->nextToUpdate;
3522+
3523+	while (idx < target)
3524+		idx += ZSTD_insertBt1(zc, base + idx, mls, iend, nbCompares, 1);
3525+}
3526+
3527+/** Tree updater, providing best match */
3528+static size_t ZSTD_BtFindBestMatch_extDict(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
3529+					   const U32 mls)
3530+{
3531+	if (ip < zc->base + zc->nextToUpdate)
3532+		return 0; /* skipped area */
3533+	ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls);
3534+	return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 1);
3535+}
3536+
3537+static size_t ZSTD_BtFindBestMatch_selectMLS_extDict(ZSTD_CCtx *zc, /* Index table will be updated */
3538+						     const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
3539+						     const U32 matchLengthSearch)
3540+{
3541+	switch (matchLengthSearch) {
3542+	default: /* includes case 3 */
3543+	case 4: return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4);
3544+	case 5: return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5);
3545+	case 7:
3546+	case 6: return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6);
3547+	}
3548+}
3549+
3550+/* *********************************
3551+*  Hash Chain
3552+***********************************/
3553+#define NEXT_IN_CHAIN(d, mask) chainTable[(d)&mask]
3554+
3555+/* Update chains up to ip (excluded)
3556+   Assumption : always within prefix (i.e. not within extDict) */
3557+FORCE_INLINE
3558+U32 ZSTD_insertAndFindFirstIndex(ZSTD_CCtx *zc, const BYTE *ip, U32 mls)
3559+{
3560+	U32 *const hashTable = zc->hashTable;
3561+	const U32 hashLog = zc->params.cParams.hashLog;
3562+	U32 *const chainTable = zc->chainTable;
3563+	const U32 chainMask = (1 << zc->params.cParams.chainLog) - 1;
3564+	const BYTE *const base = zc->base;
3565+	const U32 target = (U32)(ip - base);
3566+	U32 idx = zc->nextToUpdate;
3567+
3568+	while (idx < target) { /* catch up */
3569+		size_t const h = ZSTD_hashPtr(base + idx, hashLog, mls);
3570+		NEXT_IN_CHAIN(idx, chainMask) = hashTable[h];
3571+		hashTable[h] = idx;
3572+		idx++;
3573+	}
3574+
3575+	zc->nextToUpdate = target;
3576+	return hashTable[ZSTD_hashPtr(ip, hashLog, mls)];
3577+}
3578+
3579+/* inlining is important to hardwire a hot branch (template emulation) */
3580+FORCE_INLINE
3581+size_t ZSTD_HcFindBestMatch_generic(ZSTD_CCtx *zc, /* Index table will be updated */
3582+				    const BYTE *const ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts, const U32 mls,
3583+				    const U32 extDict)
3584+{
3585+	U32 *const chainTable = zc->chainTable;
3586+	const U32 chainSize = (1 << zc->params.cParams.chainLog);
3587+	const U32 chainMask = chainSize - 1;
3588+	const BYTE *const base = zc->base;
3589+	const BYTE *const dictBase = zc->dictBase;
3590+	const U32 dictLimit = zc->dictLimit;
3591+	const BYTE *const prefixStart = base + dictLimit;
3592+	const BYTE *const dictEnd = dictBase + dictLimit;
3593+	const U32 lowLimit = zc->lowLimit;
3594+	const U32 curr = (U32)(ip - base);
3595+	const U32 minChain = curr > chainSize ? curr - chainSize : 0;
3596+	int nbAttempts = maxNbAttempts;
3597+	size_t ml = EQUAL_READ32 - 1;
3598+
3599+	/* HC4 match finder */
3600+	U32 matchIndex = ZSTD_insertAndFindFirstIndex(zc, ip, mls);
3601+
3602+	for (; (matchIndex > lowLimit) & (nbAttempts > 0); nbAttempts--) {
3603+		const BYTE *match;
3604+		size_t currMl = 0;
3605+		if ((!extDict) || matchIndex >= dictLimit) {
3606+			match = base + matchIndex;
3607+			if (match[ml] == ip[ml]) /* potentially better */
3608+				currMl = ZSTD_count(ip, match, iLimit);
3609+		} else {
3610+			match = dictBase + matchIndex;
3611+			if (ZSTD_read32(match) == ZSTD_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */
3612+				currMl = ZSTD_count_2segments(ip + EQUAL_READ32, match + EQUAL_READ32, iLimit, dictEnd, prefixStart) + EQUAL_READ32;
3613+		}
3614+
3615+		/* save best solution */
3616+		if (currMl > ml) {
3617+			ml = currMl;
3618+			*offsetPtr = curr - matchIndex + ZSTD_REP_MOVE;
3619+			if (ip + currMl == iLimit)
3620+				break; /* best possible, and avoid read overflow*/
3621+		}
3622+
3623+		if (matchIndex <= minChain)
3624+			break;
3625+		matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask);
3626+	}
3627+
3628+	return ml;
3629+}
3630+
3631+FORCE_INLINE size_t ZSTD_HcFindBestMatch_selectMLS(ZSTD_CCtx *zc, const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
3632+						   const U32 matchLengthSearch)
3633+{
3634+	switch (matchLengthSearch) {
3635+	default: /* includes case 3 */
3636+	case 4: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 0);
3637+	case 5: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 0);
3638+	case 7:
3639+	case 6: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 0);
3640+	}
3641+}
3642+
3643+FORCE_INLINE size_t ZSTD_HcFindBestMatch_extDict_selectMLS(ZSTD_CCtx *zc, const BYTE *ip, const BYTE *const iLimit, size_t *offsetPtr, const U32 maxNbAttempts,
3644+							   const U32 matchLengthSearch)
3645+{
3646+	switch (matchLengthSearch) {
3647+	default: /* includes case 3 */
3648+	case 4: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 1);
3649+	case 5: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 1);
3650+	case 7:
3651+	case 6: return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 1);
3652+	}
3653+}
3654+
3655+/* *******************************
3656+*  Common parser - lazy strategy
3657+*********************************/
3658+FORCE_INLINE
3659+void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 searchMethod, const U32 depth)
3660+{
3661+	seqStore_t *seqStorePtr = &(ctx->seqStore);
3662+	const BYTE *const istart = (const BYTE *)src;
3663+	const BYTE *ip = istart;
3664+	const BYTE *anchor = istart;
3665+	const BYTE *const iend = istart + srcSize;
3666+	const BYTE *const ilimit = iend - 8;
3667+	const BYTE *const base = ctx->base + ctx->dictLimit;
3668+
3669+	U32 const maxSearches = 1 << ctx->params.cParams.searchLog;
3670+	U32 const mls = ctx->params.cParams.searchLength;
3671+
3672+	typedef size_t (*searchMax_f)(ZSTD_CCtx * zc, const BYTE *ip, const BYTE *iLimit, size_t *offsetPtr, U32 maxNbAttempts, U32 matchLengthSearch);
3673+	searchMax_f const searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS;
3674+	U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1], savedOffset = 0;
3675+
3676+	/* init */
3677+	ip += (ip == base);
3678+	ctx->nextToUpdate3 = ctx->nextToUpdate;
3679+	{
3680+		U32 const maxRep = (U32)(ip - base);
3681+		if (offset_2 > maxRep)
3682+			savedOffset = offset_2, offset_2 = 0;
3683+		if (offset_1 > maxRep)
3684+			savedOffset = offset_1, offset_1 = 0;
3685+	}
3686+
3687+	/* Match Loop */
3688+	while (ip < ilimit) {
3689+		size_t matchLength = 0;
3690+		size_t offset = 0;
3691+		const BYTE *start = ip + 1;
3692+
3693+		/* check repCode */
3694+		if ((offset_1 > 0) & (ZSTD_read32(ip + 1) == ZSTD_read32(ip + 1 - offset_1))) {
3695+			/* repcode : we take it */
3696+			matchLength = ZSTD_count(ip + 1 + EQUAL_READ32, ip + 1 + EQUAL_READ32 - offset_1, iend) + EQUAL_READ32;
3697+			if (depth == 0)
3698+				goto _storeSequence;
3699+		}
3700+
3701+		/* first search (depth 0) */
3702+		{
3703+			size_t offsetFound = 99999999;
3704+			size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
3705+			if (ml2 > matchLength)
3706+				matchLength = ml2, start = ip, offset = offsetFound;
3707+		}
3708+
3709+		if (matchLength < EQUAL_READ32) {
3710+			ip += ((ip - anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */
3711+			continue;
3712+		}
3713+
3714+		/* let's try to find a better solution */
3715+		if (depth >= 1)
3716+			while (ip < ilimit) {
3717+				ip++;
3718+				if ((offset) && ((offset_1 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_1)))) {
3719+					size_t const mlRep = ZSTD_count(ip + EQUAL_READ32, ip + EQUAL_READ32 - offset_1, iend) + EQUAL_READ32;
3720+					int const gain2 = (int)(mlRep * 3);
3721+					int const gain1 = (int)(matchLength * 3 - ZSTD_highbit32((U32)offset + 1) + 1);
3722+					if ((mlRep >= EQUAL_READ32) && (gain2 > gain1))
3723+						matchLength = mlRep, offset = 0, start = ip;
3724+				}
3725+				{
3726+					size_t offset2 = 99999999;
3727+					size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
3728+					int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
3729+					int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 4);
3730+					if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
3731+						matchLength = ml2, offset = offset2, start = ip;
3732+						continue; /* search a better one */
3733+					}
3734+				}
3735+
3736+				/* let's find an even better one */
3737+				if ((depth == 2) && (ip < ilimit)) {
3738+					ip++;
3739+					if ((offset) && ((offset_1 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_1)))) {
3740+						size_t const ml2 = ZSTD_count(ip + EQUAL_READ32, ip + EQUAL_READ32 - offset_1, iend) + EQUAL_READ32;
3741+						int const gain2 = (int)(ml2 * 4);
3742+						int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 1);
3743+						if ((ml2 >= EQUAL_READ32) && (gain2 > gain1))
3744+							matchLength = ml2, offset = 0, start = ip;
3745+					}
3746+					{
3747+						size_t offset2 = 99999999;
3748+						size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
3749+						int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
3750+						int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 7);
3751+						if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
3752+							matchLength = ml2, offset = offset2, start = ip;
3753+							continue;
3754+						}
3755+					}
3756+				}
3757+				break; /* nothing found : store previous solution */
3758+			}
3759+
3760+		/* NOTE:
3761+		 * start[-offset+ZSTD_REP_MOVE-1] is undefined behavior.
3762+		 * (-offset+ZSTD_REP_MOVE-1) is unsigned, and is added to start, which
3763+		 * overflows the pointer, which is undefined behavior.
3764+		 */
3765+		/* catch up */
3766+		if (offset) {
3767+			while ((start > anchor) && (start > base + offset - ZSTD_REP_MOVE) &&
3768+			       (start[-1] == (start-offset+ZSTD_REP_MOVE)[-1])) /* only search for offset within prefix */
3769+			{
3770+				start--;
3771+				matchLength++;
3772+			}
3773+			offset_2 = offset_1;
3774+			offset_1 = (U32)(offset - ZSTD_REP_MOVE);
3775+		}
3776+
3777+	/* store sequence */
3778+_storeSequence:
3779+		{
3780+			size_t const litLength = start - anchor;
3781+			ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength - MINMATCH);
3782+			anchor = ip = start + matchLength;
3783+		}
3784+
3785+		/* check immediate repcode */
3786+		while ((ip <= ilimit) && ((offset_2 > 0) & (ZSTD_read32(ip) == ZSTD_read32(ip - offset_2)))) {
3787+			/* store sequence */
3788+			matchLength = ZSTD_count(ip + EQUAL_READ32, ip + EQUAL_READ32 - offset_2, iend) + EQUAL_READ32;
3789+			offset = offset_2;
3790+			offset_2 = offset_1;
3791+			offset_1 = (U32)offset; /* swap repcodes */
3792+			ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength - MINMATCH);
3793+			ip += matchLength;
3794+			anchor = ip;
3795+			continue; /* faster when present ... (?) */
3796+		}
3797+	}
3798+
3799+	/* Save reps for next block */
3800+	ctx->repToConfirm[0] = offset_1 ? offset_1 : savedOffset;
3801+	ctx->repToConfirm[1] = offset_2 ? offset_2 : savedOffset;
3802+
3803+	/* Last Literals */
3804+	{
3805+		size_t const lastLLSize = iend - anchor;
3806+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
3807+		seqStorePtr->lit += lastLLSize;
3808+	}
3809+}
3810+
3811+static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 1, 2); }
3812+
3813+static void ZSTD_compressBlock_lazy2(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 2); }
3814+
3815+static void ZSTD_compressBlock_lazy(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 1); }
3816+
3817+static void ZSTD_compressBlock_greedy(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 0); }
3818+
3819+FORCE_INLINE
3820+void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const U32 searchMethod, const U32 depth)
3821+{
3822+	seqStore_t *seqStorePtr = &(ctx->seqStore);
3823+	const BYTE *const istart = (const BYTE *)src;
3824+	const BYTE *ip = istart;
3825+	const BYTE *anchor = istart;
3826+	const BYTE *const iend = istart + srcSize;
3827+	const BYTE *const ilimit = iend - 8;
3828+	const BYTE *const base = ctx->base;
3829+	const U32 dictLimit = ctx->dictLimit;
3830+	const U32 lowestIndex = ctx->lowLimit;
3831+	const BYTE *const prefixStart = base + dictLimit;
3832+	const BYTE *const dictBase = ctx->dictBase;
3833+	const BYTE *const dictEnd = dictBase + dictLimit;
3834+	const BYTE *const dictStart = dictBase + ctx->lowLimit;
3835+
3836+	const U32 maxSearches = 1 << ctx->params.cParams.searchLog;
3837+	const U32 mls = ctx->params.cParams.searchLength;
3838+
3839+	typedef size_t (*searchMax_f)(ZSTD_CCtx * zc, const BYTE *ip, const BYTE *iLimit, size_t *offsetPtr, U32 maxNbAttempts, U32 matchLengthSearch);
3840+	searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS_extDict : ZSTD_HcFindBestMatch_extDict_selectMLS;
3841+
3842+	U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1];
3843+
3844+	/* init */
3845+	ctx->nextToUpdate3 = ctx->nextToUpdate;
3846+	ip += (ip == prefixStart);
3847+
3848+	/* Match Loop */
3849+	while (ip < ilimit) {
3850+		size_t matchLength = 0;
3851+		size_t offset = 0;
3852+		const BYTE *start = ip + 1;
3853+		U32 curr = (U32)(ip - base);
3854+
3855+		/* check repCode */
3856+		{
3857+			const U32 repIndex = (U32)(curr + 1 - offset_1);
3858+			const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
3859+			const BYTE *const repMatch = repBase + repIndex;
3860+			if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
3861+				if (ZSTD_read32(ip + 1) == ZSTD_read32(repMatch)) {
3862+					/* repcode detected we should take it */
3863+					const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
3864+					matchLength =
3865+					    ZSTD_count_2segments(ip + 1 + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repEnd, prefixStart) + EQUAL_READ32;
3866+					if (depth == 0)
3867+						goto _storeSequence;
3868+				}
3869+		}
3870+
3871+		/* first search (depth 0) */
3872+		{
3873+			size_t offsetFound = 99999999;
3874+			size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls);
3875+			if (ml2 > matchLength)
3876+				matchLength = ml2, start = ip, offset = offsetFound;
3877+		}
3878+
3879+		if (matchLength < EQUAL_READ32) {
3880+			ip += ((ip - anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */
3881+			continue;
3882+		}
3883+
3884+		/* let's try to find a better solution */
3885+		if (depth >= 1)
3886+			while (ip < ilimit) {
3887+				ip++;
3888+				curr++;
3889+				/* check repCode */
3890+				if (offset) {
3891+					const U32 repIndex = (U32)(curr - offset_1);
3892+					const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
3893+					const BYTE *const repMatch = repBase + repIndex;
3894+					if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
3895+						if (ZSTD_read32(ip) == ZSTD_read32(repMatch)) {
3896+							/* repcode detected */
3897+							const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
3898+							size_t const repLength =
3899+							    ZSTD_count_2segments(ip + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repEnd, prefixStart) +
3900+							    EQUAL_READ32;
3901+							int const gain2 = (int)(repLength * 3);
3902+							int const gain1 = (int)(matchLength * 3 - ZSTD_highbit32((U32)offset + 1) + 1);
3903+							if ((repLength >= EQUAL_READ32) && (gain2 > gain1))
3904+								matchLength = repLength, offset = 0, start = ip;
3905+						}
3906+				}
3907+
3908+				/* search match, depth 1 */
3909+				{
3910+					size_t offset2 = 99999999;
3911+					size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
3912+					int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
3913+					int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 4);
3914+					if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
3915+						matchLength = ml2, offset = offset2, start = ip;
3916+						continue; /* search a better one */
3917+					}
3918+				}
3919+
3920+				/* let's find an even better one */
3921+				if ((depth == 2) && (ip < ilimit)) {
3922+					ip++;
3923+					curr++;
3924+					/* check repCode */
3925+					if (offset) {
3926+						const U32 repIndex = (U32)(curr - offset_1);
3927+						const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
3928+						const BYTE *const repMatch = repBase + repIndex;
3929+						if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
3930+							if (ZSTD_read32(ip) == ZSTD_read32(repMatch)) {
3931+								/* repcode detected */
3932+								const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
3933+								size_t repLength = ZSTD_count_2segments(ip + EQUAL_READ32, repMatch + EQUAL_READ32, iend,
3934+													repEnd, prefixStart) +
3935+										   EQUAL_READ32;
3936+								int gain2 = (int)(repLength * 4);
3937+								int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 1);
3938+								if ((repLength >= EQUAL_READ32) && (gain2 > gain1))
3939+									matchLength = repLength, offset = 0, start = ip;
3940+							}
3941+					}
3942+
3943+					/* search match, depth 2 */
3944+					{
3945+						size_t offset2 = 99999999;
3946+						size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls);
3947+						int const gain2 = (int)(ml2 * 4 - ZSTD_highbit32((U32)offset2 + 1)); /* raw approx */
3948+						int const gain1 = (int)(matchLength * 4 - ZSTD_highbit32((U32)offset + 1) + 7);
3949+						if ((ml2 >= EQUAL_READ32) && (gain2 > gain1)) {
3950+							matchLength = ml2, offset = offset2, start = ip;
3951+							continue;
3952+						}
3953+					}
3954+				}
3955+				break; /* nothing found : store previous solution */
3956+			}
3957+
3958+		/* catch up */
3959+		if (offset) {
3960+			U32 const matchIndex = (U32)((start - base) - (offset - ZSTD_REP_MOVE));
3961+			const BYTE *match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex;
3962+			const BYTE *const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart;
3963+			while ((start > anchor) && (match > mStart) && (start[-1] == match[-1])) {
3964+				start--;
3965+				match--;
3966+				matchLength++;
3967+			} /* catch up */
3968+			offset_2 = offset_1;
3969+			offset_1 = (U32)(offset - ZSTD_REP_MOVE);
3970+		}
3971+
3972+	/* store sequence */
3973+	_storeSequence : {
3974+		size_t const litLength = start - anchor;
3975+		ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength - MINMATCH);
3976+		anchor = ip = start + matchLength;
3977+	}
3978+
3979+		/* check immediate repcode */
3980+		while (ip <= ilimit) {
3981+			const U32 repIndex = (U32)((ip - base) - offset_2);
3982+			const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
3983+			const BYTE *const repMatch = repBase + repIndex;
3984+			if (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
3985+				if (ZSTD_read32(ip) == ZSTD_read32(repMatch)) {
3986+					/* repcode detected we should take it */
3987+					const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
3988+					matchLength =
3989+					    ZSTD_count_2segments(ip + EQUAL_READ32, repMatch + EQUAL_READ32, iend, repEnd, prefixStart) + EQUAL_READ32;
3990+					offset = offset_2;
3991+					offset_2 = offset_1;
3992+					offset_1 = (U32)offset; /* swap offset history */
3993+					ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength - MINMATCH);
3994+					ip += matchLength;
3995+					anchor = ip;
3996+					continue; /* faster when present ... (?) */
3997+				}
3998+			break;
3999+		}
4000+	}
4001+
4002+	/* Save reps for next block */
4003+	ctx->repToConfirm[0] = offset_1;
4004+	ctx->repToConfirm[1] = offset_2;
4005+
4006+	/* Last Literals */
4007+	{
4008+		size_t const lastLLSize = iend - anchor;
4009+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
4010+		seqStorePtr->lit += lastLLSize;
4011+	}
4012+}
4013+
4014+void ZSTD_compressBlock_greedy_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize) { ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 0); }
4015+
4016+static void ZSTD_compressBlock_lazy_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
4017+{
4018+	ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 1);
4019+}
4020+
4021+static void ZSTD_compressBlock_lazy2_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
4022+{
4023+	ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 2);
4024+}
4025+
4026+static void ZSTD_compressBlock_btlazy2_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
4027+{
4028+	ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 1, 2);
4029+}
4030+
4031+/* The optimal parser */
4032+#include "zstd_opt.h"
4033+
4034+static void ZSTD_compressBlock_btopt(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
4035+{
4036+#ifdef ZSTD_OPT_H_91842398743
4037+	ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 0);
4038+#else
4039+	(void)ctx;
4040+	(void)src;
4041+	(void)srcSize;
4042+	return;
4043+#endif
4044+}
4045+
4046+static void ZSTD_compressBlock_btopt2(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
4047+{
4048+#ifdef ZSTD_OPT_H_91842398743
4049+	ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 1);
4050+#else
4051+	(void)ctx;
4052+	(void)src;
4053+	(void)srcSize;
4054+	return;
4055+#endif
4056+}
4057+
4058+static void ZSTD_compressBlock_btopt_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
4059+{
4060+#ifdef ZSTD_OPT_H_91842398743
4061+	ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 0);
4062+#else
4063+	(void)ctx;
4064+	(void)src;
4065+	(void)srcSize;
4066+	return;
4067+#endif
4068+}
4069+
4070+static void ZSTD_compressBlock_btopt2_extDict(ZSTD_CCtx *ctx, const void *src, size_t srcSize)
4071+{
4072+#ifdef ZSTD_OPT_H_91842398743
4073+	ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 1);
4074+#else
4075+	(void)ctx;
4076+	(void)src;
4077+	(void)srcSize;
4078+	return;
4079+#endif
4080+}
4081+
4082+typedef void (*ZSTD_blockCompressor)(ZSTD_CCtx *ctx, const void *src, size_t srcSize);
4083+
4084+static ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict)
4085+{
4086+	static const ZSTD_blockCompressor blockCompressor[2][8] = {
4087+	    {ZSTD_compressBlock_fast, ZSTD_compressBlock_doubleFast, ZSTD_compressBlock_greedy, ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2,
4088+	     ZSTD_compressBlock_btlazy2, ZSTD_compressBlock_btopt, ZSTD_compressBlock_btopt2},
4089+	    {ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_doubleFast_extDict, ZSTD_compressBlock_greedy_extDict, ZSTD_compressBlock_lazy_extDict,
4090+	     ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, ZSTD_compressBlock_btopt_extDict, ZSTD_compressBlock_btopt2_extDict}};
4091+
4092+	return blockCompressor[extDict][(U32)strat];
4093+}
4094+
4095+static size_t ZSTD_compressBlock_internal(ZSTD_CCtx *zc, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
4096+{
4097+	ZSTD_blockCompressor const blockCompressor = ZSTD_selectBlockCompressor(zc->params.cParams.strategy, zc->lowLimit < zc->dictLimit);
4098+	const BYTE *const base = zc->base;
4099+	const BYTE *const istart = (const BYTE *)src;
4100+	const U32 curr = (U32)(istart - base);
4101+	if (srcSize < MIN_CBLOCK_SIZE + ZSTD_blockHeaderSize + 1)
4102+		return 0; /* don't even attempt compression below a certain srcSize */
4103+	ZSTD_resetSeqStore(&(zc->seqStore));
4104+	if (curr > zc->nextToUpdate + 384)
4105+		zc->nextToUpdate = curr - MIN(192, (U32)(curr - zc->nextToUpdate - 384)); /* update tree not updated after finding very long rep matches */
4106+	blockCompressor(zc, src, srcSize);
4107+	return ZSTD_compressSequences(zc, dst, dstCapacity, srcSize);
4108+}
4109+
4110+/*! ZSTD_compress_generic() :
4111+*   Compress a chunk of data into one or multiple blocks.
4112+*   All blocks will be terminated, all input will be consumed.
4113+*   Function will issue an error if there is not enough `dstCapacity` to hold the compressed content.
4114+*   Frame is supposed already started (header already produced)
4115+*   @return : compressed size, or an error code
4116+*/
4117+static size_t ZSTD_compress_generic(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, U32 lastFrameChunk)
4118+{
4119+	size_t blockSize = cctx->blockSize;
4120+	size_t remaining = srcSize;
4121+	const BYTE *ip = (const BYTE *)src;
4122+	BYTE *const ostart = (BYTE *)dst;
4123+	BYTE *op = ostart;
4124+	U32 const maxDist = 1 << cctx->params.cParams.windowLog;
4125+
4126+	if (cctx->params.fParams.checksumFlag && srcSize)
4127+		xxh64_update(&cctx->xxhState, src, srcSize);
4128+
4129+	while (remaining) {
4130+		U32 const lastBlock = lastFrameChunk & (blockSize >= remaining);
4131+		size_t cSize;
4132+
4133+		if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE)
4134+			return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */
4135+		if (remaining < blockSize)
4136+			blockSize = remaining;
4137+
4138+		/* preemptive overflow correction */
4139+		if (cctx->lowLimit > (3U << 29)) {
4140+			U32 const cycleMask = (1 << ZSTD_cycleLog(cctx->params.cParams.hashLog, cctx->params.cParams.strategy)) - 1;
4141+			U32 const curr = (U32)(ip - cctx->base);
4142+			U32 const newCurr = (curr & cycleMask) + (1 << cctx->params.cParams.windowLog);
4143+			U32 const correction = curr - newCurr;
4144+			ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_64 <= 30);
4145+			ZSTD_reduceIndex(cctx, correction);
4146+			cctx->base += correction;
4147+			cctx->dictBase += correction;
4148+			cctx->lowLimit -= correction;
4149+			cctx->dictLimit -= correction;
4150+			if (cctx->nextToUpdate < correction)
4151+				cctx->nextToUpdate = 0;
4152+			else
4153+				cctx->nextToUpdate -= correction;
4154+		}
4155+
4156+		if ((U32)(ip + blockSize - cctx->base) > cctx->loadedDictEnd + maxDist) {
4157+			/* enforce maxDist */
4158+			U32 const newLowLimit = (U32)(ip + blockSize - cctx->base) - maxDist;
4159+			if (cctx->lowLimit < newLowLimit)
4160+				cctx->lowLimit = newLowLimit;
4161+			if (cctx->dictLimit < cctx->lowLimit)
4162+				cctx->dictLimit = cctx->lowLimit;
4163+		}
4164+
4165+		cSize = ZSTD_compressBlock_internal(cctx, op + ZSTD_blockHeaderSize, dstCapacity - ZSTD_blockHeaderSize, ip, blockSize);
4166+		if (ZSTD_isError(cSize))
4167+			return cSize;
4168+
4169+		if (cSize == 0) { /* block is not compressible */
4170+			U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw) << 1) + (U32)(blockSize << 3);
4171+			if (blockSize + ZSTD_blockHeaderSize > dstCapacity)
4172+				return ERROR(dstSize_tooSmall);
4173+			ZSTD_writeLE32(op, cBlockHeader24); /* no pb, 4th byte will be overwritten */
4174+			memcpy(op + ZSTD_blockHeaderSize, ip, blockSize);
4175+			cSize = ZSTD_blockHeaderSize + blockSize;
4176+		} else {
4177+			U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed) << 1) + (U32)(cSize << 3);
4178+			ZSTD_writeLE24(op, cBlockHeader24);
4179+			cSize += ZSTD_blockHeaderSize;
4180+		}
4181+
4182+		remaining -= blockSize;
4183+		dstCapacity -= cSize;
4184+		ip += blockSize;
4185+		op += cSize;
4186+	}
4187+
4188+	if (lastFrameChunk && (op > ostart))
4189+		cctx->stage = ZSTDcs_ending;
4190+	return op - ostart;
4191+}
4192+
4193+static size_t ZSTD_writeFrameHeader(void *dst, size_t dstCapacity, ZSTD_parameters params, U64 pledgedSrcSize, U32 dictID)
4194+{
4195+	BYTE *const op = (BYTE *)dst;
4196+	U32 const dictIDSizeCode = (dictID > 0) + (dictID >= 256) + (dictID >= 65536); /* 0-3 */
4197+	U32 const checksumFlag = params.fParams.checksumFlag > 0;
4198+	U32 const windowSize = 1U << params.cParams.windowLog;
4199+	U32 const singleSegment = params.fParams.contentSizeFlag && (windowSize >= pledgedSrcSize);
4200+	BYTE const windowLogByte = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) << 3);
4201+	U32 const fcsCode =
4202+	    params.fParams.contentSizeFlag ? (pledgedSrcSize >= 256) + (pledgedSrcSize >= 65536 + 256) + (pledgedSrcSize >= 0xFFFFFFFFU) : 0; /* 0-3 */
4203+	BYTE const frameHeaderDescriptionByte = (BYTE)(dictIDSizeCode + (checksumFlag << 2) + (singleSegment << 5) + (fcsCode << 6));
4204+	size_t pos;
4205+
4206+	if (dstCapacity < ZSTD_frameHeaderSize_max)
4207+		return ERROR(dstSize_tooSmall);
4208+
4209+	ZSTD_writeLE32(dst, ZSTD_MAGICNUMBER);
4210+	op[4] = frameHeaderDescriptionByte;
4211+	pos = 5;
4212+	if (!singleSegment)
4213+		op[pos++] = windowLogByte;
4214+	switch (dictIDSizeCode) {
4215+	default: /* impossible */
4216+	case 0: break;
4217+	case 1:
4218+		op[pos] = (BYTE)(dictID);
4219+		pos++;
4220+		break;
4221+	case 2:
4222+		ZSTD_writeLE16(op + pos, (U16)dictID);
4223+		pos += 2;
4224+		break;
4225+	case 3:
4226+		ZSTD_writeLE32(op + pos, dictID);
4227+		pos += 4;
4228+		break;
4229+	}
4230+	switch (fcsCode) {
4231+	default: /* impossible */
4232+	case 0:
4233+		if (singleSegment)
4234+			op[pos++] = (BYTE)(pledgedSrcSize);
4235+		break;
4236+	case 1:
4237+		ZSTD_writeLE16(op + pos, (U16)(pledgedSrcSize - 256));
4238+		pos += 2;
4239+		break;
4240+	case 2:
4241+		ZSTD_writeLE32(op + pos, (U32)(pledgedSrcSize));
4242+		pos += 4;
4243+		break;
4244+	case 3:
4245+		ZSTD_writeLE64(op + pos, (U64)(pledgedSrcSize));
4246+		pos += 8;
4247+		break;
4248+	}
4249+	return pos;
4250+}
4251+
4252+static size_t ZSTD_compressContinue_internal(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, U32 frame, U32 lastFrameChunk)
4253+{
4254+	const BYTE *const ip = (const BYTE *)src;
4255+	size_t fhSize = 0;
4256+
4257+	if (cctx->stage == ZSTDcs_created)
4258+		return ERROR(stage_wrong); /* missing init (ZSTD_compressBegin) */
4259+
4260+	if (frame && (cctx->stage == ZSTDcs_init)) {
4261+		fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, cctx->frameContentSize, cctx->dictID);
4262+		if (ZSTD_isError(fhSize))
4263+			return fhSize;
4264+		dstCapacity -= fhSize;
4265+		dst = (char *)dst + fhSize;
4266+		cctx->stage = ZSTDcs_ongoing;
4267+	}
4268+
4269+	/* Check if blocks follow each other */
4270+	if (src != cctx->nextSrc) {
4271+		/* not contiguous */
4272+		ptrdiff_t const delta = cctx->nextSrc - ip;
4273+		cctx->lowLimit = cctx->dictLimit;
4274+		cctx->dictLimit = (U32)(cctx->nextSrc - cctx->base);
4275+		cctx->dictBase = cctx->base;
4276+		cctx->base -= delta;
4277+		cctx->nextToUpdate = cctx->dictLimit;
4278+		if (cctx->dictLimit - cctx->lowLimit < HASH_READ_SIZE)
4279+			cctx->lowLimit = cctx->dictLimit; /* too small extDict */
4280+	}
4281+
4282+	/* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */
4283+	if ((ip + srcSize > cctx->dictBase + cctx->lowLimit) & (ip < cctx->dictBase + cctx->dictLimit)) {
4284+		ptrdiff_t const highInputIdx = (ip + srcSize) - cctx->dictBase;
4285+		U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)cctx->dictLimit) ? cctx->dictLimit : (U32)highInputIdx;
4286+		cctx->lowLimit = lowLimitMax;
4287+	}
4288+
4289+	cctx->nextSrc = ip + srcSize;
4290+
4291+	if (srcSize) {
4292+		size_t const cSize = frame ? ZSTD_compress_generic(cctx, dst, dstCapacity, src, srcSize, lastFrameChunk)
4293+					   : ZSTD_compressBlock_internal(cctx, dst, dstCapacity, src, srcSize);
4294+		if (ZSTD_isError(cSize))
4295+			return cSize;
4296+		return cSize + fhSize;
4297+	} else
4298+		return fhSize;
4299+}
4300+
4301+size_t ZSTD_compressContinue(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
4302+{
4303+	return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1, 0);
4304+}
4305+
4306+size_t ZSTD_getBlockSizeMax(ZSTD_CCtx *cctx) { return MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, 1 << cctx->params.cParams.windowLog); }
4307+
4308+size_t ZSTD_compressBlock(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
4309+{
4310+	size_t const blockSizeMax = ZSTD_getBlockSizeMax(cctx);
4311+	if (srcSize > blockSizeMax)
4312+		return ERROR(srcSize_wrong);
4313+	return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 0, 0);
4314+}
4315+
4316+/*! ZSTD_loadDictionaryContent() :
4317+ *  @return : 0, or an error code
4318+ */
4319+static size_t ZSTD_loadDictionaryContent(ZSTD_CCtx *zc, const void *src, size_t srcSize)
4320+{
4321+	const BYTE *const ip = (const BYTE *)src;
4322+	const BYTE *const iend = ip + srcSize;
4323+
4324+	/* input becomes curr prefix */
4325+	zc->lowLimit = zc->dictLimit;
4326+	zc->dictLimit = (U32)(zc->nextSrc - zc->base);
4327+	zc->dictBase = zc->base;
4328+	zc->base += ip - zc->nextSrc;
4329+	zc->nextToUpdate = zc->dictLimit;
4330+	zc->loadedDictEnd = zc->forceWindow ? 0 : (U32)(iend - zc->base);
4331+
4332+	zc->nextSrc = iend;
4333+	if (srcSize <= HASH_READ_SIZE)
4334+		return 0;
4335+
4336+	switch (zc->params.cParams.strategy) {
4337+	case ZSTD_fast: ZSTD_fillHashTable(zc, iend, zc->params.cParams.searchLength); break;
4338+
4339+	case ZSTD_dfast: ZSTD_fillDoubleHashTable(zc, iend, zc->params.cParams.searchLength); break;
4340+
4341+	case ZSTD_greedy:
4342+	case ZSTD_lazy:
4343+	case ZSTD_lazy2:
4344+		if (srcSize >= HASH_READ_SIZE)
4345+			ZSTD_insertAndFindFirstIndex(zc, iend - HASH_READ_SIZE, zc->params.cParams.searchLength);
4346+		break;
4347+
4348+	case ZSTD_btlazy2:
4349+	case ZSTD_btopt:
4350+	case ZSTD_btopt2:
4351+		if (srcSize >= HASH_READ_SIZE)
4352+			ZSTD_updateTree(zc, iend - HASH_READ_SIZE, iend, 1 << zc->params.cParams.searchLog, zc->params.cParams.searchLength);
4353+		break;
4354+
4355+	default:
4356+		return ERROR(GENERIC); /* strategy doesn't exist; impossible */
4357+	}
4358+
4359+	zc->nextToUpdate = (U32)(iend - zc->base);
4360+	return 0;
4361+}
4362+
4363+/* Dictionaries that assign zero probability to symbols that show up causes problems
4364+   when FSE encoding.  Refuse dictionaries that assign zero probability to symbols
4365+   that we may encounter during compression.
4366+   NOTE: This behavior is not standard and could be improved in the future. */
4367+static size_t ZSTD_checkDictNCount(short *normalizedCounter, unsigned dictMaxSymbolValue, unsigned maxSymbolValue)
4368+{
4369+	U32 s;
4370+	if (dictMaxSymbolValue < maxSymbolValue)
4371+		return ERROR(dictionary_corrupted);
4372+	for (s = 0; s <= maxSymbolValue; ++s) {
4373+		if (normalizedCounter[s] == 0)
4374+			return ERROR(dictionary_corrupted);
4375+	}
4376+	return 0;
4377+}
4378+
4379+/* Dictionary format :
4380+ * See :
4381+ * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#dictionary-format
4382+ */
4383+/*! ZSTD_loadZstdDictionary() :
4384+ * @return : 0, or an error code
4385+ *  assumptions : magic number supposed already checked
4386+ *                dictSize supposed > 8
4387+ */
4388+static size_t ZSTD_loadZstdDictionary(ZSTD_CCtx *cctx, const void *dict, size_t dictSize)
4389+{
4390+	const BYTE *dictPtr = (const BYTE *)dict;
4391+	const BYTE *const dictEnd = dictPtr + dictSize;
4392+	short offcodeNCount[MaxOff + 1];
4393+	unsigned offcodeMaxValue = MaxOff;
4394+
4395+	dictPtr += 4; /* skip magic number */
4396+	cctx->dictID = cctx->params.fParams.noDictIDFlag ? 0 : ZSTD_readLE32(dictPtr);
4397+	dictPtr += 4;
4398+
4399+	{
4400+		size_t const hufHeaderSize = HUF_readCTable_wksp(cctx->hufTable, 255, dictPtr, dictEnd - dictPtr, cctx->tmpCounters, sizeof(cctx->tmpCounters));
4401+		if (HUF_isError(hufHeaderSize))
4402+			return ERROR(dictionary_corrupted);
4403+		dictPtr += hufHeaderSize;
4404+	}
4405+
4406+	{
4407+		unsigned offcodeLog;
4408+		size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd - dictPtr);
4409+		if (FSE_isError(offcodeHeaderSize))
4410+			return ERROR(dictionary_corrupted);
4411+		if (offcodeLog > OffFSELog)
4412+			return ERROR(dictionary_corrupted);
4413+		/* Defer checking offcodeMaxValue because we need to know the size of the dictionary content */
4414+		CHECK_E(FSE_buildCTable_wksp(cctx->offcodeCTable, offcodeNCount, offcodeMaxValue, offcodeLog, cctx->tmpCounters, sizeof(cctx->tmpCounters)),
4415+			dictionary_corrupted);
4416+		dictPtr += offcodeHeaderSize;
4417+	}
4418+
4419+	{
4420+		short matchlengthNCount[MaxML + 1];
4421+		unsigned matchlengthMaxValue = MaxML, matchlengthLog;
4422+		size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd - dictPtr);
4423+		if (FSE_isError(matchlengthHeaderSize))
4424+			return ERROR(dictionary_corrupted);
4425+		if (matchlengthLog > MLFSELog)
4426+			return ERROR(dictionary_corrupted);
4427+		/* Every match length code must have non-zero probability */
4428+		CHECK_F(ZSTD_checkDictNCount(matchlengthNCount, matchlengthMaxValue, MaxML));
4429+		CHECK_E(
4430+		    FSE_buildCTable_wksp(cctx->matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, cctx->tmpCounters, sizeof(cctx->tmpCounters)),
4431+		    dictionary_corrupted);
4432+		dictPtr += matchlengthHeaderSize;
4433+	}
4434+
4435+	{
4436+		short litlengthNCount[MaxLL + 1];
4437+		unsigned litlengthMaxValue = MaxLL, litlengthLog;
4438+		size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd - dictPtr);
4439+		if (FSE_isError(litlengthHeaderSize))
4440+			return ERROR(dictionary_corrupted);
4441+		if (litlengthLog > LLFSELog)
4442+			return ERROR(dictionary_corrupted);
4443+		/* Every literal length code must have non-zero probability */
4444+		CHECK_F(ZSTD_checkDictNCount(litlengthNCount, litlengthMaxValue, MaxLL));
4445+		CHECK_E(FSE_buildCTable_wksp(cctx->litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog, cctx->tmpCounters, sizeof(cctx->tmpCounters)),
4446+			dictionary_corrupted);
4447+		dictPtr += litlengthHeaderSize;
4448+	}
4449+
4450+	if (dictPtr + 12 > dictEnd)
4451+		return ERROR(dictionary_corrupted);
4452+	cctx->rep[0] = ZSTD_readLE32(dictPtr + 0);
4453+	cctx->rep[1] = ZSTD_readLE32(dictPtr + 4);
4454+	cctx->rep[2] = ZSTD_readLE32(dictPtr + 8);
4455+	dictPtr += 12;
4456+
4457+	{
4458+		size_t const dictContentSize = (size_t)(dictEnd - dictPtr);
4459+		U32 offcodeMax = MaxOff;
4460+		if (dictContentSize <= ((U32)-1) - 128 KB) {
4461+			U32 const maxOffset = (U32)dictContentSize + 128 KB; /* The maximum offset that must be supported */
4462+			offcodeMax = ZSTD_highbit32(maxOffset);		     /* Calculate minimum offset code required to represent maxOffset */
4463+		}
4464+		/* All offset values <= dictContentSize + 128 KB must be representable */
4465+		CHECK_F(ZSTD_checkDictNCount(offcodeNCount, offcodeMaxValue, MIN(offcodeMax, MaxOff)));
4466+		/* All repCodes must be <= dictContentSize and != 0*/
4467+		{
4468+			U32 u;
4469+			for (u = 0; u < 3; u++) {
4470+				if (cctx->rep[u] == 0)
4471+					return ERROR(dictionary_corrupted);
4472+				if (cctx->rep[u] > dictContentSize)
4473+					return ERROR(dictionary_corrupted);
4474+			}
4475+		}
4476+
4477+		cctx->flagStaticTables = 1;
4478+		cctx->flagStaticHufTable = HUF_repeat_valid;
4479+		return ZSTD_loadDictionaryContent(cctx, dictPtr, dictContentSize);
4480+	}
4481+}
4482+
4483+/** ZSTD_compress_insertDictionary() :
4484+*   @return : 0, or an error code */
4485+static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx *cctx, const void *dict, size_t dictSize)
4486+{
4487+	if ((dict == NULL) || (dictSize <= 8))
4488+		return 0;
4489+
4490+	/* dict as pure content */
4491+	if ((ZSTD_readLE32(dict) != ZSTD_DICT_MAGIC) || (cctx->forceRawDict))
4492+		return ZSTD_loadDictionaryContent(cctx, dict, dictSize);
4493+
4494+	/* dict as zstd dictionary */
4495+	return ZSTD_loadZstdDictionary(cctx, dict, dictSize);
4496+}
4497+
4498+/*! ZSTD_compressBegin_internal() :
4499+*   @return : 0, or an error code */
4500+static size_t ZSTD_compressBegin_internal(ZSTD_CCtx *cctx, const void *dict, size_t dictSize, ZSTD_parameters params, U64 pledgedSrcSize)
4501+{
4502+	ZSTD_compResetPolicy_e const crp = dictSize ? ZSTDcrp_fullReset : ZSTDcrp_continue;
4503+	CHECK_F(ZSTD_resetCCtx_advanced(cctx, params, pledgedSrcSize, crp));
4504+	return ZSTD_compress_insertDictionary(cctx, dict, dictSize);
4505+}
4506+
4507+/*! ZSTD_compressBegin_advanced() :
4508+*   @return : 0, or an error code */
4509+size_t ZSTD_compressBegin_advanced(ZSTD_CCtx *cctx, const void *dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize)
4510+{
4511+	/* compression parameters verification and optimization */
4512+	CHECK_F(ZSTD_checkCParams(params.cParams));
4513+	return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, pledgedSrcSize);
4514+}
4515+
4516+size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx *cctx, const void *dict, size_t dictSize, int compressionLevel)
4517+{
4518+	ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize);
4519+	return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, 0);
4520+}
4521+
4522+size_t ZSTD_compressBegin(ZSTD_CCtx *cctx, int compressionLevel) { return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel); }
4523+
4524+/*! ZSTD_writeEpilogue() :
4525+*   Ends a frame.
4526+*   @return : nb of bytes written into dst (or an error code) */
4527+static size_t ZSTD_writeEpilogue(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity)
4528+{
4529+	BYTE *const ostart = (BYTE *)dst;
4530+	BYTE *op = ostart;
4531+	size_t fhSize = 0;
4532+
4533+	if (cctx->stage == ZSTDcs_created)
4534+		return ERROR(stage_wrong); /* init missing */
4535+
4536+	/* special case : empty frame */
4537+	if (cctx->stage == ZSTDcs_init) {
4538+		fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, 0, 0);
4539+		if (ZSTD_isError(fhSize))
4540+			return fhSize;
4541+		dstCapacity -= fhSize;
4542+		op += fhSize;
4543+		cctx->stage = ZSTDcs_ongoing;
4544+	}
4545+
4546+	if (cctx->stage != ZSTDcs_ending) {
4547+		/* write one last empty block, make it the "last" block */
4548+		U32 const cBlockHeader24 = 1 /* last block */ + (((U32)bt_raw) << 1) + 0;
4549+		if (dstCapacity < 4)
4550+			return ERROR(dstSize_tooSmall);
4551+		ZSTD_writeLE32(op, cBlockHeader24);
4552+		op += ZSTD_blockHeaderSize;
4553+		dstCapacity -= ZSTD_blockHeaderSize;
4554+	}
4555+
4556+	if (cctx->params.fParams.checksumFlag) {
4557+		U32 const checksum = (U32)xxh64_digest(&cctx->xxhState);
4558+		if (dstCapacity < 4)
4559+			return ERROR(dstSize_tooSmall);
4560+		ZSTD_writeLE32(op, checksum);
4561+		op += 4;
4562+	}
4563+
4564+	cctx->stage = ZSTDcs_created; /* return to "created but no init" status */
4565+	return op - ostart;
4566+}
4567+
4568+size_t ZSTD_compressEnd(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
4569+{
4570+	size_t endResult;
4571+	size_t const cSize = ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1, 1);
4572+	if (ZSTD_isError(cSize))
4573+		return cSize;
4574+	endResult = ZSTD_writeEpilogue(cctx, (char *)dst + cSize, dstCapacity - cSize);
4575+	if (ZSTD_isError(endResult))
4576+		return endResult;
4577+	return cSize + endResult;
4578+}
4579+
4580+static size_t ZSTD_compress_internal(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const void *dict, size_t dictSize,
4581+				     ZSTD_parameters params)
4582+{
4583+	CHECK_F(ZSTD_compressBegin_internal(cctx, dict, dictSize, params, srcSize));
4584+	return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
4585+}
4586+
4587+size_t ZSTD_compress_usingDict(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const void *dict, size_t dictSize,
4588+			       ZSTD_parameters params)
4589+{
4590+	return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params);
4591+}
4592+
4593+size_t ZSTD_compressCCtx(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, ZSTD_parameters params)
4594+{
4595+	return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, NULL, 0, params);
4596+}
4597+
4598+/* =====  Dictionary API  ===== */
4599+
4600+struct ZSTD_CDict_s {
4601+	void *dictBuffer;
4602+	const void *dictContent;
4603+	size_t dictContentSize;
4604+	ZSTD_CCtx *refContext;
4605+}; /* typedef'd tp ZSTD_CDict within "zstd.h" */
4606+
4607+size_t ZSTD_CDictWorkspaceBound(ZSTD_compressionParameters cParams) { return ZSTD_CCtxWorkspaceBound(cParams) + ZSTD_ALIGN(sizeof(ZSTD_CDict)); }
4608+
4609+static ZSTD_CDict *ZSTD_createCDict_advanced(const void *dictBuffer, size_t dictSize, unsigned byReference, ZSTD_parameters params, ZSTD_customMem customMem)
4610+{
4611+	if (!customMem.customAlloc || !customMem.customFree)
4612+		return NULL;
4613+
4614+	{
4615+		ZSTD_CDict *const cdict = (ZSTD_CDict *)ZSTD_malloc(sizeof(ZSTD_CDict), customMem);
4616+		ZSTD_CCtx *const cctx = ZSTD_createCCtx_advanced(customMem);
4617+
4618+		if (!cdict || !cctx) {
4619+			ZSTD_free(cdict, customMem);
4620+			ZSTD_freeCCtx(cctx);
4621+			return NULL;
4622+		}
4623+
4624+		if ((byReference) || (!dictBuffer) || (!dictSize)) {
4625+			cdict->dictBuffer = NULL;
4626+			cdict->dictContent = dictBuffer;
4627+		} else {
4628+			void *const internalBuffer = ZSTD_malloc(dictSize, customMem);
4629+			if (!internalBuffer) {
4630+				ZSTD_free(cctx, customMem);
4631+				ZSTD_free(cdict, customMem);
4632+				return NULL;
4633+			}
4634+			memcpy(internalBuffer, dictBuffer, dictSize);
4635+			cdict->dictBuffer = internalBuffer;
4636+			cdict->dictContent = internalBuffer;
4637+		}
4638+
4639+		{
4640+			size_t const errorCode = ZSTD_compressBegin_advanced(cctx, cdict->dictContent, dictSize, params, 0);
4641+			if (ZSTD_isError(errorCode)) {
4642+				ZSTD_free(cdict->dictBuffer, customMem);
4643+				ZSTD_free(cdict, customMem);
4644+				ZSTD_freeCCtx(cctx);
4645+				return NULL;
4646+			}
4647+		}
4648+
4649+		cdict->refContext = cctx;
4650+		cdict->dictContentSize = dictSize;
4651+		return cdict;
4652+	}
4653+}
4654+
4655+ZSTD_CDict *ZSTD_initCDict(const void *dict, size_t dictSize, ZSTD_parameters params, void *workspace, size_t workspaceSize)
4656+{
4657+	ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
4658+	return ZSTD_createCDict_advanced(dict, dictSize, 1, params, stackMem);
4659+}
4660+
4661+size_t ZSTD_freeCDict(ZSTD_CDict *cdict)
4662+{
4663+	if (cdict == NULL)
4664+		return 0; /* support free on NULL */
4665+	{
4666+		ZSTD_customMem const cMem = cdict->refContext->customMem;
4667+		ZSTD_freeCCtx(cdict->refContext);
4668+		ZSTD_free(cdict->dictBuffer, cMem);
4669+		ZSTD_free(cdict, cMem);
4670+		return 0;
4671+	}
4672+}
4673+
4674+static ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict *cdict) { return ZSTD_getParamsFromCCtx(cdict->refContext); }
4675+
4676+size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx *cctx, const ZSTD_CDict *cdict, unsigned long long pledgedSrcSize)
4677+{
4678+	if (cdict->dictContentSize)
4679+		CHECK_F(ZSTD_copyCCtx(cctx, cdict->refContext, pledgedSrcSize))
4680+	else {
4681+		ZSTD_parameters params = cdict->refContext->params;
4682+		params.fParams.contentSizeFlag = (pledgedSrcSize > 0);
4683+		CHECK_F(ZSTD_compressBegin_advanced(cctx, NULL, 0, params, pledgedSrcSize));
4684+	}
4685+	return 0;
4686+}
4687+
4688+/*! ZSTD_compress_usingCDict() :
4689+*   Compression using a digested Dictionary.
4690+*   Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
4691+*   Note that compression level is decided during dictionary creation */
4692+size_t ZSTD_compress_usingCDict(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const ZSTD_CDict *cdict)
4693+{
4694+	CHECK_F(ZSTD_compressBegin_usingCDict(cctx, cdict, srcSize));
4695+
4696+	if (cdict->refContext->params.fParams.contentSizeFlag == 1) {
4697+		cctx->params.fParams.contentSizeFlag = 1;
4698+		cctx->frameContentSize = srcSize;
4699+	} else {
4700+		cctx->params.fParams.contentSizeFlag = 0;
4701+	}
4702+
4703+	return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
4704+}
4705+
4706+/* ******************************************************************
4707+*  Streaming
4708+********************************************************************/
4709+
4710+typedef enum { zcss_init, zcss_load, zcss_flush, zcss_final } ZSTD_cStreamStage;
4711+
4712+struct ZSTD_CStream_s {
4713+	ZSTD_CCtx *cctx;
4714+	ZSTD_CDict *cdictLocal;
4715+	const ZSTD_CDict *cdict;
4716+	char *inBuff;
4717+	size_t inBuffSize;
4718+	size_t inToCompress;
4719+	size_t inBuffPos;
4720+	size_t inBuffTarget;
4721+	size_t blockSize;
4722+	char *outBuff;
4723+	size_t outBuffSize;
4724+	size_t outBuffContentSize;
4725+	size_t outBuffFlushedSize;
4726+	ZSTD_cStreamStage stage;
4727+	U32 checksum;
4728+	U32 frameEnded;
4729+	U64 pledgedSrcSize;
4730+	U64 inputProcessed;
4731+	ZSTD_parameters params;
4732+	ZSTD_customMem customMem;
4733+}; /* typedef'd to ZSTD_CStream within "zstd.h" */
4734+
4735+size_t ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters cParams)
4736+{
4737+	size_t const inBuffSize = (size_t)1 << cParams.windowLog;
4738+	size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, inBuffSize);
4739+	size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1;
4740+
4741+	return ZSTD_CCtxWorkspaceBound(cParams) + ZSTD_ALIGN(sizeof(ZSTD_CStream)) + ZSTD_ALIGN(inBuffSize) + ZSTD_ALIGN(outBuffSize);
4742+}
4743+
4744+ZSTD_CStream *ZSTD_createCStream_advanced(ZSTD_customMem customMem)
4745+{
4746+	ZSTD_CStream *zcs;
4747+
4748+	if (!customMem.customAlloc || !customMem.customFree)
4749+		return NULL;
4750+
4751+	zcs = (ZSTD_CStream *)ZSTD_malloc(sizeof(ZSTD_CStream), customMem);
4752+	if (zcs == NULL)
4753+		return NULL;
4754+	memset(zcs, 0, sizeof(ZSTD_CStream));
4755+	memcpy(&zcs->customMem, &customMem, sizeof(ZSTD_customMem));
4756+	zcs->cctx = ZSTD_createCCtx_advanced(customMem);
4757+	if (zcs->cctx == NULL) {
4758+		ZSTD_freeCStream(zcs);
4759+		return NULL;
4760+	}
4761+	return zcs;
4762+}
4763+
4764+size_t ZSTD_freeCStream(ZSTD_CStream *zcs)
4765+{
4766+	if (zcs == NULL)
4767+		return 0; /* support free on NULL */
4768+	{
4769+		ZSTD_customMem const cMem = zcs->customMem;
4770+		ZSTD_freeCCtx(zcs->cctx);
4771+		zcs->cctx = NULL;
4772+		ZSTD_freeCDict(zcs->cdictLocal);
4773+		zcs->cdictLocal = NULL;
4774+		ZSTD_free(zcs->inBuff, cMem);
4775+		zcs->inBuff = NULL;
4776+		ZSTD_free(zcs->outBuff, cMem);
4777+		zcs->outBuff = NULL;
4778+		ZSTD_free(zcs, cMem);
4779+		return 0;
4780+	}
4781+}
4782+
4783+/*======   Initialization   ======*/
4784+
4785+size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; }
4786+size_t ZSTD_CStreamOutSize(void) { return ZSTD_compressBound(ZSTD_BLOCKSIZE_ABSOLUTEMAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */; }
4787+
4788+static size_t ZSTD_resetCStream_internal(ZSTD_CStream *zcs, unsigned long long pledgedSrcSize)
4789+{
4790+	if (zcs->inBuffSize == 0)
4791+		return ERROR(stage_wrong); /* zcs has not been init at least once => can't reset */
4792+
4793+	if (zcs->cdict)
4794+		CHECK_F(ZSTD_compressBegin_usingCDict(zcs->cctx, zcs->cdict, pledgedSrcSize))
4795+	else
4796+		CHECK_F(ZSTD_compressBegin_advanced(zcs->cctx, NULL, 0, zcs->params, pledgedSrcSize));
4797+
4798+	zcs->inToCompress = 0;
4799+	zcs->inBuffPos = 0;
4800+	zcs->inBuffTarget = zcs->blockSize;
4801+	zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0;
4802+	zcs->stage = zcss_load;
4803+	zcs->frameEnded = 0;
4804+	zcs->pledgedSrcSize = pledgedSrcSize;
4805+	zcs->inputProcessed = 0;
4806+	return 0; /* ready to go */
4807+}
4808+
4809+size_t ZSTD_resetCStream(ZSTD_CStream *zcs, unsigned long long pledgedSrcSize)
4810+{
4811+
4812+	zcs->params.fParams.contentSizeFlag = (pledgedSrcSize > 0);
4813+
4814+	return ZSTD_resetCStream_internal(zcs, pledgedSrcSize);
4815+}
4816+
4817+static size_t ZSTD_initCStream_advanced(ZSTD_CStream *zcs, const void *dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize)
4818+{
4819+	/* allocate buffers */
4820+	{
4821+		size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
4822+		if (zcs->inBuffSize < neededInBuffSize) {
4823+			zcs->inBuffSize = neededInBuffSize;
4824+			ZSTD_free(zcs->inBuff, zcs->customMem);
4825+			zcs->inBuff = (char *)ZSTD_malloc(neededInBuffSize, zcs->customMem);
4826+			if (zcs->inBuff == NULL)
4827+				return ERROR(memory_allocation);
4828+		}
4829+		zcs->blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, neededInBuffSize);
4830+	}
4831+	if (zcs->outBuffSize < ZSTD_compressBound(zcs->blockSize) + 1) {
4832+		zcs->outBuffSize = ZSTD_compressBound(zcs->blockSize) + 1;
4833+		ZSTD_free(zcs->outBuff, zcs->customMem);
4834+		zcs->outBuff = (char *)ZSTD_malloc(zcs->outBuffSize, zcs->customMem);
4835+		if (zcs->outBuff == NULL)
4836+			return ERROR(memory_allocation);
4837+	}
4838+
4839+	if (dict && dictSize >= 8) {
4840+		ZSTD_freeCDict(zcs->cdictLocal);
4841+		zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0, params, zcs->customMem);
4842+		if (zcs->cdictLocal == NULL)
4843+			return ERROR(memory_allocation);
4844+		zcs->cdict = zcs->cdictLocal;
4845+	} else
4846+		zcs->cdict = NULL;
4847+
4848+	zcs->checksum = params.fParams.checksumFlag > 0;
4849+	zcs->params = params;
4850+
4851+	return ZSTD_resetCStream_internal(zcs, pledgedSrcSize);
4852+}
4853+
4854+ZSTD_CStream *ZSTD_initCStream(ZSTD_parameters params, unsigned long long pledgedSrcSize, void *workspace, size_t workspaceSize)
4855+{
4856+	ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
4857+	ZSTD_CStream *const zcs = ZSTD_createCStream_advanced(stackMem);
4858+	if (zcs) {
4859+		size_t const code = ZSTD_initCStream_advanced(zcs, NULL, 0, params, pledgedSrcSize);
4860+		if (ZSTD_isError(code)) {
4861+			return NULL;
4862+		}
4863+	}
4864+	return zcs;
4865+}
4866+
4867+ZSTD_CStream *ZSTD_initCStream_usingCDict(const ZSTD_CDict *cdict, unsigned long long pledgedSrcSize, void *workspace, size_t workspaceSize)
4868+{
4869+	ZSTD_parameters const params = ZSTD_getParamsFromCDict(cdict);
4870+	ZSTD_CStream *const zcs = ZSTD_initCStream(params, pledgedSrcSize, workspace, workspaceSize);
4871+	if (zcs) {
4872+		zcs->cdict = cdict;
4873+		if (ZSTD_isError(ZSTD_resetCStream_internal(zcs, pledgedSrcSize))) {
4874+			return NULL;
4875+		}
4876+	}
4877+	return zcs;
4878+}
4879+
4880+/*======   Compression   ======*/
4881+
4882+typedef enum { zsf_gather, zsf_flush, zsf_end } ZSTD_flush_e;
4883+
4884+ZSTD_STATIC size_t ZSTD_limitCopy(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
4885+{
4886+	size_t const length = MIN(dstCapacity, srcSize);
4887+	memcpy(dst, src, length);
4888+	return length;
4889+}
4890+
4891+static size_t ZSTD_compressStream_generic(ZSTD_CStream *zcs, void *dst, size_t *dstCapacityPtr, const void *src, size_t *srcSizePtr, ZSTD_flush_e const flush)
4892+{
4893+	U32 someMoreWork = 1;
4894+	const char *const istart = (const char *)src;
4895+	const char *const iend = istart + *srcSizePtr;
4896+	const char *ip = istart;
4897+	char *const ostart = (char *)dst;
4898+	char *const oend = ostart + *dstCapacityPtr;
4899+	char *op = ostart;
4900+
4901+	while (someMoreWork) {
4902+		switch (zcs->stage) {
4903+		case zcss_init:
4904+			return ERROR(init_missing); /* call ZBUFF_compressInit() first ! */
4905+
4906+		case zcss_load:
4907+			/* complete inBuffer */
4908+			{
4909+				size_t const toLoad = zcs->inBuffTarget - zcs->inBuffPos;
4910+				size_t const loaded = ZSTD_limitCopy(zcs->inBuff + zcs->inBuffPos, toLoad, ip, iend - ip);
4911+				zcs->inBuffPos += loaded;
4912+				ip += loaded;
4913+				if ((zcs->inBuffPos == zcs->inToCompress) || (!flush && (toLoad != loaded))) {
4914+					someMoreWork = 0;
4915+					break; /* not enough input to get a full block : stop there, wait for more */
4916+				}
4917+			}
4918+			/* compress curr block (note : this stage cannot be stopped in the middle) */
4919+			{
4920+				void *cDst;
4921+				size_t cSize;
4922+				size_t const iSize = zcs->inBuffPos - zcs->inToCompress;
4923+				size_t oSize = oend - op;
4924+				if (oSize >= ZSTD_compressBound(iSize))
4925+					cDst = op; /* compress directly into output buffer (avoid flush stage) */
4926+				else
4927+					cDst = zcs->outBuff, oSize = zcs->outBuffSize;
4928+				cSize = (flush == zsf_end) ? ZSTD_compressEnd(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize)
4929+							   : ZSTD_compressContinue(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize);
4930+				if (ZSTD_isError(cSize))
4931+					return cSize;
4932+				if (flush == zsf_end)
4933+					zcs->frameEnded = 1;
4934+				/* prepare next block */
4935+				zcs->inBuffTarget = zcs->inBuffPos + zcs->blockSize;
4936+				if (zcs->inBuffTarget > zcs->inBuffSize)
4937+					zcs->inBuffPos = 0, zcs->inBuffTarget = zcs->blockSize; /* note : inBuffSize >= blockSize */
4938+				zcs->inToCompress = zcs->inBuffPos;
4939+				if (cDst == op) {
4940+					op += cSize;
4941+					break;
4942+				} /* no need to flush */
4943+				zcs->outBuffContentSize = cSize;
4944+				zcs->outBuffFlushedSize = 0;
4945+				zcs->stage = zcss_flush; /* pass-through to flush stage */
4946+			}
4947+
4948+		case zcss_flush: {
4949+			size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize;
4950+			size_t const flushed = ZSTD_limitCopy(op, oend - op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush);
4951+			op += flushed;
4952+			zcs->outBuffFlushedSize += flushed;
4953+			if (toFlush != flushed) {
4954+				someMoreWork = 0;
4955+				break;
4956+			} /* dst too small to store flushed data : stop there */
4957+			zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0;
4958+			zcs->stage = zcss_load;
4959+			break;
4960+		}
4961+
4962+		case zcss_final:
4963+			someMoreWork = 0; /* do nothing */
4964+			break;
4965+
4966+		default:
4967+			return ERROR(GENERIC); /* impossible */
4968+		}
4969+	}
4970+
4971+	*srcSizePtr = ip - istart;
4972+	*dstCapacityPtr = op - ostart;
4973+	zcs->inputProcessed += *srcSizePtr;
4974+	if (zcs->frameEnded)
4975+		return 0;
4976+	{
4977+		size_t hintInSize = zcs->inBuffTarget - zcs->inBuffPos;
4978+		if (hintInSize == 0)
4979+			hintInSize = zcs->blockSize;
4980+		return hintInSize;
4981+	}
4982+}
4983+
4984+size_t ZSTD_compressStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output, ZSTD_inBuffer *input)
4985+{
4986+	size_t sizeRead = input->size - input->pos;
4987+	size_t sizeWritten = output->size - output->pos;
4988+	size_t const result =
4989+	    ZSTD_compressStream_generic(zcs, (char *)(output->dst) + output->pos, &sizeWritten, (const char *)(input->src) + input->pos, &sizeRead, zsf_gather);
4990+	input->pos += sizeRead;
4991+	output->pos += sizeWritten;
4992+	return result;
4993+}
4994+
4995+/*======   Finalize   ======*/
4996+
4997+/*! ZSTD_flushStream() :
4998+*   @return : amount of data remaining to flush */
4999+size_t ZSTD_flushStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output)
5000+{
5001+	size_t srcSize = 0;
5002+	size_t sizeWritten = output->size - output->pos;
5003+	size_t const result = ZSTD_compressStream_generic(zcs, (char *)(output->dst) + output->pos, &sizeWritten, &srcSize,
5004+							  &srcSize, /* use a valid src address instead of NULL */
5005+							  zsf_flush);
5006+	output->pos += sizeWritten;
5007+	if (ZSTD_isError(result))
5008+		return result;
5009+	return zcs->outBuffContentSize - zcs->outBuffFlushedSize; /* remaining to flush */
5010+}
5011+
5012+size_t ZSTD_endStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output)
5013+{
5014+	BYTE *const ostart = (BYTE *)(output->dst) + output->pos;
5015+	BYTE *const oend = (BYTE *)(output->dst) + output->size;
5016+	BYTE *op = ostart;
5017+
5018+	if ((zcs->pledgedSrcSize) && (zcs->inputProcessed != zcs->pledgedSrcSize))
5019+		return ERROR(srcSize_wrong); /* pledgedSrcSize not respected */
5020+
5021+	if (zcs->stage != zcss_final) {
5022+		/* flush whatever remains */
5023+		size_t srcSize = 0;
5024+		size_t sizeWritten = output->size - output->pos;
5025+		size_t const notEnded =
5026+		    ZSTD_compressStream_generic(zcs, ostart, &sizeWritten, &srcSize, &srcSize, zsf_end); /* use a valid src address instead of NULL */
5027+		size_t const remainingToFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize;
5028+		op += sizeWritten;
5029+		if (remainingToFlush) {
5030+			output->pos += sizeWritten;
5031+			return remainingToFlush + ZSTD_BLOCKHEADERSIZE /* final empty block */ + (zcs->checksum * 4);
5032+		}
5033+		/* create epilogue */
5034+		zcs->stage = zcss_final;
5035+		zcs->outBuffContentSize = !notEnded ? 0 : ZSTD_compressEnd(zcs->cctx, zcs->outBuff, zcs->outBuffSize, NULL,
5036+									   0); /* write epilogue, including final empty block, into outBuff */
5037+	}
5038+
5039+	/* flush epilogue */
5040+	{
5041+		size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize;
5042+		size_t const flushed = ZSTD_limitCopy(op, oend - op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush);
5043+		op += flushed;
5044+		zcs->outBuffFlushedSize += flushed;
5045+		output->pos += op - ostart;
5046+		if (toFlush == flushed)
5047+			zcs->stage = zcss_init; /* end reached */
5048+		return toFlush - flushed;
5049+	}
5050+}
5051+
5052+/*-=====  Pre-defined compression levels  =====-*/
5053+
5054+#define ZSTD_DEFAULT_CLEVEL 1
5055+#define ZSTD_MAX_CLEVEL 22
5056+int ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; }
5057+
5058+static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL + 1] = {
5059+    {
5060+	/* "default" */
5061+	/* W,  C,  H,  S,  L, TL, strat */
5062+	{18, 12, 12, 1, 7, 16, ZSTD_fast},    /* level  0 - never used */
5063+	{19, 13, 14, 1, 7, 16, ZSTD_fast},    /* level  1 */
5064+	{19, 15, 16, 1, 6, 16, ZSTD_fast},    /* level  2 */
5065+	{20, 16, 17, 1, 5, 16, ZSTD_dfast},   /* level  3.*/
5066+	{20, 18, 18, 1, 5, 16, ZSTD_dfast},   /* level  4.*/
5067+	{20, 15, 18, 3, 5, 16, ZSTD_greedy},  /* level  5 */
5068+	{21, 16, 19, 2, 5, 16, ZSTD_lazy},    /* level  6 */
5069+	{21, 17, 20, 3, 5, 16, ZSTD_lazy},    /* level  7 */
5070+	{21, 18, 20, 3, 5, 16, ZSTD_lazy2},   /* level  8 */
5071+	{21, 20, 20, 3, 5, 16, ZSTD_lazy2},   /* level  9 */
5072+	{21, 19, 21, 4, 5, 16, ZSTD_lazy2},   /* level 10 */
5073+	{22, 20, 22, 4, 5, 16, ZSTD_lazy2},   /* level 11 */
5074+	{22, 20, 22, 5, 5, 16, ZSTD_lazy2},   /* level 12 */
5075+	{22, 21, 22, 5, 5, 16, ZSTD_lazy2},   /* level 13 */
5076+	{22, 21, 22, 6, 5, 16, ZSTD_lazy2},   /* level 14 */
5077+	{22, 21, 21, 5, 5, 16, ZSTD_btlazy2}, /* level 15 */
5078+	{23, 22, 22, 5, 5, 16, ZSTD_btlazy2}, /* level 16 */
5079+	{23, 21, 22, 4, 5, 24, ZSTD_btopt},   /* level 17 */
5080+	{23, 23, 22, 6, 5, 32, ZSTD_btopt},   /* level 18 */
5081+	{23, 23, 22, 6, 3, 48, ZSTD_btopt},   /* level 19 */
5082+	{25, 25, 23, 7, 3, 64, ZSTD_btopt2},  /* level 20 */
5083+	{26, 26, 23, 7, 3, 256, ZSTD_btopt2}, /* level 21 */
5084+	{27, 27, 25, 9, 3, 512, ZSTD_btopt2}, /* level 22 */
5085+    },
5086+    {
5087+	/* for srcSize <= 256 KB */
5088+	/* W,  C,  H,  S,  L,  T, strat */
5089+	{0, 0, 0, 0, 0, 0, ZSTD_fast},	 /* level  0 - not used */
5090+	{18, 13, 14, 1, 6, 8, ZSTD_fast},      /* level  1 */
5091+	{18, 14, 13, 1, 5, 8, ZSTD_dfast},     /* level  2 */
5092+	{18, 16, 15, 1, 5, 8, ZSTD_dfast},     /* level  3 */
5093+	{18, 15, 17, 1, 5, 8, ZSTD_greedy},    /* level  4.*/
5094+	{18, 16, 17, 4, 5, 8, ZSTD_greedy},    /* level  5.*/
5095+	{18, 16, 17, 3, 5, 8, ZSTD_lazy},      /* level  6.*/
5096+	{18, 17, 17, 4, 4, 8, ZSTD_lazy},      /* level  7 */
5097+	{18, 17, 17, 4, 4, 8, ZSTD_lazy2},     /* level  8 */
5098+	{18, 17, 17, 5, 4, 8, ZSTD_lazy2},     /* level  9 */
5099+	{18, 17, 17, 6, 4, 8, ZSTD_lazy2},     /* level 10 */
5100+	{18, 18, 17, 6, 4, 8, ZSTD_lazy2},     /* level 11.*/
5101+	{18, 18, 17, 7, 4, 8, ZSTD_lazy2},     /* level 12.*/
5102+	{18, 19, 17, 6, 4, 8, ZSTD_btlazy2},   /* level 13 */
5103+	{18, 18, 18, 4, 4, 16, ZSTD_btopt},    /* level 14.*/
5104+	{18, 18, 18, 4, 3, 16, ZSTD_btopt},    /* level 15.*/
5105+	{18, 19, 18, 6, 3, 32, ZSTD_btopt},    /* level 16.*/
5106+	{18, 19, 18, 8, 3, 64, ZSTD_btopt},    /* level 17.*/
5107+	{18, 19, 18, 9, 3, 128, ZSTD_btopt},   /* level 18.*/
5108+	{18, 19, 18, 10, 3, 256, ZSTD_btopt},  /* level 19.*/
5109+	{18, 19, 18, 11, 3, 512, ZSTD_btopt2}, /* level 20.*/
5110+	{18, 19, 18, 12, 3, 512, ZSTD_btopt2}, /* level 21.*/
5111+	{18, 19, 18, 13, 3, 512, ZSTD_btopt2}, /* level 22.*/
5112+    },
5113+    {
5114+	/* for srcSize <= 128 KB */
5115+	/* W,  C,  H,  S,  L,  T, strat */
5116+	{17, 12, 12, 1, 7, 8, ZSTD_fast},      /* level  0 - not used */
5117+	{17, 12, 13, 1, 6, 8, ZSTD_fast},      /* level  1 */
5118+	{17, 13, 16, 1, 5, 8, ZSTD_fast},      /* level  2 */
5119+	{17, 16, 16, 2, 5, 8, ZSTD_dfast},     /* level  3 */
5120+	{17, 13, 15, 3, 4, 8, ZSTD_greedy},    /* level  4 */
5121+	{17, 15, 17, 4, 4, 8, ZSTD_greedy},    /* level  5 */
5122+	{17, 16, 17, 3, 4, 8, ZSTD_lazy},      /* level  6 */
5123+	{17, 15, 17, 4, 4, 8, ZSTD_lazy2},     /* level  7 */
5124+	{17, 17, 17, 4, 4, 8, ZSTD_lazy2},     /* level  8 */
5125+	{17, 17, 17, 5, 4, 8, ZSTD_lazy2},     /* level  9 */
5126+	{17, 17, 17, 6, 4, 8, ZSTD_lazy2},     /* level 10 */
5127+	{17, 17, 17, 7, 4, 8, ZSTD_lazy2},     /* level 11 */
5128+	{17, 17, 17, 8, 4, 8, ZSTD_lazy2},     /* level 12 */
5129+	{17, 18, 17, 6, 4, 8, ZSTD_btlazy2},   /* level 13.*/
5130+	{17, 17, 17, 7, 3, 8, ZSTD_btopt},     /* level 14.*/
5131+	{17, 17, 17, 7, 3, 16, ZSTD_btopt},    /* level 15.*/
5132+	{17, 18, 17, 7, 3, 32, ZSTD_btopt},    /* level 16.*/
5133+	{17, 18, 17, 7, 3, 64, ZSTD_btopt},    /* level 17.*/
5134+	{17, 18, 17, 7, 3, 256, ZSTD_btopt},   /* level 18.*/
5135+	{17, 18, 17, 8, 3, 256, ZSTD_btopt},   /* level 19.*/
5136+	{17, 18, 17, 9, 3, 256, ZSTD_btopt2},  /* level 20.*/
5137+	{17, 18, 17, 10, 3, 256, ZSTD_btopt2}, /* level 21.*/
5138+	{17, 18, 17, 11, 3, 512, ZSTD_btopt2}, /* level 22.*/
5139+    },
5140+    {
5141+	/* for srcSize <= 16 KB */
5142+	/* W,  C,  H,  S,  L,  T, strat */
5143+	{14, 12, 12, 1, 7, 6, ZSTD_fast},      /* level  0 - not used */
5144+	{14, 14, 14, 1, 6, 6, ZSTD_fast},      /* level  1 */
5145+	{14, 14, 14, 1, 4, 6, ZSTD_fast},      /* level  2 */
5146+	{14, 14, 14, 1, 4, 6, ZSTD_dfast},     /* level  3.*/
5147+	{14, 14, 14, 4, 4, 6, ZSTD_greedy},    /* level  4.*/
5148+	{14, 14, 14, 3, 4, 6, ZSTD_lazy},      /* level  5.*/
5149+	{14, 14, 14, 4, 4, 6, ZSTD_lazy2},     /* level  6 */
5150+	{14, 14, 14, 5, 4, 6, ZSTD_lazy2},     /* level  7 */
5151+	{14, 14, 14, 6, 4, 6, ZSTD_lazy2},     /* level  8.*/
5152+	{14, 15, 14, 6, 4, 6, ZSTD_btlazy2},   /* level  9.*/
5153+	{14, 15, 14, 3, 3, 6, ZSTD_btopt},     /* level 10.*/
5154+	{14, 15, 14, 6, 3, 8, ZSTD_btopt},     /* level 11.*/
5155+	{14, 15, 14, 6, 3, 16, ZSTD_btopt},    /* level 12.*/
5156+	{14, 15, 14, 6, 3, 24, ZSTD_btopt},    /* level 13.*/
5157+	{14, 15, 15, 6, 3, 48, ZSTD_btopt},    /* level 14.*/
5158+	{14, 15, 15, 6, 3, 64, ZSTD_btopt},    /* level 15.*/
5159+	{14, 15, 15, 6, 3, 96, ZSTD_btopt},    /* level 16.*/
5160+	{14, 15, 15, 6, 3, 128, ZSTD_btopt},   /* level 17.*/
5161+	{14, 15, 15, 6, 3, 256, ZSTD_btopt},   /* level 18.*/
5162+	{14, 15, 15, 7, 3, 256, ZSTD_btopt},   /* level 19.*/
5163+	{14, 15, 15, 8, 3, 256, ZSTD_btopt2},  /* level 20.*/
5164+	{14, 15, 15, 9, 3, 256, ZSTD_btopt2},  /* level 21.*/
5165+	{14, 15, 15, 10, 3, 256, ZSTD_btopt2}, /* level 22.*/
5166+    },
5167+};
5168+
5169+/*! ZSTD_getCParams() :
5170+*   @return ZSTD_compressionParameters structure for a selected compression level, `srcSize` and `dictSize`.
5171+*   Size values are optional, provide 0 if not known or unused */
5172+ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long srcSize, size_t dictSize)
5173+{
5174+	ZSTD_compressionParameters cp;
5175+	size_t const addedSize = srcSize ? 0 : 500;
5176+	U64 const rSize = srcSize + dictSize ? srcSize + dictSize + addedSize : (U64)-1;
5177+	U32 const tableID = (rSize <= 256 KB) + (rSize <= 128 KB) + (rSize <= 16 KB); /* intentional underflow for srcSizeHint == 0 */
5178+	if (compressionLevel <= 0)
5179+		compressionLevel = ZSTD_DEFAULT_CLEVEL; /* 0 == default; no negative compressionLevel yet */
5180+	if (compressionLevel > ZSTD_MAX_CLEVEL)
5181+		compressionLevel = ZSTD_MAX_CLEVEL;
5182+	cp = ZSTD_defaultCParameters[tableID][compressionLevel];
5183+	if (ZSTD_32bits()) { /* auto-correction, for 32-bits mode */
5184+		if (cp.windowLog > ZSTD_WINDOWLOG_MAX)
5185+			cp.windowLog = ZSTD_WINDOWLOG_MAX;
5186+		if (cp.chainLog > ZSTD_CHAINLOG_MAX)
5187+			cp.chainLog = ZSTD_CHAINLOG_MAX;
5188+		if (cp.hashLog > ZSTD_HASHLOG_MAX)
5189+			cp.hashLog = ZSTD_HASHLOG_MAX;
5190+	}
5191+	cp = ZSTD_adjustCParams(cp, srcSize, dictSize);
5192+	return cp;
5193+}
5194+
5195+/*! ZSTD_getParams() :
5196+*   same as ZSTD_getCParams(), but @return a `ZSTD_parameters` object (instead of `ZSTD_compressionParameters`).
5197+*   All fields of `ZSTD_frameParameters` are set to default (0) */
5198+ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize, size_t dictSize)
5199+{
5200+	ZSTD_parameters params;
5201+	ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, srcSize, dictSize);
5202+	memset(&params, 0, sizeof(params));
5203+	params.cParams = cParams;
5204+	return params;
5205+}
5206+
5207+EXPORT_SYMBOL(ZSTD_maxCLevel);
5208+EXPORT_SYMBOL(ZSTD_compressBound);
5209+
5210+EXPORT_SYMBOL(ZSTD_CCtxWorkspaceBound);
5211+EXPORT_SYMBOL(ZSTD_initCCtx);
5212+EXPORT_SYMBOL(ZSTD_compressCCtx);
5213+EXPORT_SYMBOL(ZSTD_compress_usingDict);
5214+
5215+EXPORT_SYMBOL(ZSTD_CDictWorkspaceBound);
5216+EXPORT_SYMBOL(ZSTD_initCDict);
5217+EXPORT_SYMBOL(ZSTD_compress_usingCDict);
5218+
5219+EXPORT_SYMBOL(ZSTD_CStreamWorkspaceBound);
5220+EXPORT_SYMBOL(ZSTD_initCStream);
5221+EXPORT_SYMBOL(ZSTD_initCStream_usingCDict);
5222+EXPORT_SYMBOL(ZSTD_resetCStream);
5223+EXPORT_SYMBOL(ZSTD_compressStream);
5224+EXPORT_SYMBOL(ZSTD_flushStream);
5225+EXPORT_SYMBOL(ZSTD_endStream);
5226+EXPORT_SYMBOL(ZSTD_CStreamInSize);
5227+EXPORT_SYMBOL(ZSTD_CStreamOutSize);
5228+
5229+EXPORT_SYMBOL(ZSTD_getCParams);
5230+EXPORT_SYMBOL(ZSTD_getParams);
5231+EXPORT_SYMBOL(ZSTD_checkCParams);
5232+EXPORT_SYMBOL(ZSTD_adjustCParams);
5233+
5234+EXPORT_SYMBOL(ZSTD_compressBegin);
5235+EXPORT_SYMBOL(ZSTD_compressBegin_usingDict);
5236+EXPORT_SYMBOL(ZSTD_compressBegin_advanced);
5237+EXPORT_SYMBOL(ZSTD_copyCCtx);
5238+EXPORT_SYMBOL(ZSTD_compressBegin_usingCDict);
5239+EXPORT_SYMBOL(ZSTD_compressContinue);
5240+EXPORT_SYMBOL(ZSTD_compressEnd);
5241+
5242+EXPORT_SYMBOL(ZSTD_getBlockSizeMax);
5243+EXPORT_SYMBOL(ZSTD_compressBlock);
5244+
5245+MODULE_LICENSE("Dual BSD/GPL");
5246+MODULE_DESCRIPTION("Zstd Compressor");
5247diff --git a/lib/zstd/decompress.c b/lib/zstd/decompress.c
5248new file mode 100644
5249index 0000000..72df4828
5250--- /dev/null
5251+++ b/lib/zstd/decompress.c
5252@@ -0,0 +1,2526 @@
5253+/**
5254+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5255+ * All rights reserved.
5256+ *
5257+ * This source code is licensed under the BSD-style license found in the
5258+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
5259+ *
5260+ * This program is free software; you can redistribute it and/or modify it under
5261+ * the terms of the GNU General Public License version 2 as published by the
5262+ * Free Software Foundation. This program is dual-licensed; you may select
5263+ * either version 2 of the GNU General Public License ("GPL") or BSD license
5264+ * ("BSD").
5265+ */
5266+
5267+/* ***************************************************************
5268+*  Tuning parameters
5269+*****************************************************************/
5270+/*!
5271+*  MAXWINDOWSIZE_DEFAULT :
5272+*  maximum window size accepted by DStream, by default.
5273+*  Frames requiring more memory will be rejected.
5274+*/
5275+#ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
5276+#define ZSTD_MAXWINDOWSIZE_DEFAULT ((1 << ZSTD_WINDOWLOG_MAX) + 1) /* defined within zstd.h */
5277+#endif
5278+
5279+/*-*******************************************************
5280+*  Dependencies
5281+*********************************************************/
5282+#include "fse.h"
5283+#include "huf.h"
5284+#include "mem.h" /* low level memory routines */
5285+#include "zstd_internal.h"
5286+#include <linux/kernel.h>
5287+#include <linux/module.h>
5288+#include <linux/string.h> /* memcpy, memmove, memset */
5289+
5290+#define ZSTD_PREFETCH(ptr) __builtin_prefetch(ptr, 0, 0)
5291+
5292+/*-*************************************
5293+*  Macros
5294+***************************************/
5295+#define ZSTD_isError ERR_isError /* for inlining */
5296+#define FSE_isError ERR_isError
5297+#define HUF_isError ERR_isError
5298+
5299+/*_*******************************************************
5300+*  Memory operations
5301+**********************************************************/
5302+static void ZSTD_copy4(void *dst, const void *src) { memcpy(dst, src, 4); }
5303+
5304+/*-*************************************************************
5305+*   Context management
5306+***************************************************************/
5307+typedef enum {
5308+	ZSTDds_getFrameHeaderSize,
5309+	ZSTDds_decodeFrameHeader,
5310+	ZSTDds_decodeBlockHeader,
5311+	ZSTDds_decompressBlock,
5312+	ZSTDds_decompressLastBlock,
5313+	ZSTDds_checkChecksum,
5314+	ZSTDds_decodeSkippableHeader,
5315+	ZSTDds_skipFrame
5316+} ZSTD_dStage;
5317+
5318+typedef struct {
5319+	FSE_DTable LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
5320+	FSE_DTable OFTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
5321+	FSE_DTable MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
5322+	HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */
5323+	U64 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32 / 2];
5324+	U32 rep[ZSTD_REP_NUM];
5325+} ZSTD_entropyTables_t;
5326+
5327+struct ZSTD_DCtx_s {
5328+	const FSE_DTable *LLTptr;
5329+	const FSE_DTable *MLTptr;
5330+	const FSE_DTable *OFTptr;
5331+	const HUF_DTable *HUFptr;
5332+	ZSTD_entropyTables_t entropy;
5333+	const void *previousDstEnd; /* detect continuity */
5334+	const void *base;	   /* start of curr segment */
5335+	const void *vBase;	  /* virtual start of previous segment if it was just before curr one */
5336+	const void *dictEnd;	/* end of previous segment */
5337+	size_t expected;
5338+	ZSTD_frameParams fParams;
5339+	blockType_e bType; /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */
5340+	ZSTD_dStage stage;
5341+	U32 litEntropy;
5342+	U32 fseEntropy;
5343+	struct xxh64_state xxhState;
5344+	size_t headerSize;
5345+	U32 dictID;
5346+	const BYTE *litPtr;
5347+	ZSTD_customMem customMem;
5348+	size_t litSize;
5349+	size_t rleSize;
5350+	BYTE litBuffer[ZSTD_BLOCKSIZE_ABSOLUTEMAX + WILDCOPY_OVERLENGTH];
5351+	BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
5352+}; /* typedef'd to ZSTD_DCtx within "zstd.h" */
5353+
5354+size_t ZSTD_DCtxWorkspaceBound(void) { return ZSTD_ALIGN(sizeof(ZSTD_stack)) + ZSTD_ALIGN(sizeof(ZSTD_DCtx)); }
5355+
5356+size_t ZSTD_decompressBegin(ZSTD_DCtx *dctx)
5357+{
5358+	dctx->expected = ZSTD_frameHeaderSize_prefix;
5359+	dctx->stage = ZSTDds_getFrameHeaderSize;
5360+	dctx->previousDstEnd = NULL;
5361+	dctx->base = NULL;
5362+	dctx->vBase = NULL;
5363+	dctx->dictEnd = NULL;
5364+	dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
5365+	dctx->litEntropy = dctx->fseEntropy = 0;
5366+	dctx->dictID = 0;
5367+	ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
5368+	memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
5369+	dctx->LLTptr = dctx->entropy.LLTable;
5370+	dctx->MLTptr = dctx->entropy.MLTable;
5371+	dctx->OFTptr = dctx->entropy.OFTable;
5372+	dctx->HUFptr = dctx->entropy.hufTable;
5373+	return 0;
5374+}
5375+
5376+ZSTD_DCtx *ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
5377+{
5378+	ZSTD_DCtx *dctx;
5379+
5380+	if (!customMem.customAlloc || !customMem.customFree)
5381+		return NULL;
5382+
5383+	dctx = (ZSTD_DCtx *)ZSTD_malloc(sizeof(ZSTD_DCtx), customMem);
5384+	if (!dctx)
5385+		return NULL;
5386+	memcpy(&dctx->customMem, &customMem, sizeof(customMem));
5387+	ZSTD_decompressBegin(dctx);
5388+	return dctx;
5389+}
5390+
5391+ZSTD_DCtx *ZSTD_initDCtx(void *workspace, size_t workspaceSize)
5392+{
5393+	ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
5394+	return ZSTD_createDCtx_advanced(stackMem);
5395+}
5396+
5397+size_t ZSTD_freeDCtx(ZSTD_DCtx *dctx)
5398+{
5399+	if (dctx == NULL)
5400+		return 0; /* support free on NULL */
5401+	ZSTD_free(dctx, dctx->customMem);
5402+	return 0; /* reserved as a potential error code in the future */
5403+}
5404+
5405+void ZSTD_copyDCtx(ZSTD_DCtx *dstDCtx, const ZSTD_DCtx *srcDCtx)
5406+{
5407+	size_t const workSpaceSize = (ZSTD_BLOCKSIZE_ABSOLUTEMAX + WILDCOPY_OVERLENGTH) + ZSTD_frameHeaderSize_max;
5408+	memcpy(dstDCtx, srcDCtx, sizeof(ZSTD_DCtx) - workSpaceSize); /* no need to copy workspace */
5409+}
5410+
5411+static void ZSTD_refDDict(ZSTD_DCtx *dstDCtx, const ZSTD_DDict *ddict);
5412+
5413+/*-*************************************************************
5414+*   Decompression section
5415+***************************************************************/
5416+
5417+/*! ZSTD_isFrame() :
5418+ *  Tells if the content of `buffer` starts with a valid Frame Identifier.
5419+ *  Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
5420+ *  Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
5421+ *  Note 3 : Skippable Frame Identifiers are considered valid. */
5422+unsigned ZSTD_isFrame(const void *buffer, size_t size)
5423+{
5424+	if (size < 4)
5425+		return 0;
5426+	{
5427+		U32 const magic = ZSTD_readLE32(buffer);
5428+		if (magic == ZSTD_MAGICNUMBER)
5429+			return 1;
5430+		if ((magic & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START)
5431+			return 1;
5432+	}
5433+	return 0;
5434+}
5435+
5436+/** ZSTD_frameHeaderSize() :
5437+*   srcSize must be >= ZSTD_frameHeaderSize_prefix.
5438+*   @return : size of the Frame Header */
5439+static size_t ZSTD_frameHeaderSize(const void *src, size_t srcSize)
5440+{
5441+	if (srcSize < ZSTD_frameHeaderSize_prefix)
5442+		return ERROR(srcSize_wrong);
5443+	{
5444+		BYTE const fhd = ((const BYTE *)src)[4];
5445+		U32 const dictID = fhd & 3;
5446+		U32 const singleSegment = (fhd >> 5) & 1;
5447+		U32 const fcsId = fhd >> 6;
5448+		return ZSTD_frameHeaderSize_prefix + !singleSegment + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] + (singleSegment && !fcsId);
5449+	}
5450+}
5451+
5452+/** ZSTD_getFrameParams() :
5453+*   decode Frame Header, or require larger `srcSize`.
5454+*   @return : 0, `fparamsPtr` is correctly filled,
5455+*            >0, `srcSize` is too small, result is expected `srcSize`,
5456+*             or an error code, which can be tested using ZSTD_isError() */
5457+size_t ZSTD_getFrameParams(ZSTD_frameParams *fparamsPtr, const void *src, size_t srcSize)
5458+{
5459+	const BYTE *ip = (const BYTE *)src;
5460+
5461+	if (srcSize < ZSTD_frameHeaderSize_prefix)
5462+		return ZSTD_frameHeaderSize_prefix;
5463+	if (ZSTD_readLE32(src) != ZSTD_MAGICNUMBER) {
5464+		if ((ZSTD_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
5465+			if (srcSize < ZSTD_skippableHeaderSize)
5466+				return ZSTD_skippableHeaderSize; /* magic number + skippable frame length */
5467+			memset(fparamsPtr, 0, sizeof(*fparamsPtr));
5468+			fparamsPtr->frameContentSize = ZSTD_readLE32((const char *)src + 4);
5469+			fparamsPtr->windowSize = 0; /* windowSize==0 means a frame is skippable */
5470+			return 0;
5471+		}
5472+		return ERROR(prefix_unknown);
5473+	}
5474+
5475+	/* ensure there is enough `srcSize` to fully read/decode frame header */
5476+	{
5477+		size_t const fhsize = ZSTD_frameHeaderSize(src, srcSize);
5478+		if (srcSize < fhsize)
5479+			return fhsize;
5480+	}
5481+
5482+	{
5483+		BYTE const fhdByte = ip[4];
5484+		size_t pos = 5;
5485+		U32 const dictIDSizeCode = fhdByte & 3;
5486+		U32 const checksumFlag = (fhdByte >> 2) & 1;
5487+		U32 const singleSegment = (fhdByte >> 5) & 1;
5488+		U32 const fcsID = fhdByte >> 6;
5489+		U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX;
5490+		U32 windowSize = 0;
5491+		U32 dictID = 0;
5492+		U64 frameContentSize = 0;
5493+		if ((fhdByte & 0x08) != 0)
5494+			return ERROR(frameParameter_unsupported); /* reserved bits, which must be zero */
5495+		if (!singleSegment) {
5496+			BYTE const wlByte = ip[pos++];
5497+			U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
5498+			if (windowLog > ZSTD_WINDOWLOG_MAX)
5499+				return ERROR(frameParameter_windowTooLarge); /* avoids issue with 1 << windowLog */
5500+			windowSize = (1U << windowLog);
5501+			windowSize += (windowSize >> 3) * (wlByte & 7);
5502+		}
5503+
5504+		switch (dictIDSizeCode) {
5505+		default: /* impossible */
5506+		case 0: break;
5507+		case 1:
5508+			dictID = ip[pos];
5509+			pos++;
5510+			break;
5511+		case 2:
5512+			dictID = ZSTD_readLE16(ip + pos);
5513+			pos += 2;
5514+			break;
5515+		case 3:
5516+			dictID = ZSTD_readLE32(ip + pos);
5517+			pos += 4;
5518+			break;
5519+		}
5520+		switch (fcsID) {
5521+		default: /* impossible */
5522+		case 0:
5523+			if (singleSegment)
5524+				frameContentSize = ip[pos];
5525+			break;
5526+		case 1: frameContentSize = ZSTD_readLE16(ip + pos) + 256; break;
5527+		case 2: frameContentSize = ZSTD_readLE32(ip + pos); break;
5528+		case 3: frameContentSize = ZSTD_readLE64(ip + pos); break;
5529+		}
5530+		if (!windowSize)
5531+			windowSize = (U32)frameContentSize;
5532+		if (windowSize > windowSizeMax)
5533+			return ERROR(frameParameter_windowTooLarge);
5534+		fparamsPtr->frameContentSize = frameContentSize;
5535+		fparamsPtr->windowSize = windowSize;
5536+		fparamsPtr->dictID = dictID;
5537+		fparamsPtr->checksumFlag = checksumFlag;
5538+	}
5539+	return 0;
5540+}
5541+
5542+/** ZSTD_getFrameContentSize() :
5543+*   compatible with legacy mode
5544+*   @return : decompressed size of the single frame pointed to be `src` if known, otherwise
5545+*             - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
5546+*             - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
5547+unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
5548+{
5549+	{
5550+		ZSTD_frameParams fParams;
5551+		if (ZSTD_getFrameParams(&fParams, src, srcSize) != 0)
5552+			return ZSTD_CONTENTSIZE_ERROR;
5553+		if (fParams.windowSize == 0) {
5554+			/* Either skippable or empty frame, size == 0 either way */
5555+			return 0;
5556+		} else if (fParams.frameContentSize != 0) {
5557+			return fParams.frameContentSize;
5558+		} else {
5559+			return ZSTD_CONTENTSIZE_UNKNOWN;
5560+		}
5561+	}
5562+}
5563+
5564+/** ZSTD_findDecompressedSize() :
5565+ *  compatible with legacy mode
5566+ *  `srcSize` must be the exact length of some number of ZSTD compressed and/or
5567+ *      skippable frames
5568+ *  @return : decompressed size of the frames contained */
5569+unsigned long long ZSTD_findDecompressedSize(const void *src, size_t srcSize)
5570+{
5571+	{
5572+		unsigned long long totalDstSize = 0;
5573+		while (srcSize >= ZSTD_frameHeaderSize_prefix) {
5574+			const U32 magicNumber = ZSTD_readLE32(src);
5575+
5576+			if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
5577+				size_t skippableSize;
5578+				if (srcSize < ZSTD_skippableHeaderSize)
5579+					return ERROR(srcSize_wrong);
5580+				skippableSize = ZSTD_readLE32((const BYTE *)src + 4) + ZSTD_skippableHeaderSize;
5581+				if (srcSize < skippableSize) {
5582+					return ZSTD_CONTENTSIZE_ERROR;
5583+				}
5584+
5585+				src = (const BYTE *)src + skippableSize;
5586+				srcSize -= skippableSize;
5587+				continue;
5588+			}
5589+
5590+			{
5591+				unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
5592+				if (ret >= ZSTD_CONTENTSIZE_ERROR)
5593+					return ret;
5594+
5595+				/* check for overflow */
5596+				if (totalDstSize + ret < totalDstSize)
5597+					return ZSTD_CONTENTSIZE_ERROR;
5598+				totalDstSize += ret;
5599+			}
5600+			{
5601+				size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
5602+				if (ZSTD_isError(frameSrcSize)) {
5603+					return ZSTD_CONTENTSIZE_ERROR;
5604+				}
5605+
5606+				src = (const BYTE *)src + frameSrcSize;
5607+				srcSize -= frameSrcSize;
5608+			}
5609+		}
5610+
5611+		if (srcSize) {
5612+			return ZSTD_CONTENTSIZE_ERROR;
5613+		}
5614+
5615+		return totalDstSize;
5616+	}
5617+}
5618+
5619+/** ZSTD_decodeFrameHeader() :
5620+*   `headerSize` must be the size provided by ZSTD_frameHeaderSize().
5621+*   @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
5622+static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx *dctx, const void *src, size_t headerSize)
5623+{
5624+	size_t const result = ZSTD_getFrameParams(&(dctx->fParams), src, headerSize);
5625+	if (ZSTD_isError(result))
5626+		return result; /* invalid header */
5627+	if (result > 0)
5628+		return ERROR(srcSize_wrong); /* headerSize too small */
5629+	if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID))
5630+		return ERROR(dictionary_wrong);
5631+	if (dctx->fParams.checksumFlag)
5632+		xxh64_reset(&dctx->xxhState, 0);
5633+	return 0;
5634+}
5635+
5636+typedef struct {
5637+	blockType_e blockType;
5638+	U32 lastBlock;
5639+	U32 origSize;
5640+} blockProperties_t;
5641+
5642+/*! ZSTD_getcBlockSize() :
5643+*   Provides the size of compressed block from block header `src` */
5644+size_t ZSTD_getcBlockSize(const void *src, size_t srcSize, blockProperties_t *bpPtr)
5645+{
5646+	if (srcSize < ZSTD_blockHeaderSize)
5647+		return ERROR(srcSize_wrong);
5648+	{
5649+		U32 const cBlockHeader = ZSTD_readLE24(src);
5650+		U32 const cSize = cBlockHeader >> 3;
5651+		bpPtr->lastBlock = cBlockHeader & 1;
5652+		bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3);
5653+		bpPtr->origSize = cSize; /* only useful for RLE */
5654+		if (bpPtr->blockType == bt_rle)
5655+			return 1;
5656+		if (bpPtr->blockType == bt_reserved)
5657+			return ERROR(corruption_detected);
5658+		return cSize;
5659+	}
5660+}
5661+
5662+static size_t ZSTD_copyRawBlock(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
5663+{
5664+	if (srcSize > dstCapacity)
5665+		return ERROR(dstSize_tooSmall);
5666+	memcpy(dst, src, srcSize);
5667+	return srcSize;
5668+}
5669+
5670+static size_t ZSTD_setRleBlock(void *dst, size_t dstCapacity, const void *src, size_t srcSize, size_t regenSize)
5671+{
5672+	if (srcSize != 1)
5673+		return ERROR(srcSize_wrong);
5674+	if (regenSize > dstCapacity)
5675+		return ERROR(dstSize_tooSmall);
5676+	memset(dst, *(const BYTE *)src, regenSize);
5677+	return regenSize;
5678+}
5679+
5680+/*! ZSTD_decodeLiteralsBlock() :
5681+	@return : nb of bytes read from src (< srcSize ) */
5682+size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx *dctx, const void *src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
5683+{
5684+	if (srcSize < MIN_CBLOCK_SIZE)
5685+		return ERROR(corruption_detected);
5686+
5687+	{
5688+		const BYTE *const istart = (const BYTE *)src;
5689+		symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
5690+
5691+		switch (litEncType) {
5692+		case set_repeat:
5693+			if (dctx->litEntropy == 0)
5694+				return ERROR(dictionary_corrupted);
5695+		/* fall-through */
5696+		case set_compressed:
5697+			if (srcSize < 5)
5698+				return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */
5699+			{
5700+				size_t lhSize, litSize, litCSize;
5701+				U32 singleStream = 0;
5702+				U32 const lhlCode = (istart[0] >> 2) & 3;
5703+				U32 const lhc = ZSTD_readLE32(istart);
5704+				switch (lhlCode) {
5705+				case 0:
5706+				case 1:
5707+				default: /* note : default is impossible, since lhlCode into [0..3] */
5708+					/* 2 - 2 - 10 - 10 */
5709+					singleStream = !lhlCode;
5710+					lhSize = 3;
5711+					litSize = (lhc >> 4) & 0x3FF;
5712+					litCSize = (lhc >> 14) & 0x3FF;
5713+					break;
5714+				case 2:
5715+					/* 2 - 2 - 14 - 14 */
5716+					lhSize = 4;
5717+					litSize = (lhc >> 4) & 0x3FFF;
5718+					litCSize = lhc >> 18;
5719+					break;
5720+				case 3:
5721+					/* 2 - 2 - 18 - 18 */
5722+					lhSize = 5;
5723+					litSize = (lhc >> 4) & 0x3FFFF;
5724+					litCSize = (lhc >> 22) + (istart[4] << 10);
5725+					break;
5726+				}
5727+				if (litSize > ZSTD_BLOCKSIZE_ABSOLUTEMAX)
5728+					return ERROR(corruption_detected);
5729+				if (litCSize + lhSize > srcSize)
5730+					return ERROR(corruption_detected);
5731+
5732+				if (HUF_isError(
5733+					(litEncType == set_repeat)
5734+					    ? (singleStream ? HUF_decompress1X_usingDTable(dctx->litBuffer, litSize, istart + lhSize, litCSize, dctx->HUFptr)
5735+							    : HUF_decompress4X_usingDTable(dctx->litBuffer, litSize, istart + lhSize, litCSize, dctx->HUFptr))
5736+					    : (singleStream
5737+						   ? HUF_decompress1X2_DCtx_wksp(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart + lhSize, litCSize,
5738+										 dctx->entropy.workspace, sizeof(dctx->entropy.workspace))
5739+						   : HUF_decompress4X_hufOnly_wksp(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart + lhSize, litCSize,
5740+										   dctx->entropy.workspace, sizeof(dctx->entropy.workspace)))))
5741+					return ERROR(corruption_detected);
5742+
5743+				dctx->litPtr = dctx->litBuffer;
5744+				dctx->litSize = litSize;
5745+				dctx->litEntropy = 1;
5746+				if (litEncType == set_compressed)
5747+					dctx->HUFptr = dctx->entropy.hufTable;
5748+				memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
5749+				return litCSize + lhSize;
5750+			}
5751+
5752+		case set_basic: {
5753+			size_t litSize, lhSize;
5754+			U32 const lhlCode = ((istart[0]) >> 2) & 3;
5755+			switch (lhlCode) {
5756+			case 0:
5757+			case 2:
5758+			default: /* note : default is impossible, since lhlCode into [0..3] */
5759+				lhSize = 1;
5760+				litSize = istart[0] >> 3;
5761+				break;
5762+			case 1:
5763+				lhSize = 2;
5764+				litSize = ZSTD_readLE16(istart) >> 4;
5765+				break;
5766+			case 3:
5767+				lhSize = 3;
5768+				litSize = ZSTD_readLE24(istart) >> 4;
5769+				break;
5770+			}
5771+
5772+			if (lhSize + litSize + WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
5773+				if (litSize + lhSize > srcSize)
5774+					return ERROR(corruption_detected);
5775+				memcpy(dctx->litBuffer, istart + lhSize, litSize);
5776+				dctx->litPtr = dctx->litBuffer;
5777+				dctx->litSize = litSize;
5778+				memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
5779+				return lhSize + litSize;
5780+			}
5781+			/* direct reference into compressed stream */
5782+			dctx->litPtr = istart + lhSize;
5783+			dctx->litSize = litSize;
5784+			return lhSize + litSize;
5785+		}
5786+
5787+		case set_rle: {
5788+			U32 const lhlCode = ((istart[0]) >> 2) & 3;
5789+			size_t litSize, lhSize;
5790+			switch (lhlCode) {
5791+			case 0:
5792+			case 2:
5793+			default: /* note : default is impossible, since lhlCode into [0..3] */
5794+				lhSize = 1;
5795+				litSize = istart[0] >> 3;
5796+				break;
5797+			case 1:
5798+				lhSize = 2;
5799+				litSize = ZSTD_readLE16(istart) >> 4;
5800+				break;
5801+			case 3:
5802+				lhSize = 3;
5803+				litSize = ZSTD_readLE24(istart) >> 4;
5804+				if (srcSize < 4)
5805+					return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
5806+				break;
5807+			}
5808+			if (litSize > ZSTD_BLOCKSIZE_ABSOLUTEMAX)
5809+				return ERROR(corruption_detected);
5810+			memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
5811+			dctx->litPtr = dctx->litBuffer;
5812+			dctx->litSize = litSize;
5813+			return lhSize + 1;
5814+		}
5815+		default:
5816+			return ERROR(corruption_detected); /* impossible */
5817+		}
5818+	}
5819+}
5820+
5821+typedef union {
5822+	FSE_decode_t realData;
5823+	U32 alignedBy4;
5824+} FSE_decode_t4;
5825+
5826+static const FSE_decode_t4 LL_defaultDTable[(1 << LL_DEFAULTNORMLOG) + 1] = {
5827+    {{LL_DEFAULTNORMLOG, 1, 1}}, /* header : tableLog, fastMode, fastMode */
5828+    {{0, 0, 4}},		 /* 0 : base, symbol, bits */
5829+    {{16, 0, 4}},
5830+    {{32, 1, 5}},
5831+    {{0, 3, 5}},
5832+    {{0, 4, 5}},
5833+    {{0, 6, 5}},
5834+    {{0, 7, 5}},
5835+    {{0, 9, 5}},
5836+    {{0, 10, 5}},
5837+    {{0, 12, 5}},
5838+    {{0, 14, 6}},
5839+    {{0, 16, 5}},
5840+    {{0, 18, 5}},
5841+    {{0, 19, 5}},
5842+    {{0, 21, 5}},
5843+    {{0, 22, 5}},
5844+    {{0, 24, 5}},
5845+    {{32, 25, 5}},
5846+    {{0, 26, 5}},
5847+    {{0, 27, 6}},
5848+    {{0, 29, 6}},
5849+    {{0, 31, 6}},
5850+    {{32, 0, 4}},
5851+    {{0, 1, 4}},
5852+    {{0, 2, 5}},
5853+    {{32, 4, 5}},
5854+    {{0, 5, 5}},
5855+    {{32, 7, 5}},
5856+    {{0, 8, 5}},
5857+    {{32, 10, 5}},
5858+    {{0, 11, 5}},
5859+    {{0, 13, 6}},
5860+    {{32, 16, 5}},
5861+    {{0, 17, 5}},
5862+    {{32, 19, 5}},
5863+    {{0, 20, 5}},
5864+    {{32, 22, 5}},
5865+    {{0, 23, 5}},
5866+    {{0, 25, 4}},
5867+    {{16, 25, 4}},
5868+    {{32, 26, 5}},
5869+    {{0, 28, 6}},
5870+    {{0, 30, 6}},
5871+    {{48, 0, 4}},
5872+    {{16, 1, 4}},
5873+    {{32, 2, 5}},
5874+    {{32, 3, 5}},
5875+    {{32, 5, 5}},
5876+    {{32, 6, 5}},
5877+    {{32, 8, 5}},
5878+    {{32, 9, 5}},
5879+    {{32, 11, 5}},
5880+    {{32, 12, 5}},
5881+    {{0, 15, 6}},
5882+    {{32, 17, 5}},
5883+    {{32, 18, 5}},
5884+    {{32, 20, 5}},
5885+    {{32, 21, 5}},
5886+    {{32, 23, 5}},
5887+    {{32, 24, 5}},
5888+    {{0, 35, 6}},
5889+    {{0, 34, 6}},
5890+    {{0, 33, 6}},
5891+    {{0, 32, 6}},
5892+}; /* LL_defaultDTable */
5893+
5894+static const FSE_decode_t4 ML_defaultDTable[(1 << ML_DEFAULTNORMLOG) + 1] = {
5895+    {{ML_DEFAULTNORMLOG, 1, 1}}, /* header : tableLog, fastMode, fastMode */
5896+    {{0, 0, 6}},		 /* 0 : base, symbol, bits */
5897+    {{0, 1, 4}},
5898+    {{32, 2, 5}},
5899+    {{0, 3, 5}},
5900+    {{0, 5, 5}},
5901+    {{0, 6, 5}},
5902+    {{0, 8, 5}},
5903+    {{0, 10, 6}},
5904+    {{0, 13, 6}},
5905+    {{0, 16, 6}},
5906+    {{0, 19, 6}},
5907+    {{0, 22, 6}},
5908+    {{0, 25, 6}},
5909+    {{0, 28, 6}},
5910+    {{0, 31, 6}},
5911+    {{0, 33, 6}},
5912+    {{0, 35, 6}},
5913+    {{0, 37, 6}},
5914+    {{0, 39, 6}},
5915+    {{0, 41, 6}},
5916+    {{0, 43, 6}},
5917+    {{0, 45, 6}},
5918+    {{16, 1, 4}},
5919+    {{0, 2, 4}},
5920+    {{32, 3, 5}},
5921+    {{0, 4, 5}},
5922+    {{32, 6, 5}},
5923+    {{0, 7, 5}},
5924+    {{0, 9, 6}},
5925+    {{0, 12, 6}},
5926+    {{0, 15, 6}},
5927+    {{0, 18, 6}},
5928+    {{0, 21, 6}},
5929+    {{0, 24, 6}},
5930+    {{0, 27, 6}},
5931+    {{0, 30, 6}},
5932+    {{0, 32, 6}},
5933+    {{0, 34, 6}},
5934+    {{0, 36, 6}},
5935+    {{0, 38, 6}},
5936+    {{0, 40, 6}},
5937+    {{0, 42, 6}},
5938+    {{0, 44, 6}},
5939+    {{32, 1, 4}},
5940+    {{48, 1, 4}},
5941+    {{16, 2, 4}},
5942+    {{32, 4, 5}},
5943+    {{32, 5, 5}},
5944+    {{32, 7, 5}},
5945+    {{32, 8, 5}},
5946+    {{0, 11, 6}},
5947+    {{0, 14, 6}},
5948+    {{0, 17, 6}},
5949+    {{0, 20, 6}},
5950+    {{0, 23, 6}},
5951+    {{0, 26, 6}},
5952+    {{0, 29, 6}},
5953+    {{0, 52, 6}},
5954+    {{0, 51, 6}},
5955+    {{0, 50, 6}},
5956+    {{0, 49, 6}},
5957+    {{0, 48, 6}},
5958+    {{0, 47, 6}},
5959+    {{0, 46, 6}},
5960+}; /* ML_defaultDTable */
5961+
5962+static const FSE_decode_t4 OF_defaultDTable[(1 << OF_DEFAULTNORMLOG) + 1] = {
5963+    {{OF_DEFAULTNORMLOG, 1, 1}}, /* header : tableLog, fastMode, fastMode */
5964+    {{0, 0, 5}},		 /* 0 : base, symbol, bits */
5965+    {{0, 6, 4}},
5966+    {{0, 9, 5}},
5967+    {{0, 15, 5}},
5968+    {{0, 21, 5}},
5969+    {{0, 3, 5}},
5970+    {{0, 7, 4}},
5971+    {{0, 12, 5}},
5972+    {{0, 18, 5}},
5973+    {{0, 23, 5}},
5974+    {{0, 5, 5}},
5975+    {{0, 8, 4}},
5976+    {{0, 14, 5}},
5977+    {{0, 20, 5}},
5978+    {{0, 2, 5}},
5979+    {{16, 7, 4}},
5980+    {{0, 11, 5}},
5981+    {{0, 17, 5}},
5982+    {{0, 22, 5}},
5983+    {{0, 4, 5}},
5984+    {{16, 8, 4}},
5985+    {{0, 13, 5}},
5986+    {{0, 19, 5}},
5987+    {{0, 1, 5}},
5988+    {{16, 6, 4}},
5989+    {{0, 10, 5}},
5990+    {{0, 16, 5}},
5991+    {{0, 28, 5}},
5992+    {{0, 27, 5}},
5993+    {{0, 26, 5}},
5994+    {{0, 25, 5}},
5995+    {{0, 24, 5}},
5996+}; /* OF_defaultDTable */
5997+
5998+/*! ZSTD_buildSeqTable() :
5999+	@return : nb bytes read from src,
6000+			  or an error code if it fails, testable with ZSTD_isError()
6001+*/
6002+static size_t ZSTD_buildSeqTable(FSE_DTable *DTableSpace, const FSE_DTable **DTablePtr, symbolEncodingType_e type, U32 max, U32 maxLog, const void *src,
6003+				 size_t srcSize, const FSE_decode_t4 *defaultTable, U32 flagRepeatTable, void *workspace, size_t workspaceSize)
6004+{
6005+	const void *const tmpPtr = defaultTable; /* bypass strict aliasing */
6006+	switch (type) {
6007+	case set_rle:
6008+		if (!srcSize)
6009+			return ERROR(srcSize_wrong);
6010+		if ((*(const BYTE *)src) > max)
6011+			return ERROR(corruption_detected);
6012+		FSE_buildDTable_rle(DTableSpace, *(const BYTE *)src);
6013+		*DTablePtr = DTableSpace;
6014+		return 1;
6015+	case set_basic: *DTablePtr = (const FSE_DTable *)tmpPtr; return 0;
6016+	case set_repeat:
6017+		if (!flagRepeatTable)
6018+			return ERROR(corruption_detected);
6019+		return 0;
6020+	default: /* impossible */
6021+	case set_compressed: {
6022+		U32 tableLog;
6023+		S16 *norm = (S16 *)workspace;
6024+		size_t const spaceUsed32 = ALIGN(sizeof(S16) * (MaxSeq + 1), sizeof(U32)) >> 2;
6025+
6026+		if ((spaceUsed32 << 2) > workspaceSize)
6027+			return ERROR(GENERIC);
6028+		workspace = (U32 *)workspace + spaceUsed32;
6029+		workspaceSize -= (spaceUsed32 << 2);
6030+		{
6031+			size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
6032+			if (FSE_isError(headerSize))
6033+				return ERROR(corruption_detected);
6034+			if (tableLog > maxLog)
6035+				return ERROR(corruption_detected);
6036+			FSE_buildDTable_wksp(DTableSpace, norm, max, tableLog, workspace, workspaceSize);
6037+			*DTablePtr = DTableSpace;
6038+			return headerSize;
6039+		}
6040+	}
6041+	}
6042+}
6043+
6044+size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx *dctx, int *nbSeqPtr, const void *src, size_t srcSize)
6045+{
6046+	const BYTE *const istart = (const BYTE *const)src;
6047+	const BYTE *const iend = istart + srcSize;
6048+	const BYTE *ip = istart;
6049+
6050+	/* check */
6051+	if (srcSize < MIN_SEQUENCES_SIZE)
6052+		return ERROR(srcSize_wrong);
6053+
6054+	/* SeqHead */
6055+	{
6056+		int nbSeq = *ip++;
6057+		if (!nbSeq) {
6058+			*nbSeqPtr = 0;
6059+			return 1;
6060+		}
6061+		if (nbSeq > 0x7F) {
6062+			if (nbSeq == 0xFF) {
6063+				if (ip + 2 > iend)
6064+					return ERROR(srcSize_wrong);
6065+				nbSeq = ZSTD_readLE16(ip) + LONGNBSEQ, ip += 2;
6066+			} else {
6067+				if (ip >= iend)
6068+					return ERROR(srcSize_wrong);
6069+				nbSeq = ((nbSeq - 0x80) << 8) + *ip++;
6070+			}
6071+		}
6072+		*nbSeqPtr = nbSeq;
6073+	}
6074+
6075+	/* FSE table descriptors */
6076+	if (ip + 4 > iend)
6077+		return ERROR(srcSize_wrong); /* minimum possible size */
6078+	{
6079+		symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);
6080+		symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);
6081+		symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3);
6082+		ip++;
6083+
6084+		/* Build DTables */
6085+		{
6086+			size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr, LLtype, MaxLL, LLFSELog, ip, iend - ip,
6087+								  LL_defaultDTable, dctx->fseEntropy, dctx->entropy.workspace, sizeof(dctx->entropy.workspace));
6088+			if (ZSTD_isError(llhSize))
6089+				return ERROR(corruption_detected);
6090+			ip += llhSize;
6091+		}
6092+		{
6093+			size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr, OFtype, MaxOff, OffFSELog, ip, iend - ip,
6094+								  OF_defaultDTable, dctx->fseEntropy, dctx->entropy.workspace, sizeof(dctx->entropy.workspace));
6095+			if (ZSTD_isError(ofhSize))
6096+				return ERROR(corruption_detected);
6097+			ip += ofhSize;
6098+		}
6099+		{
6100+			size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr, MLtype, MaxML, MLFSELog, ip, iend - ip,
6101+								  ML_defaultDTable, dctx->fseEntropy, dctx->entropy.workspace, sizeof(dctx->entropy.workspace));
6102+			if (ZSTD_isError(mlhSize))
6103+				return ERROR(corruption_detected);
6104+			ip += mlhSize;
6105+		}
6106+	}
6107+
6108+	return ip - istart;
6109+}
6110+
6111+typedef struct {
6112+	size_t litLength;
6113+	size_t matchLength;
6114+	size_t offset;
6115+	const BYTE *match;
6116+} seq_t;
6117+
6118+typedef struct {
6119+	BIT_DStream_t DStream;
6120+	FSE_DState_t stateLL;
6121+	FSE_DState_t stateOffb;
6122+	FSE_DState_t stateML;
6123+	size_t prevOffset[ZSTD_REP_NUM];
6124+	const BYTE *base;
6125+	size_t pos;
6126+	uPtrDiff gotoDict;
6127+} seqState_t;
6128+
6129+FORCE_NOINLINE
6130+size_t ZSTD_execSequenceLast7(BYTE *op, BYTE *const oend, seq_t sequence, const BYTE **litPtr, const BYTE *const litLimit, const BYTE *const base,
6131+			      const BYTE *const vBase, const BYTE *const dictEnd)
6132+{
6133+	BYTE *const oLitEnd = op + sequence.litLength;
6134+	size_t const sequenceLength = sequence.litLength + sequence.matchLength;
6135+	BYTE *const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
6136+	BYTE *const oend_w = oend - WILDCOPY_OVERLENGTH;
6137+	const BYTE *const iLitEnd = *litPtr + sequence.litLength;
6138+	const BYTE *match = oLitEnd - sequence.offset;
6139+
6140+	/* check */
6141+	if (oMatchEnd > oend)
6142+		return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
6143+	if (iLitEnd > litLimit)
6144+		return ERROR(corruption_detected); /* over-read beyond lit buffer */
6145+	if (oLitEnd <= oend_w)
6146+		return ERROR(GENERIC); /* Precondition */
6147+
6148+	/* copy literals */
6149+	if (op < oend_w) {
6150+		ZSTD_wildcopy(op, *litPtr, oend_w - op);
6151+		*litPtr += oend_w - op;
6152+		op = oend_w;
6153+	}
6154+	while (op < oLitEnd)
6155+		*op++ = *(*litPtr)++;
6156+
6157+	/* copy Match */
6158+	if (sequence.offset > (size_t)(oLitEnd - base)) {
6159+		/* offset beyond prefix */
6160+		if (sequence.offset > (size_t)(oLitEnd - vBase))
6161+			return ERROR(corruption_detected);
6162+		match = dictEnd - (base - match);
6163+		if (match + sequence.matchLength <= dictEnd) {
6164+			memmove(oLitEnd, match, sequence.matchLength);
6165+			return sequenceLength;
6166+		}
6167+		/* span extDict & currPrefixSegment */
6168+		{
6169+			size_t const length1 = dictEnd - match;
6170+			memmove(oLitEnd, match, length1);
6171+			op = oLitEnd + length1;
6172+			sequence.matchLength -= length1;
6173+			match = base;
6174+		}
6175+	}
6176+	while (op < oMatchEnd)
6177+		*op++ = *match++;
6178+	return sequenceLength;
6179+}
6180+
6181+static seq_t ZSTD_decodeSequence(seqState_t *seqState)
6182+{
6183+	seq_t seq;
6184+
6185+	U32 const llCode = FSE_peekSymbol(&seqState->stateLL);
6186+	U32 const mlCode = FSE_peekSymbol(&seqState->stateML);
6187+	U32 const ofCode = FSE_peekSymbol(&seqState->stateOffb); /* <= maxOff, by table construction */
6188+
6189+	U32 const llBits = LL_bits[llCode];
6190+	U32 const mlBits = ML_bits[mlCode];
6191+	U32 const ofBits = ofCode;
6192+	U32 const totalBits = llBits + mlBits + ofBits;
6193+
6194+	static const U32 LL_base[MaxLL + 1] = {0,  1,  2,  3,  4,  5,  6,  7,  8,    9,     10,    11,    12,    13,     14,     15,     16,     18,
6195+					       20, 22, 24, 28, 32, 40, 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000};
6196+
6197+	static const U32 ML_base[MaxML + 1] = {3,  4,  5,  6,  7,  8,  9,  10,   11,    12,    13,    14,    15,     16,     17,     18,     19,     20,
6198+					       21, 22, 23, 24, 25, 26, 27, 28,   29,    30,    31,    32,    33,     34,     35,     37,     39,     41,
6199+					       43, 47, 51, 59, 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803, 0x1003, 0x2003, 0x4003, 0x8003, 0x10003};
6200+
6201+	static const U32 OF_base[MaxOff + 1] = {0,       1,	1,	5,	0xD,      0x1D,      0x3D,      0x7D,      0xFD,     0x1FD,
6202+						0x3FD,   0x7FD,    0xFFD,    0x1FFD,   0x3FFD,   0x7FFD,    0xFFFD,    0x1FFFD,   0x3FFFD,  0x7FFFD,
6203+						0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD, 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD};
6204+
6205+	/* sequence */
6206+	{
6207+		size_t offset;
6208+		if (!ofCode)
6209+			offset = 0;
6210+		else {
6211+			offset = OF_base[ofCode] + BIT_readBitsFast(&seqState->DStream, ofBits); /* <=  (ZSTD_WINDOWLOG_MAX-1) bits */
6212+			if (ZSTD_32bits())
6213+				BIT_reloadDStream(&seqState->DStream);
6214+		}
6215+
6216+		if (ofCode <= 1) {
6217+			offset += (llCode == 0);
6218+			if (offset) {
6219+				size_t temp = (offset == 3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
6220+				temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
6221+				if (offset != 1)
6222+					seqState->prevOffset[2] = seqState->prevOffset[1];
6223+				seqState->prevOffset[1] = seqState->prevOffset[0];
6224+				seqState->prevOffset[0] = offset = temp;
6225+			} else {
6226+				offset = seqState->prevOffset[0];
6227+			}
6228+		} else {
6229+			seqState->prevOffset[2] = seqState->prevOffset[1];
6230+			seqState->prevOffset[1] = seqState->prevOffset[0];
6231+			seqState->prevOffset[0] = offset;
6232+		}
6233+		seq.offset = offset;
6234+	}
6235+
6236+	seq.matchLength = ML_base[mlCode] + ((mlCode > 31) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <=  16 bits */
6237+	if (ZSTD_32bits() && (mlBits + llBits > 24))
6238+		BIT_reloadDStream(&seqState->DStream);
6239+
6240+	seq.litLength = LL_base[llCode] + ((llCode > 15) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <=  16 bits */
6241+	if (ZSTD_32bits() || (totalBits > 64 - 7 - (LLFSELog + MLFSELog + OffFSELog)))
6242+		BIT_reloadDStream(&seqState->DStream);
6243+
6244+	/* ANS state update */
6245+	FSE_updateState(&seqState->stateLL, &seqState->DStream); /* <=  9 bits */
6246+	FSE_updateState(&seqState->stateML, &seqState->DStream); /* <=  9 bits */
6247+	if (ZSTD_32bits())
6248+		BIT_reloadDStream(&seqState->DStream);		   /* <= 18 bits */
6249+	FSE_updateState(&seqState->stateOffb, &seqState->DStream); /* <=  8 bits */
6250+
6251+	seq.match = NULL;
6252+
6253+	return seq;
6254+}
6255+
6256+FORCE_INLINE
6257+size_t ZSTD_execSequence(BYTE *op, BYTE *const oend, seq_t sequence, const BYTE **litPtr, const BYTE *const litLimit, const BYTE *const base,
6258+			 const BYTE *const vBase, const BYTE *const dictEnd)
6259+{
6260+	BYTE *const oLitEnd = op + sequence.litLength;
6261+	size_t const sequenceLength = sequence.litLength + sequence.matchLength;
6262+	BYTE *const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
6263+	BYTE *const oend_w = oend - WILDCOPY_OVERLENGTH;
6264+	const BYTE *const iLitEnd = *litPtr + sequence.litLength;
6265+	const BYTE *match = oLitEnd - sequence.offset;
6266+
6267+	/* check */
6268+	if (oMatchEnd > oend)
6269+		return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
6270+	if (iLitEnd > litLimit)
6271+		return ERROR(corruption_detected); /* over-read beyond lit buffer */
6272+	if (oLitEnd > oend_w)
6273+		return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, base, vBase, dictEnd);
6274+
6275+	/* copy Literals */
6276+	ZSTD_copy8(op, *litPtr);
6277+	if (sequence.litLength > 8)
6278+		ZSTD_wildcopy(op + 8, (*litPtr) + 8,
6279+			      sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
6280+	op = oLitEnd;
6281+	*litPtr = iLitEnd; /* update for next sequence */
6282+
6283+	/* copy Match */
6284+	if (sequence.offset > (size_t)(oLitEnd - base)) {
6285+		/* offset beyond prefix */
6286+		if (sequence.offset > (size_t)(oLitEnd - vBase))
6287+			return ERROR(corruption_detected);
6288+		match = dictEnd + (match - base);
6289+		if (match + sequence.matchLength <= dictEnd) {
6290+			memmove(oLitEnd, match, sequence.matchLength);
6291+			return sequenceLength;
6292+		}
6293+		/* span extDict & currPrefixSegment */
6294+		{
6295+			size_t const length1 = dictEnd - match;
6296+			memmove(oLitEnd, match, length1);
6297+			op = oLitEnd + length1;
6298+			sequence.matchLength -= length1;
6299+			match = base;
6300+			if (op > oend_w || sequence.matchLength < MINMATCH) {
6301+				U32 i;
6302+				for (i = 0; i < sequence.matchLength; ++i)
6303+					op[i] = match[i];
6304+				return sequenceLength;
6305+			}
6306+		}
6307+	}
6308+	/* Requirement: op <= oend_w && sequence.matchLength >= MINMATCH */
6309+
6310+	/* match within prefix */
6311+	if (sequence.offset < 8) {
6312+		/* close range match, overlap */
6313+		static const U32 dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4};   /* added */
6314+		static const int dec64table[] = {8, 8, 8, 7, 8, 9, 10, 11}; /* subtracted */
6315+		int const sub2 = dec64table[sequence.offset];
6316+		op[0] = match[0];
6317+		op[1] = match[1];
6318+		op[2] = match[2];
6319+		op[3] = match[3];
6320+		match += dec32table[sequence.offset];
6321+		ZSTD_copy4(op + 4, match);
6322+		match -= sub2;
6323+	} else {
6324+		ZSTD_copy8(op, match);
6325+	}
6326+	op += 8;
6327+	match += 8;
6328+
6329+	if (oMatchEnd > oend - (16 - MINMATCH)) {
6330+		if (op < oend_w) {
6331+			ZSTD_wildcopy(op, match, oend_w - op);
6332+			match += oend_w - op;
6333+			op = oend_w;
6334+		}
6335+		while (op < oMatchEnd)
6336+			*op++ = *match++;
6337+	} else {
6338+		ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength - 8); /* works even if matchLength < 8 */
6339+	}
6340+	return sequenceLength;
6341+}
6342+
6343+static size_t ZSTD_decompressSequences(ZSTD_DCtx *dctx, void *dst, size_t maxDstSize, const void *seqStart, size_t seqSize)
6344+{
6345+	const BYTE *ip = (const BYTE *)seqStart;
6346+	const BYTE *const iend = ip + seqSize;
6347+	BYTE *const ostart = (BYTE * const)dst;
6348+	BYTE *const oend = ostart + maxDstSize;
6349+	BYTE *op = ostart;
6350+	const BYTE *litPtr = dctx->litPtr;
6351+	const BYTE *const litEnd = litPtr + dctx->litSize;
6352+	const BYTE *const base = (const BYTE *)(dctx->base);
6353+	const BYTE *const vBase = (const BYTE *)(dctx->vBase);
6354+	const BYTE *const dictEnd = (const BYTE *)(dctx->dictEnd);
6355+	int nbSeq;
6356+
6357+	/* Build Decoding Tables */
6358+	{
6359+		size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, seqSize);
6360+		if (ZSTD_isError(seqHSize))
6361+			return seqHSize;
6362+		ip += seqHSize;
6363+	}
6364+
6365+	/* Regen sequences */
6366+	if (nbSeq) {
6367+		seqState_t seqState;
6368+		dctx->fseEntropy = 1;
6369+		{
6370+			U32 i;
6371+			for (i = 0; i < ZSTD_REP_NUM; i++)
6372+				seqState.prevOffset[i] = dctx->entropy.rep[i];
6373+		}
6374+		CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend - ip), corruption_detected);
6375+		FSE_initDState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
6376+		FSE_initDState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
6377+		FSE_initDState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
6378+
6379+		for (; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq;) {
6380+			nbSeq--;
6381+			{
6382+				seq_t const sequence = ZSTD_decodeSequence(&seqState);
6383+				size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
6384+				if (ZSTD_isError(oneSeqSize))
6385+					return oneSeqSize;
6386+				op += oneSeqSize;
6387+			}
6388+		}
6389+
6390+		/* check if reached exact end */
6391+		if (nbSeq)
6392+			return ERROR(corruption_detected);
6393+		/* save reps for next block */
6394+		{
6395+			U32 i;
6396+			for (i = 0; i < ZSTD_REP_NUM; i++)
6397+				dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]);
6398+		}
6399+	}
6400+
6401+	/* last literal segment */
6402+	{
6403+		size_t const lastLLSize = litEnd - litPtr;
6404+		if (lastLLSize > (size_t)(oend - op))
6405+			return ERROR(dstSize_tooSmall);
6406+		memcpy(op, litPtr, lastLLSize);
6407+		op += lastLLSize;
6408+	}
6409+
6410+	return op - ostart;
6411+}
6412+
6413+FORCE_INLINE seq_t ZSTD_decodeSequenceLong_generic(seqState_t *seqState, int const longOffsets)
6414+{
6415+	seq_t seq;
6416+
6417+	U32 const llCode = FSE_peekSymbol(&seqState->stateLL);
6418+	U32 const mlCode = FSE_peekSymbol(&seqState->stateML);
6419+	U32 const ofCode = FSE_peekSymbol(&seqState->stateOffb); /* <= maxOff, by table construction */
6420+
6421+	U32 const llBits = LL_bits[llCode];
6422+	U32 const mlBits = ML_bits[mlCode];
6423+	U32 const ofBits = ofCode;
6424+	U32 const totalBits = llBits + mlBits + ofBits;
6425+
6426+	static const U32 LL_base[MaxLL + 1] = {0,  1,  2,  3,  4,  5,  6,  7,  8,    9,     10,    11,    12,    13,     14,     15,     16,     18,
6427+					       20, 22, 24, 28, 32, 40, 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000};
6428+
6429+	static const U32 ML_base[MaxML + 1] = {3,  4,  5,  6,  7,  8,  9,  10,   11,    12,    13,    14,    15,     16,     17,     18,     19,     20,
6430+					       21, 22, 23, 24, 25, 26, 27, 28,   29,    30,    31,    32,    33,     34,     35,     37,     39,     41,
6431+					       43, 47, 51, 59, 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803, 0x1003, 0x2003, 0x4003, 0x8003, 0x10003};
6432+
6433+	static const U32 OF_base[MaxOff + 1] = {0,       1,	1,	5,	0xD,      0x1D,      0x3D,      0x7D,      0xFD,     0x1FD,
6434+						0x3FD,   0x7FD,    0xFFD,    0x1FFD,   0x3FFD,   0x7FFD,    0xFFFD,    0x1FFFD,   0x3FFFD,  0x7FFFD,
6435+						0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD, 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD};
6436+
6437+	/* sequence */
6438+	{
6439+		size_t offset;
6440+		if (!ofCode)
6441+			offset = 0;
6442+		else {
6443+			if (longOffsets) {
6444+				int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN);
6445+				offset = OF_base[ofCode] + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);
6446+				if (ZSTD_32bits() || extraBits)
6447+					BIT_reloadDStream(&seqState->DStream);
6448+				if (extraBits)
6449+					offset += BIT_readBitsFast(&seqState->DStream, extraBits);
6450+			} else {
6451+				offset = OF_base[ofCode] + BIT_readBitsFast(&seqState->DStream, ofBits); /* <=  (ZSTD_WINDOWLOG_MAX-1) bits */
6452+				if (ZSTD_32bits())
6453+					BIT_reloadDStream(&seqState->DStream);
6454+			}
6455+		}
6456+
6457+		if (ofCode <= 1) {
6458+			offset += (llCode == 0);
6459+			if (offset) {
6460+				size_t temp = (offset == 3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
6461+				temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
6462+				if (offset != 1)
6463+					seqState->prevOffset[2] = seqState->prevOffset[1];
6464+				seqState->prevOffset[1] = seqState->prevOffset[0];
6465+				seqState->prevOffset[0] = offset = temp;
6466+			} else {
6467+				offset = seqState->prevOffset[0];
6468+			}
6469+		} else {
6470+			seqState->prevOffset[2] = seqState->prevOffset[1];
6471+			seqState->prevOffset[1] = seqState->prevOffset[0];
6472+			seqState->prevOffset[0] = offset;
6473+		}
6474+		seq.offset = offset;
6475+	}
6476+
6477+	seq.matchLength = ML_base[mlCode] + ((mlCode > 31) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <=  16 bits */
6478+	if (ZSTD_32bits() && (mlBits + llBits > 24))
6479+		BIT_reloadDStream(&seqState->DStream);
6480+
6481+	seq.litLength = LL_base[llCode] + ((llCode > 15) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <=  16 bits */
6482+	if (ZSTD_32bits() || (totalBits > 64 - 7 - (LLFSELog + MLFSELog + OffFSELog)))
6483+		BIT_reloadDStream(&seqState->DStream);
6484+
6485+	{
6486+		size_t const pos = seqState->pos + seq.litLength;
6487+		seq.match = seqState->base + pos - seq.offset; /* single memory segment */
6488+		if (seq.offset > pos)
6489+			seq.match += seqState->gotoDict; /* separate memory segment */
6490+		seqState->pos = pos + seq.matchLength;
6491+	}
6492+
6493+	/* ANS state update */
6494+	FSE_updateState(&seqState->stateLL, &seqState->DStream); /* <=  9 bits */
6495+	FSE_updateState(&seqState->stateML, &seqState->DStream); /* <=  9 bits */
6496+	if (ZSTD_32bits())
6497+		BIT_reloadDStream(&seqState->DStream);		   /* <= 18 bits */
6498+	FSE_updateState(&seqState->stateOffb, &seqState->DStream); /* <=  8 bits */
6499+
6500+	return seq;
6501+}
6502+
6503+static seq_t ZSTD_decodeSequenceLong(seqState_t *seqState, unsigned const windowSize)
6504+{
6505+	if (ZSTD_highbit32(windowSize) > STREAM_ACCUMULATOR_MIN) {
6506+		return ZSTD_decodeSequenceLong_generic(seqState, 1);
6507+	} else {
6508+		return ZSTD_decodeSequenceLong_generic(seqState, 0);
6509+	}
6510+}
6511+
6512+FORCE_INLINE
6513+size_t ZSTD_execSequenceLong(BYTE *op, BYTE *const oend, seq_t sequence, const BYTE **litPtr, const BYTE *const litLimit, const BYTE *const base,
6514+			     const BYTE *const vBase, const BYTE *const dictEnd)
6515+{
6516+	BYTE *const oLitEnd = op + sequence.litLength;
6517+	size_t const sequenceLength = sequence.litLength + sequence.matchLength;
6518+	BYTE *const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
6519+	BYTE *const oend_w = oend - WILDCOPY_OVERLENGTH;
6520+	const BYTE *const iLitEnd = *litPtr + sequence.litLength;
6521+	const BYTE *match = sequence.match;
6522+
6523+	/* check */
6524+	if (oMatchEnd > oend)
6525+		return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
6526+	if (iLitEnd > litLimit)
6527+		return ERROR(corruption_detected); /* over-read beyond lit buffer */
6528+	if (oLitEnd > oend_w)
6529+		return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, base, vBase, dictEnd);
6530+
6531+	/* copy Literals */
6532+	ZSTD_copy8(op, *litPtr);
6533+	if (sequence.litLength > 8)
6534+		ZSTD_wildcopy(op + 8, (*litPtr) + 8,
6535+			      sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
6536+	op = oLitEnd;
6537+	*litPtr = iLitEnd; /* update for next sequence */
6538+
6539+	/* copy Match */
6540+	if (sequence.offset > (size_t)(oLitEnd - base)) {
6541+		/* offset beyond prefix */
6542+		if (sequence.offset > (size_t)(oLitEnd - vBase))
6543+			return ERROR(corruption_detected);
6544+		if (match + sequence.matchLength <= dictEnd) {
6545+			memmove(oLitEnd, match, sequence.matchLength);
6546+			return sequenceLength;
6547+		}
6548+		/* span extDict & currPrefixSegment */
6549+		{
6550+			size_t const length1 = dictEnd - match;
6551+			memmove(oLitEnd, match, length1);
6552+			op = oLitEnd + length1;
6553+			sequence.matchLength -= length1;
6554+			match = base;
6555+			if (op > oend_w || sequence.matchLength < MINMATCH) {
6556+				U32 i;
6557+				for (i = 0; i < sequence.matchLength; ++i)
6558+					op[i] = match[i];
6559+				return sequenceLength;
6560+			}
6561+		}
6562+	}
6563+	/* Requirement: op <= oend_w && sequence.matchLength >= MINMATCH */
6564+
6565+	/* match within prefix */
6566+	if (sequence.offset < 8) {
6567+		/* close range match, overlap */
6568+		static const U32 dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4};   /* added */
6569+		static const int dec64table[] = {8, 8, 8, 7, 8, 9, 10, 11}; /* subtracted */
6570+		int const sub2 = dec64table[sequence.offset];
6571+		op[0] = match[0];
6572+		op[1] = match[1];
6573+		op[2] = match[2];
6574+		op[3] = match[3];
6575+		match += dec32table[sequence.offset];
6576+		ZSTD_copy4(op + 4, match);
6577+		match -= sub2;
6578+	} else {
6579+		ZSTD_copy8(op, match);
6580+	}
6581+	op += 8;
6582+	match += 8;
6583+
6584+	if (oMatchEnd > oend - (16 - MINMATCH)) {
6585+		if (op < oend_w) {
6586+			ZSTD_wildcopy(op, match, oend_w - op);
6587+			match += oend_w - op;
6588+			op = oend_w;
6589+		}
6590+		while (op < oMatchEnd)
6591+			*op++ = *match++;
6592+	} else {
6593+		ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength - 8); /* works even if matchLength < 8 */
6594+	}
6595+	return sequenceLength;
6596+}
6597+
6598+static size_t ZSTD_decompressSequencesLong(ZSTD_DCtx *dctx, void *dst, size_t maxDstSize, const void *seqStart, size_t seqSize)
6599+{
6600+	const BYTE *ip = (const BYTE *)seqStart;
6601+	const BYTE *const iend = ip + seqSize;
6602+	BYTE *const ostart = (BYTE * const)dst;
6603+	BYTE *const oend = ostart + maxDstSize;
6604+	BYTE *op = ostart;
6605+	const BYTE *litPtr = dctx->litPtr;
6606+	const BYTE *const litEnd = litPtr + dctx->litSize;
6607+	const BYTE *const base = (const BYTE *)(dctx->base);
6608+	const BYTE *const vBase = (const BYTE *)(dctx->vBase);
6609+	const BYTE *const dictEnd = (const BYTE *)(dctx->dictEnd);
6610+	unsigned const windowSize = dctx->fParams.windowSize;
6611+	int nbSeq;
6612+
6613+	/* Build Decoding Tables */
6614+	{
6615+		size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, seqSize);
6616+		if (ZSTD_isError(seqHSize))
6617+			return seqHSize;
6618+		ip += seqHSize;
6619+	}
6620+
6621+	/* Regen sequences */
6622+	if (nbSeq) {
6623+#define STORED_SEQS 4
6624+#define STOSEQ_MASK (STORED_SEQS - 1)
6625+#define ADVANCED_SEQS 4
6626+		seq_t *sequences = (seq_t *)dctx->entropy.workspace;
6627+		int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS);
6628+		seqState_t seqState;
6629+		int seqNb;
6630+		ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.workspace) >= sizeof(seq_t) * STORED_SEQS);
6631+		dctx->fseEntropy = 1;
6632+		{
6633+			U32 i;
6634+			for (i = 0; i < ZSTD_REP_NUM; i++)
6635+				seqState.prevOffset[i] = dctx->entropy.rep[i];
6636+		}
6637+		seqState.base = base;
6638+		seqState.pos = (size_t)(op - base);
6639+		seqState.gotoDict = (uPtrDiff)dictEnd - (uPtrDiff)base; /* cast to avoid undefined behaviour */
6640+		CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend - ip), corruption_detected);
6641+		FSE_initDState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
6642+		FSE_initDState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
6643+		FSE_initDState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
6644+
6645+		/* prepare in advance */
6646+		for (seqNb = 0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && seqNb < seqAdvance; seqNb++) {
6647+			sequences[seqNb] = ZSTD_decodeSequenceLong(&seqState, windowSize);
6648+		}
6649+		if (seqNb < seqAdvance)
6650+			return ERROR(corruption_detected);
6651+
6652+		/* decode and decompress */
6653+		for (; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && seqNb < nbSeq; seqNb++) {
6654+			seq_t const sequence = ZSTD_decodeSequenceLong(&seqState, windowSize);
6655+			size_t const oneSeqSize =
6656+			    ZSTD_execSequenceLong(op, oend, sequences[(seqNb - ADVANCED_SEQS) & STOSEQ_MASK], &litPtr, litEnd, base, vBase, dictEnd);
6657+			if (ZSTD_isError(oneSeqSize))
6658+				return oneSeqSize;
6659+			ZSTD_PREFETCH(sequence.match);
6660+			sequences[seqNb & STOSEQ_MASK] = sequence;
6661+			op += oneSeqSize;
6662+		}
6663+		if (seqNb < nbSeq)
6664+			return ERROR(corruption_detected);
6665+
6666+		/* finish queue */
6667+		seqNb -= seqAdvance;
6668+		for (; seqNb < nbSeq; seqNb++) {
6669+			size_t const oneSeqSize = ZSTD_execSequenceLong(op, oend, sequences[seqNb & STOSEQ_MASK], &litPtr, litEnd, base, vBase, dictEnd);
6670+			if (ZSTD_isError(oneSeqSize))
6671+				return oneSeqSize;
6672+			op += oneSeqSize;
6673+		}
6674+
6675+		/* save reps for next block */
6676+		{
6677+			U32 i;
6678+			for (i = 0; i < ZSTD_REP_NUM; i++)
6679+				dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]);
6680+		}
6681+	}
6682+
6683+	/* last literal segment */
6684+	{
6685+		size_t const lastLLSize = litEnd - litPtr;
6686+		if (lastLLSize > (size_t)(oend - op))
6687+			return ERROR(dstSize_tooSmall);
6688+		memcpy(op, litPtr, lastLLSize);
6689+		op += lastLLSize;
6690+	}
6691+
6692+	return op - ostart;
6693+}
6694+
6695+static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
6696+{ /* blockType == blockCompressed */
6697+	const BYTE *ip = (const BYTE *)src;
6698+
6699+	if (srcSize >= ZSTD_BLOCKSIZE_ABSOLUTEMAX)
6700+		return ERROR(srcSize_wrong);
6701+
6702+	/* Decode literals section */
6703+	{
6704+		size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
6705+		if (ZSTD_isError(litCSize))
6706+			return litCSize;
6707+		ip += litCSize;
6708+		srcSize -= litCSize;
6709+	}
6710+	if (sizeof(size_t) > 4) /* do not enable prefetching on 32-bits x86, as it's performance detrimental */
6711+				/* likely because of register pressure */
6712+				/* if that's the correct cause, then 32-bits ARM should be affected differently */
6713+				/* it would be good to test this on ARM real hardware, to see if prefetch version improves speed */
6714+		if (dctx->fParams.windowSize > (1 << 23))
6715+			return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize);
6716+	return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize);
6717+}
6718+
6719+static void ZSTD_checkContinuity(ZSTD_DCtx *dctx, const void *dst)
6720+{
6721+	if (dst != dctx->previousDstEnd) { /* not contiguous */
6722+		dctx->dictEnd = dctx->previousDstEnd;
6723+		dctx->vBase = (const char *)dst - ((const char *)(dctx->previousDstEnd) - (const char *)(dctx->base));
6724+		dctx->base = dst;
6725+		dctx->previousDstEnd = dst;
6726+	}
6727+}
6728+
6729+size_t ZSTD_decompressBlock(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
6730+{
6731+	size_t dSize;
6732+	ZSTD_checkContinuity(dctx, dst);
6733+	dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
6734+	dctx->previousDstEnd = (char *)dst + dSize;
6735+	return dSize;
6736+}
6737+
6738+/** ZSTD_insertBlock() :
6739+	insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
6740+size_t ZSTD_insertBlock(ZSTD_DCtx *dctx, const void *blockStart, size_t blockSize)
6741+{
6742+	ZSTD_checkContinuity(dctx, blockStart);
6743+	dctx->previousDstEnd = (const char *)blockStart + blockSize;
6744+	return blockSize;
6745+}
6746+
6747+size_t ZSTD_generateNxBytes(void *dst, size_t dstCapacity, BYTE byte, size_t length)
6748+{
6749+	if (length > dstCapacity)
6750+		return ERROR(dstSize_tooSmall);
6751+	memset(dst, byte, length);
6752+	return length;
6753+}
6754+
6755+/** ZSTD_findFrameCompressedSize() :
6756+ *  compatible with legacy mode
6757+ *  `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame
6758+ *  `srcSize` must be at least as large as the frame contained
6759+ *  @return : the compressed size of the frame starting at `src` */
6760+size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
6761+{
6762+	if (srcSize >= ZSTD_skippableHeaderSize && (ZSTD_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
6763+		return ZSTD_skippableHeaderSize + ZSTD_readLE32((const BYTE *)src + 4);
6764+	} else {
6765+		const BYTE *ip = (const BYTE *)src;
6766+		const BYTE *const ipstart = ip;
6767+		size_t remainingSize = srcSize;
6768+		ZSTD_frameParams fParams;
6769+
6770+		size_t const headerSize = ZSTD_frameHeaderSize(ip, remainingSize);
6771+		if (ZSTD_isError(headerSize))
6772+			return headerSize;
6773+
6774+		/* Frame Header */
6775+		{
6776+			size_t const ret = ZSTD_getFrameParams(&fParams, ip, remainingSize);
6777+			if (ZSTD_isError(ret))
6778+				return ret;
6779+			if (ret > 0)
6780+				return ERROR(srcSize_wrong);
6781+		}
6782+
6783+		ip += headerSize;
6784+		remainingSize -= headerSize;
6785+
6786+		/* Loop on each block */
6787+		while (1) {
6788+			blockProperties_t blockProperties;
6789+			size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
6790+			if (ZSTD_isError(cBlockSize))
6791+				return cBlockSize;
6792+
6793+			if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
6794+				return ERROR(srcSize_wrong);
6795+
6796+			ip += ZSTD_blockHeaderSize + cBlockSize;
6797+			remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
6798+
6799+			if (blockProperties.lastBlock)
6800+				break;
6801+		}
6802+
6803+		if (fParams.checksumFlag) { /* Frame content checksum */
6804+			if (remainingSize < 4)
6805+				return ERROR(srcSize_wrong);
6806+			ip += 4;
6807+			remainingSize -= 4;
6808+		}
6809+
6810+		return ip - ipstart;
6811+	}
6812+}
6813+
6814+/*! ZSTD_decompressFrame() :
6815+*   @dctx must be properly initialized */
6816+static size_t ZSTD_decompressFrame(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void **srcPtr, size_t *srcSizePtr)
6817+{
6818+	const BYTE *ip = (const BYTE *)(*srcPtr);
6819+	BYTE *const ostart = (BYTE * const)dst;
6820+	BYTE *const oend = ostart + dstCapacity;
6821+	BYTE *op = ostart;
6822+	size_t remainingSize = *srcSizePtr;
6823+
6824+	/* check */
6825+	if (remainingSize < ZSTD_frameHeaderSize_min + ZSTD_blockHeaderSize)
6826+		return ERROR(srcSize_wrong);
6827+
6828+	/* Frame Header */
6829+	{
6830+		size_t const frameHeaderSize = ZSTD_frameHeaderSize(ip, ZSTD_frameHeaderSize_prefix);
6831+		if (ZSTD_isError(frameHeaderSize))
6832+			return frameHeaderSize;
6833+		if (remainingSize < frameHeaderSize + ZSTD_blockHeaderSize)
6834+			return ERROR(srcSize_wrong);
6835+		CHECK_F(ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize));
6836+		ip += frameHeaderSize;
6837+		remainingSize -= frameHeaderSize;
6838+	}
6839+
6840+	/* Loop on each block */
6841+	while (1) {
6842+		size_t decodedSize;
6843+		blockProperties_t blockProperties;
6844+		size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
6845+		if (ZSTD_isError(cBlockSize))
6846+			return cBlockSize;
6847+
6848+		ip += ZSTD_blockHeaderSize;
6849+		remainingSize -= ZSTD_blockHeaderSize;
6850+		if (cBlockSize > remainingSize)
6851+			return ERROR(srcSize_wrong);
6852+
6853+		switch (blockProperties.blockType) {
6854+		case bt_compressed: decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend - op, ip, cBlockSize); break;
6855+		case bt_raw: decodedSize = ZSTD_copyRawBlock(op, oend - op, ip, cBlockSize); break;
6856+		case bt_rle: decodedSize = ZSTD_generateNxBytes(op, oend - op, *ip, blockProperties.origSize); break;
6857+		case bt_reserved:
6858+		default: return ERROR(corruption_detected);
6859+		}
6860+
6861+		if (ZSTD_isError(decodedSize))
6862+			return decodedSize;
6863+		if (dctx->fParams.checksumFlag)
6864+			xxh64_update(&dctx->xxhState, op, decodedSize);
6865+		op += decodedSize;
6866+		ip += cBlockSize;
6867+		remainingSize -= cBlockSize;
6868+		if (blockProperties.lastBlock)
6869+			break;
6870+	}
6871+
6872+	if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
6873+		U32 const checkCalc = (U32)xxh64_digest(&dctx->xxhState);
6874+		U32 checkRead;
6875+		if (remainingSize < 4)
6876+			return ERROR(checksum_wrong);
6877+		checkRead = ZSTD_readLE32(ip);
6878+		if (checkRead != checkCalc)
6879+			return ERROR(checksum_wrong);
6880+		ip += 4;
6881+		remainingSize -= 4;
6882+	}
6883+
6884+	/* Allow caller to get size read */
6885+	*srcPtr = ip;
6886+	*srcSizePtr = remainingSize;
6887+	return op - ostart;
6888+}
6889+
6890+static const void *ZSTD_DDictDictContent(const ZSTD_DDict *ddict);
6891+static size_t ZSTD_DDictDictSize(const ZSTD_DDict *ddict);
6892+
6893+static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const void *dict, size_t dictSize,
6894+					const ZSTD_DDict *ddict)
6895+{
6896+	void *const dststart = dst;
6897+
6898+	if (ddict) {
6899+		if (dict) {
6900+			/* programmer error, these two cases should be mutually exclusive */
6901+			return ERROR(GENERIC);
6902+		}
6903+
6904+		dict = ZSTD_DDictDictContent(ddict);
6905+		dictSize = ZSTD_DDictDictSize(ddict);
6906+	}
6907+
6908+	while (srcSize >= ZSTD_frameHeaderSize_prefix) {
6909+		U32 magicNumber;
6910+
6911+		magicNumber = ZSTD_readLE32(src);
6912+		if (magicNumber != ZSTD_MAGICNUMBER) {
6913+			if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
6914+				size_t skippableSize;
6915+				if (srcSize < ZSTD_skippableHeaderSize)
6916+					return ERROR(srcSize_wrong);
6917+				skippableSize = ZSTD_readLE32((const BYTE *)src + 4) + ZSTD_skippableHeaderSize;
6918+				if (srcSize < skippableSize) {
6919+					return ERROR(srcSize_wrong);
6920+				}
6921+
6922+				src = (const BYTE *)src + skippableSize;
6923+				srcSize -= skippableSize;
6924+				continue;
6925+			} else {
6926+				return ERROR(prefix_unknown);
6927+			}
6928+		}
6929+
6930+		if (ddict) {
6931+			/* we were called from ZSTD_decompress_usingDDict */
6932+			ZSTD_refDDict(dctx, ddict);
6933+		} else {
6934+			/* this will initialize correctly with no dict if dict == NULL, so
6935+			 * use this in all cases but ddict */
6936+			CHECK_F(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize));
6937+		}
6938+		ZSTD_checkContinuity(dctx, dst);
6939+
6940+		{
6941+			const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity, &src, &srcSize);
6942+			if (ZSTD_isError(res))
6943+				return res;
6944+			/* don't need to bounds check this, ZSTD_decompressFrame will have
6945+			 * already */
6946+			dst = (BYTE *)dst + res;
6947+			dstCapacity -= res;
6948+		}
6949+	}
6950+
6951+	if (srcSize)
6952+		return ERROR(srcSize_wrong); /* input not entirely consumed */
6953+
6954+	return (BYTE *)dst - (BYTE *)dststart;
6955+}
6956+
6957+size_t ZSTD_decompress_usingDict(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const void *dict, size_t dictSize)
6958+{
6959+	return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
6960+}
6961+
6962+size_t ZSTD_decompressDCtx(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
6963+{
6964+	return ZSTD_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0);
6965+}
6966+
6967+/*-**************************************
6968+*   Advanced Streaming Decompression API
6969+*   Bufferless and synchronous
6970+****************************************/
6971+size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx *dctx) { return dctx->expected; }
6972+
6973+ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx *dctx)
6974+{
6975+	switch (dctx->stage) {
6976+	default: /* should not happen */
6977+	case ZSTDds_getFrameHeaderSize:
6978+	case ZSTDds_decodeFrameHeader: return ZSTDnit_frameHeader;
6979+	case ZSTDds_decodeBlockHeader: return ZSTDnit_blockHeader;
6980+	case ZSTDds_decompressBlock: return ZSTDnit_block;
6981+	case ZSTDds_decompressLastBlock: return ZSTDnit_lastBlock;
6982+	case ZSTDds_checkChecksum: return ZSTDnit_checksum;
6983+	case ZSTDds_decodeSkippableHeader:
6984+	case ZSTDds_skipFrame: return ZSTDnit_skippableFrame;
6985+	}
6986+}
6987+
6988+int ZSTD_isSkipFrame(ZSTD_DCtx *dctx) { return dctx->stage == ZSTDds_skipFrame; } /* for zbuff */
6989+
6990+/** ZSTD_decompressContinue() :
6991+*   @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
6992+*             or an error code, which can be tested using ZSTD_isError() */
6993+size_t ZSTD_decompressContinue(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize)
6994+{
6995+	/* Sanity check */
6996+	if (srcSize != dctx->expected)
6997+		return ERROR(srcSize_wrong);
6998+	if (dstCapacity)
6999+		ZSTD_checkContinuity(dctx, dst);
7000+
7001+	switch (dctx->stage) {
7002+	case ZSTDds_getFrameHeaderSize:
7003+		if (srcSize != ZSTD_frameHeaderSize_prefix)
7004+			return ERROR(srcSize_wrong);					/* impossible */
7005+		if ((ZSTD_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
7006+			memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_prefix);
7007+			dctx->expected = ZSTD_skippableHeaderSize - ZSTD_frameHeaderSize_prefix; /* magic number + skippable frame length */
7008+			dctx->stage = ZSTDds_decodeSkippableHeader;
7009+			return 0;
7010+		}
7011+		dctx->headerSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_prefix);
7012+		if (ZSTD_isError(dctx->headerSize))
7013+			return dctx->headerSize;
7014+		memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_prefix);
7015+		if (dctx->headerSize > ZSTD_frameHeaderSize_prefix) {
7016+			dctx->expected = dctx->headerSize - ZSTD_frameHeaderSize_prefix;
7017+			dctx->stage = ZSTDds_decodeFrameHeader;
7018+			return 0;
7019+		}
7020+		dctx->expected = 0; /* not necessary to copy more */
7021+
7022+	case ZSTDds_decodeFrameHeader:
7023+		memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_prefix, src, dctx->expected);
7024+		CHECK_F(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize));
7025+		dctx->expected = ZSTD_blockHeaderSize;
7026+		dctx->stage = ZSTDds_decodeBlockHeader;
7027+		return 0;
7028+
7029+	case ZSTDds_decodeBlockHeader: {
7030+		blockProperties_t bp;
7031+		size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
7032+		if (ZSTD_isError(cBlockSize))
7033+			return cBlockSize;
7034+		dctx->expected = cBlockSize;
7035+		dctx->bType = bp.blockType;
7036+		dctx->rleSize = bp.origSize;
7037+		if (cBlockSize) {
7038+			dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
7039+			return 0;
7040+		}
7041+		/* empty block */
7042+		if (bp.lastBlock) {
7043+			if (dctx->fParams.checksumFlag) {
7044+				dctx->expected = 4;
7045+				dctx->stage = ZSTDds_checkChecksum;
7046+			} else {
7047+				dctx->expected = 0; /* end of frame */
7048+				dctx->stage = ZSTDds_getFrameHeaderSize;
7049+			}
7050+		} else {
7051+			dctx->expected = 3; /* go directly to next header */
7052+			dctx->stage = ZSTDds_decodeBlockHeader;
7053+		}
7054+		return 0;
7055+	}
7056+	case ZSTDds_decompressLastBlock:
7057+	case ZSTDds_decompressBlock: {
7058+		size_t rSize;
7059+		switch (dctx->bType) {
7060+		case bt_compressed: rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize); break;
7061+		case bt_raw: rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize); break;
7062+		case bt_rle: rSize = ZSTD_setRleBlock(dst, dstCapacity, src, srcSize, dctx->rleSize); break;
7063+		case bt_reserved: /* should never happen */
7064+		default: return ERROR(corruption_detected);
7065+		}
7066+		if (ZSTD_isError(rSize))
7067+			return rSize;
7068+		if (dctx->fParams.checksumFlag)
7069+			xxh64_update(&dctx->xxhState, dst, rSize);
7070+
7071+		if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */
7072+			if (dctx->fParams.checksumFlag) {	/* another round for frame checksum */
7073+				dctx->expected = 4;
7074+				dctx->stage = ZSTDds_checkChecksum;
7075+			} else {
7076+				dctx->expected = 0; /* ends here */
7077+				dctx->stage = ZSTDds_getFrameHeaderSize;
7078+			}
7079+		} else {
7080+			dctx->stage = ZSTDds_decodeBlockHeader;
7081+			dctx->expected = ZSTD_blockHeaderSize;
7082+			dctx->previousDstEnd = (char *)dst + rSize;
7083+		}
7084+		return rSize;
7085+	}
7086+	case ZSTDds_checkChecksum: {
7087+		U32 const h32 = (U32)xxh64_digest(&dctx->xxhState);
7088+		U32 const check32 = ZSTD_readLE32(src); /* srcSize == 4, guaranteed by dctx->expected */
7089+		if (check32 != h32)
7090+			return ERROR(checksum_wrong);
7091+		dctx->expected = 0;
7092+		dctx->stage = ZSTDds_getFrameHeaderSize;
7093+		return 0;
7094+	}
7095+	case ZSTDds_decodeSkippableHeader: {
7096+		memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_prefix, src, dctx->expected);
7097+		dctx->expected = ZSTD_readLE32(dctx->headerBuffer + 4);
7098+		dctx->stage = ZSTDds_skipFrame;
7099+		return 0;
7100+	}
7101+	case ZSTDds_skipFrame: {
7102+		dctx->expected = 0;
7103+		dctx->stage = ZSTDds_getFrameHeaderSize;
7104+		return 0;
7105+	}
7106+	default:
7107+		return ERROR(GENERIC); /* impossible */
7108+	}
7109+}
7110+
7111+static size_t ZSTD_refDictContent(ZSTD_DCtx *dctx, const void *dict, size_t dictSize)
7112+{
7113+	dctx->dictEnd = dctx->previousDstEnd;
7114+	dctx->vBase = (const char *)dict - ((const char *)(dctx->previousDstEnd) - (const char *)(dctx->base));
7115+	dctx->base = dict;
7116+	dctx->previousDstEnd = (const char *)dict + dictSize;
7117+	return 0;
7118+}
7119+
7120+/* ZSTD_loadEntropy() :
7121+ * dict : must point at beginning of a valid zstd dictionary
7122+ * @return : size of entropy tables read */
7123+static size_t ZSTD_loadEntropy(ZSTD_entropyTables_t *entropy, const void *const dict, size_t const dictSize)
7124+{
7125+	const BYTE *dictPtr = (const BYTE *)dict;
7126+	const BYTE *const dictEnd = dictPtr + dictSize;
7127+
7128+	if (dictSize <= 8)
7129+		return ERROR(dictionary_corrupted);
7130+	dictPtr += 8; /* skip header = magic + dictID */
7131+
7132+	{
7133+		size_t const hSize = HUF_readDTableX4_wksp(entropy->hufTable, dictPtr, dictEnd - dictPtr, entropy->workspace, sizeof(entropy->workspace));
7134+		if (HUF_isError(hSize))
7135+			return ERROR(dictionary_corrupted);
7136+		dictPtr += hSize;
7137+	}
7138+
7139+	{
7140+		short offcodeNCount[MaxOff + 1];
7141+		U32 offcodeMaxValue = MaxOff, offcodeLog;
7142+		size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd - dictPtr);
7143+		if (FSE_isError(offcodeHeaderSize))
7144+			return ERROR(dictionary_corrupted);
7145+		if (offcodeLog > OffFSELog)
7146+			return ERROR(dictionary_corrupted);
7147+		CHECK_E(FSE_buildDTable_wksp(entropy->OFTable, offcodeNCount, offcodeMaxValue, offcodeLog, entropy->workspace, sizeof(entropy->workspace)), dictionary_corrupted);
7148+		dictPtr += offcodeHeaderSize;
7149+	}
7150+
7151+	{
7152+		short matchlengthNCount[MaxML + 1];
7153+		unsigned matchlengthMaxValue = MaxML, matchlengthLog;
7154+		size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd - dictPtr);
7155+		if (FSE_isError(matchlengthHeaderSize))
7156+			return ERROR(dictionary_corrupted);
7157+		if (matchlengthLog > MLFSELog)
7158+			return ERROR(dictionary_corrupted);
7159+		CHECK_E(FSE_buildDTable_wksp(entropy->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, entropy->workspace, sizeof(entropy->workspace)), dictionary_corrupted);
7160+		dictPtr += matchlengthHeaderSize;
7161+	}
7162+
7163+	{
7164+		short litlengthNCount[MaxLL + 1];
7165+		unsigned litlengthMaxValue = MaxLL, litlengthLog;
7166+		size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd - dictPtr);
7167+		if (FSE_isError(litlengthHeaderSize))
7168+			return ERROR(dictionary_corrupted);
7169+		if (litlengthLog > LLFSELog)
7170+			return ERROR(dictionary_corrupted);
7171+		CHECK_E(FSE_buildDTable_wksp(entropy->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog, entropy->workspace, sizeof(entropy->workspace)), dictionary_corrupted);
7172+		dictPtr += litlengthHeaderSize;
7173+	}
7174+
7175+	if (dictPtr + 12 > dictEnd)
7176+		return ERROR(dictionary_corrupted);
7177+	{
7178+		int i;
7179+		size_t const dictContentSize = (size_t)(dictEnd - (dictPtr + 12));
7180+		for (i = 0; i < 3; i++) {
7181+			U32 const rep = ZSTD_readLE32(dictPtr);
7182+			dictPtr += 4;
7183+			if (rep == 0 || rep >= dictContentSize)
7184+				return ERROR(dictionary_corrupted);
7185+			entropy->rep[i] = rep;
7186+		}
7187+	}
7188+
7189+	return dictPtr - (const BYTE *)dict;
7190+}
7191+
7192+static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx *dctx, const void *dict, size_t dictSize)
7193+{
7194+	if (dictSize < 8)
7195+		return ZSTD_refDictContent(dctx, dict, dictSize);
7196+	{
7197+		U32 const magic = ZSTD_readLE32(dict);
7198+		if (magic != ZSTD_DICT_MAGIC) {
7199+			return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
7200+		}
7201+	}
7202+	dctx->dictID = ZSTD_readLE32((const char *)dict + 4);
7203+
7204+	/* load entropy tables */
7205+	{
7206+		size_t const eSize = ZSTD_loadEntropy(&dctx->entropy, dict, dictSize);
7207+		if (ZSTD_isError(eSize))
7208+			return ERROR(dictionary_corrupted);
7209+		dict = (const char *)dict + eSize;
7210+		dictSize -= eSize;
7211+	}
7212+	dctx->litEntropy = dctx->fseEntropy = 1;
7213+
7214+	/* reference dictionary content */
7215+	return ZSTD_refDictContent(dctx, dict, dictSize);
7216+}
7217+
7218+size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx *dctx, const void *dict, size_t dictSize)
7219+{
7220+	CHECK_F(ZSTD_decompressBegin(dctx));
7221+	if (dict && dictSize)
7222+		CHECK_E(ZSTD_decompress_insertDictionary(dctx, dict, dictSize), dictionary_corrupted);
7223+	return 0;
7224+}
7225+
7226+/* ======   ZSTD_DDict   ====== */
7227+
7228+struct ZSTD_DDict_s {
7229+	void *dictBuffer;
7230+	const void *dictContent;
7231+	size_t dictSize;
7232+	ZSTD_entropyTables_t entropy;
7233+	U32 dictID;
7234+	U32 entropyPresent;
7235+	ZSTD_customMem cMem;
7236+}; /* typedef'd to ZSTD_DDict within "zstd.h" */
7237+
7238+size_t ZSTD_DDictWorkspaceBound(void) { return ZSTD_ALIGN(sizeof(ZSTD_stack)) + ZSTD_ALIGN(sizeof(ZSTD_DDict)); }
7239+
7240+static const void *ZSTD_DDictDictContent(const ZSTD_DDict *ddict) { return ddict->dictContent; }
7241+
7242+static size_t ZSTD_DDictDictSize(const ZSTD_DDict *ddict) { return ddict->dictSize; }
7243+
7244+static void ZSTD_refDDict(ZSTD_DCtx *dstDCtx, const ZSTD_DDict *ddict)
7245+{
7246+	ZSTD_decompressBegin(dstDCtx); /* init */
7247+	if (ddict) {		       /* support refDDict on NULL */
7248+		dstDCtx->dictID = ddict->dictID;
7249+		dstDCtx->base = ddict->dictContent;
7250+		dstDCtx->vBase = ddict->dictContent;
7251+		dstDCtx->dictEnd = (const BYTE *)ddict->dictContent + ddict->dictSize;
7252+		dstDCtx->previousDstEnd = dstDCtx->dictEnd;
7253+		if (ddict->entropyPresent) {
7254+			dstDCtx->litEntropy = 1;
7255+			dstDCtx->fseEntropy = 1;
7256+			dstDCtx->LLTptr = ddict->entropy.LLTable;
7257+			dstDCtx->MLTptr = ddict->entropy.MLTable;
7258+			dstDCtx->OFTptr = ddict->entropy.OFTable;
7259+			dstDCtx->HUFptr = ddict->entropy.hufTable;
7260+			dstDCtx->entropy.rep[0] = ddict->entropy.rep[0];
7261+			dstDCtx->entropy.rep[1] = ddict->entropy.rep[1];
7262+			dstDCtx->entropy.rep[2] = ddict->entropy.rep[2];
7263+		} else {
7264+			dstDCtx->litEntropy = 0;
7265+			dstDCtx->fseEntropy = 0;
7266+		}
7267+	}
7268+}
7269+
7270+static size_t ZSTD_loadEntropy_inDDict(ZSTD_DDict *ddict)
7271+{
7272+	ddict->dictID = 0;
7273+	ddict->entropyPresent = 0;
7274+	if (ddict->dictSize < 8)
7275+		return 0;
7276+	{
7277+		U32 const magic = ZSTD_readLE32(ddict->dictContent);
7278+		if (magic != ZSTD_DICT_MAGIC)
7279+			return 0; /* pure content mode */
7280+	}
7281+	ddict->dictID = ZSTD_readLE32((const char *)ddict->dictContent + 4);
7282+
7283+	/* load entropy tables */
7284+	CHECK_E(ZSTD_loadEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize), dictionary_corrupted);
7285+	ddict->entropyPresent = 1;
7286+	return 0;
7287+}
7288+
7289+static ZSTD_DDict *ZSTD_createDDict_advanced(const void *dict, size_t dictSize, unsigned byReference, ZSTD_customMem customMem)
7290+{
7291+	if (!customMem.customAlloc || !customMem.customFree)
7292+		return NULL;
7293+
7294+	{
7295+		ZSTD_DDict *const ddict = (ZSTD_DDict *)ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
7296+		if (!ddict)
7297+			return NULL;
7298+		ddict->cMem = customMem;
7299+
7300+		if ((byReference) || (!dict) || (!dictSize)) {
7301+			ddict->dictBuffer = NULL;
7302+			ddict->dictContent = dict;
7303+		} else {
7304+			void *const internalBuffer = ZSTD_malloc(dictSize, customMem);
7305+			if (!internalBuffer) {
7306+				ZSTD_freeDDict(ddict);
7307+				return NULL;
7308+			}
7309+			memcpy(internalBuffer, dict, dictSize);
7310+			ddict->dictBuffer = internalBuffer;
7311+			ddict->dictContent = internalBuffer;
7312+		}
7313+		ddict->dictSize = dictSize;
7314+		ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
7315+		/* parse dictionary content */
7316+		{
7317+			size_t const errorCode = ZSTD_loadEntropy_inDDict(ddict);
7318+			if (ZSTD_isError(errorCode)) {
7319+				ZSTD_freeDDict(ddict);
7320+				return NULL;
7321+			}
7322+		}
7323+
7324+		return ddict;
7325+	}
7326+}
7327+
7328+/*! ZSTD_initDDict() :
7329+*   Create a digested dictionary, to start decompression without startup delay.
7330+*   `dict` content is copied inside DDict.
7331+*   Consequently, `dict` can be released after `ZSTD_DDict` creation */
7332+ZSTD_DDict *ZSTD_initDDict(const void *dict, size_t dictSize, void *workspace, size_t workspaceSize)
7333+{
7334+	ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
7335+	return ZSTD_createDDict_advanced(dict, dictSize, 1, stackMem);
7336+}
7337+
7338+size_t ZSTD_freeDDict(ZSTD_DDict *ddict)
7339+{
7340+	if (ddict == NULL)
7341+		return 0; /* support free on NULL */
7342+	{
7343+		ZSTD_customMem const cMem = ddict->cMem;
7344+		ZSTD_free(ddict->dictBuffer, cMem);
7345+		ZSTD_free(ddict, cMem);
7346+		return 0;
7347+	}
7348+}
7349+
7350+/*! ZSTD_getDictID_fromDict() :
7351+ *  Provides the dictID stored within dictionary.
7352+ *  if @return == 0, the dictionary is not conformant with Zstandard specification.
7353+ *  It can still be loaded, but as a content-only dictionary. */
7354+unsigned ZSTD_getDictID_fromDict(const void *dict, size_t dictSize)
7355+{
7356+	if (dictSize < 8)
7357+		return 0;
7358+	if (ZSTD_readLE32(dict) != ZSTD_DICT_MAGIC)
7359+		return 0;
7360+	return ZSTD_readLE32((const char *)dict + 4);
7361+}
7362+
7363+/*! ZSTD_getDictID_fromDDict() :
7364+ *  Provides the dictID of the dictionary loaded into `ddict`.
7365+ *  If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
7366+ *  Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
7367+unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict *ddict)
7368+{
7369+	if (ddict == NULL)
7370+		return 0;
7371+	return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize);
7372+}
7373+
7374+/*! ZSTD_getDictID_fromFrame() :
7375+ *  Provides the dictID required to decompressed the frame stored within `src`.
7376+ *  If @return == 0, the dictID could not be decoded.
7377+ *  This could for one of the following reasons :
7378+ *  - The frame does not require a dictionary to be decoded (most common case).
7379+ *  - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
7380+ *    Note : this use case also happens when using a non-conformant dictionary.
7381+ *  - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
7382+ *  - This is not a Zstandard frame.
7383+ *  When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code. */
7384+unsigned ZSTD_getDictID_fromFrame(const void *src, size_t srcSize)
7385+{
7386+	ZSTD_frameParams zfp = {0, 0, 0, 0};
7387+	size_t const hError = ZSTD_getFrameParams(&zfp, src, srcSize);
7388+	if (ZSTD_isError(hError))
7389+		return 0;
7390+	return zfp.dictID;
7391+}
7392+
7393+/*! ZSTD_decompress_usingDDict() :
7394+*   Decompression using a pre-digested Dictionary
7395+*   Use dictionary without significant overhead. */
7396+size_t ZSTD_decompress_usingDDict(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity, const void *src, size_t srcSize, const ZSTD_DDict *ddict)
7397+{
7398+	/* pass content and size in case legacy frames are encountered */
7399+	return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, NULL, 0, ddict);
7400+}
7401+
7402+/*=====================================
7403+*   Streaming decompression
7404+*====================================*/
7405+
7406+typedef enum { zdss_init, zdss_loadHeader, zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;
7407+
7408+/* *** Resource management *** */
7409+struct ZSTD_DStream_s {
7410+	ZSTD_DCtx *dctx;
7411+	ZSTD_DDict *ddictLocal;
7412+	const ZSTD_DDict *ddict;
7413+	ZSTD_frameParams fParams;
7414+	ZSTD_dStreamStage stage;
7415+	char *inBuff;
7416+	size_t inBuffSize;
7417+	size_t inPos;
7418+	size_t maxWindowSize;
7419+	char *outBuff;
7420+	size_t outBuffSize;
7421+	size_t outStart;
7422+	size_t outEnd;
7423+	size_t blockSize;
7424+	BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; /* tmp buffer to store frame header */
7425+	size_t lhSize;
7426+	ZSTD_customMem customMem;
7427+	void *legacyContext;
7428+	U32 previousLegacyVersion;
7429+	U32 legacyVersion;
7430+	U32 hostageByte;
7431+}; /* typedef'd to ZSTD_DStream within "zstd.h" */
7432+
7433+size_t ZSTD_DStreamWorkspaceBound(size_t maxWindowSize)
7434+{
7435+	size_t const blockSize = MIN(maxWindowSize, ZSTD_BLOCKSIZE_ABSOLUTEMAX);
7436+	size_t const inBuffSize = blockSize;
7437+	size_t const outBuffSize = maxWindowSize + blockSize + WILDCOPY_OVERLENGTH * 2;
7438+	return ZSTD_DCtxWorkspaceBound() + ZSTD_ALIGN(sizeof(ZSTD_DStream)) + ZSTD_ALIGN(inBuffSize) + ZSTD_ALIGN(outBuffSize);
7439+}
7440+
7441+static ZSTD_DStream *ZSTD_createDStream_advanced(ZSTD_customMem customMem)
7442+{
7443+	ZSTD_DStream *zds;
7444+
7445+	if (!customMem.customAlloc || !customMem.customFree)
7446+		return NULL;
7447+
7448+	zds = (ZSTD_DStream *)ZSTD_malloc(sizeof(ZSTD_DStream), customMem);
7449+	if (zds == NULL)
7450+		return NULL;
7451+	memset(zds, 0, sizeof(ZSTD_DStream));
7452+	memcpy(&zds->customMem, &customMem, sizeof(ZSTD_customMem));
7453+	zds->dctx = ZSTD_createDCtx_advanced(customMem);
7454+	if (zds->dctx == NULL) {
7455+		ZSTD_freeDStream(zds);
7456+		return NULL;
7457+	}
7458+	zds->stage = zdss_init;
7459+	zds->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
7460+	return zds;
7461+}
7462+
7463+ZSTD_DStream *ZSTD_initDStream(size_t maxWindowSize, void *workspace, size_t workspaceSize)
7464+{
7465+	ZSTD_customMem const stackMem = ZSTD_initStack(workspace, workspaceSize);
7466+	ZSTD_DStream *zds = ZSTD_createDStream_advanced(stackMem);
7467+	if (!zds) {
7468+		return NULL;
7469+	}
7470+
7471+	zds->maxWindowSize = maxWindowSize;
7472+	zds->stage = zdss_loadHeader;
7473+	zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
7474+	ZSTD_freeDDict(zds->ddictLocal);
7475+	zds->ddictLocal = NULL;
7476+	zds->ddict = zds->ddictLocal;
7477+	zds->legacyVersion = 0;
7478+	zds->hostageByte = 0;
7479+
7480+	{
7481+		size_t const blockSize = MIN(zds->maxWindowSize, ZSTD_BLOCKSIZE_ABSOLUTEMAX);
7482+		size_t const neededOutSize = zds->maxWindowSize + blockSize + WILDCOPY_OVERLENGTH * 2;
7483+
7484+		zds->inBuff = (char *)ZSTD_malloc(blockSize, zds->customMem);
7485+		zds->inBuffSize = blockSize;
7486+		zds->outBuff = (char *)ZSTD_malloc(neededOutSize, zds->customMem);
7487+		zds->outBuffSize = neededOutSize;
7488+		if (zds->inBuff == NULL || zds->outBuff == NULL) {
7489+			ZSTD_freeDStream(zds);
7490+			return NULL;
7491+		}
7492+	}
7493+	return zds;
7494+}
7495+
7496+ZSTD_DStream *ZSTD_initDStream_usingDDict(size_t maxWindowSize, const ZSTD_DDict *ddict, void *workspace, size_t workspaceSize)
7497+{
7498+	ZSTD_DStream *zds = ZSTD_initDStream(maxWindowSize, workspace, workspaceSize);
7499+	if (zds) {
7500+		zds->ddict = ddict;
7501+	}
7502+	return zds;
7503+}
7504+
7505+size_t ZSTD_freeDStream(ZSTD_DStream *zds)
7506+{
7507+	if (zds == NULL)
7508+		return 0; /* support free on null */
7509+	{
7510+		ZSTD_customMem const cMem = zds->customMem;
7511+		ZSTD_freeDCtx(zds->dctx);
7512+		zds->dctx = NULL;
7513+		ZSTD_freeDDict(zds->ddictLocal);
7514+		zds->ddictLocal = NULL;
7515+		ZSTD_free(zds->inBuff, cMem);
7516+		zds->inBuff = NULL;
7517+		ZSTD_free(zds->outBuff, cMem);
7518+		zds->outBuff = NULL;
7519+		ZSTD_free(zds, cMem);
7520+		return 0;
7521+	}
7522+}
7523+
7524+/* *** Initialization *** */
7525+
7526+size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX + ZSTD_blockHeaderSize; }
7527+size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; }
7528+
7529+size_t ZSTD_resetDStream(ZSTD_DStream *zds)
7530+{
7531+	zds->stage = zdss_loadHeader;
7532+	zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
7533+	zds->legacyVersion = 0;
7534+	zds->hostageByte = 0;
7535+	return ZSTD_frameHeaderSize_prefix;
7536+}
7537+
7538+/* *****   Decompression   ***** */
7539+
7540+ZSTD_STATIC size_t ZSTD_limitCopy(void *dst, size_t dstCapacity, const void *src, size_t srcSize)
7541+{
7542+	size_t const length = MIN(dstCapacity, srcSize);
7543+	memcpy(dst, src, length);
7544+	return length;
7545+}
7546+
7547+size_t ZSTD_decompressStream(ZSTD_DStream *zds, ZSTD_outBuffer *output, ZSTD_inBuffer *input)
7548+{
7549+	const char *const istart = (const char *)(input->src) + input->pos;
7550+	const char *const iend = (const char *)(input->src) + input->size;
7551+	const char *ip = istart;
7552+	char *const ostart = (char *)(output->dst) + output->pos;
7553+	char *const oend = (char *)(output->dst) + output->size;
7554+	char *op = ostart;
7555+	U32 someMoreWork = 1;
7556+
7557+	while (someMoreWork) {
7558+		switch (zds->stage) {
7559+		case zdss_init:
7560+			ZSTD_resetDStream(zds); /* transparent reset on starting decoding a new frame */
7561+						/* fall-through */
7562+
7563+		case zdss_loadHeader: {
7564+			size_t const hSize = ZSTD_getFrameParams(&zds->fParams, zds->headerBuffer, zds->lhSize);
7565+			if (ZSTD_isError(hSize))
7566+				return hSize;
7567+			if (hSize != 0) {				   /* need more input */
7568+				size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */
7569+				if (toLoad > (size_t)(iend - ip)) {	/* not enough input to load full header */
7570+					memcpy(zds->headerBuffer + zds->lhSize, ip, iend - ip);
7571+					zds->lhSize += iend - ip;
7572+					input->pos = input->size;
7573+					return (MAX(ZSTD_frameHeaderSize_min, hSize) - zds->lhSize) +
7574+					       ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
7575+				}
7576+				memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad);
7577+				zds->lhSize = hSize;
7578+				ip += toLoad;
7579+				break;
7580+			}
7581+
7582+			/* check for single-pass mode opportunity */
7583+			if (zds->fParams.frameContentSize && zds->fParams.windowSize /* skippable frame if == 0 */
7584+			    && (U64)(size_t)(oend - op) >= zds->fParams.frameContentSize) {
7585+				size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend - istart);
7586+				if (cSize <= (size_t)(iend - istart)) {
7587+					size_t const decompressedSize = ZSTD_decompress_usingDDict(zds->dctx, op, oend - op, istart, cSize, zds->ddict);
7588+					if (ZSTD_isError(decompressedSize))
7589+						return decompressedSize;
7590+					ip = istart + cSize;
7591+					op += decompressedSize;
7592+					zds->dctx->expected = 0;
7593+					zds->stage = zdss_init;
7594+					someMoreWork = 0;
7595+					break;
7596+				}
7597+			}
7598+
7599+			/* Consume header */
7600+			ZSTD_refDDict(zds->dctx, zds->ddict);
7601+			{
7602+				size_t const h1Size = ZSTD_nextSrcSizeToDecompress(zds->dctx); /* == ZSTD_frameHeaderSize_prefix */
7603+				CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer, h1Size));
7604+				{
7605+					size_t const h2Size = ZSTD_nextSrcSizeToDecompress(zds->dctx);
7606+					CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer + h1Size, h2Size));
7607+				}
7608+			}
7609+
7610+			zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
7611+			if (zds->fParams.windowSize > zds->maxWindowSize)
7612+				return ERROR(frameParameter_windowTooLarge);
7613+
7614+			/* Buffers are preallocated, but double check */
7615+			{
7616+				size_t const blockSize = MIN(zds->maxWindowSize, ZSTD_BLOCKSIZE_ABSOLUTEMAX);
7617+				size_t const neededOutSize = zds->maxWindowSize + blockSize + WILDCOPY_OVERLENGTH * 2;
7618+				if (zds->inBuffSize < blockSize) {
7619+					return ERROR(GENERIC);
7620+				}
7621+				if (zds->outBuffSize < neededOutSize) {
7622+					return ERROR(GENERIC);
7623+				}
7624+				zds->blockSize = blockSize;
7625+			}
7626+			zds->stage = zdss_read;
7627+		}
7628+		/* pass-through */
7629+
7630+		case zdss_read: {
7631+			size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds->dctx);
7632+			if (neededInSize == 0) { /* end of frame */
7633+				zds->stage = zdss_init;
7634+				someMoreWork = 0;
7635+				break;
7636+			}
7637+			if ((size_t)(iend - ip) >= neededInSize) { /* decode directly from src */
7638+				const int isSkipFrame = ZSTD_isSkipFrame(zds->dctx);
7639+				size_t const decodedSize = ZSTD_decompressContinue(zds->dctx, zds->outBuff + zds->outStart,
7640+										   (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart), ip, neededInSize);
7641+				if (ZSTD_isError(decodedSize))
7642+					return decodedSize;
7643+				ip += neededInSize;
7644+				if (!decodedSize && !isSkipFrame)
7645+					break; /* this was just a header */
7646+				zds->outEnd = zds->outStart + decodedSize;
7647+				zds->stage = zdss_flush;
7648+				break;
7649+			}
7650+			if (ip == iend) {
7651+				someMoreWork = 0;
7652+				break;
7653+			} /* no more input */
7654+			zds->stage = zdss_load;
7655+			/* pass-through */
7656+		}
7657+
7658+		case zdss_load: {
7659+			size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds->dctx);
7660+			size_t const toLoad = neededInSize - zds->inPos; /* should always be <= remaining space within inBuff */
7661+			size_t loadedSize;
7662+			if (toLoad > zds->inBuffSize - zds->inPos)
7663+				return ERROR(corruption_detected); /* should never happen */
7664+			loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend - ip);
7665+			ip += loadedSize;
7666+			zds->inPos += loadedSize;
7667+			if (loadedSize < toLoad) {
7668+				someMoreWork = 0;
7669+				break;
7670+			} /* not enough input, wait for more */
7671+
7672+			/* decode loaded input */
7673+			{
7674+				const int isSkipFrame = ZSTD_isSkipFrame(zds->dctx);
7675+				size_t const decodedSize = ZSTD_decompressContinue(zds->dctx, zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart,
7676+										   zds->inBuff, neededInSize);
7677+				if (ZSTD_isError(decodedSize))
7678+					return decodedSize;
7679+				zds->inPos = 0; /* input is consumed */
7680+				if (!decodedSize && !isSkipFrame) {
7681+					zds->stage = zdss_read;
7682+					break;
7683+				} /* this was just a header */
7684+				zds->outEnd = zds->outStart + decodedSize;
7685+				zds->stage = zdss_flush;
7686+				/* pass-through */
7687+			}
7688+		}
7689+
7690+		case zdss_flush: {
7691+			size_t const toFlushSize = zds->outEnd - zds->outStart;
7692+			size_t const flushedSize = ZSTD_limitCopy(op, oend - op, zds->outBuff + zds->outStart, toFlushSize);
7693+			op += flushedSize;
7694+			zds->outStart += flushedSize;
7695+			if (flushedSize == toFlushSize) { /* flush completed */
7696+				zds->stage = zdss_read;
7697+				if (zds->outStart + zds->blockSize > zds->outBuffSize)
7698+					zds->outStart = zds->outEnd = 0;
7699+				break;
7700+			}
7701+			/* cannot complete flush */
7702+			someMoreWork = 0;
7703+			break;
7704+		}
7705+		default:
7706+			return ERROR(GENERIC); /* impossible */
7707+		}
7708+	}
7709+
7710+	/* result */
7711+	input->pos += (size_t)(ip - istart);
7712+	output->pos += (size_t)(op - ostart);
7713+	{
7714+		size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds->dctx);
7715+		if (!nextSrcSizeHint) {			    /* frame fully decoded */
7716+			if (zds->outEnd == zds->outStart) { /* output fully flushed */
7717+				if (zds->hostageByte) {
7718+					if (input->pos >= input->size) {
7719+						zds->stage = zdss_read;
7720+						return 1;
7721+					}	     /* can't release hostage (not present) */
7722+					input->pos++; /* release hostage */
7723+				}
7724+				return 0;
7725+			}
7726+			if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
7727+				input->pos--;    /* note : pos > 0, otherwise, impossible to finish reading last block */
7728+				zds->hostageByte = 1;
7729+			}
7730+			return 1;
7731+		}
7732+		nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds->dctx) == ZSTDnit_block); /* preload header of next block */
7733+		if (zds->inPos > nextSrcSizeHint)
7734+			return ERROR(GENERIC); /* should never happen */
7735+		nextSrcSizeHint -= zds->inPos; /* already loaded*/
7736+		return nextSrcSizeHint;
7737+	}
7738+}
7739+
7740+EXPORT_SYMBOL(ZSTD_DCtxWorkspaceBound);
7741+EXPORT_SYMBOL(ZSTD_initDCtx);
7742+EXPORT_SYMBOL(ZSTD_decompressDCtx);
7743+EXPORT_SYMBOL(ZSTD_decompress_usingDict);
7744+
7745+EXPORT_SYMBOL(ZSTD_DDictWorkspaceBound);
7746+EXPORT_SYMBOL(ZSTD_initDDict);
7747+EXPORT_SYMBOL(ZSTD_decompress_usingDDict);
7748+
7749+EXPORT_SYMBOL(ZSTD_DStreamWorkspaceBound);
7750+EXPORT_SYMBOL(ZSTD_initDStream);
7751+EXPORT_SYMBOL(ZSTD_initDStream_usingDDict);
7752+EXPORT_SYMBOL(ZSTD_resetDStream);
7753+EXPORT_SYMBOL(ZSTD_decompressStream);
7754+EXPORT_SYMBOL(ZSTD_DStreamInSize);
7755+EXPORT_SYMBOL(ZSTD_DStreamOutSize);
7756+
7757+EXPORT_SYMBOL(ZSTD_findFrameCompressedSize);
7758+EXPORT_SYMBOL(ZSTD_getFrameContentSize);
7759+EXPORT_SYMBOL(ZSTD_findDecompressedSize);
7760+
7761+EXPORT_SYMBOL(ZSTD_isFrame);
7762+EXPORT_SYMBOL(ZSTD_getDictID_fromDict);
7763+EXPORT_SYMBOL(ZSTD_getDictID_fromDDict);
7764+EXPORT_SYMBOL(ZSTD_getDictID_fromFrame);
7765+
7766+EXPORT_SYMBOL(ZSTD_getFrameParams);
7767+EXPORT_SYMBOL(ZSTD_decompressBegin);
7768+EXPORT_SYMBOL(ZSTD_decompressBegin_usingDict);
7769+EXPORT_SYMBOL(ZSTD_copyDCtx);
7770+EXPORT_SYMBOL(ZSTD_nextSrcSizeToDecompress);
7771+EXPORT_SYMBOL(ZSTD_decompressContinue);
7772+EXPORT_SYMBOL(ZSTD_nextInputType);
7773+
7774+EXPORT_SYMBOL(ZSTD_decompressBlock);
7775+EXPORT_SYMBOL(ZSTD_insertBlock);
7776+
7777+MODULE_LICENSE("Dual BSD/GPL");
7778+MODULE_DESCRIPTION("Zstd Decompressor");
7779diff --git a/lib/zstd/entropy_common.c b/lib/zstd/entropy_common.c
7780new file mode 100644
7781index 0000000..2b0a643
7782--- /dev/null
7783+++ b/lib/zstd/entropy_common.c
7784@@ -0,0 +1,243 @@
7785+/*
7786+ * Common functions of New Generation Entropy library
7787+ * Copyright (C) 2016, Yann Collet.
7788+ *
7789+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7790+ *
7791+ * Redistribution and use in source and binary forms, with or without
7792+ * modification, are permitted provided that the following conditions are
7793+ * met:
7794+ *
7795+ *   * Redistributions of source code must retain the above copyright
7796+ * notice, this list of conditions and the following disclaimer.
7797+ *   * Redistributions in binary form must reproduce the above
7798+ * copyright notice, this list of conditions and the following disclaimer
7799+ * in the documentation and/or other materials provided with the
7800+ * distribution.
7801+ *
7802+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7803+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7804+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7805+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7806+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7807+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7808+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7809+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7810+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7811+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
7812+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7813+ *
7814+ * This program is free software; you can redistribute it and/or modify it under
7815+ * the terms of the GNU General Public License version 2 as published by the
7816+ * Free Software Foundation. This program is dual-licensed; you may select
7817+ * either version 2 of the GNU General Public License ("GPL") or BSD license
7818+ * ("BSD").
7819+ *
7820+ * You can contact the author at :
7821+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
7822+ */
7823+
7824+/* *************************************
7825+*  Dependencies
7826+***************************************/
7827+#include "error_private.h" /* ERR_*, ERROR */
7828+#include "fse.h"
7829+#include "huf.h"
7830+#include "mem.h"
7831+
7832+/*===   Version   ===*/
7833+unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
7834+
7835+/*===   Error Management   ===*/
7836+unsigned FSE_isError(size_t code) { return ERR_isError(code); }
7837+
7838+unsigned HUF_isError(size_t code) { return ERR_isError(code); }
7839+
7840+/*-**************************************************************
7841+*  FSE NCount encoding-decoding
7842+****************************************************************/
7843+size_t FSE_readNCount(short *normalizedCounter, unsigned *maxSVPtr, unsigned *tableLogPtr, const void *headerBuffer, size_t hbSize)
7844+{
7845+	const BYTE *const istart = (const BYTE *)headerBuffer;
7846+	const BYTE *const iend = istart + hbSize;
7847+	const BYTE *ip = istart;
7848+	int nbBits;
7849+	int remaining;
7850+	int threshold;
7851+	U32 bitStream;
7852+	int bitCount;
7853+	unsigned charnum = 0;
7854+	int previous0 = 0;
7855+
7856+	if (hbSize < 4)
7857+		return ERROR(srcSize_wrong);
7858+	bitStream = ZSTD_readLE32(ip);
7859+	nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
7860+	if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX)
7861+		return ERROR(tableLog_tooLarge);
7862+	bitStream >>= 4;
7863+	bitCount = 4;
7864+	*tableLogPtr = nbBits;
7865+	remaining = (1 << nbBits) + 1;
7866+	threshold = 1 << nbBits;
7867+	nbBits++;
7868+
7869+	while ((remaining > 1) & (charnum <= *maxSVPtr)) {
7870+		if (previous0) {
7871+			unsigned n0 = charnum;
7872+			while ((bitStream & 0xFFFF) == 0xFFFF) {
7873+				n0 += 24;
7874+				if (ip < iend - 5) {
7875+					ip += 2;
7876+					bitStream = ZSTD_readLE32(ip) >> bitCount;
7877+				} else {
7878+					bitStream >>= 16;
7879+					bitCount += 16;
7880+				}
7881+			}
7882+			while ((bitStream & 3) == 3) {
7883+				n0 += 3;
7884+				bitStream >>= 2;
7885+				bitCount += 2;
7886+			}
7887+			n0 += bitStream & 3;
7888+			bitCount += 2;
7889+			if (n0 > *maxSVPtr)
7890+				return ERROR(maxSymbolValue_tooSmall);
7891+			while (charnum < n0)
7892+				normalizedCounter[charnum++] = 0;
7893+			if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) {
7894+				ip += bitCount >> 3;
7895+				bitCount &= 7;
7896+				bitStream = ZSTD_readLE32(ip) >> bitCount;
7897+			} else {
7898+				bitStream >>= 2;
7899+			}
7900+		}
7901+		{
7902+			int const max = (2 * threshold - 1) - remaining;
7903+			int count;
7904+
7905+			if ((bitStream & (threshold - 1)) < (U32)max) {
7906+				count = bitStream & (threshold - 1);
7907+				bitCount += nbBits - 1;
7908+			} else {
7909+				count = bitStream & (2 * threshold - 1);
7910+				if (count >= threshold)
7911+					count -= max;
7912+				bitCount += nbBits;
7913+			}
7914+
7915+			count--;				 /* extra accuracy */
7916+			remaining -= count < 0 ? -count : count; /* -1 means +1 */
7917+			normalizedCounter[charnum++] = (short)count;
7918+			previous0 = !count;
7919+			while (remaining < threshold) {
7920+				nbBits--;
7921+				threshold >>= 1;
7922+			}
7923+
7924+			if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) {
7925+				ip += bitCount >> 3;
7926+				bitCount &= 7;
7927+			} else {
7928+				bitCount -= (int)(8 * (iend - 4 - ip));
7929+				ip = iend - 4;
7930+			}
7931+			bitStream = ZSTD_readLE32(ip) >> (bitCount & 31);
7932+		}
7933+	} /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
7934+	if (remaining != 1)
7935+		return ERROR(corruption_detected);
7936+	if (bitCount > 32)
7937+		return ERROR(corruption_detected);
7938+	*maxSVPtr = charnum - 1;
7939+
7940+	ip += (bitCount + 7) >> 3;
7941+	return ip - istart;
7942+}
7943+
7944+/*! HUF_readStats() :
7945+	Read compact Huffman tree, saved by HUF_writeCTable().
7946+	`huffWeight` is destination buffer.
7947+	`rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
7948+	@return : size read from `src` , or an error Code .
7949+	Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
7950+*/
7951+size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
7952+{
7953+	U32 weightTotal;
7954+	const BYTE *ip = (const BYTE *)src;
7955+	size_t iSize;
7956+	size_t oSize;
7957+
7958+	if (!srcSize)
7959+		return ERROR(srcSize_wrong);
7960+	iSize = ip[0];
7961+	/* memset(huffWeight, 0, hwSize);   */ /* is not necessary, even though some analyzer complain ... */
7962+
7963+	if (iSize >= 128) { /* special header */
7964+		oSize = iSize - 127;
7965+		iSize = ((oSize + 1) / 2);
7966+		if (iSize + 1 > srcSize)
7967+			return ERROR(srcSize_wrong);
7968+		if (oSize >= hwSize)
7969+			return ERROR(corruption_detected);
7970+		ip += 1;
7971+		{
7972+			U32 n;
7973+			for (n = 0; n < oSize; n += 2) {
7974+				huffWeight[n] = ip[n / 2] >> 4;
7975+				huffWeight[n + 1] = ip[n / 2] & 15;
7976+			}
7977+		}
7978+	} else {						 /* header compressed with FSE (normal case) */
7979+		if (iSize + 1 > srcSize)
7980+			return ERROR(srcSize_wrong);
7981+		oSize = FSE_decompress_wksp(huffWeight, hwSize - 1, ip + 1, iSize, 6, workspace, workspaceSize); /* max (hwSize-1) values decoded, as last one is implied */
7982+		if (FSE_isError(oSize))
7983+			return oSize;
7984+	}
7985+
7986+	/* collect weight stats */
7987+	memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
7988+	weightTotal = 0;
7989+	{
7990+		U32 n;
7991+		for (n = 0; n < oSize; n++) {
7992+			if (huffWeight[n] >= HUF_TABLELOG_MAX)
7993+				return ERROR(corruption_detected);
7994+			rankStats[huffWeight[n]]++;
7995+			weightTotal += (1 << huffWeight[n]) >> 1;
7996+		}
7997+	}
7998+	if (weightTotal == 0)
7999+		return ERROR(corruption_detected);
8000+
8001+	/* get last non-null symbol weight (implied, total must be 2^n) */
8002+	{
8003+		U32 const tableLog = BIT_highbit32(weightTotal) + 1;
8004+		if (tableLog > HUF_TABLELOG_MAX)
8005+			return ERROR(corruption_detected);
8006+		*tableLogPtr = tableLog;
8007+		/* determine last weight */
8008+		{
8009+			U32 const total = 1 << tableLog;
8010+			U32 const rest = total - weightTotal;
8011+			U32 const verif = 1 << BIT_highbit32(rest);
8012+			U32 const lastWeight = BIT_highbit32(rest) + 1;
8013+			if (verif != rest)
8014+				return ERROR(corruption_detected); /* last value must be a clean power of 2 */
8015+			huffWeight[oSize] = (BYTE)lastWeight;
8016+			rankStats[lastWeight]++;
8017+		}
8018+	}
8019+
8020+	/* check tree construction validity */
8021+	if ((rankStats[1] < 2) || (rankStats[1] & 1))
8022+		return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
8023+
8024+	/* results */
8025+	*nbSymbolsPtr = (U32)(oSize + 1);
8026+	return iSize + 1;
8027+}
8028diff --git a/lib/zstd/error_private.h b/lib/zstd/error_private.h
8029new file mode 100644
8030index 0000000..2062ff0
8031--- /dev/null
8032+++ b/lib/zstd/error_private.h
8033@@ -0,0 +1,51 @@
8034+/**
8035+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
8036+ * All rights reserved.
8037+ *
8038+ * This source code is licensed under the BSD-style license found in the
8039+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
8040+ *
8041+ * This program is free software; you can redistribute it and/or modify it under
8042+ * the terms of the GNU General Public License version 2 as published by the
8043+ * Free Software Foundation. This program is dual-licensed; you may select
8044+ * either version 2 of the GNU General Public License ("GPL") or BSD license
8045+ * ("BSD").
8046+ */
8047+
8048+/* Note : this module is expected to remain private, do not expose it */
8049+
8050+#ifndef ERROR_H_MODULE
8051+#define ERROR_H_MODULE
8052+
8053+/* ****************************************
8054+*  Dependencies
8055+******************************************/
8056+#include <linux/types.h> /* size_t */
8057+#include <linux/zstd.h>  /* enum list */
8058+
8059+/* ****************************************
8060+*  Compiler-specific
8061+******************************************/
8062+#define ERR_STATIC static __attribute__((unused))
8063+
8064+/*-****************************************
8065+*  Customization (error_public.h)
8066+******************************************/
8067+typedef ZSTD_ErrorCode ERR_enum;
8068+#define PREFIX(name) ZSTD_error_##name
8069+
8070+/*-****************************************
8071+*  Error codes handling
8072+******************************************/
8073+#define ERROR(name) ((size_t)-PREFIX(name))
8074+
8075+ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
8076+
8077+ERR_STATIC ERR_enum ERR_getErrorCode(size_t code)
8078+{
8079+	if (!ERR_isError(code))
8080+		return (ERR_enum)0;
8081+	return (ERR_enum)(0 - code);
8082+}
8083+
8084+#endif /* ERROR_H_MODULE */
8085diff --git a/lib/zstd/fse.h b/lib/zstd/fse.h
8086new file mode 100644
8087index 0000000..7460ab0
8088--- /dev/null
8089+++ b/lib/zstd/fse.h
8090@@ -0,0 +1,575 @@
8091+/*
8092+ * FSE : Finite State Entropy codec
8093+ * Public Prototypes declaration
8094+ * Copyright (C) 2013-2016, Yann Collet.
8095+ *
8096+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
8097+ *
8098+ * Redistribution and use in source and binary forms, with or without
8099+ * modification, are permitted provided that the following conditions are
8100+ * met:
8101+ *
8102+ *   * Redistributions of source code must retain the above copyright
8103+ * notice, this list of conditions and the following disclaimer.
8104+ *   * Redistributions in binary form must reproduce the above
8105+ * copyright notice, this list of conditions and the following disclaimer
8106+ * in the documentation and/or other materials provided with the
8107+ * distribution.
8108+ *
8109+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
8110+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8111+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8112+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8113+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8114+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8115+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8116+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8117+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8118+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8119+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8120+ *
8121+ * This program is free software; you can redistribute it and/or modify it under
8122+ * the terms of the GNU General Public License version 2 as published by the
8123+ * Free Software Foundation. This program is dual-licensed; you may select
8124+ * either version 2 of the GNU General Public License ("GPL") or BSD license
8125+ * ("BSD").
8126+ *
8127+ * You can contact the author at :
8128+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8129+ */
8130+#ifndef FSE_H
8131+#define FSE_H
8132+
8133+/*-*****************************************
8134+*  Dependencies
8135+******************************************/
8136+#include <linux/types.h> /* size_t, ptrdiff_t */
8137+
8138+/*-*****************************************
8139+*  FSE_PUBLIC_API : control library symbols visibility
8140+******************************************/
8141+#define FSE_PUBLIC_API
8142+
8143+/*------   Version   ------*/
8144+#define FSE_VERSION_MAJOR 0
8145+#define FSE_VERSION_MINOR 9
8146+#define FSE_VERSION_RELEASE 0
8147+
8148+#define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE
8149+#define FSE_QUOTE(str) #str
8150+#define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str)
8151+#define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION)
8152+
8153+#define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR * 100 * 100 + FSE_VERSION_MINOR * 100 + FSE_VERSION_RELEASE)
8154+FSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */
8155+
8156+/*-*****************************************
8157+*  Tool functions
8158+******************************************/
8159+FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */
8160+
8161+/* Error Management */
8162+FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */
8163+
8164+/*-*****************************************
8165+*  FSE detailed API
8166+******************************************/
8167+/*!
8168+FSE_compress() does the following:
8169+1. count symbol occurrence from source[] into table count[]
8170+2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog)
8171+3. save normalized counters to memory buffer using writeNCount()
8172+4. build encoding table 'CTable' from normalized counters
8173+5. encode the data stream using encoding table 'CTable'
8174+
8175+FSE_decompress() does the following:
8176+1. read normalized counters with readNCount()
8177+2. build decoding table 'DTable' from normalized counters
8178+3. decode the data stream using decoding table 'DTable'
8179+
8180+The following API allows targeting specific sub-functions for advanced tasks.
8181+For example, it's possible to compress several blocks using the same 'CTable',
8182+or to save and provide normalized distribution using external method.
8183+*/
8184+
8185+/* *** COMPRESSION *** */
8186+/*! FSE_optimalTableLog():
8187+	dynamically downsize 'tableLog' when conditions are met.
8188+	It saves CPU time, by using smaller tables, while preserving or even improving compression ratio.
8189+	@return : recommended tableLog (necessarily <= 'maxTableLog') */
8190+FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
8191+
8192+/*! FSE_normalizeCount():
8193+	normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)
8194+	'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).
8195+	@return : tableLog,
8196+			  or an errorCode, which can be tested using FSE_isError() */
8197+FSE_PUBLIC_API size_t FSE_normalizeCount(short *normalizedCounter, unsigned tableLog, const unsigned *count, size_t srcSize, unsigned maxSymbolValue);
8198+
8199+/*! FSE_NCountWriteBound():
8200+	Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.
8201+	Typically useful for allocation purpose. */
8202+FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);
8203+
8204+/*! FSE_writeNCount():
8205+	Compactly save 'normalizedCounter' into 'buffer'.
8206+	@return : size of the compressed table,
8207+			  or an errorCode, which can be tested using FSE_isError(). */
8208+FSE_PUBLIC_API size_t FSE_writeNCount(void *buffer, size_t bufferSize, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
8209+
8210+/*! Constructor and Destructor of FSE_CTable.
8211+	Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */
8212+typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */
8213+
8214+/*! FSE_compress_usingCTable():
8215+	Compress `src` using `ct` into `dst` which must be already allocated.
8216+	@return : size of compressed data (<= `dstCapacity`),
8217+			  or 0 if compressed data could not fit into `dst`,
8218+			  or an errorCode, which can be tested using FSE_isError() */
8219+FSE_PUBLIC_API size_t FSE_compress_usingCTable(void *dst, size_t dstCapacity, const void *src, size_t srcSize, const FSE_CTable *ct);
8220+
8221+/*!
8222+Tutorial :
8223+----------
8224+The first step is to count all symbols. FSE_count() does this job very fast.
8225+Result will be saved into 'count', a table of unsigned int, which must be already allocated, and have 'maxSymbolValuePtr[0]+1' cells.
8226+'src' is a table of bytes of size 'srcSize'. All values within 'src' MUST be <= maxSymbolValuePtr[0]
8227+maxSymbolValuePtr[0] will be updated, with its real value (necessarily <= original value)
8228+FSE_count() will return the number of occurrence of the most frequent symbol.
8229+This can be used to know if there is a single symbol within 'src', and to quickly evaluate its compressibility.
8230+If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).
8231+
8232+The next step is to normalize the frequencies.
8233+FSE_normalizeCount() will ensure that sum of frequencies is == 2 ^'tableLog'.
8234+It also guarantees a minimum of 1 to any Symbol with frequency >= 1.
8235+You can use 'tableLog'==0 to mean "use default tableLog value".
8236+If you are unsure of which tableLog value to use, you can ask FSE_optimalTableLog(),
8237+which will provide the optimal valid tableLog given sourceSize, maxSymbolValue, and a user-defined maximum (0 means "default").
8238+
8239+The result of FSE_normalizeCount() will be saved into a table,
8240+called 'normalizedCounter', which is a table of signed short.
8241+'normalizedCounter' must be already allocated, and have at least 'maxSymbolValue+1' cells.
8242+The return value is tableLog if everything proceeded as expected.
8243+It is 0 if there is a single symbol within distribution.
8244+If there is an error (ex: invalid tableLog value), the function will return an ErrorCode (which can be tested using FSE_isError()).
8245+
8246+'normalizedCounter' can be saved in a compact manner to a memory area using FSE_writeNCount().
8247+'buffer' must be already allocated.
8248+For guaranteed success, buffer size must be at least FSE_headerBound().
8249+The result of the function is the number of bytes written into 'buffer'.
8250+If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError(); ex : buffer size too small).
8251+
8252+'normalizedCounter' can then be used to create the compression table 'CTable'.
8253+The space required by 'CTable' must be already allocated, using FSE_createCTable().
8254+You can then use FSE_buildCTable() to fill 'CTable'.
8255+If there is an error, both functions will return an ErrorCode (which can be tested using FSE_isError()).
8256+
8257+'CTable' can then be used to compress 'src', with FSE_compress_usingCTable().
8258+Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize'
8259+The function returns the size of compressed data (without header), necessarily <= `dstCapacity`.
8260+If it returns '0', compressed data could not fit into 'dst'.
8261+If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).
8262+*/
8263+
8264+/* *** DECOMPRESSION *** */
8265+
8266+/*! FSE_readNCount():
8267+	Read compactly saved 'normalizedCounter' from 'rBuffer'.
8268+	@return : size read from 'rBuffer',
8269+			  or an errorCode, which can be tested using FSE_isError().
8270+			  maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */
8271+FSE_PUBLIC_API size_t FSE_readNCount(short *normalizedCounter, unsigned *maxSymbolValuePtr, unsigned *tableLogPtr, const void *rBuffer, size_t rBuffSize);
8272+
8273+/*! Constructor and Destructor of FSE_DTable.
8274+	Note that its size depends on 'tableLog' */
8275+typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
8276+
8277+/*! FSE_buildDTable():
8278+	Builds 'dt', which must be already allocated, using FSE_createDTable().
8279+	return : 0, or an errorCode, which can be tested using FSE_isError() */
8280+FSE_PUBLIC_API size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize);
8281+
8282+/*! FSE_decompress_usingDTable():
8283+	Decompress compressed source `cSrc` of size `cSrcSize` using `dt`
8284+	into `dst` which must be already allocated.
8285+	@return : size of regenerated data (necessarily <= `dstCapacity`),
8286+			  or an errorCode, which can be tested using FSE_isError() */
8287+FSE_PUBLIC_API size_t FSE_decompress_usingDTable(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt);
8288+
8289+/*!
8290+Tutorial :
8291+----------
8292+(Note : these functions only decompress FSE-compressed blocks.
8293+ If block is uncompressed, use memcpy() instead
8294+ If block is a single repeated byte, use memset() instead )
8295+
8296+The first step is to obtain the normalized frequencies of symbols.
8297+This can be performed by FSE_readNCount() if it was saved using FSE_writeNCount().
8298+'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short.
8299+In practice, that means it's necessary to know 'maxSymbolValue' beforehand,
8300+or size the table to handle worst case situations (typically 256).
8301+FSE_readNCount() will provide 'tableLog' and 'maxSymbolValue'.
8302+The result of FSE_readNCount() is the number of bytes read from 'rBuffer'.
8303+Note that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that.
8304+If there is an error, the function will return an error code, which can be tested using FSE_isError().
8305+
8306+The next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'.
8307+This is performed by the function FSE_buildDTable().
8308+The space required by 'FSE_DTable' must be already allocated using FSE_createDTable().
8309+If there is an error, the function will return an error code, which can be tested using FSE_isError().
8310+
8311+`FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable().
8312+`cSrcSize` must be strictly correct, otherwise decompression will fail.
8313+FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`).
8314+If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small)
8315+*/
8316+
8317+/* *** Dependency *** */
8318+#include "bitstream.h"
8319+
8320+/* *****************************************
8321+*  Static allocation
8322+*******************************************/
8323+/* FSE buffer bounds */
8324+#define FSE_NCOUNTBOUND 512
8325+#define FSE_BLOCKBOUND(size) (size + (size >> 7))
8326+#define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
8327+
8328+/* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */
8329+#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1 << (maxTableLog - 1)) + ((maxSymbolValue + 1) * 2))
8330+#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1 << maxTableLog))
8331+
8332+/* *****************************************
8333+*  FSE advanced API
8334+*******************************************/
8335+/* FSE_count_wksp() :
8336+ * Same as FSE_count(), but using an externally provided scratch buffer.
8337+ * `workSpace` size must be table of >= `1024` unsigned
8338+ */
8339+size_t FSE_count_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, unsigned *workSpace);
8340+
8341+/* FSE_countFast_wksp() :
8342+ * Same as FSE_countFast(), but using an externally provided scratch buffer.
8343+ * `workSpace` must be a table of minimum `1024` unsigned
8344+ */
8345+size_t FSE_countFast_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize, unsigned *workSpace);
8346+
8347+/*! FSE_count_simple
8348+ * Same as FSE_countFast(), but does not use any additional memory (not even on stack).
8349+ * This function is unsafe, and will segfault if any value within `src` is `> *maxSymbolValuePtr` (presuming it's also the size of `count`).
8350+*/
8351+size_t FSE_count_simple(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize);
8352+
8353+unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus);
8354+/**< same as FSE_optimalTableLog(), which used `minus==2` */
8355+
8356+size_t FSE_buildCTable_raw(FSE_CTable *ct, unsigned nbBits);
8357+/**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */
8358+
8359+size_t FSE_buildCTable_rle(FSE_CTable *ct, unsigned char symbolValue);
8360+/**< build a fake FSE_CTable, designed to compress always the same symbolValue */
8361+
8362+/* FSE_buildCTable_wksp() :
8363+ * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
8364+ * `wkspSize` must be >= `(1<<tableLog)`.
8365+ */
8366+size_t FSE_buildCTable_wksp(FSE_CTable *ct, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workSpace, size_t wkspSize);
8367+
8368+size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits);
8369+/**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */
8370+
8371+size_t FSE_buildDTable_rle(FSE_DTable *dt, unsigned char symbolValue);
8372+/**< build a fake FSE_DTable, designed to always generate the same symbolValue */
8373+
8374+size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize);
8375+/**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */
8376+
8377+/* *****************************************
8378+*  FSE symbol compression API
8379+*******************************************/
8380+/*!
8381+   This API consists of small unitary functions, which highly benefit from being inlined.
8382+   Hence their body are included in next section.
8383+*/
8384+typedef struct {
8385+	ptrdiff_t value;
8386+	const void *stateTable;
8387+	const void *symbolTT;
8388+	unsigned stateLog;
8389+} FSE_CState_t;
8390+
8391+static void FSE_initCState(FSE_CState_t *CStatePtr, const FSE_CTable *ct);
8392+
8393+static void FSE_encodeSymbol(BIT_CStream_t *bitC, FSE_CState_t *CStatePtr, unsigned symbol);
8394+
8395+static void FSE_flushCState(BIT_CStream_t *bitC, const FSE_CState_t *CStatePtr);
8396+
8397+/**<
8398+These functions are inner components of FSE_compress_usingCTable().
8399+They allow the creation of custom streams, mixing multiple tables and bit sources.
8400+
8401+A key property to keep in mind is that encoding and decoding are done **in reverse direction**.
8402+So the first symbol you will encode is the last you will decode, like a LIFO stack.
8403+
8404+You will need a few variables to track your CStream. They are :
8405+
8406+FSE_CTable    ct;         // Provided by FSE_buildCTable()
8407+BIT_CStream_t bitStream;  // bitStream tracking structure
8408+FSE_CState_t  state;      // State tracking structure (can have several)
8409+
8410+
8411+The first thing to do is to init bitStream and state.
8412+	size_t errorCode = BIT_initCStream(&bitStream, dstBuffer, maxDstSize);
8413+	FSE_initCState(&state, ct);
8414+
8415+Note that BIT_initCStream() can produce an error code, so its result should be tested, using FSE_isError();
8416+You can then encode your input data, byte after byte.
8417+FSE_encodeSymbol() outputs a maximum of 'tableLog' bits at a time.
8418+Remember decoding will be done in reverse direction.
8419+	FSE_encodeByte(&bitStream, &state, symbol);
8420+
8421+At any time, you can also add any bit sequence.
8422+Note : maximum allowed nbBits is 25, for compatibility with 32-bits decoders
8423+	BIT_addBits(&bitStream, bitField, nbBits);
8424+
8425+The above methods don't commit data to memory, they just store it into local register, for speed.
8426+Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
8427+Writing data to memory is a manual operation, performed by the flushBits function.
8428+	BIT_flushBits(&bitStream);
8429+
8430+Your last FSE encoding operation shall be to flush your last state value(s).
8431+	FSE_flushState(&bitStream, &state);
8432+
8433+Finally, you must close the bitStream.
8434+The function returns the size of CStream in bytes.
8435+If data couldn't fit into dstBuffer, it will return a 0 ( == not compressible)
8436+If there is an error, it returns an errorCode (which can be tested using FSE_isError()).
8437+	size_t size = BIT_closeCStream(&bitStream);
8438+*/
8439+
8440+/* *****************************************
8441+*  FSE symbol decompression API
8442+*******************************************/
8443+typedef struct {
8444+	size_t state;
8445+	const void *table; /* precise table may vary, depending on U16 */
8446+} FSE_DState_t;
8447+
8448+static void FSE_initDState(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD, const FSE_DTable *dt);
8449+
8450+static unsigned char FSE_decodeSymbol(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD);
8451+
8452+static unsigned FSE_endOfDState(const FSE_DState_t *DStatePtr);
8453+
8454+/**<
8455+Let's now decompose FSE_decompress_usingDTable() into its unitary components.
8456+You will decode FSE-encoded symbols from the bitStream,
8457+and also any other bitFields you put in, **in reverse order**.
8458+
8459+You will need a few variables to track your bitStream. They are :
8460+
8461+BIT_DStream_t DStream;    // Stream context
8462+FSE_DState_t  DState;     // State context. Multiple ones are possible
8463+FSE_DTable*   DTablePtr;  // Decoding table, provided by FSE_buildDTable()
8464+
8465+The first thing to do is to init the bitStream.
8466+	errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);
8467+
8468+You should then retrieve your initial state(s)
8469+(in reverse flushing order if you have several ones) :
8470+	errorCode = FSE_initDState(&DState, &DStream, DTablePtr);
8471+
8472+You can then decode your data, symbol after symbol.
8473+For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.
8474+Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
8475+	unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);
8476+
8477+You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
8478+Note : maximum allowed nbBits is 25, for 32-bits compatibility
8479+	size_t bitField = BIT_readBits(&DStream, nbBits);
8480+
8481+All above operations only read from local register (which size depends on size_t).
8482+Refueling the register from memory is manually performed by the reload method.
8483+	endSignal = FSE_reloadDStream(&DStream);
8484+
8485+BIT_reloadDStream() result tells if there is still some more data to read from DStream.
8486+BIT_DStream_unfinished : there is still some data left into the DStream.
8487+BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
8488+BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
8489+BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
8490+
8491+When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
8492+to properly detect the exact end of stream.
8493+After each decoded symbol, check if DStream is fully consumed using this simple test :
8494+	BIT_reloadDStream(&DStream) >= BIT_DStream_completed
8495+
8496+When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
8497+Checking if DStream has reached its end is performed by :
8498+	BIT_endOfDStream(&DStream);
8499+Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
8500+	FSE_endOfDState(&DState);
8501+*/
8502+
8503+/* *****************************************
8504+*  FSE unsafe API
8505+*******************************************/
8506+static unsigned char FSE_decodeSymbolFast(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD);
8507+/* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
8508+
8509+/* *****************************************
8510+*  Implementation of inlined functions
8511+*******************************************/
8512+typedef struct {
8513+	int deltaFindState;
8514+	U32 deltaNbBits;
8515+} FSE_symbolCompressionTransform; /* total 8 bytes */
8516+
8517+ZSTD_STATIC void FSE_initCState(FSE_CState_t *statePtr, const FSE_CTable *ct)
8518+{
8519+	const void *ptr = ct;
8520+	const U16 *u16ptr = (const U16 *)ptr;
8521+	const U32 tableLog = ZSTD_read16(ptr);
8522+	statePtr->value = (ptrdiff_t)1 << tableLog;
8523+	statePtr->stateTable = u16ptr + 2;
8524+	statePtr->symbolTT = ((const U32 *)ct + 1 + (tableLog ? (1 << (tableLog - 1)) : 1));
8525+	statePtr->stateLog = tableLog;
8526+}
8527+
8528+/*! FSE_initCState2() :
8529+*   Same as FSE_initCState(), but the first symbol to include (which will be the last to be read)
8530+*   uses the smallest state value possible, saving the cost of this symbol */
8531+ZSTD_STATIC void FSE_initCState2(FSE_CState_t *statePtr, const FSE_CTable *ct, U32 symbol)
8532+{
8533+	FSE_initCState(statePtr, ct);
8534+	{
8535+		const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform *)(statePtr->symbolTT))[symbol];
8536+		const U16 *stateTable = (const U16 *)(statePtr->stateTable);
8537+		U32 nbBitsOut = (U32)((symbolTT.deltaNbBits + (1 << 15)) >> 16);
8538+		statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits;
8539+		statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
8540+	}
8541+}
8542+
8543+ZSTD_STATIC void FSE_encodeSymbol(BIT_CStream_t *bitC, FSE_CState_t *statePtr, U32 symbol)
8544+{
8545+	const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform *)(statePtr->symbolTT))[symbol];
8546+	const U16 *const stateTable = (const U16 *)(statePtr->stateTable);
8547+	U32 nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16);
8548+	BIT_addBits(bitC, statePtr->value, nbBitsOut);
8549+	statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
8550+}
8551+
8552+ZSTD_STATIC void FSE_flushCState(BIT_CStream_t *bitC, const FSE_CState_t *statePtr)
8553+{
8554+	BIT_addBits(bitC, statePtr->value, statePtr->stateLog);
8555+	BIT_flushBits(bitC);
8556+}
8557+
8558+/* ======    Decompression    ====== */
8559+
8560+typedef struct {
8561+	U16 tableLog;
8562+	U16 fastMode;
8563+} FSE_DTableHeader; /* sizeof U32 */
8564+
8565+typedef struct {
8566+	unsigned short newState;
8567+	unsigned char symbol;
8568+	unsigned char nbBits;
8569+} FSE_decode_t; /* size == U32 */
8570+
8571+ZSTD_STATIC void FSE_initDState(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD, const FSE_DTable *dt)
8572+{
8573+	const void *ptr = dt;
8574+	const FSE_DTableHeader *const DTableH = (const FSE_DTableHeader *)ptr;
8575+	DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);
8576+	BIT_reloadDStream(bitD);
8577+	DStatePtr->table = dt + 1;
8578+}
8579+
8580+ZSTD_STATIC BYTE FSE_peekSymbol(const FSE_DState_t *DStatePtr)
8581+{
8582+	FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state];
8583+	return DInfo.symbol;
8584+}
8585+
8586+ZSTD_STATIC void FSE_updateState(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD)
8587+{
8588+	FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state];
8589+	U32 const nbBits = DInfo.nbBits;
8590+	size_t const lowBits = BIT_readBits(bitD, nbBits);
8591+	DStatePtr->state = DInfo.newState + lowBits;
8592+}
8593+
8594+ZSTD_STATIC BYTE FSE_decodeSymbol(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD)
8595+{
8596+	FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state];
8597+	U32 const nbBits = DInfo.nbBits;
8598+	BYTE const symbol = DInfo.symbol;
8599+	size_t const lowBits = BIT_readBits(bitD, nbBits);
8600+
8601+	DStatePtr->state = DInfo.newState + lowBits;
8602+	return symbol;
8603+}
8604+
8605+/*! FSE_decodeSymbolFast() :
8606+	unsafe, only works if no symbol has a probability > 50% */
8607+ZSTD_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t *DStatePtr, BIT_DStream_t *bitD)
8608+{
8609+	FSE_decode_t const DInfo = ((const FSE_decode_t *)(DStatePtr->table))[DStatePtr->state];
8610+	U32 const nbBits = DInfo.nbBits;
8611+	BYTE const symbol = DInfo.symbol;
8612+	size_t const lowBits = BIT_readBitsFast(bitD, nbBits);
8613+
8614+	DStatePtr->state = DInfo.newState + lowBits;
8615+	return symbol;
8616+}
8617+
8618+ZSTD_STATIC unsigned FSE_endOfDState(const FSE_DState_t *DStatePtr) { return DStatePtr->state == 0; }
8619+
8620+/* **************************************************************
8621+*  Tuning parameters
8622+****************************************************************/
8623+/*!MEMORY_USAGE :
8624+*  Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
8625+*  Increasing memory usage improves compression ratio
8626+*  Reduced memory usage can improve speed, due to cache effect
8627+*  Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
8628+#ifndef FSE_MAX_MEMORY_USAGE
8629+#define FSE_MAX_MEMORY_USAGE 14
8630+#endif
8631+#ifndef FSE_DEFAULT_MEMORY_USAGE
8632+#define FSE_DEFAULT_MEMORY_USAGE 13
8633+#endif
8634+
8635+/*!FSE_MAX_SYMBOL_VALUE :
8636+*  Maximum symbol value authorized.
8637+*  Required for proper stack allocation */
8638+#ifndef FSE_MAX_SYMBOL_VALUE
8639+#define FSE_MAX_SYMBOL_VALUE 255
8640+#endif
8641+
8642+/* **************************************************************
8643+*  template functions type & suffix
8644+****************************************************************/
8645+#define FSE_FUNCTION_TYPE BYTE
8646+#define FSE_FUNCTION_EXTENSION
8647+#define FSE_DECODE_TYPE FSE_decode_t
8648+
8649+/* ***************************************************************
8650+*  Constants
8651+*****************************************************************/
8652+#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE - 2)
8653+#define FSE_MAX_TABLESIZE (1U << FSE_MAX_TABLELOG)
8654+#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE - 1)
8655+#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE - 2)
8656+#define FSE_MIN_TABLELOG 5
8657+
8658+#define FSE_TABLELOG_ABSOLUTE_MAX 15
8659+#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
8660+#error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
8661+#endif
8662+
8663+#define FSE_TABLESTEP(tableSize) ((tableSize >> 1) + (tableSize >> 3) + 3)
8664+
8665+#endif /* FSE_H */
8666diff --git a/lib/zstd/fse_compress.c b/lib/zstd/fse_compress.c
8667new file mode 100644
8668index 0000000..ef3d174
8669--- /dev/null
8670+++ b/lib/zstd/fse_compress.c
8671@@ -0,0 +1,795 @@
8672+/*
8673+ * FSE : Finite State Entropy encoder
8674+ * Copyright (C) 2013-2015, Yann Collet.
8675+ *
8676+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
8677+ *
8678+ * Redistribution and use in source and binary forms, with or without
8679+ * modification, are permitted provided that the following conditions are
8680+ * met:
8681+ *
8682+ *   * Redistributions of source code must retain the above copyright
8683+ * notice, this list of conditions and the following disclaimer.
8684+ *   * Redistributions in binary form must reproduce the above
8685+ * copyright notice, this list of conditions and the following disclaimer
8686+ * in the documentation and/or other materials provided with the
8687+ * distribution.
8688+ *
8689+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
8690+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8691+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8692+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8693+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8694+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8695+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8696+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8697+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8698+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8699+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8700+ *
8701+ * This program is free software; you can redistribute it and/or modify it under
8702+ * the terms of the GNU General Public License version 2 as published by the
8703+ * Free Software Foundation. This program is dual-licensed; you may select
8704+ * either version 2 of the GNU General Public License ("GPL") or BSD license
8705+ * ("BSD").
8706+ *
8707+ * You can contact the author at :
8708+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8709+ */
8710+
8711+/* **************************************************************
8712+*  Compiler specifics
8713+****************************************************************/
8714+#define FORCE_INLINE static __always_inline
8715+
8716+/* **************************************************************
8717+*  Includes
8718+****************************************************************/
8719+#include "bitstream.h"
8720+#include "fse.h"
8721+#include <linux/compiler.h>
8722+#include <linux/kernel.h>
8723+#include <linux/math64.h>
8724+#include <linux/string.h> /* memcpy, memset */
8725+
8726+/* **************************************************************
8727+*  Error Management
8728+****************************************************************/
8729+#define FSE_STATIC_ASSERT(c)                                   \
8730+	{                                                      \
8731+		enum { FSE_static_assert = 1 / (int)(!!(c)) }; \
8732+	} /* use only *after* variable declarations */
8733+
8734+/* **************************************************************
8735+*  Templates
8736+****************************************************************/
8737+/*
8738+  designed to be included
8739+  for type-specific functions (template emulation in C)
8740+  Objective is to write these functions only once, for improved maintenance
8741+*/
8742+
8743+/* safety checks */
8744+#ifndef FSE_FUNCTION_EXTENSION
8745+#error "FSE_FUNCTION_EXTENSION must be defined"
8746+#endif
8747+#ifndef FSE_FUNCTION_TYPE
8748+#error "FSE_FUNCTION_TYPE must be defined"
8749+#endif
8750+
8751+/* Function names */
8752+#define FSE_CAT(X, Y) X##Y
8753+#define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y)
8754+#define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y)
8755+
8756+/* Function templates */
8757+
8758+/* FSE_buildCTable_wksp() :
8759+ * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
8760+ * wkspSize should be sized to handle worst case situation, which is `1<<max_tableLog * sizeof(FSE_FUNCTION_TYPE)`
8761+ * workSpace must also be properly aligned with FSE_FUNCTION_TYPE requirements
8762+ */
8763+size_t FSE_buildCTable_wksp(FSE_CTable *ct, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize)
8764+{
8765+	U32 const tableSize = 1 << tableLog;
8766+	U32 const tableMask = tableSize - 1;
8767+	void *const ptr = ct;
8768+	U16 *const tableU16 = ((U16 *)ptr) + 2;
8769+	void *const FSCT = ((U32 *)ptr) + 1 /* header */ + (tableLog ? tableSize >> 1 : 1);
8770+	FSE_symbolCompressionTransform *const symbolTT = (FSE_symbolCompressionTransform *)(FSCT);
8771+	U32 const step = FSE_TABLESTEP(tableSize);
8772+	U32 highThreshold = tableSize - 1;
8773+
8774+	U32 *cumul;
8775+	FSE_FUNCTION_TYPE *tableSymbol;
8776+	size_t spaceUsed32 = 0;
8777+
8778+	cumul = (U32 *)workspace + spaceUsed32;
8779+	spaceUsed32 += FSE_MAX_SYMBOL_VALUE + 2;
8780+	tableSymbol = (FSE_FUNCTION_TYPE *)((U32 *)workspace + spaceUsed32);
8781+	spaceUsed32 += ALIGN(sizeof(FSE_FUNCTION_TYPE) * ((size_t)1 << tableLog), sizeof(U32)) >> 2;
8782+
8783+	if ((spaceUsed32 << 2) > workspaceSize)
8784+		return ERROR(tableLog_tooLarge);
8785+	workspace = (U32 *)workspace + spaceUsed32;
8786+	workspaceSize -= (spaceUsed32 << 2);
8787+
8788+	/* CTable header */
8789+	tableU16[-2] = (U16)tableLog;
8790+	tableU16[-1] = (U16)maxSymbolValue;
8791+
8792+	/* For explanations on how to distribute symbol values over the table :
8793+	*  http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */
8794+
8795+	/* symbol start positions */
8796+	{
8797+		U32 u;
8798+		cumul[0] = 0;
8799+		for (u = 1; u <= maxSymbolValue + 1; u++) {
8800+			if (normalizedCounter[u - 1] == -1) { /* Low proba symbol */
8801+				cumul[u] = cumul[u - 1] + 1;
8802+				tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u - 1);
8803+			} else {
8804+				cumul[u] = cumul[u - 1] + normalizedCounter[u - 1];
8805+			}
8806+		}
8807+		cumul[maxSymbolValue + 1] = tableSize + 1;
8808+	}
8809+
8810+	/* Spread symbols */
8811+	{
8812+		U32 position = 0;
8813+		U32 symbol;
8814+		for (symbol = 0; symbol <= maxSymbolValue; symbol++) {
8815+			int nbOccurrences;
8816+			for (nbOccurrences = 0; nbOccurrences < normalizedCounter[symbol]; nbOccurrences++) {
8817+				tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
8818+				position = (position + step) & tableMask;
8819+				while (position > highThreshold)
8820+					position = (position + step) & tableMask; /* Low proba area */
8821+			}
8822+		}
8823+
8824+		if (position != 0)
8825+			return ERROR(GENERIC); /* Must have gone through all positions */
8826+	}
8827+
8828+	/* Build table */
8829+	{
8830+		U32 u;
8831+		for (u = 0; u < tableSize; u++) {
8832+			FSE_FUNCTION_TYPE s = tableSymbol[u];	/* note : static analyzer may not understand tableSymbol is properly initialized */
8833+			tableU16[cumul[s]++] = (U16)(tableSize + u); /* TableU16 : sorted by symbol order; gives next state value */
8834+		}
8835+	}
8836+
8837+	/* Build Symbol Transformation Table */
8838+	{
8839+		unsigned total = 0;
8840+		unsigned s;
8841+		for (s = 0; s <= maxSymbolValue; s++) {
8842+			switch (normalizedCounter[s]) {
8843+			case 0: break;
8844+
8845+			case -1:
8846+			case 1:
8847+				symbolTT[s].deltaNbBits = (tableLog << 16) - (1 << tableLog);
8848+				symbolTT[s].deltaFindState = total - 1;
8849+				total++;
8850+				break;
8851+			default: {
8852+				U32 const maxBitsOut = tableLog - BIT_highbit32(normalizedCounter[s] - 1);
8853+				U32 const minStatePlus = normalizedCounter[s] << maxBitsOut;
8854+				symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;
8855+				symbolTT[s].deltaFindState = total - normalizedCounter[s];
8856+				total += normalizedCounter[s];
8857+			}
8858+			}
8859+		}
8860+	}
8861+
8862+	return 0;
8863+}
8864+
8865+/*-**************************************************************
8866+*  FSE NCount encoding-decoding
8867+****************************************************************/
8868+size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
8869+{
8870+	size_t const maxHeaderSize = (((maxSymbolValue + 1) * tableLog) >> 3) + 3;
8871+	return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND; /* maxSymbolValue==0 ? use default */
8872+}
8873+
8874+static size_t FSE_writeNCount_generic(void *header, size_t headerBufferSize, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
8875+				      unsigned writeIsSafe)
8876+{
8877+	BYTE *const ostart = (BYTE *)header;
8878+	BYTE *out = ostart;
8879+	BYTE *const oend = ostart + headerBufferSize;
8880+	int nbBits;
8881+	const int tableSize = 1 << tableLog;
8882+	int remaining;
8883+	int threshold;
8884+	U32 bitStream;
8885+	int bitCount;
8886+	unsigned charnum = 0;
8887+	int previous0 = 0;
8888+
8889+	bitStream = 0;
8890+	bitCount = 0;
8891+	/* Table Size */
8892+	bitStream += (tableLog - FSE_MIN_TABLELOG) << bitCount;
8893+	bitCount += 4;
8894+
8895+	/* Init */
8896+	remaining = tableSize + 1; /* +1 for extra accuracy */
8897+	threshold = tableSize;
8898+	nbBits = tableLog + 1;
8899+
8900+	while (remaining > 1) { /* stops at 1 */
8901+		if (previous0) {
8902+			unsigned start = charnum;
8903+			while (!normalizedCounter[charnum])
8904+				charnum++;
8905+			while (charnum >= start + 24) {
8906+				start += 24;
8907+				bitStream += 0xFFFFU << bitCount;
8908+				if ((!writeIsSafe) && (out > oend - 2))
8909+					return ERROR(dstSize_tooSmall); /* Buffer overflow */
8910+				out[0] = (BYTE)bitStream;
8911+				out[1] = (BYTE)(bitStream >> 8);
8912+				out += 2;
8913+				bitStream >>= 16;
8914+			}
8915+			while (charnum >= start + 3) {
8916+				start += 3;
8917+				bitStream += 3 << bitCount;
8918+				bitCount += 2;
8919+			}
8920+			bitStream += (charnum - start) << bitCount;
8921+			bitCount += 2;
8922+			if (bitCount > 16) {
8923+				if ((!writeIsSafe) && (out > oend - 2))
8924+					return ERROR(dstSize_tooSmall); /* Buffer overflow */
8925+				out[0] = (BYTE)bitStream;
8926+				out[1] = (BYTE)(bitStream >> 8);
8927+				out += 2;
8928+				bitStream >>= 16;
8929+				bitCount -= 16;
8930+			}
8931+		}
8932+		{
8933+			int count = normalizedCounter[charnum++];
8934+			int const max = (2 * threshold - 1) - remaining;
8935+			remaining -= count < 0 ? -count : count;
8936+			count++; /* +1 for extra accuracy */
8937+			if (count >= threshold)
8938+				count += max; /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
8939+			bitStream += count << bitCount;
8940+			bitCount += nbBits;
8941+			bitCount -= (count < max);
8942+			previous0 = (count == 1);
8943+			if (remaining < 1)
8944+				return ERROR(GENERIC);
8945+			while (remaining < threshold)
8946+				nbBits--, threshold >>= 1;
8947+		}
8948+		if (bitCount > 16) {
8949+			if ((!writeIsSafe) && (out > oend - 2))
8950+				return ERROR(dstSize_tooSmall); /* Buffer overflow */
8951+			out[0] = (BYTE)bitStream;
8952+			out[1] = (BYTE)(bitStream >> 8);
8953+			out += 2;
8954+			bitStream >>= 16;
8955+			bitCount -= 16;
8956+		}
8957+	}
8958+
8959+	/* flush remaining bitStream */
8960+	if ((!writeIsSafe) && (out > oend - 2))
8961+		return ERROR(dstSize_tooSmall); /* Buffer overflow */
8962+	out[0] = (BYTE)bitStream;
8963+	out[1] = (BYTE)(bitStream >> 8);
8964+	out += (bitCount + 7) / 8;
8965+
8966+	if (charnum > maxSymbolValue + 1)
8967+		return ERROR(GENERIC);
8968+
8969+	return (out - ostart);
8970+}
8971+
8972+size_t FSE_writeNCount(void *buffer, size_t bufferSize, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
8973+{
8974+	if (tableLog > FSE_MAX_TABLELOG)
8975+		return ERROR(tableLog_tooLarge); /* Unsupported */
8976+	if (tableLog < FSE_MIN_TABLELOG)
8977+		return ERROR(GENERIC); /* Unsupported */
8978+
8979+	if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog))
8980+		return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);
8981+
8982+	return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1);
8983+}
8984+
8985+/*-**************************************************************
8986+*  Counting histogram
8987+****************************************************************/
8988+/*! FSE_count_simple
8989+	This function counts byte values within `src`, and store the histogram into table `count`.
8990+	It doesn't use any additional memory.
8991+	But this function is unsafe : it doesn't check that all values within `src` can fit into `count`.
8992+	For this reason, prefer using a table `count` with 256 elements.
8993+	@return : count of most numerous element
8994+*/
8995+size_t FSE_count_simple(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize)
8996+{
8997+	const BYTE *ip = (const BYTE *)src;
8998+	const BYTE *const end = ip + srcSize;
8999+	unsigned maxSymbolValue = *maxSymbolValuePtr;
9000+	unsigned max = 0;
9001+
9002+	memset(count, 0, (maxSymbolValue + 1) * sizeof(*count));
9003+	if (srcSize == 0) {
9004+		*maxSymbolValuePtr = 0;
9005+		return 0;
9006+	}
9007+
9008+	while (ip < end)
9009+		count[*ip++]++;
9010+
9011+	while (!count[maxSymbolValue])
9012+		maxSymbolValue--;
9013+	*maxSymbolValuePtr = maxSymbolValue;
9014+
9015+	{
9016+		U32 s;
9017+		for (s = 0; s <= maxSymbolValue; s++)
9018+			if (count[s] > max)
9019+				max = count[s];
9020+	}
9021+
9022+	return (size_t)max;
9023+}
9024+
9025+/* FSE_count_parallel_wksp() :
9026+ * Same as FSE_count_parallel(), but using an externally provided scratch buffer.
9027+ * `workSpace` size must be a minimum of `1024 * sizeof(unsigned)`` */
9028+static size_t FSE_count_parallel_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, unsigned checkMax,
9029+				      unsigned *const workSpace)
9030+{
9031+	const BYTE *ip = (const BYTE *)source;
9032+	const BYTE *const iend = ip + sourceSize;
9033+	unsigned maxSymbolValue = *maxSymbolValuePtr;
9034+	unsigned max = 0;
9035+	U32 *const Counting1 = workSpace;
9036+	U32 *const Counting2 = Counting1 + 256;
9037+	U32 *const Counting3 = Counting2 + 256;
9038+	U32 *const Counting4 = Counting3 + 256;
9039+
9040+	memset(Counting1, 0, 4 * 256 * sizeof(unsigned));
9041+
9042+	/* safety checks */
9043+	if (!sourceSize) {
9044+		memset(count, 0, maxSymbolValue + 1);
9045+		*maxSymbolValuePtr = 0;
9046+		return 0;
9047+	}
9048+	if (!maxSymbolValue)
9049+		maxSymbolValue = 255; /* 0 == default */
9050+
9051+	/* by stripes of 16 bytes */
9052+	{
9053+		U32 cached = ZSTD_read32(ip);
9054+		ip += 4;
9055+		while (ip < iend - 15) {
9056+			U32 c = cached;
9057+			cached = ZSTD_read32(ip);
9058+			ip += 4;
9059+			Counting1[(BYTE)c]++;
9060+			Counting2[(BYTE)(c >> 8)]++;
9061+			Counting3[(BYTE)(c >> 16)]++;
9062+			Counting4[c >> 24]++;
9063+			c = cached;
9064+			cached = ZSTD_read32(ip);
9065+			ip += 4;
9066+			Counting1[(BYTE)c]++;
9067+			Counting2[(BYTE)(c >> 8)]++;
9068+			Counting3[(BYTE)(c >> 16)]++;
9069+			Counting4[c >> 24]++;
9070+			c = cached;
9071+			cached = ZSTD_read32(ip);
9072+			ip += 4;
9073+			Counting1[(BYTE)c]++;
9074+			Counting2[(BYTE)(c >> 8)]++;
9075+			Counting3[(BYTE)(c >> 16)]++;
9076+			Counting4[c >> 24]++;
9077+			c = cached;
9078+			cached = ZSTD_read32(ip);
9079+			ip += 4;
9080+			Counting1[(BYTE)c]++;
9081+			Counting2[(BYTE)(c >> 8)]++;
9082+			Counting3[(BYTE)(c >> 16)]++;
9083+			Counting4[c >> 24]++;
9084+		}
9085+		ip -= 4;
9086+	}
9087+
9088+	/* finish last symbols */
9089+	while (ip < iend)
9090+		Counting1[*ip++]++;
9091+
9092+	if (checkMax) { /* verify stats will fit into destination table */
9093+		U32 s;
9094+		for (s = 255; s > maxSymbolValue; s--) {
9095+			Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
9096+			if (Counting1[s])
9097+				return ERROR(maxSymbolValue_tooSmall);
9098+		}
9099+	}
9100+
9101+	{
9102+		U32 s;
9103+		for (s = 0; s <= maxSymbolValue; s++) {
9104+			count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s];
9105+			if (count[s] > max)
9106+				max = count[s];
9107+		}
9108+	}
9109+
9110+	while (!count[maxSymbolValue])
9111+		maxSymbolValue--;
9112+	*maxSymbolValuePtr = maxSymbolValue;
9113+	return (size_t)max;
9114+}
9115+
9116+/* FSE_countFast_wksp() :
9117+ * Same as FSE_countFast(), but using an externally provided scratch buffer.
9118+ * `workSpace` size must be table of >= `1024` unsigned */
9119+size_t FSE_countFast_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, unsigned *workSpace)
9120+{
9121+	if (sourceSize < 1500)
9122+		return FSE_count_simple(count, maxSymbolValuePtr, source, sourceSize);
9123+	return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 0, workSpace);
9124+}
9125+
9126+/* FSE_count_wksp() :
9127+ * Same as FSE_count(), but using an externally provided scratch buffer.
9128+ * `workSpace` size must be table of >= `1024` unsigned */
9129+size_t FSE_count_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, unsigned *workSpace)
9130+{
9131+	if (*maxSymbolValuePtr < 255)
9132+		return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 1, workSpace);
9133+	*maxSymbolValuePtr = 255;
9134+	return FSE_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace);
9135+}
9136+
9137+/*-**************************************************************
9138+*  FSE Compression Code
9139+****************************************************************/
9140+/*! FSE_sizeof_CTable() :
9141+	FSE_CTable is a variable size structure which contains :
9142+	`U16 tableLog;`
9143+	`U16 maxSymbolValue;`
9144+	`U16 nextStateNumber[1 << tableLog];`                         // This size is variable
9145+	`FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1];`  // This size is variable
9146+Allocation is manual (C standard does not support variable-size structures).
9147+*/
9148+size_t FSE_sizeof_CTable(unsigned maxSymbolValue, unsigned tableLog)
9149+{
9150+	if (tableLog > FSE_MAX_TABLELOG)
9151+		return ERROR(tableLog_tooLarge);
9152+	return FSE_CTABLE_SIZE_U32(tableLog, maxSymbolValue) * sizeof(U32);
9153+}
9154+
9155+/* provides the minimum logSize to safely represent a distribution */
9156+static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)
9157+{
9158+	U32 minBitsSrc = BIT_highbit32((U32)(srcSize - 1)) + 1;
9159+	U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2;
9160+	U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols;
9161+	return minBits;
9162+}
9163+
9164+unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
9165+{
9166+	U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus;
9167+	U32 tableLog = maxTableLog;
9168+	U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue);
9169+	if (tableLog == 0)
9170+		tableLog = FSE_DEFAULT_TABLELOG;
9171+	if (maxBitsSrc < tableLog)
9172+		tableLog = maxBitsSrc; /* Accuracy can be reduced */
9173+	if (minBits > tableLog)
9174+		tableLog = minBits; /* Need a minimum to safely represent all symbol values */
9175+	if (tableLog < FSE_MIN_TABLELOG)
9176+		tableLog = FSE_MIN_TABLELOG;
9177+	if (tableLog > FSE_MAX_TABLELOG)
9178+		tableLog = FSE_MAX_TABLELOG;
9179+	return tableLog;
9180+}
9181+
9182+unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
9183+{
9184+	return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2);
9185+}
9186+
9187+/* Secondary normalization method.
9188+   To be used when primary method fails. */
9189+
9190+static size_t FSE_normalizeM2(short *norm, U32 tableLog, const unsigned *count, size_t total, U32 maxSymbolValue)
9191+{
9192+	short const NOT_YET_ASSIGNED = -2;
9193+	U32 s;
9194+	U32 distributed = 0;
9195+	U32 ToDistribute;
9196+
9197+	/* Init */
9198+	U32 const lowThreshold = (U32)(total >> tableLog);
9199+	U32 lowOne = (U32)((total * 3) >> (tableLog + 1));
9200+
9201+	for (s = 0; s <= maxSymbolValue; s++) {
9202+		if (count[s] == 0) {
9203+			norm[s] = 0;
9204+			continue;
9205+		}
9206+		if (count[s] <= lowThreshold) {
9207+			norm[s] = -1;
9208+			distributed++;
9209+			total -= count[s];
9210+			continue;
9211+		}
9212+		if (count[s] <= lowOne) {
9213+			norm[s] = 1;
9214+			distributed++;
9215+			total -= count[s];
9216+			continue;
9217+		}
9218+
9219+		norm[s] = NOT_YET_ASSIGNED;
9220+	}
9221+	ToDistribute = (1 << tableLog) - distributed;
9222+
9223+	if ((total / ToDistribute) > lowOne) {
9224+		/* risk of rounding to zero */
9225+		lowOne = (U32)((total * 3) / (ToDistribute * 2));
9226+		for (s = 0; s <= maxSymbolValue; s++) {
9227+			if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) {
9228+				norm[s] = 1;
9229+				distributed++;
9230+				total -= count[s];
9231+				continue;
9232+			}
9233+		}
9234+		ToDistribute = (1 << tableLog) - distributed;
9235+	}
9236+
9237+	if (distributed == maxSymbolValue + 1) {
9238+		/* all values are pretty poor;
9239+		   probably incompressible data (should have already been detected);
9240+		   find max, then give all remaining points to max */
9241+		U32 maxV = 0, maxC = 0;
9242+		for (s = 0; s <= maxSymbolValue; s++)
9243+			if (count[s] > maxC)
9244+				maxV = s, maxC = count[s];
9245+		norm[maxV] += (short)ToDistribute;
9246+		return 0;
9247+	}
9248+
9249+	if (total == 0) {
9250+		/* all of the symbols were low enough for the lowOne or lowThreshold */
9251+		for (s = 0; ToDistribute > 0; s = (s + 1) % (maxSymbolValue + 1))
9252+			if (norm[s] > 0)
9253+				ToDistribute--, norm[s]++;
9254+		return 0;
9255+	}
9256+
9257+	{
9258+		U64 const vStepLog = 62 - tableLog;
9259+		U64 const mid = (1ULL << (vStepLog - 1)) - 1;
9260+		U64 const rStep = div_u64((((U64)1 << vStepLog) * ToDistribute) + mid, (U32)total); /* scale on remaining */
9261+		U64 tmpTotal = mid;
9262+		for (s = 0; s <= maxSymbolValue; s++) {
9263+			if (norm[s] == NOT_YET_ASSIGNED) {
9264+				U64 const end = tmpTotal + (count[s] * rStep);
9265+				U32 const sStart = (U32)(tmpTotal >> vStepLog);
9266+				U32 const sEnd = (U32)(end >> vStepLog);
9267+				U32 const weight = sEnd - sStart;
9268+				if (weight < 1)
9269+					return ERROR(GENERIC);
9270+				norm[s] = (short)weight;
9271+				tmpTotal = end;
9272+			}
9273+		}
9274+	}
9275+
9276+	return 0;
9277+}
9278+
9279+size_t FSE_normalizeCount(short *normalizedCounter, unsigned tableLog, const unsigned *count, size_t total, unsigned maxSymbolValue)
9280+{
9281+	/* Sanity checks */
9282+	if (tableLog == 0)
9283+		tableLog = FSE_DEFAULT_TABLELOG;
9284+	if (tableLog < FSE_MIN_TABLELOG)
9285+		return ERROR(GENERIC); /* Unsupported size */
9286+	if (tableLog > FSE_MAX_TABLELOG)
9287+		return ERROR(tableLog_tooLarge); /* Unsupported size */
9288+	if (tableLog < FSE_minTableLog(total, maxSymbolValue))
9289+		return ERROR(GENERIC); /* Too small tableLog, compression potentially impossible */
9290+
9291+	{
9292+		U32 const rtbTable[] = {0, 473195, 504333, 520860, 550000, 700000, 750000, 830000};
9293+		U64 const scale = 62 - tableLog;
9294+		U64 const step = div_u64((U64)1 << 62, (U32)total); /* <== here, one division ! */
9295+		U64 const vStep = 1ULL << (scale - 20);
9296+		int stillToDistribute = 1 << tableLog;
9297+		unsigned s;
9298+		unsigned largest = 0;
9299+		short largestP = 0;
9300+		U32 lowThreshold = (U32)(total >> tableLog);
9301+
9302+		for (s = 0; s <= maxSymbolValue; s++) {
9303+			if (count[s] == total)
9304+				return 0; /* rle special case */
9305+			if (count[s] == 0) {
9306+				normalizedCounter[s] = 0;
9307+				continue;
9308+			}
9309+			if (count[s] <= lowThreshold) {
9310+				normalizedCounter[s] = -1;
9311+				stillToDistribute--;
9312+			} else {
9313+				short proba = (short)((count[s] * step) >> scale);
9314+				if (proba < 8) {
9315+					U64 restToBeat = vStep * rtbTable[proba];
9316+					proba += (count[s] * step) - ((U64)proba << scale) > restToBeat;
9317+				}
9318+				if (proba > largestP)
9319+					largestP = proba, largest = s;
9320+				normalizedCounter[s] = proba;
9321+				stillToDistribute -= proba;
9322+			}
9323+		}
9324+		if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
9325+			/* corner case, need another normalization method */
9326+			size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
9327+			if (FSE_isError(errorCode))
9328+				return errorCode;
9329+		} else
9330+			normalizedCounter[largest] += (short)stillToDistribute;
9331+	}
9332+
9333+	return tableLog;
9334+}
9335+
9336+/* fake FSE_CTable, for raw (uncompressed) input */
9337+size_t FSE_buildCTable_raw(FSE_CTable *ct, unsigned nbBits)
9338+{
9339+	const unsigned tableSize = 1 << nbBits;
9340+	const unsigned tableMask = tableSize - 1;
9341+	const unsigned maxSymbolValue = tableMask;
9342+	void *const ptr = ct;
9343+	U16 *const tableU16 = ((U16 *)ptr) + 2;
9344+	void *const FSCT = ((U32 *)ptr) + 1 /* header */ + (tableSize >> 1); /* assumption : tableLog >= 1 */
9345+	FSE_symbolCompressionTransform *const symbolTT = (FSE_symbolCompressionTransform *)(FSCT);
9346+	unsigned s;
9347+
9348+	/* Sanity checks */
9349+	if (nbBits < 1)
9350+		return ERROR(GENERIC); /* min size */
9351+
9352+	/* header */
9353+	tableU16[-2] = (U16)nbBits;
9354+	tableU16[-1] = (U16)maxSymbolValue;
9355+
9356+	/* Build table */
9357+	for (s = 0; s < tableSize; s++)
9358+		tableU16[s] = (U16)(tableSize + s);
9359+
9360+	/* Build Symbol Transformation Table */
9361+	{
9362+		const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);
9363+		for (s = 0; s <= maxSymbolValue; s++) {
9364+			symbolTT[s].deltaNbBits = deltaNbBits;
9365+			symbolTT[s].deltaFindState = s - 1;
9366+		}
9367+	}
9368+
9369+	return 0;
9370+}
9371+
9372+/* fake FSE_CTable, for rle input (always same symbol) */
9373+size_t FSE_buildCTable_rle(FSE_CTable *ct, BYTE symbolValue)
9374+{
9375+	void *ptr = ct;
9376+	U16 *tableU16 = ((U16 *)ptr) + 2;
9377+	void *FSCTptr = (U32 *)ptr + 2;
9378+	FSE_symbolCompressionTransform *symbolTT = (FSE_symbolCompressionTransform *)FSCTptr;
9379+
9380+	/* header */
9381+	tableU16[-2] = (U16)0;
9382+	tableU16[-1] = (U16)symbolValue;
9383+
9384+	/* Build table */
9385+	tableU16[0] = 0;
9386+	tableU16[1] = 0; /* just in case */
9387+
9388+	/* Build Symbol Transformation Table */
9389+	symbolTT[symbolValue].deltaNbBits = 0;
9390+	symbolTT[symbolValue].deltaFindState = 0;
9391+
9392+	return 0;
9393+}
9394+
9395+static size_t FSE_compress_usingCTable_generic(void *dst, size_t dstSize, const void *src, size_t srcSize, const FSE_CTable *ct, const unsigned fast)
9396+{
9397+	const BYTE *const istart = (const BYTE *)src;
9398+	const BYTE *const iend = istart + srcSize;
9399+	const BYTE *ip = iend;
9400+
9401+	BIT_CStream_t bitC;
9402+	FSE_CState_t CState1, CState2;
9403+
9404+	/* init */
9405+	if (srcSize <= 2)
9406+		return 0;
9407+	{
9408+		size_t const initError = BIT_initCStream(&bitC, dst, dstSize);
9409+		if (FSE_isError(initError))
9410+			return 0; /* not enough space available to write a bitstream */
9411+	}
9412+
9413+#define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
9414+
9415+	if (srcSize & 1) {
9416+		FSE_initCState2(&CState1, ct, *--ip);
9417+		FSE_initCState2(&CState2, ct, *--ip);
9418+		FSE_encodeSymbol(&bitC, &CState1, *--ip);
9419+		FSE_FLUSHBITS(&bitC);
9420+	} else {
9421+		FSE_initCState2(&CState2, ct, *--ip);
9422+		FSE_initCState2(&CState1, ct, *--ip);
9423+	}
9424+
9425+	/* join to mod 4 */
9426+	srcSize -= 2;
9427+	if ((sizeof(bitC.bitContainer) * 8 > FSE_MAX_TABLELOG * 4 + 7) && (srcSize & 2)) { /* test bit 2 */
9428+		FSE_encodeSymbol(&bitC, &CState2, *--ip);
9429+		FSE_encodeSymbol(&bitC, &CState1, *--ip);
9430+		FSE_FLUSHBITS(&bitC);
9431+	}
9432+
9433+	/* 2 or 4 encoding per loop */
9434+	while (ip > istart) {
9435+
9436+		FSE_encodeSymbol(&bitC, &CState2, *--ip);
9437+
9438+		if (sizeof(bitC.bitContainer) * 8 < FSE_MAX_TABLELOG * 2 + 7) /* this test must be static */
9439+			FSE_FLUSHBITS(&bitC);
9440+
9441+		FSE_encodeSymbol(&bitC, &CState1, *--ip);
9442+
9443+		if (sizeof(bitC.bitContainer) * 8 > FSE_MAX_TABLELOG * 4 + 7) { /* this test must be static */
9444+			FSE_encodeSymbol(&bitC, &CState2, *--ip);
9445+			FSE_encodeSymbol(&bitC, &CState1, *--ip);
9446+		}
9447+
9448+		FSE_FLUSHBITS(&bitC);
9449+	}
9450+
9451+	FSE_flushCState(&bitC, &CState2);
9452+	FSE_flushCState(&bitC, &CState1);
9453+	return BIT_closeCStream(&bitC);
9454+}
9455+
9456+size_t FSE_compress_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const FSE_CTable *ct)
9457+{
9458+	unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize));
9459+
9460+	if (fast)
9461+		return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1);
9462+	else
9463+		return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0);
9464+}
9465+
9466+size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }
9467diff --git a/lib/zstd/fse_decompress.c b/lib/zstd/fse_decompress.c
9468new file mode 100644
9469index 0000000..a84300e
9470--- /dev/null
9471+++ b/lib/zstd/fse_decompress.c
9472@@ -0,0 +1,332 @@
9473+/*
9474+ * FSE : Finite State Entropy decoder
9475+ * Copyright (C) 2013-2015, Yann Collet.
9476+ *
9477+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
9478+ *
9479+ * Redistribution and use in source and binary forms, with or without
9480+ * modification, are permitted provided that the following conditions are
9481+ * met:
9482+ *
9483+ *   * Redistributions of source code must retain the above copyright
9484+ * notice, this list of conditions and the following disclaimer.
9485+ *   * Redistributions in binary form must reproduce the above
9486+ * copyright notice, this list of conditions and the following disclaimer
9487+ * in the documentation and/or other materials provided with the
9488+ * distribution.
9489+ *
9490+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9491+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
9492+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9493+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9494+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9495+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9496+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9497+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9498+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9499+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
9500+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9501+ *
9502+ * This program is free software; you can redistribute it and/or modify it under
9503+ * the terms of the GNU General Public License version 2 as published by the
9504+ * Free Software Foundation. This program is dual-licensed; you may select
9505+ * either version 2 of the GNU General Public License ("GPL") or BSD license
9506+ * ("BSD").
9507+ *
9508+ * You can contact the author at :
9509+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
9510+ */
9511+
9512+/* **************************************************************
9513+*  Compiler specifics
9514+****************************************************************/
9515+#define FORCE_INLINE static __always_inline
9516+
9517+/* **************************************************************
9518+*  Includes
9519+****************************************************************/
9520+#include "bitstream.h"
9521+#include "fse.h"
9522+#include <linux/compiler.h>
9523+#include <linux/kernel.h>
9524+#include <linux/string.h> /* memcpy, memset */
9525+
9526+/* **************************************************************
9527+*  Error Management
9528+****************************************************************/
9529+#define FSE_isError ERR_isError
9530+#define FSE_STATIC_ASSERT(c)                                   \
9531+	{                                                      \
9532+		enum { FSE_static_assert = 1 / (int)(!!(c)) }; \
9533+	} /* use only *after* variable declarations */
9534+
9535+/* check and forward error code */
9536+#define CHECK_F(f)                  \
9537+	{                           \
9538+		size_t const e = f; \
9539+		if (FSE_isError(e)) \
9540+			return e;   \
9541+	}
9542+
9543+/* **************************************************************
9544+*  Templates
9545+****************************************************************/
9546+/*
9547+  designed to be included
9548+  for type-specific functions (template emulation in C)
9549+  Objective is to write these functions only once, for improved maintenance
9550+*/
9551+
9552+/* safety checks */
9553+#ifndef FSE_FUNCTION_EXTENSION
9554+#error "FSE_FUNCTION_EXTENSION must be defined"
9555+#endif
9556+#ifndef FSE_FUNCTION_TYPE
9557+#error "FSE_FUNCTION_TYPE must be defined"
9558+#endif
9559+
9560+/* Function names */
9561+#define FSE_CAT(X, Y) X##Y
9562+#define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y)
9563+#define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y)
9564+
9565+/* Function templates */
9566+
9567+size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize)
9568+{
9569+	void *const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
9570+	FSE_DECODE_TYPE *const tableDecode = (FSE_DECODE_TYPE *)(tdPtr);
9571+	U16 *symbolNext = (U16 *)workspace;
9572+
9573+	U32 const maxSV1 = maxSymbolValue + 1;
9574+	U32 const tableSize = 1 << tableLog;
9575+	U32 highThreshold = tableSize - 1;
9576+
9577+	/* Sanity Checks */
9578+	if (workspaceSize < sizeof(U16) * (FSE_MAX_SYMBOL_VALUE + 1))
9579+		return ERROR(tableLog_tooLarge);
9580+	if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE)
9581+		return ERROR(maxSymbolValue_tooLarge);
9582+	if (tableLog > FSE_MAX_TABLELOG)
9583+		return ERROR(tableLog_tooLarge);
9584+
9585+	/* Init, lay down lowprob symbols */
9586+	{
9587+		FSE_DTableHeader DTableH;
9588+		DTableH.tableLog = (U16)tableLog;
9589+		DTableH.fastMode = 1;
9590+		{
9591+			S16 const largeLimit = (S16)(1 << (tableLog - 1));
9592+			U32 s;
9593+			for (s = 0; s < maxSV1; s++) {
9594+				if (normalizedCounter[s] == -1) {
9595+					tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
9596+					symbolNext[s] = 1;
9597+				} else {
9598+					if (normalizedCounter[s] >= largeLimit)
9599+						DTableH.fastMode = 0;
9600+					symbolNext[s] = normalizedCounter[s];
9601+				}
9602+			}
9603+		}
9604+		memcpy(dt, &DTableH, sizeof(DTableH));
9605+	}
9606+
9607+	/* Spread symbols */
9608+	{
9609+		U32 const tableMask = tableSize - 1;
9610+		U32 const step = FSE_TABLESTEP(tableSize);
9611+		U32 s, position = 0;
9612+		for (s = 0; s < maxSV1; s++) {
9613+			int i;
9614+			for (i = 0; i < normalizedCounter[s]; i++) {
9615+				tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
9616+				position = (position + step) & tableMask;
9617+				while (position > highThreshold)
9618+					position = (position + step) & tableMask; /* lowprob area */
9619+			}
9620+		}
9621+		if (position != 0)
9622+			return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
9623+	}
9624+
9625+	/* Build Decoding table */
9626+	{
9627+		U32 u;
9628+		for (u = 0; u < tableSize; u++) {
9629+			FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
9630+			U16 nextState = symbolNext[symbol]++;
9631+			tableDecode[u].nbBits = (BYTE)(tableLog - BIT_highbit32((U32)nextState));
9632+			tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize);
9633+		}
9634+	}
9635+
9636+	return 0;
9637+}
9638+
9639+/*-*******************************************************
9640+*  Decompression (Byte symbols)
9641+*********************************************************/
9642+size_t FSE_buildDTable_rle(FSE_DTable *dt, BYTE symbolValue)
9643+{
9644+	void *ptr = dt;
9645+	FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
9646+	void *dPtr = dt + 1;
9647+	FSE_decode_t *const cell = (FSE_decode_t *)dPtr;
9648+
9649+	DTableH->tableLog = 0;
9650+	DTableH->fastMode = 0;
9651+
9652+	cell->newState = 0;
9653+	cell->symbol = symbolValue;
9654+	cell->nbBits = 0;
9655+
9656+	return 0;
9657+}
9658+
9659+size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits)
9660+{
9661+	void *ptr = dt;
9662+	FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
9663+	void *dPtr = dt + 1;
9664+	FSE_decode_t *const dinfo = (FSE_decode_t *)dPtr;
9665+	const unsigned tableSize = 1 << nbBits;
9666+	const unsigned tableMask = tableSize - 1;
9667+	const unsigned maxSV1 = tableMask + 1;
9668+	unsigned s;
9669+
9670+	/* Sanity checks */
9671+	if (nbBits < 1)
9672+		return ERROR(GENERIC); /* min size */
9673+
9674+	/* Build Decoding Table */
9675+	DTableH->tableLog = (U16)nbBits;
9676+	DTableH->fastMode = 1;
9677+	for (s = 0; s < maxSV1; s++) {
9678+		dinfo[s].newState = 0;
9679+		dinfo[s].symbol = (BYTE)s;
9680+		dinfo[s].nbBits = (BYTE)nbBits;
9681+	}
9682+
9683+	return 0;
9684+}
9685+
9686+FORCE_INLINE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt,
9687+						       const unsigned fast)
9688+{
9689+	BYTE *const ostart = (BYTE *)dst;
9690+	BYTE *op = ostart;
9691+	BYTE *const omax = op + maxDstSize;
9692+	BYTE *const olimit = omax - 3;
9693+
9694+	BIT_DStream_t bitD;
9695+	FSE_DState_t state1;
9696+	FSE_DState_t state2;
9697+
9698+	/* Init */
9699+	CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
9700+
9701+	FSE_initDState(&state1, &bitD, dt);
9702+	FSE_initDState(&state2, &bitD, dt);
9703+
9704+#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
9705+
9706+	/* 4 symbols per loop */
9707+	for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) {
9708+		op[0] = FSE_GETSYMBOL(&state1);
9709+
9710+		if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
9711+			BIT_reloadDStream(&bitD);
9712+
9713+		op[1] = FSE_GETSYMBOL(&state2);
9714+
9715+		if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
9716+		{
9717+			if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) {
9718+				op += 2;
9719+				break;
9720+			}
9721+		}
9722+
9723+		op[2] = FSE_GETSYMBOL(&state1);
9724+
9725+		if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
9726+			BIT_reloadDStream(&bitD);
9727+
9728+		op[3] = FSE_GETSYMBOL(&state2);
9729+	}
9730+
9731+	/* tail */
9732+	/* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
9733+	while (1) {
9734+		if (op > (omax - 2))
9735+			return ERROR(dstSize_tooSmall);
9736+		*op++ = FSE_GETSYMBOL(&state1);
9737+		if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
9738+			*op++ = FSE_GETSYMBOL(&state2);
9739+			break;
9740+		}
9741+
9742+		if (op > (omax - 2))
9743+			return ERROR(dstSize_tooSmall);
9744+		*op++ = FSE_GETSYMBOL(&state2);
9745+		if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
9746+			*op++ = FSE_GETSYMBOL(&state1);
9747+			break;
9748+		}
9749+	}
9750+
9751+	return op - ostart;
9752+}
9753+
9754+size_t FSE_decompress_usingDTable(void *dst, size_t originalSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt)
9755+{
9756+	const void *ptr = dt;
9757+	const FSE_DTableHeader *DTableH = (const FSE_DTableHeader *)ptr;
9758+	const U32 fastMode = DTableH->fastMode;
9759+
9760+	/* select fast mode (static) */
9761+	if (fastMode)
9762+		return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
9763+	return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
9764+}
9765+
9766+size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize)
9767+{
9768+	const BYTE *const istart = (const BYTE *)cSrc;
9769+	const BYTE *ip = istart;
9770+	unsigned tableLog;
9771+	unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
9772+	size_t NCountLength;
9773+
9774+	FSE_DTable *dt;
9775+	short *counting;
9776+	size_t spaceUsed32 = 0;
9777+
9778+	FSE_STATIC_ASSERT(sizeof(FSE_DTable) == sizeof(U32));
9779+
9780+	dt = (FSE_DTable *)((U32 *)workspace + spaceUsed32);
9781+	spaceUsed32 += FSE_DTABLE_SIZE_U32(maxLog);
9782+	counting = (short *)((U32 *)workspace + spaceUsed32);
9783+	spaceUsed32 += ALIGN(sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1), sizeof(U32)) >> 2;
9784+
9785+	if ((spaceUsed32 << 2) > workspaceSize)
9786+		return ERROR(tableLog_tooLarge);
9787+	workspace = (U32 *)workspace + spaceUsed32;
9788+	workspaceSize -= (spaceUsed32 << 2);
9789+
9790+	/* normal FSE decoding mode */
9791+	NCountLength = FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
9792+	if (FSE_isError(NCountLength))
9793+		return NCountLength;
9794+	// if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong);   /* too small input size; supposed to be already checked in NCountLength, only remaining
9795+	// case : NCountLength==cSrcSize */
9796+	if (tableLog > maxLog)
9797+		return ERROR(tableLog_tooLarge);
9798+	ip += NCountLength;
9799+	cSrcSize -= NCountLength;
9800+
9801+	CHECK_F(FSE_buildDTable_wksp(dt, counting, maxSymbolValue, tableLog, workspace, workspaceSize));
9802+
9803+	return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, dt); /* always return, even if it is an error code */
9804+}
9805diff --git a/lib/zstd/huf.h b/lib/zstd/huf.h
9806new file mode 100644
9807index 0000000..2143da2
9808--- /dev/null
9809+++ b/lib/zstd/huf.h
9810@@ -0,0 +1,212 @@
9811+/*
9812+ * Huffman coder, part of New Generation Entropy library
9813+ * header file
9814+ * Copyright (C) 2013-2016, Yann Collet.
9815+ *
9816+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
9817+ *
9818+ * Redistribution and use in source and binary forms, with or without
9819+ * modification, are permitted provided that the following conditions are
9820+ * met:
9821+ *
9822+ *   * Redistributions of source code must retain the above copyright
9823+ * notice, this list of conditions and the following disclaimer.
9824+ *   * Redistributions in binary form must reproduce the above
9825+ * copyright notice, this list of conditions and the following disclaimer
9826+ * in the documentation and/or other materials provided with the
9827+ * distribution.
9828+ *
9829+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9830+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
9831+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9832+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9833+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9834+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9835+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9836+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9837+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9838+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
9839+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9840+ *
9841+ * This program is free software; you can redistribute it and/or modify it under
9842+ * the terms of the GNU General Public License version 2 as published by the
9843+ * Free Software Foundation. This program is dual-licensed; you may select
9844+ * either version 2 of the GNU General Public License ("GPL") or BSD license
9845+ * ("BSD").
9846+ *
9847+ * You can contact the author at :
9848+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
9849+ */
9850+#ifndef HUF_H_298734234
9851+#define HUF_H_298734234
9852+
9853+/* *** Dependencies *** */
9854+#include <linux/types.h> /* size_t */
9855+
9856+/* ***   Tool functions *** */
9857+#define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
9858+size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
9859+
9860+/* Error Management */
9861+unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
9862+
9863+/* ***   Advanced function   *** */
9864+
9865+/** HUF_compress4X_wksp() :
9866+*   Same as HUF_compress2(), but uses externally allocated `workSpace`, which must be a table of >= 1024 unsigned */
9867+size_t HUF_compress4X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
9868+			   size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
9869+
9870+/* *** Dependencies *** */
9871+#include "mem.h" /* U32 */
9872+
9873+/* *** Constants *** */
9874+#define HUF_TABLELOG_MAX 12     /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
9875+#define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */
9876+#define HUF_SYMBOLVALUE_MAX 255
9877+
9878+#define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
9879+#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
9880+#error "HUF_TABLELOG_MAX is too large !"
9881+#endif
9882+
9883+/* ****************************************
9884+*  Static allocation
9885+******************************************/
9886+/* HUF buffer bounds */
9887+#define HUF_CTABLEBOUND 129
9888+#define HUF_BLOCKBOUND(size) (size + (size >> 8) + 8)			 /* only true if incompressible pre-filtered with fast heuristic */
9889+#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
9890+
9891+/* static allocation of HUF's Compression Table */
9892+#define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
9893+	U32 name##hb[maxSymbolValue + 1];              \
9894+	void *name##hv = &(name##hb);                  \
9895+	HUF_CElt *name = (HUF_CElt *)(name##hv) /* no final ; */
9896+
9897+/* static allocation of HUF's DTable */
9898+typedef U32 HUF_DTable;
9899+#define HUF_DTABLE_SIZE(maxTableLog) (1 + (1 << (maxTableLog)))
9900+#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = {((U32)((maxTableLog)-1) * 0x01000001)}
9901+#define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = {((U32)(maxTableLog)*0x01000001)}
9902+
9903+/* The workspace must have alignment at least 4 and be at least this large */
9904+#define HUF_COMPRESS_WORKSPACE_SIZE (6 << 10)
9905+#define HUF_COMPRESS_WORKSPACE_SIZE_U32 (HUF_COMPRESS_WORKSPACE_SIZE / sizeof(U32))
9906+
9907+/* The workspace must have alignment at least 4 and be at least this large */
9908+#define HUF_DECOMPRESS_WORKSPACE_SIZE (3 << 10)
9909+#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
9910+
9911+/* ****************************************
9912+*  Advanced decompression functions
9913+******************************************/
9914+size_t HUF_decompress4X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize); /**< decodes RLE and uncompressed */
9915+size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
9916+				size_t workspaceSize);							       /**< considers RLE and uncompressed as errors */
9917+size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
9918+				   size_t workspaceSize); /**< single-symbol decoder */
9919+size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
9920+				   size_t workspaceSize); /**< double-symbols decoder */
9921+
9922+/* ****************************************
9923+*  HUF detailed API
9924+******************************************/
9925+/*!
9926+HUF_compress() does the following:
9927+1. count symbol occurrence from source[] into table count[] using FSE_count()
9928+2. (optional) refine tableLog using HUF_optimalTableLog()
9929+3. build Huffman table from count using HUF_buildCTable()
9930+4. save Huffman table to memory buffer using HUF_writeCTable_wksp()
9931+5. encode the data stream using HUF_compress4X_usingCTable()
9932+
9933+The following API allows targeting specific sub-functions for advanced tasks.
9934+For example, it's possible to compress several blocks using the same 'CTable',
9935+or to save and regenerate 'CTable' using external methods.
9936+*/
9937+/* FSE_count() : find it within "fse.h" */
9938+unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
9939+typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
9940+size_t HUF_writeCTable_wksp(void *dst, size_t maxDstSize, const HUF_CElt *CTable, unsigned maxSymbolValue, unsigned huffLog, void *workspace, size_t workspaceSize);
9941+size_t HUF_compress4X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable);
9942+
9943+typedef enum {
9944+	HUF_repeat_none,  /**< Cannot use the previous table */
9945+	HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1,
9946+			     4}X_repeat */
9947+	HUF_repeat_valid  /**< Can use the previous table and it is assumed to be valid */
9948+} HUF_repeat;
9949+/** HUF_compress4X_repeat() :
9950+*   Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
9951+*   If it uses hufTable it does not modify hufTable or repeat.
9952+*   If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
9953+*   If preferRepeat then the old table will always be used if valid. */
9954+size_t HUF_compress4X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
9955+			     size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat,
9956+			     int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
9957+
9958+/** HUF_buildCTable_wksp() :
9959+ *  Same as HUF_buildCTable(), but using externally allocated scratch buffer.
9960+ *  `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned.
9961+ */
9962+size_t HUF_buildCTable_wksp(HUF_CElt *tree, const U32 *count, U32 maxSymbolValue, U32 maxNbBits, void *workSpace, size_t wkspSize);
9963+
9964+/*! HUF_readStats() :
9965+	Read compact Huffman tree, saved by HUF_writeCTable().
9966+	`huffWeight` is destination buffer.
9967+	@return : size read from `src` , or an error Code .
9968+	Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
9969+size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize,
9970+			  void *workspace, size_t workspaceSize);
9971+
9972+/** HUF_readCTable() :
9973+*   Loading a CTable saved with HUF_writeCTable() */
9974+size_t HUF_readCTable_wksp(HUF_CElt *CTable, unsigned maxSymbolValue, const void *src, size_t srcSize, void *workspace, size_t workspaceSize);
9975+
9976+/*
9977+HUF_decompress() does the following:
9978+1. select the decompression algorithm (X2, X4) based on pre-computed heuristics
9979+2. build Huffman table from save, using HUF_readDTableXn()
9980+3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable
9981+*/
9982+
9983+/** HUF_selectDecoder() :
9984+*   Tells which decoder is likely to decode faster,
9985+*   based on a set of pre-determined metrics.
9986+*   @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
9987+*   Assumption : 0 < cSrcSize < dstSize <= 128 KB */
9988+U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize);
9989+
9990+size_t HUF_readDTableX2_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize);
9991+size_t HUF_readDTableX4_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize);
9992+
9993+size_t HUF_decompress4X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
9994+size_t HUF_decompress4X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
9995+size_t HUF_decompress4X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
9996+
9997+/* single stream variants */
9998+
9999+size_t HUF_compress1X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
10000+			   size_t wkspSize); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
10001+size_t HUF_compress1X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable);
10002+/** HUF_compress1X_repeat() :
10003+*   Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
10004+*   If it uses hufTable it does not modify hufTable or repeat.
10005+*   If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
10006+*   If preferRepeat then the old table will always be used if valid. */
10007+size_t HUF_compress1X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void *workSpace,
10008+			     size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat,
10009+			     int preferRepeat); /**< `workSpace` must be a table of at least HUF_COMPRESS_WORKSPACE_SIZE_U32 unsigned */
10010+
10011+size_t HUF_decompress1X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize);
10012+size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
10013+				   size_t workspaceSize); /**< single-symbol decoder */
10014+size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace,
10015+				   size_t workspaceSize); /**< double-symbols decoder */
10016+
10017+size_t HUF_decompress1X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize,
10018+				    const HUF_DTable *DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */
10019+size_t HUF_decompress1X2_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
10020+size_t HUF_decompress1X4_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable);
10021+
10022+#endif /* HUF_H_298734234 */
10023diff --git a/lib/zstd/huf_compress.c b/lib/zstd/huf_compress.c
10024new file mode 100644
10025index 0000000..40055a7
10026--- /dev/null
10027+++ b/lib/zstd/huf_compress.c
10028@@ -0,0 +1,770 @@
10029+/*
10030+ * Huffman encoder, part of New Generation Entropy library
10031+ * Copyright (C) 2013-2016, Yann Collet.
10032+ *
10033+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
10034+ *
10035+ * Redistribution and use in source and binary forms, with or without
10036+ * modification, are permitted provided that the following conditions are
10037+ * met:
10038+ *
10039+ *   * Redistributions of source code must retain the above copyright
10040+ * notice, this list of conditions and the following disclaimer.
10041+ *   * Redistributions in binary form must reproduce the above
10042+ * copyright notice, this list of conditions and the following disclaimer
10043+ * in the documentation and/or other materials provided with the
10044+ * distribution.
10045+ *
10046+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10047+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10048+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
10049+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10050+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10051+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10052+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10053+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
10054+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10055+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
10056+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10057+ *
10058+ * This program is free software; you can redistribute it and/or modify it under
10059+ * the terms of the GNU General Public License version 2 as published by the
10060+ * Free Software Foundation. This program is dual-licensed; you may select
10061+ * either version 2 of the GNU General Public License ("GPL") or BSD license
10062+ * ("BSD").
10063+ *
10064+ * You can contact the author at :
10065+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
10066+ */
10067+
10068+/* **************************************************************
10069+*  Includes
10070+****************************************************************/
10071+#include "bitstream.h"
10072+#include "fse.h" /* header compression */
10073+#include "huf.h"
10074+#include <linux/kernel.h>
10075+#include <linux/string.h> /* memcpy, memset */
10076+
10077+/* **************************************************************
10078+*  Error Management
10079+****************************************************************/
10080+#define HUF_STATIC_ASSERT(c)                                   \
10081+	{                                                      \
10082+		enum { HUF_static_assert = 1 / (int)(!!(c)) }; \
10083+	} /* use only *after* variable declarations */
10084+#define CHECK_V_F(e, f)     \
10085+	size_t const e = f; \
10086+	if (ERR_isError(e)) \
10087+	return f
10088+#define CHECK_F(f)                        \
10089+	{                                 \
10090+		CHECK_V_F(_var_err__, f); \
10091+	}
10092+
10093+/* **************************************************************
10094+*  Utils
10095+****************************************************************/
10096+unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
10097+{
10098+	return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1);
10099+}
10100+
10101+/* *******************************************************
10102+*  HUF : Huffman block compression
10103+*********************************************************/
10104+/* HUF_compressWeights() :
10105+ * Same as FSE_compress(), but dedicated to huff0's weights compression.
10106+ * The use case needs much less stack memory.
10107+ * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX.
10108+ */
10109+#define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
10110+size_t HUF_compressWeights_wksp(void *dst, size_t dstSize, const void *weightTable, size_t wtSize, void *workspace, size_t workspaceSize)
10111+{
10112+	BYTE *const ostart = (BYTE *)dst;
10113+	BYTE *op = ostart;
10114+	BYTE *const oend = ostart + dstSize;
10115+
10116+	U32 maxSymbolValue = HUF_TABLELOG_MAX;
10117+	U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER;
10118+
10119+	FSE_CTable *CTable;
10120+	U32 *count;
10121+	S16 *norm;
10122+	size_t spaceUsed32 = 0;
10123+
10124+	HUF_STATIC_ASSERT(sizeof(FSE_CTable) == sizeof(U32));
10125+
10126+	CTable = (FSE_CTable *)((U32 *)workspace + spaceUsed32);
10127+	spaceUsed32 += FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX);
10128+	count = (U32 *)workspace + spaceUsed32;
10129+	spaceUsed32 += HUF_TABLELOG_MAX + 1;
10130+	norm = (S16 *)((U32 *)workspace + spaceUsed32);
10131+	spaceUsed32 += ALIGN(sizeof(S16) * (HUF_TABLELOG_MAX + 1), sizeof(U32)) >> 2;
10132+
10133+	if ((spaceUsed32 << 2) > workspaceSize)
10134+		return ERROR(tableLog_tooLarge);
10135+	workspace = (U32 *)workspace + spaceUsed32;
10136+	workspaceSize -= (spaceUsed32 << 2);
10137+
10138+	/* init conditions */
10139+	if (wtSize <= 1)
10140+		return 0; /* Not compressible */
10141+
10142+	/* Scan input and build symbol stats */
10143+	{
10144+		CHECK_V_F(maxCount, FSE_count_simple(count, &maxSymbolValue, weightTable, wtSize));
10145+		if (maxCount == wtSize)
10146+			return 1; /* only a single symbol in src : rle */
10147+		if (maxCount == 1)
10148+			return 0; /* each symbol present maximum once => not compressible */
10149+	}
10150+
10151+	tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue);
10152+	CHECK_F(FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue));
10153+
10154+	/* Write table description header */
10155+	{
10156+		CHECK_V_F(hSize, FSE_writeNCount(op, oend - op, norm, maxSymbolValue, tableLog));
10157+		op += hSize;
10158+	}
10159+
10160+	/* Compress */
10161+	CHECK_F(FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, workspace, workspaceSize));
10162+	{
10163+		CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, weightTable, wtSize, CTable));
10164+		if (cSize == 0)
10165+			return 0; /* not enough space for compressed data */
10166+		op += cSize;
10167+	}
10168+
10169+	return op - ostart;
10170+}
10171+
10172+struct HUF_CElt_s {
10173+	U16 val;
10174+	BYTE nbBits;
10175+}; /* typedef'd to HUF_CElt within "huf.h" */
10176+
10177+/*! HUF_writeCTable_wksp() :
10178+	`CTable` : Huffman tree to save, using huf representation.
10179+	@return : size of saved CTable */
10180+size_t HUF_writeCTable_wksp(void *dst, size_t maxDstSize, const HUF_CElt *CTable, U32 maxSymbolValue, U32 huffLog, void *workspace, size_t workspaceSize)
10181+{
10182+	BYTE *op = (BYTE *)dst;
10183+	U32 n;
10184+
10185+	BYTE *bitsToWeight;
10186+	BYTE *huffWeight;
10187+	size_t spaceUsed32 = 0;
10188+
10189+	bitsToWeight = (BYTE *)((U32 *)workspace + spaceUsed32);
10190+	spaceUsed32 += ALIGN(HUF_TABLELOG_MAX + 1, sizeof(U32)) >> 2;
10191+	huffWeight = (BYTE *)((U32 *)workspace + spaceUsed32);
10192+	spaceUsed32 += ALIGN(HUF_SYMBOLVALUE_MAX, sizeof(U32)) >> 2;
10193+
10194+	if ((spaceUsed32 << 2) > workspaceSize)
10195+		return ERROR(tableLog_tooLarge);
10196+	workspace = (U32 *)workspace + spaceUsed32;
10197+	workspaceSize -= (spaceUsed32 << 2);
10198+
10199+	/* check conditions */
10200+	if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
10201+		return ERROR(maxSymbolValue_tooLarge);
10202+
10203+	/* convert to weight */
10204+	bitsToWeight[0] = 0;
10205+	for (n = 1; n < huffLog + 1; n++)
10206+		bitsToWeight[n] = (BYTE)(huffLog + 1 - n);
10207+	for (n = 0; n < maxSymbolValue; n++)
10208+		huffWeight[n] = bitsToWeight[CTable[n].nbBits];
10209+
10210+	/* attempt weights compression by FSE */
10211+	{
10212+		CHECK_V_F(hSize, HUF_compressWeights_wksp(op + 1, maxDstSize - 1, huffWeight, maxSymbolValue, workspace, workspaceSize));
10213+		if ((hSize > 1) & (hSize < maxSymbolValue / 2)) { /* FSE compressed */
10214+			op[0] = (BYTE)hSize;
10215+			return hSize + 1;
10216+		}
10217+	}
10218+
10219+	/* write raw values as 4-bits (max : 15) */
10220+	if (maxSymbolValue > (256 - 128))
10221+		return ERROR(GENERIC); /* should not happen : likely means source cannot be compressed */
10222+	if (((maxSymbolValue + 1) / 2) + 1 > maxDstSize)
10223+		return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */
10224+	op[0] = (BYTE)(128 /*special case*/ + (maxSymbolValue - 1));
10225+	huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause msan issue in final combination */
10226+	for (n = 0; n < maxSymbolValue; n += 2)
10227+		op[(n / 2) + 1] = (BYTE)((huffWeight[n] << 4) + huffWeight[n + 1]);
10228+	return ((maxSymbolValue + 1) / 2) + 1;
10229+}
10230+
10231+size_t HUF_readCTable_wksp(HUF_CElt *CTable, U32 maxSymbolValue, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
10232+{
10233+	U32 *rankVal;
10234+	BYTE *huffWeight;
10235+	U32 tableLog = 0;
10236+	U32 nbSymbols = 0;
10237+	size_t readSize;
10238+	size_t spaceUsed32 = 0;
10239+
10240+	rankVal = (U32 *)workspace + spaceUsed32;
10241+	spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
10242+	huffWeight = (BYTE *)((U32 *)workspace + spaceUsed32);
10243+	spaceUsed32 += ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
10244+
10245+	if ((spaceUsed32 << 2) > workspaceSize)
10246+		return ERROR(tableLog_tooLarge);
10247+	workspace = (U32 *)workspace + spaceUsed32;
10248+	workspaceSize -= (spaceUsed32 << 2);
10249+
10250+	/* get symbol weights */
10251+	readSize = HUF_readStats_wksp(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize, workspace, workspaceSize);
10252+	if (ERR_isError(readSize))
10253+		return readSize;
10254+
10255+	/* check result */
10256+	if (tableLog > HUF_TABLELOG_MAX)
10257+		return ERROR(tableLog_tooLarge);
10258+	if (nbSymbols > maxSymbolValue + 1)
10259+		return ERROR(maxSymbolValue_tooSmall);
10260+
10261+	/* Prepare base value per rank */
10262+	{
10263+		U32 n, nextRankStart = 0;
10264+		for (n = 1; n <= tableLog; n++) {
10265+			U32 curr = nextRankStart;
10266+			nextRankStart += (rankVal[n] << (n - 1));
10267+			rankVal[n] = curr;
10268+		}
10269+	}
10270+
10271+	/* fill nbBits */
10272+	{
10273+		U32 n;
10274+		for (n = 0; n < nbSymbols; n++) {
10275+			const U32 w = huffWeight[n];
10276+			CTable[n].nbBits = (BYTE)(tableLog + 1 - w);
10277+		}
10278+	}
10279+
10280+	/* fill val */
10281+	{
10282+		U16 nbPerRank[HUF_TABLELOG_MAX + 2] = {0}; /* support w=0=>n=tableLog+1 */
10283+		U16 valPerRank[HUF_TABLELOG_MAX + 2] = {0};
10284+		{
10285+			U32 n;
10286+			for (n = 0; n < nbSymbols; n++)
10287+				nbPerRank[CTable[n].nbBits]++;
10288+		}
10289+		/* determine stating value per rank */
10290+		valPerRank[tableLog + 1] = 0; /* for w==0 */
10291+		{
10292+			U16 min = 0;
10293+			U32 n;
10294+			for (n = tableLog; n > 0; n--) { /* start at n=tablelog <-> w=1 */
10295+				valPerRank[n] = min;     /* get starting value within each rank */
10296+				min += nbPerRank[n];
10297+				min >>= 1;
10298+			}
10299+		}
10300+		/* assign value within rank, symbol order */
10301+		{
10302+			U32 n;
10303+			for (n = 0; n <= maxSymbolValue; n++)
10304+				CTable[n].val = valPerRank[CTable[n].nbBits]++;
10305+		}
10306+	}
10307+
10308+	return readSize;
10309+}
10310+
10311+typedef struct nodeElt_s {
10312+	U32 count;
10313+	U16 parent;
10314+	BYTE byte;
10315+	BYTE nbBits;
10316+} nodeElt;
10317+
10318+static U32 HUF_setMaxHeight(nodeElt *huffNode, U32 lastNonNull, U32 maxNbBits)
10319+{
10320+	const U32 largestBits = huffNode[lastNonNull].nbBits;
10321+	if (largestBits <= maxNbBits)
10322+		return largestBits; /* early exit : no elt > maxNbBits */
10323+
10324+	/* there are several too large elements (at least >= 2) */
10325+	{
10326+		int totalCost = 0;
10327+		const U32 baseCost = 1 << (largestBits - maxNbBits);
10328+		U32 n = lastNonNull;
10329+
10330+		while (huffNode[n].nbBits > maxNbBits) {
10331+			totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits));
10332+			huffNode[n].nbBits = (BYTE)maxNbBits;
10333+			n--;
10334+		} /* n stops at huffNode[n].nbBits <= maxNbBits */
10335+		while (huffNode[n].nbBits == maxNbBits)
10336+			n--; /* n end at index of smallest symbol using < maxNbBits */
10337+
10338+		/* renorm totalCost */
10339+		totalCost >>= (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */
10340+
10341+		/* repay normalized cost */
10342+		{
10343+			U32 const noSymbol = 0xF0F0F0F0;
10344+			U32 rankLast[HUF_TABLELOG_MAX + 2];
10345+			int pos;
10346+
10347+			/* Get pos of last (smallest) symbol per rank */
10348+			memset(rankLast, 0xF0, sizeof(rankLast));
10349+			{
10350+				U32 currNbBits = maxNbBits;
10351+				for (pos = n; pos >= 0; pos--) {
10352+					if (huffNode[pos].nbBits >= currNbBits)
10353+						continue;
10354+					currNbBits = huffNode[pos].nbBits; /* < maxNbBits */
10355+					rankLast[maxNbBits - currNbBits] = pos;
10356+				}
10357+			}
10358+
10359+			while (totalCost > 0) {
10360+				U32 nBitsToDecrease = BIT_highbit32(totalCost) + 1;
10361+				for (; nBitsToDecrease > 1; nBitsToDecrease--) {
10362+					U32 highPos = rankLast[nBitsToDecrease];
10363+					U32 lowPos = rankLast[nBitsToDecrease - 1];
10364+					if (highPos == noSymbol)
10365+						continue;
10366+					if (lowPos == noSymbol)
10367+						break;
10368+					{
10369+						U32 const highTotal = huffNode[highPos].count;
10370+						U32 const lowTotal = 2 * huffNode[lowPos].count;
10371+						if (highTotal <= lowTotal)
10372+							break;
10373+					}
10374+				}
10375+				/* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */
10376+				/* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */
10377+				while ((nBitsToDecrease <= HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol))
10378+					nBitsToDecrease++;
10379+				totalCost -= 1 << (nBitsToDecrease - 1);
10380+				if (rankLast[nBitsToDecrease - 1] == noSymbol)
10381+					rankLast[nBitsToDecrease - 1] = rankLast[nBitsToDecrease]; /* this rank is no longer empty */
10382+				huffNode[rankLast[nBitsToDecrease]].nbBits++;
10383+				if (rankLast[nBitsToDecrease] == 0) /* special case, reached largest symbol */
10384+					rankLast[nBitsToDecrease] = noSymbol;
10385+				else {
10386+					rankLast[nBitsToDecrease]--;
10387+					if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits - nBitsToDecrease)
10388+						rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */
10389+				}
10390+			} /* while (totalCost > 0) */
10391+
10392+			while (totalCost < 0) {		       /* Sometimes, cost correction overshoot */
10393+				if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0
10394+								  (using maxNbBits) */
10395+					while (huffNode[n].nbBits == maxNbBits)
10396+						n--;
10397+					huffNode[n + 1].nbBits--;
10398+					rankLast[1] = n + 1;
10399+					totalCost++;
10400+					continue;
10401+				}
10402+				huffNode[rankLast[1] + 1].nbBits--;
10403+				rankLast[1]++;
10404+				totalCost++;
10405+			}
10406+		}
10407+	} /* there are several too large elements (at least >= 2) */
10408+
10409+	return maxNbBits;
10410+}
10411+
10412+typedef struct {
10413+	U32 base;
10414+	U32 curr;
10415+} rankPos;
10416+
10417+static void HUF_sort(nodeElt *huffNode, const U32 *count, U32 maxSymbolValue)
10418+{
10419+	rankPos rank[32];
10420+	U32 n;
10421+
10422+	memset(rank, 0, sizeof(rank));
10423+	for (n = 0; n <= maxSymbolValue; n++) {
10424+		U32 r = BIT_highbit32(count[n] + 1);
10425+		rank[r].base++;
10426+	}
10427+	for (n = 30; n > 0; n--)
10428+		rank[n - 1].base += rank[n].base;
10429+	for (n = 0; n < 32; n++)
10430+		rank[n].curr = rank[n].base;
10431+	for (n = 0; n <= maxSymbolValue; n++) {
10432+		U32 const c = count[n];
10433+		U32 const r = BIT_highbit32(c + 1) + 1;
10434+		U32 pos = rank[r].curr++;
10435+		while ((pos > rank[r].base) && (c > huffNode[pos - 1].count))
10436+			huffNode[pos] = huffNode[pos - 1], pos--;
10437+		huffNode[pos].count = c;
10438+		huffNode[pos].byte = (BYTE)n;
10439+	}
10440+}
10441+
10442+/** HUF_buildCTable_wksp() :
10443+ *  Same as HUF_buildCTable(), but using externally allocated scratch buffer.
10444+ *  `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned.
10445+ */
10446+#define STARTNODE (HUF_SYMBOLVALUE_MAX + 1)
10447+typedef nodeElt huffNodeTable[2 * HUF_SYMBOLVALUE_MAX + 1 + 1];
10448+size_t HUF_buildCTable_wksp(HUF_CElt *tree, const U32 *count, U32 maxSymbolValue, U32 maxNbBits, void *workSpace, size_t wkspSize)
10449+{
10450+	nodeElt *const huffNode0 = (nodeElt *)workSpace;
10451+	nodeElt *const huffNode = huffNode0 + 1;
10452+	U32 n, nonNullRank;
10453+	int lowS, lowN;
10454+	U16 nodeNb = STARTNODE;
10455+	U32 nodeRoot;
10456+
10457+	/* safety checks */
10458+	if (wkspSize < sizeof(huffNodeTable))
10459+		return ERROR(GENERIC); /* workSpace is not large enough */
10460+	if (maxNbBits == 0)
10461+		maxNbBits = HUF_TABLELOG_DEFAULT;
10462+	if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
10463+		return ERROR(GENERIC);
10464+	memset(huffNode0, 0, sizeof(huffNodeTable));
10465+
10466+	/* sort, decreasing order */
10467+	HUF_sort(huffNode, count, maxSymbolValue);
10468+
10469+	/* init for parents */
10470+	nonNullRank = maxSymbolValue;
10471+	while (huffNode[nonNullRank].count == 0)
10472+		nonNullRank--;
10473+	lowS = nonNullRank;
10474+	nodeRoot = nodeNb + lowS - 1;
10475+	lowN = nodeNb;
10476+	huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS - 1].count;
10477+	huffNode[lowS].parent = huffNode[lowS - 1].parent = nodeNb;
10478+	nodeNb++;
10479+	lowS -= 2;
10480+	for (n = nodeNb; n <= nodeRoot; n++)
10481+		huffNode[n].count = (U32)(1U << 30);
10482+	huffNode0[0].count = (U32)(1U << 31); /* fake entry, strong barrier */
10483+
10484+	/* create parents */
10485+	while (nodeNb <= nodeRoot) {
10486+		U32 n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
10487+		U32 n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
10488+		huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count;
10489+		huffNode[n1].parent = huffNode[n2].parent = nodeNb;
10490+		nodeNb++;
10491+	}
10492+
10493+	/* distribute weights (unlimited tree height) */
10494+	huffNode[nodeRoot].nbBits = 0;
10495+	for (n = nodeRoot - 1; n >= STARTNODE; n--)
10496+		huffNode[n].nbBits = huffNode[huffNode[n].parent].nbBits + 1;
10497+	for (n = 0; n <= nonNullRank; n++)
10498+		huffNode[n].nbBits = huffNode[huffNode[n].parent].nbBits + 1;
10499+
10500+	/* enforce maxTableLog */
10501+	maxNbBits = HUF_setMaxHeight(huffNode, nonNullRank, maxNbBits);
10502+
10503+	/* fill result into tree (val, nbBits) */
10504+	{
10505+		U16 nbPerRank[HUF_TABLELOG_MAX + 1] = {0};
10506+		U16 valPerRank[HUF_TABLELOG_MAX + 1] = {0};
10507+		if (maxNbBits > HUF_TABLELOG_MAX)
10508+			return ERROR(GENERIC); /* check fit into table */
10509+		for (n = 0; n <= nonNullRank; n++)
10510+			nbPerRank[huffNode[n].nbBits]++;
10511+		/* determine stating value per rank */
10512+		{
10513+			U16 min = 0;
10514+			for (n = maxNbBits; n > 0; n--) {
10515+				valPerRank[n] = min; /* get starting value within each rank */
10516+				min += nbPerRank[n];
10517+				min >>= 1;
10518+			}
10519+		}
10520+		for (n = 0; n <= maxSymbolValue; n++)
10521+			tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
10522+		for (n = 0; n <= maxSymbolValue; n++)
10523+			tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */
10524+	}
10525+
10526+	return maxNbBits;
10527+}
10528+
10529+static size_t HUF_estimateCompressedSize(HUF_CElt *CTable, const unsigned *count, unsigned maxSymbolValue)
10530+{
10531+	size_t nbBits = 0;
10532+	int s;
10533+	for (s = 0; s <= (int)maxSymbolValue; ++s) {
10534+		nbBits += CTable[s].nbBits * count[s];
10535+	}
10536+	return nbBits >> 3;
10537+}
10538+
10539+static int HUF_validateCTable(const HUF_CElt *CTable, const unsigned *count, unsigned maxSymbolValue)
10540+{
10541+	int bad = 0;
10542+	int s;
10543+	for (s = 0; s <= (int)maxSymbolValue; ++s) {
10544+		bad |= (count[s] != 0) & (CTable[s].nbBits == 0);
10545+	}
10546+	return !bad;
10547+}
10548+
10549+static void HUF_encodeSymbol(BIT_CStream_t *bitCPtr, U32 symbol, const HUF_CElt *CTable)
10550+{
10551+	BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits);
10552+}
10553+
10554+size_t HUF_compressBound(size_t size) { return HUF_COMPRESSBOUND(size); }
10555+
10556+#define HUF_FLUSHBITS(s)  BIT_flushBits(s)
10557+
10558+#define HUF_FLUSHBITS_1(stream)                                            \
10559+	if (sizeof((stream)->bitContainer) * 8 < HUF_TABLELOG_MAX * 2 + 7) \
10560+	HUF_FLUSHBITS(stream)
10561+
10562+#define HUF_FLUSHBITS_2(stream)                                            \
10563+	if (sizeof((stream)->bitContainer) * 8 < HUF_TABLELOG_MAX * 4 + 7) \
10564+	HUF_FLUSHBITS(stream)
10565+
10566+size_t HUF_compress1X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable)
10567+{
10568+	const BYTE *ip = (const BYTE *)src;
10569+	BYTE *const ostart = (BYTE *)dst;
10570+	BYTE *const oend = ostart + dstSize;
10571+	BYTE *op = ostart;
10572+	size_t n;
10573+	BIT_CStream_t bitC;
10574+
10575+	/* init */
10576+	if (dstSize < 8)
10577+		return 0; /* not enough space to compress */
10578+	{
10579+		size_t const initErr = BIT_initCStream(&bitC, op, oend - op);
10580+		if (HUF_isError(initErr))
10581+			return 0;
10582+	}
10583+
10584+	n = srcSize & ~3; /* join to mod 4 */
10585+	switch (srcSize & 3) {
10586+	case 3: HUF_encodeSymbol(&bitC, ip[n + 2], CTable); HUF_FLUSHBITS_2(&bitC);
10587+	case 2: HUF_encodeSymbol(&bitC, ip[n + 1], CTable); HUF_FLUSHBITS_1(&bitC);
10588+	case 1: HUF_encodeSymbol(&bitC, ip[n + 0], CTable); HUF_FLUSHBITS(&bitC);
10589+	case 0:
10590+	default:;
10591+	}
10592+
10593+	for (; n > 0; n -= 4) { /* note : n&3==0 at this stage */
10594+		HUF_encodeSymbol(&bitC, ip[n - 1], CTable);
10595+		HUF_FLUSHBITS_1(&bitC);
10596+		HUF_encodeSymbol(&bitC, ip[n - 2], CTable);
10597+		HUF_FLUSHBITS_2(&bitC);
10598+		HUF_encodeSymbol(&bitC, ip[n - 3], CTable);
10599+		HUF_FLUSHBITS_1(&bitC);
10600+		HUF_encodeSymbol(&bitC, ip[n - 4], CTable);
10601+		HUF_FLUSHBITS(&bitC);
10602+	}
10603+
10604+	return BIT_closeCStream(&bitC);
10605+}
10606+
10607+size_t HUF_compress4X_usingCTable(void *dst, size_t dstSize, const void *src, size_t srcSize, const HUF_CElt *CTable)
10608+{
10609+	size_t const segmentSize = (srcSize + 3) / 4; /* first 3 segments */
10610+	const BYTE *ip = (const BYTE *)src;
10611+	const BYTE *const iend = ip + srcSize;
10612+	BYTE *const ostart = (BYTE *)dst;
10613+	BYTE *const oend = ostart + dstSize;
10614+	BYTE *op = ostart;
10615+
10616+	if (dstSize < 6 + 1 + 1 + 1 + 8)
10617+		return 0; /* minimum space to compress successfully */
10618+	if (srcSize < 12)
10619+		return 0; /* no saving possible : too small input */
10620+	op += 6;	  /* jumpTable */
10621+
10622+	{
10623+		CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend - op, ip, segmentSize, CTable));
10624+		if (cSize == 0)
10625+			return 0;
10626+		ZSTD_writeLE16(ostart, (U16)cSize);
10627+		op += cSize;
10628+	}
10629+
10630+	ip += segmentSize;
10631+	{
10632+		CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend - op, ip, segmentSize, CTable));
10633+		if (cSize == 0)
10634+			return 0;
10635+		ZSTD_writeLE16(ostart + 2, (U16)cSize);
10636+		op += cSize;
10637+	}
10638+
10639+	ip += segmentSize;
10640+	{
10641+		CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend - op, ip, segmentSize, CTable));
10642+		if (cSize == 0)
10643+			return 0;
10644+		ZSTD_writeLE16(ostart + 4, (U16)cSize);
10645+		op += cSize;
10646+	}
10647+
10648+	ip += segmentSize;
10649+	{
10650+		CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend - op, ip, iend - ip, CTable));
10651+		if (cSize == 0)
10652+			return 0;
10653+		op += cSize;
10654+	}
10655+
10656+	return op - ostart;
10657+}
10658+
10659+static size_t HUF_compressCTable_internal(BYTE *const ostart, BYTE *op, BYTE *const oend, const void *src, size_t srcSize, unsigned singleStream,
10660+					  const HUF_CElt *CTable)
10661+{
10662+	size_t const cSize =
10663+	    singleStream ? HUF_compress1X_usingCTable(op, oend - op, src, srcSize, CTable) : HUF_compress4X_usingCTable(op, oend - op, src, srcSize, CTable);
10664+	if (HUF_isError(cSize)) {
10665+		return cSize;
10666+	}
10667+	if (cSize == 0) {
10668+		return 0;
10669+	} /* uncompressible */
10670+	op += cSize;
10671+	/* check compressibility */
10672+	if ((size_t)(op - ostart) >= srcSize - 1) {
10673+		return 0;
10674+	}
10675+	return op - ostart;
10676+}
10677+
10678+/* `workSpace` must a table of at least 1024 unsigned */
10679+static size_t HUF_compress_internal(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog,
10680+				    unsigned singleStream, void *workSpace, size_t wkspSize, HUF_CElt *oldHufTable, HUF_repeat *repeat, int preferRepeat)
10681+{
10682+	BYTE *const ostart = (BYTE *)dst;
10683+	BYTE *const oend = ostart + dstSize;
10684+	BYTE *op = ostart;
10685+
10686+	U32 *count;
10687+	size_t const countSize = sizeof(U32) * (HUF_SYMBOLVALUE_MAX + 1);
10688+	HUF_CElt *CTable;
10689+	size_t const CTableSize = sizeof(HUF_CElt) * (HUF_SYMBOLVALUE_MAX + 1);
10690+
10691+	/* checks & inits */
10692+	if (wkspSize < sizeof(huffNodeTable) + countSize + CTableSize)
10693+		return ERROR(GENERIC);
10694+	if (!srcSize)
10695+		return 0; /* Uncompressed (note : 1 means rle, so first byte must be correct) */
10696+	if (!dstSize)
10697+		return 0; /* cannot fit within dst budget */
10698+	if (srcSize > HUF_BLOCKSIZE_MAX)
10699+		return ERROR(srcSize_wrong); /* curr block size limit */
10700+	if (huffLog > HUF_TABLELOG_MAX)
10701+		return ERROR(tableLog_tooLarge);
10702+	if (!maxSymbolValue)
10703+		maxSymbolValue = HUF_SYMBOLVALUE_MAX;
10704+	if (!huffLog)
10705+		huffLog = HUF_TABLELOG_DEFAULT;
10706+
10707+	count = (U32 *)workSpace;
10708+	workSpace = (BYTE *)workSpace + countSize;
10709+	wkspSize -= countSize;
10710+	CTable = (HUF_CElt *)workSpace;
10711+	workSpace = (BYTE *)workSpace + CTableSize;
10712+	wkspSize -= CTableSize;
10713+
10714+	/* Heuristic : If we don't need to check the validity of the old table use the old table for small inputs */
10715+	if (preferRepeat && repeat && *repeat == HUF_repeat_valid) {
10716+		return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, oldHufTable);
10717+	}
10718+
10719+	/* Scan input and build symbol stats */
10720+	{
10721+		CHECK_V_F(largest, FSE_count_wksp(count, &maxSymbolValue, (const BYTE *)src, srcSize, (U32 *)workSpace));
10722+		if (largest == srcSize) {
10723+			*ostart = ((const BYTE *)src)[0];
10724+			return 1;
10725+		} /* single symbol, rle */
10726+		if (largest <= (srcSize >> 7) + 1)
10727+			return 0; /* Fast heuristic : not compressible enough */
10728+	}
10729+
10730+	/* Check validity of previous table */
10731+	if (repeat && *repeat == HUF_repeat_check && !HUF_validateCTable(oldHufTable, count, maxSymbolValue)) {
10732+		*repeat = HUF_repeat_none;
10733+	}
10734+	/* Heuristic : use existing table for small inputs */
10735+	if (preferRepeat && repeat && *repeat != HUF_repeat_none) {
10736+		return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, oldHufTable);
10737+	}
10738+
10739+	/* Build Huffman Tree */
10740+	huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue);
10741+	{
10742+		CHECK_V_F(maxBits, HUF_buildCTable_wksp(CTable, count, maxSymbolValue, huffLog, workSpace, wkspSize));
10743+		huffLog = (U32)maxBits;
10744+		/* Zero the unused symbols so we can check it for validity */
10745+		memset(CTable + maxSymbolValue + 1, 0, CTableSize - (maxSymbolValue + 1) * sizeof(HUF_CElt));
10746+	}
10747+
10748+	/* Write table description header */
10749+	{
10750+		CHECK_V_F(hSize, HUF_writeCTable_wksp(op, dstSize, CTable, maxSymbolValue, huffLog, workSpace, wkspSize));
10751+		/* Check if using the previous table will be beneficial */
10752+		if (repeat && *repeat != HUF_repeat_none) {
10753+			size_t const oldSize = HUF_estimateCompressedSize(oldHufTable, count, maxSymbolValue);
10754+			size_t const newSize = HUF_estimateCompressedSize(CTable, count, maxSymbolValue);
10755+			if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) {
10756+				return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, oldHufTable);
10757+			}
10758+		}
10759+		/* Use the new table */
10760+		if (hSize + 12ul >= srcSize) {
10761+			return 0;
10762+		}
10763+		op += hSize;
10764+		if (repeat) {
10765+			*repeat = HUF_repeat_none;
10766+		}
10767+		if (oldHufTable) {
10768+			memcpy(oldHufTable, CTable, CTableSize);
10769+		} /* Save the new table */
10770+	}
10771+	return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, singleStream, CTable);
10772+}
10773+
10774+size_t HUF_compress1X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace,
10775+			   size_t wkspSize)
10776+{
10777+	return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1 /* single stream */, workSpace, wkspSize, NULL, NULL, 0);
10778+}
10779+
10780+size_t HUF_compress1X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace,
10781+			     size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, int preferRepeat)
10782+{
10783+	return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1 /* single stream */, workSpace, wkspSize, hufTable, repeat,
10784+				     preferRepeat);
10785+}
10786+
10787+size_t HUF_compress4X_wksp(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace,
10788+			   size_t wkspSize)
10789+{
10790+	return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 0 /* 4 streams */, workSpace, wkspSize, NULL, NULL, 0);
10791+}
10792+
10793+size_t HUF_compress4X_repeat(void *dst, size_t dstSize, const void *src, size_t srcSize, unsigned maxSymbolValue, unsigned huffLog, void *workSpace,
10794+			     size_t wkspSize, HUF_CElt *hufTable, HUF_repeat *repeat, int preferRepeat)
10795+{
10796+	return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 0 /* 4 streams */, workSpace, wkspSize, hufTable, repeat,
10797+				     preferRepeat);
10798+}
10799diff --git a/lib/zstd/huf_decompress.c b/lib/zstd/huf_decompress.c
10800new file mode 100644
10801index 0000000..6526482
10802--- /dev/null
10803+++ b/lib/zstd/huf_decompress.c
10804@@ -0,0 +1,960 @@
10805+/*
10806+ * Huffman decoder, part of New Generation Entropy library
10807+ * Copyright (C) 2013-2016, Yann Collet.
10808+ *
10809+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
10810+ *
10811+ * Redistribution and use in source and binary forms, with or without
10812+ * modification, are permitted provided that the following conditions are
10813+ * met:
10814+ *
10815+ *   * Redistributions of source code must retain the above copyright
10816+ * notice, this list of conditions and the following disclaimer.
10817+ *   * Redistributions in binary form must reproduce the above
10818+ * copyright notice, this list of conditions and the following disclaimer
10819+ * in the documentation and/or other materials provided with the
10820+ * distribution.
10821+ *
10822+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10823+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10824+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
10825+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10826+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10827+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10828+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10829+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
10830+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10831+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
10832+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10833+ *
10834+ * This program is free software; you can redistribute it and/or modify it under
10835+ * the terms of the GNU General Public License version 2 as published by the
10836+ * Free Software Foundation. This program is dual-licensed; you may select
10837+ * either version 2 of the GNU General Public License ("GPL") or BSD license
10838+ * ("BSD").
10839+ *
10840+ * You can contact the author at :
10841+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
10842+ */
10843+
10844+/* **************************************************************
10845+*  Compiler specifics
10846+****************************************************************/
10847+#define FORCE_INLINE static __always_inline
10848+
10849+/* **************************************************************
10850+*  Dependencies
10851+****************************************************************/
10852+#include "bitstream.h" /* BIT_* */
10853+#include "fse.h"       /* header compression */
10854+#include "huf.h"
10855+#include <linux/compiler.h>
10856+#include <linux/kernel.h>
10857+#include <linux/string.h> /* memcpy, memset */
10858+
10859+/* **************************************************************
10860+*  Error Management
10861+****************************************************************/
10862+#define HUF_STATIC_ASSERT(c)                                   \
10863+	{                                                      \
10864+		enum { HUF_static_assert = 1 / (int)(!!(c)) }; \
10865+	} /* use only *after* variable declarations */
10866+
10867+/*-***************************/
10868+/*  generic DTableDesc       */
10869+/*-***************************/
10870+
10871+typedef struct {
10872+	BYTE maxTableLog;
10873+	BYTE tableType;
10874+	BYTE tableLog;
10875+	BYTE reserved;
10876+} DTableDesc;
10877+
10878+static DTableDesc HUF_getDTableDesc(const HUF_DTable *table)
10879+{
10880+	DTableDesc dtd;
10881+	memcpy(&dtd, table, sizeof(dtd));
10882+	return dtd;
10883+}
10884+
10885+/*-***************************/
10886+/*  single-symbol decoding   */
10887+/*-***************************/
10888+
10889+typedef struct {
10890+	BYTE byte;
10891+	BYTE nbBits;
10892+} HUF_DEltX2; /* single-symbol decoding */
10893+
10894+size_t HUF_readDTableX2_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
10895+{
10896+	U32 tableLog = 0;
10897+	U32 nbSymbols = 0;
10898+	size_t iSize;
10899+	void *const dtPtr = DTable + 1;
10900+	HUF_DEltX2 *const dt = (HUF_DEltX2 *)dtPtr;
10901+
10902+	U32 *rankVal;
10903+	BYTE *huffWeight;
10904+	size_t spaceUsed32 = 0;
10905+
10906+	rankVal = (U32 *)workspace + spaceUsed32;
10907+	spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
10908+	huffWeight = (BYTE *)((U32 *)workspace + spaceUsed32);
10909+	spaceUsed32 += ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
10910+
10911+	if ((spaceUsed32 << 2) > workspaceSize)
10912+		return ERROR(tableLog_tooLarge);
10913+	workspace = (U32 *)workspace + spaceUsed32;
10914+	workspaceSize -= (spaceUsed32 << 2);
10915+
10916+	HUF_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
10917+	/* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
10918+
10919+	iSize = HUF_readStats_wksp(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize, workspace, workspaceSize);
10920+	if (HUF_isError(iSize))
10921+		return iSize;
10922+
10923+	/* Table header */
10924+	{
10925+		DTableDesc dtd = HUF_getDTableDesc(DTable);
10926+		if (tableLog > (U32)(dtd.maxTableLog + 1))
10927+			return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */
10928+		dtd.tableType = 0;
10929+		dtd.tableLog = (BYTE)tableLog;
10930+		memcpy(DTable, &dtd, sizeof(dtd));
10931+	}
10932+
10933+	/* Calculate starting value for each rank */
10934+	{
10935+		U32 n, nextRankStart = 0;
10936+		for (n = 1; n < tableLog + 1; n++) {
10937+			U32 const curr = nextRankStart;
10938+			nextRankStart += (rankVal[n] << (n - 1));
10939+			rankVal[n] = curr;
10940+		}
10941+	}
10942+
10943+	/* fill DTable */
10944+	{
10945+		U32 n;
10946+		for (n = 0; n < nbSymbols; n++) {
10947+			U32 const w = huffWeight[n];
10948+			U32 const length = (1 << w) >> 1;
10949+			U32 u;
10950+			HUF_DEltX2 D;
10951+			D.byte = (BYTE)n;
10952+			D.nbBits = (BYTE)(tableLog + 1 - w);
10953+			for (u = rankVal[w]; u < rankVal[w] + length; u++)
10954+				dt[u] = D;
10955+			rankVal[w] += length;
10956+		}
10957+	}
10958+
10959+	return iSize;
10960+}
10961+
10962+static BYTE HUF_decodeSymbolX2(BIT_DStream_t *Dstream, const HUF_DEltX2 *dt, const U32 dtLog)
10963+{
10964+	size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
10965+	BYTE const c = dt[val].byte;
10966+	BIT_skipBits(Dstream, dt[val].nbBits);
10967+	return c;
10968+}
10969+
10970+#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
10971+
10972+#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr)         \
10973+	if (ZSTD_64bits() || (HUF_TABLELOG_MAX <= 12)) \
10974+	HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
10975+
10976+#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
10977+	if (ZSTD_64bits())                     \
10978+	HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
10979+
10980+FORCE_INLINE size_t HUF_decodeStreamX2(BYTE *p, BIT_DStream_t *const bitDPtr, BYTE *const pEnd, const HUF_DEltX2 *const dt, const U32 dtLog)
10981+{
10982+	BYTE *const pStart = p;
10983+
10984+	/* up to 4 symbols at a time */
10985+	while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd - 4)) {
10986+		HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
10987+		HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
10988+		HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
10989+		HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
10990+	}
10991+
10992+	/* closer to the end */
10993+	while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd))
10994+		HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
10995+
10996+	/* no more data to retrieve from bitstream, hence no need to reload */
10997+	while (p < pEnd)
10998+		HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
10999+
11000+	return pEnd - pStart;
11001+}
11002+
11003+static size_t HUF_decompress1X2_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11004+{
11005+	BYTE *op = (BYTE *)dst;
11006+	BYTE *const oend = op + dstSize;
11007+	const void *dtPtr = DTable + 1;
11008+	const HUF_DEltX2 *const dt = (const HUF_DEltX2 *)dtPtr;
11009+	BIT_DStream_t bitD;
11010+	DTableDesc const dtd = HUF_getDTableDesc(DTable);
11011+	U32 const dtLog = dtd.tableLog;
11012+
11013+	{
11014+		size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
11015+		if (HUF_isError(errorCode))
11016+			return errorCode;
11017+	}
11018+
11019+	HUF_decodeStreamX2(op, &bitD, oend, dt, dtLog);
11020+
11021+	/* check */
11022+	if (!BIT_endOfDStream(&bitD))
11023+		return ERROR(corruption_detected);
11024+
11025+	return dstSize;
11026+}
11027+
11028+size_t HUF_decompress1X2_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11029+{
11030+	DTableDesc dtd = HUF_getDTableDesc(DTable);
11031+	if (dtd.tableType != 0)
11032+		return ERROR(GENERIC);
11033+	return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
11034+}
11035+
11036+size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable *DCtx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
11037+{
11038+	const BYTE *ip = (const BYTE *)cSrc;
11039+
11040+	size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize, workspace, workspaceSize);
11041+	if (HUF_isError(hSize))
11042+		return hSize;
11043+	if (hSize >= cSrcSize)
11044+		return ERROR(srcSize_wrong);
11045+	ip += hSize;
11046+	cSrcSize -= hSize;
11047+
11048+	return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx);
11049+}
11050+
11051+static size_t HUF_decompress4X2_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11052+{
11053+	/* Check */
11054+	if (cSrcSize < 10)
11055+		return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
11056+
11057+	{
11058+		const BYTE *const istart = (const BYTE *)cSrc;
11059+		BYTE *const ostart = (BYTE *)dst;
11060+		BYTE *const oend = ostart + dstSize;
11061+		const void *const dtPtr = DTable + 1;
11062+		const HUF_DEltX2 *const dt = (const HUF_DEltX2 *)dtPtr;
11063+
11064+		/* Init */
11065+		BIT_DStream_t bitD1;
11066+		BIT_DStream_t bitD2;
11067+		BIT_DStream_t bitD3;
11068+		BIT_DStream_t bitD4;
11069+		size_t const length1 = ZSTD_readLE16(istart);
11070+		size_t const length2 = ZSTD_readLE16(istart + 2);
11071+		size_t const length3 = ZSTD_readLE16(istart + 4);
11072+		size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
11073+		const BYTE *const istart1 = istart + 6; /* jumpTable */
11074+		const BYTE *const istart2 = istart1 + length1;
11075+		const BYTE *const istart3 = istart2 + length2;
11076+		const BYTE *const istart4 = istart3 + length3;
11077+		const size_t segmentSize = (dstSize + 3) / 4;
11078+		BYTE *const opStart2 = ostart + segmentSize;
11079+		BYTE *const opStart3 = opStart2 + segmentSize;
11080+		BYTE *const opStart4 = opStart3 + segmentSize;
11081+		BYTE *op1 = ostart;
11082+		BYTE *op2 = opStart2;
11083+		BYTE *op3 = opStart3;
11084+		BYTE *op4 = opStart4;
11085+		U32 endSignal;
11086+		DTableDesc const dtd = HUF_getDTableDesc(DTable);
11087+		U32 const dtLog = dtd.tableLog;
11088+
11089+		if (length4 > cSrcSize)
11090+			return ERROR(corruption_detected); /* overflow */
11091+		{
11092+			size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1);
11093+			if (HUF_isError(errorCode))
11094+				return errorCode;
11095+		}
11096+		{
11097+			size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2);
11098+			if (HUF_isError(errorCode))
11099+				return errorCode;
11100+		}
11101+		{
11102+			size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3);
11103+			if (HUF_isError(errorCode))
11104+				return errorCode;
11105+		}
11106+		{
11107+			size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4);
11108+			if (HUF_isError(errorCode))
11109+				return errorCode;
11110+		}
11111+
11112+		/* 16-32 symbols per loop (4-8 symbols per stream) */
11113+		endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
11114+		for (; (endSignal == BIT_DStream_unfinished) && (op4 < (oend - 7));) {
11115+			HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
11116+			HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
11117+			HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
11118+			HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
11119+			HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
11120+			HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
11121+			HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
11122+			HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
11123+			HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
11124+			HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
11125+			HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
11126+			HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
11127+			HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
11128+			HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
11129+			HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
11130+			HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
11131+			endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
11132+		}
11133+
11134+		/* check corruption */
11135+		if (op1 > opStart2)
11136+			return ERROR(corruption_detected);
11137+		if (op2 > opStart3)
11138+			return ERROR(corruption_detected);
11139+		if (op3 > opStart4)
11140+			return ERROR(corruption_detected);
11141+		/* note : op4 supposed already verified within main loop */
11142+
11143+		/* finish bitStreams one by one */
11144+		HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
11145+		HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
11146+		HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
11147+		HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
11148+
11149+		/* check */
11150+		endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
11151+		if (!endSignal)
11152+			return ERROR(corruption_detected);
11153+
11154+		/* decoded size */
11155+		return dstSize;
11156+	}
11157+}
11158+
11159+size_t HUF_decompress4X2_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11160+{
11161+	DTableDesc dtd = HUF_getDTableDesc(DTable);
11162+	if (dtd.tableType != 0)
11163+		return ERROR(GENERIC);
11164+	return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
11165+}
11166+
11167+size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
11168+{
11169+	const BYTE *ip = (const BYTE *)cSrc;
11170+
11171+	size_t const hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize, workspace, workspaceSize);
11172+	if (HUF_isError(hSize))
11173+		return hSize;
11174+	if (hSize >= cSrcSize)
11175+		return ERROR(srcSize_wrong);
11176+	ip += hSize;
11177+	cSrcSize -= hSize;
11178+
11179+	return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx);
11180+}
11181+
11182+/* *************************/
11183+/* double-symbols decoding */
11184+/* *************************/
11185+typedef struct {
11186+	U16 sequence;
11187+	BYTE nbBits;
11188+	BYTE length;
11189+} HUF_DEltX4; /* double-symbols decoding */
11190+
11191+typedef struct {
11192+	BYTE symbol;
11193+	BYTE weight;
11194+} sortedSymbol_t;
11195+
11196+/* HUF_fillDTableX4Level2() :
11197+ * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */
11198+static void HUF_fillDTableX4Level2(HUF_DEltX4 *DTable, U32 sizeLog, const U32 consumed, const U32 *rankValOrigin, const int minWeight,
11199+				   const sortedSymbol_t *sortedSymbols, const U32 sortedListSize, U32 nbBitsBaseline, U16 baseSeq)
11200+{
11201+	HUF_DEltX4 DElt;
11202+	U32 rankVal[HUF_TABLELOG_MAX + 1];
11203+
11204+	/* get pre-calculated rankVal */
11205+	memcpy(rankVal, rankValOrigin, sizeof(rankVal));
11206+
11207+	/* fill skipped values */
11208+	if (minWeight > 1) {
11209+		U32 i, skipSize = rankVal[minWeight];
11210+		ZSTD_writeLE16(&(DElt.sequence), baseSeq);
11211+		DElt.nbBits = (BYTE)(consumed);
11212+		DElt.length = 1;
11213+		for (i = 0; i < skipSize; i++)
11214+			DTable[i] = DElt;
11215+	}
11216+
11217+	/* fill DTable */
11218+	{
11219+		U32 s;
11220+		for (s = 0; s < sortedListSize; s++) { /* note : sortedSymbols already skipped */
11221+			const U32 symbol = sortedSymbols[s].symbol;
11222+			const U32 weight = sortedSymbols[s].weight;
11223+			const U32 nbBits = nbBitsBaseline - weight;
11224+			const U32 length = 1 << (sizeLog - nbBits);
11225+			const U32 start = rankVal[weight];
11226+			U32 i = start;
11227+			const U32 end = start + length;
11228+
11229+			ZSTD_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
11230+			DElt.nbBits = (BYTE)(nbBits + consumed);
11231+			DElt.length = 2;
11232+			do {
11233+				DTable[i++] = DElt;
11234+			} while (i < end); /* since length >= 1 */
11235+
11236+			rankVal[weight] += length;
11237+		}
11238+	}
11239+}
11240+
11241+typedef U32 rankVal_t[HUF_TABLELOG_MAX][HUF_TABLELOG_MAX + 1];
11242+typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];
11243+
11244+static void HUF_fillDTableX4(HUF_DEltX4 *DTable, const U32 targetLog, const sortedSymbol_t *sortedList, const U32 sortedListSize, const U32 *rankStart,
11245+			     rankVal_t rankValOrigin, const U32 maxWeight, const U32 nbBitsBaseline)
11246+{
11247+	U32 rankVal[HUF_TABLELOG_MAX + 1];
11248+	const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
11249+	const U32 minBits = nbBitsBaseline - maxWeight;
11250+	U32 s;
11251+
11252+	memcpy(rankVal, rankValOrigin, sizeof(rankVal));
11253+
11254+	/* fill DTable */
11255+	for (s = 0; s < sortedListSize; s++) {
11256+		const U16 symbol = sortedList[s].symbol;
11257+		const U32 weight = sortedList[s].weight;
11258+		const U32 nbBits = nbBitsBaseline - weight;
11259+		const U32 start = rankVal[weight];
11260+		const U32 length = 1 << (targetLog - nbBits);
11261+
11262+		if (targetLog - nbBits >= minBits) { /* enough room for a second symbol */
11263+			U32 sortedRank;
11264+			int minWeight = nbBits + scaleLog;
11265+			if (minWeight < 1)
11266+				minWeight = 1;
11267+			sortedRank = rankStart[minWeight];
11268+			HUF_fillDTableX4Level2(DTable + start, targetLog - nbBits, nbBits, rankValOrigin[nbBits], minWeight, sortedList + sortedRank,
11269+					       sortedListSize - sortedRank, nbBitsBaseline, symbol);
11270+		} else {
11271+			HUF_DEltX4 DElt;
11272+			ZSTD_writeLE16(&(DElt.sequence), symbol);
11273+			DElt.nbBits = (BYTE)(nbBits);
11274+			DElt.length = 1;
11275+			{
11276+				U32 const end = start + length;
11277+				U32 u;
11278+				for (u = start; u < end; u++)
11279+					DTable[u] = DElt;
11280+			}
11281+		}
11282+		rankVal[weight] += length;
11283+	}
11284+}
11285+
11286+size_t HUF_readDTableX4_wksp(HUF_DTable *DTable, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
11287+{
11288+	U32 tableLog, maxW, sizeOfSort, nbSymbols;
11289+	DTableDesc dtd = HUF_getDTableDesc(DTable);
11290+	U32 const maxTableLog = dtd.maxTableLog;
11291+	size_t iSize;
11292+	void *dtPtr = DTable + 1; /* force compiler to avoid strict-aliasing */
11293+	HUF_DEltX4 *const dt = (HUF_DEltX4 *)dtPtr;
11294+	U32 *rankStart;
11295+
11296+	rankValCol_t *rankVal;
11297+	U32 *rankStats;
11298+	U32 *rankStart0;
11299+	sortedSymbol_t *sortedSymbol;
11300+	BYTE *weightList;
11301+	size_t spaceUsed32 = 0;
11302+
11303+	HUF_STATIC_ASSERT((sizeof(rankValCol_t) & 3) == 0);
11304+
11305+	rankVal = (rankValCol_t *)((U32 *)workspace + spaceUsed32);
11306+	spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;
11307+	rankStats = (U32 *)workspace + spaceUsed32;
11308+	spaceUsed32 += HUF_TABLELOG_MAX + 1;
11309+	rankStart0 = (U32 *)workspace + spaceUsed32;
11310+	spaceUsed32 += HUF_TABLELOG_MAX + 2;
11311+	sortedSymbol = (sortedSymbol_t *)((U32 *)workspace + spaceUsed32);
11312+	spaceUsed32 += ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;
11313+	weightList = (BYTE *)((U32 *)workspace + spaceUsed32);
11314+	spaceUsed32 += ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
11315+
11316+	if ((spaceUsed32 << 2) > workspaceSize)
11317+		return ERROR(tableLog_tooLarge);
11318+	workspace = (U32 *)workspace + spaceUsed32;
11319+	workspaceSize -= (spaceUsed32 << 2);
11320+
11321+	rankStart = rankStart0 + 1;
11322+	memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));
11323+
11324+	HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */
11325+	if (maxTableLog > HUF_TABLELOG_MAX)
11326+		return ERROR(tableLog_tooLarge);
11327+	/* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
11328+
11329+	iSize = HUF_readStats_wksp(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize, workspace, workspaceSize);
11330+	if (HUF_isError(iSize))
11331+		return iSize;
11332+
11333+	/* check result */
11334+	if (tableLog > maxTableLog)
11335+		return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
11336+
11337+	/* find maxWeight */
11338+	for (maxW = tableLog; rankStats[maxW] == 0; maxW--) {
11339+	} /* necessarily finds a solution before 0 */
11340+
11341+	/* Get start index of each weight */
11342+	{
11343+		U32 w, nextRankStart = 0;
11344+		for (w = 1; w < maxW + 1; w++) {
11345+			U32 curr = nextRankStart;
11346+			nextRankStart += rankStats[w];
11347+			rankStart[w] = curr;
11348+		}
11349+		rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
11350+		sizeOfSort = nextRankStart;
11351+	}
11352+
11353+	/* sort symbols by weight */
11354+	{
11355+		U32 s;
11356+		for (s = 0; s < nbSymbols; s++) {
11357+			U32 const w = weightList[s];
11358+			U32 const r = rankStart[w]++;
11359+			sortedSymbol[r].symbol = (BYTE)s;
11360+			sortedSymbol[r].weight = (BYTE)w;
11361+		}
11362+		rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
11363+	}
11364+
11365+	/* Build rankVal */
11366+	{
11367+		U32 *const rankVal0 = rankVal[0];
11368+		{
11369+			int const rescale = (maxTableLog - tableLog) - 1; /* tableLog <= maxTableLog */
11370+			U32 nextRankVal = 0;
11371+			U32 w;
11372+			for (w = 1; w < maxW + 1; w++) {
11373+				U32 curr = nextRankVal;
11374+				nextRankVal += rankStats[w] << (w + rescale);
11375+				rankVal0[w] = curr;
11376+			}
11377+		}
11378+		{
11379+			U32 const minBits = tableLog + 1 - maxW;
11380+			U32 consumed;
11381+			for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
11382+				U32 *const rankValPtr = rankVal[consumed];
11383+				U32 w;
11384+				for (w = 1; w < maxW + 1; w++) {
11385+					rankValPtr[w] = rankVal0[w] >> consumed;
11386+				}
11387+			}
11388+		}
11389+	}
11390+
11391+	HUF_fillDTableX4(dt, maxTableLog, sortedSymbol, sizeOfSort, rankStart0, rankVal, maxW, tableLog + 1);
11392+
11393+	dtd.tableLog = (BYTE)maxTableLog;
11394+	dtd.tableType = 1;
11395+	memcpy(DTable, &dtd, sizeof(dtd));
11396+	return iSize;
11397+}
11398+
11399+static U32 HUF_decodeSymbolX4(void *op, BIT_DStream_t *DStream, const HUF_DEltX4 *dt, const U32 dtLog)
11400+{
11401+	size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
11402+	memcpy(op, dt + val, 2);
11403+	BIT_skipBits(DStream, dt[val].nbBits);
11404+	return dt[val].length;
11405+}
11406+
11407+static U32 HUF_decodeLastSymbolX4(void *op, BIT_DStream_t *DStream, const HUF_DEltX4 *dt, const U32 dtLog)
11408+{
11409+	size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
11410+	memcpy(op, dt + val, 1);
11411+	if (dt[val].length == 1)
11412+		BIT_skipBits(DStream, dt[val].nbBits);
11413+	else {
11414+		if (DStream->bitsConsumed < (sizeof(DStream->bitContainer) * 8)) {
11415+			BIT_skipBits(DStream, dt[val].nbBits);
11416+			if (DStream->bitsConsumed > (sizeof(DStream->bitContainer) * 8))
11417+				/* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
11418+				DStream->bitsConsumed = (sizeof(DStream->bitContainer) * 8);
11419+		}
11420+	}
11421+	return 1;
11422+}
11423+
11424+#define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
11425+
11426+#define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr)         \
11427+	if (ZSTD_64bits() || (HUF_TABLELOG_MAX <= 12)) \
11428+	ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
11429+
11430+#define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
11431+	if (ZSTD_64bits())                     \
11432+	ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
11433+
11434+FORCE_INLINE size_t HUF_decodeStreamX4(BYTE *p, BIT_DStream_t *bitDPtr, BYTE *const pEnd, const HUF_DEltX4 *const dt, const U32 dtLog)
11435+{
11436+	BYTE *const pStart = p;
11437+
11438+	/* up to 8 symbols at a time */
11439+	while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd - (sizeof(bitDPtr->bitContainer) - 1))) {
11440+		HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
11441+		HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
11442+		HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
11443+		HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
11444+	}
11445+
11446+	/* closer to end : up to 2 symbols at a time */
11447+	while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd - 2))
11448+		HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
11449+
11450+	while (p <= pEnd - 2)
11451+		HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
11452+
11453+	if (p < pEnd)
11454+		p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
11455+
11456+	return p - pStart;
11457+}
11458+
11459+static size_t HUF_decompress1X4_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11460+{
11461+	BIT_DStream_t bitD;
11462+
11463+	/* Init */
11464+	{
11465+		size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
11466+		if (HUF_isError(errorCode))
11467+			return errorCode;
11468+	}
11469+
11470+	/* decode */
11471+	{
11472+		BYTE *const ostart = (BYTE *)dst;
11473+		BYTE *const oend = ostart + dstSize;
11474+		const void *const dtPtr = DTable + 1; /* force compiler to not use strict-aliasing */
11475+		const HUF_DEltX4 *const dt = (const HUF_DEltX4 *)dtPtr;
11476+		DTableDesc const dtd = HUF_getDTableDesc(DTable);
11477+		HUF_decodeStreamX4(ostart, &bitD, oend, dt, dtd.tableLog);
11478+	}
11479+
11480+	/* check */
11481+	if (!BIT_endOfDStream(&bitD))
11482+		return ERROR(corruption_detected);
11483+
11484+	/* decoded size */
11485+	return dstSize;
11486+}
11487+
11488+size_t HUF_decompress1X4_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11489+{
11490+	DTableDesc dtd = HUF_getDTableDesc(DTable);
11491+	if (dtd.tableType != 1)
11492+		return ERROR(GENERIC);
11493+	return HUF_decompress1X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
11494+}
11495+
11496+size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable *DCtx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
11497+{
11498+	const BYTE *ip = (const BYTE *)cSrc;
11499+
11500+	size_t const hSize = HUF_readDTableX4_wksp(DCtx, cSrc, cSrcSize, workspace, workspaceSize);
11501+	if (HUF_isError(hSize))
11502+		return hSize;
11503+	if (hSize >= cSrcSize)
11504+		return ERROR(srcSize_wrong);
11505+	ip += hSize;
11506+	cSrcSize -= hSize;
11507+
11508+	return HUF_decompress1X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx);
11509+}
11510+
11511+static size_t HUF_decompress4X4_usingDTable_internal(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11512+{
11513+	if (cSrcSize < 10)
11514+		return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
11515+
11516+	{
11517+		const BYTE *const istart = (const BYTE *)cSrc;
11518+		BYTE *const ostart = (BYTE *)dst;
11519+		BYTE *const oend = ostart + dstSize;
11520+		const void *const dtPtr = DTable + 1;
11521+		const HUF_DEltX4 *const dt = (const HUF_DEltX4 *)dtPtr;
11522+
11523+		/* Init */
11524+		BIT_DStream_t bitD1;
11525+		BIT_DStream_t bitD2;
11526+		BIT_DStream_t bitD3;
11527+		BIT_DStream_t bitD4;
11528+		size_t const length1 = ZSTD_readLE16(istart);
11529+		size_t const length2 = ZSTD_readLE16(istart + 2);
11530+		size_t const length3 = ZSTD_readLE16(istart + 4);
11531+		size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
11532+		const BYTE *const istart1 = istart + 6; /* jumpTable */
11533+		const BYTE *const istart2 = istart1 + length1;
11534+		const BYTE *const istart3 = istart2 + length2;
11535+		const BYTE *const istart4 = istart3 + length3;
11536+		size_t const segmentSize = (dstSize + 3) / 4;
11537+		BYTE *const opStart2 = ostart + segmentSize;
11538+		BYTE *const opStart3 = opStart2 + segmentSize;
11539+		BYTE *const opStart4 = opStart3 + segmentSize;
11540+		BYTE *op1 = ostart;
11541+		BYTE *op2 = opStart2;
11542+		BYTE *op3 = opStart3;
11543+		BYTE *op4 = opStart4;
11544+		U32 endSignal;
11545+		DTableDesc const dtd = HUF_getDTableDesc(DTable);
11546+		U32 const dtLog = dtd.tableLog;
11547+
11548+		if (length4 > cSrcSize)
11549+			return ERROR(corruption_detected); /* overflow */
11550+		{
11551+			size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1);
11552+			if (HUF_isError(errorCode))
11553+				return errorCode;
11554+		}
11555+		{
11556+			size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2);
11557+			if (HUF_isError(errorCode))
11558+				return errorCode;
11559+		}
11560+		{
11561+			size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3);
11562+			if (HUF_isError(errorCode))
11563+				return errorCode;
11564+		}
11565+		{
11566+			size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4);
11567+			if (HUF_isError(errorCode))
11568+				return errorCode;
11569+		}
11570+
11571+		/* 16-32 symbols per loop (4-8 symbols per stream) */
11572+		endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
11573+		for (; (endSignal == BIT_DStream_unfinished) & (op4 < (oend - (sizeof(bitD4.bitContainer) - 1)));) {
11574+			HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
11575+			HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
11576+			HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
11577+			HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
11578+			HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
11579+			HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
11580+			HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
11581+			HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
11582+			HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
11583+			HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
11584+			HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
11585+			HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
11586+			HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
11587+			HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
11588+			HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
11589+			HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
11590+
11591+			endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
11592+		}
11593+
11594+		/* check corruption */
11595+		if (op1 > opStart2)
11596+			return ERROR(corruption_detected);
11597+		if (op2 > opStart3)
11598+			return ERROR(corruption_detected);
11599+		if (op3 > opStart4)
11600+			return ERROR(corruption_detected);
11601+		/* note : op4 already verified within main loop */
11602+
11603+		/* finish bitStreams one by one */
11604+		HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
11605+		HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
11606+		HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
11607+		HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
11608+
11609+		/* check */
11610+		{
11611+			U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
11612+			if (!endCheck)
11613+				return ERROR(corruption_detected);
11614+		}
11615+
11616+		/* decoded size */
11617+		return dstSize;
11618+	}
11619+}
11620+
11621+size_t HUF_decompress4X4_usingDTable(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11622+{
11623+	DTableDesc dtd = HUF_getDTableDesc(DTable);
11624+	if (dtd.tableType != 1)
11625+		return ERROR(GENERIC);
11626+	return HUF_decompress4X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
11627+}
11628+
11629+size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
11630+{
11631+	const BYTE *ip = (const BYTE *)cSrc;
11632+
11633+	size_t hSize = HUF_readDTableX4_wksp(dctx, cSrc, cSrcSize, workspace, workspaceSize);
11634+	if (HUF_isError(hSize))
11635+		return hSize;
11636+	if (hSize >= cSrcSize)
11637+		return ERROR(srcSize_wrong);
11638+	ip += hSize;
11639+	cSrcSize -= hSize;
11640+
11641+	return HUF_decompress4X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx);
11642+}
11643+
11644+/* ********************************/
11645+/* Generic decompression selector */
11646+/* ********************************/
11647+
11648+size_t HUF_decompress1X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11649+{
11650+	DTableDesc const dtd = HUF_getDTableDesc(DTable);
11651+	return dtd.tableType ? HUF_decompress1X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable)
11652+			     : HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
11653+}
11654+
11655+size_t HUF_decompress4X_usingDTable(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const HUF_DTable *DTable)
11656+{
11657+	DTableDesc const dtd = HUF_getDTableDesc(DTable);
11658+	return dtd.tableType ? HUF_decompress4X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable)
11659+			     : HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
11660+}
11661+
11662+typedef struct {
11663+	U32 tableTime;
11664+	U32 decode256Time;
11665+} algo_time_t;
11666+static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] = {
11667+    /* single, double, quad */
11668+    {{0, 0}, {1, 1}, {2, 2}},		     /* Q==0 : impossible */
11669+    {{0, 0}, {1, 1}, {2, 2}},		     /* Q==1 : impossible */
11670+    {{38, 130}, {1313, 74}, {2151, 38}},     /* Q == 2 : 12-18% */
11671+    {{448, 128}, {1353, 74}, {2238, 41}},    /* Q == 3 : 18-25% */
11672+    {{556, 128}, {1353, 74}, {2238, 47}},    /* Q == 4 : 25-32% */
11673+    {{714, 128}, {1418, 74}, {2436, 53}},    /* Q == 5 : 32-38% */
11674+    {{883, 128}, {1437, 74}, {2464, 61}},    /* Q == 6 : 38-44% */
11675+    {{897, 128}, {1515, 75}, {2622, 68}},    /* Q == 7 : 44-50% */
11676+    {{926, 128}, {1613, 75}, {2730, 75}},    /* Q == 8 : 50-56% */
11677+    {{947, 128}, {1729, 77}, {3359, 77}},    /* Q == 9 : 56-62% */
11678+    {{1107, 128}, {2083, 81}, {4006, 84}},   /* Q ==10 : 62-69% */
11679+    {{1177, 128}, {2379, 87}, {4785, 88}},   /* Q ==11 : 69-75% */
11680+    {{1242, 128}, {2415, 93}, {5155, 84}},   /* Q ==12 : 75-81% */
11681+    {{1349, 128}, {2644, 106}, {5260, 106}}, /* Q ==13 : 81-87% */
11682+    {{1455, 128}, {2422, 124}, {4174, 124}}, /* Q ==14 : 87-93% */
11683+    {{722, 128}, {1891, 145}, {1936, 146}},  /* Q ==15 : 93-99% */
11684+};
11685+
11686+/** HUF_selectDecoder() :
11687+*   Tells which decoder is likely to decode faster,
11688+*   based on a set of pre-determined metrics.
11689+*   @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
11690+*   Assumption : 0 < cSrcSize < dstSize <= 128 KB */
11691+U32 HUF_selectDecoder(size_t dstSize, size_t cSrcSize)
11692+{
11693+	/* decoder timing evaluation */
11694+	U32 const Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */
11695+	U32 const D256 = (U32)(dstSize >> 8);
11696+	U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);
11697+	U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);
11698+	DTime1 += DTime1 >> 3; /* advantage to algorithm using less memory, for cache eviction */
11699+
11700+	return DTime1 < DTime0;
11701+}
11702+
11703+typedef size_t (*decompressionAlgo)(void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize);
11704+
11705+size_t HUF_decompress4X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
11706+{
11707+	/* validation checks */
11708+	if (dstSize == 0)
11709+		return ERROR(dstSize_tooSmall);
11710+	if (cSrcSize > dstSize)
11711+		return ERROR(corruption_detected); /* invalid */
11712+	if (cSrcSize == dstSize) {
11713+		memcpy(dst, cSrc, dstSize);
11714+		return dstSize;
11715+	} /* not compressed */
11716+	if (cSrcSize == 1) {
11717+		memset(dst, *(const BYTE *)cSrc, dstSize);
11718+		return dstSize;
11719+	} /* RLE */
11720+
11721+	{
11722+		U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
11723+		return algoNb ? HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize)
11724+			      : HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize);
11725+	}
11726+}
11727+
11728+size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
11729+{
11730+	/* validation checks */
11731+	if (dstSize == 0)
11732+		return ERROR(dstSize_tooSmall);
11733+	if ((cSrcSize >= dstSize) || (cSrcSize <= 1))
11734+		return ERROR(corruption_detected); /* invalid */
11735+
11736+	{
11737+		U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
11738+		return algoNb ? HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize)
11739+			      : HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize);
11740+	}
11741+}
11742+
11743+size_t HUF_decompress1X_DCtx_wksp(HUF_DTable *dctx, void *dst, size_t dstSize, const void *cSrc, size_t cSrcSize, void *workspace, size_t workspaceSize)
11744+{
11745+	/* validation checks */
11746+	if (dstSize == 0)
11747+		return ERROR(dstSize_tooSmall);
11748+	if (cSrcSize > dstSize)
11749+		return ERROR(corruption_detected); /* invalid */
11750+	if (cSrcSize == dstSize) {
11751+		memcpy(dst, cSrc, dstSize);
11752+		return dstSize;
11753+	} /* not compressed */
11754+	if (cSrcSize == 1) {
11755+		memset(dst, *(const BYTE *)cSrc, dstSize);
11756+		return dstSize;
11757+	} /* RLE */
11758+
11759+	{
11760+		U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
11761+		return algoNb ? HUF_decompress1X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize)
11762+			      : HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workspace, workspaceSize);
11763+	}
11764+}
11765diff --git a/lib/zstd/mem.h b/lib/zstd/mem.h
11766new file mode 100644
11767index 0000000..42a697b
11768--- /dev/null
11769+++ b/lib/zstd/mem.h
11770@@ -0,0 +1,149 @@
11771+/**
11772+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
11773+ * All rights reserved.
11774+ *
11775+ * This source code is licensed under the BSD-style license found in the
11776+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
11777+ *
11778+ * This program is free software; you can redistribute it and/or modify it under
11779+ * the terms of the GNU General Public License version 2 as published by the
11780+ * Free Software Foundation. This program is dual-licensed; you may select
11781+ * either version 2 of the GNU General Public License ("GPL") or BSD license
11782+ * ("BSD").
11783+ */
11784+
11785+#ifndef MEM_H_MODULE
11786+#define MEM_H_MODULE
11787+
11788+/*-****************************************
11789+*  Dependencies
11790+******************************************/
11791+#include <asm/unaligned.h>
11792+#include <linux/string.h> /* memcpy */
11793+#include <linux/types.h>  /* size_t, ptrdiff_t */
11794+
11795+/*-****************************************
11796+*  Compiler specifics
11797+******************************************/
11798+#define ZSTD_STATIC static __inline __attribute__((unused))
11799+
11800+/*-**************************************************************
11801+*  Basic Types
11802+*****************************************************************/
11803+typedef uint8_t BYTE;
11804+typedef uint16_t U16;
11805+typedef int16_t S16;
11806+typedef uint32_t U32;
11807+typedef int32_t S32;
11808+typedef uint64_t U64;
11809+typedef int64_t S64;
11810+typedef ptrdiff_t iPtrDiff;
11811+typedef uintptr_t uPtrDiff;
11812+
11813+/*-**************************************************************
11814+*  Memory I/O
11815+*****************************************************************/
11816+ZSTD_STATIC unsigned ZSTD_32bits(void) { return sizeof(size_t) == 4; }
11817+ZSTD_STATIC unsigned ZSTD_64bits(void) { return sizeof(size_t) == 8; }
11818+
11819+#if defined(__LITTLE_ENDIAN)
11820+#define ZSTD_LITTLE_ENDIAN 1
11821+#else
11822+#define ZSTD_LITTLE_ENDIAN 0
11823+#endif
11824+
11825+ZSTD_STATIC unsigned ZSTD_isLittleEndian(void) { return ZSTD_LITTLE_ENDIAN; }
11826+
11827+ZSTD_STATIC U16 ZSTD_read16(const void *memPtr) { return get_unaligned((const U16 *)memPtr); }
11828+
11829+ZSTD_STATIC U32 ZSTD_read32(const void *memPtr) { return get_unaligned((const U32 *)memPtr); }
11830+
11831+ZSTD_STATIC U64 ZSTD_read64(const void *memPtr) { return get_unaligned((const U64 *)memPtr); }
11832+
11833+ZSTD_STATIC size_t ZSTD_readST(const void *memPtr) { return get_unaligned((const size_t *)memPtr); }
11834+
11835+ZSTD_STATIC void ZSTD_write16(void *memPtr, U16 value) { put_unaligned(value, (U16 *)memPtr); }
11836+
11837+ZSTD_STATIC void ZSTD_write32(void *memPtr, U32 value) { put_unaligned(value, (U32 *)memPtr); }
11838+
11839+ZSTD_STATIC void ZSTD_write64(void *memPtr, U64 value) { put_unaligned(value, (U64 *)memPtr); }
11840+
11841+/*=== Little endian r/w ===*/
11842+
11843+ZSTD_STATIC U16 ZSTD_readLE16(const void *memPtr) { return get_unaligned_le16(memPtr); }
11844+
11845+ZSTD_STATIC void ZSTD_writeLE16(void *memPtr, U16 val) { put_unaligned_le16(val, memPtr); }
11846+
11847+ZSTD_STATIC U32 ZSTD_readLE24(const void *memPtr) { return ZSTD_readLE16(memPtr) + (((const BYTE *)memPtr)[2] << 16); }
11848+
11849+ZSTD_STATIC void ZSTD_writeLE24(void *memPtr, U32 val)
11850+{
11851+	ZSTD_writeLE16(memPtr, (U16)val);
11852+	((BYTE *)memPtr)[2] = (BYTE)(val >> 16);
11853+}
11854+
11855+ZSTD_STATIC U32 ZSTD_readLE32(const void *memPtr) { return get_unaligned_le32(memPtr); }
11856+
11857+ZSTD_STATIC void ZSTD_writeLE32(void *memPtr, U32 val32) { put_unaligned_le32(val32, memPtr); }
11858+
11859+ZSTD_STATIC U64 ZSTD_readLE64(const void *memPtr) { return get_unaligned_le64(memPtr); }
11860+
11861+ZSTD_STATIC void ZSTD_writeLE64(void *memPtr, U64 val64) { put_unaligned_le64(val64, memPtr); }
11862+
11863+ZSTD_STATIC size_t ZSTD_readLEST(const void *memPtr)
11864+{
11865+	if (ZSTD_32bits())
11866+		return (size_t)ZSTD_readLE32(memPtr);
11867+	else
11868+		return (size_t)ZSTD_readLE64(memPtr);
11869+}
11870+
11871+ZSTD_STATIC void ZSTD_writeLEST(void *memPtr, size_t val)
11872+{
11873+	if (ZSTD_32bits())
11874+		ZSTD_writeLE32(memPtr, (U32)val);
11875+	else
11876+		ZSTD_writeLE64(memPtr, (U64)val);
11877+}
11878+
11879+/*=== Big endian r/w ===*/
11880+
11881+ZSTD_STATIC U32 ZSTD_readBE32(const void *memPtr) { return get_unaligned_be32(memPtr); }
11882+
11883+ZSTD_STATIC void ZSTD_writeBE32(void *memPtr, U32 val32) { put_unaligned_be32(val32, memPtr); }
11884+
11885+ZSTD_STATIC U64 ZSTD_readBE64(const void *memPtr) { return get_unaligned_be64(memPtr); }
11886+
11887+ZSTD_STATIC void ZSTD_writeBE64(void *memPtr, U64 val64) { put_unaligned_be64(val64, memPtr); }
11888+
11889+ZSTD_STATIC size_t ZSTD_readBEST(const void *memPtr)
11890+{
11891+	if (ZSTD_32bits())
11892+		return (size_t)ZSTD_readBE32(memPtr);
11893+	else
11894+		return (size_t)ZSTD_readBE64(memPtr);
11895+}
11896+
11897+ZSTD_STATIC void ZSTD_writeBEST(void *memPtr, size_t val)
11898+{
11899+	if (ZSTD_32bits())
11900+		ZSTD_writeBE32(memPtr, (U32)val);
11901+	else
11902+		ZSTD_writeBE64(memPtr, (U64)val);
11903+}
11904+
11905+/* function safe only for comparisons */
11906+ZSTD_STATIC U32 ZSTD_readMINMATCH(const void *memPtr, U32 length)
11907+{
11908+	switch (length) {
11909+	default:
11910+	case 4: return ZSTD_read32(memPtr);
11911+	case 3:
11912+		if (ZSTD_isLittleEndian())
11913+			return ZSTD_read32(memPtr) << 8;
11914+		else
11915+			return ZSTD_read32(memPtr) >> 8;
11916+	}
11917+}
11918+
11919+#endif /* MEM_H_MODULE */
11920diff --git a/lib/zstd/zstd_common.c b/lib/zstd/zstd_common.c
11921new file mode 100644
11922index 0000000..e5f06d7
11923--- /dev/null
11924+++ b/lib/zstd/zstd_common.c
11925@@ -0,0 +1,73 @@
11926+/**
11927+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
11928+ * All rights reserved.
11929+ *
11930+ * This source code is licensed under the BSD-style license found in the
11931+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
11932+ *
11933+ * This program is free software; you can redistribute it and/or modify it under
11934+ * the terms of the GNU General Public License version 2 as published by the
11935+ * Free Software Foundation. This program is dual-licensed; you may select
11936+ * either version 2 of the GNU General Public License ("GPL") or BSD license
11937+ * ("BSD").
11938+ */
11939+
11940+/*-*************************************
11941+*  Dependencies
11942+***************************************/
11943+#include "error_private.h"
11944+#include "zstd_internal.h" /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode, ZSTD_getErrorString, ZSTD_versionNumber */
11945+#include <linux/kernel.h>
11946+
11947+/*=**************************************************************
11948+*  Custom allocator
11949+****************************************************************/
11950+
11951+#define stack_push(stack, size)                                 \
11952+	({                                                      \
11953+		void *const ptr = ZSTD_PTR_ALIGN((stack)->ptr); \
11954+		(stack)->ptr = (char *)ptr + (size);            \
11955+		(stack)->ptr <= (stack)->end ? ptr : NULL;      \
11956+	})
11957+
11958+ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize)
11959+{
11960+	ZSTD_customMem stackMem = {ZSTD_stackAlloc, ZSTD_stackFree, workspace};
11961+	ZSTD_stack *stack = (ZSTD_stack *)workspace;
11962+	/* Verify preconditions */
11963+	if (!workspace || workspaceSize < sizeof(ZSTD_stack) || workspace != ZSTD_PTR_ALIGN(workspace)) {
11964+		ZSTD_customMem error = {NULL, NULL, NULL};
11965+		return error;
11966+	}
11967+	/* Initialize the stack */
11968+	stack->ptr = workspace;
11969+	stack->end = (char *)workspace + workspaceSize;
11970+	stack_push(stack, sizeof(ZSTD_stack));
11971+	return stackMem;
11972+}
11973+
11974+void *ZSTD_stackAllocAll(void *opaque, size_t *size)
11975+{
11976+	ZSTD_stack *stack = (ZSTD_stack *)opaque;
11977+	*size = (BYTE const *)stack->end - (BYTE *)ZSTD_PTR_ALIGN(stack->ptr);
11978+	return stack_push(stack, *size);
11979+}
11980+
11981+void *ZSTD_stackAlloc(void *opaque, size_t size)
11982+{
11983+	ZSTD_stack *stack = (ZSTD_stack *)opaque;
11984+	return stack_push(stack, size);
11985+}
11986+void ZSTD_stackFree(void *opaque, void *address)
11987+{
11988+	(void)opaque;
11989+	(void)address;
11990+}
11991+
11992+void *ZSTD_malloc(size_t size, ZSTD_customMem customMem) { return customMem.customAlloc(customMem.opaque, size); }
11993+
11994+void ZSTD_free(void *ptr, ZSTD_customMem customMem)
11995+{
11996+	if (ptr != NULL)
11997+		customMem.customFree(customMem.opaque, ptr);
11998+}
11999diff --git a/lib/zstd/zstd_internal.h b/lib/zstd/zstd_internal.h
12000new file mode 100644
12001index 0000000..a0fb83e
12002--- /dev/null
12003+++ b/lib/zstd/zstd_internal.h
12004@@ -0,0 +1,261 @@
12005+/**
12006+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
12007+ * All rights reserved.
12008+ *
12009+ * This source code is licensed under the BSD-style license found in the
12010+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
12011+ *
12012+ * This program is free software; you can redistribute it and/or modify it under
12013+ * the terms of the GNU General Public License version 2 as published by the
12014+ * Free Software Foundation. This program is dual-licensed; you may select
12015+ * either version 2 of the GNU General Public License ("GPL") or BSD license
12016+ * ("BSD").
12017+ */
12018+
12019+#ifndef ZSTD_CCOMMON_H_MODULE
12020+#define ZSTD_CCOMMON_H_MODULE
12021+
12022+/*-*******************************************************
12023+*  Compiler specifics
12024+*********************************************************/
12025+#define FORCE_INLINE static __always_inline
12026+#define FORCE_NOINLINE static noinline
12027+
12028+/*-*************************************
12029+*  Dependencies
12030+***************************************/
12031+#include "error_private.h"
12032+#include "mem.h"
12033+#include <linux/compiler.h>
12034+#include <linux/kernel.h>
12035+#include <linux/xxhash.h>
12036+#include <linux/zstd.h>
12037+
12038+/*-*************************************
12039+*  shared macros
12040+***************************************/
12041+#define MIN(a, b) ((a) < (b) ? (a) : (b))
12042+#define MAX(a, b) ((a) > (b) ? (a) : (b))
12043+#define CHECK_F(f)                       \
12044+	{                                \
12045+		size_t const errcod = f; \
12046+		if (ERR_isError(errcod)) \
12047+			return errcod;   \
12048+	} /* check and Forward error code */
12049+#define CHECK_E(f, e)                    \
12050+	{                                \
12051+		size_t const errcod = f; \
12052+		if (ERR_isError(errcod)) \
12053+			return ERROR(e); \
12054+	} /* check and send Error code */
12055+#define ZSTD_STATIC_ASSERT(c)                                   \
12056+	{                                                       \
12057+		enum { ZSTD_static_assert = 1 / (int)(!!(c)) }; \
12058+	}
12059+
12060+/*-*************************************
12061+*  Common constants
12062+***************************************/
12063+#define ZSTD_OPT_NUM (1 << 12)
12064+#define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */
12065+
12066+#define ZSTD_REP_NUM 3		      /* number of repcodes */
12067+#define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
12068+#define ZSTD_REP_MOVE (ZSTD_REP_NUM - 1)
12069+#define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
12070+static const U32 repStartValue[ZSTD_REP_NUM] = {1, 4, 8};
12071+
12072+#define KB *(1 << 10)
12073+#define MB *(1 << 20)
12074+#define GB *(1U << 30)
12075+
12076+#define BIT7 128
12077+#define BIT6 64
12078+#define BIT5 32
12079+#define BIT4 16
12080+#define BIT1 2
12081+#define BIT0 1
12082+
12083+#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
12084+static const size_t ZSTD_fcs_fieldSize[4] = {0, 2, 4, 8};
12085+static const size_t ZSTD_did_fieldSize[4] = {0, 1, 2, 4};
12086+
12087+#define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
12088+static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
12089+typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
12090+
12091+#define MIN_SEQUENCES_SIZE 1									  /* nbSeq==0 */
12092+#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
12093+
12094+#define HufLog 12
12095+typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
12096+
12097+#define LONGNBSEQ 0x7F00
12098+
12099+#define MINMATCH 3
12100+#define EQUAL_READ32 4
12101+
12102+#define Litbits 8
12103+#define MaxLit ((1 << Litbits) - 1)
12104+#define MaxML 52
12105+#define MaxLL 35
12106+#define MaxOff 28
12107+#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
12108+#define MLFSELog 9
12109+#define LLFSELog 9
12110+#define OffFSELog 8
12111+
12112+static const U32 LL_bits[MaxLL + 1] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
12113+static const S16 LL_defaultNorm[MaxLL + 1] = {4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, -1, -1, -1, -1};
12114+#define LL_DEFAULTNORMLOG 6 /* for static allocation */
12115+static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
12116+
12117+static const U32 ML_bits[MaxML + 1] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0, 0,
12118+				       0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
12119+static const S16 ML_defaultNorm[MaxML + 1] = {1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  1,  1,  1,  1,  1,  1, 1,
12120+					      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1};
12121+#define ML_DEFAULTNORMLOG 6 /* for static allocation */
12122+static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
12123+
12124+static const S16 OF_defaultNorm[MaxOff + 1] = {1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1};
12125+#define OF_DEFAULTNORMLOG 5 /* for static allocation */
12126+static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
12127+
12128+/*-*******************************************
12129+*  Shared functions to include for inlining
12130+*********************************************/
12131+ZSTD_STATIC void ZSTD_copy8(void *dst, const void *src) {
12132+	memcpy(dst, src, 8);
12133+}
12134+/*! ZSTD_wildcopy() :
12135+*   custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
12136+#define WILDCOPY_OVERLENGTH 8
12137+ZSTD_STATIC void ZSTD_wildcopy(void *dst, const void *src, ptrdiff_t length)
12138+{
12139+	const BYTE* ip = (const BYTE*)src;
12140+	BYTE* op = (BYTE*)dst;
12141+	BYTE* const oend = op + length;
12142+	/* Work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81388.
12143+	 * Avoid the bad case where the loop only runs once by handling the
12144+	 * special case separately. This doesn't trigger the bug because it
12145+	 * doesn't involve pointer/integer overflow.
12146+	 */
12147+	if (length <= 8)
12148+		return ZSTD_copy8(dst, src);
12149+	do {
12150+		ZSTD_copy8(op, ip);
12151+		op += 8;
12152+		ip += 8;
12153+	} while (op < oend);
12154+}
12155+
12156+/*-*******************************************
12157+*  Private interfaces
12158+*********************************************/
12159+typedef struct ZSTD_stats_s ZSTD_stats_t;
12160+
12161+typedef struct {
12162+	U32 off;
12163+	U32 len;
12164+} ZSTD_match_t;
12165+
12166+typedef struct {
12167+	U32 price;
12168+	U32 off;
12169+	U32 mlen;
12170+	U32 litlen;
12171+	U32 rep[ZSTD_REP_NUM];
12172+} ZSTD_optimal_t;
12173+
12174+typedef struct seqDef_s {
12175+	U32 offset;
12176+	U16 litLength;
12177+	U16 matchLength;
12178+} seqDef;
12179+
12180+typedef struct {
12181+	seqDef *sequencesStart;
12182+	seqDef *sequences;
12183+	BYTE *litStart;
12184+	BYTE *lit;
12185+	BYTE *llCode;
12186+	BYTE *mlCode;
12187+	BYTE *ofCode;
12188+	U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
12189+	U32 longLengthPos;
12190+	/* opt */
12191+	ZSTD_optimal_t *priceTable;
12192+	ZSTD_match_t *matchTable;
12193+	U32 *matchLengthFreq;
12194+	U32 *litLengthFreq;
12195+	U32 *litFreq;
12196+	U32 *offCodeFreq;
12197+	U32 matchLengthSum;
12198+	U32 matchSum;
12199+	U32 litLengthSum;
12200+	U32 litSum;
12201+	U32 offCodeSum;
12202+	U32 log2matchLengthSum;
12203+	U32 log2matchSum;
12204+	U32 log2litLengthSum;
12205+	U32 log2litSum;
12206+	U32 log2offCodeSum;
12207+	U32 factor;
12208+	U32 staticPrices;
12209+	U32 cachedPrice;
12210+	U32 cachedLitLength;
12211+	const BYTE *cachedLiterals;
12212+} seqStore_t;
12213+
12214+const seqStore_t *ZSTD_getSeqStore(const ZSTD_CCtx *ctx);
12215+void ZSTD_seqToCodes(const seqStore_t *seqStorePtr);
12216+int ZSTD_isSkipFrame(ZSTD_DCtx *dctx);
12217+
12218+/*= Custom memory allocation functions */
12219+typedef void *(*ZSTD_allocFunction)(void *opaque, size_t size);
12220+typedef void (*ZSTD_freeFunction)(void *opaque, void *address);
12221+typedef struct {
12222+	ZSTD_allocFunction customAlloc;
12223+	ZSTD_freeFunction customFree;
12224+	void *opaque;
12225+} ZSTD_customMem;
12226+
12227+void *ZSTD_malloc(size_t size, ZSTD_customMem customMem);
12228+void ZSTD_free(void *ptr, ZSTD_customMem customMem);
12229+
12230+/*====== stack allocation  ======*/
12231+
12232+typedef struct {
12233+	void *ptr;
12234+	const void *end;
12235+} ZSTD_stack;
12236+
12237+#define ZSTD_ALIGN(x) ALIGN(x, sizeof(size_t))
12238+#define ZSTD_PTR_ALIGN(p) PTR_ALIGN(p, sizeof(size_t))
12239+
12240+ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize);
12241+
12242+void *ZSTD_stackAllocAll(void *opaque, size_t *size);
12243+void *ZSTD_stackAlloc(void *opaque, size_t size);
12244+void ZSTD_stackFree(void *opaque, void *address);
12245+
12246+/*======  common function  ======*/
12247+
12248+ZSTD_STATIC U32 ZSTD_highbit32(U32 val) { return 31 - __builtin_clz(val); }
12249+
12250+/* hidden functions */
12251+
12252+/* ZSTD_invalidateRepCodes() :
12253+ * ensures next compression will not use repcodes from previous block.
12254+ * Note : only works with regular variant;
12255+ *        do not use with extDict variant ! */
12256+void ZSTD_invalidateRepCodes(ZSTD_CCtx *cctx);
12257+
12258+size_t ZSTD_freeCCtx(ZSTD_CCtx *cctx);
12259+size_t ZSTD_freeDCtx(ZSTD_DCtx *dctx);
12260+size_t ZSTD_freeCDict(ZSTD_CDict *cdict);
12261+size_t ZSTD_freeDDict(ZSTD_DDict *cdict);
12262+size_t ZSTD_freeCStream(ZSTD_CStream *zcs);
12263+size_t ZSTD_freeDStream(ZSTD_DStream *zds);
12264+
12265+#endif /* ZSTD_CCOMMON_H_MODULE */
12266diff --git a/lib/zstd/zstd_opt.h b/lib/zstd/zstd_opt.h
12267new file mode 100644
12268index 0000000..ecdd725
12269--- /dev/null
12270+++ b/lib/zstd/zstd_opt.h
12271@@ -0,0 +1,1012 @@
12272+/**
12273+ * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
12274+ * All rights reserved.
12275+ *
12276+ * This source code is licensed under the BSD-style license found in the
12277+ * LICENSE file in the root directory of https://github.com/facebook/zstd.
12278+ *
12279+ * This program is free software; you can redistribute it and/or modify it under
12280+ * the terms of the GNU General Public License version 2 as published by the
12281+ * Free Software Foundation. This program is dual-licensed; you may select
12282+ * either version 2 of the GNU General Public License ("GPL") or BSD license
12283+ * ("BSD").
12284+ */
12285+
12286+/* Note : this file is intended to be included within zstd_compress.c */
12287+
12288+#ifndef ZSTD_OPT_H_91842398743
12289+#define ZSTD_OPT_H_91842398743
12290+
12291+#define ZSTD_LITFREQ_ADD 2
12292+#define ZSTD_FREQ_DIV 4
12293+#define ZSTD_MAX_PRICE (1 << 30)
12294+
12295+/*-*************************************
12296+*  Price functions for optimal parser
12297+***************************************/
12298+FORCE_INLINE void ZSTD_setLog2Prices(seqStore_t *ssPtr)
12299+{
12300+	ssPtr->log2matchLengthSum = ZSTD_highbit32(ssPtr->matchLengthSum + 1);
12301+	ssPtr->log2litLengthSum = ZSTD_highbit32(ssPtr->litLengthSum + 1);
12302+	ssPtr->log2litSum = ZSTD_highbit32(ssPtr->litSum + 1);
12303+	ssPtr->log2offCodeSum = ZSTD_highbit32(ssPtr->offCodeSum + 1);
12304+	ssPtr->factor = 1 + ((ssPtr->litSum >> 5) / ssPtr->litLengthSum) + ((ssPtr->litSum << 1) / (ssPtr->litSum + ssPtr->matchSum));
12305+}
12306+
12307+ZSTD_STATIC void ZSTD_rescaleFreqs(seqStore_t *ssPtr, const BYTE *src, size_t srcSize)
12308+{
12309+	unsigned u;
12310+
12311+	ssPtr->cachedLiterals = NULL;
12312+	ssPtr->cachedPrice = ssPtr->cachedLitLength = 0;
12313+	ssPtr->staticPrices = 0;
12314+
12315+	if (ssPtr->litLengthSum == 0) {
12316+		if (srcSize <= 1024)
12317+			ssPtr->staticPrices = 1;
12318+
12319+		for (u = 0; u <= MaxLit; u++)
12320+			ssPtr->litFreq[u] = 0;
12321+		for (u = 0; u < srcSize; u++)
12322+			ssPtr->litFreq[src[u]]++;
12323+
12324+		ssPtr->litSum = 0;
12325+		ssPtr->litLengthSum = MaxLL + 1;
12326+		ssPtr->matchLengthSum = MaxML + 1;
12327+		ssPtr->offCodeSum = (MaxOff + 1);
12328+		ssPtr->matchSum = (ZSTD_LITFREQ_ADD << Litbits);
12329+
12330+		for (u = 0; u <= MaxLit; u++) {
12331+			ssPtr->litFreq[u] = 1 + (ssPtr->litFreq[u] >> ZSTD_FREQ_DIV);
12332+			ssPtr->litSum += ssPtr->litFreq[u];
12333+		}
12334+		for (u = 0; u <= MaxLL; u++)
12335+			ssPtr->litLengthFreq[u] = 1;
12336+		for (u = 0; u <= MaxML; u++)
12337+			ssPtr->matchLengthFreq[u] = 1;
12338+		for (u = 0; u <= MaxOff; u++)
12339+			ssPtr->offCodeFreq[u] = 1;
12340+	} else {
12341+		ssPtr->matchLengthSum = 0;
12342+		ssPtr->litLengthSum = 0;
12343+		ssPtr->offCodeSum = 0;
12344+		ssPtr->matchSum = 0;
12345+		ssPtr->litSum = 0;
12346+
12347+		for (u = 0; u <= MaxLit; u++) {
12348+			ssPtr->litFreq[u] = 1 + (ssPtr->litFreq[u] >> (ZSTD_FREQ_DIV + 1));
12349+			ssPtr->litSum += ssPtr->litFreq[u];
12350+		}
12351+		for (u = 0; u <= MaxLL; u++) {
12352+			ssPtr->litLengthFreq[u] = 1 + (ssPtr->litLengthFreq[u] >> (ZSTD_FREQ_DIV + 1));
12353+			ssPtr->litLengthSum += ssPtr->litLengthFreq[u];
12354+		}
12355+		for (u = 0; u <= MaxML; u++) {
12356+			ssPtr->matchLengthFreq[u] = 1 + (ssPtr->matchLengthFreq[u] >> ZSTD_FREQ_DIV);
12357+			ssPtr->matchLengthSum += ssPtr->matchLengthFreq[u];
12358+			ssPtr->matchSum += ssPtr->matchLengthFreq[u] * (u + 3);
12359+		}
12360+		ssPtr->matchSum *= ZSTD_LITFREQ_ADD;
12361+		for (u = 0; u <= MaxOff; u++) {
12362+			ssPtr->offCodeFreq[u] = 1 + (ssPtr->offCodeFreq[u] >> ZSTD_FREQ_DIV);
12363+			ssPtr->offCodeSum += ssPtr->offCodeFreq[u];
12364+		}
12365+	}
12366+
12367+	ZSTD_setLog2Prices(ssPtr);
12368+}
12369+
12370+FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t *ssPtr, U32 litLength, const BYTE *literals)
12371+{
12372+	U32 price, u;
12373+
12374+	if (ssPtr->staticPrices)
12375+		return ZSTD_highbit32((U32)litLength + 1) + (litLength * 6);
12376+
12377+	if (litLength == 0)
12378+		return ssPtr->log2litLengthSum - ZSTD_highbit32(ssPtr->litLengthFreq[0] + 1);
12379+
12380+	/* literals */
12381+	if (ssPtr->cachedLiterals == literals) {
12382+		U32 const additional = litLength - ssPtr->cachedLitLength;
12383+		const BYTE *literals2 = ssPtr->cachedLiterals + ssPtr->cachedLitLength;
12384+		price = ssPtr->cachedPrice + additional * ssPtr->log2litSum;
12385+		for (u = 0; u < additional; u++)
12386+			price -= ZSTD_highbit32(ssPtr->litFreq[literals2[u]] + 1);
12387+		ssPtr->cachedPrice = price;
12388+		ssPtr->cachedLitLength = litLength;
12389+	} else {
12390+		price = litLength * ssPtr->log2litSum;
12391+		for (u = 0; u < litLength; u++)
12392+			price -= ZSTD_highbit32(ssPtr->litFreq[literals[u]] + 1);
12393+
12394+		if (litLength >= 12) {
12395+			ssPtr->cachedLiterals = literals;
12396+			ssPtr->cachedPrice = price;
12397+			ssPtr->cachedLitLength = litLength;
12398+		}
12399+	}
12400+
12401+	/* literal Length */
12402+	{
12403+		const BYTE LL_deltaCode = 19;
12404+		const BYTE llCode = (litLength > 63) ? (BYTE)ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
12405+		price += LL_bits[llCode] + ssPtr->log2litLengthSum - ZSTD_highbit32(ssPtr->litLengthFreq[llCode] + 1);
12406+	}
12407+
12408+	return price;
12409+}
12410+
12411+FORCE_INLINE U32 ZSTD_getPrice(seqStore_t *seqStorePtr, U32 litLength, const BYTE *literals, U32 offset, U32 matchLength, const int ultra)
12412+{
12413+	/* offset */
12414+	U32 price;
12415+	BYTE const offCode = (BYTE)ZSTD_highbit32(offset + 1);
12416+
12417+	if (seqStorePtr->staticPrices)
12418+		return ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit32((U32)matchLength + 1) + 16 + offCode;
12419+
12420+	price = offCode + seqStorePtr->log2offCodeSum - ZSTD_highbit32(seqStorePtr->offCodeFreq[offCode] + 1);
12421+	if (!ultra && offCode >= 20)
12422+		price += (offCode - 19) * 2;
12423+
12424+	/* match Length */
12425+	{
12426+		const BYTE ML_deltaCode = 36;
12427+		const BYTE mlCode = (matchLength > 127) ? (BYTE)ZSTD_highbit32(matchLength) + ML_deltaCode : ML_Code[matchLength];
12428+		price += ML_bits[mlCode] + seqStorePtr->log2matchLengthSum - ZSTD_highbit32(seqStorePtr->matchLengthFreq[mlCode] + 1);
12429+	}
12430+
12431+	return price + ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + seqStorePtr->factor;
12432+}
12433+
12434+ZSTD_STATIC void ZSTD_updatePrice(seqStore_t *seqStorePtr, U32 litLength, const BYTE *literals, U32 offset, U32 matchLength)
12435+{
12436+	U32 u;
12437+
12438+	/* literals */
12439+	seqStorePtr->litSum += litLength * ZSTD_LITFREQ_ADD;
12440+	for (u = 0; u < litLength; u++)
12441+		seqStorePtr->litFreq[literals[u]] += ZSTD_LITFREQ_ADD;
12442+
12443+	/* literal Length */
12444+	{
12445+		const BYTE LL_deltaCode = 19;
12446+		const BYTE llCode = (litLength > 63) ? (BYTE)ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength];
12447+		seqStorePtr->litLengthFreq[llCode]++;
12448+		seqStorePtr->litLengthSum++;
12449+	}
12450+
12451+	/* match offset */
12452+	{
12453+		BYTE const offCode = (BYTE)ZSTD_highbit32(offset + 1);
12454+		seqStorePtr->offCodeSum++;
12455+		seqStorePtr->offCodeFreq[offCode]++;
12456+	}
12457+
12458+	/* match Length */
12459+	{
12460+		const BYTE ML_deltaCode = 36;
12461+		const BYTE mlCode = (matchLength > 127) ? (BYTE)ZSTD_highbit32(matchLength) + ML_deltaCode : ML_Code[matchLength];
12462+		seqStorePtr->matchLengthFreq[mlCode]++;
12463+		seqStorePtr->matchLengthSum++;
12464+	}
12465+
12466+	ZSTD_setLog2Prices(seqStorePtr);
12467+}
12468+
12469+#define SET_PRICE(pos, mlen_, offset_, litlen_, price_)           \
12470+	{                                                         \
12471+		while (last_pos < pos) {                          \
12472+			opt[last_pos + 1].price = ZSTD_MAX_PRICE; \
12473+			last_pos++;                               \
12474+		}                                                 \
12475+		opt[pos].mlen = mlen_;                            \
12476+		opt[pos].off = offset_;                           \
12477+		opt[pos].litlen = litlen_;                        \
12478+		opt[pos].price = price_;                          \
12479+	}
12480+
12481+/* Update hashTable3 up to ip (excluded)
12482+   Assumption : always within prefix (i.e. not within extDict) */
12483+FORCE_INLINE
12484+U32 ZSTD_insertAndFindFirstIndexHash3(ZSTD_CCtx *zc, const BYTE *ip)
12485+{
12486+	U32 *const hashTable3 = zc->hashTable3;
12487+	U32 const hashLog3 = zc->hashLog3;
12488+	const BYTE *const base = zc->base;
12489+	U32 idx = zc->nextToUpdate3;
12490+	const U32 target = zc->nextToUpdate3 = (U32)(ip - base);
12491+	const size_t hash3 = ZSTD_hash3Ptr(ip, hashLog3);
12492+
12493+	while (idx < target) {
12494+		hashTable3[ZSTD_hash3Ptr(base + idx, hashLog3)] = idx;
12495+		idx++;
12496+	}
12497+
12498+	return hashTable3[hash3];
12499+}
12500+
12501+/*-*************************************
12502+*  Binary Tree search
12503+***************************************/
12504+static U32 ZSTD_insertBtAndGetAllMatches(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iLimit, U32 nbCompares, const U32 mls, U32 extDict,
12505+					 ZSTD_match_t *matches, const U32 minMatchLen)
12506+{
12507+	const BYTE *const base = zc->base;
12508+	const U32 curr = (U32)(ip - base);
12509+	const U32 hashLog = zc->params.cParams.hashLog;
12510+	const size_t h = ZSTD_hashPtr(ip, hashLog, mls);
12511+	U32 *const hashTable = zc->hashTable;
12512+	U32 matchIndex = hashTable[h];
12513+	U32 *const bt = zc->chainTable;
12514+	const U32 btLog = zc->params.cParams.chainLog - 1;
12515+	const U32 btMask = (1U << btLog) - 1;
12516+	size_t commonLengthSmaller = 0, commonLengthLarger = 0;
12517+	const BYTE *const dictBase = zc->dictBase;
12518+	const U32 dictLimit = zc->dictLimit;
12519+	const BYTE *const dictEnd = dictBase + dictLimit;
12520+	const BYTE *const prefixStart = base + dictLimit;
12521+	const U32 btLow = btMask >= curr ? 0 : curr - btMask;
12522+	const U32 windowLow = zc->lowLimit;
12523+	U32 *smallerPtr = bt + 2 * (curr & btMask);
12524+	U32 *largerPtr = bt + 2 * (curr & btMask) + 1;
12525+	U32 matchEndIdx = curr + 8;
12526+	U32 dummy32; /* to be nullified at the end */
12527+	U32 mnum = 0;
12528+
12529+	const U32 minMatch = (mls == 3) ? 3 : 4;
12530+	size_t bestLength = minMatchLen - 1;
12531+
12532+	if (minMatch == 3) { /* HC3 match finder */
12533+		U32 const matchIndex3 = ZSTD_insertAndFindFirstIndexHash3(zc, ip);
12534+		if (matchIndex3 > windowLow && (curr - matchIndex3 < (1 << 18))) {
12535+			const BYTE *match;
12536+			size_t currMl = 0;
12537+			if ((!extDict) || matchIndex3 >= dictLimit) {
12538+				match = base + matchIndex3;
12539+				if (match[bestLength] == ip[bestLength])
12540+					currMl = ZSTD_count(ip, match, iLimit);
12541+			} else {
12542+				match = dictBase + matchIndex3;
12543+				if (ZSTD_readMINMATCH(match, MINMATCH) ==
12544+				    ZSTD_readMINMATCH(ip, MINMATCH)) /* assumption : matchIndex3 <= dictLimit-4 (by table construction) */
12545+					currMl = ZSTD_count_2segments(ip + MINMATCH, match + MINMATCH, iLimit, dictEnd, prefixStart) + MINMATCH;
12546+			}
12547+
12548+			/* save best solution */
12549+			if (currMl > bestLength) {
12550+				bestLength = currMl;
12551+				matches[mnum].off = ZSTD_REP_MOVE_OPT + curr - matchIndex3;
12552+				matches[mnum].len = (U32)currMl;
12553+				mnum++;
12554+				if (currMl > ZSTD_OPT_NUM)
12555+					goto update;
12556+				if (ip + currMl == iLimit)
12557+					goto update; /* best possible, and avoid read overflow*/
12558+			}
12559+		}
12560+	}
12561+
12562+	hashTable[h] = curr; /* Update Hash Table */
12563+
12564+	while (nbCompares-- && (matchIndex > windowLow)) {
12565+		U32 *nextPtr = bt + 2 * (matchIndex & btMask);
12566+		size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */
12567+		const BYTE *match;
12568+
12569+		if ((!extDict) || (matchIndex + matchLength >= dictLimit)) {
12570+			match = base + matchIndex;
12571+			if (match[matchLength] == ip[matchLength]) {
12572+				matchLength += ZSTD_count(ip + matchLength + 1, match + matchLength + 1, iLimit) + 1;
12573+			}
12574+		} else {
12575+			match = dictBase + matchIndex;
12576+			matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iLimit, dictEnd, prefixStart);
12577+			if (matchIndex + matchLength >= dictLimit)
12578+				match = base + matchIndex; /* to prepare for next usage of match[matchLength] */
12579+		}
12580+
12581+		if (matchLength > bestLength) {
12582+			if (matchLength > matchEndIdx - matchIndex)
12583+				matchEndIdx = matchIndex + (U32)matchLength;
12584+			bestLength = matchLength;
12585+			matches[mnum].off = ZSTD_REP_MOVE_OPT + curr - matchIndex;
12586+			matches[mnum].len = (U32)matchLength;
12587+			mnum++;
12588+			if (matchLength > ZSTD_OPT_NUM)
12589+				break;
12590+			if (ip + matchLength == iLimit) /* equal : no way to know if inf or sup */
12591+				break;			/* drop, to guarantee consistency (miss a little bit of compression) */
12592+		}
12593+
12594+		if (match[matchLength] < ip[matchLength]) {
12595+			/* match is smaller than curr */
12596+			*smallerPtr = matchIndex;	  /* update smaller idx */
12597+			commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */
12598+			if (matchIndex <= btLow) {
12599+				smallerPtr = &dummy32;
12600+				break;
12601+			}			  /* beyond tree size, stop the search */
12602+			smallerPtr = nextPtr + 1; /* new "smaller" => larger of match */
12603+			matchIndex = nextPtr[1];  /* new matchIndex larger than previous (closer to curr) */
12604+		} else {
12605+			/* match is larger than curr */
12606+			*largerPtr = matchIndex;
12607+			commonLengthLarger = matchLength;
12608+			if (matchIndex <= btLow) {
12609+				largerPtr = &dummy32;
12610+				break;
12611+			} /* beyond tree size, stop the search */
12612+			largerPtr = nextPtr;
12613+			matchIndex = nextPtr[0];
12614+		}
12615+	}
12616+
12617+	*smallerPtr = *largerPtr = 0;
12618+
12619+update:
12620+	zc->nextToUpdate = (matchEndIdx > curr + 8) ? matchEndIdx - 8 : curr + 1;
12621+	return mnum;
12622+}
12623+
12624+/** Tree updater, providing best match */
12625+static U32 ZSTD_BtGetAllMatches(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iLimit, const U32 maxNbAttempts, const U32 mls, ZSTD_match_t *matches,
12626+				const U32 minMatchLen)
12627+{
12628+	if (ip < zc->base + zc->nextToUpdate)
12629+		return 0; /* skipped area */
12630+	ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls);
12631+	return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 0, matches, minMatchLen);
12632+}
12633+
12634+static U32 ZSTD_BtGetAllMatches_selectMLS(ZSTD_CCtx *zc, /* Index table will be updated */
12635+					  const BYTE *ip, const BYTE *const iHighLimit, const U32 maxNbAttempts, const U32 matchLengthSearch,
12636+					  ZSTD_match_t *matches, const U32 minMatchLen)
12637+{
12638+	switch (matchLengthSearch) {
12639+	case 3: return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 3, matches, minMatchLen);
12640+	default:
12641+	case 4: return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minMatchLen);
12642+	case 5: return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minMatchLen);
12643+	case 7:
12644+	case 6: return ZSTD_BtGetAllMatches(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minMatchLen);
12645+	}
12646+}
12647+
12648+/** Tree updater, providing best match */
12649+static U32 ZSTD_BtGetAllMatches_extDict(ZSTD_CCtx *zc, const BYTE *const ip, const BYTE *const iLimit, const U32 maxNbAttempts, const U32 mls,
12650+					ZSTD_match_t *matches, const U32 minMatchLen)
12651+{
12652+	if (ip < zc->base + zc->nextToUpdate)
12653+		return 0; /* skipped area */
12654+	ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls);
12655+	return ZSTD_insertBtAndGetAllMatches(zc, ip, iLimit, maxNbAttempts, mls, 1, matches, minMatchLen);
12656+}
12657+
12658+static U32 ZSTD_BtGetAllMatches_selectMLS_extDict(ZSTD_CCtx *zc, /* Index table will be updated */
12659+						  const BYTE *ip, const BYTE *const iHighLimit, const U32 maxNbAttempts, const U32 matchLengthSearch,
12660+						  ZSTD_match_t *matches, const U32 minMatchLen)
12661+{
12662+	switch (matchLengthSearch) {
12663+	case 3: return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 3, matches, minMatchLen);
12664+	default:
12665+	case 4: return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 4, matches, minMatchLen);
12666+	case 5: return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 5, matches, minMatchLen);
12667+	case 7:
12668+	case 6: return ZSTD_BtGetAllMatches_extDict(zc, ip, iHighLimit, maxNbAttempts, 6, matches, minMatchLen);
12669+	}
12670+}
12671+
12672+/*-*******************************
12673+*  Optimal parser
12674+*********************************/
12675+FORCE_INLINE
12676+void ZSTD_compressBlock_opt_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const int ultra)
12677+{
12678+	seqStore_t *seqStorePtr = &(ctx->seqStore);
12679+	const BYTE *const istart = (const BYTE *)src;
12680+	const BYTE *ip = istart;
12681+	const BYTE *anchor = istart;
12682+	const BYTE *const iend = istart + srcSize;
12683+	const BYTE *const ilimit = iend - 8;
12684+	const BYTE *const base = ctx->base;
12685+	const BYTE *const prefixStart = base + ctx->dictLimit;
12686+
12687+	const U32 maxSearches = 1U << ctx->params.cParams.searchLog;
12688+	const U32 sufficient_len = ctx->params.cParams.targetLength;
12689+	const U32 mls = ctx->params.cParams.searchLength;
12690+	const U32 minMatch = (ctx->params.cParams.searchLength == 3) ? 3 : 4;
12691+
12692+	ZSTD_optimal_t *opt = seqStorePtr->priceTable;
12693+	ZSTD_match_t *matches = seqStorePtr->matchTable;
12694+	const BYTE *inr;
12695+	U32 offset, rep[ZSTD_REP_NUM];
12696+
12697+	/* init */
12698+	ctx->nextToUpdate3 = ctx->nextToUpdate;
12699+	ZSTD_rescaleFreqs(seqStorePtr, (const BYTE *)src, srcSize);
12700+	ip += (ip == prefixStart);
12701+	{
12702+		U32 i;
12703+		for (i = 0; i < ZSTD_REP_NUM; i++)
12704+			rep[i] = ctx->rep[i];
12705+	}
12706+
12707+	/* Match Loop */
12708+	while (ip < ilimit) {
12709+		U32 cur, match_num, last_pos, litlen, price;
12710+		U32 u, mlen, best_mlen, best_off, litLength;
12711+		memset(opt, 0, sizeof(ZSTD_optimal_t));
12712+		last_pos = 0;
12713+		litlen = (U32)(ip - anchor);
12714+
12715+		/* check repCode */
12716+		{
12717+			U32 i, last_i = ZSTD_REP_CHECK + (ip == anchor);
12718+			for (i = (ip == anchor); i < last_i; i++) {
12719+				const S32 repCur = (i == ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : rep[i];
12720+				if ((repCur > 0) && (repCur < (S32)(ip - prefixStart)) &&
12721+				    (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(ip - repCur, minMatch))) {
12722+					mlen = (U32)ZSTD_count(ip + minMatch, ip + minMatch - repCur, iend) + minMatch;
12723+					if (mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) {
12724+						best_mlen = mlen;
12725+						best_off = i;
12726+						cur = 0;
12727+						last_pos = 1;
12728+						goto _storeSequence;
12729+					}
12730+					best_off = i - (ip == anchor);
12731+					do {
12732+						price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
12733+						if (mlen > last_pos || price < opt[mlen].price)
12734+							SET_PRICE(mlen, mlen, i, litlen, price); /* note : macro modifies last_pos */
12735+						mlen--;
12736+					} while (mlen >= minMatch);
12737+				}
12738+			}
12739+		}
12740+
12741+		match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, ip, iend, maxSearches, mls, matches, minMatch);
12742+
12743+		if (!last_pos && !match_num) {
12744+			ip++;
12745+			continue;
12746+		}
12747+
12748+		if (match_num && (matches[match_num - 1].len > sufficient_len || matches[match_num - 1].len >= ZSTD_OPT_NUM)) {
12749+			best_mlen = matches[match_num - 1].len;
12750+			best_off = matches[match_num - 1].off;
12751+			cur = 0;
12752+			last_pos = 1;
12753+			goto _storeSequence;
12754+		}
12755+
12756+		/* set prices using matches at position = 0 */
12757+		best_mlen = (last_pos) ? last_pos : minMatch;
12758+		for (u = 0; u < match_num; u++) {
12759+			mlen = (u > 0) ? matches[u - 1].len + 1 : best_mlen;
12760+			best_mlen = matches[u].len;
12761+			while (mlen <= best_mlen) {
12762+				price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off - 1, mlen - MINMATCH, ultra);
12763+				if (mlen > last_pos || price < opt[mlen].price)
12764+					SET_PRICE(mlen, mlen, matches[u].off, litlen, price); /* note : macro modifies last_pos */
12765+				mlen++;
12766+			}
12767+		}
12768+
12769+		if (last_pos < minMatch) {
12770+			ip++;
12771+			continue;
12772+		}
12773+
12774+		/* initialize opt[0] */
12775+		{
12776+			U32 i;
12777+			for (i = 0; i < ZSTD_REP_NUM; i++)
12778+				opt[0].rep[i] = rep[i];
12779+		}
12780+		opt[0].mlen = 1;
12781+		opt[0].litlen = litlen;
12782+
12783+		/* check further positions */
12784+		for (cur = 1; cur <= last_pos; cur++) {
12785+			inr = ip + cur;
12786+
12787+			if (opt[cur - 1].mlen == 1) {
12788+				litlen = opt[cur - 1].litlen + 1;
12789+				if (cur > litlen) {
12790+					price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr - litlen);
12791+				} else
12792+					price = ZSTD_getLiteralPrice(seqStorePtr, litlen, anchor);
12793+			} else {
12794+				litlen = 1;
12795+				price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr - 1);
12796+			}
12797+
12798+			if (cur > last_pos || price <= opt[cur].price)
12799+				SET_PRICE(cur, 1, 0, litlen, price);
12800+
12801+			if (cur == last_pos)
12802+				break;
12803+
12804+			if (inr > ilimit) /* last match must start at a minimum distance of 8 from oend */
12805+				continue;
12806+
12807+			mlen = opt[cur].mlen;
12808+			if (opt[cur].off > ZSTD_REP_MOVE_OPT) {
12809+				opt[cur].rep[2] = opt[cur - mlen].rep[1];
12810+				opt[cur].rep[1] = opt[cur - mlen].rep[0];
12811+				opt[cur].rep[0] = opt[cur].off - ZSTD_REP_MOVE_OPT;
12812+			} else {
12813+				opt[cur].rep[2] = (opt[cur].off > 1) ? opt[cur - mlen].rep[1] : opt[cur - mlen].rep[2];
12814+				opt[cur].rep[1] = (opt[cur].off > 0) ? opt[cur - mlen].rep[0] : opt[cur - mlen].rep[1];
12815+				opt[cur].rep[0] =
12816+				    ((opt[cur].off == ZSTD_REP_MOVE_OPT) && (mlen != 1)) ? (opt[cur - mlen].rep[0] - 1) : (opt[cur - mlen].rep[opt[cur].off]);
12817+			}
12818+
12819+			best_mlen = minMatch;
12820+			{
12821+				U32 i, last_i = ZSTD_REP_CHECK + (mlen != 1);
12822+				for (i = (opt[cur].mlen != 1); i < last_i; i++) { /* check rep */
12823+					const S32 repCur = (i == ZSTD_REP_MOVE_OPT) ? (opt[cur].rep[0] - 1) : opt[cur].rep[i];
12824+					if ((repCur > 0) && (repCur < (S32)(inr - prefixStart)) &&
12825+					    (ZSTD_readMINMATCH(inr, minMatch) == ZSTD_readMINMATCH(inr - repCur, minMatch))) {
12826+						mlen = (U32)ZSTD_count(inr + minMatch, inr + minMatch - repCur, iend) + minMatch;
12827+
12828+						if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) {
12829+							best_mlen = mlen;
12830+							best_off = i;
12831+							last_pos = cur + 1;
12832+							goto _storeSequence;
12833+						}
12834+
12835+						best_off = i - (opt[cur].mlen != 1);
12836+						if (mlen > best_mlen)
12837+							best_mlen = mlen;
12838+
12839+						do {
12840+							if (opt[cur].mlen == 1) {
12841+								litlen = opt[cur].litlen;
12842+								if (cur > litlen) {
12843+									price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr - litlen,
12844+															best_off, mlen - MINMATCH, ultra);
12845+								} else
12846+									price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
12847+							} else {
12848+								litlen = 0;
12849+								price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, best_off, mlen - MINMATCH, ultra);
12850+							}
12851+
12852+							if (cur + mlen > last_pos || price <= opt[cur + mlen].price)
12853+								SET_PRICE(cur + mlen, mlen, i, litlen, price);
12854+							mlen--;
12855+						} while (mlen >= minMatch);
12856+					}
12857+				}
12858+			}
12859+
12860+			match_num = ZSTD_BtGetAllMatches_selectMLS(ctx, inr, iend, maxSearches, mls, matches, best_mlen);
12861+
12862+			if (match_num > 0 && (matches[match_num - 1].len > sufficient_len || cur + matches[match_num - 1].len >= ZSTD_OPT_NUM)) {
12863+				best_mlen = matches[match_num - 1].len;
12864+				best_off = matches[match_num - 1].off;
12865+				last_pos = cur + 1;
12866+				goto _storeSequence;
12867+			}
12868+
12869+			/* set prices using matches at position = cur */
12870+			for (u = 0; u < match_num; u++) {
12871+				mlen = (u > 0) ? matches[u - 1].len + 1 : best_mlen;
12872+				best_mlen = matches[u].len;
12873+
12874+				while (mlen <= best_mlen) {
12875+					if (opt[cur].mlen == 1) {
12876+						litlen = opt[cur].litlen;
12877+						if (cur > litlen)
12878+							price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip + cur - litlen,
12879+													matches[u].off - 1, mlen - MINMATCH, ultra);
12880+						else
12881+							price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off - 1, mlen - MINMATCH, ultra);
12882+					} else {
12883+						litlen = 0;
12884+						price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off - 1, mlen - MINMATCH, ultra);
12885+					}
12886+
12887+					if (cur + mlen > last_pos || (price < opt[cur + mlen].price))
12888+						SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price);
12889+
12890+					mlen++;
12891+				}
12892+			}
12893+		}
12894+
12895+		best_mlen = opt[last_pos].mlen;
12896+		best_off = opt[last_pos].off;
12897+		cur = last_pos - best_mlen;
12898+
12899+	/* store sequence */
12900+_storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */
12901+		opt[0].mlen = 1;
12902+
12903+		while (1) {
12904+			mlen = opt[cur].mlen;
12905+			offset = opt[cur].off;
12906+			opt[cur].mlen = best_mlen;
12907+			opt[cur].off = best_off;
12908+			best_mlen = mlen;
12909+			best_off = offset;
12910+			if (mlen > cur)
12911+				break;
12912+			cur -= mlen;
12913+		}
12914+
12915+		for (u = 0; u <= last_pos;) {
12916+			u += opt[u].mlen;
12917+		}
12918+
12919+		for (cur = 0; cur < last_pos;) {
12920+			mlen = opt[cur].mlen;
12921+			if (mlen == 1) {
12922+				ip++;
12923+				cur++;
12924+				continue;
12925+			}
12926+			offset = opt[cur].off;
12927+			cur += mlen;
12928+			litLength = (U32)(ip - anchor);
12929+
12930+			if (offset > ZSTD_REP_MOVE_OPT) {
12931+				rep[2] = rep[1];
12932+				rep[1] = rep[0];
12933+				rep[0] = offset - ZSTD_REP_MOVE_OPT;
12934+				offset--;
12935+			} else {
12936+				if (offset != 0) {
12937+					best_off = (offset == ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : (rep[offset]);
12938+					if (offset != 1)
12939+						rep[2] = rep[1];
12940+					rep[1] = rep[0];
12941+					rep[0] = best_off;
12942+				}
12943+				if (litLength == 0)
12944+					offset--;
12945+			}
12946+
12947+			ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen - MINMATCH);
12948+			ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen - MINMATCH);
12949+			anchor = ip = ip + mlen;
12950+		}
12951+	} /* for (cur=0; cur < last_pos; ) */
12952+
12953+	/* Save reps for next block */
12954+	{
12955+		int i;
12956+		for (i = 0; i < ZSTD_REP_NUM; i++)
12957+			ctx->repToConfirm[i] = rep[i];
12958+	}
12959+
12960+	/* Last Literals */
12961+	{
12962+		size_t const lastLLSize = iend - anchor;
12963+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
12964+		seqStorePtr->lit += lastLLSize;
12965+	}
12966+}
12967+
12968+FORCE_INLINE
12969+void ZSTD_compressBlock_opt_extDict_generic(ZSTD_CCtx *ctx, const void *src, size_t srcSize, const int ultra)
12970+{
12971+	seqStore_t *seqStorePtr = &(ctx->seqStore);
12972+	const BYTE *const istart = (const BYTE *)src;
12973+	const BYTE *ip = istart;
12974+	const BYTE *anchor = istart;
12975+	const BYTE *const iend = istart + srcSize;
12976+	const BYTE *const ilimit = iend - 8;
12977+	const BYTE *const base = ctx->base;
12978+	const U32 lowestIndex = ctx->lowLimit;
12979+	const U32 dictLimit = ctx->dictLimit;
12980+	const BYTE *const prefixStart = base + dictLimit;
12981+	const BYTE *const dictBase = ctx->dictBase;
12982+	const BYTE *const dictEnd = dictBase + dictLimit;
12983+
12984+	const U32 maxSearches = 1U << ctx->params.cParams.searchLog;
12985+	const U32 sufficient_len = ctx->params.cParams.targetLength;
12986+	const U32 mls = ctx->params.cParams.searchLength;
12987+	const U32 minMatch = (ctx->params.cParams.searchLength == 3) ? 3 : 4;
12988+
12989+	ZSTD_optimal_t *opt = seqStorePtr->priceTable;
12990+	ZSTD_match_t *matches = seqStorePtr->matchTable;
12991+	const BYTE *inr;
12992+
12993+	/* init */
12994+	U32 offset, rep[ZSTD_REP_NUM];
12995+	{
12996+		U32 i;
12997+		for (i = 0; i < ZSTD_REP_NUM; i++)
12998+			rep[i] = ctx->rep[i];
12999+	}
13000+
13001+	ctx->nextToUpdate3 = ctx->nextToUpdate;
13002+	ZSTD_rescaleFreqs(seqStorePtr, (const BYTE *)src, srcSize);
13003+	ip += (ip == prefixStart);
13004+
13005+	/* Match Loop */
13006+	while (ip < ilimit) {
13007+		U32 cur, match_num, last_pos, litlen, price;
13008+		U32 u, mlen, best_mlen, best_off, litLength;
13009+		U32 curr = (U32)(ip - base);
13010+		memset(opt, 0, sizeof(ZSTD_optimal_t));
13011+		last_pos = 0;
13012+		opt[0].litlen = (U32)(ip - anchor);
13013+
13014+		/* check repCode */
13015+		{
13016+			U32 i, last_i = ZSTD_REP_CHECK + (ip == anchor);
13017+			for (i = (ip == anchor); i < last_i; i++) {
13018+				const S32 repCur = (i == ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : rep[i];
13019+				const U32 repIndex = (U32)(curr - repCur);
13020+				const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
13021+				const BYTE *const repMatch = repBase + repIndex;
13022+				if ((repCur > 0 && repCur <= (S32)curr) &&
13023+				    (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
13024+				    && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch))) {
13025+					/* repcode detected we should take it */
13026+					const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
13027+					mlen = (U32)ZSTD_count_2segments(ip + minMatch, repMatch + minMatch, iend, repEnd, prefixStart) + minMatch;
13028+
13029+					if (mlen > sufficient_len || mlen >= ZSTD_OPT_NUM) {
13030+						best_mlen = mlen;
13031+						best_off = i;
13032+						cur = 0;
13033+						last_pos = 1;
13034+						goto _storeSequence;
13035+					}
13036+
13037+					best_off = i - (ip == anchor);
13038+					litlen = opt[0].litlen;
13039+					do {
13040+						price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
13041+						if (mlen > last_pos || price < opt[mlen].price)
13042+							SET_PRICE(mlen, mlen, i, litlen, price); /* note : macro modifies last_pos */
13043+						mlen--;
13044+					} while (mlen >= minMatch);
13045+				}
13046+			}
13047+		}
13048+
13049+		match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, ip, iend, maxSearches, mls, matches, minMatch); /* first search (depth 0) */
13050+
13051+		if (!last_pos && !match_num) {
13052+			ip++;
13053+			continue;
13054+		}
13055+
13056+		{
13057+			U32 i;
13058+			for (i = 0; i < ZSTD_REP_NUM; i++)
13059+				opt[0].rep[i] = rep[i];
13060+		}
13061+		opt[0].mlen = 1;
13062+
13063+		if (match_num && (matches[match_num - 1].len > sufficient_len || matches[match_num - 1].len >= ZSTD_OPT_NUM)) {
13064+			best_mlen = matches[match_num - 1].len;
13065+			best_off = matches[match_num - 1].off;
13066+			cur = 0;
13067+			last_pos = 1;
13068+			goto _storeSequence;
13069+		}
13070+
13071+		best_mlen = (last_pos) ? last_pos : minMatch;
13072+
13073+		/* set prices using matches at position = 0 */
13074+		for (u = 0; u < match_num; u++) {
13075+			mlen = (u > 0) ? matches[u - 1].len + 1 : best_mlen;
13076+			best_mlen = matches[u].len;
13077+			litlen = opt[0].litlen;
13078+			while (mlen <= best_mlen) {
13079+				price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off - 1, mlen - MINMATCH, ultra);
13080+				if (mlen > last_pos || price < opt[mlen].price)
13081+					SET_PRICE(mlen, mlen, matches[u].off, litlen, price);
13082+				mlen++;
13083+			}
13084+		}
13085+
13086+		if (last_pos < minMatch) {
13087+			ip++;
13088+			continue;
13089+		}
13090+
13091+		/* check further positions */
13092+		for (cur = 1; cur <= last_pos; cur++) {
13093+			inr = ip + cur;
13094+
13095+			if (opt[cur - 1].mlen == 1) {
13096+				litlen = opt[cur - 1].litlen + 1;
13097+				if (cur > litlen) {
13098+					price = opt[cur - litlen].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr - litlen);
13099+				} else
13100+					price = ZSTD_getLiteralPrice(seqStorePtr, litlen, anchor);
13101+			} else {
13102+				litlen = 1;
13103+				price = opt[cur - 1].price + ZSTD_getLiteralPrice(seqStorePtr, litlen, inr - 1);
13104+			}
13105+
13106+			if (cur > last_pos || price <= opt[cur].price)
13107+				SET_PRICE(cur, 1, 0, litlen, price);
13108+
13109+			if (cur == last_pos)
13110+				break;
13111+
13112+			if (inr > ilimit) /* last match must start at a minimum distance of 8 from oend */
13113+				continue;
13114+
13115+			mlen = opt[cur].mlen;
13116+			if (opt[cur].off > ZSTD_REP_MOVE_OPT) {
13117+				opt[cur].rep[2] = opt[cur - mlen].rep[1];
13118+				opt[cur].rep[1] = opt[cur - mlen].rep[0];
13119+				opt[cur].rep[0] = opt[cur].off - ZSTD_REP_MOVE_OPT;
13120+			} else {
13121+				opt[cur].rep[2] = (opt[cur].off > 1) ? opt[cur - mlen].rep[1] : opt[cur - mlen].rep[2];
13122+				opt[cur].rep[1] = (opt[cur].off > 0) ? opt[cur - mlen].rep[0] : opt[cur - mlen].rep[1];
13123+				opt[cur].rep[0] =
13124+				    ((opt[cur].off == ZSTD_REP_MOVE_OPT) && (mlen != 1)) ? (opt[cur - mlen].rep[0] - 1) : (opt[cur - mlen].rep[opt[cur].off]);
13125+			}
13126+
13127+			best_mlen = minMatch;
13128+			{
13129+				U32 i, last_i = ZSTD_REP_CHECK + (mlen != 1);
13130+				for (i = (mlen != 1); i < last_i; i++) {
13131+					const S32 repCur = (i == ZSTD_REP_MOVE_OPT) ? (opt[cur].rep[0] - 1) : opt[cur].rep[i];
13132+					const U32 repIndex = (U32)(curr + cur - repCur);
13133+					const BYTE *const repBase = repIndex < dictLimit ? dictBase : base;
13134+					const BYTE *const repMatch = repBase + repIndex;
13135+					if ((repCur > 0 && repCur <= (S32)(curr + cur)) &&
13136+					    (((U32)((dictLimit - 1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */
13137+					    && (ZSTD_readMINMATCH(inr, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch))) {
13138+						/* repcode detected */
13139+						const BYTE *const repEnd = repIndex < dictLimit ? dictEnd : iend;
13140+						mlen = (U32)ZSTD_count_2segments(inr + minMatch, repMatch + minMatch, iend, repEnd, prefixStart) + minMatch;
13141+
13142+						if (mlen > sufficient_len || cur + mlen >= ZSTD_OPT_NUM) {
13143+							best_mlen = mlen;
13144+							best_off = i;
13145+							last_pos = cur + 1;
13146+							goto _storeSequence;
13147+						}
13148+
13149+						best_off = i - (opt[cur].mlen != 1);
13150+						if (mlen > best_mlen)
13151+							best_mlen = mlen;
13152+
13153+						do {
13154+							if (opt[cur].mlen == 1) {
13155+								litlen = opt[cur].litlen;
13156+								if (cur > litlen) {
13157+									price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, inr - litlen,
13158+															best_off, mlen - MINMATCH, ultra);
13159+								} else
13160+									price = ZSTD_getPrice(seqStorePtr, litlen, anchor, best_off, mlen - MINMATCH, ultra);
13161+							} else {
13162+								litlen = 0;
13163+								price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, best_off, mlen - MINMATCH, ultra);
13164+							}
13165+
13166+							if (cur + mlen > last_pos || price <= opt[cur + mlen].price)
13167+								SET_PRICE(cur + mlen, mlen, i, litlen, price);
13168+							mlen--;
13169+						} while (mlen >= minMatch);
13170+					}
13171+				}
13172+			}
13173+
13174+			match_num = ZSTD_BtGetAllMatches_selectMLS_extDict(ctx, inr, iend, maxSearches, mls, matches, minMatch);
13175+
13176+			if (match_num > 0 && (matches[match_num - 1].len > sufficient_len || cur + matches[match_num - 1].len >= ZSTD_OPT_NUM)) {
13177+				best_mlen = matches[match_num - 1].len;
13178+				best_off = matches[match_num - 1].off;
13179+				last_pos = cur + 1;
13180+				goto _storeSequence;
13181+			}
13182+
13183+			/* set prices using matches at position = cur */
13184+			for (u = 0; u < match_num; u++) {
13185+				mlen = (u > 0) ? matches[u - 1].len + 1 : best_mlen;
13186+				best_mlen = matches[u].len;
13187+
13188+				while (mlen <= best_mlen) {
13189+					if (opt[cur].mlen == 1) {
13190+						litlen = opt[cur].litlen;
13191+						if (cur > litlen)
13192+							price = opt[cur - litlen].price + ZSTD_getPrice(seqStorePtr, litlen, ip + cur - litlen,
13193+													matches[u].off - 1, mlen - MINMATCH, ultra);
13194+						else
13195+							price = ZSTD_getPrice(seqStorePtr, litlen, anchor, matches[u].off - 1, mlen - MINMATCH, ultra);
13196+					} else {
13197+						litlen = 0;
13198+						price = opt[cur].price + ZSTD_getPrice(seqStorePtr, 0, NULL, matches[u].off - 1, mlen - MINMATCH, ultra);
13199+					}
13200+
13201+					if (cur + mlen > last_pos || (price < opt[cur + mlen].price))
13202+						SET_PRICE(cur + mlen, mlen, matches[u].off, litlen, price);
13203+
13204+					mlen++;
13205+				}
13206+			}
13207+		} /* for (cur = 1; cur <= last_pos; cur++) */
13208+
13209+		best_mlen = opt[last_pos].mlen;
13210+		best_off = opt[last_pos].off;
13211+		cur = last_pos - best_mlen;
13212+
13213+	/* store sequence */
13214+_storeSequence: /* cur, last_pos, best_mlen, best_off have to be set */
13215+		opt[0].mlen = 1;
13216+
13217+		while (1) {
13218+			mlen = opt[cur].mlen;
13219+			offset = opt[cur].off;
13220+			opt[cur].mlen = best_mlen;
13221+			opt[cur].off = best_off;
13222+			best_mlen = mlen;
13223+			best_off = offset;
13224+			if (mlen > cur)
13225+				break;
13226+			cur -= mlen;
13227+		}
13228+
13229+		for (u = 0; u <= last_pos;) {
13230+			u += opt[u].mlen;
13231+		}
13232+
13233+		for (cur = 0; cur < last_pos;) {
13234+			mlen = opt[cur].mlen;
13235+			if (mlen == 1) {
13236+				ip++;
13237+				cur++;
13238+				continue;
13239+			}
13240+			offset = opt[cur].off;
13241+			cur += mlen;
13242+			litLength = (U32)(ip - anchor);
13243+
13244+			if (offset > ZSTD_REP_MOVE_OPT) {
13245+				rep[2] = rep[1];
13246+				rep[1] = rep[0];
13247+				rep[0] = offset - ZSTD_REP_MOVE_OPT;
13248+				offset--;
13249+			} else {
13250+				if (offset != 0) {
13251+					best_off = (offset == ZSTD_REP_MOVE_OPT) ? (rep[0] - 1) : (rep[offset]);
13252+					if (offset != 1)
13253+						rep[2] = rep[1];
13254+					rep[1] = rep[0];
13255+					rep[0] = best_off;
13256+				}
13257+
13258+				if (litLength == 0)
13259+					offset--;
13260+			}
13261+
13262+			ZSTD_updatePrice(seqStorePtr, litLength, anchor, offset, mlen - MINMATCH);
13263+			ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, mlen - MINMATCH);
13264+			anchor = ip = ip + mlen;
13265+		}
13266+	} /* for (cur=0; cur < last_pos; ) */
13267+
13268+	/* Save reps for next block */
13269+	{
13270+		int i;
13271+		for (i = 0; i < ZSTD_REP_NUM; i++)
13272+			ctx->repToConfirm[i] = rep[i];
13273+	}
13274+
13275+	/* Last Literals */
13276+	{
13277+		size_t lastLLSize = iend - anchor;
13278+		memcpy(seqStorePtr->lit, anchor, lastLLSize);
13279+		seqStorePtr->lit += lastLLSize;
13280+	}
13281+}
13282+
13283+#endif /* ZSTD_OPT_H_91842398743 */
13284--
132852.9.5
13286