]> mj.ucw.cz Git - libucw.git/blobdiff - lib/buck2obj.c
obj_add_attr_ref() with an on-stack buffer is not advisable, better
[libucw.git] / lib / buck2obj.c
index e90ff875447eead2125b08e7d8624804df20a612..d75b48ab19e212b2d65e8996981db3b6dda85325 100644 (file)
@@ -14,6 +14,7 @@
 #include "lib/bucket.h"
 #include "lib/lizard.h"
 #include "lib/bbuf.h"
+#include "lib/ff-utf8.h"
 
 #include <stdlib.h>
 #include <errno.h>
@@ -108,12 +109,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(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);
@@ -128,17 +149,9 @@ buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fas
       ptr = buf->bb.ptr;
       copied = 1;
     }
-    end = ptr + len;
+    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)
@@ -165,7 +178,10 @@ buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fas
       bdirect_read_commit_modified(body, ptr);
   }
   else
-    RET_ERR(EINVAL);
+    {
+      bskip(body, buck_len);
+      RET_ERR(EINVAL);
+    }
   return 0;
 }