Hello, Rafael

I am the maintainer of gentoo hibernate-script, tuxonice (suspend2).
I wish to add uswsup/suspend into portage.

We do not currently maintain liblzf, as it seems upstream is not cooperative.
I've created a patch so that the user may select to use liblzf or lzo-2
during compilation.

lzo is much better supported, maintained and used by many other applications.
It enables us to maintain this package without new dependency.

There are many compression algorithms in lzo that might be usable
for users. For now, I've selected the standard one, lzo1x_1.

It does not seems to degrate performance, and adds something
like 2K to executables.

This attached patch is against trunk
It adds new compress.c and compress.h files that provides a common
compression interface, the implementation of compress.c use the
requested implementation. Future algorithms may be added to this file
without affecting the main implementation.

Please consider to merge.
I will happy to fix whatever needed.

Best Regards,
Alon Bar-Lev.

---

diff -urNp suspend.org/compress.c suspend/compress.c
--- suspend.org/compress.c      1970-01-01 02:00:00.000000000 +0200
+++ suspend/compress.c  2007-07-18 19:27:57.000000000 +0300
@@ -0,0 +1,80 @@
+/*
+ * compress.c
+ *
+ * Compression for suspend and resume tools.
+ *
+ * Copyright (C) 2006 Rafael J. Wysocki <[EMAIL PROTECTED]>
+ *
+ * This file is released under the GPLv2.
+ *
+ */
+
+#if defined(CONFIG_COMPRESS_ALGO_LZF)
+
+#include <lzf.h>
+
+void
+compress_init(void) {
+}
+
+unsigned int 
+compress_compress(const void *const in_data,  unsigned int in_len,
+       void *out_data, unsigned int out_len)
+{
+       return lzf_compress(in_data, in_len, out_data, out_len);
+}
+
+unsigned int 
+compress_decompress(const void *const in_data,  unsigned int in_len,
+       void *out_data, unsigned int out_len)
+{
+       return lzf_decompress(in_data, in_len, out_data, out_len);
+}
+
+#elif defined(CONFIG_COMPRESS_ALGO_LZO)
+
+#include <lzo/lzo1x.h>
+#include <string.h>
+
+/* Safegaurd, or we may pass max_block_size */
+/* We don't like to dynalloc this */
+#define MAX_COMPRESS_BLOCK (8*1024+64)
+
+static lzo_byte _buffer[MAX_COMPRESS_BLOCK + MAX_COMPRESS_BLOCK/16 + 64 + 3];
+static lzo_byte _work[LZO1X_1_MEM_COMPRESS];
+
+int
+compress_init(void) {
+       return lzo_init() == LZO_E_OK;
+}
+
+unsigned int 
+compress_compress(const void *const in_data,  unsigned int in_len,
+       void *out_data, unsigned int out_len)
+{
+       lzo_uint result_out_len = out_len;
+       if (in_len > MAX_COMPRESS_BLOCK)
+               return 0;
+       if (lzo1x_1_compress(in_data, in_len, _buffer, &result_out_len, _work) 
!= LZO_E_OK)
+               return 0;
+       if (result_out_len >= in_len || result_out_len > out_len)
+               return 0;
+       memcpy (out_data, _buffer, result_out_len);
+       return result_out_len;
+}
+
+unsigned int 
+compress_decompress(const void *const in_data,  unsigned int in_len,
+       void *out_data, unsigned int out_len)
+{
+       lzo_uint result_out_len = out_len;
+       if (lzo1x_decompress_safe(in_data, in_len, out_data, &result_out_len, 
NULL) != LZO_E_OK)
+               return 0;
+       return result_out_len;
+}
+
+#else
+
+static void _dummy () {}
+
+#endif
diff -urNp suspend.org/compress.h suspend/compress.h
--- suspend.org/compress.h      1970-01-01 02:00:00.000000000 +0200
+++ suspend/compress.h  2007-07-18 06:10:57.000000000 +0300
@@ -0,0 +1,26 @@
+/*
+ * compress.h
+ *
+ * Compression-related definitions for user space suspend and resume
+ * tools.
+ *
+ * Copyright (C) 2006 Rafael J. Wysocki <[EMAIL PROTECTED]>
+ *
+ * This file is released under the GPLv2.
+ *
+ */
+
+#ifdef CONFIG_COMPRESS
+
+int
+compress_init (void);
+
+unsigned int 
+compress_compress (const void *const in_data,  unsigned int in_len,
+       void *out_data, unsigned int out_len);
+
+unsigned int 
+compress_decompress (const void *const in_data,  unsigned int in_len,
+       void *out_data, unsigned int out_len);
+
+#endif
diff -urNp suspend.org/HOWTO suspend/HOWTO
--- suspend.org/HOWTO   2007-03-26 17:20:29.000000000 +0200
+++ suspend/HOWTO       2007-07-18 19:59:47.000000000 +0300
@@ -16,7 +16,8 @@ I. Quick start
 crw-r--r--  1 root root 10, 231 Jan 13 21:21 /dev/snapshot
 
 (e) libx86, a hardware-independent library for executing real-mode x86 code
