Branch data Line data Source code
1 : : %{
2 : : /*
3 : : * Copyright (C) 2001-2012 Free Software Foundation, Inc.
4 : : *
5 : : * This file is part of LIBTASN1.
6 : : *
7 : : * The LIBTASN1 library is free software; you can redistribute it
8 : : * and/or modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful, but
13 : : * WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General Public
18 : : * License along with this library; if not, write to the Free Software
19 : : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 : : * 02110-1301, USA
21 : : */
22 : :
23 : : /*****************************************************/
24 : : /* File: x509_ASN.y */
25 : : /* Description: input file for 'bison' program. */
26 : : /* The output file is a parser (in C language) for */
27 : : /* ASN.1 syntax */
28 : : /*****************************************************/
29 : :
30 : : #include <int.h>
31 : : #include <parser_aux.h>
32 : : #include <structure.h>
33 : :
34 : : static FILE *file_asn1; /* Pointer to file to parse */
35 : : static asn1_retCode result_parse; /* result of the parser
36 : : algorithm */
37 : : static ASN1_TYPE p_tree; /* pointer to the root of the
38 : : structure created by the
39 : : parser*/
40 : : static unsigned long lineNumber; /* line number describing the
41 : : parser position inside the
42 : : file */
43 : : static char lastToken[ASN1_MAX_NAME_SIZE+1]; /* last token find in the file
44 : : to parse before the 'parse
45 : : error' */
46 : : extern char _asn1_identifierMissing[];
47 : : static const char *fileName; /* file to parse */
48 : :
49 : : static int _asn1_yyerror (const char *);
50 : : static int _asn1_yylex(void);
51 : :
52 : : %}
53 : :
54 : : /* Prefix symbols and functions with _asn1_ */
55 : : %name-prefix="_asn1_yy"
56 : :
57 : : %union {
58 : : unsigned int constant;
59 : : char str[ASN1_MAX_NAME_SIZE+1];
60 : : ASN1_TYPE node;
61 : : }
62 : :
63 : :
64 : : %token ASSIG "::="
65 : : %token <str> NUM
66 : : %token <str> IDENTIFIER
67 : : %token OPTIONAL
68 : : %token INTEGER
69 : : %token SIZE
70 : : %token OCTET
71 : : %token STRING
72 : : %token SEQUENCE
73 : : %token BIT
74 : : %token UNIVERSAL
75 : : %token PRIVATE
76 : : %token APPLICATION
77 : : %token DEFAULT
78 : : %token CHOICE
79 : : %token OF
80 : : %token OBJECT
81 : : %token STR_IDENTIFIER
82 : : %token BOOLEAN
83 : : %token ASN1_TRUE
84 : : %token ASN1_FALSE
85 : : %token TOKEN_NULL
86 : : %token ANY
87 : : %token DEFINED
88 : : %token BY
89 : : %token SET
90 : : %token EXPLICIT
91 : : %token IMPLICIT
92 : : %token DEFINITIONS
93 : : %token TAGS
94 : : %token BEGIN
95 : : %token END
96 : : %token UTCTime
97 : : %token GeneralizedTime
98 : : %token GeneralString
99 : : %token FROM
100 : : %token IMPORTS
101 : : %token ENUMERATED
102 : :
103 : : %type <node> octet_string_def constant constant_list type_assig_right
104 : : %type <node> integer_def type_assig type_assig_list sequence_def type_def
105 : : %type <node> bit_string_def default size_def choise_def object_def
106 : : %type <node> boolean_def any_def size_def2 obj_constant obj_constant_list
107 : : %type <node> constant_def type_constant type_constant_list definitions
108 : : %type <node> definitions_id Time bit_element bit_element_list set_def
109 : : %type <node> tag_type tag type_assig_right_tag generalstring_def
110 : : %type <node> type_assig_right_tag_default enumerated_def
111 : : %type <str> pos_num neg_num pos_neg_num pos_neg_identifier pos_neg_list
112 : : %type <str> num_identifier
113 : : %type <constant> class explicit_implicit
114 : :
115 : : %%
116 : :
117 : :
118 : : definitions: definitions_id
119 : : DEFINITIONS explicit_implicit TAGS "::=" BEGIN /* imports_def */
120 : : type_constant_list END
121 : 26 : {$$=_asn1_add_node(TYPE_DEFINITIONS|$3);
122 : 26 : _asn1_set_name($$,_asn1_get_name($1));
123 : 26 : _asn1_set_name($1,"");
124 : 26 : _asn1_set_right($1,$7);
125 : 26 : _asn1_set_down($$,$1);
126 : :
127 : 26 : p_tree=$$;
128 : : }
129 : 26 : ;
130 : :
131 : 214 : pos_num : NUM {strcpy($$,$1);}
132 : 214 : | '+' NUM {strcpy($$,$2);}
133 : 0 : ;
134 : :
135 : 3 : neg_num : '-' NUM {strcpy($$,"-");
136 : 3 : strcat($$,$2);}
137 : 3 : ;
138 : :
139 : 214 : pos_neg_num : pos_num {strcpy($$,$1);}
140 : 214 : | neg_num {strcpy($$,$1);}
141 : 3 : ;
142 : :
143 : 505 : num_identifier : NUM {strcpy($$,$1);}
144 : 505 : | IDENTIFIER {strcpy($$,$1);}
145 : 336 : ;
146 : :
147 : 6 : pos_neg_identifier : pos_neg_num {strcpy($$,$1);}
148 : 6 : | IDENTIFIER {strcpy($$,$1);}
149 : 28 : ;
150 : :
151 : 0 : constant: '(' pos_neg_num ')' {$$=_asn1_add_node(TYPE_CONSTANT);
152 : 0 : _asn1_set_value($$,$2,strlen($2)+1);}
153 : 33 : | IDENTIFIER'('pos_neg_num')' {$$=_asn1_add_node(TYPE_CONSTANT);
154 : 33 : _asn1_set_name($$,$1);
155 : 33 : _asn1_set_value($$,$3,strlen($3)+1);}
156 : 33 : ;
157 : :
158 : 12 : constant_list: constant {$$=$1;}
159 : 12 : | constant_list ',' constant {$$=$1;
160 : 21 : _asn1_set_right(_asn1_get_last_right($1),$3);}
161 : 21 : ;
162 : :
163 : 452 : obj_constant: num_identifier {$$=_asn1_add_node(TYPE_CONSTANT);
164 : 452 : _asn1_set_value($$,$1,strlen($1)+1);}
165 : 786 : | IDENTIFIER'('NUM')' {$$=_asn1_add_node(TYPE_CONSTANT);
166 : 334 : _asn1_set_name($$,$1);
167 : 334 : _asn1_set_value($$,$3,strlen($3)+1);}
168 : 334 : ;
169 : :
170 : 232 : obj_constant_list: obj_constant {$$=$1;}
171 : 232 : | obj_constant_list obj_constant {$$=$1;
172 : 554 : _asn1_set_right(_asn1_get_last_right($1),$2);}
173 : 554 : ;
174 : :
175 : 24 : class : UNIVERSAL {$$=CONST_UNIVERSAL;}
176 : 24 : | PRIVATE {$$=CONST_PRIVATE;}
177 : 0 : | APPLICATION {$$=CONST_APPLICATION;}
178 : 5 : ;
179 : :
180 : 158 : tag_type : '[' NUM ']' {$$=_asn1_add_node(TYPE_TAG);
181 : 158 : _asn1_set_value($$,$2,strlen($2)+1);}
182 : 187 : | '[' class NUM ']' {$$=_asn1_add_node(TYPE_TAG | $2);
183 : 29 : _asn1_set_value($$,$3,strlen($3)+1);}
184 : 29 : ;
185 : :
186 : 86 : tag : tag_type {$$=$1;}
187 : 150 : | tag_type EXPLICIT {$$=_asn1_mod_type($1,CONST_EXPLICIT);}
188 : 101 : | tag_type IMPLICIT {$$=_asn1_mod_type($1,CONST_IMPLICIT);}
189 : 37 : ;
190 : :
191 : 34 : default : DEFAULT pos_neg_identifier {$$=_asn1_add_node(TYPE_DEFAULT);
192 : 34 : _asn1_set_value($$,$2,strlen($2)+1);}
193 : 35 : | DEFAULT ASN1_TRUE {$$=_asn1_add_node(TYPE_DEFAULT|CONST_TRUE);}
194 : 11 : | DEFAULT ASN1_FALSE {$$=_asn1_add_node(TYPE_DEFAULT|CONST_FALSE);}
195 : 10 : ;
196 : :
197 : :
198 : : pos_neg_list: pos_neg_num
199 : : | pos_neg_list '|' pos_neg_num
200 : : ;
201 : :
202 : :
203 : 106 : integer_def: INTEGER {$$=_asn1_add_node(TYPE_INTEGER);}
204 : 116 : | INTEGER'{'constant_list'}' {$$=_asn1_add_node(TYPE_INTEGER|CONST_LIST);
205 : 10 : _asn1_set_down($$,$3);}
206 : 47 : | integer_def'(' pos_neg_list ')' {$$=_asn1_add_node(TYPE_INTEGER);}
207 : 37 : | integer_def'('num_identifier'.''.'num_identifier')'
208 : 14 : {$$=_asn1_add_node(TYPE_INTEGER|CONST_MIN_MAX);
209 : 14 : _asn1_set_down($$,_asn1_add_node(TYPE_SIZE));
210 : 14 : _asn1_set_value(_asn1_get_down($$),$6,strlen($6)+1);
211 : 14 : _asn1_set_name(_asn1_get_down($$),$3);}
212 : 14 : ;
213 : :
214 : 12 : boolean_def: BOOLEAN {$$=_asn1_add_node(TYPE_BOOLEAN);}
215 : 12 : ;
216 : :
217 : 3 : Time: UTCTime {$$=_asn1_add_node(TYPE_TIME|CONST_UTC);}
218 : 13 : | GeneralizedTime {$$=_asn1_add_node(TYPE_TIME|CONST_GENERALIZED);}
219 : 10 : ;
220 : :
221 : 35 : size_def2: SIZE'('num_identifier')' {$$=_asn1_add_node(TYPE_SIZE|CONST_1_PARAM);
222 : 35 : _asn1_set_value($$,$3,strlen($3)+1);}
223 : 35 : | SIZE'('num_identifier'.''.'num_identifier')'
224 : 163 : {$$=_asn1_add_node(TYPE_SIZE|CONST_MIN_MAX);
225 : 163 : _asn1_set_value($$,$3,strlen($3)+1);
226 : 163 : _asn1_set_name($$,$6);}
227 : 163 : ;
228 : :
229 : 38 : size_def: size_def2 {$$=$1;}
230 : 38 : | '(' size_def2 ')' {$$=$2;}
231 : 160 : ;
232 : :
233 : 23 : generalstring_def: GeneralString {$$=_asn1_add_node(TYPE_GENERALSTRING);}
234 : 23 : | GeneralString size_def {$$=_asn1_add_node(TYPE_GENERALSTRING|CONST_SIZE);
235 : 0 : _asn1_set_down($$,$2);}
236 : 0 : ;
237 : :
238 : 54 : octet_string_def : OCTET STRING {$$=_asn1_add_node(TYPE_OCTET_STRING);}
239 : 55 : | OCTET STRING size_def {$$=_asn1_add_node(TYPE_OCTET_STRING|CONST_SIZE);
240 : 1 : _asn1_set_down($$,$3);}
241 : 1 : ;
242 : :
243 : 52 : bit_element : IDENTIFIER'('NUM')' {$$=_asn1_add_node(TYPE_CONSTANT);
244 : 52 : _asn1_set_name($$,$1);
245 : 52 : _asn1_set_value($$,$3,strlen($3)+1);}
246 : 52 : ;
247 : :
248 : 7 : bit_element_list : bit_element {$$=$1;}
249 : 7 : | bit_element_list ',' bit_element {$$=$1;
250 : 45 : _asn1_set_right(_asn1_get_last_right($1),$3);}
251 : 45 : ;
252 : :
253 : 12 : bit_string_def : BIT STRING {$$=_asn1_add_node(TYPE_BIT_STRING);}
254 : 33 : | BIT STRING size_def {$$=_asn1_add_node(TYPE_BIT_STRING|CONST_SIZE);}
255 : 21 : | BIT STRING'{'bit_element_list'}'
256 : 4 : {$$=_asn1_add_node(TYPE_BIT_STRING|CONST_LIST);
257 : 4 : _asn1_set_down($$,$4);}
258 : 4 : ;
259 : :
260 : : enumerated_def : ENUMERATED'{'bit_element_list'}'
261 : 3 : {$$=_asn1_add_node(TYPE_ENUMERATED|CONST_LIST);
262 : 3 : _asn1_set_down($$,$3);}
263 : 3 : ;
264 : :
265 : :
266 : 54 : object_def : OBJECT STR_IDENTIFIER {$$=_asn1_add_node(TYPE_OBJECT_ID);}
267 : 54 : ;
268 : :
269 : 322 : type_assig_right: IDENTIFIER {$$=_asn1_add_node(TYPE_IDENTIFIER);
270 : 322 : _asn1_set_value($$,$1,strlen($1)+1);}
271 : 460 : | IDENTIFIER size_def {$$=_asn1_add_node(TYPE_IDENTIFIER|CONST_SIZE);
272 : 138 : _asn1_set_value($$,$1,strlen($1)+1);
273 : 138 : _asn1_set_down($$,$2);}
274 : 138 : | integer_def {$$=$1;}
275 : 116 : | enumerated_def {$$=$1;}
276 : 3 : | boolean_def {$$=$1;}
277 : 12 : | Time
278 : 55 : | octet_string_def {$$=$1;}
279 : 55 : | bit_string_def {$$=$1;}
280 : 37 : | generalstring_def {$$=$1;}
281 : 23 : | sequence_def {$$=$1;}
282 : 193 : | object_def {$$=$1;}
283 : 54 : | choise_def {$$=$1;}
284 : 37 : | any_def {$$=$1;}
285 : 20 : | set_def {$$=$1;}
286 : 29 : | TOKEN_NULL {$$=_asn1_add_node(TYPE_NULL);}
287 : 0 : ;
288 : :
289 : 804 : type_assig_right_tag : type_assig_right {$$=$1;}
290 : 991 : | tag type_assig_right {$$=_asn1_mod_type($2,CONST_TAG);
291 : 187 : _asn1_set_right($1,_asn1_get_down($$));
292 : 187 : _asn1_set_down($$,$1);}
293 : 187 : ;
294 : :
295 : 378 : type_assig_right_tag_default : type_assig_right_tag {$$=$1;}
296 : 423 : | type_assig_right_tag default {$$=_asn1_mod_type($1,CONST_DEFAULT);
297 : 45 : _asn1_set_right($2,_asn1_get_down($$));
298 : 45 : _asn1_set_down($$,$2);}
299 : 179 : | type_assig_right_tag OPTIONAL {$$=_asn1_mod_type($1,CONST_OPTION);}
300 : 134 : ;
301 : :
302 : 557 : type_assig : IDENTIFIER type_assig_right_tag_default {$$=_asn1_set_name($2,$1);}
303 : 557 : ;
304 : :
305 : 198 : type_assig_list : type_assig {$$=$1;}
306 : 198 : | type_assig_list','type_assig {$$=$1;
307 : 359 : _asn1_set_right(_asn1_get_last_right($1),$3);}
308 : 359 : ;
309 : :
310 : 153 : sequence_def : SEQUENCE'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_SEQUENCE);
311 : 153 : _asn1_set_down($$,$3);}
312 : 163 : | SEQUENCE OF type_assig_right {$$=_asn1_add_node(TYPE_SEQUENCE_OF);
313 : 10 : _asn1_set_down($$,$3);}
314 : 40 : | SEQUENCE size_def OF type_assig_right {$$=_asn1_add_node(TYPE_SEQUENCE_OF|CONST_SIZE);
315 : 30 : _asn1_set_right($2,$4);
316 : 30 : _asn1_set_down($$,$2);}
317 : 30 : ;
318 : :
319 : 8 : set_def : SET'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_SET);
320 : 8 : _asn1_set_down($$,$3);}
321 : 21 : | SET OF type_assig_right {$$=_asn1_add_node(TYPE_SET_OF);
322 : 13 : _asn1_set_down($$,$3);}
323 : 21 : | SET size_def OF type_assig_right {$$=_asn1_add_node(TYPE_SET_OF|CONST_SIZE);
324 : 8 : _asn1_set_right($2,$4);
325 : 8 : _asn1_set_down($$,$2);}
326 : 8 : ;
327 : :
328 : 37 : choise_def : CHOICE'{'type_assig_list'}' {$$=_asn1_add_node(TYPE_CHOICE);
329 : 37 : _asn1_set_down($$,$3);}
330 : 37 : ;
331 : :
332 : 6 : any_def : ANY {$$=_asn1_add_node(TYPE_ANY);}
333 : 20 : | ANY DEFINED BY IDENTIFIER {$$=_asn1_add_node(TYPE_ANY|CONST_DEFINED_BY);
334 : 14 : _asn1_set_down($$,_asn1_add_node(TYPE_CONSTANT));
335 : 14 : _asn1_set_name(_asn1_get_down($$),$4);}
336 : 14 : ;
337 : :
338 : 433 : type_def : IDENTIFIER "::=" type_assig_right_tag {$$=_asn1_set_name($3,$1);}
339 : 433 : ;
340 : :
341 : : constant_def : IDENTIFIER OBJECT STR_IDENTIFIER "::=" '{'obj_constant_list'}'
342 : 162 : {$$=_asn1_add_node(TYPE_OBJECT_ID|CONST_ASSIGN);
343 : 162 : _asn1_set_name($$,$1);
344 : 162 : _asn1_set_down($$,$6);}
345 : 162 : | IDENTIFIER IDENTIFIER "::=" '{' obj_constant_list '}'
346 : 43 : {$$=_asn1_add_node(TYPE_OBJECT_ID|CONST_ASSIGN|CONST_1_PARAM);
347 : 43 : _asn1_set_name($$,$1);
348 : 43 : _asn1_set_value($$,$2,strlen($2)+1);
349 : 43 : _asn1_set_down($$,$5);}
350 : 43 : | IDENTIFIER INTEGER "::=" pos_neg_num
351 : 119 : {$$=_asn1_add_node(TYPE_INTEGER|CONST_ASSIGN);
352 : 119 : _asn1_set_name($$,$1);
353 : 119 : _asn1_set_value($$,$4,strlen($4)+1);}
354 : 119 : ;
355 : :
356 : 433 : type_constant: type_def {$$=$1;}
357 : 433 : | constant_def {$$=$1;}
358 : 324 : ;
359 : :
360 : 27 : type_constant_list : type_constant {$$=$1;}
361 : 27 : | type_constant_list type_constant {$$=$1;
362 : 730 : _asn1_set_right(_asn1_get_last_right($1),$2);}
363 : 730 : ;
364 : :
365 : 27 : definitions_id : IDENTIFIER '{' obj_constant_list '}' {$$=_asn1_add_node(TYPE_OBJECT_ID);
366 : 27 : _asn1_set_down($$,$3);
367 : 27 : _asn1_set_name($$,$1);}
368 : 31 : | IDENTIFIER '{' '}' {$$=_asn1_add_node(TYPE_OBJECT_ID);
369 : 4 : _asn1_set_name($$,$1);}
370 : 4 : ;
371 : :
372 : : /*
373 : : identifier_list : IDENTIFIER {$$=_asn1_add_node(TYPE_IDENTIFIER);
374 : : _asn1_set_name($$,$1);}
375 : : | identifier_list IDENTIFIER
376 : : {$$=$1;
377 : : _asn1_set_right(_asn1_get_last_right($$),_asn1_add_node(TYPE_IDENTIFIER));
378 : : _asn1_set_name(_asn1_get_last_right($$),$2);}
379 : : ;
380 : :
381 : :
382 : : imports_def : empty {$$=NULL;}
383 : : | IMPORTS identifier_list FROM IDENTIFIER obj_constant_list
384 : : {$$=_asn1_add_node(TYPE_IMPORTS);
385 : : _asn1_set_down($$,_asn1_add_node(TYPE_OBJECT_ID));
386 : : _asn1_set_name(_asn1_get_down($$),$4);
387 : : _asn1_set_down(_asn1_get_down($$),$5);
388 : : _asn1_set_right($$,$2);}
389 : : ;
390 : : */
391 : :
392 : 0 : explicit_implicit : EXPLICIT {$$=CONST_EXPLICIT;}
393 : 0 : | IMPLICIT {$$=CONST_IMPLICIT;}
394 : 31 : ;
395 : :
396 : :
397 : : %%
398 : :
399 : :
400 : :
401 : : static const char *key_word[] = {
402 : : "::=","OPTIONAL","INTEGER","SIZE","OCTET","STRING"
403 : : ,"SEQUENCE","BIT","UNIVERSAL","PRIVATE","OPTIONAL"
404 : : ,"DEFAULT","CHOICE","OF","OBJECT","IDENTIFIER"
405 : : ,"BOOLEAN","TRUE","FALSE","APPLICATION","ANY","DEFINED"
406 : : ,"SET","BY","EXPLICIT","IMPLICIT","DEFINITIONS","TAGS"
407 : : ,"BEGIN","END","UTCTime","GeneralizedTime"
408 : : ,"GeneralString","FROM","IMPORTS","NULL","ENUMERATED"};
409 : : static const int key_word_token[] = {
410 : : ASSIG,OPTIONAL,INTEGER,SIZE,OCTET,STRING
411 : : ,SEQUENCE,BIT,UNIVERSAL,PRIVATE,OPTIONAL
412 : : ,DEFAULT,CHOICE,OF,OBJECT,STR_IDENTIFIER
413 : : ,BOOLEAN,ASN1_TRUE,ASN1_FALSE,APPLICATION,ANY,DEFINED
414 : : ,SET,BY,EXPLICIT,IMPLICIT,DEFINITIONS,TAGS
415 : : ,BEGIN,END,UTCTime,GeneralizedTime
416 : : ,GeneralString,FROM,IMPORTS,TOKEN_NULL,ENUMERATED};
417 : :
418 : : /*************************************************************/
419 : : /* Function: _asn1_yylex */
420 : : /* Description: looks for tokens in file_asn1 pointer file. */
421 : : /* Return: int */
422 : : /* Token identifier or ASCII code or 0(zero: End Of File) */
423 : : /*************************************************************/
424 : : static int
425 : 10453 : _asn1_yylex()
426 : : {
427 : 10453 : int c,counter=0,k,lastc;
428 : : char string[ASN1_MAX_NAME_SIZE+1]; /* will contain the next token */
429 : : size_t i;
430 : :
431 : : while(1)
432 : : {
433 [ + + ][ + + ]: 29226 : while((c=fgetc(file_asn1))==' ' || c=='\t' || c=='\n')
[ + + ]
434 [ + + ]: 17825 : if(c=='\n') lineNumber++;
435 : :
436 [ + + ]: 11401 : if(c==EOF){
437 : 25 : strcpy(lastToken,"End Of File");
438 : 25 : return 0;
439 : : }
440 : :
441 [ + + ][ + + ]: 11376 : if(c=='(' || c==')' || c=='[' || c==']' ||
[ + + ][ + + ]
[ + + ]
442 [ + + ][ + + ]: 8892 : c=='{' || c=='}' || c==',' || c=='.' ||
[ + + ][ + - ]
443 [ + + ]: 7658 : c=='+' || c=='|'){
444 : 3740 : lastToken[0]=c;lastToken[1]=0;
445 : 3740 : return c;
446 : : }
447 [ + + ]: 7636 : if(c=='-'){ /* Maybe the first '-' of a comment */
448 [ + + ]: 951 : if((c=fgetc(file_asn1))!='-'){
449 : 3 : ungetc(c,file_asn1);
450 : 3 : lastToken[0]='-';lastToken[1]=0;
451 : 3 : return '-';
452 : : }
453 : : else{ /* Comments */
454 : 948 : lastc=0;
455 : 948 : counter=0;
456 : : /* A comment finishes at the next double hypen or the end of line */
457 [ + - ][ + + ]: 31051 : while((c=fgetc(file_asn1))!=EOF && c!='\n' &&
[ + + ]
458 [ + - ][ + + ]: 319 : (lastc!='-' || (lastc=='-' && c!='-')))
459 : 30103 : lastc=c;
460 [ - + ]: 948 : if(c==EOF){
461 : 0 : strcpy(lastToken,"End Of File");
462 : 0 : return 0;
463 : : }
464 : : else{
465 [ + + ]: 948 : if(c=='\n') lineNumber++;
466 : 948 : continue; /* next char, please! (repeat the search) */
467 : : }
468 : : }
469 : : }
470 : 6685 : string[counter++]=c;
471 : : /* Till the end of the token */
472 [ + - ][ + + ]: 96306 : while(!((c=fgetc(file_asn1))==EOF || c==' '|| c=='\t' || c=='\n' ||
[ + + ][ + + ]
[ + + ][ + + ]
473 [ + + ][ + + ]: 46151 : c=='(' || c==')' || c=='[' || c==']' ||
[ + + ][ + + ]
474 [ + + ][ + + ]: 45242 : c=='{' || c=='}' || c==',' || c=='.'))
475 : : {
476 [ + + ]: 44724 : if(counter>=ASN1_MAX_NAME_SIZE){
477 : 1 : result_parse=ASN1_NAME_TOO_LONG;
478 : 1 : return 0;
479 : : }
480 : 44723 : string[counter++]=c;
481 : : }
482 : 6684 : ungetc(c,file_asn1);
483 : 6684 : string[counter]=0;
484 : 6684 : strcpy(lastToken,string);
485 : :
486 : : /* Is STRING a number? */
487 [ + + ]: 8385 : for(k=0;k<counter;k++)
488 [ + + ]: 7089 : if(!isdigit(string[k])) break;
489 [ + + ]: 6684 : if(k>=counter)
490 : : {
491 : 1296 : strcpy(yylval.str,string);
492 : 1296 : return NUM; /* return the number */
493 : : }
494 : :
495 : : /* Is STRING a keyword? */
496 [ + + ]: 124938 : for(i=0;i<(sizeof(key_word)/sizeof(char*));i++)
497 [ + + ]: 122285 : if(!strcmp(string,key_word[i])) return key_word_token[i];
498 : :
499 : : /* STRING is an IDENTIFIER */
500 : 2653 : strcpy(yylval.str,string);
501 : 10453 : return IDENTIFIER;
502 : 948 : }
503 : : }
504 : :
505 : : /*************************************************************/
506 : : /* Function: _asn1_create_errorDescription */
507 : : /* Description: creates a string with the description of the*/
508 : : /* error. */
509 : : /* Parameters: */
510 : : /* error : error to describe. */
511 : : /* errorDescription: string that will contain the */
512 : : /* description. */
513 : : /*************************************************************/
514 : : static void
515 : 31 : _asn1_create_errorDescription(int error,char *errorDescription)
516 : : {
517 [ + + + + : 31 : switch(error){
- ]
518 : : case ASN1_SUCCESS: case ASN1_FILE_NOT_FOUND:
519 [ + - ]: 20 : if (errorDescription!=NULL) errorDescription[0]=0;
520 : 20 : break;
521 : : case ASN1_SYNTAX_ERROR:
522 [ + - ]: 5 : if (errorDescription!=NULL) {
523 : 5 : strcpy(errorDescription,fileName);
524 : 5 : strcat(errorDescription,":");
525 : 5 : _asn1_ltostr(lineNumber,errorDescription+strlen(fileName)+1);
526 : 5 : strcat(errorDescription,": parse error near '");
527 : 5 : strcat(errorDescription,lastToken);
528 : 5 : strcat(errorDescription,"'");
529 : : }
530 : 5 : break;
531 : : case ASN1_NAME_TOO_LONG:
532 [ + - ]: 1 : if (errorDescription!=NULL) {
533 : 1 : strcpy(errorDescription,fileName);
534 : 1 : strcat(errorDescription,":");
535 : 1 : _asn1_ltostr(lineNumber,errorDescription+strlen(fileName)+1);
536 : 1 : strcat(errorDescription,": name too long (more than ");
537 : 1 : _asn1_ltostr(ASN1_MAX_NAME_SIZE,errorDescription+strlen(errorDescription));
538 : 1 : strcat(errorDescription," characters)");
539 : : }
540 : 1 : break;
541 : : case ASN1_IDENTIFIER_NOT_FOUND:
542 [ + - ]: 5 : if (errorDescription!=NULL) {
543 : 5 : strcpy(errorDescription,fileName);
544 : 5 : strcat(errorDescription,":");
545 : 5 : strcat(errorDescription,": identifier '");
546 : 5 : strcat(errorDescription,_asn1_identifierMissing);
547 : 5 : strcat(errorDescription,"' not found");
548 : : }
549 : 5 : break;
550 : : default:
551 [ # # ]: 0 : if (errorDescription!=NULL) errorDescription[0]=0;
552 : 0 : break;
553 : : }
554 : :
555 : 31 : }
556 : :
557 : : /**
558 : : * asn1_parser2tree:
559 : : * @file_name: specify the path and the name of file that contains
560 : : * ASN.1 declarations.
561 : : * @definitions: return the pointer to the structure created from
562 : : * "file_name" ASN.1 declarations.
563 : : * @errorDescription: return the error description or an empty
564 : : * string if success.
565 : : *
566 : : * Function used to start the parse algorithm. Creates the structures
567 : : * needed to manage the definitions included in @file_name file.
568 : : *
569 : : * Returns: %ASN1_SUCCESS if the file has a correct syntax and every
570 : : * identifier is known, %ASN1_ELEMENT_NOT_EMPTY if @definitions not
571 : : * %ASN1_TYPE_EMPTY, %ASN1_FILE_NOT_FOUND if an error occured while
572 : : * opening @file_name, %ASN1_SYNTAX_ERROR if the syntax is not
573 : : * correct, %ASN1_IDENTIFIER_NOT_FOUND if in the file there is an
574 : : * identifier that is not defined, %ASN1_NAME_TOO_LONG if in the
575 : : * file there is an identifier whith more than %ASN1_MAX_NAME_SIZE
576 : : * characters.
577 : : **/
578 : : asn1_retCode
579 : 31 : asn1_parser2tree(const char *file_name, ASN1_TYPE *definitions,
580 : : char *errorDescription){
581 : :
582 : 31 : p_tree=ASN1_TYPE_EMPTY;
583 : :
584 [ - + ]: 31 : if(*definitions != ASN1_TYPE_EMPTY)
585 : 0 : return ASN1_ELEMENT_NOT_EMPTY;
586 : :
587 : 31 : *definitions=ASN1_TYPE_EMPTY;
588 : :
589 : 31 : fileName = file_name;
590 : :
591 : : /* open the file to parse */
592 : 31 : file_asn1=fopen(file_name,"r");
593 : :
594 [ - + ]: 31 : if(file_asn1==NULL){
595 : 0 : result_parse=ASN1_FILE_NOT_FOUND;
596 : : }
597 : : else{
598 : 31 : result_parse=ASN1_SUCCESS;
599 : :
600 : 31 : lineNumber=1;
601 : 31 : yyparse();
602 : :
603 : 31 : fclose(file_asn1);
604 : :
605 [ + + ]: 31 : if(result_parse==ASN1_SUCCESS){ /* syntax OK */
606 : : /* set IMPLICIT or EXPLICIT property */
607 : 25 : _asn1_set_default_tag(p_tree);
608 : : /* set CONST_SET and CONST_NOT_USED */
609 : 25 : _asn1_type_set_config(p_tree);
610 : : /* check the identifier definitions */
611 : 25 : result_parse=_asn1_check_identifier(p_tree);
612 [ + + ]: 25 : if(result_parse==ASN1_SUCCESS){ /* all identifier defined */
613 : : /* Delete the list and keep the ASN1 structure */
614 : 20 : _asn1_delete_list();
615 : : /* Convert into DER coding the value assign to INTEGER constants */
616 : 20 : _asn1_change_integer_value(p_tree);
617 : : /* Expand the IDs of OBJECT IDENTIFIER constants */
618 : 20 : _asn1_expand_object_id(p_tree);
619 : :
620 : 20 : *definitions=p_tree;
621 : : }
622 : : else /* some identifiers not defined */
623 : : /* Delete the list and the ASN1 structure */
624 : 5 : _asn1_delete_list_and_nodes();
625 : : }
626 : : else /* syntax error */
627 : : /* Delete the list and the ASN1 structure */
628 : 6 : _asn1_delete_list_and_nodes();
629 : : }
630 : :
631 [ + - ]: 31 : if (errorDescription!=NULL)
632 : 31 : _asn1_create_errorDescription(result_parse,errorDescription);
633 : :
634 : 31 : return result_parse;
635 : : }
636 : :
637 : : /**
638 : : * asn1_parser2array:
639 : : * @inputFileName: specify the path and the name of file that
640 : : * contains ASN.1 declarations.
641 : : * @outputFileName: specify the path and the name of file that will
642 : : * contain the C vector definition.
643 : : * @vectorName: specify the name of the C vector.
644 : : * @errorDescription : return the error description or an empty
645 : : * string if success.
646 : : *
647 : : * Function that generates a C structure from an ASN1 file. Creates a
648 : : * file containing a C vector to use to manage the definitions
649 : : * included in @inputFileName file. If @inputFileName is
650 : : * "/aa/bb/xx.yy" and @outputFileName is %NULL, the file created is
651 : : * "/aa/bb/xx_asn1_tab.c". If @vectorName is %NULL the vector name
652 : : * will be "xx_asn1_tab".
653 : : *
654 : : * Returns: %ASN1_SUCCESS if the file has a correct syntax and every
655 : : * identifier is known, %ASN1_FILE_NOT_FOUND if an error occured
656 : : * while opening @inputFileName, %ASN1_SYNTAX_ERROR if the syntax is
657 : : * not correct, %ASN1_IDENTIFIER_NOT_FOUND if in the file there is
658 : : * an identifier that is not defined, %ASN1_NAME_TOO_LONG if in the
659 : : * file there is an identifier whith more than %ASN1_MAX_NAME_SIZE
660 : : * characters.
661 : : **/
662 : 0 : int asn1_parser2array(const char *inputFileName,const char *outputFileName,
663 : : const char *vectorName,char *errorDescription){
664 : 0 : char *file_out_name=NULL;
665 : 0 : char *vector_name=NULL;
666 : : const char *char_p,*slash_p,*dot_p;
667 : :
668 : 0 : p_tree=NULL;
669 : :
670 : 0 : fileName = inputFileName;
671 : :
672 : : /* open the file to parse */
673 : 0 : file_asn1=fopen(inputFileName,"r");
674 : :
675 [ # # ]: 0 : if(file_asn1==NULL)
676 : 0 : result_parse=ASN1_FILE_NOT_FOUND;
677 : : else{
678 : 0 : result_parse=ASN1_SUCCESS;
679 : :
680 : 0 : lineNumber=1;
681 : 0 : yyparse();
682 : :
683 : 0 : fclose(file_asn1);
684 : :
685 [ # # ]: 0 : if(result_parse==ASN1_SUCCESS){ /* syntax OK */
686 : : /* set IMPLICIT or EXPLICIT property */
687 : 0 : _asn1_set_default_tag(p_tree);
688 : : /* set CONST_SET and CONST_NOT_USED */
689 : 0 : _asn1_type_set_config(p_tree);
690 : : /* check the identifier definitions */
691 : 0 : result_parse=_asn1_check_identifier(p_tree);
692 : :
693 [ # # ]: 0 : if(result_parse==ASN1_SUCCESS){ /* all identifier defined */
694 : :
695 : : /* searching the last '/' and '.' in inputFileName */
696 : 0 : char_p=inputFileName;
697 : 0 : slash_p=inputFileName;
698 [ # # ]: 0 : while((char_p=strchr(char_p,'/'))){
699 : 0 : char_p++;
700 : 0 : slash_p=char_p;
701 : : }
702 : :
703 : 0 : char_p=slash_p;
704 : 0 : dot_p=inputFileName+strlen(inputFileName);
705 : :
706 [ # # ]: 0 : while((char_p=strchr(char_p,'.'))){
707 : 0 : dot_p=char_p;
708 : 0 : char_p++;
709 : : }
710 : :
711 [ # # ]: 0 : if(outputFileName == NULL){
712 : : /* file_out_name = inputFileName + _asn1_tab.c */
713 : 0 : file_out_name=(char *)malloc(dot_p-inputFileName+1+
714 : : strlen("_asn1_tab.c"));
715 : 0 : memcpy(file_out_name,inputFileName,dot_p-inputFileName);
716 : 0 : file_out_name[dot_p-inputFileName]=0;
717 : 0 : strcat(file_out_name,"_asn1_tab.c");
718 : : }
719 : : else{
720 : : /* file_out_name = inputFileName */
721 : 0 : file_out_name=(char *)malloc(strlen(outputFileName)+1);
722 : 0 : strcpy(file_out_name,outputFileName);
723 : : }
724 : :
725 [ # # ]: 0 : if(vectorName == NULL){
726 : : /* vector_name = file name + _asn1_tab */
727 : 0 : vector_name=(char *)malloc(dot_p-slash_p+1+
728 : : strlen("_asn1_tab"));
729 : 0 : memcpy(vector_name,slash_p,dot_p-slash_p);
730 : 0 : vector_name[dot_p-slash_p]=0;
731 : 0 : strcat(vector_name,"_asn1_tab");
732 : : }
733 : : else{
734 : : /* vector_name = vectorName */
735 : 0 : vector_name=(char *)malloc(strlen(vectorName)+1);
736 : 0 : strcpy(vector_name,vectorName);
737 : : }
738 : :
739 : : /* Save structure in a file */
740 : 0 : _asn1_create_static_structure(p_tree,
741 : : file_out_name,vector_name);
742 : :
743 : 0 : free(file_out_name);
744 : 0 : free(vector_name);
745 : : } /* result == OK */
746 : : } /* result == OK */
747 : :
748 : : /* Delete the list and the ASN1 structure */
749 : 0 : _asn1_delete_list_and_nodes();
750 : : } /* inputFile exist */
751 : :
752 [ # # ]: 0 : if (errorDescription!=NULL)
753 : 0 : _asn1_create_errorDescription(result_parse,errorDescription);
754 : :
755 : 0 : return result_parse;
756 : : }
757 : :
758 : : /*************************************************************/
759 : : /* Function: _asn1_yyerror */
760 : : /* Description: function called when there are syntax errors*/
761 : : /* Parameters: */
762 : : /* char *s : error description */
763 : : /* Return: int */
764 : : /* */
765 : : /*************************************************************/
766 : 6 : static int _asn1_yyerror (const char *s)
767 : : {
768 : : /* Sends the error description to the std_out */
769 : :
770 : : #if 0
771 : : printf("_asn1_yyerror:%s:%ld: %s (Last Token:'%s')\n",fileName,
772 : : lineNumber,s,lastToken);
773 : : #endif
774 : :
775 [ + + ]: 6 : if(result_parse!=ASN1_NAME_TOO_LONG)
776 : 5 : result_parse=ASN1_SYNTAX_ERROR;
777 : :
778 : 6 : return 0;
779 : : }
|