|
Bruno Wolff III |
258bc8 |
From 6113361316d5ce5bfdc118d188e5617a1fcd747c Mon Sep 17 00:00:00 2001
|
|
Bruno Wolff III |
258bc8 |
From: Sean Purcell <me@seanp.xyz>
|
|
Bruno Wolff III |
258bc8 |
Date: Mon, 14 Aug 2017 22:46:04 -0700
|
|
Bruno Wolff III |
258bc8 |
Subject: [PATCH 1/4] squashfs-tools: Add zstd support
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
This patch adds zstd support to squashfs-tools. It works with zstd
|
|
Bruno Wolff III |
258bc8 |
versions >= 1.0.0. It was originally written by Sean Purcell.
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
Signed-off-by: Sean Purcell <me@seanp.xyz>
|
|
Bruno Wolff III |
258bc8 |
Signed-off-by: Nick Terrell <terrelln@fb.com>
|
|
Bruno Wolff III |
258bc8 |
---
|
|
Bruno Wolff III |
258bc8 |
squashfs-tools/Makefile | 20 +++
|
|
Bruno Wolff III |
258bc8 |
squashfs-tools/compressor.c | 8 ++
|
|
Bruno Wolff III |
258bc8 |
squashfs-tools/squashfs_fs.h | 1 +
|
|
Bruno Wolff III |
258bc8 |
squashfs-tools/zstd_wrapper.c | 254 ++++++++++++++++++++++++++++++++++
|
|
Bruno Wolff III |
258bc8 |
squashfs-tools/zstd_wrapper.h | 48 +++++++
|
|
Bruno Wolff III |
258bc8 |
5 files changed, 331 insertions(+)
|
|
Bruno Wolff III |
258bc8 |
create mode 100644 squashfs-tools/zstd_wrapper.c
|
|
Bruno Wolff III |
258bc8 |
create mode 100644 squashfs-tools/zstd_wrapper.h
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
diff --git a/squashfs-tools/Makefile b/squashfs-tools/Makefile
|
|
Bruno Wolff III |
258bc8 |
index 52d2582..22fc559 100644
|
|
Bruno Wolff III |
258bc8 |
--- a/squashfs-tools/Makefile
|
|
Bruno Wolff III |
258bc8 |
+++ b/squashfs-tools/Makefile
|
|
Bruno Wolff III |
258bc8 |
@@ -75,6 +75,18 @@ GZIP_SUPPORT = 1
|
|
Bruno Wolff III |
258bc8 |
#LZMA_SUPPORT = 1
|
|
Bruno Wolff III |
258bc8 |
#LZMA_DIR = ../../../../LZMA/lzma465
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+########### Building ZSTD support ############
|
|
Bruno Wolff III |
258bc8 |
+#
|
|
Bruno Wolff III |
258bc8 |
+# The ZSTD library is supported
|
|
Bruno Wolff III |
258bc8 |
+# ZSTD homepage: http://zstd.net
|
|
Bruno Wolff III |
258bc8 |
+# ZSTD source repository: https://github.com/facebook/zstd
|
|
Bruno Wolff III |
258bc8 |
+#
|
|
Bruno Wolff III |
258bc8 |
+# To build using the ZSTD library - install the library and uncomment the
|
|
Bruno Wolff III |
258bc8 |
+# ZSTD_SUPPORT line below.
|
|
Bruno Wolff III |
258bc8 |
+#
|
|
Bruno Wolff III |
258bc8 |
+#ZSTD_SUPPORT = 1
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
######## Specifying default compression ########
|
|
Bruno Wolff III |
258bc8 |
#
|
|
Bruno Wolff III |
258bc8 |
# The next line specifies which compression algorithm is used by default
|
|
Bruno Wolff III |
258bc8 |
@@ -177,6 +189,14 @@ LIBS += -llz4
|
|
Bruno Wolff III |
258bc8 |
COMPRESSORS += lz4
|
|
Bruno Wolff III |
258bc8 |
endif
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
+ifeq ($(ZSTD_SUPPORT),1)
|
|
Bruno Wolff III |
258bc8 |
+CFLAGS += -DZSTD_SUPPORT
|
|
Bruno Wolff III |
258bc8 |
+MKSQUASHFS_OBJS += zstd_wrapper.o
|
|
Bruno Wolff III |
258bc8 |
+UNSQUASHFS_OBJS += zstd_wrapper.o
|
|
Bruno Wolff III |
258bc8 |
+LIBS += -lzstd
|
|
Bruno Wolff III |
258bc8 |
+COMPRESSORS += zstd
|
|
Bruno Wolff III |
258bc8 |
+endif
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
ifeq ($(XATTR_SUPPORT),1)
|
|
Bruno Wolff III |
258bc8 |
ifeq ($(XATTR_DEFAULT),1)
|
|
Bruno Wolff III |
258bc8 |
CFLAGS += -DXATTR_SUPPORT -DXATTR_DEFAULT
|
|
Bruno Wolff III |
258bc8 |
diff --git a/squashfs-tools/compressor.c b/squashfs-tools/compressor.c
|
|
Bruno Wolff III |
258bc8 |
index 525e316..02b5e90 100644
|
|
Bruno Wolff III |
258bc8 |
--- a/squashfs-tools/compressor.c
|
|
Bruno Wolff III |
258bc8 |
+++ b/squashfs-tools/compressor.c
|
|
Bruno Wolff III |
258bc8 |
@@ -65,6 +65,13 @@ static struct compressor xz_comp_ops = {
|
|
Bruno Wolff III |
258bc8 |
extern struct compressor xz_comp_ops;
|
|
Bruno Wolff III |
258bc8 |
#endif
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
+#ifndef ZSTD_SUPPORT
|
|
Bruno Wolff III |
258bc8 |
+static struct compressor zstd_comp_ops = {
|
|
Bruno Wolff III |
258bc8 |
+ ZSTD_COMPRESSION, "zstd"
|
|
Bruno Wolff III |
258bc8 |
+};
|
|
Bruno Wolff III |
258bc8 |
+#else
|
|
Bruno Wolff III |
258bc8 |
+extern struct compressor zstd_comp_ops;
|
|
Bruno Wolff III |
258bc8 |
+#endif
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
static struct compressor unknown_comp_ops = {
|
|
Bruno Wolff III |
258bc8 |
0, "unknown"
|
|
Bruno Wolff III |
258bc8 |
@@ -77,6 +84,7 @@ struct compressor *compressor[] = {
|
|
Bruno Wolff III |
258bc8 |
&lzo_comp_ops,
|
|
Bruno Wolff III |
258bc8 |
&lz4_comp_ops,
|
|
Bruno Wolff III |
258bc8 |
&xz_comp_ops,
|
|
Bruno Wolff III |
258bc8 |
+ &zstd_comp_ops,
|
|
Bruno Wolff III |
258bc8 |
&unknown_comp_ops
|
|
Bruno Wolff III |
258bc8 |
};
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
diff --git a/squashfs-tools/squashfs_fs.h b/squashfs-tools/squashfs_fs.h
|
|
Bruno Wolff III |
258bc8 |
index 791fe12..afca918 100644
|
|
Bruno Wolff III |
258bc8 |
--- a/squashfs-tools/squashfs_fs.h
|
|
Bruno Wolff III |
258bc8 |
+++ b/squashfs-tools/squashfs_fs.h
|
|
Bruno Wolff III |
258bc8 |
@@ -277,6 +277,7 @@ typedef long long squashfs_inode;
|
|
Bruno Wolff III |
258bc8 |
#define LZO_COMPRESSION 3
|
|
Bruno Wolff III |
258bc8 |
#define XZ_COMPRESSION 4
|
|
Bruno Wolff III |
258bc8 |
#define LZ4_COMPRESSION 5
|
|
Bruno Wolff III |
258bc8 |
+#define ZSTD_COMPRESSION 6
|
|
Bruno Wolff III |
258bc8 |
|
|
Bruno Wolff III |
258bc8 |
struct squashfs_super_block {
|
|
Bruno Wolff III |
258bc8 |
unsigned int s_magic;
|
|
Bruno Wolff III |
258bc8 |
diff --git a/squashfs-tools/zstd_wrapper.c b/squashfs-tools/zstd_wrapper.c
|
|
Bruno Wolff III |
258bc8 |
new file mode 100644
|
|
Bruno Wolff III |
258bc8 |
index 0000000..dcab75a
|
|
Bruno Wolff III |
258bc8 |
--- /dev/null
|
|
Bruno Wolff III |
258bc8 |
+++ b/squashfs-tools/zstd_wrapper.c
|
|
Bruno Wolff III |
258bc8 |
@@ -0,0 +1,254 @@
|
|
Bruno Wolff III |
258bc8 |
+/*
|
|
Bruno Wolff III |
258bc8 |
+ * Copyright (c) 2017
|
|
Bruno Wolff III |
258bc8 |
+ * Phillip Lougher <phillip@squashfs.org.uk>
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This program is free software; you can redistribute it and/or
|
|
Bruno Wolff III |
258bc8 |
+ * modify it under the terms of the GNU General Public License
|
|
Bruno Wolff III |
258bc8 |
+ * as published by the Free Software Foundation; either version 2,
|
|
Bruno Wolff III |
258bc8 |
+ * or (at your option) any later version.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This program is distributed in the hope that it will be useful,
|
|
Bruno Wolff III |
258bc8 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Bruno Wolff III |
258bc8 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Bruno Wolff III |
258bc8 |
+ * GNU General Public License for more details.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * zstd_wrapper.c
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * Support for ZSTD compression http://zstd.net
|
|
Bruno Wolff III |
258bc8 |
+ */
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+#include <stdio.h>
|
|
Bruno Wolff III |
258bc8 |
+#include <string.h>
|
|
Bruno Wolff III |
258bc8 |
+#include <stdlib.h>
|
|
Bruno Wolff III |
258bc8 |
+#include <zstd.h>
|
|
Bruno Wolff III |
258bc8 |
+#include <zstd_errors.h>
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+#include "squashfs_fs.h"
|
|
Bruno Wolff III |
258bc8 |
+#include "zstd_wrapper.h"
|
|
Bruno Wolff III |
258bc8 |
+#include "compressor.h"
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+static int compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+/*
|
|
Bruno Wolff III |
258bc8 |
+ * This function is called by the options parsing code in mksquashfs.c
|
|
Bruno Wolff III |
258bc8 |
+ * to parse any -X compressor option.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This function returns:
|
|
Bruno Wolff III |
258bc8 |
+ * >=0 (number of additional args parsed) on success
|
|
Bruno Wolff III |
258bc8 |
+ * -1 if the option was unrecognised, or
|
|
Bruno Wolff III |
258bc8 |
+ * -2 if the option was recognised, but otherwise bad in
|
|
Bruno Wolff III |
258bc8 |
+ * some way (e.g. invalid parameter)
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * Note: this function sets internal compressor state, but does not
|
|
Bruno Wolff III |
258bc8 |
+ * pass back the results of the parsing other than success/failure.
|
|
Bruno Wolff III |
258bc8 |
+ * The zstd_dump_options() function is called later to get the options in
|
|
Bruno Wolff III |
258bc8 |
+ * a format suitable for writing to the filesystem.
|
|
Bruno Wolff III |
258bc8 |
+ */
|
|
Bruno Wolff III |
258bc8 |
+static int zstd_options(char *argv[], int argc)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ if (strcmp(argv[0], "-Xcompression-level") == 0) {
|
|
Bruno Wolff III |
258bc8 |
+ if (argc < 2) {
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: -Xcompression-level missing "
|
|
Bruno Wolff III |
258bc8 |
+ "compression level\n");
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: -Xcompression-level it should "
|
|
Bruno Wolff III |
258bc8 |
+ "be 1 <= n <= %d\n", ZSTD_maxCLevel());
|
|
Bruno Wolff III |
258bc8 |
+ goto failed;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ compression_level = atoi(argv[1]);
|
|
Bruno Wolff III |
258bc8 |
+ if (compression_level < 1 ||
|
|
Bruno Wolff III |
258bc8 |
+ compression_level > ZSTD_maxCLevel()) {
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: -Xcompression-level invalid, it "
|
|
Bruno Wolff III |
258bc8 |
+ "should be 1 <= n <= %d\n", ZSTD_maxCLevel());
|
|
Bruno Wolff III |
258bc8 |
+ goto failed;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ return 1;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ return -1;
|
|
Bruno Wolff III |
258bc8 |
+failed:
|
|
Bruno Wolff III |
258bc8 |
+ return -2;
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+/*
|
|
Bruno Wolff III |
258bc8 |
+ * This function is called by mksquashfs to dump the parsed
|
|
Bruno Wolff III |
258bc8 |
+ * compressor options in a format suitable for writing to the
|
|
Bruno Wolff III |
258bc8 |
+ * compressor options field in the filesystem (stored immediately
|
|
Bruno Wolff III |
258bc8 |
+ * after the superblock).
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This function returns a pointer to the compression options structure
|
|
Bruno Wolff III |
258bc8 |
+ * to be stored (and the size), or NULL if there are no compression
|
|
Bruno Wolff III |
258bc8 |
+ * options.
|
|
Bruno Wolff III |
258bc8 |
+ */
|
|
Bruno Wolff III |
258bc8 |
+static void *zstd_dump_options(int block_size, int *size)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ static struct zstd_comp_opts comp_opts;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ /* don't return anything if the options are all default */
|
|
Bruno Wolff III |
258bc8 |
+ if (compression_level == ZSTD_DEFAULT_COMPRESSION_LEVEL)
|
|
Bruno Wolff III |
258bc8 |
+ return NULL;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ comp_opts.compression_level = compression_level;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ SQUASHFS_INSWAP_COMP_OPTS(&comp_opts);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ *size = sizeof(comp_opts);
|
|
Bruno Wolff III |
258bc8 |
+ return &comp_opts;
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+/*
|
|
Bruno Wolff III |
258bc8 |
+ * This function is a helper specifically for the append mode of
|
|
Bruno Wolff III |
258bc8 |
+ * mksquashfs. Its purpose is to set the internal compressor state
|
|
Bruno Wolff III |
258bc8 |
+ * to the stored compressor options in the passed compressor options
|
|
Bruno Wolff III |
258bc8 |
+ * structure.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * In effect this function sets up the compressor options
|
|
Bruno Wolff III |
258bc8 |
+ * to the same state they were when the filesystem was originally
|
|
Bruno Wolff III |
258bc8 |
+ * generated, this is to ensure on appending, the compressor uses
|
|
Bruno Wolff III |
258bc8 |
+ * the same compression options that were used to generate the
|
|
Bruno Wolff III |
258bc8 |
+ * original filesystem.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * Note, even if there are no compressor options, this function is still
|
|
Bruno Wolff III |
258bc8 |
+ * called with an empty compressor structure (size == 0), to explicitly
|
|
Bruno Wolff III |
258bc8 |
+ * set the default options, this is to ensure any user supplied
|
|
Bruno Wolff III |
258bc8 |
+ * -X options on the appending mksquashfs command line are over-ridden.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This function returns 0 on sucessful extraction of options, and -1 on error.
|
|
Bruno Wolff III |
258bc8 |
+ */
|
|
Bruno Wolff III |
258bc8 |
+static int zstd_extract_options(int block_size, void *buffer, int size)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ struct zstd_comp_opts *comp_opts = buffer;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ if (size == 0) {
|
|
Bruno Wolff III |
258bc8 |
+ /* Set default values */
|
|
Bruno Wolff III |
258bc8 |
+ compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
|
|
Bruno Wolff III |
258bc8 |
+ return 0;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ /* we expect a comp_opts structure of sufficient size to be present */
|
|
Bruno Wolff III |
258bc8 |
+ if (size < sizeof(*comp_opts))
|
|
Bruno Wolff III |
258bc8 |
+ goto failed;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ if (comp_opts->compression_level < 1 ||
|
|
Bruno Wolff III |
258bc8 |
+ comp_opts->compression_level > ZSTD_maxCLevel()) {
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: bad compression level in compression "
|
|
Bruno Wolff III |
258bc8 |
+ "options structure\n");
|
|
Bruno Wolff III |
258bc8 |
+ goto failed;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ compression_level = comp_opts->compression_level;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ return 0;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+failed:
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: error reading stored compressor options from "
|
|
Bruno Wolff III |
258bc8 |
+ "filesystem!\n");
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ return -1;
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+static void zstd_display_options(void *buffer, int size)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ struct zstd_comp_opts *comp_opts = buffer;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ /* we expect a comp_opts structure of sufficient size to be present */
|
|
Bruno Wolff III |
258bc8 |
+ if (size < sizeof(*comp_opts))
|
|
Bruno Wolff III |
258bc8 |
+ goto failed;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ if (comp_opts->compression_level < 1 ||
|
|
Bruno Wolff III |
258bc8 |
+ comp_opts->compression_level > ZSTD_maxCLevel()) {
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: bad compression level in compression "
|
|
Bruno Wolff III |
258bc8 |
+ "options structure\n");
|
|
Bruno Wolff III |
258bc8 |
+ goto failed;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ printf("\tcompression-level %d\n", comp_opts->compression_level);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ return;
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+failed:
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: error reading stored compressor options from "
|
|
Bruno Wolff III |
258bc8 |
+ "filesystem!\n");
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+/*
|
|
Bruno Wolff III |
258bc8 |
+ * This function is called by mksquashfs to initialise the
|
|
Bruno Wolff III |
258bc8 |
+ * compressor, before compress() is called.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This function returns 0 on success, and -1 on error.
|
|
Bruno Wolff III |
258bc8 |
+ */
|
|
Bruno Wolff III |
258bc8 |
+static int zstd_init(void **strm, int block_size, int datablock)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ ZSTD_CCtx *cctx = ZSTD_createCCtx();
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ if (!cctx) {
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "zstd: failed to allocate compression "
|
|
Bruno Wolff III |
258bc8 |
+ "context!\n");
|
|
Bruno Wolff III |
258bc8 |
+ return -1;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ *strm = cctx;
|
|
Bruno Wolff III |
258bc8 |
+ return 0;
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+static int zstd_compress(void *strm, void *dest, void *src, int size,
|
|
Bruno Wolff III |
258bc8 |
+ int block_size, int *error)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ const size_t res = ZSTD_compressCCtx((ZSTD_CCtx*)strm, dest, block_size,
|
|
Bruno Wolff III |
258bc8 |
+ src, size, compression_level);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ if (ZSTD_isError(res)) {
|
|
Bruno Wolff III |
258bc8 |
+ /* FIXME:
|
|
Bruno Wolff III |
258bc8 |
+ * zstd does not expose stable error codes. The error enum may
|
|
Bruno Wolff III |
258bc8 |
+ * change between versions. Until upstream zstd stablizes the
|
|
Bruno Wolff III |
258bc8 |
+ * error codes, we have no way of knowing why the error occurs.
|
|
Bruno Wolff III |
258bc8 |
+ * zstd shouldn't fail to compress any input unless there isn't
|
|
Bruno Wolff III |
258bc8 |
+ * enough output space. We assume that is the cause and return
|
|
Bruno Wolff III |
258bc8 |
+ * the special error code for not enough output space.
|
|
Bruno Wolff III |
258bc8 |
+ */
|
|
Bruno Wolff III |
258bc8 |
+ return 0;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ return (int)res;
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+static int zstd_uncompress(void *dest, void *src, int size, int outsize,
|
|
Bruno Wolff III |
258bc8 |
+ int *error)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ const size_t res = ZSTD_decompress(dest, outsize, src, size);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ if (ZSTD_isError(res)) {
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "\t%d %d\n", outsize, size);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ *error = (int)ZSTD_getErrorCode(res);
|
|
Bruno Wolff III |
258bc8 |
+ return -1;
|
|
Bruno Wolff III |
258bc8 |
+ }
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+ return (int)res;
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+static void zstd_usage(void)
|
|
Bruno Wolff III |
258bc8 |
+{
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "\t -Xcompression-level <compression-level>\n");
|
|
Bruno Wolff III |
258bc8 |
+ fprintf(stderr, "\t\t<compression-level> should be 1 .. %d (default "
|
|
Bruno Wolff III |
258bc8 |
+ "%d)\n", ZSTD_maxCLevel(), ZSTD_DEFAULT_COMPRESSION_LEVEL);
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+struct compressor zstd_comp_ops = {
|
|
Bruno Wolff III |
258bc8 |
+ .init = zstd_init,
|
|
Bruno Wolff III |
258bc8 |
+ .compress = zstd_compress,
|
|
Bruno Wolff III |
258bc8 |
+ .uncompress = zstd_uncompress,
|
|
Bruno Wolff III |
258bc8 |
+ .options = zstd_options,
|
|
Bruno Wolff III |
258bc8 |
+ .dump_options = zstd_dump_options,
|
|
Bruno Wolff III |
258bc8 |
+ .extract_options = zstd_extract_options,
|
|
Bruno Wolff III |
258bc8 |
+ .display_options = zstd_display_options,
|
|
Bruno Wolff III |
258bc8 |
+ .usage = zstd_usage,
|
|
Bruno Wolff III |
258bc8 |
+ .id = ZSTD_COMPRESSION,
|
|
Bruno Wolff III |
258bc8 |
+ .name = "zstd",
|
|
Bruno Wolff III |
258bc8 |
+ .supported = 1
|
|
Bruno Wolff III |
258bc8 |
+};
|
|
Bruno Wolff III |
258bc8 |
diff --git a/squashfs-tools/zstd_wrapper.h b/squashfs-tools/zstd_wrapper.h
|
|
Bruno Wolff III |
258bc8 |
new file mode 100644
|
|
Bruno Wolff III |
258bc8 |
index 0000000..4fbef0a
|
|
Bruno Wolff III |
258bc8 |
--- /dev/null
|
|
Bruno Wolff III |
258bc8 |
+++ b/squashfs-tools/zstd_wrapper.h
|
|
Bruno Wolff III |
258bc8 |
@@ -0,0 +1,48 @@
|
|
Bruno Wolff III |
258bc8 |
+#ifndef ZSTD_WRAPPER_H
|
|
Bruno Wolff III |
258bc8 |
+#define ZSTD_WRAPPER_H
|
|
Bruno Wolff III |
258bc8 |
+/*
|
|
Bruno Wolff III |
258bc8 |
+ * Squashfs
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * Copyright (c) 2017
|
|
Bruno Wolff III |
258bc8 |
+ * Phillip Lougher <phillip@squashfs.org.uk>
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This program is free software; you can redistribute it and/or
|
|
Bruno Wolff III |
258bc8 |
+ * modify it under the terms of the GNU General Public License
|
|
Bruno Wolff III |
258bc8 |
+ * as published by the Free Software Foundation; either version 2,
|
|
Bruno Wolff III |
258bc8 |
+ * or (at your option) any later version.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * This program is distributed in the hope that it will be useful,
|
|
Bruno Wolff III |
258bc8 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Bruno Wolff III |
258bc8 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Bruno Wolff III |
258bc8 |
+ * GNU General Public License for more details.
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ * zstd_wrapper.h
|
|
Bruno Wolff III |
258bc8 |
+ *
|
|
Bruno Wolff III |
258bc8 |
+ */
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+#ifndef linux
|
|
Bruno Wolff III |
258bc8 |
+#define __BYTE_ORDER BYTE_ORDER
|
|
Bruno Wolff III |
258bc8 |
+#define __BIG_ENDIAN BIG_ENDIAN
|
|
Bruno Wolff III |
258bc8 |
+#define __LITTLE_ENDIAN LITTLE_ENDIAN
|
|
Bruno Wolff III |
258bc8 |
+#else
|
|
Bruno Wolff III |
258bc8 |
+#include <endian.h>
|
|
Bruno Wolff III |
258bc8 |
+#endif
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+#if __BYTE_ORDER == __BIG_ENDIAN
|
|
Bruno Wolff III |
258bc8 |
+extern unsigned int inswap_le16(unsigned short);
|
|
Bruno Wolff III |
258bc8 |
+extern unsigned int inswap_le32(unsigned int);
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+#define SQUASHFS_INSWAP_COMP_OPTS(s) { \
|
|
Bruno Wolff III |
258bc8 |
+ (s)->compression_level = inswap_le32((s)->compression_level); \
|
|
Bruno Wolff III |
258bc8 |
+}
|
|
Bruno Wolff III |
258bc8 |
+#else
|
|
Bruno Wolff III |
258bc8 |
+#define SQUASHFS_INSWAP_COMP_OPTS(s)
|
|
Bruno Wolff III |
258bc8 |
+#endif
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+/* Default compression */
|
|
Bruno Wolff III |
258bc8 |
+#define ZSTD_DEFAULT_COMPRESSION_LEVEL 15
|
|
Bruno Wolff III |
258bc8 |
+
|
|
Bruno Wolff III |
258bc8 |
+struct zstd_comp_opts {
|
|
Bruno Wolff III |
258bc8 |
+ int compression_level;
|
|
Bruno Wolff III |
258bc8 |
+};
|
|
Bruno Wolff III |
258bc8 |
+#endif
|
|
Bruno Wolff III |
258bc8 |
--
|
|
Bruno Wolff III |
258bc8 |
2.19.1
|
|
Bruno Wolff III |
258bc8 |
|