]> mj.ucw.cz Git - libucw.git/blobdiff - lib/buck2obj.c
enlarge MAX_ATTR_SIZE
[libucw.git] / lib / buck2obj.c
index e90ff875447eead2125b08e7d8624804df20a612..dacdeba4fd6276350c426007c58ced8ae30c454e 100644 (file)
@@ -5,6 +5,8 @@
  *     (c) 2004, Martin Mares <mj@ucw.cz>
  */
 
+#undef LOCAL_DEBUG
+
 #include "lib/lib.h"
 #include "lib/unaligned.h"
 #include "lib/mempool.h"
@@ -14,6 +16,7 @@
 #include "lib/bucket.h"
 #include "lib/lizard.h"
 #include "lib/bbuf.h"
+#include "lib/ff-utf8.h"
 
 #include <stdlib.h>
 #include <errno.h>
@@ -27,6 +30,146 @@ struct buck2obj_buf
   struct lizard_buffer *lizard;
 };
 
+static uns get_attr_type;
+
+void
+get_attr_set_type(uns type)
+{
+  if (type < BUCKET_TYPE_PLAIN || type > BUCKET_TYPE_V33_LIZARD)
+    die("Unknown buckettype %x", type);
+  get_attr_type = type;
+}
+
+int
+get_attr(byte **pos, byte *end, struct parsed_attr *attr)
+{
+  byte *ptr = *pos;
+  if (ptr >= end)
+    return -1;
+  if (get_attr_type < BUCKET_TYPE_V33)
+  {
+    if (get_attr_type == BUCKET_TYPE_PLAIN)
+    {
+      while (ptr < end && *ptr == '\n')
+       ptr++;
+      *pos = ptr;
+      if (ptr >= end)
+       return -1;
+    }
+    else if (*ptr == '\n')
+    {
+      *pos = ++ptr;
+      attr->attr = 0;
+      return 0;
+    }
+    attr->attr = *ptr++;
+    attr->val = ptr;
+    while (ptr < end && *ptr != '\n')
+      ptr++;
+    attr->len = ptr++ - attr->val;
+  }
+  else
+  {
+    uns len;
+    GET_UTF8_32(ptr, len);
+    if (!len--)
+    {
+      *pos = ptr;
+      attr->attr = 0;
+      return 0;
+    }
+    attr->attr = ptr[len];
+    attr->val = ptr;
+    attr->len = len;
+    ptr += len+1;
+  }
+  if (ptr > end)
+    die("Incomplete attribute %c", attr->attr);
+  *pos = ptr;
+  return attr->attr;
+}
+
+int
+bget_attr(struct fastbuf *b, struct parsed_attr *attr)
+{
+  static bb_t buf;
+  if (get_attr_type < BUCKET_TYPE_V33)
+  {
+    int c = bgetc(b);
+    if (c < 0)
+      return -1;
+    if (get_attr_type == BUCKET_TYPE_PLAIN)
+    {
+      while (c == '\n')
+       c = bgetc(b);
+      if (c < 0)
+       return -1;
+    }
+    else if (c == '\n')
+    {
+      attr->attr = 0;
+      return 0;
+    }
+    attr->attr = c;
+
+    byte *ptr, *end;
+    uns len = bdirect_read_prepare(b, &ptr);
+    end = ptr + len;
+    attr->val = ptr;
+    while (ptr < end && *ptr != '\n')
+      ptr++;
+    if (ptr < end)
+    {
+      bdirect_read_commit(b, ptr+1);
+      attr->len = ptr - attr->val;
+      return attr->attr;
+    }
+
+    len = 0;
+    c = bgetc(b);
+    while (c >= 0 && c != '\n')
+    {
+      bb_grow(&buf, len+1);
+      buf.ptr[len++] = c;
+      c = bgetc(b);
+    }
+    if (c < 0)
+      die("Incomplete attribute %c", attr->attr);
+    attr->val = buf.ptr;
+    attr->len = len;
+  }
+  else
+  {
+    int len = bget_utf8_32(b);
+    if (len < 0)
+      return -1;
+    if (!len)
+    {
+      attr->attr = 0;
+      return 0;
+    }
+    attr->len = len-1;
+
+    byte *ptr;
+    int avail = bdirect_read_prepare(b, &ptr);
+    if (avail >= len)
+    {
+      attr->val = ptr;
+      attr->attr = ptr[len-1];
+      bdirect_read_commit(b, ptr + len);
+      return attr->attr;
+    }
+    bb_grow(&buf, --len);
+    breadb(b, buf.ptr, len);
+    attr->val = buf.ptr;
+    attr->len = len;
+    attr->attr = bgetc(b);
+    if (attr->attr < 0)
+      die("Incomplete attribute %c", attr->attr);
+  }
+  return attr->attr;
+}
+
 struct buck2obj_buf *
 buck2obj_alloc(void)
 {
@@ -51,7 +194,7 @@ decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
     while (ptr < end)
     {
       uns len;
-      GET_UTF8(ptr, len);
+      GET_UTF8_32(ptr, len);
       if (!len--)
        break;
       byte type = ptr[len];
@@ -65,7 +208,7 @@ decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
     while (ptr < end)
     {
       uns len;
-      GET_UTF8(ptr, len);
+      GET_UTF8_32(ptr, len);
       if (!len--)
        break;
       byte type = ptr[len];
@@ -108,12 +251,32 @@ buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fas
     {
       while (btell(body) < end && bgets(body, buf, sizeof(buf)))
        if (buf[0])
-         obj_add_attr(o_hdr, buf[0], buf+1);
+         obj_add_attr(o_body, buf[0], buf+1);
       ASSERT(btell(body) == end);
     }
   }
   else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
   {
+    /* Avoid reading the whole bucket if only its header is needed.  */
+    if (body_start)
+    {
+      sh_off_t start = btell(body);
+      sh_off_t end = start + buck_len;
+      while (btell(body) < end)
+      {
+       uns len = bget_utf8_32(body);
+       if (!len)
+         break;
+       byte *buf = mp_alloc_fast_noalign(o_hdr->pool, len);
+       bread(body, buf, len);
+       uns type = buf[--len];
+       buf[len] = 0;
+       obj_add_attr_ref(o_hdr, type, buf);
+      }
+      *body_start = btell(body) - start;
+      return 0;
+    }
+
     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
     byte *ptr, *end;
     uns len = bdirect_read_prepare(body, &ptr);
@@ -123,26 +286,25 @@ buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fas
     {
       /* Copy if the original buffer is too small.
        * If it is write-protected, copy it also if it is uncompressed.  */
+      DBG("NO ZC: %d < %d, %d %08x", len, buck_len, body->can_overwrite_buffer, buck_type);
       bb_grow(&buf->bb, buck_len);
       len = bread(body, buf->bb.ptr, buck_len);
       ptr = buf->bb.ptr;
       copied = 1;
     }
-    end = ptr + len;
+    else
+      DBG("ZC (%d >= %d, %d %08x)", len, buck_len, body->can_overwrite_buffer, buck_type);
+    end = ptr + buck_len;
 
-    byte *start = ptr;
     ptr = decode_attributes(ptr, end, o_hdr, 0);               // header
-    if (body_start)
-    {
-      *body_start = ptr - start;
-      if (!copied)
-       bdirect_read_commit(body, ptr);
-      return 0;
-    }
     if (buck_type == BUCKET_TYPE_V33_LIZARD)           // decompression
     {
       if (ptr + 8 > end)
-       RET_ERR(EINVAL);
+       {
+         if (ptr == end)                               // truncated bucket
+           goto commit;
+         RET_ERR(EINVAL);
+       }
       len = GET_U32(ptr);
       ptr += 4;
       uns adler = GET_U32(ptr);
@@ -161,11 +323,15 @@ buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fas
     ptr = decode_attributes(ptr, end, o_body, 2);      // body
     if (ptr != end)
       RET_ERR(EINVAL);
+  commit:
     if (!copied)
       bdirect_read_commit_modified(body, ptr);
   }
   else
-    RET_ERR(EINVAL);
+    {
+      bskip(body, buck_len);
+      RET_ERR(EINVAL);
+    }
   return 0;
 }