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