Skip to content

Commit

Permalink
Applied @brchiu partial fix for vlm#293 (still needs more testing, and a
Browse files Browse the repository at this point in the history
similar fix for OCTET STRING)
  • Loading branch information
mouse07410 committed Sep 24, 2018
1 parent da3c61c commit 6306aaf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
49 changes: 41 additions & 8 deletions libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -2386,6 +2386,21 @@ emit_default_string_value(arg_t *arg, asn1p_value_t *v) {
}
}

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 @@ -2481,9 +2496,10 @@ 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(!(etype & ASN_STRING_KM_MASK) &&
(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 @@ -2501,15 +2517,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 @@ -2518,7 +2542,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 @@ -2532,7 +2559,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.y
Original file line number Diff line number Diff line change
Expand Up @@ -2525,9 +2525,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 6306aaf

Please sign in to comment.