Skip to content
Snippets Groups Projects
Commit 8e8078aa authored by Lev Walkin's avatar Lev Walkin
Browse files

clean-up; removed unnecessary field from type descriptor

parent 9821098d
No related branches found
No related tags found
No related merge requests found
Showing
with 114 additions and 279 deletions
...@@ -18,9 +18,8 @@ asn1_TYPE_descriptor_t asn1_DEF_ANY = { ...@@ -18,9 +18,8 @@ asn1_TYPE_descriptor_t asn1_DEF_ANY = {
ANY_encode_xer, ANY_encode_xer,
0, /* Use generic outmost tag fetcher */ 0, /* Use generic outmost tag fetcher */
0, 0, 0, 0, 0, 0, 0, 0,
-1, /* Both ways are fine (primitive and constructed) */
0, 0, /* No members */ 0, 0, /* No members */
(void *)1 /* Special indicator that this is an ANY type */ (void *)2 /* Special indicator that this is an ANY type */
}; };
...@@ -139,7 +138,7 @@ static int ANY__consume_bytes(const void *buffer, size_t size, void *key) { ...@@ -139,7 +138,7 @@ static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
size_t nsize = (arg->size ? arg->size << 2 : 16) + size; size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
void *p = REALLOC(arg->buffer, nsize); void *p = REALLOC(arg->buffer, nsize);
if(!p) return -1; if(!p) return -1;
(void *)arg->buffer = p; arg->buffer = (uint8_t *)p;
arg->size = nsize; arg->size = nsize;
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
#include <asn_internal.h> #include <asn_internal.h>
#include <BIT_STRING.h> #include <BIT_STRING.h>
#include <asn_internal.h>
/* /*
* BIT STRING basic type description. * BIT STRING basic type description.
...@@ -27,9 +28,8 @@ asn1_TYPE_descriptor_t asn1_DEF_BIT_STRING = { ...@@ -27,9 +28,8 @@ asn1_TYPE_descriptor_t asn1_DEF_BIT_STRING = {
asn1_DEF_BIT_STRING_tags, /* Same as above */ asn1_DEF_BIT_STRING_tags, /* Same as above */
sizeof(asn1_DEF_BIT_STRING_tags) sizeof(asn1_DEF_BIT_STRING_tags)
/ sizeof(asn1_DEF_BIT_STRING_tags[0]), / sizeof(asn1_DEF_BIT_STRING_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
(void *)-1 /* Special indicator that this is a BIT STRING */ (void *)1 /* Special indicator that this is a BIT STRING */
}; };
/* /*
...@@ -144,9 +144,10 @@ BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -144,9 +144,10 @@ BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
(void)td; /* Unused argument */ (void)td; /* Unused argument */
if(!st || !st->buf) return cb("<absent>", 8, app_key); if(!st || !st->buf)
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
ilevel += 4; ilevel++;
buf = st->buf; buf = st->buf;
end = buf + st->size; end = buf + st->size;
...@@ -156,12 +157,9 @@ BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -156,12 +157,9 @@ BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
for(buf++; buf < end; buf++) { for(buf++; buf < end; buf++) {
if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 17) if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 17)
&& buf != st->buf+1) { && buf != st->buf+1) {
int i; _i_INDENT(1);
/* Indentation */
if(cb("\n", 1, app_key)) return -1;
for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
/* Dump the string */ /* Dump the string */
if(cb(scratch, p - scratch, app_key)) return -1; if(cb(scratch, p - scratch, app_key) < 0) return -1;
p = scratch; p = scratch;
} }
*p++ = h2c[*buf >> 4]; *p++ = h2c[*buf >> 4];
...@@ -173,13 +171,11 @@ BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -173,13 +171,11 @@ BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
p--; /* Eat the tailing space */ p--; /* Eat the tailing space */
if((st->size > 17)) { if((st->size > 17)) {
int i; _i_INDENT(1);
if(cb("\n", 1, app_key)) return -1;
for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
} }
/* Dump the incomplete 16-bytes row */ /* Dump the incomplete 16-bytes row */
if(cb(scratch, p - scratch, app_key)) if(cb(scratch, p - scratch, app_key) < 0)
return -1; return -1;
} }
......
...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_BMPString = { ...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_BMPString = {
asn1_DEF_BMPString_tags, asn1_DEF_BMPString_tags,
sizeof(asn1_DEF_BMPString_tags) sizeof(asn1_DEF_BMPString_tags)
/ sizeof(asn1_DEF_BMPString_tags[0]), / sizeof(asn1_DEF_BMPString_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
...@@ -101,7 +100,8 @@ BMPString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -101,7 +100,8 @@ BMPString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
(void)td; /* Unused argument */ (void)td; /* Unused argument */
(void)ilevel; /* Unused argument */ (void)ilevel; /* Unused argument */
if(!st || !st->buf) return cb("<absent>", 8, app_key); if(!st || !st->buf)
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
if(BMPString__dump(st, cb, app_key) < 0) if(BMPString__dump(st, cb, app_key) < 0)
return -1; return -1;
......
...@@ -25,7 +25,6 @@ asn1_TYPE_descriptor_t asn1_DEF_BOOLEAN = { ...@@ -25,7 +25,6 @@ asn1_TYPE_descriptor_t asn1_DEF_BOOLEAN = {
sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]), sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]),
asn1_DEF_BOOLEAN_tags, /* Same as above */ asn1_DEF_BOOLEAN_tags, /* Same as above */
sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]), sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]),
0, /* Always in primitive form */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
...@@ -43,7 +42,7 @@ BOOLEAN_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -43,7 +42,7 @@ BOOLEAN_decode_ber(asn1_TYPE_descriptor_t *td,
ber_tlv_len_t lidx; ber_tlv_len_t lidx;
if(st == NULL) { if(st == NULL) {
(void *)st = *bool_value = CALLOC(1, sizeof(*st)); st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
if(st == NULL) { if(st == NULL) {
rval.code = RC_FAIL; rval.code = RC_FAIL;
rval.consumed = 0; rval.consumed = 0;
...@@ -57,7 +56,7 @@ BOOLEAN_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -57,7 +56,7 @@ BOOLEAN_decode_ber(asn1_TYPE_descriptor_t *td,
/* /*
* Check tags. * Check tags.
*/ */
rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, &length, 0); rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, 0, &length, 0);
if(rval.code != RC_OK) if(rval.code != RC_OK)
return rval; return rval;
...@@ -101,7 +100,7 @@ BOOLEAN_encode_der(asn1_TYPE_descriptor_t *td, void *sptr, ...@@ -101,7 +100,7 @@ BOOLEAN_encode_der(asn1_TYPE_descriptor_t *td, void *sptr,
asn_enc_rval_t erval; asn_enc_rval_t erval;
BOOLEAN_t *st = (BOOLEAN_t *)sptr; BOOLEAN_t *st = (BOOLEAN_t *)sptr;
erval.encoded = der_write_tags(td, 1, tag_mode, tag, cb, app_key); erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
if(erval.encoded == -1) { if(erval.encoded == -1) {
erval.failed_type = td; erval.failed_type = td;
erval.structure_ptr = sptr; erval.structure_ptr = sptr;
...@@ -110,11 +109,10 @@ BOOLEAN_encode_der(asn1_TYPE_descriptor_t *td, void *sptr, ...@@ -110,11 +109,10 @@ BOOLEAN_encode_der(asn1_TYPE_descriptor_t *td, void *sptr,
if(cb) { if(cb) {
uint8_t bool_value; uint8_t bool_value;
ssize_t ret;
bool_value = *st?0xff:0; /* 0xff mandated by DER */ bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
ret = cb(&bool_value, 1, app_key);
if(ret == -1) { if(cb(&bool_value, 1, app_key) < 0) {
erval.encoded = -1; erval.encoded = -1;
erval.failed_type = td; erval.failed_type = td;
erval.structure_ptr = sptr; erval.structure_ptr = sptr;
...@@ -154,18 +152,26 @@ int ...@@ -154,18 +152,26 @@ int
BOOLEAN_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, BOOLEAN_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) { asn_app_consume_bytes_f *cb, void *app_key) {
const BOOLEAN_t *st = (const BOOLEAN_t *)sptr; const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
const char *buf;
size_t buflen;
(void)td; /* Unused argument */ (void)td; /* Unused argument */
(void)ilevel; /* Unused argument */ (void)ilevel; /* Unused argument */
if(st) { if(st) {
if(*st) if(*st) {
return cb("TRUE", 4, app_key); buf = "TRUE";
else buflen = 4;
return cb("FALSE", 5, app_key); } else {
buf = "FALSE";
buflen = 5;
}
} else { } else {
return cb("<absent>", 8, app_key); buf = "<absent>";
buflen = 8;
} }
return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
} }
void void
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
#include <asn_internal.h> #include <asn_internal.h>
#include <ENUMERATED.h> #include <ENUMERATED.h>
#include <ber_codec_prim.h>
/* /*
* ENUMERATED basic type description. * ENUMERATED basic type description.
...@@ -13,10 +14,10 @@ static ber_tlv_tag_t asn1_DEF_ENUMERATED_tags[] = { ...@@ -13,10 +14,10 @@ static ber_tlv_tag_t asn1_DEF_ENUMERATED_tags[] = {
}; };
asn1_TYPE_descriptor_t asn1_DEF_ENUMERATED = { asn1_TYPE_descriptor_t asn1_DEF_ENUMERATED = {
"ENUMERATED", "ENUMERATED",
INTEGER_free, /* Implemented in terms of INTEGER */ ASN__PRIMITIVE_TYPE_free,
INTEGER_print, /* Implemented in terms of INTEGER */ INTEGER_print, /* Implemented in terms of INTEGER */
asn_generic_no_constraint, asn_generic_no_constraint,
INTEGER_decode_ber, /* Implemented in terms of INTEGER */ ber_decode_primitive,
INTEGER_encode_der, /* Implemented in terms of INTEGER */ INTEGER_encode_der, /* Implemented in terms of INTEGER */
0, /* Not implemented yet */ 0, /* Not implemented yet */
INTEGER_encode_xer, /* Implemented in terms of INTEGER */ INTEGER_encode_xer, /* Implemented in terms of INTEGER */
...@@ -25,7 +26,6 @@ asn1_TYPE_descriptor_t asn1_DEF_ENUMERATED = { ...@@ -25,7 +26,6 @@ asn1_TYPE_descriptor_t asn1_DEF_ENUMERATED = {
sizeof(asn1_DEF_ENUMERATED_tags) / sizeof(asn1_DEF_ENUMERATED_tags[0]), sizeof(asn1_DEF_ENUMERATED_tags) / sizeof(asn1_DEF_ENUMERATED_tags[0]),
asn1_DEF_ENUMERATED_tags, /* Same as above */ asn1_DEF_ENUMERATED_tags, /* Same as above */
sizeof(asn1_DEF_ENUMERATED_tags) / sizeof(asn1_DEF_ENUMERATED_tags[0]), sizeof(asn1_DEF_ENUMERATED_tags) / sizeof(asn1_DEF_ENUMERATED_tags[0]),
0, /* Primitive */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
......
...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_GeneralString = { ...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_GeneralString = {
asn1_DEF_GeneralString_tags, asn1_DEF_GeneralString_tags,
sizeof(asn1_DEF_GeneralString_tags) sizeof(asn1_DEF_GeneralString_tags)
/ sizeof(asn1_DEF_GeneralString_tags[0]), / sizeof(asn1_DEF_GeneralString_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
......
...@@ -110,7 +110,9 @@ static time_t timegm(struct tm *tm) { ...@@ -110,7 +110,9 @@ static time_t timegm(struct tm *tm) {
* GeneralizedTime basic type description. * GeneralizedTime basic type description.
*/ */
static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = { static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (24 << 2)) (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
}; };
asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = { asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
"GeneralizedTime", "GeneralizedTime",
...@@ -124,11 +126,10 @@ asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = { ...@@ -124,11 +126,10 @@ asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = {
0, /* Use generic outmost tag fetcher */ 0, /* Use generic outmost tag fetcher */
asn1_DEF_GeneralizedTime_tags, asn1_DEF_GeneralizedTime_tags,
sizeof(asn1_DEF_GeneralizedTime_tags) sizeof(asn1_DEF_GeneralizedTime_tags)
/ sizeof(asn1_DEF_GeneralizedTime_tags[0]), / sizeof(asn1_DEF_GeneralizedTime_tags[0]) - 2,
asn1_DEF_GeneralizedTime_tags, /* Same as above */ asn1_DEF_GeneralizedTime_tags,
sizeof(asn1_DEF_GeneralizedTime_tags) sizeof(asn1_DEF_GeneralizedTime_tags)
/ sizeof(asn1_DEF_GeneralizedTime_tags[0]), / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
...@@ -242,16 +243,16 @@ GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -242,16 +243,16 @@ GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
errno = EPERM; errno = EPERM;
if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
return cb("<bad-value>", 11, app_key); return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
ret = snprintf(buf, sizeof(buf), ret = snprintf(buf, sizeof(buf),
"%04d-%02d-%02d %02d:%02d%02d (GMT)", "%04d-%02d-%02d %02d:%02d%02d (GMT)",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec); tm.tm_hour, tm.tm_min, tm.tm_sec);
assert(ret > 0 && ret < (int)sizeof(buf)); assert(ret > 0 && ret < (int)sizeof(buf));
return cb(buf, ret, app_key); return (cb(buf, ret, app_key) < 0) ? -1 : 0;
} else { } else {
return cb("<absent>", 8, app_key); return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
} }
} }
...@@ -493,7 +494,7 @@ asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) { ...@@ -493,7 +494,7 @@ asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
} }
/* Pre-allocate a buffer of sufficient yet small length */ /* Pre-allocate a buffer of sufficient yet small length */
(void *)buf = MALLOC(buf_size); buf = (char *)MALLOC(buf_size);
if(!buf) return 0; if(!buf) return 0;
gmtoff = GMTOFF(*tm); gmtoff = GMTOFF(*tm);
...@@ -536,7 +537,7 @@ asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) { ...@@ -536,7 +537,7 @@ asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
if(opt_gt->buf) if(opt_gt->buf)
FREEMEM(opt_gt->buf); FREEMEM(opt_gt->buf);
} else { } else {
(void *)opt_gt = CALLOC(1, sizeof *opt_gt); opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
if(!opt_gt) { free(buf); return 0; } if(!opt_gt) { free(buf); return 0; }
} }
......
...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_GraphicString = { ...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_GraphicString = {
asn1_DEF_GraphicString_tags, asn1_DEF_GraphicString_tags,
sizeof(asn1_DEF_GraphicString_tags) sizeof(asn1_DEF_GraphicString_tags)
/ sizeof(asn1_DEF_GraphicString_tags[0]), / sizeof(asn1_DEF_GraphicString_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
......
...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_IA5String = { ...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_IA5String = {
asn1_DEF_IA5String_tags, asn1_DEF_IA5String_tags,
sizeof(asn1_DEF_IA5String_tags) sizeof(asn1_DEF_IA5String_tags)
/ sizeof(asn1_DEF_IA5String_tags[0]), / sizeof(asn1_DEF_IA5String_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
#include <asn_internal.h> #include <asn_internal.h>
#include <INTEGER.h> #include <INTEGER.h>
#include <ber_codec_prim.h> /* Encoder and decoder of a primitive */
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
...@@ -15,10 +16,10 @@ static ber_tlv_tag_t asn1_DEF_INTEGER_tags[] = { ...@@ -15,10 +16,10 @@ static ber_tlv_tag_t asn1_DEF_INTEGER_tags[] = {
}; };
asn1_TYPE_descriptor_t asn1_DEF_INTEGER = { asn1_TYPE_descriptor_t asn1_DEF_INTEGER = {
"INTEGER", "INTEGER",
INTEGER_free, ASN__PRIMITIVE_TYPE_free,
INTEGER_print, INTEGER_print,
asn_generic_no_constraint, asn_generic_no_constraint,
INTEGER_decode_ber, ber_decode_primitive,
INTEGER_encode_der, INTEGER_encode_der,
0, /* Not implemented yet */ 0, /* Not implemented yet */
INTEGER_encode_xer, INTEGER_encode_xer,
...@@ -27,89 +28,21 @@ asn1_TYPE_descriptor_t asn1_DEF_INTEGER = { ...@@ -27,89 +28,21 @@ asn1_TYPE_descriptor_t asn1_DEF_INTEGER = {
sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]), sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
asn1_DEF_INTEGER_tags, /* Same as above */ asn1_DEF_INTEGER_tags, /* Same as above */
sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]), sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
0, /* Always in primitive form */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
/*
* Decode INTEGER type.
*/
ber_dec_rval_t
INTEGER_decode_ber(asn1_TYPE_descriptor_t *td,
void **int_structure, void *buf_ptr, size_t size, int tag_mode) {
INTEGER_t *st = (INTEGER_t *)*int_structure;
ber_dec_rval_t rval;
ber_tlv_len_t length;
/*
* If the structure is not there, allocate it.
*/
if(st == NULL) {
(void *)st = *int_structure = CALLOC(1, sizeof(*st));
if(st == NULL) {
rval.code = RC_FAIL;
rval.consumed = 0;
return rval;
}
}
ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
td->name, tag_mode);
/*
* Check tags.
*/
rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, &length, 0);
if(rval.code != RC_OK)
return rval;
ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
/*
* Make sure we have this length.
*/
buf_ptr = ((char *)buf_ptr) + rval.consumed;
size -= rval.consumed;
if(length > (ber_tlv_len_t)size) {
rval.code = RC_WMORE;
rval.consumed = 0;
return rval;
}
st->buf = (uint8_t *)MALLOC(length);
if(st->buf) {
st->size = length;
} else {
rval.code = RC_FAIL;
rval.consumed = 0;
return rval;
}
memcpy(st->buf, buf_ptr, st->size);
rval.code = RC_OK;
rval.consumed += length;
ASN_DEBUG("Took %ld/%ld bytes to encode %s",
(long)rval.consumed,
(long)length, td->name);
return rval;
}
/* /*
* Encode INTEGER type using DER. * Encode INTEGER type using DER.
*/ */
asn_enc_rval_t asn_enc_rval_t
INTEGER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr, INTEGER_encode_der(asn1_TYPE_descriptor_t *td, void *sptr,
int tag_mode, ber_tlv_tag_t tag, int tag_mode, ber_tlv_tag_t tag,
asn_app_consume_bytes_f *cb, void *app_key) { asn_app_consume_bytes_f *cb, void *app_key) {
asn_enc_rval_t erval; INTEGER_t *st = (INTEGER_t *)sptr;
INTEGER_t *st = (INTEGER_t *)ptr;
ASN_DEBUG("%s %s as INTEGER (tm=%d)", ASN_DEBUG("%s %s as INTEGER (tm=%d)",
cb?"Encoding":"Estimating", sd->name, tag_mode); cb?"Encoding":"Estimating", td->name, tag_mode);
/* /*
* Canonicalize integer in the buffer. * Canonicalize integer in the buffer.
...@@ -155,32 +88,7 @@ INTEGER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr, ...@@ -155,32 +88,7 @@ INTEGER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
} /* if(1) */ } /* if(1) */
erval.encoded = der_write_tags(sd, st->size, tag_mode, tag, return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
cb, app_key);
ASN_DEBUG("INTEGER %s wrote tags %d", sd->name, (int)erval.encoded);
if(erval.encoded == -1) {
erval.failed_type = sd;
erval.structure_ptr = ptr;
return erval;
}
if(cb && st->buf) {
ssize_t ret;
ret = cb(st->buf, st->size, app_key);
if(ret == -1) {
erval.encoded = -1;
erval.failed_type = sd;
erval.structure_ptr = ptr;
return erval;
}
} else {
assert(st->buf || st->size == 0);
}
erval.encoded += st->size;
return erval;
} }
/* /*
...@@ -254,13 +162,17 @@ int ...@@ -254,13 +162,17 @@ int
INTEGER_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, INTEGER_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) { asn_app_consume_bytes_f *cb, void *app_key) {
const INTEGER_t *st = (const INTEGER_t *)sptr; const INTEGER_t *st = (const INTEGER_t *)sptr;
ssize_t ret;
(void)td; (void)td;
(void)ilevel; (void)ilevel;
if(!st && !st->buf) return cb("<absent>", 8, app_key); if(!st && !st->buf)
ret = cb("<absent>", 8, app_key);
else
ret = INTEGER__dump(st, cb, app_key);
return (INTEGER__dump(st, cb, app_key) < 0) ? -1 : 0; return (ret < 0) ? -1 : 0;
} }
asn_enc_rval_t asn_enc_rval_t
...@@ -282,25 +194,6 @@ INTEGER_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, ...@@ -282,25 +194,6 @@ INTEGER_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
return er; return er;
} }
void
INTEGER_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
INTEGER_t *st = (INTEGER_t *)ptr;
if(!td || !st)
return;
ASN_DEBUG("Freeing %s as INTEGER (%d, %p, %p)",
td->name, contents_only, st, st->buf);
if(st->buf) {
FREEMEM(st->buf);
}
if(!contents_only) {
FREEMEM(st);
}
}
int int
asn1_INTEGER2long(const INTEGER_t *iptr, long *lptr) { asn1_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
uint8_t *b, *end; uint8_t *b, *end;
......
...@@ -6,15 +6,12 @@ ...@@ -6,15 +6,12 @@
#define _INTEGER_H_ #define _INTEGER_H_
#include <asn_application.h> #include <asn_application.h>
#include <ber_codec_prim.h>
typedef struct INTEGER { typedef ASN__PRIMITIVE_TYPE_t INTEGER_t;
uint8_t *buf; /* Buffer with consecutive INTEGER bits (big-endian) */
int size; /* Size of the buffer */
} INTEGER_t;
extern asn1_TYPE_descriptor_t asn1_DEF_INTEGER; extern asn1_TYPE_descriptor_t asn1_DEF_INTEGER;
asn_struct_free_f INTEGER_free;
asn_struct_print_f INTEGER_print; asn_struct_print_f INTEGER_print;
ber_type_decoder_f INTEGER_decode_ber; ber_type_decoder_f INTEGER_decode_ber;
der_type_encoder_f INTEGER_encode_der; der_type_encoder_f INTEGER_encode_der;
......
...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_ISO646String = { ...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_ISO646String = {
asn1_DEF_ISO646String_tags, asn1_DEF_ISO646String_tags,
sizeof(asn1_DEF_ISO646String_tags) sizeof(asn1_DEF_ISO646String_tags)
/ sizeof(asn1_DEF_ISO646String_tags[0]), / sizeof(asn1_DEF_ISO646String_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
......
...@@ -26,7 +26,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NULL = { ...@@ -26,7 +26,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NULL = {
sizeof(asn1_DEF_NULL_tags) / sizeof(asn1_DEF_NULL_tags[0]), sizeof(asn1_DEF_NULL_tags) / sizeof(asn1_DEF_NULL_tags[0]),
asn1_DEF_NULL_tags, /* Same as above */ asn1_DEF_NULL_tags, /* Same as above */
sizeof(asn1_DEF_NULL_tags) / sizeof(asn1_DEF_NULL_tags[0]), sizeof(asn1_DEF_NULL_tags) / sizeof(asn1_DEF_NULL_tags[0]),
0, /* Always in primitive form */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
...@@ -37,7 +36,7 @@ NULL_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, ...@@ -37,7 +36,7 @@ NULL_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
asn_app_consume_bytes_f *cb, void *app_key) { asn_app_consume_bytes_f *cb, void *app_key) {
asn_enc_rval_t erval; asn_enc_rval_t erval;
erval.encoded = der_write_tags(td, 0, tag_mode, tag, cb, app_key); erval.encoded = der_write_tags(td, 0, tag_mode, 0, tag, cb, app_key);
if(erval.encoded == -1) { if(erval.encoded == -1) {
erval.failed_type = td; erval.failed_type = td;
erval.structure_ptr = ptr; erval.structure_ptr = ptr;
...@@ -73,8 +72,8 @@ NULL_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -73,8 +72,8 @@ NULL_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
(void)ilevel; /* Unused argument */ (void)ilevel; /* Unused argument */
if(sptr) { if(sptr) {
return cb("<present>", 9, app_key); return (cb("<present>", 9, app_key) < 0) ? -1 : 0;
} else { } else {
return cb("<absent>", 8, app_key); return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
} }
} }
...@@ -32,7 +32,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NativeEnumerated = { ...@@ -32,7 +32,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NativeEnumerated = {
sizeof(asn1_DEF_NativeEnumerated_tags) / sizeof(asn1_DEF_NativeEnumerated_tags[0]), sizeof(asn1_DEF_NativeEnumerated_tags) / sizeof(asn1_DEF_NativeEnumerated_tags[0]),
asn1_DEF_NativeEnumerated_tags, /* Same as above */ asn1_DEF_NativeEnumerated_tags, /* Same as above */
sizeof(asn1_DEF_NativeEnumerated_tags) / sizeof(asn1_DEF_NativeEnumerated_tags[0]), sizeof(asn1_DEF_NativeEnumerated_tags) / sizeof(asn1_DEF_NativeEnumerated_tags[0]),
0, /* Always in primitive form */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
......
...@@ -34,7 +34,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NativeInteger = { ...@@ -34,7 +34,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NativeInteger = {
sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]), sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]),
asn1_DEF_NativeInteger_tags, /* Same as above */ asn1_DEF_NativeInteger_tags, /* Same as above */
sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]), sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]),
0, /* Always in primitive form */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
...@@ -53,7 +52,7 @@ NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -53,7 +52,7 @@ NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td,
* If the structure is not there, allocate it. * If the structure is not there, allocate it.
*/ */
if(Int == NULL) { if(Int == NULL) {
(void *)Int = *int_ptr = CALLOC(1, sizeof(*Int)); Int = (int *)(*int_ptr = CALLOC(1, sizeof(*Int)));
if(Int == NULL) { if(Int == NULL) {
rval.code = RC_FAIL; rval.code = RC_FAIL;
rval.consumed = 0; rval.consumed = 0;
...@@ -67,7 +66,7 @@ NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -67,7 +66,7 @@ NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td,
/* /*
* Check tags. * Check tags.
*/ */
rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, &length, 0); rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, 0, &length, 0);
if(rval.code != RC_OK) if(rval.code != RC_OK)
return rval; return rval;
...@@ -199,9 +198,9 @@ NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -199,9 +198,9 @@ NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
if(Int) { if(Int) {
ret = snprintf(scratch, sizeof(scratch), "%d", *Int); ret = snprintf(scratch, sizeof(scratch), "%d", *Int);
assert(ret > 0 && ret < (int)sizeof(scratch)); assert(ret > 0 && ret < (int)sizeof(scratch));
return cb(scratch, ret, app_key); return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
} else { } else {
return cb("<absent>", 8, app_key); return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
} }
} }
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
*/ */
#include <asn_internal.h> #include <asn_internal.h>
#include <NativeReal.h> #include <NativeReal.h>
#include <INTEGER.h>
#include <REAL.h> #include <REAL.h>
#include <assert.h> #include <assert.h>
...@@ -35,7 +34,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NativeReal = { ...@@ -35,7 +34,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NativeReal = {
sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]), sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
asn1_DEF_NativeReal_tags, /* Same as above */ asn1_DEF_NativeReal_tags, /* Same as above */
sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]), sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
0, /* Always in primitive form */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
...@@ -68,7 +66,7 @@ NativeReal_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -68,7 +66,7 @@ NativeReal_decode_ber(asn1_TYPE_descriptor_t *td,
/* /*
* Check tags. * Check tags.
*/ */
rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, &length, 0); rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, 0, &length, 0);
if(rval.code != RC_OK) if(rval.code != RC_OK)
return rval; return rval;
...@@ -132,8 +130,8 @@ NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, ...@@ -132,8 +130,8 @@ NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
return erval; return erval;
} }
/* Encode fake REAL */ /* Encode a fake REAL */
erval = INTEGER_encode_der(td, &tmp, tag_mode, tag, cb, app_key); erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
if(erval.encoded == -1) { if(erval.encoded == -1) {
assert(erval.structure_ptr == &tmp); assert(erval.structure_ptr == &tmp);
erval.structure_ptr = ptr; erval.structure_ptr = ptr;
...@@ -148,13 +146,12 @@ NativeReal_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, ...@@ -148,13 +146,12 @@ NativeReal_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
asn_app_consume_bytes_f *cb, void *app_key) { asn_app_consume_bytes_f *cb, void *app_key) {
const double *Dbl = (const double *)sptr; const double *Dbl = (const double *)sptr;
asn_enc_rval_t er; asn_enc_rval_t er;
double d;
(void)ilevel; (void)ilevel;
if(!Dbl) _ASN_ENCODE_FAILED; if(!Dbl) _ASN_ENCODE_FAILED;
er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key); er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
if(er.encoded < 0) _ASN_ENCODE_FAILED; if(er.encoded < 0) _ASN_ENCODE_FAILED;
return er; return er;
...@@ -171,7 +168,7 @@ NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -171,7 +168,7 @@ NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
(void)td; /* Unused argument */ (void)td; /* Unused argument */
(void)ilevel; /* Unused argument */ (void)ilevel; /* Unused argument */
if(!Dbl) return cb("<absent>", 8, app_key); if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0; return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
} }
......
...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NumericString = { ...@@ -28,7 +28,6 @@ asn1_TYPE_descriptor_t asn1_DEF_NumericString = {
asn1_DEF_NumericString_tags, asn1_DEF_NumericString_tags,
sizeof(asn1_DEF_NumericString_tags) sizeof(asn1_DEF_NumericString_tags)
/ sizeof(asn1_DEF_NumericString_tags[0]), / sizeof(asn1_DEF_NumericString_tags[0]),
-1, /* Both ways are fine */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
......
...@@ -16,11 +16,11 @@ static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = { ...@@ -16,11 +16,11 @@ static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = {
}; };
asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = { asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = {
"OBJECT IDENTIFIER", "OBJECT IDENTIFIER",
INTEGER_free, ASN__PRIMITIVE_TYPE_free,
OBJECT_IDENTIFIER_print, OBJECT_IDENTIFIER_print,
OBJECT_IDENTIFIER_constraint, OBJECT_IDENTIFIER_constraint,
INTEGER_decode_ber, /* Implemented in terms of INTEGER type */ ber_decode_primitive,
OBJECT_IDENTIFIER_encode_der, der_encode_primitive,
0, /* Not implemented yet */ 0, /* Not implemented yet */
OBJECT_IDENTIFIER_encode_xer, OBJECT_IDENTIFIER_encode_xer,
0, /* Use generic outmost tag fetcher */ 0, /* Use generic outmost tag fetcher */
...@@ -30,52 +30,11 @@ asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = { ...@@ -30,52 +30,11 @@ asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = {
asn1_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */ asn1_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags) sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
/ sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]), / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
0, /* Always in primitive form */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
/*
* Encode OBJECT IDENTIFIER type using DER.
*/
asn_enc_rval_t
OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
int tag_mode, ber_tlv_tag_t tag,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_enc_rval_t erval;
OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr;
ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)",
cb?"Encoding":"Estimating", sd->name, tag_mode);
erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
cb, app_key);
ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d",
sd->name, (int)erval.encoded);
if(erval.encoded == -1) {
erval.failed_type = sd;
erval.structure_ptr = ptr;
return erval;
}
if(cb && st->buf) {
int ret = cb(st->buf, st->size, app_key);
if(ret < 0) {
erval.encoded = -1;
erval.failed_type = sd;
erval.structure_ptr = ptr;
return erval;
}
} else {
assert(st->buf || st->size == 0);
}
erval.encoded += st->size;
return erval;
}
int int
OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
asn_app_consume_bytes_f *app_errlog, void *app_key) { asn_app_consume_bytes_f *app_errlog, void *app_key) {
...@@ -101,11 +60,12 @@ OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, ...@@ -101,11 +60,12 @@ OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
int int
OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) { OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */ unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
uint8_t *arcend = arcbuf + arclen; /* End of arc */ uint8_t *arcend = arcbuf + arclen; /* End of arc */
void *rvstart = rvbuf; /* Original start of the value buffer */
unsigned int cache = 0; /* No more than 14 significant bits */ unsigned int cache = 0; /* No more than 14 significant bits */
unsigned char *rvbuf = (unsigned char *)rvbufp;
unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
int inc; /* Return value growth direction */ int inc; /* Return value growth direction */
rvsize *= CHAR_BIT; /* bytes to bits */ rvsize *= CHAR_BIT; /* bytes to bits */
...@@ -155,15 +115,15 @@ OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed in ...@@ -155,15 +115,15 @@ OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed in
errno = ERANGE; /* Overflow */ errno = ERANGE; /* Overflow */
return -1; return -1;
} }
*(unsigned long *)rvbuf = accum + add; *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
return 0; return 0;
} }
#ifndef WORDS_BIGENDIAN #ifndef WORDS_BIGENDIAN
if(*(unsigned char *)&LE) { /* Little endian (x86) */ if(*(unsigned char *)&LE) { /* Little endian (x86) */
/* "Convert" to big endian */ /* "Convert" to big endian */
(unsigned char *)rvbuf += rvsize / CHAR_BIT - 1; rvbuf += rvsize / CHAR_BIT - 1;
((unsigned char *)rvstart)--; rvstart--;
inc = -1; /* Descending */ inc = -1; /* Descending */
} else } else
#endif /* !WORDS_BIGENDIAN */ #endif /* !WORDS_BIGENDIAN */
...@@ -175,8 +135,8 @@ OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed in ...@@ -175,8 +135,8 @@ OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed in
/* Clear the high unused bits */ /* Clear the high unused bits */
for(bits = rvsize - arclen; for(bits = rvsize - arclen;
bits > CHAR_BIT; bits > CHAR_BIT;
(unsigned char *)rvbuf += inc, bits -= CHAR_BIT) rvbuf += inc, bits -= CHAR_BIT)
*(unsigned char *)rvbuf = 0; *rvbuf = 0;
/* Fill the body of a value */ /* Fill the body of a value */
for(; arcbuf < arcend; arcbuf++) { for(; arcbuf < arcend; arcbuf++) {
...@@ -184,25 +144,24 @@ OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed in ...@@ -184,25 +144,24 @@ OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed in
bits += 7; bits += 7;
if(bits >= CHAR_BIT) { if(bits >= CHAR_BIT) {
bits -= CHAR_BIT; bits -= CHAR_BIT;
*(unsigned char *)rvbuf = (cache >> bits); *rvbuf = (cache >> bits);
(unsigned char *)rvbuf += inc; rvbuf += inc;
} }
} }
if(bits) { if(bits) {
*(unsigned char *)rvbuf = cache; *rvbuf = cache;
(unsigned char *)rvbuf += inc; rvbuf += inc;
} }
} }
if(add) { if(add) {
for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) { for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
int v = add + *(unsigned char *)rvbuf; int v = add + *rvbuf;
if(v & (-1 << CHAR_BIT)) { if(v & (-1 << CHAR_BIT)) {
*(unsigned char *)rvbuf *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
= (unsigned char)(v + (1 << CHAR_BIT));
add = -1; add = -1;
} else { } else {
*(unsigned char *)rvbuf = v; *rvbuf = v;
break; break;
} }
} }
...@@ -331,16 +290,16 @@ OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr, ...@@ -331,16 +290,16 @@ OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
(void)ilevel; /* Unused argument */ (void)ilevel; /* Unused argument */
if(!st || !st->buf) if(!st || !st->buf)
return cb("<absent>", 8, app_key); return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/* Dump preamble */ /* Dump preamble */
if(cb("{ ", 2, app_key)) if(cb("{ ", 2, app_key) < 0)
return -1; return -1;
if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
return -1; return -1;
return cb(" }", 2, app_key); return (cb(" }", 2, app_key) < 0) ? -1 : 0;
} }
int int
......
...@@ -5,9 +5,10 @@ ...@@ -5,9 +5,10 @@
#ifndef _OBJECT_IDENTIFIER_H_ #ifndef _OBJECT_IDENTIFIER_H_
#define _OBJECT_IDENTIFIER_H_ #define _OBJECT_IDENTIFIER_H_
#include <INTEGER.h> #include <asn_application.h>
#include <ber_codec_prim.h>
typedef INTEGER_t OBJECT_IDENTIFIER_t; /* Implemented via INTEGER */ typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t;
extern asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER; extern asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER;
......
...@@ -29,7 +29,6 @@ asn1_TYPE_descriptor_t asn1_DEF_OCTET_STRING = { ...@@ -29,7 +29,6 @@ asn1_TYPE_descriptor_t asn1_DEF_OCTET_STRING = {
asn1_DEF_OCTET_STRING_tags, /* Same as above */ asn1_DEF_OCTET_STRING_tags, /* Same as above */
sizeof(asn1_DEF_OCTET_STRING_tags) sizeof(asn1_DEF_OCTET_STRING_tags)
/ sizeof(asn1_DEF_OCTET_STRING_tags[0]), / sizeof(asn1_DEF_OCTET_STRING_tags[0]),
-1, /* Both ways are fine (primitive and constructed) */
0, 0, /* No members */ 0, 0, /* No members */
0 /* No specifics */ 0 /* No specifics */
}; };
...@@ -161,10 +160,9 @@ OCTET_STRING_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -161,10 +160,9 @@ OCTET_STRING_decode_ber(asn1_TYPE_descriptor_t *td,
int tlv_constr; int tlv_constr;
enum type_type_e { enum type_type_e {
_TT_GENERIC = 0, /* Just a random OCTET STRING */ _TT_GENERIC = 0, /* Just a random OCTET STRING */
_TT_BIT_STRING = -1, /* BIT STRING type, a special case */ _TT_BIT_STRING = 1, /* BIT STRING type, a special case */
_TT_ANY = 1, /* ANY type, a special case too */ _TT_ANY = 2, /* ANY type, a special case too */
} type_type } type_type = (enum type_type_e)(int)td->specifics;
= (enum type_type_e)(int)td->specifics; /* An ugly hack */
ASN_DEBUG("Decoding %s as %s (frame %ld)", ASN_DEBUG("Decoding %s as %s (frame %ld)",
td->name, td->name,
...@@ -189,7 +187,7 @@ OCTET_STRING_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -189,7 +187,7 @@ OCTET_STRING_decode_ber(asn1_TYPE_descriptor_t *td,
* Check tags. * Check tags.
*/ */
rval = ber_check_tags(td, ctx, rval = ber_check_tags(td, ctx,
buf_ptr, size, tag_mode, buf_ptr, size, tag_mode, -1,
&ctx->left, &tlv_constr); &ctx->left, &tlv_constr);
if(rval.code != RC_OK) { if(rval.code != RC_OK) {
RETURN(rval.code); RETURN(rval.code);
...@@ -234,8 +232,9 @@ OCTET_STRING_decode_ber(asn1_TYPE_descriptor_t *td, ...@@ -234,8 +232,9 @@ OCTET_STRING_decode_ber(asn1_TYPE_descriptor_t *td,
ber_tlv_len_t tlv_len; ber_tlv_len_t tlv_len;
ber_tlv_tag_t expected_tag; ber_tlv_tag_t expected_tag;
ssize_t tl, ll; ssize_t tl, ll;
ssize_t Left = ((!sel||sel->left==-1||sel->left >= size) /* This one works even if (sel->left == -1) */
?size:sel->left); ssize_t Left = ((!sel||(size_t)sel->left >= size)
?size:(size_t)sel->left);
ASN_DEBUG("fetch tag(size=%d,L=%d), %sstack, left=%d, want0=%d", ASN_DEBUG("fetch tag(size=%d,L=%d), %sstack, left=%d, want0=%d",
...@@ -475,8 +474,8 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, ...@@ -475,8 +474,8 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
asn_enc_rval_t erval; asn_enc_rval_t erval;
OCTET_STRING_t *st = (OCTET_STRING_t *)ptr; OCTET_STRING_t *st = (OCTET_STRING_t *)ptr;
int add_byte = 0; int add_byte = 0;
int is_bit_str = (td->specifics == (void *)-1); int is_bit_str = (td->specifics == (void *)1);
int is_ANY_type = (td->specifics == (void *)1); int is_ANY_type = (td->specifics == (void *)2);
ASN_DEBUG("%s %s as OCTET STRING", ASN_DEBUG("%s %s as OCTET STRING",
cb?"Estimating":"Encoding", td->name); cb?"Estimating":"Encoding", td->name);
...@@ -498,7 +497,7 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, ...@@ -498,7 +497,7 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
erval.encoded = 0; erval.encoded = 0;
} else { } else {
erval.encoded = der_write_tags(td, st->size + add_byte, erval.encoded = der_write_tags(td, st->size + add_byte,
tag_mode, tag, cb, app_key); tag_mode, 0, tag, cb, app_key);
if(erval.encoded == -1) { if(erval.encoded == -1) {
erval.failed_type = td; erval.failed_type = td;
erval.structure_ptr = ptr; erval.structure_ptr = ptr;
...@@ -510,7 +509,6 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, ...@@ -510,7 +509,6 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
uint8_t zero; uint8_t zero;
uint8_t *buf; uint8_t *buf;
int size; int size;
ssize_t ret;
/* BIT STRING-aware handling */ /* BIT STRING-aware handling */
if(add_byte) { if(add_byte) {
...@@ -527,8 +525,7 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, ...@@ -527,8 +525,7 @@ OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
} }
if(size) { if(size) {
ret = cb(buf, size, app_key); if(cb(buf, size, app_key) < 0) {
if(ret == -1) {
erval.encoded = -1; erval.encoded = -1;
erval.failed_type = td; erval.failed_type = td;
erval.structure_ptr = ptr; erval.structure_ptr = ptr;
...@@ -636,11 +633,10 @@ OCTET_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -636,11 +633,10 @@ OCTET_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
uint8_t *buf; uint8_t *buf;
uint8_t *end; uint8_t *end;
size_t i; size_t i;
int lvl;
(void)td; /* Unused argument */ (void)td; /* Unused argument */
if(!st || !st->buf) return cb("<absent>", 8, app_key); if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/* /*
* Dump the contents of the buffer in hexadecimal. * Dump the contents of the buffer in hexadecimal.
...@@ -649,11 +645,9 @@ OCTET_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -649,11 +645,9 @@ OCTET_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
end = buf + st->size; end = buf + st->size;
for(i = 0; buf < end; buf++, i++) { for(i = 0; buf < end; buf++, i++) {
if(!(i % 16) && (i || st->size > 16)) { if(!(i % 16) && (i || st->size > 16)) {
if(cb(scratch, p - scratch, app_key) if(cb(scratch, p - scratch, app_key) < 0)
|| cb("\n", 1, app_key))
return -1; return -1;
for(lvl = 0; lvl < ilevel; lvl++) _i_INDENT(1);
cb(" ", 1, app_key);
p = scratch; p = scratch;
} }
*p++ = h2c[(*buf >> 4) & 0x0F]; *p++ = h2c[(*buf >> 4) & 0x0F];
...@@ -663,7 +657,7 @@ OCTET_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, ...@@ -663,7 +657,7 @@ OCTET_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
if(p > scratch) { if(p > scratch) {
p--; /* Remove the tail space */ p--; /* Remove the tail space */
if(cb(scratch, p - scratch, app_key)) if(cb(scratch, p - scratch, app_key) < 0)
return -1; return -1;
} }
...@@ -679,9 +673,9 @@ OCTET_STRING_print_ascii(asn1_TYPE_descriptor_t *td, const void *sptr, ...@@ -679,9 +673,9 @@ OCTET_STRING_print_ascii(asn1_TYPE_descriptor_t *td, const void *sptr,
(void)ilevel; /* Unused argument */ (void)ilevel; /* Unused argument */
if(st && st->buf) { if(st && st->buf) {
return cb(st->buf, st->size, app_key); return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
} else { } else {
return cb("<absent>", 8, app_key); return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment