asn1c/skeletons/NumericString.c

102 lines
2.7 KiB
C
Raw Normal View History

2004-06-03 03:38:44 +00:00
/*-
2006-10-09 12:07:58 +00:00
* Copyright (c) 2003, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
2004-06-03 03:38:44 +00:00
* Redistribution and modifications are permitted subject to BSD license.
*/
2004-09-22 16:06:28 +00:00
#include <asn_internal.h>
2004-06-03 03:38:44 +00:00
#include <NumericString.h>
/*
* NumericString basic type description.
*/
2004-09-29 13:26:15 +00:00
static ber_tlv_tag_t asn_DEF_NumericString_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (18 << 2)), /* [UNIVERSAL 18] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
2004-06-03 03:38:44 +00:00
};
2006-10-09 12:07:58 +00:00
static int asn_DEF_NumericString_v2c(unsigned int value) {
switch(value) {
case 0x20: return 0;
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
return value - (0x30 - 1);
}
return -1;
}
static int asn_DEF_NumericString_c2v(unsigned int code) {
if(code > 0) {
if(code <= 10)
return code + (0x30 - 1);
else
return -1;
} else {
return 0x20;
}
}
static asn_per_constraints_t asn_DEF_NumericString_constraints = {
{ APC_CONSTRAINED, 4, 4, 0x20, 0x39 }, /* Value */
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
asn_DEF_NumericString_v2c,
asn_DEF_NumericString_c2v
};
2004-09-29 13:26:15 +00:00
asn_TYPE_descriptor_t asn_DEF_NumericString = {
2004-10-20 15:50:55 +00:00
"NumericString",
2004-06-03 03:38:44 +00:00
"NumericString",
2004-09-22 16:06:28 +00:00
OCTET_STRING_free,
2004-10-20 15:50:55 +00:00
OCTET_STRING_print_utf8, /* ASCII subset */
2004-06-03 03:38:44 +00:00
NumericString_constraint,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
2004-10-20 15:50:55 +00:00
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
2006-10-09 12:07:58 +00:00
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
2004-06-03 03:38:44 +00:00
0, /* Use generic outmost tag fetcher */
2004-09-29 13:26:15 +00:00
asn_DEF_NumericString_tags,
sizeof(asn_DEF_NumericString_tags)
/ sizeof(asn_DEF_NumericString_tags[0]) - 1,
asn_DEF_NumericString_tags,
sizeof(asn_DEF_NumericString_tags)
/ sizeof(asn_DEF_NumericString_tags[0]),
2006-10-09 12:07:58 +00:00
&asn_DEF_NumericString_constraints,
2004-08-20 13:23:42 +00:00
0, 0, /* No members */
2004-06-05 08:17:50 +00:00
0 /* No specifics */
2004-06-03 03:38:44 +00:00
};
int
2004-09-29 13:26:15 +00:00
NumericString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
2006-07-13 11:19:01 +00:00
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
2004-08-11 09:07:36 +00:00
const NumericString_t *st = (const NumericString_t *)sptr;
2004-06-03 03:38:44 +00:00
if(st && st->buf) {
uint8_t *buf = st->buf;
uint8_t *end = buf + st->size;
/*
* Check the alphabet of the NumericString.
* ASN.1:1984 (X.409)
*/
for(; buf < end; buf++) {
switch(*buf) {
case 0x20:
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
continue;
}
2006-10-16 12:18:41 +00:00
_ASN_CTFAIL(app_key, td, sptr,
2004-10-26 09:59:42 +00:00
"%s: value byte %ld (%d) "
2004-08-22 13:47:59 +00:00
"not in NumericString alphabet (%s:%d)",
2004-06-03 03:38:44 +00:00
td->name,
2004-10-26 09:59:42 +00:00
(long)((buf - st->buf) + 1),
2004-08-22 13:47:59 +00:00
*buf,
__FILE__, __LINE__);
2004-06-03 03:38:44 +00:00
return -1;
}
} else {
2006-10-16 12:18:41 +00:00
_ASN_CTFAIL(app_key, td, sptr,
2004-08-22 13:47:59 +00:00
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
2004-06-03 03:38:44 +00:00
return -1;
}
return 0;
}