]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
- buck2obj_alloc() initially allocates no buffers
[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, struct fastbuf *body, uns want_body)
130 {
131   mp_flush(buf->mp);
132   struct odes *o = obj_new(buf->mp);
133
134   if (buck_type < BUCKET_TYPE_V33)
135   {
136     if (want_body)                      // ignore empty lines, read until EOF
137       obj_read_multi(body, o);
138     else                                // end on EOF or the first empty line
139       obj_read(body, o);
140   }
141   else
142   {
143     /* Compute the length of the bucket.  We cannot fetch this attribute
144      * directly due to remote indexing.  */
145     bseek(body, 0, SEEK_END);
146     sh_off_t buck_len = btell(body);
147     bsetpos(body, 0);
148
149     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
150     int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
151     if (can_overwrite < 0)
152       can_overwrite = 0;
153     uns overwritten;
154     byte *ptr, *end;
155     uns len = bdirect_read_prepare(body, &ptr);
156     if (len < buck_len
157     || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
158     {
159       /* Copy if the original buffer is too small.
160        * If it is write-protected, copy it also if it is uncompressed.  */
161       if (buck_len > buf->raw_len)
162         buck2obj_realloc(buf, buck_len);
163       len = bread(body, buf->raw, buck_len);
164       ptr = buf->raw;
165       can_overwrite = 2;
166       overwritten = 0;
167     }
168     else
169       overwritten = can_overwrite > 1;
170     end = ptr + len;
171
172     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
173     if (!want_body)
174       return o;
175     if (buck_type == BUCKET_TYPE_V33)
176       ;
177     else if (buck_type == BUCKET_TYPE_V33_LIZARD)       // decompression
178     {
179       len = GET_U32(ptr);
180       ptr += 4;
181       int res;
182 decompress:
183       res = lizard_decompress_safe(ptr, buf->lizard, len);
184       if (res != (int) len)
185       {
186         if (res >= 0)
187           errno = EINVAL;
188         else if (errno == EFBIG)
189         {
190           lizard_realloc(buf->lizard, len);
191           goto decompress;
192         }
193         else
194           return NULL;
195       }
196       ptr = buf->lizard->ptr;
197       end = ptr + len;
198       can_overwrite = 2;
199     }
200     else                                                // unknown bucket type
201       RET_ERR(EINVAL);
202     ASSERT(can_overwrite == 2);                         // because of the policy and decompression
203     ptr = decode_attributes(ptr, end, o, 2);            // body
204
205     if (ptr != end)
206       RET_ERR(EINVAL);
207     /* If (overwritten), bflush(body) might be needed.  */
208   }
209   return o;
210 }