]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
7d651dfe6b65f6941f375a4c32ef53f88033c06d
[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/pools.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 + 4 > end)
134         RET_ERR(EINVAL);
135       len = GET_U32(ptr);
136       ptr += 4;
137       byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
138       if (!new_ptr)
139         return -1;
140       if (!copied)
141         bdirect_read_commit(body, end);
142       ptr = new_ptr;
143       end = ptr + len;
144       copied = 1;
145     }
146     ptr = decode_attributes(ptr, end, o_body, 2);       // body
147     if (ptr != end)
148       RET_ERR(EINVAL);
149     if (!copied)
150       bdirect_read_commit_modified(body, ptr);
151   }
152   else
153     RET_ERR(EINVAL);
154   return 0;
155 }
156
157 struct odes *
158 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
159 {
160   struct odes *o = obj_new(pool);
161   if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0)
162     return NULL;
163   else
164     return o;
165 }
166
167 int
168 obj_read(struct fastbuf *f, struct odes *o)
169 {
170   byte buf[MAX_ATTR_SIZE];
171
172   while (bgets(f, buf, sizeof(buf)))
173     {
174       if (!buf[0])
175         return 1;
176       obj_add_attr(o, buf[0], buf+1);
177     }
178   return 0;
179 }
180
181 void
182 obj_read_multi(struct fastbuf *f, struct odes *o)
183 {
184   /* Read a multi-part object ending with either EOF or a NUL character */
185   byte buf[MAX_ATTR_SIZE];
186   while (bpeekc(f) > 0 && bgets(f, buf, sizeof(buf)))
187     if (buf[0])
188       obj_add_attr(o, buf[0], buf+1);
189 }