]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
1daabed91e698b39237ed71f119abde95282004e
[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     sh_off_t end = start + buck_len;
96     byte buf[MAX_ATTR_SIZE];
97     while (btell(body) < end && bgets(body, buf, sizeof(buf)) && buf[0])
98       obj_add_attr(o_hdr, buf[0], buf+1);
99     if (body_start)
100       *body_start = btell(body) - start;
101     else
102     {
103       while (btell(body) < end && bgets(body, buf, sizeof(buf)))
104         if (buf[0])
105           obj_add_attr(o_hdr, buf[0], buf+1);
106       ASSERT(btell(body) == end);
107     }
108   }
109   else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
110   {
111     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
112     byte *ptr, *end;
113     uns len = bdirect_read_prepare(body, &ptr);
114     uns copied = 0;
115     if (len < buck_len
116     || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
117     {
118       /* Copy if the original buffer is too small.
119        * If it is write-protected, copy it also if it is uncompressed.  */
120       bb_grow(&buf->bb, buck_len);
121       len = bread(body, buf->bb.ptr, buck_len);
122       ptr = buf->bb.ptr;
123       copied = 1;
124     }
125     end = ptr + len;
126
127     byte *start = ptr;
128     ptr = decode_attributes(ptr, end, o_hdr, 0);                // header
129     if (body_start)
130     {
131       *body_start = ptr - start;
132       if (!copied)
133         bdirect_read_commit(body, ptr);
134       return 0;
135     }
136     if (buck_type == BUCKET_TYPE_V33_LIZARD)            // decompression
137     {
138       if (ptr + 8 > end)
139         RET_ERR(EINVAL);
140       len = GET_U32(ptr);
141       ptr += 4;
142       uns adler = GET_U32(ptr);
143       ptr += 4;
144       byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
145       if (!new_ptr)
146         return -1;
147       if (adler32(new_ptr, len) != adler)
148         RET_ERR(EINVAL);
149       if (!copied)
150         bdirect_read_commit(body, end);
151       ptr = new_ptr;
152       end = ptr + len;
153       copied = 1;
154     }
155     ptr = decode_attributes(ptr, end, o_body, 2);       // body
156     if (ptr != end)
157       RET_ERR(EINVAL);
158     if (!copied)
159       bdirect_read_commit_modified(body, ptr);
160   }
161   else
162     RET_ERR(EINVAL);
163   return 0;
164 }
165
166 struct odes *
167 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
168 {
169   struct odes *o = obj_new(pool);
170   if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
171     return NULL;
172   else
173     return o;
174 }
175
176 int
177 obj_read(struct fastbuf *f, struct odes *o)
178 {
179   byte buf[MAX_ATTR_SIZE];
180
181   while (bgets(f, buf, sizeof(buf)))
182     {
183       if (!buf[0])
184         return 1;
185       obj_add_attr(o, buf[0], buf+1);
186     }
187   return 0;
188 }
189
190 void
191 obj_read_multi(struct fastbuf *f, struct odes *o)
192 {
193   /* Read a multi-part object ending with either EOF or a NUL character */
194   byte buf[MAX_ATTR_SIZE];
195   while (bpeekc(f) > 0 && bgets(f, buf, sizeof(buf)))
196     if (buf[0])
197       obj_add_attr(o, buf[0], buf+1);
198 }