Browse code

lz4: Move towards a newer LZ4 API

We are using a deprecated function, LZ4_compress_limitedOutput(), which
will be removed with time. The correct function to use is
LZ4_compress_default(). Both function takes the same number of
arguments and data types, so the change is minimal.

This patch will also enforce the system LZ4 library to be at least v1.7.1.
If the system library is not found or it is older, it will be build using
the bundled LZ4 library. The version number requirement is based on the
LZ4 version we ship.

The changes in configure.ac for the version check is modelled around the
same approach we use for OpenSSL. Plus it does a few minor reformats and
improvements to comply with more recommend autoconf coding style.

This patch is a result of the discussions in this mail thread:
https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg14135.html

Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20170907172004.22534-1-davids@openvpn.net>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg15396.html
Signed-off-by: David Sommerseth <davids@openvpn.net>

David Sommerseth authored on 2017/09/08 02:20:04
Showing 2 changed files
... ...
@@ -1088,37 +1088,67 @@ dnl
1088 1088
 AC_ARG_VAR([LZ4_CFLAGS], [C compiler flags for lz4])
1089 1089
 AC_ARG_VAR([LZ4_LIBS], [linker flags for lz4])
1090 1090
 if test "$enable_lz4" = "yes" && test "$enable_comp_stub" = "no"; then
1091
-    AC_CHECKING([for LZ4 Library and Header files])
1092
-    havelz4lib=1
1093
-
1094
-    # if LZ4_LIBS is set, we assume it will work, otherwise test
1095
-    if test -z "${LZ4_LIBS}"; then
1096
-	AC_CHECK_LIB(lz4, LZ4_compress,
1097
-	    [ LZ4_LIBS="-llz4" ],
1098
-	    [
1099
-	        AC_MSG_RESULT([LZ4 library not found.])
1100
-	        havelz4lib=0
1101
-	    ])
1091
+    if test -z "${LZ4_CFLAGS}" -a -z "${LZ4_LIBS}"; then
1092
+	# if the user did not explicitly specify flags, try to autodetect
1093
+	PKG_CHECK_MODULES([LZ4],
1094
+			  [liblz4 >= 1.7.1],
1095
+			  [have_lz4="yes"],
1096
+			  [] # If this fails, we will do another test next
1097
+	)
1102 1098
     fi
1103 1099
 
1104 1100
     saved_CFLAGS="${CFLAGS}"
1101
+    saved_LIBS="${LIBS}"
1105 1102
     CFLAGS="${CFLAGS} ${LZ4_CFLAGS}"
1106
-    AC_CHECK_HEADERS(lz4.h,
1107
-       ,
1108
-       [
1109
-	   AC_MSG_RESULT([LZ4 headers not found.])
1110
-	   havelz4lib=0
1111
-       ])
1103
+    LIBS="${LIBS} ${LZ4_LIBS}"
1104
+
1105
+    # If pkgconfig check failed or LZ4_CFLAGS/LZ4_LIBS env vars
1106
+    # are used, check the version directly in the LZ4 include file
1107
+    if test "${have_lz4}" != "yes"; then
1108
+	AC_CHECK_HEADERS([lz4.h],
1109
+			 [have_lz4h="yes"],
1110
+			 [])
1111
+
1112
+	if test "${have_lz4h}" = "yes" ; then
1113
+	    AC_MSG_CHECKING([additionally if system LZ4 version >= 1.7.1])
1114
+	    AC_COMPILE_IFELSE(
1115
+		[AC_LANG_PROGRAM([[
1116
+#include <lz4.h>
1117
+				 ]],
1118
+				 [[
1119
+/* Version encoding: MMNNPP (Major miNor Patch) - see lz4.h for details */
1120
+#if LZ4_VERSION_NUMBER < 10701L
1121
+#error LZ4 is too old
1122
+#endif
1123
+				 ]]
1124
+				)],
1125
+		[
1126
+		    AC_MSG_RESULT([ok])
1127
+		    have_lz4="yes"
1128
+		],
1129
+		[AC_MSG_RESULT([system LZ4 library is too old])]
1130
+	    )
1131
+	fi
1132
+    fi
1133
+
1134
+    # if LZ4_LIBS is set, we assume it will work, otherwise test
1135
+    if test -z "${LZ4_LIBS}"; then
1136
+	AC_CHECK_LIB([lz4],
1137
+		     [LZ4_compress],
1138
+		     [LZ4_LIBS="-llz4"],
1139
+		     [have_lz4="no"])
1140
+    fi
1112 1141
 
1113
-    if test $havelz4lib = 0 ; then
1114
-	AC_MSG_RESULT([LZ4 library or header not found, using version in src/compat/compat-lz4.*])
1142
+    if test "${have_lz4}" != "yes" ; then
1143
+	AC_MSG_RESULT([		usuable LZ4 library or header not found, using version in src/compat/compat-lz4.*])
1115 1144
 	AC_DEFINE([NEED_COMPAT_LZ4], [1], [use copy of LZ4 source in compat/])
1116 1145
 	LZ4_LIBS=""
1117 1146
     fi
1118 1147
     OPTIONAL_LZ4_CFLAGS="${LZ4_CFLAGS}"
1119 1148
     OPTIONAL_LZ4_LIBS="${LZ4_LIBS}"
1120
-    AC_DEFINE(ENABLE_LZ4, 1, [Enable LZ4 compression library])
1149
+    AC_DEFINE(ENABLE_LZ4, [1], [Enable LZ4 compression library])
1121 1150
     CFLAGS="${saved_CFLAGS}"
1151
+    LIBS="${saved_LIBS}"
1122 1152
 fi
1123 1153
 
1124 1154
 
... ...
@@ -43,6 +43,7 @@
43 43
 
44 44
 #include "memdbg.h"
45 45
 
46
+
46 47
 static void
47 48
 lz4_compress_init(struct compress_context *compctx)
48 49
 {
... ...
@@ -86,7 +87,7 @@ do_lz4_compress(struct buffer *buf,
86 86
             return false;
87 87
         }
88 88
 
89
-        zlen = LZ4_compress_limitedOutput((const char *)BPTR(buf), (char *)BPTR(work), BLEN(buf), zlen_max );
89
+        zlen = LZ4_compress_default((const char *)BPTR(buf), (char *)BPTR(work), BLEN(buf), zlen_max);
90 90
 
91 91
         if (zlen <= 0)
92 92
         {