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"
20 #define MAX_HEADER_SIZE 1024 // extra space for the header not countet to MaxObjSize
26 struct lizard_buffer *lizard;
31 buck2obj_alloc(uns max_len, struct mempool *mp)
33 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
34 buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
35 buf->raw = xmalloc(buf->raw_len);
36 buf->lizard = lizard_alloc(max_len);
42 buck2obj_free(struct buck2obj_buf *buf)
44 lizard_free(buf->lizard);
50 decode_attributes(byte *ptr, byte *end, struct odes *o)
60 obj_add_attr_ref(o, type, ptr);
66 #define RET_ERR(num) ({ errno = num; return NULL; })
68 buck2obj_convert(struct buck2obj_buf *buf, struct obuck_header *hdr, struct fastbuf *body)
71 struct odes *o = obj_new(buf->mp);
73 if (hdr->type < BUCKET_TYPE_V33)
74 obj_read_multi(body, o);
77 /* Read all the bucket into 1 buffer, 0-copy if possible. */
79 uns len = bdirect_read_prepare(body, &ptr); // WARNING: must NOT use mmaped-I/O
80 if (len < hdr->length)
82 if (hdr->length > buf->raw_len)
84 len = bread(body, buf->raw, hdr->length);
89 ptr = decode_attributes(ptr, end, o); // header
90 if (hdr->type == BUCKET_TYPE_V33)
92 else if (hdr->type == BUCKET_TYPE_V33_LIZARD) // decompression
96 int res = lizard_decompress_safe(ptr, buf->lizard, len);
103 ptr = buf->lizard->ptr;
106 else // unknown bucket type
108 ptr = decode_attributes(ptr, end, o); // body