Commit fa5a147e authored by Bi-Ruei, Chiu's avatar Bi-Ruei, Chiu

Fix issue 293 by handling default value with type ATV_BITVECTOR

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().
parent 3d76a039
...@@ -2390,11 +2390,26 @@ emit_default_string_value(arg_t *arg, asn1p_value_t *v) { ...@@ -2390,11 +2390,26 @@ emit_default_string_value(arg_t *arg, asn1p_value_t *v) {
uint8_t *e = v->value.string.size + b; uint8_t *e = v->value.string.size + b;
OUT("{ "); OUT("{ ");
for(;b < e; b++) for(;b < e; b++)
OUT("0x%02x, ", *b); OUT("0x%02X, ", *b);
OUT("0 };\n"); 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 static int
try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) { try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
int save_target = arg->target->target; int save_target = arg->target->target;
...@@ -2490,9 +2505,9 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) { ...@@ -2490,9 +2505,9 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
//expr->marker.flags &= ~EM_INDIRECT; //expr->marker.flags &= ~EM_INDIRECT;
return 0; return 0;
default: default:
if(etype & ASN_STRING_KM_MASK) { if(expr->marker.default_value) {
if(expr->marker.default_value == NULL if((expr->marker.default_value->type != ATV_STRING) &&
|| expr->marker.default_value->type != ATV_STRING) (expr->marker.default_value->type != ATV_BITVECTOR))
break; break;
if(!out) { if(!out) {
if(C99_MODE) OUT(".default_value_cmp = "); if(C99_MODE) OUT(".default_value_cmp = ");
...@@ -2510,15 +2525,23 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) { ...@@ -2510,15 +2525,23 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
OUT("static int asn_DFL_%d_cmp(const void *sptr) {\n", OUT("static int asn_DFL_%d_cmp(const void *sptr) {\n",
expr->_type_unique_index); expr->_type_unique_index);
INDENT(+1); 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("const %s *st = sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
OUT("\n"); OUT("\n");
OUT("if(!st) {\n"); OUT("if(!st) {\n");
OUT("\treturn -1; /* No value is not a default value */\n"); OUT("\treturn -1; /* No value is not a default value */\n");
OUT("}\n"); OUT("}\n");
OUT("\n"); OUT("\n");
OUT("if(st->size == (sizeof(defv) - 1)\n"); if(expr->marker.default_value->type == ATV_STRING) {
OUT("&& memcmp(st->buf, &defv, sizeof(defv) - 1) == 0)\n"); 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("\treturn 0;\n");
OUT("return 1;\n"); OUT("return 1;\n");
INDENT(-1); INDENT(-1);
...@@ -2527,7 +2550,10 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) { ...@@ -2527,7 +2550,10 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
OUT("static int asn_DFL_%d_set(void **sptr) {\n", OUT("static int asn_DFL_%d_set(void **sptr) {\n",
expr->_type_unique_index); expr->_type_unique_index);
INDENT(+1); 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("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
OUT("uint8_t *nstr = MALLOC(sizeof(defv));\n"); OUT("uint8_t *nstr = MALLOC(sizeof(defv));\n");
OUT("\n"); OUT("\n");
...@@ -2541,7 +2567,13 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) { ...@@ -2541,7 +2567,13 @@ try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
OUT("\tif(!st) { FREEMEM(nstr); return -1; }\n"); OUT("\tif(!st) { FREEMEM(nstr); return -1; }\n");
OUT("}\n"); OUT("}\n");
OUT("st->buf = nstr;\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("\n");
OUT("return 0;\n"); OUT("return 0;\n");
INDENT(-1); INDENT(-1);
......
...@@ -5227,9 +5227,10 @@ _convert_bitstring2binary(char *str, int base) { ...@@ -5227,9 +5227,10 @@ _convert_bitstring2binary(char *str, int base) {
memlen = slen / (8 / baselen); /* Conservative estimate */ memlen = slen / (8 / baselen); /* Conservative estimate */
bv_ptr = binary_vector = malloc(memlen + 1); bv_ptr = binary_vector = malloc(memlen + 1);
if(bv_ptr == NULL) if(bv_ptr == NULL) {
/* ENOMEM */ /* ENOMEM */
return NULL; return NULL;
}
cur_val = 0; cur_val = 0;
bits = 0; bits = 0;
......
...@@ -2533,9 +2533,10 @@ _convert_bitstring2binary(char *str, int base) { ...@@ -2533,9 +2533,10 @@ _convert_bitstring2binary(char *str, int base) {
memlen = slen / (8 / baselen); /* Conservative estimate */ memlen = slen / (8 / baselen); /* Conservative estimate */
bv_ptr = binary_vector = malloc(memlen + 1); bv_ptr = binary_vector = malloc(memlen + 1);
if(bv_ptr == NULL) if(bv_ptr == NULL) {
/* ENOMEM */ /* ENOMEM */
return NULL; return NULL;
}
cur_val = 0; cur_val = 0;
bits = 0; bits = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment