]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
b2eb455ee165537e70bb16fbc28172f726c4f719
[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 MAX_HEADER_SIZE 1024            // extra space for the header not counted in MaxObjSize
22 #define RET_ERR(num)    ({ errno = num; return NULL; })
23
24 struct buck2obj_buf
25 {
26   byte *raw;
27   uns raw_len;
28   struct lizard_buffer *lizard;
29   struct mempool *mp;
30 };
31
32 struct buck2obj_buf *
33 buck2obj_alloc(uns max_len, struct mempool *mp)
34 {
35   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
36   buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
37   buf->raw = xmalloc(buf->raw_len);
38   buf->lizard = lizard_alloc(max_len);
39   buf->mp = mp;
40   return buf;
41 }
42
43 void
44 buck2obj_free(struct buck2obj_buf *buf)
45 {
46   lizard_free(buf->lizard);
47   xfree(buf->raw);
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 if (can_overwrite == 1)
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       ptr[len] = 0;
78       obj_add_attr(o, type, ptr);
79       ptr[len] = type;
80
81       ptr += len + 1;
82     }
83   else
84     while (ptr < end)
85     {
86       uns len;
87       GET_UTF8(ptr, len);
88       if (!len--)
89         break;
90       byte type = ptr[len];
91
92       byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
93       memcpy(dup, ptr, len);
94       dup[len] = 0;
95       obj_add_attr_ref(o, type, dup);
96
97       ptr += len + 1;
98     }
99   return ptr;
100 }
101
102 struct odes *
103 buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
104 {
105   mp_flush(buf->mp);
106   struct odes *o = obj_new(buf->mp);
107
108   if (buck_type < BUCKET_TYPE_V33)
109     obj_read_multi(body, o);
110   else
111   {
112     /* Compute the length of the bucket.  We cannot fetch this attribute
113      * directly due to remote indexing.  */
114     bseek(body, 0, SEEK_END);
115     sh_off_t buck_len = btell(body);
116     bsetpos(body, 0);
117
118     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
119     int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
120     if (can_overwrite < 0)
121       can_overwrite = 0;
122     uns overwritten;
123     byte *ptr, *end;
124     uns len = bdirect_read_prepare(body, &ptr);
125     if (len < buck_len
126     || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
127     {
128       /* Copy if the original buffer is too small.
129        * If it is write-protected, copy it also if it is uncompressed.  */
130       if (buck_len > buf->raw_len)
131         RET_ERR(EFBIG);
132       len = bread(body, buf->raw, buck_len);
133       ptr = buf->raw;
134       can_overwrite = 2;
135       overwritten = 0;
136     }
137     else
138       overwritten = can_overwrite > 1;
139     end = ptr + len;
140
141     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
142     if (buck_type == BUCKET_TYPE_V33)
143       ;
144     else if (buck_type == BUCKET_TYPE_V33_LIZARD)       // decompression
145     {
146       len = GET_U32(ptr);
147       ptr += 4;
148       int res = lizard_decompress_safe(ptr, buf->lizard, len);
149       if (res != (int) len)
150       {
151         if (res >= 0)
152           errno = EINVAL;
153         return NULL;
154       }
155       ptr = buf->lizard->ptr;
156       end = ptr + len;
157       can_overwrite = 2;
158     }
159     else                                                // unknown bucket type
160       RET_ERR(EINVAL);
161     ASSERT(can_overwrite == 2);                         // because of the policy and decompression
162     ptr = decode_attributes(ptr, end, o, 2);            // body
163
164     if (ptr != end)
165       RET_ERR(EINVAL);
166     /* If (overwritten), bflush(body) might be needed.  */
167   }
168   return o;
169 }