Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Important notes about in-place decompression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * At least on x86, the kernel is decompressed in place: the compressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * is placed to the end of the output buffer, and the decompressor overwrites
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * most of the compressed data. There must be enough safety margin to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * guarantee that the write position is always behind the read position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * The safety margin for ZSTD with a 128 KB block size is calculated below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Note that the margin with ZSTD is bigger than with GZIP or XZ!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * The worst case for in-place decompression is that the beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * the file is compressed extremely well, and the rest of the file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * uncompressible. Thus, we must look for worst-case expansion when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * compressor is encoding uncompressible data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * The structure of the .zst file in case of a compresed kernel is as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Maximum sizes (as bytes) of the fields are in parenthesis.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *    Frame Header: (18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *    Blocks: (N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *    Checksum: (4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * The frame header and checksum overhead is at most 22 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * ZSTD stores the data in blocks. Each block has a header whose size is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * a 3 bytes. After the block header, there is up to 128 KB of payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * The maximum uncompressed size of the payload is 128 KB. The minimum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * uncompressed size of the payload is never less than the payload size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * (excluding the block header).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * The assumption, that the uncompressed size of the payload is never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * smaller than the payload itself, is valid only when talking about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * the payload as a whole. It is possible that the payload has parts where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * the decompressor consumes more input than it produces output. Calculating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * the worst case for this would be tricky. Instead of trying to do that,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * let's simply make sure that the decompressor never overwrites any bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * of the payload which it is currently reading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * Now we have enough information to calculate the safety margin. We need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *   - 22 bytes for the .zst file format headers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *   - 3 bytes per every 128 KiB of uncompressed size (one block header per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *     block); and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *   - 128 KiB (biggest possible zstd block size) to make sure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *     decompressor never overwrites anything from the block it is currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *     reading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * We get the following formula:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *    safety_margin = 22 + uncompressed_size * 3 / 131072 + 131072
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *                 <= 22 + (uncompressed_size >> 15) + 131072
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * Preboot environments #include "path/to/decompress_unzstd.c".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * All of the source files we depend on must be #included.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * zstd's only source dependeny is xxhash, which has no source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * dependencies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * When UNZSTD_PREBOOT is defined we declare __decompress(), which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * used for kernel decompression, instead of unzstd().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * Define __DISABLE_EXPORTS in preboot environments to prevent symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * from xxhash and zstd from being exported by the EXPORT_SYMBOL macro.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #ifdef STATIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) # define UNZSTD_PREBOOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) # include "xxhash.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) # include "zstd/entropy_common.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) # include "zstd/fse_decompress.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) # include "zstd/huf_decompress.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) # include "zstd/zstd_common.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) # include "zstd/decompress.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #include <linux/decompress/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #include <linux/zstd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /* 128MB is the maximum window size supported by zstd. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define ZSTD_WINDOWSIZE_MAX	(1 << ZSTD_WINDOWLOG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * Size of the input and output buffers in multi-call mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * Pick a larger size because it isn't used during kernel decompression,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * since that is single pass, and we have to allocate a large buffer for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * zstd's window anyway. The larger size speeds up initramfs decompression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define ZSTD_IOBUF_SIZE		(1 << 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int INIT handle_zstd_error(size_t ret, void (*error)(char *x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	const int err = ZSTD_getErrorCode(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (!ZSTD_isError(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	case ZSTD_error_memory_allocation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		error("ZSTD decompressor ran out of memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	case ZSTD_error_prefix_unknown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		error("Input is not in the ZSTD format (wrong magic bytes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	case ZSTD_error_dstSize_tooSmall:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	case ZSTD_error_corruption_detected:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	case ZSTD_error_checksum_wrong:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		error("ZSTD-compressed data is corrupt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		error("ZSTD-compressed data is probably corrupt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * Handle the case where we have the entire input and output in one segment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * We can allocate less memory (no circular buffer for the sliding window),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * and avoid some memcpy() calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int INIT decompress_single(const u8 *in_buf, long in_len, u8 *out_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 				  long out_len, long *in_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				  void (*error)(char *x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	const size_t wksp_size = ZSTD_DCtxWorkspaceBound();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	void *wksp = large_malloc(wksp_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	ZSTD_DCtx *dctx = ZSTD_initDCtx(wksp, wksp_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	size_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (dctx == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		error("Out of memory while allocating ZSTD_DCtx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 * Find out how large the frame actually is, there may be junk at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	 * the end of the frame that ZSTD_decompressDCtx() can't handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	ret = ZSTD_findFrameCompressedSize(in_buf, in_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	err = handle_zstd_error(ret, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	in_len = (long)ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	ret = ZSTD_decompressDCtx(dctx, out_buf, out_len, in_buf, in_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	err = handle_zstd_error(ret, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (in_pos != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		*in_pos = in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (wksp != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		large_free(wksp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int INIT __unzstd(unsigned char *in_buf, long in_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			 long (*fill)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			 long (*flush)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			 unsigned char *out_buf, long out_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			 long *in_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			 void (*error)(char *x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	ZSTD_inBuffer in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	ZSTD_outBuffer out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	ZSTD_frameParams params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	void *in_allocated = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	void *out_allocated = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	void *wksp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	size_t wksp_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	ZSTD_DStream *dstream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	size_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 * ZSTD decompression code won't be happy if the buffer size is so big
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 * that its end address overflows. When the size is not provided, make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	 * it as big as possible without having the end address overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (out_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		out_len = UINTPTR_MAX - (uintptr_t)out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (fill == NULL && flush == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		 * We can decompress faster and with less memory when we have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		 * single chunk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return decompress_single(in_buf, in_len, out_buf, out_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 					 in_pos, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 * If in_buf is not provided, we must be using fill(), so allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 * a large enough buffer. If it is provided, it must be at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	 * ZSTD_IOBUF_SIZE large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (in_buf == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		in_allocated = large_malloc(ZSTD_IOBUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		if (in_allocated == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			error("Out of memory while allocating input buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		in_buf = in_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		in_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	/* Read the first chunk, since we need to decode the frame header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (fill != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		in_len = fill(in_buf, ZSTD_IOBUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (in_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		error("ZSTD-compressed data is truncated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/* Set the first non-empty input buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	in.src = in_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	in.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	in.size = in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* Allocate the output buffer if we are using flush(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (flush != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		out_allocated = large_malloc(ZSTD_IOBUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (out_allocated == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			error("Out of memory while allocating output buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		out_buf = out_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		out_len = ZSTD_IOBUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/* Set the output buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	out.dst = out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	out.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	out.size = out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	 * We need to know the window size to allocate the ZSTD_DStream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	 * Since we are streaming, we need to allocate a buffer for the sliding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 * window. The window size varies from 1 KB to ZSTD_WINDOWSIZE_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 * (8 MB), so it is important to use the actual value so as not to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	 * waste memory when it is smaller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	ret = ZSTD_getFrameParams(&params, in.src, in.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	err = handle_zstd_error(ret, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		error("ZSTD-compressed data has an incomplete frame header");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (params.windowSize > ZSTD_WINDOWSIZE_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		error("ZSTD-compressed data has too large a window size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 * Allocate the ZSTD_DStream now that we know how much memory is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	wksp_size = ZSTD_DStreamWorkspaceBound(params.windowSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	wksp = large_malloc(wksp_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	dstream = ZSTD_initDStream(params.windowSize, wksp, wksp_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (dstream == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		error("Out of memory while allocating ZSTD_DStream");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	 * Decompression loop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	 * Read more data if necessary (error if no more data can be read).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	 * Call the decompression function, which returns 0 when finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	 * Flush any data produced if using flush().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (in_pos != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		*in_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		 * If we need to reload data, either we have fill() and can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		 * try to get more data, or we don't and the input is truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (in.pos == in.size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			if (in_pos != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 				*in_pos += in.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			in_len = fill ? fill(in_buf, ZSTD_IOBUF_SIZE) : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			if (in_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				error("ZSTD-compressed data is truncated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 				err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			in.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			in.size = in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		/* Returns zero when the frame is complete. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		ret = ZSTD_decompressStream(dstream, &out, &in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		err = handle_zstd_error(ret, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		/* Flush all of the data produced if using flush(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		if (flush != NULL && out.pos > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			if (out.pos != flush(out.dst, out.pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				error("Failed to flush()");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 				err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			out.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	} while (ret != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (in_pos != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		*in_pos += in.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (in_allocated != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		large_free(in_allocated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (out_allocated != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		large_free(out_allocated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (wksp != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		large_free(wksp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) #ifndef UNZSTD_PREBOOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) STATIC int INIT unzstd(unsigned char *buf, long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		       long (*fill)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		       long (*flush)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		       unsigned char *out_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		       long *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		       void (*error)(char *x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return __unzstd(buf, len, fill, flush, out_buf, 0, pos, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) STATIC int INIT __decompress(unsigned char *buf, long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			     long (*fill)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			     long (*flush)(void*, unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			     unsigned char *out_buf, long out_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			     long *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			     void (*error)(char *x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	return __unzstd(buf, len, fill, flush, out_buf, out_len, pos, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) #endif