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