]> mj.ucw.cz Git - moe.git/blob - sherlock/obj-linear.c
Moved everything related to MO-P to mop/. This cleans up bin/.
[moe.git] / sherlock / obj-linear.c
1 /*
2  *      Sherlock Library -- Linear Representation of Objects
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  *      (c) 2005 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "sherlock/sherlock.h"
12 #include "lib/fastbuf.h"
13 #include "lib/unaligned.h"
14 #include "lib/lizard.h"
15 #include "sherlock/object.h"
16 #include "sherlock/lizard-fb.h"
17
18 byte *
19 obj_linearize(struct odes *d, uns min_compress, uns *plen)
20 {
21   // Create uncompressed linearization
22   put_attr_set_type(BUCKET_TYPE_V33);
23   uns size = size_object(d);
24   byte *out = xmalloc(size+LIZARD_COMPRESS_HEADER + LIZARD_NEEDS_CHARS) + LIZARD_COMPRESS_HEADER;
25   byte *t = put_object(out, d);
26   ASSERT(t == out+size);
27
28   struct lizard_block_req req = {
29     .type = BUCKET_TYPE_V33_LIZARD,
30     .ratio = min_compress / 100.,
31     .in_ptr = out,
32     .in_len = size,
33     .out_ptr = NULL,
34     .out_len = 0,
35   };
36   // Allocate a buffer for compressed data
37   int res = lizard_compress_req(&req);
38   ASSERT(res <= 0);
39   byte *buf = res<0 ? req.out_ptr=xmalloc(req.out_len+=LIZARD_COMPRESS_HEADER) : NULL;
40   res = lizard_compress_req_header(&req, 1);
41   ASSERT(res > 0);
42   if (req.out_ptr != out-LIZARD_COMPRESS_HEADER)
43     xfree(out-LIZARD_COMPRESS_HEADER);
44   else if (buf)
45     xfree(buf);
46
47   *plen = req.out_len;
48   return req.out_ptr;
49 }
50
51 struct odes *
52 obj_delinearize(struct buck2obj_buf *bbuf, struct mempool *mp, byte *buf, uns len, uns destructive)
53 {
54   struct odes *o = obj_new(mp);
55   ASSERT(len >= LIZARD_COMPRESS_HEADER);
56   uns buck_type = buf[0] + BUCKET_TYPE_PLAIN;
57
58   struct fastbuf fb;
59   uns sh = LIZARD_COMPRESS_HEADER - 1;
60   fbbuf_init_read(&fb, buf+sh, len-sh, destructive);
61   if (buck2obj_parse(bbuf, buck_type, len-sh, &fb, NULL, NULL, o, 1) < 0)
62     return NULL;
63   else
64     return o;
65 }