-(f) [optionally] Marc Lehmann's libLZF library (for image compression)
+(f) [optionally] Marc Lehmann's libLZF library (for lzf image compression)
+    [optionally] Markus F.X.J. Oberhumer's lzo library (for lzo image 
compression)
 (g) [optionally] libgcrypt (for image encryption)
 (h) [optionally] libsplashy (for user space splash)
 
@@ -60,7 +61,7 @@ Next, become root and run
 
 (c) [optionally] Install the Marc Lehmann's libLZF library
 
-If you want to use the image compression functionality and libLZF it's not
+If you want to use the image lzf compression functionality and libLZF it's not
 installed on your system, download the tarball from
 http://www.goof.com/pcg/marc/liblzf.html, unpack it, go to the directory
 containing the source and run
@@ -75,6 +76,23 @@ Next, become root and run
 [This will put the lzf.h file into /usr/local/include, the library
 into /usr/local/lib, and the lzf binary into /usr/local/bin.]
 
+    [optionally] Install the Markus F.X.J. Oberhumer's lzo library
+
+If you want to use the image lzo compression functionality and lzo it's not
+installed on your system, download the tarball from
+http://www.oberhumer.com/opensource/lzo, unpack it, go to the directory
+containing the source and run
+
+$ ./configure
+$ make
+
+Next, become root and run
+
+# make install
+
+[This will put the lzo files into /usr/local/include/lzo, the library
+into /usr/local/lib.]
+
 (d) [optionally] Install libgrypt
 
 If you want to use the image encryption functionality of the suspend
diff -urNp suspend.org/Makefile suspend/Makefile
--- suspend.org/Makefile        2007-05-15 22:32:48.000000000 +0300
+++ suspend/Makefile    2007-07-18 19:43:44.000000000 +0300
@@ -1,4 +1,4 @@
-#CONFIG_COMPRESS=yes
+#CONFIG_COMPRESS=lzf|lzo
 #CONFIG_ENCRYPT=yes
 #CONFIG_SPLASHY=yes
 #CONFIG_UDEV=yes
@@ -23,7 +23,7 @@ BINARIES=s2disk s2both s2ram swap-offset
 BINARIES_MIN=s2disk swap-offset
 
 S2RAM_OBJ=vt.o config.o
-SWSUSP_OBJ=vt.o md5.o encrypt.o config.o loglevel.o splash.o bootsplash.o 
+SWSUSP_OBJ=vt.o md5.o encrypt.o compress.o config.o loglevel.o splash.o 
bootsplash.o 
 
 S2RAM_LD_FLAGS = $(LD_FLAGS)
 SWSUSP_LD_FLAGS = $(LD_FLAGS)
@@ -40,7 +40,15 @@ endif
 
 ifdef CONFIG_COMPRESS
 CC_FLAGS       += -DCONFIG_COMPRESS
+ifeq ($(CONFIG_COMPRESS), lzf)
+CC_FLAGS       += -DCONFIG_COMPRESS_ALGO_LZF
 SWSUSP_LD_FLAGS        += -llzf
+else ifeq ($(CONFIG_COMPRESS), lzo)
+CC_FLAGS       += -DCONFIG_COMPRESS_ALGO_LZO
+SWSUSP_LD_FLAGS        += -llzo2
+else
+$(error Invalid compression algorithm)
+endif
 endif
 
 ifdef CONFIG_ENCRYPT
@@ -93,7 +101,7 @@ md5.o encrypt.o: %.o : %.c %.h md5.h
        $(CC) $(CC_FLAGS) -DHAVE_INTTYPES_H -DHAVE_STDINT_H -c $< -o $@
 
 # Simple objects with header
-config.o vt.o bootsplash.o splash.o splashy_funcs.o vbetool/vbetool.o 
s2ram-ppc.o: %.o : %.c %.h
+config.o vt.o compress.o bootsplash.o splash.o splashy_funcs.o 
vbetool/vbetool.o s2ram-ppc.o: %.o : %.c %.h
        $(CC) $(CC_FLAGS) -c $< -o $@
 
 # Simple object without header
