NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / libasn1fix / asn1fix_constraint_compat.c
1 #include "asn1fix_internal.h"
2 #include "asn1fix_crange.h"
3
4 /*
5  * Check that a specific constraint is compatible
6  * with the given expression type.
7  */
8 int
9 asn1constraint_compatible(asn1p_expr_type_e expr_type,
10         enum asn1p_constraint_type_e constr_type, int fbless_SIZE) {
11
12         /*
13          * X.680-0207, Table 9.
14          */
15
16         switch(constr_type) {
17         case ACT_INVALID:
18                 return 0;
19         case ACT_EL_TYPE:
20                 return 1;
21         case ACT_EL_VALUE:
22                 return 1;
23         case ACT_EL_RANGE:
24         case ACT_EL_LLRANGE:
25         case ACT_EL_RLRANGE:
26         case ACT_EL_ULRANGE:
27                 switch(expr_type) {
28                 case ASN_BASIC_ENUMERATED:
29                 case ASN_BASIC_BOOLEAN:
30                         /*
31                          * The ValueRange constraint is not formally
32                          * applicable to the above types. However, we
33                          * support it just fine.
34                          */
35                         /* Fall through */
36                 case ASN_BASIC_INTEGER:
37                 case ASN_BASIC_REAL:
38                         return 1;
39                 default:
40                         if(expr_type & ASN_STRING_MASK)
41                                 return 1;
42                 }
43                 return 0;
44         case ACT_EL_EXT:
45                 return -1;
46         case ACT_CT_FROM:
47                 if(expr_type & ASN_STRING_MASK)
48                         return 1;
49                 return 0;
50         case ACT_CT_SIZE:
51                 switch(expr_type) {
52                 case ASN_BASIC_INTEGER:
53                 case ASN_BASIC_ENUMERATED:
54                         if(fbless_SIZE)
55                                 return 1;
56                         break;
57                 case ASN_BASIC_BIT_STRING:
58                 case ASN_BASIC_OCTET_STRING:
59                 case ASN_BASIC_CHARACTER_STRING:
60                 case ASN_CONSTR_SEQUENCE_OF:
61                 case ASN_CONSTR_SET_OF:
62                         return 1;
63                 default:
64                         if(expr_type & ASN_STRING_MASK)
65                                 return 1;
66                 }
67                 return 0;
68         case ACT_CT_WCOMP:
69         case ACT_CT_WCOMPS:
70                 switch(expr_type) {
71                 case A1TC_INSTANCE:
72                 case ASN_BASIC_EXTERNAL:
73                 case ASN_BASIC_EMBEDDED_PDV:
74                 case ASN_BASIC_REAL:
75                 case ASN_BASIC_CHARACTER_STRING:
76                 case ASN_CONSTR_CHOICE:
77                 case ASN_CONSTR_SEQUENCE:
78                 case ASN_CONSTR_SEQUENCE_OF:
79                 case ASN_CONSTR_SET:
80                 case ASN_CONSTR_SET_OF:
81                         return 1;
82                 default: break;
83                 }
84                 return 0;
85         case ACT_CT_CTDBY:
86                 return 1;
87         case ACT_CT_CTNG:       /* X.682, #11 */
88                 switch(expr_type) {
89                 case ASN_BASIC_OCTET_STRING:
90                 case ASN_BASIC_BIT_STRING:
91                         return 1;
92                 default:
93                         return 0;
94                 }
95         case ACT_CT_PATTERN:
96                 if(expr_type & ASN_STRING_MASK)
97                         return 1;
98                 return 0;
99         case ACT_CA_SET:
100         case ACT_CA_CRC:
101         case ACT_CA_CSV:
102         case ACT_CA_UNI:
103         case ACT_CA_INT:
104         case ACT_CA_EXC:
105         case ACT_CA_AEX:
106                 return 1;
107         }
108
109         return -1;
110 }
111
112
113 #define DECL_RANGE(foo, lb, ub, ov, pv)     \
114     static asn1cnst_range_t range_##foo = { \
115         {ARE_VALUE, 0, lb}, {ARE_VALUE, 0, ub}, 0, 0, 0, 0, 0, 0, 0, ov, pv}
116
117 #define DECL(name, lb, ub) DECL_RANGE(name, lb, ub, 0, 0)
118 #define DECL_notOPV(name, lb, ub) DECL_RANGE(name, lb, ub, 1, 1)
119
120 asn1cnst_range_t *
121 asn1constraint_default_alphabet(asn1p_expr_type_e expr_type) {
122         DECL_notOPV(octstr,     0x00, 0xff);    /* Not OER- and PER-visible */
123         DECL_notOPV(utf8,       0x00, 0x7fffffff);      /* Not OER- and PER-visible */
124         DECL(bmp,       0x00, 65533);   /* 64K-2 cells */
125         DECL(uint7,     0x00, 0x7f);
126         DECL(uint32,    0x00, 0xffffffff);
127         DECL(Space,     0x20, 0x20);
128         DECL(ApostropheAndParens, 0x27, 0x29);
129         DECL(PlusTillColon, 0x2b, 0x3a);
130         DECL(Equal,     0x3d, 0x3d);
131         DECL(QuestionMark, 0x3f, 0x3f);
132         DECL(Digits,    0x30, 0x39);
133         DECL(AlphaCap,  0x41, 0x5a);
134         DECL(AlphaLow,  0x61, 0x7a);
135         DECL(PlusCommaMinusDot, 0x2b, 0x2e);
136         DECL(Plus,      0x2b, 0x2b);
137         DECL(MinusDot,  0x2d, 0x2e);
138         DECL(Z,         0x5a, 0x5a);
139         static asn1cnst_range_t *range_NumericString_array[] = {
140                         &range_Space, &range_Digits };
141         static asn1cnst_range_t *range_PrintableString_array[] = {
142                         &range_Space,
143                         &range_ApostropheAndParens,
144                         &range_PlusTillColon,
145                         &range_Equal,
146                         &range_QuestionMark,
147                         &range_AlphaCap,
148                         &range_AlphaLow
149         };
150         static asn1cnst_range_t *range_UTCTime_array[] = {
151                         &range_Plus, &range_MinusDot, &range_Digits, &range_Z };
152         static asn1cnst_range_t *range_GeneralizedTime_array[] = {
153                         &range_PlusCommaMinusDot, &range_Digits, &range_Z };
154
155         static asn1cnst_range_t range_notPERVisible = {
156                         { ARE_MIN, 0, 0 },
157                         { ARE_MAX, 0, 0 },
158                         0, 0, 0, 0, 0, 0, 0, 0, 1 };
159         static asn1cnst_range_t range_NumericString = {
160                         { ARE_VALUE, 0, 0x20 },
161                         { ARE_VALUE, 0, 0x39 },
162                         NOT_NARROW,
163                         range_NumericString_array,
164                         sizeof(range_NumericString_array)
165                                 /sizeof(range_NumericString_array[0]),
166                         0, 0, 0, 0, 0, 0 };
167         static asn1cnst_range_t range_PrintableString = {
168                         { ARE_VALUE, 0, 0x20 },
169                         { ARE_VALUE, 0, 0x7a },
170                         NOT_NARROW,
171                         range_PrintableString_array,
172                         sizeof(range_PrintableString_array)
173                                 /sizeof(range_PrintableString_array[0]),
174                         0, 0, 0, 0, 0, 0 };
175         static asn1cnst_range_t range_VisibleString = {
176                         { ARE_VALUE, 0, 0x20 },
177                         { ARE_VALUE, 0, 0x7e },
178                         NOT_NARROW, 0, 0, 0, 0, 0, 0, 0, 0 };
179         static asn1cnst_range_t range_UTCTime = {
180                         { ARE_VALUE, 0, 0x2b },
181                         { ARE_VALUE, 0, 0x5a },
182                         NOT_NARROW,
183                         range_UTCTime_array,
184                         sizeof(range_UTCTime_array)
185                                 /sizeof(range_UTCTime_array[0]),
186                         0, 0, 0, 0, 0, 1 };
187         static asn1cnst_range_t range_GeneralizedTime = {
188                         { ARE_VALUE, 0, 0x2b },
189                         { ARE_VALUE, 0, 0x5a },
190                         NOT_NARROW,
191                         range_GeneralizedTime_array,
192                         sizeof(range_GeneralizedTime_array)
193                                 /sizeof(range_GeneralizedTime_array[0]),
194                         0, 0, 0, 0, 0, 1 };
195
196         switch(expr_type) {
197         case ASN_STRING_NumericString:
198                 return &range_NumericString;
199         case ASN_STRING_PrintableString:
200                 return &range_PrintableString;
201         case ASN_STRING_VisibleString:
202                 return &range_VisibleString;
203         case ASN_STRING_IA5String:
204                 return &range_uint7;
205         case ASN_STRING_BMPString:
206                 return &range_bmp;
207         case ASN_STRING_UTF8String:
208                 /*
209                  * X.691, #9.3.6
210                  * Not a known-multipler character string type.
211                  */
212                 assert(range_utf8.not_OER_visible);
213                 assert(range_utf8.not_PER_visible);
214                 return &range_utf8;
215         case ASN_STRING_UniversalString:
216                 return &range_uint32;
217         case ASN_BASIC_UTCTime:
218                 /* Permitted alphabet constraint is not applicable */
219                 assert(range_UTCTime.not_PER_visible);
220                 return &range_UTCTime;
221         case ASN_BASIC_GeneralizedTime:
222                 /* Permitted alphabet constraint is not applicable */
223                 assert(range_GeneralizedTime.not_PER_visible);
224                 return &range_GeneralizedTime;
225         case ASN_BASIC_OCTET_STRING:
226                 /*
227                  * Permitted alphabet constraint is not applicable
228                  * to this type. However, we support it, albeit not
229                  * in a strict PER mode.
230                  */
231                 assert(range_octstr.not_PER_visible);
232                 return &range_octstr;
233         default:
234                 if(!(expr_type & ASN_STRING_MASK))
235                         break;
236                 assert(expr_type & ASN_STRING_NKM_MASK);
237                 /*
238                  * X.691, 9.3.6
239                  * Not a known-multiplier character string.
240                  */
241                 return &range_notPERVisible;
242         }
243
244         return NULL;
245 }