File Implement-zstd-compression-for-diskdump.patch of Package libkdumpfile.36085

From: Petr Tesarik <ptesarik@suse.com>
Date: Fri, 4 Feb 2022 23:28:50 +0100
Subject: Implement zstd compression for diskdump
References: bsc#1231429
Upstream: merged
Git-commit: aee86952e5e80558844cccb6a4bb18ab4f5bc55c

Zstandard compression was added in makedumpfile-1.7.0. Add
corresponding support to libkdumpfile.

[ ptesarik: Removed tests. ]
Signed-off-by: Petr Tesarik <ptesarik@suse.com>
---
 config.h.in               |    3 
 configure                 |  239 ++++++++++++++++++++++++++++++++++++++++++++++
 configure.ac              |    1 
 src/kdumpfile/Makefile.am |    6 -
 src/kdumpfile/Makefile.in |   10 +
 src/kdumpfile/diskdump.c  |   23 ++++
 6 files changed, 278 insertions(+), 4 deletions(-)
 create mode 100755 tests/diskdump-basic-zstd

--- a/configure.ac
+++ b/configure.ac
@@ -65,6 +65,7 @@ m4_pattern_forbid([^_?PKG_[A-Z_]+$],[***
 kdump_COMPRESSION(zlib, ZLIB, z, uncompress)
 kdump_COMPRESSION(lzo, LZO, lzo2, lzo1x_decompress_safe)
 kdump_COMPRESSION(snappy, SNAPPY, snappy, snappy_uncompress)
+kdump_COMPRESSION(libzstd, ZSTD, zstd, ZSTD_decompress)
 
 dnl check for pthread support
 AC_ARG_WITH(pthread,
--- a/src/kdumpfile/Makefile.am
+++ b/src/kdumpfile/Makefile.am
@@ -23,7 +23,8 @@ AM_CPPFLAGS = -I$(top_builddir)/include
 AM_CFLAGS = -fvisibility=hidden \
 	$(ZLIB_CFLAGS)	\
 	$(LZO_CFLAGS)	\
-	$(SNAPPY_CFLAGS)
+	$(SNAPPY_CFLAGS) \
+	$(ZSTD_CFLAGS)
 
 lib_LTLIBRARIES = libkdumpfile.la
 libkdumpfile_la_SOURCES = \
@@ -56,7 +57,8 @@ libkdumpfile_la_LIBADD = \
 	$(top_builddir)/src/addrxlat/libaddrxlat.la	\
 	$(ZLIB_LIBS)	\
 	$(LZO_LIBS)	\
-	$(SNAPPY_LIBS)
+	$(SNAPPY_LIBS)	\
+	$(ZSTD_LIBS)
 
 libkdumpfile_la_LDFLAGS = -version-info 9:0:0
 
--- a/src/kdumpfile/diskdump.c
+++ b/src/kdumpfile/diskdump.c
@@ -42,6 +42,9 @@
 #if USE_SNAPPY
 # include <snappy-c.h>
 #endif
+#if USE_ZSTD
+# include <zstd.h>
+#endif
 
 #define SIG_LEN	8
 
@@ -176,12 +179,14 @@ struct setup_data {
 #define DUMP_DH_COMPRESSED_ZLIB	0x1	/* page is compressed with zlib */
 #define DUMP_DH_COMPRESSED_LZO	0x2	/* page is compressed with lzo */
 #define DUMP_DH_COMPRESSED_SNAPPY 0x4	/* page is compressed with snappy */
+#define DUMP_DH_COMPRESSED_ZSTD	0x20	/* page is compressed with zstd */
 
 /* Any compression flag */
 #define DUMP_DH_COMPRESSED	( 0	\
 	| DUMP_DH_COMPRESSED_ZLIB	\
 	| DUMP_DH_COMPRESSED_LZO	\
 	| DUMP_DH_COMPRESSED_SNAPPY	\
+	| DUMP_DH_COMPRESSED_ZSTD	\
 		)
 
 static void diskdump_cleanup(struct kdump_shared *shared);
@@ -460,6 +465,24 @@ diskdump_read_page(kdump_ctx_t *ctx, str
 				 "Unsupported compression method: %s",
 				 "snappy");
 #endif
+	} else if (pd.flags & DUMP_DH_COMPRESSED_ZSTD) {
+#if USE_ZSTD
+		size_t ret;
+		ret = ZSTD_decompress(pio->chunk.data, get_page_size(ctx),
+				      fch.data, pd.size);
+		fcache_put_chunk(&fch);
+		if (ZSTD_isError(ret))
+			return set_error(ctx, KDUMP_ERR_CORRUPT,
+					 "Decompression failed: %s",
+					 ZSTD_getErrorName(ret));
+		if (ret != get_page_size(ctx))
+			return set_error(ctx, KDUMP_ERR_CORRUPT,
+					 "Wrong uncompressed size: %zu", ret);
+#else
+		return set_error(ctx, KDUMP_ERR_NOTIMPL,
+				 "Unsupported compression method: %s",
+				 "zstd");
+#endif
 	}
 
 	return KDUMP_OK;
--- a/configure
+++ b/configure
@@ -651,6 +651,9 @@ PYTHON_PREFIX
 PYTHON_VERSION
 PYTHON
 PTHREAD_LIBS
+ZSTD_REQUIRES
+ZSTD_LIBS
+ZSTD_CFLAGS
 SNAPPY_REQUIRES
 SNAPPY_LIBS
 SNAPPY_CFLAGS
@@ -848,6 +851,7 @@ enable_internal_doc
 with_zlib
 with_lzo
 with_snappy
+with_libzstd
 with_pthread
 enable_debug
 with_python
@@ -872,6 +876,8 @@ LZO_CFLAGS
 LZO_LIBS
 SNAPPY_CFLAGS
 SNAPPY_LIBS
+ZSTD_CFLAGS
+ZSTD_LIBS
 PYTHON'
 
 
@@ -1531,6 +1537,7 @@ Optional Packages:
   --with-zlib             support for zlib compression [default=check]
   --with-lzo              support for lzo compression [default=check]
   --with-snappy           support for snappy compression [default=check]
+  --with-libzstd          support for libzstd compression [default=check]
   --without-pthread       pthread support [default=check]
     --with-python           build Python bindings [default=check]
 
@@ -1559,6 +1566,8 @@ Some influential environment variables:
   SNAPPY_CFLAGS
               C compiler flags for SNAPPY, overriding pkg-config
   SNAPPY_LIBS linker flags for SNAPPY, overriding pkg-config
+  ZSTD_CFLAGS C compiler flags for ZSTD, overriding pkg-config
+  ZSTD_LIBS   linker flags for ZSTD, overriding pkg-config
   PYTHON      the Python interpreter
 
 Use these variables to override the choices made by `configure' or to help
@@ -15192,6 +15201,236 @@ else
 
 fi
 
+fi
+
+
+
+
+
+# Check whether --with-libzstd was given.
+if test "${with_libzstd+set}" = set; then :
+  withval=$with_libzstd;
+else
+  with_libzstd=check
+fi
+
+if test "x$with_libzstd" != xno; then :
+
+pkg_failed=no
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for libzstd" >&5
+$as_echo_n "checking for libzstd... " >&6; }
+
+if test -n "$ZSTD_CFLAGS"; then
+    pkg_cv_ZSTD_CFLAGS="$ZSTD_CFLAGS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"zstd\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libzstd") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_ZSTD_CFLAGS=`$PKG_CONFIG --cflags "libzstd" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+if test -n "$ZSTD_LIBS"; then
+    pkg_cv_ZSTD_LIBS="$ZSTD_LIBS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libzstd\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libzstd") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_ZSTD_LIBS=`$PKG_CONFIG --libs "libzstd" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+
+
+
+if test $pkg_failed = yes; then
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
+fi
+        if test $_pkg_short_errors_supported = yes; then
+	        ZSTD_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libzstd" 2>&1`
+        else
+	        ZSTD_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libzstd" 2>&1`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$ZSTD_PKG_ERRORS" >&5
+
+	      saved_LIBS="$LIBS"
+      { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing ZSTD_decompress" >&5
+$as_echo_n "checking for library containing ZSTD_decompress... " >&6; }
+if ${ac_cv_search_ZSTD_decompress+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_func_search_save_LIBS=$LIBS
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char ZSTD_decompress ();
+int
+main ()
+{
+return ZSTD_decompress ();
+  ;
+  return 0;
+}
+_ACEOF
+for ac_lib in '' zstd; do
+  if test -z "$ac_lib"; then
+    ac_res="none required"
+  else
+    ac_res=-l$ac_lib
+    LIBS="-l$ac_lib  $ac_func_search_save_LIBS"
+  fi
+  if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_search_ZSTD_decompress=$ac_res
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext
+  if ${ac_cv_search_ZSTD_decompress+:} false; then :
+  break
+fi
+done
+if ${ac_cv_search_ZSTD_decompress+:} false; then :
+
+else
+  ac_cv_search_ZSTD_decompress=no
+fi
+rm conftest.$ac_ext
+LIBS=$ac_func_search_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_ZSTD_decompress" >&5
+$as_echo "$ac_cv_search_ZSTD_decompress" >&6; }
+ac_res=$ac_cv_search_ZSTD_decompress
+if test "$ac_res" != no; then :
+  test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
+          ZSTD_LIBS=-lzstd
+        have_libzstd=yes
+
+else
+          have_libzstd=no
+
+fi
+
+      LIBS="$saved_LIBS"
+
+elif test $pkg_failed = untried; then
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+	      saved_LIBS="$LIBS"
+      { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing ZSTD_decompress" >&5
+$as_echo_n "checking for library containing ZSTD_decompress... " >&6; }
+if ${ac_cv_search_ZSTD_decompress+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_func_search_save_LIBS=$LIBS
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char ZSTD_decompress ();
+int
+main ()
+{
+return ZSTD_decompress ();
+  ;
+  return 0;
+}
+_ACEOF
+for ac_lib in '' zstd; do
+  if test -z "$ac_lib"; then
+    ac_res="none required"
+  else
+    ac_res=-l$ac_lib
+    LIBS="-l$ac_lib  $ac_func_search_save_LIBS"
+  fi
+  if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_search_ZSTD_decompress=$ac_res
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext
+  if ${ac_cv_search_ZSTD_decompress+:} false; then :
+  break
+fi
+done
+if ${ac_cv_search_ZSTD_decompress+:} false; then :
+
+else
+  ac_cv_search_ZSTD_decompress=no
+fi
+rm conftest.$ac_ext
+LIBS=$ac_func_search_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_ZSTD_decompress" >&5
+$as_echo "$ac_cv_search_ZSTD_decompress" >&6; }
+ac_res=$ac_cv_search_ZSTD_decompress
+if test "$ac_res" != no; then :
+  test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
+          ZSTD_LIBS=-lzstd
+        have_libzstd=yes
+
+else
+          have_libzstd=no
+
+fi
+
+      LIBS="$saved_LIBS"
+
+else
+	ZSTD_CFLAGS=$pkg_cv_ZSTD_CFLAGS
+	ZSTD_LIBS=$pkg_cv_ZSTD_LIBS
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+	ZSTD_REQUIRES=libzstd
+      have_libzstd=yes
+
+fi
+
+else
+     have_libzstd=no
+
+fi
+if test "x$have_libzstd" = xyes; then :
+
+$as_echo "#define USE_ZSTD 1" >>confdefs.h
+
+
+else
+
+   if test "x$with_libzstd" = xyes; then :
+  as_fn_error $? "libzstd requested but neither pkg-config nor -lzstd found" "$LINENO" 5
+
+fi
+
 fi
 
 
--- a/src/kdumpfile/Makefile.in
+++ b/src/kdumpfile/Makefile.in
@@ -522,6 +522,10 @@ VERSION = @VERSION@
 ZLIB_CFLAGS = @ZLIB_CFLAGS@
 ZLIB_LIBS = @ZLIB_LIBS@
 ZLIB_REQUIRES = @ZLIB_REQUIRES@
+ZSTD_CFLAGS = @ZSTD_CFLAGS@
+ZSTD_LIBS = @ZSTD_LIBS@
+ZSTD_PC_LIBS = @ZSTD_PC_LIBS@
+ZSTD_REQUIRES = @ZSTD_REQUIRES@
 abs_builddir = @abs_builddir@
 abs_srcdir = @abs_srcdir@
 abs_top_builddir = @abs_top_builddir@
@@ -582,7 +586,8 @@ AM_CPPFLAGS = -I$(top_builddir)/include
 AM_CFLAGS = -fvisibility=hidden \
 	$(ZLIB_CFLAGS)	\
 	$(LZO_CFLAGS)	\
-	$(SNAPPY_CFLAGS)
+	$(SNAPPY_CFLAGS) \
+	$(ZSTD_CFLAGS)
 
 lib_LTLIBRARIES = libkdumpfile.la
 libkdumpfile_la_SOURCES = \
@@ -615,7 +620,8 @@ libkdumpfile_la_LIBADD = \
 	$(top_builddir)/src/addrxlat/libaddrxlat.la	\
 	$(ZLIB_LIBS)	\
 	$(LZO_LIBS)	\
-	$(SNAPPY_LIBS)
+	$(SNAPPY_LIBS)	\
+	$(ZSTD_LIBS)
 
 libkdumpfile_la_LDFLAGS = -version-info 9:0:0 $(am__append_1)
 @HAVE_LD_VERSION_SCRIPT_TRUE@EXTRA_libkdumpfile_la_DEPENDENCIES = libkdumpfile.map
--- a/config.h.in
+++ b/config.h.in
@@ -75,5 +75,8 @@
 /* Define to enable support for zlib compression */
 #undef USE_ZLIB
 
+/* Define to enable support for libzstd compression */
+#undef USE_ZSTD
+
 /* Version number of package */
 #undef VERSION
openSUSE Build Service is sponsored by