diff -urNp suspend.org/resume.c suspend/resume.c
--- suspend.org/resume.c        2007-05-13 20:53:13.000000000 +0300
+++ suspend/resume.c    2007-07-18 06:11:50.000000000 +0300
@@ -25,17 +25,13 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#ifdef CONFIG_COMPRESS
-#include <lzf.h>
-#else
-#define lzf_decompress(a, b, c, d)     0
-#endif
 
 #include "swsusp.h"
 #include "config.h"
 #include "md5.h"
 #include "splash.h"
 #include "loglevel.h"
+#include "compress.h"
 
 static char snapshot_dev_name[MAX_STR_LEN] = SNAPSHOT_DEVICE;
 static char resume_dev_name[MAX_STR_LEN] = RESUME_DEVICE;
@@ -276,13 +272,14 @@ static int restore(struct swap_map_handl
        void *buf = handle->page_buffer;
 
        block = (struct buf_block *)(handle->read_buffer + disp);
+#ifdef CONFIG_COMPRESS
        if (decompress) {
                unsigned short cnt = block->size;
 
                if (cnt == page_size) {
                        memcpy(buf, block->data, page_size);
                } else if (cnt < page_size) {
-                       cnt = lzf_decompress(block->data, cnt, buf, page_size);
+                       cnt = compress_decompress(block->data, cnt, buf, 
page_size);
                        if (cnt != page_size)
                                return -ENODATA;
                } else {
@@ -291,6 +288,7 @@ static int restore(struct swap_map_handl
                block->size += sizeof(short);
                return block->size;
        }
+#endif
        memcpy(buf, block, page_size);
        return page_size;
 }
@@ -589,6 +587,10 @@ static int read_image(int dev, int fd, s
                        printf("resume: Compressed image\n");
 #ifdef CONFIG_COMPRESS
                        decompress = 1;
+                       if (!compress_init()) {
+                               fprintf(stderr, "suspend: Could not initialize 
compress\n");
+                               error = -ENOPKG;
+                       }
 #else
                        fprintf(stderr,"resume: Compression not supported\n");
                        error = -EINVAL;
diff -urNp suspend.org/suspend.c suspend/suspend.c
--- suspend.org/suspend.c       2007-05-13 23:16:53.000000000 +0300
+++ suspend/suspend.c   2007-07-18 19:18:37.000000000 +0300
@@ -32,11 +32,6 @@
 #include <errno.h>
 #include <signal.h>
 #include <termios.h>
-#ifdef CONFIG_COMPRESS
-#include <lzf.h>
-#else
-#define lzf_compress(a, b, c, d)       0
-#endif
 
 #include "swsusp.h"
 #include "config.h"
@@ -44,6 +39,7 @@
 #include "splash.h"
 #include "vt.h"
 #include "loglevel.h"
+#include "compress.h"
 #ifdef CONFIG_BOTH
 #include "s2ram.h"
 #endif
@@ -64,7 +60,7 @@ static char compress;
 #else
 #define compress 0
 #endif
-static unsigned long compr_diff;
+static unsigned long compr_diff = 0;
 #ifdef CONFIG_ENCRYPT
 static char encrypt;
 static char use_RSA;
@@ -302,10 +298,11 @@ static int prepare(struct swap_map_handl
        void *buf = handle->page_buffer;
 
        block = (struct buf_block *)(handle->write_buffer + disp);
+#ifdef CONFIG_COMPRESS
        if (compress) {
                unsigned short cnt;
 
-               cnt = lzf_compress(buf, page_size,
+               cnt = compress_compress(buf, page_size,
                                block->data, page_size - sizeof(short));
                if (!cnt) {
                        memcpy(block->data, buf, page_size);
@@ -318,6 +315,7 @@ static int prepare(struct swap_map_handl
                cnt += sizeof(short);
                return cnt;
        }
+#endif
        memcpy(block, buf, page_size);
        return page_size;
 }
@@ -1324,6 +1322,14 @@ int main(int argc, char *argv[])
        buffer_size = BUFFER_PAGES * page_size;
 
        mem_size = 3 * page_size + buffer_size;
+#ifdef CONFIG_COMPRESS
+       if (compress) {
+               if (!compress_init()) {
+                       fprintf(stderr, "suspend: Could not initialize 
compress\n");
+                       return ENOPKG;
+               }
+       }
+#endif
 #ifdef CONFIG_ENCRYPT
        if (encrypt)
                mem_size += buffer_size;

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Suspend-devel mailing list
Suspend-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/suspend-devel

Reply via email to