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