2 * Generating Objects from Buckets
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
5 * (c) 2004, Martin Mares <mj@ucw.cz>
9 #include "lib/unaligned.h"
10 #include "lib/mempool.h"
11 #include "lib/fastbuf.h"
12 #include "lib/unicode.h"
13 #include "lib/object.h"
14 #include "lib/bucket.h"
15 #include "lib/lizard.h"
17 #include "lib/ff-utf8.h"
23 #define RET_ERR(num) ({ errno = num; return -1; })
28 struct lizard_buffer *lizard;
34 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
36 buf->lizard = lizard_alloc();
41 buck2obj_free(struct buck2obj_buf *buf)
43 lizard_free(buf->lizard);
49 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
51 if (can_overwrite >= 2)
61 obj_add_attr_ref(o, type, ptr);
74 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
75 memcpy(dup, ptr, len);
77 obj_add_attr_ref(o, type, dup);
85 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)
87 if (buck_type <= BUCKET_TYPE_PLAIN)
89 if (body_start) // there is no header part
91 // ignore empty lines and read until the end of the bucket
92 sh_off_t end = btell(body) + buck_len;
93 byte buf[MAX_ATTR_SIZE];
94 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
96 obj_add_attr(o_hdr, buf[0], buf+1);
97 ASSERT(btell(body) == end);
99 else if (buck_type == BUCKET_TYPE_V30)
101 sh_off_t start = btell(body);
102 sh_off_t end = start + buck_len;
103 byte buf[MAX_ATTR_SIZE];
104 while (btell(body) < end && bgets(body, buf, sizeof(buf)) && buf[0])
105 obj_add_attr(o_hdr, buf[0], buf+1);
107 *body_start = btell(body) - start;
110 while (btell(body) < end && bgets(body, buf, sizeof(buf)))
112 obj_add_attr(o_body, buf[0], buf+1);
113 ASSERT(btell(body) == end);
116 else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
118 /* Avoid reading the whole bucket if only its header is needed. */
121 sh_off_t start = btell(body);
122 sh_off_t end = start + buck_len;
123 while (btell(body) < end)
125 uns len = bget_utf8(body);
128 byte *buf = mp_alloc_fast_noalign(o_hdr->pool, len);
129 bread(body, buf, len);
130 uns type = buf[--len];
132 obj_add_attr_ref(o_hdr, type, buf);
134 *body_start = btell(body) - start;
138 /* Read all the bucket into 1 buffer, 0-copy if possible. */
140 uns len = bdirect_read_prepare(body, &ptr);
143 || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
145 /* Copy if the original buffer is too small.
146 * If it is write-protected, copy it also if it is uncompressed. */
147 bb_grow(&buf->bb, buck_len);
148 len = bread(body, buf->bb.ptr, buck_len);
152 end = ptr + buck_len;
154 ptr = decode_attributes(ptr, end, o_hdr, 0); // header
155 if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
161 uns adler = GET_U32(ptr);
163 byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
166 if (adler32(new_ptr, len) != adler)
169 bdirect_read_commit(body, end);
174 ptr = decode_attributes(ptr, end, o_body, 2); // body
178 bdirect_read_commit_modified(body, ptr);
182 bskip(body, buck_len);
189 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
191 struct odes *o = obj_new(pool);
192 if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
199 obj_read(struct fastbuf *f, struct odes *o)
201 byte buf[MAX_ATTR_SIZE];
203 while (bgets(f, buf, sizeof(buf)))
207 obj_add_attr(o, buf[0], buf+1);