]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
Accept V33_LIZARD buckets without body -- such buckets can legally
[libucw.git] / lib / buck2obj.c
1 /*
2  *      Generating Objects from Buckets
3  *
4  *      (c) 2004, Robert Spalek <robert@ucw.cz>
5  *      (c) 2004, Martin Mares <mj@ucw.cz>
6  */
7
8 #undef LOCAL_DEBUG
9
10 #include "lib/lib.h"
11 #include "lib/unaligned.h"
12 #include "lib/mempool.h"
13 #include "lib/fastbuf.h"
14 #include "lib/unicode.h"
15 #include "lib/object.h"
16 #include "lib/bucket.h"
17 #include "lib/lizard.h"
18 #include "lib/bbuf.h"
19 #include "lib/ff-utf8.h"
20
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <unistd.h>
24
25 #define RET_ERR(num)    ({ errno = num; return -1; })
26
27 struct buck2obj_buf
28 {
29   bb_t bb;
30   struct lizard_buffer *lizard;
31 };
32
33 struct buck2obj_buf *
34 buck2obj_alloc(void)
35 {
36   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
37   bb_init(&buf->bb);
38   buf->lizard = lizard_alloc();
39   return buf;
40 }
41
42 void
43 buck2obj_free(struct buck2obj_buf *buf)
44 {
45   lizard_free(buf->lizard);
46   bb_done(&buf->bb);
47   xfree(buf);
48 }
49
50 static inline byte *
51 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
52 {
53   if (can_overwrite >= 2)
54     while (ptr < end)
55     {
56       uns len;
57       GET_UTF8(ptr, len);
58       if (!len--)
59         break;
60       byte type = ptr[len];
61
62       ptr[len] = 0;
63       obj_add_attr_ref(o, type, ptr);
64
65       ptr += len + 1;
66     }
67   else
68     while (ptr < end)
69     {
70       uns len;
71       GET_UTF8(ptr, len);
72       if (!len--)
73         break;
74       byte type = ptr[len];
75
76       byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
77       memcpy(dup, ptr, len);
78       dup[len] = 0;
79       obj_add_attr_ref(o, type, dup);
80
81       ptr += len + 1;
82     }
83   return ptr;
84 }
85
86 int
87 buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, struct odes *o_hdr, uns *body_start, struct odes *o_body)
88 {
89   if (buck_type <= BUCKET_TYPE_PLAIN)
90   {
91     if (body_start)                     // there is no header part
92       *body_start = 0;
93     // ignore empty lines and read until the end of the bucket
94     sh_off_t end = btell(body) + buck_len;
95     byte buf[MAX_ATTR_SIZE];
96     while (btell(body) < end && bgets(body, buf, sizeof(buf)))
97       if (buf[0])
98         obj_add_attr(o_hdr, buf[0], buf+1);
99     ASSERT(btell(body) == end);
100   }
101   else if (buck_type == BUCKET_TYPE_V30)
102   {
103     sh_off_t start = btell(body);
104     sh_off_t end = start + buck_len;
105     byte buf[MAX_ATTR_SIZE];
106     while (btell(body) < end && bgets(body, buf, sizeof(buf)) && buf[0])
107       obj_add_attr(o_hdr, buf[0], buf+1);
108     if (body_start)
109       *body_start = btell(body) - start;
110     else
111     {
112       while (btell(body) < end && bgets(body, buf, sizeof(buf)))
113         if (buf[0])
114           obj_add_attr(o_body, buf[0], buf+1);
115       ASSERT(btell(body) == end);
116     }
117   }
118   else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
119   {
120     /* Avoid reading the whole bucket if only its header is needed.  */
121     if (body_start)
122     {
123       sh_off_t start = btell(body);
124       sh_off_t end = start + buck_len;
125       while (btell(body) < end)
126       {
127         uns len = bget_utf8(body);
128         if (!len)
129           break;
130         byte *buf = mp_alloc_fast_noalign(o_hdr->pool, len);
131         bread(body, buf, len);
132         uns type = buf[--len];
133         buf[len] = 0;
134         obj_add_attr_ref(o_hdr, type, buf);
135       }
136       *body_start = btell(body) - start;
137       return 0;
138     }
139
140     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
141     byte *ptr, *end;
142     uns len = bdirect_read_prepare(body, &ptr);
143     uns copied = 0;
144     if (len < buck_len
145     || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
146     {
147       /* Copy if the original buffer is too small.
148        * If it is write-protected, copy it also if it is uncompressed.  */
149       DBG("NO ZC: %d < %d, %d %08x", len, buck_len, body->can_overwrite_buffer, buck_type);
150       bb_grow(&buf->bb, buck_len);
151       len = bread(body, buf->bb.ptr, buck_len);
152       ptr = buf->bb.ptr;
153       copied = 1;
154     }
155     else
156       DBG("ZC (%d >= %d, %d %08x)", len, buck_len, body->can_overwrite_buffer, buck_type);
157     end = ptr + buck_len;
158
159     ptr = decode_attributes(ptr, end, o_hdr, 0);                // header
160     if (buck_type == BUCKET_TYPE_V33_LIZARD)            // decompression
161     {
162       if (ptr + 8 > end)
163         {
164           if (ptr == end)                               // truncated bucket
165             goto commit;
166           RET_ERR(EINVAL);
167         }
168       len = GET_U32(ptr);
169       ptr += 4;
170       uns adler = GET_U32(ptr);
171       ptr += 4;
172       byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
173       if (!new_ptr)
174         return -1;
175       if (adler32(new_ptr, len) != adler)
176         RET_ERR(EINVAL);
177       if (!copied)
178         bdirect_read_commit(body, end);
179       ptr = new_ptr;
180       end = ptr + len;
181       copied = 1;
182     }
183     ptr = decode_attributes(ptr, end, o_body, 2);       // body
184     if (ptr != end)
185       RET_ERR(EINVAL);
186   commit:
187     if (!copied)
188       bdirect_read_commit_modified(body, ptr);
189   }
190   else
191     {
192       bskip(body, buck_len);
193       RET_ERR(EINVAL);
194     }
195   return 0;
196 }
197
198 struct odes *
199 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
200 {
201   struct odes *o = obj_new(pool);
202   if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
203     return NULL;
204   else
205     return o;
206 }
207
208 int
209 obj_read(struct fastbuf *f, struct odes *o)
210 {
211   byte buf[MAX_ATTR_SIZE];
212
213   while (bgets(f, buf, sizeof(buf)))
214     {
215       if (!buf[0])
216         return 1;
217       obj_add_attr(o, buf[0], buf+1);
218     }
219   return 0;
220 }