]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
8c74bdd13bb6dac1028e8bb5d1df7a2ad94fbe51
[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   buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
37   buf->raw = xmalloc(buf->raw_len);
38   buf->lizard = lizard_alloc(max_len);
39 }
40
41 static void
42 buck2obj_free_internal(struct buck2obj_buf *buf)
43 {
44   lizard_free(buf->lizard);
45   xfree(buf->raw);
46 }
47
48 struct buck2obj_buf *
49 buck2obj_alloc(uns max_len, struct mempool *mp)
50 {
51   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
52   buck2obj_alloc_internal(buf, max_len);
53   buf->mp = mp;
54   return buf;
55 }
56
57 void
58 buck2obj_free(struct buck2obj_buf *buf)
59 {
60   buck2obj_free_internal(buf);
61   xfree(buf);
62 }
63
64 void
65 buck2obj_realloc(struct buck2obj_buf *buf, uns max_len)
66 {
67   if (max_len <= buf->max_len)
68     return;
69   if (max_len < 2*buf->max_len + 1)             // to ensure amortized logarithmic complexity
70     max_len = 2*buf->max_len + 1;
71   buck2obj_free_internal(buf);
72   buck2obj_alloc_internal(buf, max_len);
73 }
74
75 static inline byte *
76 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
77 {
78   if (can_overwrite >= 2)
79     while (ptr < end)
80     {
81       uns len;
82       GET_UTF8(ptr, len);
83       if (!len--)
84         break;
85       byte type = ptr[len];
86
87       ptr[len] = 0;
88       obj_add_attr_ref(o, type, ptr);
89
90       ptr += len + 1;
91     }
92   else if (can_overwrite == 1)
93     while (ptr < end)
94     {
95       uns len;
96       GET_UTF8(ptr, len);
97       if (!len--)
98         break;
99       byte type = ptr[len];
100
101       ptr[len] = 0;
102       obj_add_attr(o, type, ptr);
103       ptr[len] = type;
104
105       ptr += len + 1;
106     }
107   else
108     while (ptr < end)
109     {
110       uns len;
111       GET_UTF8(ptr, len);
112       if (!len--)
113         break;
114       byte type = ptr[len];
115
116       byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
117       memcpy(dup, ptr, len);
118       dup[len] = 0;
119       obj_add_attr_ref(o, type, dup);
120
121       ptr += len + 1;
122     }
123   return ptr;
124 }
125
126 struct odes *
127 buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
128 {
129   mp_flush(buf->mp);
130   struct odes *o = obj_new(buf->mp);
131
132   if (buck_type < BUCKET_TYPE_V33)
133     obj_read_multi(body, o);
134   else
135   {
136     /* Compute the length of the bucket.  We cannot fetch this attribute
137      * directly due to remote indexing.  */
138     bseek(body, 0, SEEK_END);
139     sh_off_t buck_len = btell(body);
140     bsetpos(body, 0);
141
142     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
143     int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
144     if (can_overwrite < 0)
145       can_overwrite = 0;
146     uns overwritten;
147     byte *ptr, *end;
148     uns len = bdirect_read_prepare(body, &ptr);
149     if (len < buck_len
150     || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
151     {
152       /* Copy if the original buffer is too small.
153        * If it is write-protected, copy it also if it is uncompressed.  */
154       if (buck_len > buf->raw_len)
155         buck2obj_realloc(buf, buck_len);
156       len = bread(body, buf->raw, buck_len);
157       ptr = buf->raw;
158       can_overwrite = 2;
159       overwritten = 0;
160     }
161     else
162       overwritten = can_overwrite > 1;
163     end = ptr + len;
164
165     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
166     if (buck_type == BUCKET_TYPE_V33)
167       ;
168     else if (buck_type == BUCKET_TYPE_V33_LIZARD)       // decompression
169     {
170       len = GET_U32(ptr);
171       ptr += 4;
172       int res = lizard_decompress_safe(ptr, buf->lizard, len);
173       if (res != (int) len)
174       {
175         if (res >= 0)
176           errno = EINVAL;
177         return NULL;
178       }
179       ptr = buf->lizard->ptr;
180       end = ptr + len;
181       can_overwrite = 2;
182     }
183     else                                                // unknown bucket type
184       RET_ERR(EINVAL);
185     ASSERT(can_overwrite == 2);                         // because of the policy and decompression
186     ptr = decode_attributes(ptr, end, o, 2);            // body
187
188     if (ptr != end)
189       RET_ERR(EINVAL);
190     /* If (overwritten), bflush(body) might be needed.  */
191   }
192   return o;
193 }