]> mj.ucw.cz Git - libucw.git/blob - lib/buck2obj.c
cannot ask for struct obuck_header due to remote indexing
[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   while (ptr < end)
55   {
56     uns len;
57     GET_UTF8(ptr, len);
58     if (!len--)
59       break;
60     byte type = ptr[len];
61     if (can_overwrite == 2)
62     {
63       ptr[len] = 0;
64       obj_add_attr_ref(o, type, ptr);
65     }
66     else if (can_overwrite == 1)
67     {
68       ptr[len] = 0;
69       obj_add_attr(o, type, ptr);
70       ptr[len] = type;
71     }
72     else
73     {
74       byte *dup = mp_alloc(o->pool, len+1);
75       memcpy(dup, ptr, len);
76       dup[len] = 0;
77       obj_add_attr_ref(o, type, ptr);
78     }
79     ptr += len + 1;
80   }
81   return ptr;
82 }
83
84 struct odes *
85 buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
86 {
87   mp_flush(buf->mp);
88   struct odes *o = obj_new(buf->mp);
89
90   if (buck_type < BUCKET_TYPE_V33)
91     obj_read_multi(body, o);
92   else
93   {
94     /* Compute the length of the bucket.  We cannot fetch this attribute
95      * directly due to remote indexing.  */
96     bseek(body, 0, SEEK_END);
97     sh_off_t buck_len = btell(body);
98     bsetpos(body, 0);
99
100     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
101     int can_overwrite = MAX(bconfig(body, BCONFIG_CAN_OVERWRITE, 0), 0);
102     uns overwritten;
103     byte *ptr, *end;
104     uns len = bdirect_read_prepare(body, &ptr);
105     if (len < buck_len
106     || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
107     {
108       /* Copy if the original buffer is too small.
109        * If it is write-protected, copy it also if it is uncompressed.  */
110       if (buck_len > buf->raw_len)
111         RET_ERR(EFBIG);
112       len = bread(body, buf->raw, buck_len);
113       ptr = buf->raw;
114       can_overwrite = 2;
115       overwritten = 0;
116     }
117     else
118       overwritten = can_overwrite > 1;
119     end = ptr + len;
120
121     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
122     if (buck_type == BUCKET_TYPE_V33)
123       ;
124     else if (buck_type == BUCKET_TYPE_V33_LIZARD)       // decompression
125     {
126       len = GET_U32(ptr);
127       ptr += 4;
128       int res = lizard_decompress_safe(ptr, buf->lizard, len);
129       if (res != (int) len)
130       {
131         if (res >= 0)
132           errno = EINVAL;
133         return NULL;
134       }
135       ptr = buf->lizard->ptr;
136       end = ptr + len;
137       can_overwrite = 2;
138     }
139     else                                                // unknown bucket type
140       RET_ERR(EINVAL);
141     ASSERT(can_overwrite == 2);                         // because of the policy and decompression
142     ptr = decode_attributes(ptr, end, o, 2);            // body
143
144     if (ptr != end)
145       RET_ERR(EINVAL);
146     if (overwritten)
147       bflush(body);
148   }
149   return o;
150 }