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;
36 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
38 buf->lizard = lizard_alloc();
43 buck2obj_free(struct buck2obj_buf *buf)
45 lizard_free(buf->lizard);
51 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
53 if (can_overwrite >= 2)
63 obj_add_attr_ref(o, type, ptr);
76 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
77 memcpy(dup, ptr, len);
79 obj_add_attr_ref(o, type, dup);
87 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
89 struct odes *o = obj_new(pool);
91 if (buck_type == BUCKET_TYPE_PLAIN || buck_type == BUCKET_TYPE_V30)
93 if (!body_start) // header + body: ignore empty lines, read until EOF
95 obj_read_multi(body, o);
98 else // header only: end on EOF or the first empty line
100 sh_off_t start = btell(body);
102 *body_start = btell(body) - start;
105 else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
107 /* Read all the bucket into 1 buffer, 0-copy if possible. */
108 int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, -1);
109 /* FIXME: This could be cached in buck2obj_buf */
111 uns len = bdirect_read_prepare(body, &ptr);
113 || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
115 /* Copy if the original buffer is too small.
116 * If it is write-protected, copy it also if it is uncompressed. */
117 bb_grow(&buf->bb, buck_len);
118 len = bread(body, buf->bb.ptr, buck_len);
124 ptr = decode_attributes(ptr, end, o, 0); // header
127 *body_start = ptr - start;
130 if (buck_type == BUCKET_TYPE_V33)
132 else if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
136 byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
142 else // unknown bucket type
144 ptr = decode_attributes(ptr, end, o, 2); // body
148 /* If the bucket fit into the fastbuf buffer, can_overwrite == 2, and
149 * buck_type == BUCKET_TYPE_V33, then bflush(body) is needed before
150 * anything else than sequential read. */