2 * Bucket -> Object Converter
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
5 * (c) 2004, Martin Mares <mj@ucw.cz>
9 #include "lib/unaligned.h"
10 #include "lib/pools.h"
11 #include "lib/fastbuf.h"
12 #include "charset/unicode.h"
13 #include "lib/object.h"
14 #include "lib/bucket.h"
15 #include "lib/lizard.h"
16 #include "lib/buck2obj.h"
22 #define RET_ERR(num) ({ errno = num; return NULL; })
24 #define GBUF_TYPE byte
25 #define GBUF_PREFIX(x) bb_##x
31 struct lizard_buffer *lizard;
37 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
39 buf->lizard = lizard_alloc();
44 buck2obj_free(struct buck2obj_buf *buf)
46 lizard_free(buf->lizard);
52 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
54 if (can_overwrite >= 2)
64 obj_add_attr_ref(o, type, ptr);
77 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
78 memcpy(dup, ptr, len);
80 obj_add_attr_ref(o, type, dup);
88 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
90 struct odes *o = obj_new(pool);
92 if (buck_type == BUCKET_TYPE_PLAIN || buck_type == BUCKET_TYPE_V30)
94 if (!body_start) // header + body: ignore empty lines, read until EOF or NUL
96 obj_read_multi(body, o);
99 else // header only: end on EOF or the first empty line
101 sh_off_t start = btell(body);
103 *body_start = btell(body) - start;
106 else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
108 /* Read all the bucket into 1 buffer, 0-copy if possible. */
110 uns len = bdirect_read_prepare(body, &ptr);
113 || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
115 /* Copy if the original buffer is too small.
116 * If it is write-protected, copy it also if it is uncompressed. */
117 bb_grow(&buf->bb, buck_len);
118 len = bread(body, buf->bb.ptr, buck_len);
125 ptr = decode_attributes(ptr, end, o, 0); // header
128 *body_start = ptr - start;
130 bdirect_read_commit(body, ptr);
133 if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
139 byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
143 bdirect_read_commit(body, end);
148 ptr = decode_attributes(ptr, end, o, 2); // body
152 bdirect_read_commit_modified(body, ptr);