]> mj.ucw.cz Git - libucw.git/blobdiff - lib/lizard-safe.c
Added bit_array_assign(), replaced BIT_ARRAY_ALLOC by functions.
[libucw.git] / lib / lizard-safe.c
index 4e07036a67a5a1eed30f3bb27b55a2c1efd62778..4ca3d9befec41ed9ef001ca16cbc3d28aeac56e4 100644 (file)
@@ -10,7 +10,6 @@
 #include "lib/lib.h"
 #include "lib/lizard.h"
 
-#include <stdlib.h>
 #include <sys/mman.h>
 #include <sys/user.h>
 #include <fcntl.h>
 #include <setjmp.h>
 #include <errno.h>
 
-static void
-lizard_alloc_internal(struct lizard_buffer *buf, uns max_len)
-{
-  buf->len = ALIGN(max_len + 3, PAGE_SIZE);            // +3 due to the unaligned access
-  buf->start = mmap(NULL, buf->len + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
-  if (buf->start == MAP_FAILED)
-    die("mmap(anonymous): %m");
-  if (mprotect(buf->start + buf->len, PAGE_SIZE, PROT_NONE) < 0)
-    die("mprotect: %m");
-}
+struct lizard_buffer {
+  uns len;
+  void *ptr;
+  struct sigaction old_sigsegv_handler;
+};
 
 struct lizard_buffer *
-lizard_alloc(uns max_len)
+lizard_alloc(void)
 {
   struct lizard_buffer *buf = xmalloc(sizeof(struct lizard_buffer));
-  lizard_alloc_internal(buf, max_len);
-  buf->old_sigsegv_handler = xmalloc(sizeof(struct sigaction));
-  handle_signal(SIGSEGV, buf->old_sigsegv_handler);
+  buf->len = 0;
+  buf->ptr = NULL;
+  handle_signal(SIGSEGV, &buf->old_sigsegv_handler);
   return buf;
 }
 
 void
 lizard_free(struct lizard_buffer *buf)
 {
-  munmap(buf->start, buf->len + PAGE_SIZE);
-  unhandle_signal(SIGSEGV, buf->old_sigsegv_handler);
-  xfree(buf->old_sigsegv_handler);
+  if (buf->ptr)
+    munmap(buf->ptr, buf->len + PAGE_SIZE);
+  unhandle_signal(SIGSEGV, &buf->old_sigsegv_handler);
   xfree(buf);
 }
 
-void
+static void
 lizard_realloc(struct lizard_buffer *buf, uns max_len)
+  /* max_len needs to be aligned to PAGE_SIZE */
 {
-  munmap(buf->start, buf->len + PAGE_SIZE);
-  lizard_alloc_internal(buf, max_len);
+  if (max_len <= buf->len)
+    return;
+  if (max_len < 2*buf->len)                            // to ensure logarithmic cost
+    max_len = 2*buf->len;
+
+  if (buf->ptr)
+    munmap(buf->ptr, buf->len + PAGE_SIZE);
+  buf->len = max_len;
+  buf->ptr = mmap(NULL, buf->len + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  if (buf->ptr == MAP_FAILED)
+    die("mmap(anonymous): %m");
+  if (mprotect(buf->ptr + buf->len, PAGE_SIZE, PROT_NONE) < 0)
+    die("mprotect: %m");
 }
 
 static jmp_buf safe_decompress_jump;
@@ -64,33 +70,35 @@ sigsegv_handler(int signal UNUSED)
   return 1;
 }
 
-int
+byte *
 lizard_decompress_safe(byte *in, struct lizard_buffer *buf, uns expected_length)
-  /* Decompresses into buf->ptr and returns the length of the uncompressed
-   * file.  If an error has occured, -1 is returned and errno is set.  SIGSEGV
-   * is caught in the case of buffer-overflow.  The function is not re-entrant
-   * because of a static longjmp handler.  */
+  /* Decompresses in into buf, sets *ptr to the data, and returns the
+   * uncompressed length.  If an error has occured, -1 is returned and errno is
+   * set.  The buffer buf is automatically reallocated.  SIGSEGV is caught in
+   * case of buffer-overflow.  The function is not re-entrant because of a
+   * static longjmp handler.  */
 {
   uns lock_offset = ALIGN(expected_length + 3, PAGE_SIZE);     // +3 due to the unaligned access
   if (lock_offset > buf->len)
-  {
-    errno = EFBIG;
-    return -1;
-  }
+    lizard_realloc(buf, lock_offset);
   volatile sh_sighandler_t old_handler = signal_handler[SIGSEGV];
   signal_handler[SIGSEGV] = sigsegv_handler;
-  int len;
+  byte *ptr;
   if (!setjmp(safe_decompress_jump))
   {
-    buf->ptr = buf->start + buf->len - lock_offset;
-    len = lizard_decompress(in, buf->ptr);
+    ptr = buf->ptr + buf->len - lock_offset;
+    int len = lizard_decompress(in, ptr);
+    if (len != (int) expected_length)
+    {
+      ptr = NULL;
+      errno = EINVAL;
+    }
   }
   else
   {
-    buf->ptr = NULL;
-    len = -1;
+    ptr = NULL;
     errno = EFAULT;
   }
   signal_handler[SIGSEGV] = old_handler;
-  return len;
+  return ptr;
 }