From 068b8926a1b92e1e05f8e12446d2ab27fc98de6f Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Fri, 25 Jun 2004 11:28:40 +0000 Subject: [PATCH] modifications done at home, not yet incorporated MJ's objections: - added buck2obj_{alloc,free}() - extract_odes() renamed to buck2obj_convert() and simplified its interface. it flushes the memory pool and calls obj_new() as used e.g. in scanner.c - attribute lengths are stored incremented s.t. 0-lengths are allowed - the length of the compressed part is stored as U32 instead of UTF8 to allow 0-copy compression - temporary usage of object.c's oa_allocate removed and we call obj_add_attr_ref() instead of obj_add_attr() - renamed constants BUCKET_TYPE_* - defined internal macro RET_ERR() --- lib/buck2obj.c | 91 ++++++++++++++++++++++++++++++++------------------ lib/buck2obj.h | 6 +++- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/lib/buck2obj.c b/lib/buck2obj.c index ae3f1830..ef53de2a 100644 --- a/lib/buck2obj.c +++ b/lib/buck2obj.c @@ -5,6 +5,8 @@ */ #include "lib/lib.h" +#include "lib/unaligned.h" +#include "lib/pools.h" #include "lib/fastbuf.h" #include "charset/unicode.h" #include "lib/object.h" @@ -12,76 +14,101 @@ #include "lib/lizard.h" #include "lib/buck2obj.h" +#include #include +#define MAX_HEADER_SIZE 1024 // extra space for the header not countet to MaxObjSize + +struct buck2obj_buf +{ + byte *raw; + uns raw_len; + struct lizard_buffer *lizard; + struct mempool *mp; +}; + +struct buck2obj_buf * +buck2obj_alloc(uns max_len, struct mempool *mp) +{ + struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf)); + buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE; + buf->raw = xmalloc(buf->raw_len); + buf->lizard = lizard_alloc(max_len); + buf->mp = mp; + return buf; +} + +void +buck2obj_free(struct buck2obj_buf *buf) +{ + lizard_free(buf->lizard); + xfree(buf->raw); + xfree(buf); +} + static inline byte * decode_attributes(byte *ptr, byte *end, struct odes *o) { - /* FIXME: this forbids storing attributes with empty string as a value. - * Verify whether it is used or not. */ while (ptr < end) { uns len; GET_UTF8(ptr, len); - if (!len) + if (!len--) break; byte type = ptr[len]; ptr[len] = 0; - obj_add_attr(o, type, ptr); + obj_add_attr_ref(o, type, ptr); ptr += len + 1; } return ptr; } -int -extract_odes(struct obuck_header *hdr, struct fastbuf *body, struct odes *o, byte *buf, uns buf_len, struct lizard_buffer *lizard_buf) +#define RET_ERR(num) ({ errno = num; return NULL; }) +struct odes * +buck2obj_convert(struct buck2obj_buf *buf, struct obuck_header *hdr, struct fastbuf *body) { - if (hdr->type < BUCKET_TYPE_V30C) - { - oa_allocate = 1; + mp_flush(buf->mp); + struct odes *o = obj_new(buf->mp); + + if (hdr->type < BUCKET_TYPE_V33) obj_read_multi(body, o); - } else { - oa_allocate = 0; - /* Read all the bucket into 1 buffer, 0-copy if possible. */ byte *ptr, *end; uns len = bdirect_read_prepare(body, &ptr); // WARNING: must NOT use mmaped-I/O if (len < hdr->length) { - if (hdr->length > buf_len) - { - errno = EFBIG; - return -1; - } - len = bread(body, buf, hdr->length); - ptr = buf; + if (hdr->length > buf->raw_len) + RET_ERR(EFBIG); + len = bread(body, buf->raw, hdr->length); + ptr = buf->raw; } end = ptr + len; ptr = decode_attributes(ptr, end, o); // header - if (hdr->type == BUCKET_TYPE_V30C) // decompression + if (hdr->type == BUCKET_TYPE_V33) + ; + else if (hdr->type == BUCKET_TYPE_V33_LIZARD) // decompression { - GET_UTF8(ptr, len); - int res = lizard_decompress_safe(ptr, lizard_buf, len); + len = GET_U32(ptr); + ptr += 4; + int res = lizard_decompress_safe(ptr, buf->lizard, len); if (res != (int) len) { - if (res < 0) - return res; - errno = EINVAL; - return -1; + if (res >= 0) + errno = EINVAL; + return NULL; } - ptr = lizard_buf->ptr; + ptr = buf->lizard->ptr; end = ptr + len; } + else // unknown bucket type + RET_ERR(EINVAL); ptr = decode_attributes(ptr, end, o); // body if (ptr != end) - { - errno = EINVAL; - return -1; - } + RET_ERR(EINVAL); } - return 0; + return o; } diff --git a/lib/buck2obj.h b/lib/buck2obj.h index 9e272079..85e277f3 100644 --- a/lib/buck2obj.h +++ b/lib/buck2obj.h @@ -4,4 +4,8 @@ * (c) 2004, Robert Spalek */ -int extract_odes(struct obuck_header *hdr, struct fastbuf *body, struct odes *o, byte *buf, uns buf_len, struct lizard_buffer *lizard_buf); +struct buck2obj_buf; + +struct buck2obj_buf *buck2obj_alloc(uns max_len, struct mempool *mp); +void buck2obj_free(struct buck2obj_buf *buf); +struct odes *buck2obj_convert(struct buck2obj_buf *buf, struct obuck_header *hdr, struct fastbuf *body); -- 2.39.2