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;
33 static uns get_attr_type;
36 get_attr_set_type(uns type)
38 if (type < BUCKET_TYPE_PLAIN || type > BUCKET_TYPE_V33_LIZARD)
39 die("Unknown buckettype %x", type);
44 get_attr(byte **pos, byte *end, struct parsed_attr *attr)
49 if (get_attr_type < BUCKET_TYPE_V33)
51 if (get_attr_type == BUCKET_TYPE_PLAIN)
53 while (ptr < end && *ptr == '\n')
59 else if (*ptr == '\n')
66 while (ptr < end && *ptr != '\n')
68 attr->len = ptr++ - attr->val;
73 GET_UTF8_32(ptr, len);
79 attr->attr = ptr[len];
85 die("Incomplete attribute %c", attr->attr);
91 bget_attr(struct fastbuf *b, struct parsed_attr *attr)
94 if (get_attr_type < BUCKET_TYPE_V33)
99 if (get_attr_type == BUCKET_TYPE_PLAIN)
111 uns len = bdirect_read_prepare(b, &ptr);
114 while (ptr < end && *ptr != '\n')
118 bdirect_read_commit(b, ptr+1);
119 attr->len = ptr - attr->val;
125 while (c >= 0 && c != '\n')
127 bb_grow(&buf, len+1);
132 die("Incomplete attribute %c", attr->attr);
138 int len = bget_utf8_32(b);
140 return len < 0 ? -1 : 0;
144 int avail = bdirect_read_prepare(b, &ptr);
148 attr->attr = ptr[len-1];
149 bdirect_read_commit(b, ptr + len);
152 bb_grow(&buf, --len);
153 breadb(b, buf.ptr, len);
156 attr->attr = bgetc(b);
158 die("Incomplete attribute %c", attr->attr);
163 struct buck2obj_buf *
166 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
168 buf->lizard = lizard_alloc();
173 buck2obj_free(struct buck2obj_buf *buf)
175 lizard_free(buf->lizard);
181 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
183 if (can_overwrite >= 2)
187 GET_UTF8_32(ptr, len);
190 byte type = ptr[len];
193 obj_add_attr_ref(o, type, ptr);
201 GET_UTF8_32(ptr, len);
204 byte type = ptr[len];
206 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
207 memcpy(dup, ptr, len);
209 obj_add_attr_ref(o, type, dup);
217 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)
219 if (buck_type <= BUCKET_TYPE_PLAIN)
221 if (body_start) // there is no header part
223 // ignore empty lines and read until the end of the bucket
224 sh_off_t end = btell(body) + buck_len;
225 byte buf[MAX_ATTR_SIZE];
226 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
228 obj_add_attr(o_hdr, buf[0], buf+1);
229 ASSERT(btell(body) == end);
231 else if (buck_type == BUCKET_TYPE_V30)
233 sh_off_t start = btell(body);
234 sh_off_t end = start + buck_len;
235 byte buf[MAX_ATTR_SIZE];
236 while (btell(body) < end && bgets(body, buf, sizeof(buf)) && buf[0])
237 obj_add_attr(o_hdr, buf[0], buf+1);
239 *body_start = btell(body) - start;
242 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
244 obj_add_attr(o_body, buf[0], buf+1);
245 ASSERT(btell(body) == end);
248 else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
250 /* Avoid reading the whole bucket if only its header is needed. */
253 sh_off_t start = btell(body);
254 sh_off_t end = start + buck_len;
255 while (btell(body) < end)
257 uns len = bget_utf8_32(body);
260 byte *buf = mp_alloc_fast_noalign(o_hdr->pool, len);
261 bread(body, buf, len);
262 uns type = buf[--len];
264 obj_add_attr_ref(o_hdr, type, buf);
266 *body_start = btell(body) - start;
270 /* Read all the bucket into 1 buffer, 0-copy if possible. */
272 uns len = bdirect_read_prepare(body, &ptr);
275 || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
277 /* Copy if the original buffer is too small.
278 * If it is write-protected, copy it also if it is uncompressed. */
279 DBG("NO ZC: %d < %d, %d %08x", len, buck_len, body->can_overwrite_buffer, buck_type);
280 bb_grow(&buf->bb, buck_len);
281 len = bread(body, buf->bb.ptr, buck_len);
286 DBG("ZC (%d >= %d, %d %08x)", len, buck_len, body->can_overwrite_buffer, buck_type);
287 end = ptr + buck_len;
289 ptr = decode_attributes(ptr, end, o_hdr, 0); // header
290 if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
294 if (ptr == end) // truncated bucket
300 uns adler = GET_U32(ptr);
302 byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
305 if (adler32(new_ptr, len) != adler)
308 bdirect_read_commit(body, end);
313 ptr = decode_attributes(ptr, end, o_body, 2); // body
318 bdirect_read_commit_modified(body, ptr);
322 bskip(body, buck_len);
329 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
331 struct odes *o = obj_new(pool);
332 if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
339 obj_read(struct fastbuf *f, struct odes *o)
341 byte buf[MAX_ATTR_SIZE];
343 while (bgets(f, buf, sizeof(buf)))
347 obj_add_attr(o, buf[0], buf+1);