]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
obj_read_bucket() now gets the bucket length as a parameter, because
[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 #include <unistd.h>
20
21 #define MAX_HEADER_SIZE 1024            // extra space for the header not counted in MaxObjSize
22 #define RET_ERR(num)    ({ errno = num; return NULL; })
23
24 struct buck2obj_buf
25 {
26   uns max_len, raw_len;
27   byte *raw;
28   struct lizard_buffer *lizard;
29   struct mempool *mp;
30 };
31
32 static void
33 buck2obj_alloc_internal(struct buck2obj_buf *buf, uns max_len)
34 {
35   buf->max_len = max_len;
36   if (!max_len)
37   {
38     buf->raw_len = 0;
39     buf->raw = NULL;
40     return;
41   }
42   buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
43   buf->raw = xmalloc(buf->raw_len);
44 }
45
46 struct buck2obj_buf *
47 buck2obj_alloc(struct mempool *mp)
48 {
49   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
50   buck2obj_alloc_internal(buf, 0);
51   buf->lizard = lizard_alloc(0);
52   buf->mp = mp;
53   return buf;
54 }
55
56 void
57 buck2obj_free(struct buck2obj_buf *buf)
58 {
59   lizard_free(buf->lizard);
60   if (buf->raw)
61     xfree(buf->raw);
62   xfree(buf);
63 }
64
65 static void
66 buck2obj_realloc(struct buck2obj_buf *buf, uns max_len)
67 {
68   if (max_len <= buf->max_len)
69     return;
70   if (max_len < 2*buf->max_len)         // to ensure amortized logarithmic complexity
71     max_len = 2*buf->max_len;
72   if (buf->raw)
73     xfree(buf->raw);
74   buck2obj_alloc_internal(buf, max_len);
75 }
76
77 static inline byte *
78 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
79 {
80   if (can_overwrite >= 2)
81     while (ptr < end)
82     {
83       uns len;
84       GET_UTF8(ptr, len);
85       if (!len--)
86         break;
87       byte type = ptr[len];
88
89       ptr[len] = 0;
90       obj_add_attr_ref(o, type, ptr);
91
92       ptr += len + 1;
93     }
94   else if (can_overwrite == 1)
95     while (ptr < end)
96     {
97       uns len;
98       GET_UTF8(ptr, len);
99       if (!len--)
100         break;
101       byte type = ptr[len];
102
103       ptr[len] = 0;
104       obj_add_attr(o, type, ptr);
105       ptr[len] = type;
106
107       ptr += len + 1;
108     }
109   else
110     while (ptr < end)
111     {
112       uns len;
113       GET_UTF8(ptr, len);
114       if (!len--)
115         break;
116       byte type = ptr[len];
117
118       byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
119       memcpy(dup, ptr, len);
120       dup[len] = 0;
121       obj_add_attr_ref(o, type, dup);
122
123       ptr += len + 1;
124     }
125   return ptr;
126 }
127
128 struct odes *
129 obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns want_body)
130 {
131   struct odes *o = obj_new(buf->mp);
132
133   if (buck_type < BUCKET_TYPE_V33)
134   {
135     if (want_body)                      // ignore empty lines, read until NUL or EOF
136       {
137         obj_read_multi(body, o);
138         bgetc(body);
139       }
140     else                                // end on EOF or the first empty line
141       obj_read(body, o);
142   }
143   else
144   {
145     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
146     /* FIXME: This could be cached in buck2obj_buf */
147     int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
148     if (can_overwrite < 0)
149       can_overwrite = 0;
150     uns overwritten;
151     byte *ptr, *end;
152     uns len = bdirect_read_prepare(body, &ptr);
153     if (len < buck_len
154     || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
155     {
156       /* Copy if the original buffer is too small.
157        * If it is write-protected, copy it also if it is uncompressed.  */
158       if (buck_len > buf->raw_len)
159         buck2obj_realloc(buf, buck_len);
160       len = bread(body, buf->raw, buck_len);
161       ptr = buf->raw;
162       can_overwrite = 2;
163       overwritten = 0;
164     }
165     else
166       overwritten = can_overwrite > 1;
167     end = ptr + len;
168
169     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
170     if (!want_body)
171       return o;
172     if (buck_type == BUCKET_TYPE_V33)
173       ;
174     else if (buck_type == BUCKET_TYPE_V33_LIZARD)       // decompression
175     {
176       len = GET_U32(ptr);
177       ptr += 4;
178       int res;
179 decompress:
180       res = lizard_decompress_safe(ptr, buf->lizard, len);
181       if (res != (int) len)
182       {
183         if (res >= 0)
184           errno = EINVAL;
185         else if (errno == EFBIG)
186         {
187           lizard_realloc(buf->lizard, len);
188           goto decompress;
189         }
190         else
191           return NULL;
192       }
193       ptr = buf->lizard->ptr;
194       end = ptr + len;
195       can_overwrite = 2;
196     }
197     else                                                // unknown bucket type
198       RET_ERR(EINVAL);
199     ASSERT(can_overwrite == 2);                         // because of the policy and decompression
200     ptr = decode_attributes(ptr, end, o, 2);            // body
201
202     if (ptr != end)
203       RET_ERR(EINVAL);
204     /* If (overwritten), bflush(body) might be needed.  */
205   }
206   return o;
207 }
208
209 byte *
210 obj_attr_to_bucket(byte *buf, uns buck_type, uns attr, byte *val)
211 {
212   uns l;
213
214   switch (buck_type)
215     {
216     case BUCKET_TYPE_PLAIN:
217     case BUCKET_TYPE_V30:
218       buf += sprintf(buf, "%c%s\n", attr, val);
219       break;
220     case BUCKET_TYPE_V33:
221     case BUCKET_TYPE_V33_LIZARD:
222       l = strlen(val) + 1;
223       PUT_UTF8(buf, l);
224       l--;
225       memcpy(buf, val, l);
226       buf += l;
227       *buf++ = attr;
228       break;
229     default:
230       die("obj_attr_to_bucket called for unknown type %08x", buck_type);
231     }
232   return buf;
233 }
234
235 byte *
236 obj_attr_to_bucket_num(byte *buf, uns buck_type, uns attr, uns val)
237 {
238   byte vbuf[16];
239   sprintf(vbuf, "%d", val);
240   return obj_attr_to_bucket(buf, buck_type, attr, vbuf);
241 }