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