blob: d08ec965b74868257bb8268e285d18ea3cec6eca [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin725883b2006-10-09 12:07:58 +00002 * Copyright (c) 2003, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <NumericString.h>
7
8/*
9 * NumericString basic type description.
10 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070011static const ber_tlv_tag_t asn_DEF_NumericString_tags[] = {
Lev Walkin188ed2c2004-09-13 08:31:01 +000012 (ASN_TAG_CLASS_UNIVERSAL | (18 << 2)), /* [UNIVERSAL 18] IMPLICIT ...*/
13 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000014};
Lev Walkin725883b2006-10-09 12:07:58 +000015static int asn_DEF_NumericString_v2c(unsigned int value) {
16 switch(value) {
17 case 0x20: return 0;
18 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
19 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
20 return value - (0x30 - 1);
21 }
22 return -1;
23}
24static int asn_DEF_NumericString_c2v(unsigned int code) {
25 if(code > 0) {
26 if(code <= 10)
27 return code + (0x30 - 1);
28 else
29 return -1;
30 } else {
31 return 0x20;
32 }
33}
Lev Walkin76780762017-07-07 10:07:30 -070034static asn_per_constraints_t asn_DEF_NumericString_per_constraints = {
Lev Walkin725883b2006-10-09 12:07:58 +000035 { APC_CONSTRAINED, 4, 4, 0x20, 0x39 }, /* Value */
36 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
37 asn_DEF_NumericString_v2c,
38 asn_DEF_NumericString_c2v
39};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080040asn_TYPE_operation_t asn_OP_NumericString = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000041 OCTET_STRING_free,
Lev Walkindc06f6b2004-10-20 15:50:55 +000042 OCTET_STRING_print_utf8, /* ASCII subset */
Lev Walkincd2f48e2017-08-10 02:14:59 -070043 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000044 NumericString_constraint,
45 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000046 OCTET_STRING_encode_der,
47 OCTET_STRING_decode_xer_utf8,
48 OCTET_STRING_encode_xer_utf8,
Lev Walkincc159472017-07-06 08:26:36 -070049#ifdef ASN_DISABLE_OER_SUPPORT
50 0,
51 0,
52#else
Lev Walkin1d76f3c2017-07-25 07:58:05 -070053 OCTET_STRING_decode_oer,
54 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070055#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040056#ifdef ASN_DISABLE_PER_SUPPORT
57 0,
58 0,
59#else
60 OCTET_STRING_decode_uper,
61 OCTET_STRING_encode_uper,
62#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080063 0 /* Use generic outmost tag fetcher */
64};
65asn_TYPE_descriptor_t asn_DEF_NumericString = {
66 "NumericString",
67 "NumericString",
68 &asn_OP_NumericString,
69 NumericString_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000070 asn_DEF_NumericString_tags,
71 sizeof(asn_DEF_NumericString_tags)
72 / sizeof(asn_DEF_NumericString_tags[0]) - 1,
73 asn_DEF_NumericString_tags,
74 sizeof(asn_DEF_NumericString_tags)
75 / sizeof(asn_DEF_NumericString_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070076 0, /* No OER visible constraints */
77 &asn_DEF_NumericString_per_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +000078 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000079 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000080};
81
82int
Lev Walkin5e033762004-09-29 13:26:15 +000083NumericString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000084 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000085 const NumericString_t *st = (const NumericString_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000086
87 if(st && st->buf) {
88 uint8_t *buf = st->buf;
89 uint8_t *end = buf + st->size;
90
91 /*
92 * Check the alphabet of the NumericString.
93 * ASN.1:1984 (X.409)
94 */
95 for(; buf < end; buf++) {
96 switch(*buf) {
97 case 0x20:
98 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
99 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
100 continue;
101 }
Lev Walkin7c1dc052016-03-14 03:08:15 -0700102 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin426b0982004-10-26 09:59:42 +0000103 "%s: value byte %ld (%d) "
Lev Walkin16835b62004-08-22 13:47:59 +0000104 "not in NumericString alphabet (%s:%d)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 td->name,
Lev Walkin426b0982004-10-26 09:59:42 +0000106 (long)((buf - st->buf) + 1),
Lev Walkin16835b62004-08-22 13:47:59 +0000107 *buf,
108 __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 return -1;
110 }
111 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700112 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000113 "%s: value not given (%s:%d)",
114 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 return -1;
116 }
117
118 return 0;
119}