]> 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 c50c80bd5bd3499b587418b572f19003ec421c14..7d651dfe6b65f6941f375a4c32ef53f88033c06d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *     Bucket -> Object Converter
+ *     Generating Objects from Buckets
  *
  *     (c) 2004, Robert Spalek <robert@ucw.cz>
  *     (c) 2004, Martin Mares <mj@ucw.cz>
@@ -9,21 +9,17 @@
 #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 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
 {
@@ -84,24 +80,26 @@ decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
   return ptr;
 }
 
-struct odes *
-obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, 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(pool);
-
-  if (buck_type == BUCKET_TYPE_PLAIN || buck_type == BUCKET_TYPE_V30)
+  if (buck_type == BUCKET_TYPE_PLAIN)
+  {
+    if (body_start)
+      *body_start = 0;
+    obj_read_multi(body, o_hdr);       // ignore empty lines, read until EOF or NUL
+  }
+  else if (buck_type == BUCKET_TYPE_V30)
   {
-    if (!body_start)                   // header + body: ignore empty lines, read until EOF or NUL
+    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_multi(body, o);
+      obj_read(body, o_body);
       bgetc(body);
     }
-    else                               // header only: end on EOF or the first empty line
-    {
-      sh_off_t start = btell(body);
-      obj_read(body, o);
-      *body_start = btell(body) - start;
-    }
   }
   else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
   {
@@ -122,29 +120,30 @@ obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, u
     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;
       if (!copied)
        bdirect_read_commit(body, ptr);
-      return o;
+      return 0;
     }
     if (buck_type == BUCKET_TYPE_V33_LIZARD)           // decompression
     {
-      /* FIXME: Add checks for len<4 and other format violations */
+      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 NULL;
+       return -1;
       if (!copied)
        bdirect_read_commit(body, end);
       ptr = new_ptr;
       end = ptr + len;
       copied = 1;
     }
-    ptr = decode_attributes(ptr, end, o, 2);           // body
+    ptr = decode_attributes(ptr, end, o_body, 2);      // body
     if (ptr != end)
       RET_ERR(EINVAL);
     if (!copied)
@@ -152,5 +151,39 @@ obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, u
   }
   else
     RET_ERR(EINVAL);
-  return o;
+  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);
 }