]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
deleted obj_attr_to_bucket*(). sorry, I like them, however I have already
[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 if (can_overwrite == 1)
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       ptr[len] = 0;
85       obj_add_attr(o, type, ptr);
86       ptr[len] = type;
87
88       ptr += len + 1;
89     }
90   else
91     while (ptr < end)
92     {
93       uns len;
94       GET_UTF8(ptr, len);
95       if (!len--)
96         break;
97       byte type = ptr[len];
98
99       byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
100       memcpy(dup, ptr, len);
101       dup[len] = 0;
102       obj_add_attr_ref(o, type, dup);
103
104       ptr += len + 1;
105     }
106   return ptr;
107 }
108
109 struct odes *
110 obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
111 {
112   struct odes *o = obj_new(buf->mp);
113
114   if (buck_type < BUCKET_TYPE_V33)
115   {
116     if (!body_start)                    // header + body: ignore empty lines, read until EOF
117     {
118       obj_read_multi(body, o);
119       bgetc(body);
120     }
121     else                                // header only: end on EOF or the first empty line
122     {
123       sh_off_t start = btell(body);
124       obj_read(body, o);
125       *body_start = btell(body) - start;
126     }
127   }
128   else
129   {
130     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
131     int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, -1);
132     /* FIXME: This could be cached in buck2obj_buf */
133     if (can_overwrite < 0)
134       can_overwrite = 0;
135     uns overwritten;
136     byte *ptr, *end;
137     uns len = bdirect_read_prepare(body, &ptr);
138     if (len < buck_len
139     || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
140     {
141       /* Copy if the original buffer is too small.
142        * If it is write-protected, copy it also if it is uncompressed.  */
143       bb_grow(&buf->bb, buck_len);
144       len = bread(body, buf->bb.ptr, buck_len);
145       ptr = buf->bb.ptr;
146       can_overwrite = 2;
147       overwritten = 0;
148     }
149     else
150       overwritten = can_overwrite > 1;
151     end = ptr + len;
152
153     byte *start = ptr;
154     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
155     if (body_start)
156     {
157       *body_start = ptr - start;
158       return o;
159     }
160     if (buck_type == BUCKET_TYPE_V33)
161       ;
162     else if (buck_type == BUCKET_TYPE_V33_LIZARD)       // decompression
163     {
164       len = GET_U32(ptr);
165       ptr += 4;
166       int res;
167       byte *new_ptr;
168       res = lizard_decompress_safe(ptr, buf->lizard, len, &new_ptr);
169       if (res != (int) len)
170       {
171         if (res >= 0)
172           errno = EINVAL;
173         return NULL;
174       }
175       ptr = new_ptr;
176       end = ptr + len;
177       can_overwrite = 2;
178     }
179     else                                                // unknown bucket type
180       RET_ERR(EINVAL);
181     ASSERT(can_overwrite == 2);                         // because of the policy and decompression
182     ptr = decode_attributes(ptr, end, o, 2);            // body
183
184     if (ptr != end)
185       RET_ERR(EINVAL);
186     /* If (overwritten), bflush(body) might be needed.  */
187   }
188   return o;
189 }