mirror of
https://github.com/ventoy/Ventoy.git
synced 2026-12-05 11:56:51 +01:00
Update zstdcat to 1.6.0
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -62,6 +62,9 @@ cp -a $VENTOY_PATH/VBLADE/vblade-master/vblade_64 tool/
|
|||||||
cp -a $VENTOY_PATH/LZIP/lunzip32 tool/
|
cp -a $VENTOY_PATH/LZIP/lunzip32 tool/
|
||||||
cp -a $VENTOY_PATH/LZIP/lunzip64 tool/
|
cp -a $VENTOY_PATH/LZIP/lunzip64 tool/
|
||||||
|
|
||||||
|
cp -a $VENTOY_PATH/ZSTD/zstdcat tool/
|
||||||
|
cp -a $VENTOY_PATH/ZSTD/zstdcat64 tool/
|
||||||
|
|
||||||
cp -a $VENTOY_PATH/cryptsetup/veritysetup32 tool/
|
cp -a $VENTOY_PATH/cryptsetup/veritysetup32 tool/
|
||||||
cp -a $VENTOY_PATH/cryptsetup/veritysetup64 tool/
|
cp -a $VENTOY_PATH/cryptsetup/veritysetup64 tool/
|
||||||
|
|
||||||
@@ -96,6 +99,8 @@ cp -a $VENTOY_PATH/VBLADE/vblade-master/vblade_aa64 tool/
|
|||||||
|
|
||||||
cp -a $VENTOY_PATH/LZIP/lunzipaa64 tool/
|
cp -a $VENTOY_PATH/LZIP/lunzipaa64 tool/
|
||||||
|
|
||||||
|
cp -a $VENTOY_PATH/ZSTD/zstdcat64 tool/
|
||||||
|
|
||||||
chmod -R 777 ./tool
|
chmod -R 777 ./tool
|
||||||
|
|
||||||
find ./tool | cpio -o -H newc --owner=root:root >tool.cpio
|
find ./tool | cpio -o -H newc --owner=root:root >tool.cpio
|
||||||
|
|||||||
@@ -60,6 +60,15 @@ else
|
|||||||
OPT='-a'
|
OPT='-a'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# in case we forgot to build grub
|
||||||
|
for i in i386-pc i386-efi arm64-efi mips64el-efi x86_64-efi; do
|
||||||
|
if [ ! -f ./grub/$i/fs.lst ]; then
|
||||||
|
echo "./grub/$i/fs.lst not exist, please build grub first."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
dos2unix -q ./tool/ventoy_lib.sh
|
dos2unix -q ./tool/ventoy_lib.sh
|
||||||
dos2unix -q ./tool/VentoyWorker.sh
|
dos2unix -q ./tool/VentoyWorker.sh
|
||||||
dos2unix -q ./tool/VentoyGTK.glade
|
dos2unix -q ./tool/VentoyGTK.glade
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ -f ./zstd-1.6.0.tar.gz ]; then
|
||||||
|
echo "use local src"
|
||||||
|
else
|
||||||
|
echo "download zstd src .."
|
||||||
|
curl https://github.com/meta/zstd/releases/download/v1.6.0/zstd-1.6.0.tar.gz -o zstd-1.6.0.tar.gz
|
||||||
|
fi
|
||||||
|
|
||||||
|
CURDIR=$PWD
|
||||||
|
rm -f ./zstdcat64 ./zstdcataa64
|
||||||
|
|
||||||
|
[ -d zstd-1.6.0 ] && rm -rf ./zstd-1.6.0
|
||||||
|
tar xf zstd-1.6.0.tar.gz
|
||||||
|
cd zstd-1.6.0/build/single_file_libs
|
||||||
|
python3 combine.py -r ../../lib -x legacy/zstd_legacy.h -o zstddeclib.c zstddeclib-in.c
|
||||||
|
cp $CURDIR/mini_zstdcat.c examples/
|
||||||
|
cd examples/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
echo "build x64 ..."
|
||||||
|
gcc -specs "/usr/local/musl/lib/musl-gcc.specs" -std=c99 -static -Os -D_FILE_OFFSET_BITS=64 mini_zstdcat.c -o $CURDIR/zstdcat64
|
||||||
|
strip --strip-all $CURDIR/zstdcat64
|
||||||
|
|
||||||
|
|
||||||
|
echo "build arm64 ..."
|
||||||
|
/opt/aarch64--uclibc--stable-2020.08-1/bin/aarch64-buildroot-linux-uclibc-gcc -std=c99 -static -Os -D_FILE_OFFSET_BITS=64 mini_zstdcat.c -o $CURDIR/zstdcataa64
|
||||||
|
/opt/aarch64--uclibc--stable-2020.08-1/bin/aarch64-buildroot-linux-uclibc-strip --strip-all $CURDIR/zstdcataa64
|
||||||
|
|
||||||
|
|
||||||
|
cd $CURDIR
|
||||||
|
|
||||||
|
if [ -f ./zstdcat64 -a -f ./zstdcataa64 ]; then
|
||||||
|
echo "============= success =========="
|
||||||
|
else
|
||||||
|
echo "============= failed =========="
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ -d zstd-1.6.0 ] && rm -rf ./zstd-1.6.0
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
Build a static linked, small zstdcat tool
|
|
||||||
|
|
||||||
======== Source Code ========
|
|
||||||
use an old version of zstd
|
|
||||||
https://codeload.github.com/facebook/zstd/zip/v1.0.0
|
|
||||||
|
|
||||||
======== Build Environment ========
|
|
||||||
build for 32bit, static linked with dietlibc
|
|
||||||
1. install centos 6.10 i386 with CentOS-6.10-i386-bin-DVD1.iso
|
|
||||||
2. yum install gcc gettext gettext-devel
|
|
||||||
3. install dietc libc (just make && make install)
|
|
||||||
4. export PATH=$PATH:/opt/diet/bin
|
|
||||||
|
|
||||||
======== Build i386 Step ========
|
|
||||||
1. extract zstd source code
|
|
||||||
2. cd programs
|
|
||||||
3. diet -Os gcc -pipe -nostdinc -falign-loops=32 -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -O3 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -DZSTD_LEGACY_SUPPORT=1 ../lib/decompress/zstd_decompress.c -c -o ../lib/decompress/zstd_decompress.o
|
|
||||||
diet -Os gcc -pipe -nostdinc -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -O3 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -DZSTD_LEGACY_SUPPORT=1 ../lib/decompress/zstd_decompress.o ../lib/decompress/huf_decompress.c ../lib/common/entropy_common.c ../lib/common/fse_decompress.c ../lib/common/xxhash.c ../lib/common/zstd_common.c ../lib/compress/zstd_compress.c ../lib/compress/fse_compress.c ../lib/compress/huf_compress.c ../lib/legacy/zstd_v01.c ../lib/legacy/zstd_v02.c ../lib/legacy/zstd_v03.c ../lib/legacy/zstd_v04.c ../lib/legacy/zstd_v05.c ../lib/legacy/zstd_v06.c ../lib/legacy/zstd_v07.c ../lib/dictBuilder/divsufsort.c ../lib/dictBuilder/zdict.c zstdcli.c fileio.c bench.c datagen.c dibio.c -o zstd
|
|
||||||
4. strip --strip-all zstd
|
|
||||||
5. rename zstd to zstdcat
|
|
||||||
|
|
||||||
|
|
||||||
======== Build x86_64 Step ========
|
|
||||||
1. extract zstd source code
|
|
||||||
2. cd programs
|
|
||||||
3. /opt/diet64/bin/diet -Os gcc -pipe -nostdinc -falign-loops=32 -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -O3 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -DZSTD_LEGACY_SUPPORT=1 ../lib/decompress/zstd_decompress.c -c -o ../lib/decompress/zstd_decompress.o
|
|
||||||
/opt/diet64/bin/diet -Os gcc -pipe -nostdinc -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -O3 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -DZSTD_LEGACY_SUPPORT=1 ../lib/decompress/zstd_decompress.o ../lib/decompress/huf_decompress.c ../lib/common/entropy_common.c ../lib/common/fse_decompress.c ../lib/common/xxhash.c ../lib/common/zstd_common.c ../lib/compress/zstd_compress.c ../lib/compress/fse_compress.c ../lib/compress/huf_compress.c ../lib/legacy/zstd_v01.c ../lib/legacy/zstd_v02.c ../lib/legacy/zstd_v03.c ../lib/legacy/zstd_v04.c ../lib/legacy/zstd_v05.c ../lib/legacy/zstd_v06.c ../lib/legacy/zstd_v07.c ../lib/dictBuilder/divsufsort.c ../lib/dictBuilder/zdict.c zstdcli.c fileio.c bench.c datagen.c dibio.c -o zstd
|
|
||||||
4. strip --strip-all zstd
|
|
||||||
5. rename zstd to zstdcat
|
|
||||||
|
|
||||||
|
|
||||||
======== Build ARM64 Step ========
|
|
||||||
1. extract zstd source code
|
|
||||||
2. cd programs
|
|
||||||
3. aarch64-linux-gcc -static -falign-loops=32 -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -O3 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -DZSTD_LEGACY_SUPPORT=1 ../lib/decompress/zstd_decompress.c -c -o ../lib/decompress/zstd_decompress.o
|
|
||||||
aarch64-linux-gcc -static -I../lib -I../lib/common -I../lib/dictBuilder -I../lib/legacy -O3 -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -DZSTD_LEGACY_SUPPORT=1 ../lib/decompress/zstd_decompress.o ../lib/decompress/huf_decompress.c ../lib/common/entropy_common.c ../lib/common/fse_decompress.c ../lib/common/xxhash.c ../lib/common/zstd_common.c ../lib/compress/zstd_compress.c ../lib/compress/fse_compress.c ../lib/compress/huf_compress.c ../lib/legacy/zstd_v01.c ../lib/legacy/zstd_v02.c ../lib/legacy/zstd_v03.c ../lib/legacy/zstd_v04.c ../lib/legacy/zstd_v05.c ../lib/legacy/zstd_v06.c ../lib/legacy/zstd_v07.c ../lib/dictBuilder/divsufsort.c ../lib/dictBuilder/zdict.c zstdcli.c fileio.c bench.c datagen.c dibio.c -o zstd
|
|
||||||
4. aarch64-linux-strip --strip-all zstd
|
|
||||||
5. rename zstd to zstdcat
|
|
||||||
|
|
||||||
@@ -0,0 +1,381 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "../zstddeclib.c"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Write all bytes to the file stream reliably.
|
||||||
|
* Handles short writes caused by full pipe buffers or signal interrupts.
|
||||||
|
* Returns 0 on success, -1 on permanent write error.
|
||||||
|
*/
|
||||||
|
static int fwrite_all(FILE *fp, const void *buf, size_t len)
|
||||||
|
{
|
||||||
|
const unsigned char *ptr = (const unsigned char *)buf;
|
||||||
|
size_t remaining = len;
|
||||||
|
|
||||||
|
while (remaining > 0)
|
||||||
|
{
|
||||||
|
size_t written = fwrite(ptr, 1, remaining, fp);
|
||||||
|
if (written == 0)
|
||||||
|
{
|
||||||
|
if (ferror(fp))
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* Zero write with no error flag indicates unexpected output termination */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ptr += written;
|
||||||
|
remaining -= written;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char *in_filename = NULL;
|
||||||
|
const char *out_filename = NULL;
|
||||||
|
int exit_code = 0;
|
||||||
|
FILE *in_fp = NULL;
|
||||||
|
int use_stdin = 0;
|
||||||
|
FILE *out_fp = NULL;
|
||||||
|
int use_stdout = 1;
|
||||||
|
ZSTD_DStream *dstream = NULL;
|
||||||
|
void *in_buf = NULL;
|
||||||
|
void *out_buf = NULL;
|
||||||
|
size_t in_buf_size, out_buf_size;
|
||||||
|
ZSTD_inBuffer in = {0};
|
||||||
|
ZSTD_outBuffer out = {0};
|
||||||
|
int is_raw = 0;
|
||||||
|
int expect_frame_start = 1;
|
||||||
|
size_t skip_remaining = 0;
|
||||||
|
size_t ret;
|
||||||
|
|
||||||
|
/* Parse command line arguments */
|
||||||
|
for (int i = 1; i < argc; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(argv[i], "-o") == 0)
|
||||||
|
{
|
||||||
|
if (i + 1 < argc)
|
||||||
|
{
|
||||||
|
out_filename = argv[++i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: -o requires a filename argument\n");
|
||||||
|
exit_code = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (in_filename == NULL)
|
||||||
|
{
|
||||||
|
in_filename = argv[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Unknown argument '%s'\n", argv[i]);
|
||||||
|
exit_code = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open input stream */
|
||||||
|
if (in_filename == NULL || strcmp(in_filename, "-") == 0)
|
||||||
|
{
|
||||||
|
in_fp = stdin;
|
||||||
|
use_stdin = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
in_fp = fopen(in_filename, "rb");
|
||||||
|
if (!in_fp)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Cannot open input file '%s': %s\n",
|
||||||
|
in_filename, strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open output stream */
|
||||||
|
if (out_filename != NULL && strcmp(out_filename, "-") != 0)
|
||||||
|
{
|
||||||
|
out_fp = fopen(out_filename, "wb");
|
||||||
|
if (!out_fp)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Cannot open output file '%s': %s\n",
|
||||||
|
out_filename, strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
use_stdout = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out_fp = stdout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create and initialize decompression stream */
|
||||||
|
dstream = ZSTD_createDStream();
|
||||||
|
if (!dstream)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Failed to create ZSTD decompression stream\n");
|
||||||
|
exit_code = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ZSTD_initDStream(dstream);
|
||||||
|
if (ZSTD_isError(ret))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Failed to init ZSTD stream: %s\n",
|
||||||
|
ZSTD_getErrorName(ret));
|
||||||
|
exit_code = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate buffers using ZSTD recommended optimal sizes */
|
||||||
|
in_buf_size = ZSTD_DStreamInSize();
|
||||||
|
out_buf_size = ZSTD_DStreamOutSize();
|
||||||
|
in_buf = malloc(in_buf_size);
|
||||||
|
out_buf = malloc(out_buf_size);
|
||||||
|
if (!in_buf || !out_buf)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Memory allocation failed: %s\n", strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
in.src = in_buf;
|
||||||
|
in.size = 0;
|
||||||
|
in.pos = 0;
|
||||||
|
out.dst = out_buf;
|
||||||
|
out.size = out_buf_size;
|
||||||
|
out.pos = 0;
|
||||||
|
|
||||||
|
/* Main processing loop */
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
/* Read new data when input buffer is fully consumed */
|
||||||
|
if (in.pos == in.size)
|
||||||
|
{
|
||||||
|
if (feof(in_fp))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
in.size = fread(in_buf, 1, in_buf_size, in_fp);
|
||||||
|
in.pos = 0;
|
||||||
|
|
||||||
|
if (in.size == 0)
|
||||||
|
{
|
||||||
|
if (ferror(in_fp))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Read failed: %s\n", strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Raw passthrough mode: write non-ZSTD data directly to output */
|
||||||
|
if (is_raw)
|
||||||
|
{
|
||||||
|
size_t avail = in.size - in.pos;
|
||||||
|
if (fwrite_all(out_fp, (const char *)in_buf + in.pos, avail) != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Write failed: %s\n", strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
in.pos = in.size;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Stream-skip remaining bytes of a skippable frame */
|
||||||
|
if (skip_remaining > 0)
|
||||||
|
{
|
||||||
|
size_t avail = in.size - in.pos;
|
||||||
|
size_t skip_len = (skip_remaining < avail) ? skip_remaining : avail;
|
||||||
|
in.pos += skip_len;
|
||||||
|
skip_remaining -= skip_len;
|
||||||
|
|
||||||
|
if (skip_remaining == 0)
|
||||||
|
{
|
||||||
|
expect_frame_start = 1;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Detect frame start magic number */
|
||||||
|
if (expect_frame_start)
|
||||||
|
{
|
||||||
|
const unsigned char *header = (const unsigned char *)in_buf + in.pos;
|
||||||
|
|
||||||
|
/* Need at least 4 bytes to identify the magic number */
|
||||||
|
if (in.size - in.pos < 4)
|
||||||
|
{
|
||||||
|
if (feof(in_fp))
|
||||||
|
{
|
||||||
|
/* Less than 4 bytes left at EOF, passthrough as trailing raw data */
|
||||||
|
size_t avail = in.size - in.pos;
|
||||||
|
if (avail > 0)
|
||||||
|
{
|
||||||
|
if (fwrite_all(out_fp, header, avail) != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Write failed: %s\n", strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Shift remaining bytes to buffer head and refill */
|
||||||
|
size_t remaining = in.size - in.pos;
|
||||||
|
memmove(in_buf, header, remaining);
|
||||||
|
in.size = remaining;
|
||||||
|
in.size += fread((char *)in_buf + remaining, 1,
|
||||||
|
in_buf_size - remaining, in_fp);
|
||||||
|
in.pos = 0;
|
||||||
|
|
||||||
|
if (ferror(in_fp))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Read failed: %s\n", strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Normal ZSTD frame: magic bytes 0x28 0xB5 0x2F 0xFD */
|
||||||
|
if (memcmp(header, "\x28\xB5\x2F\xFD", 4) == 0)
|
||||||
|
{
|
||||||
|
expect_frame_start = 0;
|
||||||
|
}
|
||||||
|
/* Skippable frame: first 3 bytes 0x50 0x2A 0x4D, 4th byte high nibble = 0x5 */
|
||||||
|
else if (memcmp(header, "\x50\x2A\x4D", 3) == 0
|
||||||
|
&& (header[3] & 0xF0) == 0x50)
|
||||||
|
{
|
||||||
|
/* Need 8 bytes for full frame header (4B magic + 4B length) */
|
||||||
|
if (in.size - in.pos < 8)
|
||||||
|
{
|
||||||
|
if (feof(in_fp))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Truncated skippable frame header\n");
|
||||||
|
exit_code = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
size_t remaining = in.size - in.pos;
|
||||||
|
memmove(in_buf, header, remaining);
|
||||||
|
in.size = remaining;
|
||||||
|
in.size += fread((char *)in_buf + remaining, 1,
|
||||||
|
in_buf_size - remaining, in_fp);
|
||||||
|
in.pos = 0;
|
||||||
|
|
||||||
|
if (ferror(in_fp))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Read failed: %s\n", strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse 4-byte little-endian payload length */
|
||||||
|
uint32_t payload_size = 0;
|
||||||
|
const unsigned char *len_p = header + 4;
|
||||||
|
payload_size = (uint32_t)len_p[0];
|
||||||
|
payload_size |= (uint32_t)len_p[1] << 8;
|
||||||
|
payload_size |= (uint32_t)len_p[2] << 16;
|
||||||
|
payload_size |= (uint32_t)len_p[3] << 24;
|
||||||
|
|
||||||
|
size_t total_skip = 8 + payload_size;
|
||||||
|
size_t avail = in.size - in.pos;
|
||||||
|
size_t skip_len = (total_skip < avail) ? total_skip : avail;
|
||||||
|
in.pos += skip_len;
|
||||||
|
skip_remaining = total_skip - skip_len;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
/* Not a valid ZSTD frame, switch to raw passthrough mode */
|
||||||
|
else
|
||||||
|
{
|
||||||
|
is_raw = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Streaming decompression */
|
||||||
|
out.pos = 0;
|
||||||
|
ret = ZSTD_decompressStream(dstream, &out, &in);
|
||||||
|
|
||||||
|
if (ZSTD_isError(ret))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Decompression failed: %s\n",
|
||||||
|
ZSTD_getErrorName(ret));
|
||||||
|
exit_code = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write decompressed data to output */
|
||||||
|
if (out.pos > 0)
|
||||||
|
{
|
||||||
|
if (fwrite_all(out_fp, out_buf, out.pos) != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Write failed: %s\n", strerror(errno));
|
||||||
|
exit_code = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return value 0 indicates current frame is fully decompressed */
|
||||||
|
if (ret == 0)
|
||||||
|
{
|
||||||
|
expect_frame_start = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Validate frame integrity after loop exit */
|
||||||
|
if (exit_code == 0 && !is_raw)
|
||||||
|
{
|
||||||
|
if (skip_remaining > 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Truncated skippable frame (unexpected EOF)\n");
|
||||||
|
exit_code = 1;
|
||||||
|
}
|
||||||
|
else if (!expect_frame_start)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: Truncated zstd frame (unexpected EOF)\n");
|
||||||
|
exit_code = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
/* Flush stdout buffer to preserve already-decompressed data on abnormal exit */
|
||||||
|
if (use_stdout && out_fp)
|
||||||
|
{
|
||||||
|
fflush(out_fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Release resources in reverse order */
|
||||||
|
free(in_buf);
|
||||||
|
free(out_buf);
|
||||||
|
if (dstream)
|
||||||
|
{
|
||||||
|
ZSTD_freeDStream(dstream);
|
||||||
|
}
|
||||||
|
if (!use_stdout && out_fp)
|
||||||
|
{
|
||||||
|
fclose(out_fp);
|
||||||
|
}
|
||||||
|
if (!use_stdin && in_fp)
|
||||||
|
{
|
||||||
|
fclose(in_fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return exit_code;
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user