From d2e57005add34e6ed878fb9051b3a05e4375f0d1 Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Tue, 15 Jun 2004 09:17:36 +0000 Subject: [PATCH] - lizard.c split into lizard.c and lizard-safe.c - added LGPL header --- lib/lizard.c | 52 +++------------------------------------------------- lib/lizard.h | 6 ++++-- 2 files changed, 7 insertions(+), 51 deletions(-) diff --git a/lib/lizard.c b/lib/lizard.c index ad1a7897..d707db3e 100644 --- a/lib/lizard.c +++ b/lib/lizard.c @@ -3,6 +3,9 @@ * * (c) 2004, Robert Spalek * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + * * The file format is based on LZO1X and * the compression method is based on zlib. */ @@ -11,11 +14,6 @@ #include "lib/lizard.h" #include -#include -#include -#include -#include -#include typedef u16 hash_ptr_t; struct hash_record { @@ -399,50 +397,6 @@ perform_copy_command: return out - out_start; } -struct lizard_buffer * -lizard_alloc(uns max_len) -{ - int fd = open("/dev/zero", O_RDWR); - if (fd < 0) - die("open(/dev/zero): %m"); - struct lizard_buffer *buf = xmalloc(sizeof(struct lizard_buffer)); - buf->len = (max_len + 2*PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE; - buf->ptr = mmap(NULL, buf->len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (buf->ptr == MAP_FAILED) - die("mmap(/dev/zero): %m"); - return buf; -} - -void -lizard_free(struct lizard_buffer *buf) -{ - munmap(buf->ptr, buf->len); - xfree(buf); -} - -static void -sigsegv_handler(int UNUSED whatsit) -{ - die("SIGSEGV caught when decompressing."); -} - -int -lizard_decompress_safe(byte *in, struct lizard_buffer *buf, uns expected_length) - /* Decompresses into buf->ptr and returns the length of the uncompressed - * file. Negative return values signalise errors. If something goes wrong - * and buffer would overflow, SIGSEGV is raised. */ -{ - uns lock_offset = (expected_length + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE; - if (lock_offset + PAGE_SIZE > buf->len) - return -1; - mprotect(buf->ptr, lock_offset, PROT_READ | PROT_WRITE); - mprotect(buf->ptr + lock_offset, PAGE_SIZE, PROT_READ); - sighandler_t old_handler = signal(SIGSEGV, sigsegv_handler); - int len = lizard_decompress(in, buf->ptr); - signal(SIGSEGV, old_handler); - return len; -} - /* Description of the LZO1X format : diff --git a/lib/lizard.h b/lib/lizard.h index d0f8ba09..52ddf8b3 100644 --- a/lib/lizard.h +++ b/lib/lizard.h @@ -3,8 +3,8 @@ * * (c) 2004, Robert Spalek * - * The file format is based on LZO1X and - * the compression method is based on zlib. + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ #define LIZARD_NEEDS_CHARS 8 @@ -23,9 +23,11 @@ * total length is 2(header) + 2(link) + 19(string) = 23. */ +/* lizard.c */ int lizard_compress(byte *in, uns in_len, byte *out); int lizard_decompress(byte *in, byte *out); +/* lizard-safe.c */ struct lizard_buffer { uns len; void *ptr; -- 2.39.2