]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
- obj_parse_dump() generalized to store apart the header and the body, and
[libucw.git] / lib / buck2obj.c
1 /*
2  *      Bucket -> Object Converter
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 "charset/unicode.h"
13 #include "lib/object.h"
14 #include "lib/bucket.h"
15 #include "lib/lizard.h"
16 #include "lib/buck2obj.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 #define GBUF_TYPE       byte
25 #define GBUF_PREFIX(x)  bb_##x
26 #include "lib/gbuf.h"
27
28 struct buck2obj_buf
29 {
30   bb_t bb;
31   struct lizard_buffer *lizard;
32 };
33
34 struct buck2obj_buf *
35 buck2obj_alloc(void)
36 {
37   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
38   bb_init(&buf->bb);
39   buf->lizard = lizard_alloc();
40   return buf;
41 }
42
43 void
44 buck2obj_free(struct buck2obj_buf *buf)
45 {
46   lizard_free(buf->lizard);
47   bb_done(&buf->bb);
48   xfree(buf);
49 }
50
51 static inline byte *
52 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
53 {
54   if (can_overwrite >= 2)
55     while (ptr < end)
56     {
57       uns len;
58       GET_UTF8(ptr, len);
59       if (!len--)
60         break;
61       byte type = ptr[len];
62
63       ptr[len] = 0;
64       obj_add_attr_ref(o, type, ptr);
65
66       ptr += len + 1;
67     }
68   else
69     while (ptr < end)
70     {
71       uns len;
72       GET_UTF8(ptr, len);
73       if (!len--)
74         break;
75       byte type = ptr[len];
76
77       byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
78       memcpy(dup, ptr, len);
79       dup[len] = 0;
80       obj_add_attr_ref(o, type, dup);
81
82       ptr += len + 1;
83     }
84   return ptr;
85 }
86
87 int
88 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)
89 {
90   if (buck_type == BUCKET_TYPE_PLAIN)
91   {
92     if (body_start)
93       *body_start = 0;
94     obj_read_multi(body, o_hdr);        // ignore empty lines, read until EOF or NUL
95   }
96   else if (buck_type == BUCKET_TYPE_V30)
97   {
98     sh_off_t start = btell(body);
99     obj_read(body, o_hdr);              // end on EOF or the first empty line
100     if (body_start)
101       *body_start = btell(body) - start;
102     else
103     {
104       obj_read(body, o_body);
105       bgetc(body);
106     }
107   }
108   else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD)
109   {
110     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
111     byte *ptr, *end;
112     uns len = bdirect_read_prepare(body, &ptr);
113     uns copied = 0;
114     if (len < buck_len
115     || (body->can_overwrite_buffer < 2 && buck_type == BUCKET_TYPE_V33))
116     {
117       /* Copy if the original buffer is too small.
118        * If it is write-protected, copy it also if it is uncompressed.  */
119       bb_grow(&buf->bb, buck_len);
120       len = bread(body, buf->bb.ptr, buck_len);
121       ptr = buf->bb.ptr;
122       copied = 1;
123     }
124     end = ptr + len;
125
126     byte *start = ptr;
127     ptr = decode_attributes(ptr, end, o_hdr, 0);                // header
128     if (body_start)
129     {
130       *body_start = ptr - start;
131       if (!copied)
132         bdirect_read_commit(body, ptr);
133       return 0;
134     }
135     if (buck_type == BUCKET_TYPE_V33_LIZARD)            // decompression
136     {
137       if (ptr + 4 > end)
138         RET_ERR(EINVAL);
139       len = GET_U32(ptr);
140       ptr += 4;
141       byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
142       if (!new_ptr)
143         return -1;
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 }