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 RET_ERR(num) ({ errno = num; return NULL; })
23 #define GBUF_TYPE byte
24 #define GBUF_PREFIX(x) bb_##x
30 struct lizard_buffer *lizard;
35 buck2obj_alloc(struct mempool *mp)
37 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
39 buf->lizard = lizard_alloc();
45 buck2obj_free(struct buck2obj_buf *buf)
47 lizard_free(buf->lizard);
53 buck2obj_flush(struct buck2obj_buf *buf)
59 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
61 if (can_overwrite >= 2)
71 obj_add_attr_ref(o, type, ptr);
84 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
85 memcpy(dup, ptr, len);
87 obj_add_attr_ref(o, type, dup);
95 obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
97 struct odes *o = obj_new(buf->mp);
99 if (buck_type < BUCKET_TYPE_V33)
101 if (!body_start) // header + body: ignore empty lines, read until EOF
103 obj_read_multi(body, o);
106 else // header only: end on EOF or the first empty line
108 sh_off_t start = btell(body);
110 *body_start = btell(body) - start;
115 /* Read all the bucket into 1 buffer, 0-copy if possible. */
116 int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, -1);
117 /* FIXME: This could be cached in buck2obj_buf */
119 uns len = bdirect_read_prepare(body, &ptr);
121 || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
123 /* Copy if the original buffer is too small.
124 * If it is write-protected, copy it also if it is uncompressed. */
125 bb_grow(&buf->bb, buck_len);
126 len = bread(body, buf->bb.ptr, buck_len);
132 ptr = decode_attributes(ptr, end, o, 0); // header
135 *body_start = ptr - start;
138 if (buck_type == BUCKET_TYPE_V33)
140 else if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
146 res = lizard_decompress_safe(ptr, buf->lizard, len, &new_ptr);
147 if (res != (int) len)
156 else // unknown bucket type
158 ptr = decode_attributes(ptr, end, o, 2); // body
162 /* If the bucket fit into the fastbuf buffer, can_overwrite == 2, and
163 * buck_type == BUCKET_TYPE_V33, then bflush(body) is needed before
164 * anything else than sequential read. */