]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
- obj_read_bucket() takes a new parameter: struct mempool *pool
[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 };
32
33 struct buck2obj_buf *
34 buck2obj_alloc(void)
35 {
36   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
37   bb_init(&buf->bb);
38   buf->lizard = lizard_alloc();
39   return buf;
40 }
41
42 void
43 buck2obj_free(struct buck2obj_buf *buf)
44 {
45   lizard_free(buf->lizard);
46   bb_done(&buf->bb);
47   xfree(buf);
48 }
49
50 static inline byte *
51 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
52 {
53   if (can_overwrite >= 2)
54     while (ptr < end)
55     {
56       uns len;
57       GET_UTF8(ptr, len);
58       if (!len--)
59         break;
60       byte type = ptr[len];
61
62       ptr[len] = 0;
63       obj_add_attr_ref(o, type, ptr);
64
65       ptr += len + 1;
66     }
67   else
68     while (ptr < end)
69     {
70       uns len;
71       GET_UTF8(ptr, len);
72       if (!len--)
73         break;
74       byte type = ptr[len];
75
76       byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
77       memcpy(dup, ptr, len);
78       dup[len] = 0;
79       obj_add_attr_ref(o, type, dup);
80
81       ptr += len + 1;
82     }
83   return ptr;
84 }
85
86 struct odes *
87 obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
88 {
89   struct odes *o = obj_new(pool);
90
91   if (buck_type < BUCKET_TYPE_V33)
92   {
93     if (!body_start)                    // header + body: ignore empty lines, read until EOF
94     {
95       obj_read_multi(body, o);
96       bgetc(body);
97     }
98     else                                // header only: end on EOF or the first empty line
99     {
100       sh_off_t start = btell(body);
101       obj_read(body, o);
102       *body_start = btell(body) - start;
103     }
104   }
105   else
106   {
107     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
108     int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, -1);
109     /* FIXME: This could be cached in buck2obj_buf */
110     byte *ptr, *end;
111     uns len = bdirect_read_prepare(body, &ptr);
112     if (len < buck_len
113     || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
114     {
115       /* Copy if the original buffer is too small.
116        * If it is write-protected, copy it also if it is uncompressed.  */
117       bb_grow(&buf->bb, buck_len);
118       len = bread(body, buf->bb.ptr, buck_len);
119       ptr = buf->bb.ptr;
120     }
121     end = ptr + len;
122
123     byte *start = ptr;
124     ptr = decode_attributes(ptr, end, o, 0);            // header
125     if (body_start)
126     {
127       *body_start = ptr - start;
128       return o;
129     }
130     if (buck_type == BUCKET_TYPE_V33)
131       ;
132     else if (buck_type == BUCKET_TYPE_V33_LIZARD)       // decompression
133     {
134       len = GET_U32(ptr);
135       ptr += 4;
136       byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len);
137       if (!new_ptr)
138         return NULL;
139       ptr = new_ptr;
140       end = ptr + len;
141     }
142     else                                                // unknown bucket type
143       RET_ERR(EINVAL);
144     ptr = decode_attributes(ptr, end, o, 2);            // body
145
146     if (ptr != end)
147       RET_ERR(EINVAL);
148     /* If the bucket fit into the fastbuf buffer, can_overwrite == 2, and
149      * buck_type == BUCKET_TYPE_V33, then bflush(body) is needed before
150      * anything else than sequential read.  */
151   }
152   return o;
153 }