2 * Bucket -> Object converter
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
8 #include "lib/unaligned.h"
10 #include "lib/fastbuf.h"
11 #include "charset/unicode.h"
12 #include "lib/object.h"
13 #include "lib/bucket.h"
14 #include "lib/lizard.h"
15 #include "lib/buck2obj.h"
21 #define MAX_HEADER_SIZE 1024 // extra space for the header not counted in MaxObjSize
22 #define RET_ERR(num) ({ errno = num; return NULL; })
28 struct lizard_buffer *lizard;
33 buck2obj_alloc_internal(struct buck2obj_buf *buf, uns max_len)
35 buf->max_len = max_len;
42 buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
43 buf->raw = xmalloc(buf->raw_len);
47 buck2obj_alloc(struct mempool *mp)
49 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
50 buck2obj_alloc_internal(buf, 0);
51 buf->lizard = lizard_alloc(0);
57 buck2obj_free(struct buck2obj_buf *buf)
59 lizard_free(buf->lizard);
66 buck2obj_realloc(struct buck2obj_buf *buf, uns max_len)
68 if (max_len <= buf->max_len)
70 if (max_len < 2*buf->max_len) // to ensure amortized logarithmic complexity
71 max_len = 2*buf->max_len;
74 buck2obj_alloc_internal(buf, max_len);
78 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
80 if (can_overwrite >= 2)
90 obj_add_attr_ref(o, type, ptr);
94 else if (can_overwrite == 1)
101 byte type = ptr[len];
104 obj_add_attr(o, type, ptr);
116 byte type = ptr[len];
118 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
119 memcpy(dup, ptr, len);
121 obj_add_attr_ref(o, type, dup);
129 obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns want_body)
131 struct odes *o = obj_new(buf->mp);
133 if (buck_type < BUCKET_TYPE_V33)
135 if (want_body) // ignore empty lines, read until NUL or EOF
137 obj_read_multi(body, o);
140 else // end on EOF or the first empty line
145 /* Read all the bucket into 1 buffer, 0-copy if possible. */
146 /* FIXME: This could be cached in buck2obj_buf */
147 int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
148 if (can_overwrite < 0)
152 uns len = bdirect_read_prepare(body, &ptr);
154 || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
156 /* Copy if the original buffer is too small.
157 * If it is write-protected, copy it also if it is uncompressed. */
158 if (buck_len > buf->raw_len)
159 buck2obj_realloc(buf, buck_len);
160 len = bread(body, buf->raw, buck_len);
166 overwritten = can_overwrite > 1;
169 ptr = decode_attributes(ptr, end, o, can_overwrite);// header
172 if (buck_type == BUCKET_TYPE_V33)
174 else if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
180 res = lizard_decompress_safe(ptr, buf->lizard, len);
181 if (res != (int) len)
185 else if (errno == EFBIG)
187 lizard_realloc(buf->lizard, len);
193 ptr = buf->lizard->ptr;
197 else // unknown bucket type
199 ASSERT(can_overwrite == 2); // because of the policy and decompression
200 ptr = decode_attributes(ptr, end, o, 2); // body
204 /* If (overwritten), bflush(body) might be needed. */
210 obj_attr_to_bucket(byte *buf, uns buck_type, uns attr, byte *val)
216 case BUCKET_TYPE_PLAIN:
217 case BUCKET_TYPE_V30:
218 buf += sprintf(buf, "%c%s\n", attr, val);
220 case BUCKET_TYPE_V33:
221 case BUCKET_TYPE_V33_LIZARD:
230 die("obj_attr_to_bucket called for unknown type %08x", buck_type);
236 obj_attr_to_bucket_num(byte *buf, uns buck_type, uns attr, uns val)
239 sprintf(vbuf, "%d", val);
240 return obj_attr_to_bucket(buf, buck_type, attr, vbuf);