]> mj.ucw.cz Git - libucw.git/blobdiff - lib/buck2obj.c
Parsing of PLAIN type buckets also ends after buck_len bytes.
[libucw.git] / lib / buck2obj.c
index f268e11a0e5c7772aa9af3d984401c0560e94ef7..e90ff875447eead2125b08e7d8624804df20a612 100644 (file)
@@ -1,43 +1,38 @@
 /*
- *     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/mempool.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 NULL; })
-
-#define        GBUF_TYPE       byte
-#define        GBUF_PREFIX(x)  bb_##x
-#include "lib/gbuf.h"
+#define        RET_ERR(num)    ({ errno = num; return -1; })
 
 struct buck2obj_buf
 {
   bb_t bb;
   struct lizard_buffer *lizard;
-  struct mempool *mp;
 };
 
 struct buck2obj_buf *
-buck2obj_alloc(struct mempool *mp)
+buck2obj_alloc(void)
 {
   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
   bb_init(&buf->bb);
   buf->lizard = lizard_alloc();
-  buf->mp = mp;
   return buf;
 }
 
@@ -49,12 +44,6 @@ buck2obj_free(struct buck2obj_buf *buf)
   xfree(buf);
 }
 
-void
-buck2obj_flush(struct buck2obj_buf *buf)
-{
-  mp_flush(buf->mp);
-}
-
 static inline byte *
 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
 {
@@ -91,71 +80,115 @@ decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
   return ptr;
 }
 
-struct odes *
-obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
+int
+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)
 {
-  struct odes *o = obj_new(buf->mp);
-
-  if (buck_type < BUCKET_TYPE_V33)
+  if (buck_type <= BUCKET_TYPE_PLAIN)
   {
-    if (!body_start)                   // header + body: ignore empty lines, read until EOF
-    {
-      obj_read_multi(body, o);
-      bgetc(body);
-    }
-    else                               // header only: end on EOF or the first empty line
-    {
-      sh_off_t start = btell(body);
-      obj_read(body, o);
+    if (body_start)                    // there is no header part
+      *body_start = 0;
+    // ignore empty lines and read until the end of the bucket
+    sh_off_t end = btell(body) + buck_len;
+    byte buf[MAX_ATTR_SIZE];
+    while (btell(body) < end && bgets(body, buf, sizeof(buf)))
+      if (buf[0])
+       obj_add_attr(o_hdr, buf[0], buf+1);
+    ASSERT(btell(body) == end);
+  }
+  else if (buck_type == BUCKET_TYPE_V30)
+  {
+    sh_off_t start = btell(body);
+    sh_off_t end = start + buck_len;
+    byte buf[MAX_ATTR_SIZE];
+    while (btell(body) < end && bgets(body, buf, sizeof(buf)) && buf[0])
+      obj_add_attr(o_hdr, buf[0], buf+1);
+    if (body_start)
       *body_start = btell(body) - start;
+    else
+    {
+      while (btell(body) < end && bgets(body, buf, sizeof(buf)))
+       if (buf[0])
+         obj_add_attr(o_hdr, buf[0], buf+1);
+      ASSERT(btell(body) == end);
     }
   }
-  else
+  else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
   {
     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
-    int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, -1);
-    /* FIXME: This could be cached in buck2obj_buf */
     byte *ptr, *end;
     uns len = bdirect_read_prepare(body, &ptr);
+    uns copied = 0;
     if (len < buck_len
-    || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
+    || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
     {
       /* 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 = ptr + len;
 
     byte *start = ptr;
-    ptr = decode_attributes(ptr, end, o, 0);           // header
+    ptr = decode_attributes(ptr, end, o_hdr, 0);               // header
     if (body_start)
     {
       *body_start = ptr - start;
-      return o;
+      if (!copied)
+       bdirect_read_commit(body, ptr);
+      return 0;
     }
-    if (buck_type == BUCKET_TYPE_V33)
-      ;
-    else if (buck_type == BUCKET_TYPE_V33_LIZARD)      // decompression
+    if (buck_type == BUCKET_TYPE_V33_LIZARD)           // decompression
     {
+      if (ptr + 8 > end)
+       RET_ERR(EINVAL);
       len = GET_U32(ptr);
       ptr += 4;
+      uns adler = GET_U32(ptr);
+      ptr += 4;
       byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
       if (!new_ptr)
-       return NULL;
+       return -1;
+      if (adler32(new_ptr, len) != adler)
+       RET_ERR(EINVAL);
+      if (!copied)
+       bdirect_read_commit(body, end);
       ptr = new_ptr;
       end = ptr + len;
+      copied = 1;
     }
-    else                                               // unknown bucket type
-      RET_ERR(EINVAL);
-    ptr = decode_attributes(ptr, end, o, 2);           // body
-
+    ptr = decode_attributes(ptr, end, o_body, 2);      // body
     if (ptr != end)
       RET_ERR(EINVAL);
-    /* If the bucket fit into the fastbuf buffer, can_overwrite == 2, and
-     * buck_type == BUCKET_TYPE_V33, then bflush(body) is needed before
-     * anything else than sequential read.  */
+    if (!copied)
+      bdirect_read_commit_modified(body, ptr);
   }
-  return o;
+  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;
 }