Skip to content

Commit

Permalink
Fix issue 293 by handling default value with type ATV_BITVECTOR
Browse files Browse the repository at this point in the history
1. Modify asn1p_y.c and asn1p_y.y to fix an error that
   function _convert_bitstring2binary() always return NULL.

2. Add handling for default value with type ATV_BITVECTOR in
   function try_inline_default().
  • Loading branch information
brchiu committed Sep 30, 2018
1 parent 4cc779f commit 83c3166
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
50 changes: 41 additions & 9 deletions libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -2321,11 +2321,26 @@ emit_default_string_value(arg_t *arg, asn1p_value_t *v) {
uint8_t *e = v->value.string.size + b;
OUT("{ ");
for(;b < e; b++)
OUT("0x%02x, ", *b);
OUT("0x%02X, ", *b);
OUT("0 };\n");
}
}

static void
emit_default_bitstring_value(arg_t *arg, asn1p_value_t *v) {

OUT("static const uint8_t defv[] = { ");
assert(v->type == ATV_BITVECTOR);

uint8_t *b = v->value.binary_vector.bits;
for (int i = 0; i < (v->value.binary_vector.size_in_bits + 7)/8; i++, b++) {
OUT("0x%02X", *b);
if(i < (v->value.binary_vector.size_in_bits + 7)/8 - 1)
OUT(", ");
}
OUT(" };\n");
}

static int
try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
int save_target = arg->target->target;
Expand Down Expand Up @@ -2421,9 +2436,9 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
//expr->marker.flags &= ~EM_INDIRECT;
return 0;
default:
if(etype & ASN_STRING_KM_MASK) {
if(expr->marker.default_value == NULL
|| expr->marker.default_value->type != ATV_STRING)
if(expr->marker.default_value) {
if((expr->marker.default_value->type != ATV_STRING) &&
(expr->marker.default_value->type != ATV_BITVECTOR))
break;
if(!out) {
if(C99_MODE) OUT(".default_value_cmp = ");
Expand All @@ -2441,15 +2456,23 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
OUT("static int asn_DFL_%d_cmp(const void *sptr) {\n",
expr->_type_unique_index);
INDENT(+1);
emit_default_string_value(arg, expr->marker.default_value);
if(expr->marker.default_value->type == ATV_STRING)
emit_default_string_value(arg, expr->marker.default_value);
else
emit_default_bitstring_value(arg, expr->marker.default_value);
OUT("const %s *st = sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
OUT("\n");
OUT("if(!st) {\n");
OUT("\treturn -1; /* No value is not a default value */\n");
OUT("}\n");
OUT("\n");
OUT("if(st->size == (sizeof(defv) - 1)\n");
OUT("&& memcmp(st->buf, &defv, sizeof(defv) - 1) == 0)\n");
if(expr->marker.default_value->type == ATV_STRING) {
OUT("if(st->size == (sizeof(defv) - 1)\n");
OUT("&& memcmp(st->buf, &defv, sizeof(defv) - 1) == 0)\n");
} else {
OUT("if(st->size == (sizeof(defv))\n");
OUT("&& memcmp(st->buf, &defv, sizeof(defv)) == 0)\n");
}
OUT("\treturn 0;\n");
OUT("return 1;\n");
INDENT(-1);
Expand All @@ -2458,7 +2481,10 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
OUT("static int asn_DFL_%d_set(void **sptr) {\n",
expr->_type_unique_index);
INDENT(+1);
emit_default_string_value(arg, expr->marker.default_value);
if(expr->marker.default_value->type == ATV_STRING)
emit_default_string_value(arg, expr->marker.default_value);
else
emit_default_bitstring_value(arg, expr->marker.default_value);
OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
OUT("uint8_t *nstr = MALLOC(sizeof(defv));\n");
OUT("\n");
Expand All @@ -2472,7 +2498,13 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
OUT("\tif(!st) { FREEMEM(nstr); return -1; }\n");
OUT("}\n");
OUT("st->buf = nstr;\n");
OUT("st->size = sizeof(defv) - 1;\n");
if(expr->marker.default_value->type == ATV_STRING) {
OUT("st->size = sizeof(defv) - 1;\n");
} else {
OUT("st->size = sizeof(defv);\n");
if(etype == ASN_BASIC_BIT_STRING)
OUT("st->bits_unused = %d;\n", (8 - expr->marker.default_value->value.binary_vector.size_in_bits % 8) % 8);
}
OUT("\n");
OUT("return 0;\n");
INDENT(-1);
Expand Down
3 changes: 2 additions & 1 deletion libasn1parser/asn1p_y.c
Original file line number Diff line number Diff line change
Expand Up @@ -5208,9 +5208,10 @@ _convert_bitstring2binary(char *str, int base) {
memlen = slen / (8 / baselen); /* Conservative estimate */

bv_ptr = binary_vector = malloc(memlen + 1);
if(bv_ptr == NULL)
if(bv_ptr == NULL) {
/* ENOMEM */
return NULL;
}

cur_val = 0;
bits = 0;
Expand Down
3 changes: 2 additions & 1 deletion libasn1parser/asn1p_y.y
Original file line number Diff line number Diff line change
Expand Up @@ -2524,9 +2524,10 @@ _convert_bitstring2binary(char *str, int base) {
memlen = slen / (8 / baselen); /* Conservative estimate */

bv_ptr = binary_vector = malloc(memlen + 1);
if(bv_ptr == NULL)
if(bv_ptr == NULL) {
/* ENOMEM */
return NULL;
}

cur_val = 0;
bits = 0;
Expand Down

0 comments on commit 83c3166

Please sign in to comment.