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 counted in MaxObjSize
21 #define RET_ERR(num) ({ errno = num; return NULL; })
27 struct lizard_buffer *lizard;
32 buck2obj_alloc(uns max_len, struct mempool *mp)
34 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
35 buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
36 buf->raw = xmalloc(buf->raw_len);
37 buf->lizard = lizard_alloc(max_len);
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)
60 if (can_overwrite == 2)
63 obj_add_attr_ref(o, type, ptr);
65 else if (can_overwrite == 1)
68 obj_add_attr(o, type, ptr);
73 byte *dup = mp_alloc(o->pool, len+1);
74 memcpy(dup, ptr, len);
76 obj_add_attr_ref(o, type, ptr);
84 buck2obj_convert(struct buck2obj_buf *buf, struct obuck_header *hdr, struct fastbuf *body)
87 struct odes *o = obj_new(buf->mp);
89 if (hdr->type < BUCKET_TYPE_V33)
90 obj_read_multi(body, o);
93 /* Read all the bucket into 1 buffer, 0-copy if possible. */
94 int can_overwrite = MAX(bconfig(body, BCONFIG_CAN_OVERWRITE, 0), 0);
97 uns len = bdirect_read_prepare(body, &ptr);
99 || (can_overwrite < 2 && hdr->type == BUCKET_TYPE_V33))
101 /* Copy if the original buffer is too small.
102 * If it is write-protected, copy it also if it is uncompressed. */
103 if (hdr->length > buf->raw_len)
105 len = bread(body, buf->raw, hdr->length);
111 overwritten = can_overwrite > 1;
114 ptr = decode_attributes(ptr, end, o, can_overwrite);// header
115 if (hdr->type == BUCKET_TYPE_V33)
117 else if (hdr->type == BUCKET_TYPE_V33_LIZARD) // decompression
121 int res = lizard_decompress_safe(ptr, buf->lizard, len);
122 if (res != (int) len)
128 ptr = buf->lizard->ptr;
132 else // unknown bucket type
134 ASSERT(can_overwrite == 2); // because of the policy and decompression
135 ptr = decode_attributes(ptr, end, o, 2); // body