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')
67 while (ptr < end && *ptr != '\n')
69 attr->len = ptr++ - attr->val;
74 GET_UTF8_32(ptr, len);
81 attr->attr = ptr[len];
87 die("Incomplete attribute %c", attr->attr);
93 bget_attr(struct fastbuf *b, struct parsed_attr *attr)
96 if (get_attr_type < BUCKET_TYPE_V33)
101 if (get_attr_type == BUCKET_TYPE_PLAIN)
116 uns len = bdirect_read_prepare(b, &ptr);
119 while (ptr < end && *ptr != '\n')
123 bdirect_read_commit(b, ptr+1);
124 attr->len = ptr - attr->val;
130 while (c >= 0 && c != '\n')
132 bb_grow(&buf, len+1);
137 die("Incomplete attribute %c", attr->attr);
143 int len = bget_utf8_32(b);
154 int avail = bdirect_read_prepare(b, &ptr);
158 attr->attr = ptr[len-1];
159 bdirect_read_commit(b, ptr + len);
162 bb_grow(&buf, --len);
163 breadb(b, buf.ptr, len);
166 attr->attr = bgetc(b);
168 die("Incomplete attribute %c", attr->attr);
173 struct buck2obj_buf *
176 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
178 buf->lizard = lizard_alloc();
183 buck2obj_free(struct buck2obj_buf *buf)
185 lizard_free(buf->lizard);
191 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
193 if (can_overwrite >= 2)
197 GET_UTF8_32(ptr, len);
200 byte type = ptr[len];
203 obj_add_attr_ref(o, type, ptr);
211 GET_UTF8_32(ptr, len);
214 byte type = ptr[len];
216 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
217 memcpy(dup, ptr, len);
219 obj_add_attr_ref(o, type, dup);
227 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)
229 if (buck_type <= BUCKET_TYPE_PLAIN)
231 if (body_start) // there is no header part
233 // ignore empty lines and read until the end of the bucket
234 sh_off_t end = btell(body) + buck_len;
235 byte buf[MAX_ATTR_SIZE];
236 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
238 obj_add_attr(o_hdr, buf[0], buf+1);
239 ASSERT(btell(body) == end);
241 else if (buck_type == BUCKET_TYPE_V30)
243 sh_off_t start = btell(body);
244 sh_off_t end = start + buck_len;
245 byte buf[MAX_ATTR_SIZE];
246 while (btell(body) < end && bgets(body, buf, sizeof(buf)) && buf[0])
247 obj_add_attr(o_hdr, buf[0], buf+1);
249 *body_start = btell(body) - start;
252 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
254 obj_add_attr(o_body, buf[0], buf+1);
255 ASSERT(btell(body) == end);
258 else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
260 /* Avoid reading the whole bucket if only its header is needed. */
263 sh_off_t start = btell(body);
264 sh_off_t end = start + buck_len;
265 while (btell(body) < end)
267 uns len = bget_utf8_32(body);
270 byte *buf = mp_alloc_fast_noalign(o_hdr->pool, len);
271 bread(body, buf, len);
272 uns type = buf[--len];
274 obj_add_attr_ref(o_hdr, type, buf);
276 *body_start = btell(body) - start;
280 /* Read all the bucket into 1 buffer, 0-copy if possible. */
282 uns len = bdirect_read_prepare(body, &ptr);
285 || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
287 /* Copy if the original buffer is too small.
288 * If it is write-protected, copy it also if it is uncompressed. */
289 DBG("NO ZC: %d < %d, %d %08x", len, buck_len, body->can_overwrite_buffer, buck_type);
290 bb_grow(&buf->bb, buck_len);
291 len = bread(body, buf->bb.ptr, buck_len);
296 DBG("ZC (%d >= %d, %d %08x)", len, buck_len, body->can_overwrite_buffer, buck_type);
297 end = ptr + buck_len;
299 ptr = decode_attributes(ptr, end, o_hdr, 0); // header
300 if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
304 if (ptr == end) // truncated bucket
310 uns adler = GET_U32(ptr);
312 byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
315 if (adler32(new_ptr, len) != adler)
318 bdirect_read_commit(body, end);
323 ptr = decode_attributes(ptr, end, o_body, 2); // body
328 bdirect_read_commit_modified(body, ptr);
332 bskip(body, buck_len);
339 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
341 struct odes *o = obj_new(pool);
342 if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
349 obj_read(struct fastbuf *f, struct odes *o)
351 byte buf[MAX_ATTR_SIZE];
353 while (bgets(f, buf, sizeof(buf)))
357 obj_add_attr(o, buf[0], buf+1);