Skip to content

Commit

Permalink
update signature of vtenc_decode* API functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vteromero committed Feb 11, 2022
1 parent 0bbda5f commit 70a034a
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 258 deletions.
8 changes: 0 additions & 8 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
#include "internals.h"
#include "stack.h"

void vtenc_decoder_init(VtencDecoder *dec)
{
dec->allow_repeated_values = 1;
dec->skip_full_subtrees = 1;
dec->min_cluster_length = 1;
dec->last_error_code = VTENC_OK;
}

#define DEC_STACK_MAX_SIZE 64

struct dec_bit_cluster {
Expand Down
46 changes: 14 additions & 32 deletions decode_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the MIT License.
See LICENSE file in the project root for full license information.
*/
#include <assert.h>
#include <stddef.h>
#include <stdint.h>

Expand Down Expand Up @@ -33,20 +32,6 @@
#define vtenc_decode_(_width_) BITWIDTH_SUFFIX(vtenc_decode, _width_)
#define vtenc_decode vtenc_decode_(BITWIDTH)

#define dec_return_with_code(dec, code) \
do { \
(dec)->last_error_code = code; \
return; \
} while (0)

#define dec_return_on_error(dec, exp) \
do { \
const int code = (exp); \
if (code != VTENC_OK) { \
dec_return_with_code(dec, code); \
} \
} while (0)

struct decctx {
TYPE *values;
size_t values_len;
Expand All @@ -56,9 +41,8 @@ struct decctx {
struct bsreader bits_reader;
};

static int decctx_init(struct decctx *ctx,
const VtencDecoder *dec, const uint8_t *in, size_t in_len,
TYPE *out, size_t out_len)
static int decctx_init(struct decctx *ctx, const vtenc *dec,
const uint8_t *in, size_t in_len, TYPE *out, size_t out_len)
{
ctx->values = out;
ctx->values_len = out_len;
Expand All @@ -67,9 +51,10 @@ static int decctx_init(struct decctx *ctx,
* `skip_full_subtrees` parameter is only applicable to sets, i.e. sequences
* with no repeated values.
*/
ctx->reconstruct_full_subtrees = !dec->allow_repeated_values && dec->skip_full_subtrees;
ctx->reconstruct_full_subtrees = !dec->params.allow_repeated_values &&
dec->params.skip_full_subtrees;

ctx->min_cluster_length = dec->min_cluster_length;
ctx->min_cluster_length = dec->params.min_cluster_length;

dec_stack_init(&ctx->stack);

Expand Down Expand Up @@ -199,23 +184,20 @@ static int decode_bit_cluster_tree(struct decctx *ctx)
return VTENC_OK;
}

void vtenc_decode(VtencDecoder *dec, const uint8_t *in, size_t in_len,
TYPE *out, size_t out_len)
int vtenc_decode(vtenc *dec, const uint8_t *in, size_t in_len, TYPE *out, size_t out_len)
{
int rc;
struct decctx ctx;
uint64_t max_values = dec->allow_repeated_values ? LIST_MAX_VALUES : SET_MAX_VALUES;
uint64_t max_values = dec->params.allow_repeated_values ? LIST_MAX_VALUES : SET_MAX_VALUES;

dec->last_error_code = VTENC_OK;
if ((uint64_t)out_len > max_values)
return VTENC_ERR_OUTPUT_TOO_BIG;

dec_return_on_error(dec,
decctx_init(&ctx, dec, in, in_len, out, out_len)
);

if ((uint64_t)out_len > max_values) {
dec_return_with_code(dec, VTENC_ERR_OUTPUT_TOO_BIG);
}
rc = decctx_init(&ctx, dec, in, in_len, out, out_len);
if (rc != VTENC_OK)
return rc;

memset(out, 0, out_len * sizeof(*out));

dec_return_on_error(dec, decode_bit_cluster_tree(&ctx));
return decode_bit_cluster_tree(&ctx);
}
35 changes: 23 additions & 12 deletions tests/encdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,35 +134,46 @@ int encdec_encode(struct EncDec *encdec, const void *in, size_t in_len)

int encdec_decode(struct EncDec *encdec)
{
VtencDecoder decoder = {
.allow_repeated_values = encdec->allow_repeated_values,
.skip_full_subtrees = encdec->skip_full_subtrees,
.min_cluster_length = encdec->min_cluster_length
};
int rc, res=1;
vtenc *decoder = vtenc_create();

if (decoder == NULL) {
fprintf(stderr, "failed to create the decoder\n");
return 0;
}

vtenc_config(decoder, VTENC_CONFIG_ALLOW_REPEATED_VALUES, encdec->allow_repeated_values);
vtenc_config(decoder, VTENC_CONFIG_SKIP_FULL_SUBTREES, encdec->skip_full_subtrees);
vtenc_config(decoder, VTENC_CONFIG_MIN_CLUSTER_LENGTH, encdec->min_cluster_length);

encdec->ctx.dec_out_len = encdec->ctx.in_len;

encdec->ctx.dec_out = malloc(encdec->ctx.dec_out_len * encdec->funcs->type_size());

if (encdec->ctx.dec_out == NULL) {
fprintf(stderr, "allocation error\n");
return 0;
res = 0;
goto destroy_and_return;
}

encdec->funcs->decode(
&decoder,
rc = encdec->funcs->decode(
decoder,
encdec->ctx.enc_out,
encdec->ctx.enc_out_len,
encdec->ctx.dec_out,
encdec->ctx.dec_out_len
);

if (decoder.last_error_code != VTENC_OK) {
fprintf(stderr, "decode failed with code: %d\n", decoder.last_error_code);
return 0;
if (rc != VTENC_OK) {
fprintf(stderr, "decode failed with code: %d\n", rc);
res = 0;
goto destroy_and_return;
}

return 1;
destroy_and_return:
vtenc_destroy(decoder);

return res;
}

int encdec_check_equality(struct EncDec *encdec)
Expand Down
2 changes: 1 addition & 1 deletion tests/encdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
typedef size_t (*type_size_func_t)();
typedef size_t (*max_encoded_size_func_t)(size_t);
typedef int (*encode_func_t)(vtenc *, const void *, size_t, uint8_t *, size_t);
typedef void (*decode_func_t)(VtencDecoder *, const uint8_t *, size_t, void *, size_t);
typedef int (*decode_func_t)(vtenc *, const uint8_t *, size_t, void *, size_t);

struct EncDecFuncs {
type_size_func_t type_size;
Expand Down
Loading

0 comments on commit 70a034a

Please sign in to comment.