]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
modifications done at home, not yet incorporated MJ's objections:
[libucw.git] / lib / buck2obj.c
1 /*
2  *      Bucket -> Object converter
3  *
4  *      (c) 2004, Robert Spalek <robert@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "lib/unaligned.h"
9 #include "lib/pools.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"
16
17 #include <stdlib.h>
18 #include <errno.h>
19
20 #define MAX_HEADER_SIZE 1024            // extra space for the header not countet to MaxObjSize
21
22 struct buck2obj_buf
23 {
24   byte *raw;
25   uns raw_len;
26   struct lizard_buffer *lizard;
27   struct mempool *mp;
28 };
29
30 struct buck2obj_buf *
31 buck2obj_alloc(uns max_len, struct mempool *mp)
32 {
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);
37   buf->mp = mp;
38   return buf;
39 }
40
41 void
42 buck2obj_free(struct buck2obj_buf *buf)
43 {
44   lizard_free(buf->lizard);
45   xfree(buf->raw);
46   xfree(buf);
47 }
48
49 static inline byte *
50 decode_attributes(byte *ptr, byte *end, struct odes *o)
51 {
52   while (ptr < end)
53   {
54     uns len;
55     GET_UTF8(ptr, len);
56     if (!len--)
57       break;
58     byte type = ptr[len];
59     ptr[len] = 0;
60     obj_add_attr_ref(o, type, ptr);
61     ptr += len + 1;
62   }
63   return ptr;
64 }
65
66 #define RET_ERR(num)    ({ errno = num; return NULL; })
67 struct odes *
68 buck2obj_convert(struct buck2obj_buf *buf, struct obuck_header *hdr, struct fastbuf *body)
69 {
70   mp_flush(buf->mp);
71   struct odes *o = obj_new(buf->mp);
72
73   if (hdr->type < BUCKET_TYPE_V33)
74     obj_read_multi(body, o);
75   else
76   {
77     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
78     byte *ptr, *end;
79     uns len = bdirect_read_prepare(body, &ptr);         // WARNING: must NOT use mmaped-I/O
80     if (len < hdr->length)
81     {
82       if (hdr->length > buf->raw_len)
83         RET_ERR(EFBIG);
84       len = bread(body, buf->raw, hdr->length);
85       ptr = buf->raw;
86     }
87     end = ptr + len;
88
89     ptr = decode_attributes(ptr, end, o);               // header
90     if (hdr->type == BUCKET_TYPE_V33)
91       ;
92     else if (hdr->type == BUCKET_TYPE_V33_LIZARD)       // decompression
93     {
94       len = GET_U32(ptr);
95       ptr += 4;
96       int res = lizard_decompress_safe(ptr, buf->lizard, len);
97       if (res != (int) len)
98       {
99         if (res >= 0)
100           errno = EINVAL;
101         return NULL;
102       }
103       ptr = buf->lizard->ptr;
104       end = ptr + len;
105     }
106     else                                                // unknown bucket type
107       RET_ERR(EINVAL);
108     ptr = decode_attributes(ptr, end, o);               // body
109
110     if (ptr != end)
111       RET_ERR(EINVAL);
112   }
113   return o;
114 }