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