]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
f9375141314cffb8f1d2e2fb1206010988ab7ea7
[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 counted in MaxObjSize
21 #define RET_ERR(num)    ({ errno = num; return NULL; })
22
23 struct buck2obj_buf
24 {
25   byte *raw;
26   uns raw_len;
27   struct lizard_buffer *lizard;
28   struct mempool *mp;
29 };
30
31 struct buck2obj_buf *
32 buck2obj_alloc(uns max_len, struct mempool *mp)
33 {
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);
38   buf->mp = mp;
39   return buf;
40 }
41
42 void
43 buck2obj_free(struct buck2obj_buf *buf)
44 {
45   lizard_free(buf->lizard);
46   xfree(buf->raw);
47   xfree(buf);
48 }
49
50 static inline byte *
51 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
52 {
53   while (ptr < end)
54   {
55     uns len;
56     GET_UTF8(ptr, len);
57     if (!len--)
58       break;
59     byte type = ptr[len];
60     if (can_overwrite == 2)
61     {
62       ptr[len] = 0;
63       obj_add_attr_ref(o, type, ptr);
64     }
65     else if (can_overwrite == 1)
66     {
67       ptr[len] = 0;
68       obj_add_attr(o, type, ptr);
69       ptr[len] = type;
70     }
71     else
72     {
73       byte *dup = mp_alloc(o->pool, len+1);
74       memcpy(dup, ptr, len);
75       dup[len] = 0;
76       obj_add_attr_ref(o, type, ptr);
77     }
78     ptr += len + 1;
79   }
80   return ptr;
81 }
82
83 struct odes *
84 buck2obj_convert(struct buck2obj_buf *buf, struct obuck_header *hdr, struct fastbuf *body)
85 {
86   mp_flush(buf->mp);
87   struct odes *o = obj_new(buf->mp);
88
89   if (hdr->type < BUCKET_TYPE_V33)
90     obj_read_multi(body, o);
91   else
92   {
93     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
94     int can_overwrite = MAX(bconfig(body, BCONFIG_CAN_OVERWRITE, 0), 0);
95     uns overwritten;
96     byte *ptr, *end;
97     uns len = bdirect_read_prepare(body, &ptr);
98     if (len < hdr->length
99     || (can_overwrite < 2 && hdr->type == BUCKET_TYPE_V33))
100     {
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)
104         RET_ERR(EFBIG);
105       len = bread(body, buf->raw, hdr->length);
106       ptr = buf->raw;
107       can_overwrite = 2;
108       overwritten = 0;
109     }
110     else
111       overwritten = can_overwrite > 1;
112     end = ptr + len;
113
114     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
115     if (hdr->type == BUCKET_TYPE_V33)
116       ;
117     else if (hdr->type == BUCKET_TYPE_V33_LIZARD)       // decompression
118     {
119       len = GET_U32(ptr);
120       ptr += 4;
121       int res = lizard_decompress_safe(ptr, buf->lizard, len);
122       if (res != (int) len)
123       {
124         if (res >= 0)
125           errno = EINVAL;
126         return NULL;
127       }
128       ptr = buf->lizard->ptr;
129       end = ptr + len;
130       can_overwrite = 2;
131     }
132     else                                                // unknown bucket type
133       RET_ERR(EINVAL);
134     ASSERT(can_overwrite == 2);                         // because of the policy and decompression
135     ptr = decode_attributes(ptr, end, o, 2);            // body
136
137     if (ptr != end)
138       RET_ERR(EINVAL);
139     if (overwritten)
140       bflush(body);
141   }
142   return o;
143 }