]> mj.ucw.cz Git - libucw.git/blobdiff - lib/buck2obj.c
Moved object reading and writing functions where they belong.
[libucw.git] / lib / buck2obj.c
index 235c17dbaf325a6ebe6974a1e418c5f273e310c0..7d651dfe6b65f6941f375a4c32ef53f88033c06d 100644 (file)
 /*
- *     Bucket -> Object converter
+ *     Generating Objects from Buckets
  *
  *     (c) 2004, Robert Spalek <robert@ucw.cz>
+ *     (c) 2004, Martin Mares <mj@ucw.cz>
  */
 
 #include "lib/lib.h"
+#include "lib/unaligned.h"
+#include "lib/pools.h"
 #include "lib/fastbuf.h"
-#include "charset/unicode.h"
+#include "lib/unicode.h"
 #include "lib/object.h"
 #include "lib/bucket.h"
 #include "lib/lizard.h"
-#include "lib/buck2obj.h"
+#include "lib/bbuf.h"
 
+#include <stdlib.h>
 #include <errno.h>
+#include <unistd.h>
+
+#define        RET_ERR(num)    ({ errno = num; return -1; })
+
+struct buck2obj_buf
+{
+  bb_t bb;
+  struct lizard_buffer *lizard;
+};
+
+struct buck2obj_buf *
+buck2obj_alloc(void)
+{
+  struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
+  bb_init(&buf->bb);
+  buf->lizard = lizard_alloc();
+  return buf;
+}
+
+void
+buck2obj_free(struct buck2obj_buf *buf)
+{
+  lizard_free(buf->lizard);
+  bb_done(&buf->bb);
+  xfree(buf);
+}
 
 static inline byte *
-decode_attributes(byte *start, byte *end, struct odes *o)
+decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
 {
-  byte *p = start;
-  while (p < end)
-  {
-    uns len;
-    GET_UTF8(p, len);
-    if (!len)
-      break;
-    byte type = p[len];
-    p[len] = 0;
-    obj_add_attr(o, type, p);
-  }
-  return p;
+  if (can_overwrite >= 2)
+    while (ptr < end)
+    {
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
+      ptr[len] = 0;
+      obj_add_attr_ref(o, type, ptr);
+
+      ptr += len + 1;
+    }
+  else
+    while (ptr < end)
+    {
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
+      byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
+      memcpy(dup, ptr, len);
+      dup[len] = 0;
+      obj_add_attr_ref(o, type, dup);
+
+      ptr += len + 1;
+    }
+  return ptr;
 }
 
 int
-extract_odes(struct obuck_header *hdr, struct fastbuf *body, struct odes *o, byte *buf, uns buf_len, struct lizard_buffer *lizard_buf)
+buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, struct odes *o_hdr, uns *body_start, struct odes *o_body)
 {
-  if (hdr->type < BUCKET_TYPE_V30C)
+  if (buck_type == BUCKET_TYPE_PLAIN)
   {
-    oa_allocate = 1;
-    obj_read_multi(body, o);
+    if (body_start)
+      *body_start = 0;
+    obj_read_multi(body, o_hdr);       // ignore empty lines, read until EOF or NUL
   }
-  else
+  else if (buck_type == BUCKET_TYPE_V30)
+  {
+    sh_off_t start = btell(body);
+    obj_read(body, o_hdr);             // end on EOF or the first empty line
+    if (body_start)
+      *body_start = btell(body) - start;
+    else
+    {
+      obj_read(body, o_body);
+      bgetc(body);
+    }
+  }
+  else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
   {
-    oa_allocate = 0;
     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
-    byte *start, *end;
-    uns len = bdirect_read_prepare(body, &start);
-    if (len < hdr->length)
+    byte *ptr, *end;
+    uns len = bdirect_read_prepare(body, &ptr);
+    uns copied = 0;
+    if (len < buck_len
+    || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
     {
-      if (hdr->length > buf_len)
-      {
-       errno = EFBIG;
-       return -1;
-      }
-      len = bread(body, buf, hdr->length);
-      start = buf;
+      /* Copy if the original buffer is too small.
+       * If it is write-protected, copy it also if it is uncompressed.  */
+      bb_grow(&buf->bb, buck_len);
+      len = bread(body, buf->bb.ptr, buck_len);
+      ptr = buf->bb.ptr;
+      copied = 1;
     }
-    end = start + len;
+    end = ptr + len;
 
-    /* Decode the header, 0-copy.  */
-    byte *p = decode_attributes(start, end, o);
-
-    /* Decompress the body.  */
-    GET_UTF8(p, len);
-    int res = lizard_decompress_safe(p, lizard_buf, len);
-    if (res < 0)
-      return res;
-    if (res != (int) len)
+    byte *start = ptr;
+    ptr = decode_attributes(ptr, end, o_hdr, 0);               // header
+    if (body_start)
     {
-      errno = EINVAL;
-      return -1;
+      *body_start = ptr - start;
+      if (!copied)
+       bdirect_read_commit(body, ptr);
+      return 0;
     }
-
-    /* Decode the body, 0-copy.  */
-    start = lizard_buf->ptr;
-    end = start + len;
-    p = decode_attributes(start, end, o);
-    if (p != end)
+    if (buck_type == BUCKET_TYPE_V33_LIZARD)           // decompression
     {
-      errno = EINVAL;
-      return -1;
+      if (ptr + 4 > end)
+       RET_ERR(EINVAL);
+      len = GET_U32(ptr);
+      ptr += 4;
+      byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
+      if (!new_ptr)
+       return -1;
+      if (!copied)
+       bdirect_read_commit(body, end);
+      ptr = new_ptr;
+      end = ptr + len;
+      copied = 1;
     }
+    ptr = decode_attributes(ptr, end, o_body, 2);      // body
+    if (ptr != end)
+      RET_ERR(EINVAL);
+    if (!copied)
+      bdirect_read_commit_modified(body, ptr);
   }
+  else
+    RET_ERR(EINVAL);
+  return 0;
+}
+
+struct odes *
+obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
+{
+  struct odes *o = obj_new(pool);
+  if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
+    return NULL;
+  else
+    return o;
+}
+
+int
+obj_read(struct fastbuf *f, struct odes *o)
+{
+  byte buf[MAX_ATTR_SIZE];
+
+  while (bgets(f, buf, sizeof(buf)))
+    {
+      if (!buf[0])
+       return 1;
+      obj_add_attr(o, buf[0], buf+1);
+    }
   return 0;
 }
+
+void
+obj_read_multi(struct fastbuf *f, struct odes *o)
+{
+  /* Read a multi-part object ending with either EOF or a NUL character */
+  byte buf[MAX_ATTR_SIZE];
+  while (bpeekc(f) > 0 && bgets(f, buf, sizeof(buf)))
+    if (buf[0])
+      obj_add_attr(o, buf[0], buf+1);
+}