Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / PrintableString.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Licensed under the Apache License, Version 2.0 (the "License");            *
6 # you may not use this file except in compliance with the License.           *
7 # You may obtain a copy of the License at                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
11 # Unless required by applicable law or agreed to in writing, software        *
12 # distributed under the License is distributed on an "AS IS" BASIS,          *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
14 # See the License for the specific language governing permissions and        *
15 # limitations under the License.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*-
20  * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
21  * All rights reserved.
22  * Redistribution and modifications are permitted subject to BSD license.
23  */
24 #include <asn_internal.h>
25 #include <PrintableString.h>
26
27 /*
28  * ASN.1:1984 (X.409)
29  */
30 static const int _PrintableString_alphabet[256] = {
31  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        /*                  */
32  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        /*                  */
33  1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 0, 5, 6, 7, 8, 9,        /* .      '() +,-./ */
34 10,11,12,13,14,15,16,17,18,19,20, 0, 0,21, 0,22,        /* 0123456789:  = ? */
35  0,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,        /*  ABCDEFGHIJKLMNO */
36 38,39,40,41,42,43,44,45,46,47,48, 0, 0, 0, 0, 0,        /* PQRSTUVWXYZ      */
37  0,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,        /*  abcdefghijklmno */
38 64,65,66,67,68,69,70,71,72,73,74, 0, 0, 0, 0, 0,        /* pqrstuvwxyz      */
39 };
40 static const int _PrintableString_code2value[74] = {
41 32,39,40,41,43,44,45,46,47,48,49,50,51,52,53,54,
42 55,56,57,58,61,63,65,66,67,68,69,70,71,72,73,74,
43 75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,
44 97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,
45 113,114,115,116,117,118,119,120,121,122};
46
47 /*
48  * PrintableString basic type description.
49  */
50 static const ber_tlv_tag_t asn_DEF_PrintableString_tags[] = {
51         (ASN_TAG_CLASS_UNIVERSAL | (19 << 2)),  /* [UNIVERSAL 19] IMPLICIT ...*/
52         (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))    /* ... OCTET STRING */
53 };
54 static int asn_DEF_PrintableString_v2c(unsigned int value) {
55         return _PrintableString_alphabet[value > 255 ? 0 : value] - 1;
56 }
57 static int asn_DEF_PrintableString_c2v(unsigned int code) {
58         if(code < 74)
59                 return _PrintableString_code2value[code];
60         return -1;
61 }
62 static asn_per_constraints_t asn_DEF_PrintableString_per_constraints = {
63         { APC_CONSTRAINED, 4, 4, 0x20, 0x39 },  /* Value */
64         { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
65         asn_DEF_PrintableString_v2c,
66         asn_DEF_PrintableString_c2v
67 };
68 asn_TYPE_operation_t asn_OP_PrintableString = {
69         OCTET_STRING_free,
70         OCTET_STRING_print_utf8,        /* ASCII subset */
71         OCTET_STRING_compare,
72         OCTET_STRING_decode_ber,      /* Implemented in terms of OCTET STRING */
73         OCTET_STRING_encode_der,
74         OCTET_STRING_decode_xer_utf8,
75         OCTET_STRING_encode_xer_utf8,
76 #ifdef  ASN_DISABLE_OER_SUPPORT
77         0,
78         0,
79 #else
80         OCTET_STRING_decode_oer,
81         OCTET_STRING_encode_oer,
82 #endif  /* ASN_DISABLE_OER_SUPPORT */
83 #ifdef  ASN_DISABLE_PER_SUPPORT
84         0,
85         0,
86         0,
87         0,
88 #else
89         OCTET_STRING_decode_uper,
90         OCTET_STRING_encode_uper,
91         OCTET_STRING_decode_aper,
92         OCTET_STRING_encode_aper,
93 #endif  /* ASN_DISABLE_PER_SUPPORT */
94         OCTET_STRING_random_fill,
95         0       /* Use generic outmost tag fetcher */
96 };
97 asn_TYPE_descriptor_t asn_DEF_PrintableString = {
98         "PrintableString",
99         "PrintableString",
100         &asn_OP_PrintableString,
101         asn_DEF_PrintableString_tags,
102         sizeof(asn_DEF_PrintableString_tags)
103           / sizeof(asn_DEF_PrintableString_tags[0]) - 1,
104         asn_DEF_PrintableString_tags,
105         sizeof(asn_DEF_PrintableString_tags)
106           / sizeof(asn_DEF_PrintableString_tags[0]),
107         { 0, &asn_DEF_PrintableString_per_constraints, PrintableString_constraint },
108         0, 0,   /* No members */
109         0       /* No specifics */
110 };
111
112
113 int
114 PrintableString_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
115                            asn_app_constraint_failed_f *ctfailcb,
116                            void *app_key) {
117     const PrintableString_t *st = (const PrintableString_t *)sptr;
118
119         if(st && st->buf) {
120                 uint8_t *buf = st->buf;
121                 uint8_t *end = buf + st->size;
122
123                 /*
124                  * Check the alphabet of the PrintableString.
125                  * ASN.1:1984 (X.409)
126                  */
127                 for(; buf < end; buf++) {
128                         if(!_PrintableString_alphabet[*buf]) {
129                                 ASN__CTFAIL(app_key, td, sptr,
130                                         "%s: value byte %ld (%d) "
131                                         "not in PrintableString alphabet "
132                                         "(%s:%d)",
133                                         td->name,
134                                         (long)((buf - st->buf) + 1),
135                                         *buf,
136                                         __FILE__, __LINE__);
137                                 return -1;
138                         }
139                 }
140         } else {
141                 ASN__CTFAIL(app_key, td, sptr,
142                         "%s: value not given (%s:%d)",
143                         td->name, __FILE__, __LINE__);
144                 return -1;
145         }
146
147         return 0;
148 }