blob: 81b880aceb2db8862bc6f5342b82ceac87f5e347 [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
Harald Welteec0e2172010-07-20 00:03:44 +02002 * Copyright (c) 2003, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
Harald Welte92c45f32010-06-12 18:59:38 +02003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_internal.h>
6#include <NumericString.h>
7
8/*
9 * NumericString basic type description.
10 */
Harald Welte41b85d52015-08-31 08:56:53 +020011static const ber_tlv_tag_t asn_DEF_NumericString_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020012 (ASN_TAG_CLASS_UNIVERSAL | (18 << 2)), /* [UNIVERSAL 18] IMPLICIT ...*/
13 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
14};
Harald Welteec0e2172010-07-20 00:03:44 +020015static 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}
34static asn_per_constraints_t asn_DEF_NumericString_constraints = {
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};
Harald Welte92c45f32010-06-12 18:59:38 +020040asn_TYPE_descriptor_t asn_DEF_NumericString = {
41 "NumericString",
42 "NumericString",
43 OCTET_STRING_free,
44 OCTET_STRING_print_utf8, /* ASCII subset */
45 NumericString_constraint,
46 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
47 OCTET_STRING_encode_der,
48 OCTET_STRING_decode_xer_utf8,
49 OCTET_STRING_encode_xer_utf8,
Harald Welteec0e2172010-07-20 00:03:44 +020050 OCTET_STRING_decode_uper,
51 OCTET_STRING_encode_uper,
Harald Welte41b85d52015-08-31 08:56:53 +020052 OCTET_STRING_decode_aper,
53 OCTET_STRING_encode_aper,
Harald Welte92c45f32010-06-12 18:59:38 +020054 0, /* Use generic outmost tag fetcher */
55 asn_DEF_NumericString_tags,
56 sizeof(asn_DEF_NumericString_tags)
57 / sizeof(asn_DEF_NumericString_tags[0]) - 1,
58 asn_DEF_NumericString_tags,
59 sizeof(asn_DEF_NumericString_tags)
60 / sizeof(asn_DEF_NumericString_tags[0]),
Harald Welteec0e2172010-07-20 00:03:44 +020061 &asn_DEF_NumericString_constraints,
Harald Welte92c45f32010-06-12 18:59:38 +020062 0, 0, /* No members */
63 0 /* No specifics */
64};
65
66int
67NumericString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
68 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
69 const NumericString_t *st = (const NumericString_t *)sptr;
70
71 if(st && st->buf) {
72 uint8_t *buf = st->buf;
73 uint8_t *end = buf + st->size;
74
75 /*
76 * Check the alphabet of the NumericString.
77 * ASN.1:1984 (X.409)
78 */
79 for(; buf < end; buf++) {
80 switch(*buf) {
81 case 0x20:
82 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
83 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
84 continue;
85 }
Harald Welteec0e2172010-07-20 00:03:44 +020086 _ASN_CTFAIL(app_key, td, sptr,
Harald Welte92c45f32010-06-12 18:59:38 +020087 "%s: value byte %ld (%d) "
88 "not in NumericString alphabet (%s:%d)",
89 td->name,
90 (long)((buf - st->buf) + 1),
91 *buf,
92 __FILE__, __LINE__);
93 return -1;
94 }
95 } else {
Harald Welteec0e2172010-07-20 00:03:44 +020096 _ASN_CTFAIL(app_key, td, sptr,
Harald Welte92c45f32010-06-12 18:59:38 +020097 "%s: value not given (%s:%d)",
98 td->name, __FILE__, __LINE__);
99 return -1;
100 }
101
102 return 0;
103}