From 271d2a02e7b798fd9781018c060d49e47f277cba Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Sun, 4 Jul 2004 11:29:17 +0000 Subject: [PATCH] - obj_parse_dump() generalized to store apart the header and the body, and renamed to buck2obj_parse() - added new obj_parse_dump() with the same interface as before; it is a wrapper of buck2obj_parse() --- lib/buck2obj.c | 50 +++++++++++++++++++++++++++++++------------------- lib/buck2obj.h | 3 +++ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/buck2obj.c b/lib/buck2obj.c index b87326bb..b19963f7 100644 --- a/lib/buck2obj.c +++ b/lib/buck2obj.c @@ -19,7 +19,7 @@ #include #include -#define RET_ERR(num) ({ errno = num; return NULL; }) +#define RET_ERR(num) ({ errno = num; return -1; }) #define GBUF_TYPE byte #define GBUF_PREFIX(x) bb_##x @@ -84,24 +84,26 @@ decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite) return ptr; } -struct odes * -obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start) +int +buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, struct odes *o_hdr, uns *body_start, struct odes *o_body) { - struct odes *o = obj_new(pool); - - if (buck_type == BUCKET_TYPE_PLAIN || buck_type == BUCKET_TYPE_V30) + if (buck_type == BUCKET_TYPE_PLAIN) + { + if (body_start) + *body_start = 0; + obj_read_multi(body, o_hdr); // ignore empty lines, read until EOF or NUL + } + else if (buck_type == BUCKET_TYPE_V30) { - if (!body_start) // header + body: ignore empty lines, read until EOF or NUL + sh_off_t start = btell(body); + obj_read(body, o_hdr); // end on EOF or the first empty line + if (body_start) + *body_start = btell(body) - start; + else { - obj_read_multi(body, o); + obj_read(body, o_body); bgetc(body); } - else // header only: end on EOF or the first empty line - { - sh_off_t start = btell(body); - obj_read(body, o); - *body_start = btell(body) - start; - } } else if (buck_type == BUCKET_TYPE_V33 || buck_type == BUCKET_TYPE_V33_LIZARD) { @@ -122,13 +124,13 @@ obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, u end = ptr + len; byte *start = ptr; - ptr = decode_attributes(ptr, end, o, 0); // header + ptr = decode_attributes(ptr, end, o_hdr, 0); // header if (body_start) { *body_start = ptr - start; if (!copied) bdirect_read_commit(body, ptr); - return o; + return 0; } if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression { @@ -138,14 +140,14 @@ obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, u ptr += 4; byte *new_ptr = lizard_decompress_safe(ptr, buf->lizard, len); if (!new_ptr) - return NULL; + return -1; if (!copied) bdirect_read_commit(body, end); ptr = new_ptr; end = ptr + len; copied = 1; } - ptr = decode_attributes(ptr, end, o, 2); // body + ptr = decode_attributes(ptr, end, o_body, 2); // body if (ptr != end) RET_ERR(EINVAL); if (!copied) @@ -153,5 +155,15 @@ obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, u } else RET_ERR(EINVAL); - return o; + return 0; +} + +struct odes * +obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start) +{ + struct odes *o = obj_new(pool); + if (buck2obj_parse(buf, buck_type, buck_len, body, o, body_start, o) < 0) + return NULL; + else + return o; } diff --git a/lib/buck2obj.h b/lib/buck2obj.h index 4a12ea61..6e11c3a8 100644 --- a/lib/buck2obj.h +++ b/lib/buck2obj.h @@ -6,9 +6,12 @@ struct buck2obj_buf; struct mempool; +struct odes; struct buck2obj_buf *buck2obj_alloc(void); void buck2obj_free(struct buck2obj_buf *buf); + +int buck2obj_parse(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, struct odes *o_hdr, uns *body_start, struct odes *o_body); struct odes *obj_read_bucket(struct buck2obj_buf *buf, struct mempool *pool, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start); /* If body_start != NULL, then only the header is parsed and *body_start is * set to the position of the body. This function does a plenty of optimizations -- 2.39.2