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