ISSUE ID:- RICAPP-216
[ric-app/bouncer.git] / Bouncer / e2sm_kpm / lib / NumericString.c
1 /*-
2  * Copyright (c) 2003, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * 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  */
11 static const ber_tlv_tag_t asn_DEF_NumericString_tags[] = {
12         (ASN_TAG_CLASS_UNIVERSAL | (18 << 2)),  /* [UNIVERSAL 18] IMPLICIT ...*/
13         (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))    /* ... OCTET STRING */
14 };
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 }
34 static asn_per_constraints_t asn_DEF_NumericString_per_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 };
40 asn_TYPE_operation_t asn_OP_NumericString = {
41         OCTET_STRING_free,
42         OCTET_STRING_print_utf8,   /* ASCII subset */
43         OCTET_STRING_compare,
44         OCTET_STRING_decode_ber,    /* Implemented in terms of OCTET STRING */
45         OCTET_STRING_encode_der,
46         OCTET_STRING_decode_xer_utf8,
47         OCTET_STRING_encode_xer_utf8,
48 #ifdef  ASN_DISABLE_OER_SUPPORT
49         0,
50         0,
51 #else
52         OCTET_STRING_decode_oer,
53         OCTET_STRING_encode_oer,
54 #endif  /* ASN_DISABLE_OER_SUPPORT */
55 #ifdef  ASN_DISABLE_PER_SUPPORT
56         0,
57         0,
58         0,
59         0,
60 #else
61         OCTET_STRING_decode_uper,
62         OCTET_STRING_encode_uper,
63         OCTET_STRING_decode_aper,
64         OCTET_STRING_encode_aper,
65 #endif  /* ASN_DISABLE_PER_SUPPORT */
66         OCTET_STRING_random_fill,
67         0       /* Use generic outmost tag fetcher */
68 };
69 asn_TYPE_descriptor_t asn_DEF_NumericString = {
70         "NumericString",
71         "NumericString",
72         &asn_OP_NumericString,
73         asn_DEF_NumericString_tags,
74         sizeof(asn_DEF_NumericString_tags)
75           / sizeof(asn_DEF_NumericString_tags[0]) - 1,
76         asn_DEF_NumericString_tags,
77         sizeof(asn_DEF_NumericString_tags)
78           / sizeof(asn_DEF_NumericString_tags[0]),
79         { 0, &asn_DEF_NumericString_per_constraints, NumericString_constraint },
80         0, 0,   /* No members */
81         0       /* No specifics */
82 };
83
84 int
85 NumericString_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
86                          asn_app_constraint_failed_f *ctfailcb, void *app_key) {
87     const NumericString_t *st = (const NumericString_t *)sptr;
88
89         if(st && st->buf) {
90                 uint8_t *buf = st->buf;
91                 uint8_t *end = buf + st->size;
92
93                 /*
94                  * Check the alphabet of the NumericString.
95                  * ASN.1:1984 (X.409)
96                  */
97                 for(; buf < end; buf++) {
98                         switch(*buf) {
99                         case 0x20:
100                         case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
101                         case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
102                                 continue;
103                         }
104                         ASN__CTFAIL(app_key, td, sptr,
105                                 "%s: value byte %ld (%d) "
106                                 "not in NumericString alphabet (%s:%d)",
107                                 td->name,
108                                 (long)((buf - st->buf) + 1),
109                                 *buf,
110                                 __FILE__, __LINE__);
111                         return -1;
112                 }
113         } else {
114                 ASN__CTFAIL(app_key, td, sptr,
115                         "%s: value not given (%s:%d)",
116                         td->name, __FILE__, __LINE__);
117                 return -1;
118         }
119
120         return 0;
121 }