Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <NumericString.h> |
| 7 | |
| 8 | /* |
| 9 | * NumericString basic type description. |
| 10 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 11 | static const ber_tlv_tag_t asn_DEF_NumericString_tags[] = { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 12 | (ASN_TAG_CLASS_UNIVERSAL | (18 << 2)), /* [UNIVERSAL 18] IMPLICIT ...*/ |
| 13 | (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | }; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 15 | static 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 | } |
| 24 | static 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 Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 34 | static asn_per_constraints_t asn_DEF_NumericString_per_constraints = { |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 35 | { 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 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 40 | asn_TYPE_descriptor_t asn_DEF_NumericString = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | "NumericString", |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 42 | "NumericString", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 43 | OCTET_STRING_free, |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 44 | OCTET_STRING_print_utf8, /* ASCII subset */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | NumericString_constraint, |
| 46 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 47 | OCTET_STRING_encode_der, |
| 48 | OCTET_STRING_decode_xer_utf8, |
| 49 | OCTET_STRING_encode_xer_utf8, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 50 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 51 | 0, |
| 52 | 0, |
| 53 | #else |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 54 | OCTET_STRING_decode_oer, |
| 55 | OCTET_STRING_encode_oer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 56 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 57 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 58 | 0, |
| 59 | 0, |
| 60 | #else |
| 61 | OCTET_STRING_decode_uper, |
| 62 | OCTET_STRING_encode_uper, |
| 63 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 64 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 65 | asn_DEF_NumericString_tags, |
| 66 | sizeof(asn_DEF_NumericString_tags) |
| 67 | / sizeof(asn_DEF_NumericString_tags[0]) - 1, |
| 68 | asn_DEF_NumericString_tags, |
| 69 | sizeof(asn_DEF_NumericString_tags) |
| 70 | / sizeof(asn_DEF_NumericString_tags[0]), |
Lev Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 71 | 0, /* No OER visible constraints */ |
| 72 | &asn_DEF_NumericString_per_constraints, |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 73 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 74 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 78 | NumericString_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 79 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 80 | const NumericString_t *st = (const NumericString_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | |
| 82 | if(st && st->buf) { |
| 83 | uint8_t *buf = st->buf; |
| 84 | uint8_t *end = buf + st->size; |
| 85 | |
| 86 | /* |
| 87 | * Check the alphabet of the NumericString. |
| 88 | * ASN.1:1984 (X.409) |
| 89 | */ |
| 90 | for(; buf < end; buf++) { |
| 91 | switch(*buf) { |
| 92 | case 0x20: |
| 93 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 94 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 95 | continue; |
| 96 | } |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 97 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 426b098 | 2004-10-26 09:59:42 +0000 | [diff] [blame] | 98 | "%s: value byte %ld (%d) " |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 99 | "not in NumericString alphabet (%s:%d)", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | td->name, |
Lev Walkin | 426b098 | 2004-10-26 09:59:42 +0000 | [diff] [blame] | 101 | (long)((buf - st->buf) + 1), |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 102 | *buf, |
| 103 | __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | } else { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 107 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 108 | "%s: value not given (%s:%d)", |
| 109 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |