2 * Generating Objects from Buckets
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
5 * (c) 2004, Martin Mares <mj@ucw.cz>
11 #include "lib/unaligned.h"
12 #include "lib/mempool.h"
13 #include "lib/fastbuf.h"
14 #include "lib/unicode.h"
15 #include "lib/object.h"
16 #include "lib/bucket.h"
17 #include "lib/lizard.h"
19 #include "lib/ff-utf8.h"
25 #define RET_ERR(num) ({ errno = num; return -1; })
30 struct lizard_buffer *lizard;
36 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
38 buf->lizard = lizard_alloc();
43 buck2obj_free(struct buck2obj_buf *buf)
45 lizard_free(buf->lizard);
51 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
53 if (can_overwrite >= 2)
63 obj_add_attr_ref(o, type, ptr);
76 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
77 memcpy(dup, ptr, len);
79 obj_add_attr_ref(o, type, dup);
87 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)
89 if (buck_type <= BUCKET_TYPE_PLAIN)
91 if (body_start) // there is no header part
93 // ignore empty lines and read until the end of the bucket
94 sh_off_t end = btell(body) + buck_len;
95 byte buf[MAX_ATTR_SIZE];
96 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
98 obj_add_attr(o_hdr, buf[0], buf+1);
99 ASSERT(btell(body) == end);
101 else if (buck_type == BUCKET_TYPE_V30)
103 sh_off_t start = btell(body);
104 sh_off_t end = start + buck_len;
105 byte buf[MAX_ATTR_SIZE];
106 while (btell(body) < end && bgets(body, buf, sizeof(buf)) && buf[0])
107 obj_add_attr(o_hdr, buf[0], buf+1);
109 *body_start = btell(body) - start;
112 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
114 obj_add_attr(o_body, buf[0], buf+1);
115 ASSERT(btell(body) == end);
118 else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
120 /* Avoid reading the whole bucket if only its header is needed. */
123 sh_off_t start = btell(body);
124 sh_off_t end = start + buck_len;
125 while (btell(body) < end)
127 uns len = bget_utf8(body);
130 byte *buf = mp_alloc_fast_noalign(o_hdr->pool, len);
131 bread(body, buf, len);
132 uns type = buf[--len];
134 obj_add_attr_ref(o_hdr, type, buf);
136 *body_start = btell(body) - start;
140 /* Read all the bucket into 1 buffer, 0-copy if possible. */
142 uns len = bdirect_read_prepare(body, &ptr);
145 || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
147 /* Copy if the original buffer is too small.
148 * If it is write-protected, copy it also if it is uncompressed. */
149 DBG("NO ZC: %d < %d, %d %08x", len, buck_len, body->can_overwrite_buffer, buck_type);
150 bb_grow(&buf->bb, buck_len);
151 len = bread(body, buf->bb.ptr, buck_len);
156 DBG("ZC (%d >= %d, %d %08x)", len, buck_len, body->can_overwrite_buffer, buck_type);
157 end = ptr + buck_len;
159 ptr = decode_attributes(ptr, end, o_hdr, 0); // header
160 if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
164 if (ptr == end) // truncated bucket
170 uns adler = GET_U32(ptr);
172 byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
175 if (adler32(new_ptr, len) != adler)
178 bdirect_read_commit(body, end);
183 ptr = decode_attributes(ptr, end, o_body, 2); // body
188 bdirect_read_commit_modified(body, ptr);
192 bskip(body, buck_len);
199 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
201 struct odes *o = obj_new(pool);
202 if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
209 obj_read(struct fastbuf *f, struct odes *o)
211 byte buf[MAX_ATTR_SIZE];
213 while (bgets(f, buf, sizeof(buf)))
217 obj_add_attr(o, buf[0], buf+1);