Add back libtasn1-dont.h stuff.
[libtasn1.git] / lib / parser_aux.c
blob464ad5010ce543eccd4017dfbcee42ff9571de7b
1 /*
2 * Copyright (C) 2004, 2006 Free Software Foundation
3 * Copyright (C) 2000,2001 Fabio Fiorina
5 * This file is part of LIBTASN1.
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.
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.
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
23 #include <int.h>
24 #include <errors.h>
25 #include "parser_aux.h"
26 #include "gstr.h"
27 #include "structure.h"
28 #include "element.h"
30 char _asn1_identifierMissing[MAX_NAME_SIZE+1]; /* identifier name not found */
32 /***********************************************/
33 /* Type: list_type */
34 /* Description: type used in the list during */
35 /* the structure creation. */
36 /***********************************************/
37 typedef struct list_struct{
38 node_asn *node;
39 struct list_struct *next;
40 } list_type;
43 /* Pointer to the first element of the list */
44 list_type *firstElement=NULL;
46 /******************************************************/
47 /* Function : _asn1_add_node */
48 /* Description: creates a new NODE_ASN element and */
49 /* puts it in the list pointed by firstElement. */
50 /* Parameters: */
51 /* type: type of the new element (see TYPE_ */
52 /* and CONST_ constants). */
53 /* Return: pointer to the new element. */
54 /******************************************************/
55 node_asn *
56 _asn1_add_node(unsigned int type)
58 list_type *listElement;
59 node_asn *punt;
61 punt=(node_asn *) _asn1_malloc(sizeof(node_asn));
62 if (punt==NULL) return NULL;
64 listElement=(list_type *) _asn1_malloc(sizeof(list_type));
65 if(listElement==NULL){
66 _asn1_free(punt);
67 return NULL;
70 listElement->node=punt;
71 listElement->next=firstElement;
72 firstElement=listElement;
74 punt->left=NULL;
75 punt->name=NULL;
76 punt->type=type;
77 punt->value=NULL;
78 punt->down=NULL;
79 punt->right=NULL;
81 return punt;
84 /**
85 * asn1_find_node:
86 * @pointer: NODE_ASN element pointer.
87 * @name: null terminated string with the element's name to find.
89 * Searches for an element called NAME starting from POINTER. The
90 * name is composed by differents identifiers separated by dots. When
91 * *POINTER has a name, the first identifier must be the name of
92 * *POINTER, otherwise it must be the name of one child of *POINTER.
94 * Return value: the searching result. NULL if not found.
95 **/
96 ASN1_TYPE
97 asn1_find_node(ASN1_TYPE pointer, const char *name)
99 node_asn *p;
100 char *n_end,n[MAX_NAME_SIZE+1];
101 const char *n_start;
103 if(pointer == NULL) return NULL;
105 if(name==NULL) return NULL;
107 p=pointer;
108 n_start=name;
110 if(p->name != NULL){ /* has *pointer got a name ? */
111 n_end=strchr(n_start,'.'); /* search the first dot */
112 if(n_end){
113 memcpy(n,n_start,n_end-n_start);
114 n[n_end-n_start]=0;
115 n_start=n_end;
116 n_start++;
118 else{
119 _asn1_str_cpy(n,sizeof(n),n_start);
120 n_start=NULL;
123 while(p){
124 if((p->name) && (!strcmp(p->name,n))) break;
125 else p=p->right;
126 } /* while */
128 if(p==NULL) return NULL;
130 else{ /* *pointer doesn't have a name */
131 if(n_start[0]==0)
132 return p;
135 while(n_start){ /* Has the end of NAME been reached? */
136 n_end=strchr(n_start,'.'); /* search the next dot */
137 if(n_end){
138 memcpy(n,n_start,n_end-n_start);
139 n[n_end-n_start]=0;
140 n_start=n_end;
141 n_start++;
143 else{
144 _asn1_str_cpy(n,sizeof(n),n_start);
145 n_start=NULL;
148 if(p->down==NULL) return NULL;
150 p=p->down;
152 /* The identifier "?LAST" indicates the last element
153 in the right chain. */
154 if(!strcmp(n,"?LAST")){
155 if(p==NULL) return NULL;
156 while(p->right) p=p->right;
158 else{ /* no "?LAST" */
159 while(p){
160 if((p->name) && (!strcmp(p->name,n))) break;
161 else p=p->right;
163 if(p==NULL) return NULL;
165 } /* while */
167 return p;
171 /******************************************************************/
172 /* Function : _asn1_set_value */
173 /* Description: sets the field VALUE in a NODE_ASN element. The */
174 /* previous value (if exist) will be lost */
175 /* Parameters: */
176 /* node: element pointer. */
177 /* value: pointer to the value that you want to set. */
178 /* len: character number of value. */
179 /* Return: pointer to the NODE_ASN element. */
180 /******************************************************************/
181 node_asn *
182 _asn1_set_value(node_asn *node,const unsigned char *value,unsigned int len)
185 if(node==NULL) return node;
186 if(node->value){
187 _asn1_free(node->value);
188 node->value=NULL;
189 node->value_len = 0;
191 if(!len) return node;
192 node->value=(unsigned char *) _asn1_malloc(len);
193 if (node->value==NULL) return NULL;
194 node->value_len = len;
196 memcpy(node->value,value,len);
197 return node;
200 /******************************************************************/
201 /* Function : _asn1_set_name */
202 /* Description: sets the field NAME in a NODE_ASN element. The */
203 /* previous value (if exist) will be lost */
204 /* Parameters: */
205 /* node: element pointer. */
206 /* name: a null terminated string with the name that you want */
207 /* to set. */
208 /* Return: pointer to the NODE_ASN element. */
209 /******************************************************************/
210 node_asn *
211 _asn1_set_name(node_asn *node,const char *name)
213 if(node==NULL) return node;
215 if(node->name){
216 _asn1_free(node->name);
217 node->name=NULL;
220 if(name==NULL) return node;
222 if(strlen(name))
224 node->name=(char *) _asn1_strdup( name);
225 if (node->name==NULL) return NULL;
227 else node->name=NULL;
228 return node;
231 /******************************************************************/
232 /* Function : _asn1_set_right */
233 /* Description: sets the field RIGHT in a NODE_ASN element. */
234 /* Parameters: */
235 /* node: element pointer. */
236 /* right: pointer to a NODE_ASN element that you want be pointed*/
237 /* by NODE. */
238 /* Return: pointer to *NODE. */
239 /******************************************************************/
240 node_asn *
241 _asn1_set_right(node_asn *node,node_asn *right)
243 if(node==NULL) return node;
244 node->right=right;
245 if(right) right->left=node;
246 return node;
249 /******************************************************************/
250 /* Function : _asn1_get_right */
251 /* Description: returns the element pointed by the RIGHT field of */
252 /* a NODE_ASN element. */
253 /* Parameters: */
254 /* node: NODE_ASN element pointer. */
255 /* Return: field RIGHT of NODE. */
256 /******************************************************************/
257 node_asn *
258 _asn1_get_right(node_asn *node)
260 if(node==NULL) return NULL;
261 return node->right;
264 /******************************************************************/
265 /* Function : _asn1_get_last_right */
266 /* Description: return the last element along the right chain. */
267 /* Parameters: */
268 /* node: starting element pointer. */
269 /* Return: pointer to the last element along the right chain. */
270 /******************************************************************/
271 node_asn *
272 _asn1_get_last_right(node_asn *node)
274 node_asn *p;
276 if(node==NULL) return NULL;
277 p=node;
278 while(p->right) p=p->right;
279 return p;
282 /******************************************************************/
283 /* Function : _asn1_set_down */
284 /* Description: sets the field DOWN in a NODE_ASN element. */
285 /* Parameters: */
286 /* node: element pointer. */
287 /* down: pointer to a NODE_ASN element that you want be pointed */
288 /* by NODE. */
289 /* Return: pointer to *NODE. */
290 /******************************************************************/
291 node_asn *
292 _asn1_set_down(node_asn *node,node_asn *down)
294 if(node==NULL) return node;
295 node->down=down;
296 if(down) down->left=node;
297 return node;
300 /******************************************************************/
301 /* Function : _asn1_get_down */
302 /* Description: returns the element pointed by the DOWN field of */
303 /* a NODE_ASN element. */
304 /* Parameters: */
305 /* node: NODE_ASN element pointer. */
306 /* Return: field DOWN of NODE. */
307 /******************************************************************/
308 node_asn *
309 _asn1_get_down(node_asn *node)
311 if(node==NULL) return NULL;
312 return node->down;
315 /******************************************************************/
316 /* Function : _asn1_get_name */
317 /* Description: returns the name of a NODE_ASN element. */
318 /* Parameters: */
319 /* node: NODE_ASN element pointer. */
320 /* Return: a null terminated string. */
321 /******************************************************************/
322 char *
323 _asn1_get_name(node_asn *node)
325 if(node==NULL) return NULL;
326 return node->name;
329 /******************************************************************/
330 /* Function : _asn1_mod_type */
331 /* Description: change the field TYPE of an NODE_ASN element. */
332 /* The new value is the old one | (bitwise or) the */
333 /* paramener VALUE. */
334 /* Parameters: */
335 /* node: NODE_ASN element pointer. */
336 /* value: the integer value that must be or-ed with the current */
337 /* value of field TYPE. */
338 /* Return: NODE pointer. */
339 /******************************************************************/
340 node_asn *
341 _asn1_mod_type(node_asn *node,unsigned int value)
343 if(node==NULL) return node;
344 node->type|=value;
345 return node;
349 /******************************************************************/
350 /* Function : _asn1_remove_node */
351 /* Description: gets free the memory allocated for an NODE_ASN */
352 /* element (not the elements pointed by it). */
353 /* Parameters: */
354 /* node: NODE_ASN element pointer. */
355 /******************************************************************/
356 void
357 _asn1_remove_node(node_asn *node)
359 if(node==NULL) return;
361 if (node->name!=NULL)
362 _asn1_free(node->name);
363 if (node->value!=NULL)
364 _asn1_free(node->value);
365 _asn1_free(node);
369 * asn1_find_up:
370 * @node: NODE_ASN element pointer.
372 * Return the father of the NODE_ASN element.
374 * Return value: Return the father of the node, or %NULL if not found.
376 ASN1_TYPE
377 asn1_find_up(ASN1_TYPE node)
379 node_asn *p;
381 if(node==NULL) return NULL;
383 p=node;
385 while((p->left!=NULL) && (p->left->right==p)) p=p->left;
387 return p->left;
390 /******************************************************************/
391 /* Function : _asn1_delete_list */
392 /* Description: deletes the list elements (not the elements */
393 /* pointed by them). */
394 /******************************************************************/
395 void
396 _asn1_delete_list(void)
398 list_type *listElement;
400 while(firstElement){
401 listElement=firstElement;
402 firstElement=firstElement->next;
403 _asn1_free(listElement);
407 /******************************************************************/
408 /* Function : _asn1_delete_list_and nodes */
409 /* Description: deletes the list elements and the elements */
410 /* pointed by them. */
411 /******************************************************************/
412 void
413 _asn1_delete_list_and_nodes(void)
415 list_type *listElement;
417 while(firstElement){
418 listElement=firstElement;
419 firstElement=firstElement->next;
420 _asn1_remove_node(listElement->node);
421 _asn1_free(listElement);
426 char *
427 _asn1_ltostr(long v,char *str)
429 long d,r;
430 char temp[20];
431 int count,k,start;
433 if(v<0){
434 str[0]='-';
435 start=1;
436 v=-v;
438 else start=0;
440 count=0;
442 d=v/10;
443 r=v-d*10;
444 temp[start+count]='0'+(char)r;
445 count++;
446 v=d;
447 }while(v);
449 for(k=0;k<count;k++) str[k+start]=temp[start+count-k-1];
450 str[count+start]=0;
451 return str;
455 /******************************************************************/
456 /* Function : _asn1_change_integer_value */
457 /* Description: converts into DER coding the value assign to an */
458 /* INTEGER constant. */
459 /* Parameters: */
460 /* node: root of an ASN1element. */
461 /* Return: */
462 /* ASN1_ELEMENT_NOT_FOUND if NODE is NULL, */
463 /* otherwise ASN1_SUCCESS */
464 /******************************************************************/
465 asn1_retCode
466 _asn1_change_integer_value(ASN1_TYPE node)
468 node_asn *p;
469 unsigned char val[SIZEOF_UNSIGNED_LONG_INT];
470 unsigned char val2[SIZEOF_UNSIGNED_LONG_INT+1];
471 int len;
473 if(node==NULL) return ASN1_ELEMENT_NOT_FOUND;
475 p=node;
476 while(p){
477 if((type_field(p->type)==TYPE_INTEGER) && (p->type&CONST_ASSIGN)){
478 if(p->value){
479 _asn1_convert_integer(p->value,val,sizeof(val), &len);
480 asn1_octet_der(val,len,val2,&len);
481 _asn1_set_value(p,val2,len);
485 if(p->down){
486 p=p->down;
488 else{
489 if(p==node) p=NULL;
490 else if(p->right) p=p->right;
491 else{
492 while(1){
493 p=asn1_find_up(p);
494 if(p==node){
495 p=NULL;
496 break;
498 if(p->right){
499 p=p->right;
500 break;
507 return ASN1_SUCCESS;
511 /******************************************************************/
512 /* Function : _asn1_expand_object_id */
513 /* Description: expand the IDs of an OBJECT IDENTIFIER constant. */
514 /* Parameters: */
515 /* node: root of an ASN1 element. */
516 /* Return: */
517 /* ASN1_ELEMENT_NOT_FOUND if NODE is NULL, */
518 /* otherwise ASN1_SUCCESS */
519 /******************************************************************/
520 asn1_retCode
521 _asn1_expand_object_id(ASN1_TYPE node)
523 node_asn *p,*p2,*p3,*p4,*p5;
524 char name_root[MAX_NAME_SIZE],name2[2*MAX_NAME_SIZE+1];
525 int move, tlen;
527 if(node==NULL) return ASN1_ELEMENT_NOT_FOUND;
529 _asn1_str_cpy(name_root, sizeof(name_root), node->name);
531 p=node;
532 move=DOWN;
534 while(!((p==node) && (move==UP))){
535 if(move!=UP){
536 if((type_field(p->type)==TYPE_OBJECT_ID) && (p->type&CONST_ASSIGN)){
537 p2=p->down;
538 if(p2 && (type_field(p2->type)==TYPE_CONSTANT)){
539 if(p2->value && !isdigit(p2->value[0])){
540 _asn1_str_cpy(name2, sizeof(name2), name_root);
541 _asn1_str_cat(name2, sizeof(name2), ".");
542 _asn1_str_cat(name2, sizeof(name2), p2->value);
543 p3=asn1_find_node(node,name2);
544 if(!p3 || (type_field(p3->type)!=TYPE_OBJECT_ID) ||
545 !(p3->type&CONST_ASSIGN)) return ASN1_ELEMENT_NOT_FOUND;
546 _asn1_set_down(p,p2->right);
547 _asn1_remove_node(p2);
548 p2=p;
549 p4=p3->down;
550 while(p4){
551 if(type_field(p4->type)==TYPE_CONSTANT){
552 p5=_asn1_add_node_only(TYPE_CONSTANT);
553 _asn1_set_name(p5,p4->name);
554 tlen = strlen( p4->value);
555 if (tlen > 0)
556 _asn1_set_value(p5,p4->value,tlen+1);
557 if(p2==p){
558 _asn1_set_right(p5,p->down);
559 _asn1_set_down(p,p5);
561 else{
562 _asn1_set_right(p5,p2->right);
563 _asn1_set_right(p2,p5);
565 p2=p5;
567 p4=p4->right;
569 move=DOWN;
570 continue;
574 move=DOWN;
576 else move=RIGHT;
578 if(move==DOWN){
579 if(p->down) p=p->down;
580 else move=RIGHT;
583 if(p==node) {move=UP; continue;}
585 if(move==RIGHT){
586 if(p->right) p=p->right;
587 else move=UP;
589 if(move==UP) p=asn1_find_up(p);
593 /*******************************/
594 /* expand DEFAULT */
595 /*******************************/
596 p=node;
597 move=DOWN;
599 while(!((p==node) && (move==UP))){
600 if(move!=UP){
601 if((type_field(p->type)==TYPE_OBJECT_ID) &&
602 (p->type&CONST_DEFAULT)){
603 p2=p->down;
604 if(p2 && (type_field(p2->type)==TYPE_DEFAULT)){
605 _asn1_str_cpy(name2, sizeof(name2), name_root);
606 _asn1_str_cat(name2, sizeof(name2), ".");
607 _asn1_str_cat(name2, sizeof(name2), p2->value);
608 p3=asn1_find_node(node,name2);
609 if(!p3 || (type_field(p3->type)!=TYPE_OBJECT_ID) ||
610 !(p3->type&CONST_ASSIGN)) return ASN1_ELEMENT_NOT_FOUND;
611 p4=p3->down;
612 name2[0]=0;
613 while(p4){
614 if(type_field(p4->type)==TYPE_CONSTANT){
615 if(name2[0]) _asn1_str_cat(name2,sizeof(name2),".");
616 _asn1_str_cat(name2,sizeof(name2),p4->value);
618 p4=p4->right;
620 tlen = strlen(name2);
621 if (tlen > 0)
622 _asn1_set_value(p2,name2,tlen+1);
625 move=DOWN;
627 else move=RIGHT;
629 if(move==DOWN){
630 if(p->down) p=p->down;
631 else move=RIGHT;
634 if(p==node) {move=UP; continue;}
636 if(move==RIGHT){
637 if(p->right) p=p->right;
638 else move=UP;
640 if(move==UP) p=asn1_find_up(p);
643 return ASN1_SUCCESS;
647 /******************************************************************/
648 /* Function : _asn1_type_set_config */
649 /* Description: sets the CONST_SET and CONST_NOT_USED properties */
650 /* in the fields of the SET elements. */
651 /* Parameters: */
652 /* node: root of an ASN1 element. */
653 /* Return: */
654 /* ASN1_ELEMENT_NOT_FOUND if NODE is NULL, */
655 /* otherwise ASN1_SUCCESS */
656 /******************************************************************/
657 asn1_retCode
658 _asn1_type_set_config(ASN1_TYPE node)
660 node_asn *p,*p2;
661 int move;
663 if(node==NULL) return ASN1_ELEMENT_NOT_FOUND;
665 p=node;
666 move=DOWN;
668 while(!((p==node) && (move==UP))){
669 if(move!=UP){
670 if(type_field(p->type)==TYPE_SET){
671 p2=p->down;
672 while(p2){
673 if(type_field(p2->type)!=TYPE_TAG)
674 p2->type|=CONST_SET|CONST_NOT_USED;
675 p2=p2->right;
678 move=DOWN;
680 else move=RIGHT;
682 if(move==DOWN){
683 if(p->down) p=p->down;
684 else move=RIGHT;
687 if(p==node) {move=UP; continue;}
689 if(move==RIGHT){
690 if(p->right) p=p->right;
691 else move=UP;
693 if(move==UP) p=asn1_find_up(p);
696 return ASN1_SUCCESS;
700 /******************************************************************/
701 /* Function : _asn1_check_identifier */
702 /* Description: checks the definitions of all the identifiers */
703 /* and the first element of an OBJECT_ID (e.g. {pkix 0 4}). */
704 /* The _asn1_identifierMissing global variable is filled if */
705 /* necessary. */
706 /* Parameters: */
707 /* node: root of an ASN1 element. */
708 /* Return: */
709 /* ASN1_ELEMENT_NOT_FOUND if NODE is NULL, */
710 /* ASN1_IDENTIFIER_NOT_FOUND if an identifier is not defined, */
711 /* otherwise ASN1_SUCCESS */
712 /******************************************************************/
713 asn1_retCode
714 _asn1_check_identifier(ASN1_TYPE node)
716 node_asn *p,*p2;
717 char name2[MAX_NAME_SIZE*2+2];
719 if(node==NULL) return ASN1_ELEMENT_NOT_FOUND;
721 p=node;
722 while(p){
723 if(type_field(p->type)==TYPE_IDENTIFIER){
724 _asn1_str_cpy(name2, sizeof(name2), node->name);
725 _asn1_str_cat(name2, sizeof(name2), ".");
726 _asn1_str_cat(name2, sizeof(name2), p->value);
727 p2=asn1_find_node(node,name2);
728 if(p2==NULL){
729 strcpy(_asn1_identifierMissing,p->value);
730 return ASN1_IDENTIFIER_NOT_FOUND;
733 else if((type_field(p->type)==TYPE_OBJECT_ID) &&
734 (p->type&CONST_DEFAULT)){
735 p2=p->down;
736 if(p2 && (type_field(p2->type)==TYPE_DEFAULT)){
737 _asn1_str_cpy(name2, sizeof(name2), node->name);
738 _asn1_str_cat(name2, sizeof(name2), ".");
739 _asn1_str_cat(name2, sizeof(name2), p2->value);
740 strcpy(_asn1_identifierMissing,p2->value);
741 p2=asn1_find_node(node,name2);
742 if(!p2 || (type_field(p2->type)!=TYPE_OBJECT_ID) ||
743 !(p2->type&CONST_ASSIGN))
744 return ASN1_IDENTIFIER_NOT_FOUND;
745 else
746 _asn1_identifierMissing[0]=0;
749 else if((type_field(p->type)==TYPE_OBJECT_ID) &&
750 (p->type&CONST_ASSIGN)){
751 p2=p->down;
752 if(p2 && (type_field(p2->type)==TYPE_CONSTANT)){
753 if(p2->value && !isdigit(p2->value[0])){
754 _asn1_str_cpy(name2, sizeof(name2), node->name);
755 _asn1_str_cat(name2, sizeof(name2), ".");
756 _asn1_str_cat(name2, sizeof(name2), p2->value);
757 strcpy(_asn1_identifierMissing,p2->value);
758 p2=asn1_find_node(node,name2);
759 if(!p2 || (type_field(p2->type)!=TYPE_OBJECT_ID) ||
760 !(p2->type&CONST_ASSIGN))
761 return ASN1_IDENTIFIER_NOT_FOUND;
762 else
763 _asn1_identifierMissing[0]=0;
768 if(p->down){
769 p=p->down;
771 else if(p->right) p=p->right;
772 else{
773 while(1){
774 p=asn1_find_up(p);
775 if(p==node){
776 p=NULL;
777 break;
779 if(p->right){
780 p=p->right;
781 break;
787 return ASN1_SUCCESS;
791 /******************************************************************/
792 /* Function : _asn1_set_default_tag */
793 /* Description: sets the default IMPLICIT or EXPLICIT property in */
794 /* the tagged elements that don't have this declaration. */
795 /* Parameters: */
796 /* node: pointer to a DEFINITIONS element. */
797 /* Return: */
798 /* ASN1_ELEMENT_NOT_FOUND if NODE is NULL or not a pointer to */
799 /* a DEFINITIONS element, */
800 /* otherwise ASN1_SUCCESS */
801 /******************************************************************/
802 asn1_retCode
803 _asn1_set_default_tag(ASN1_TYPE node)
805 node_asn *p;
807 if((node==NULL) || (type_field(node->type)!=TYPE_DEFINITIONS))
808 return ASN1_ELEMENT_NOT_FOUND;
810 p=node;
811 while(p){
812 if((type_field(p->type)==TYPE_TAG) &&
813 !(p->type&CONST_EXPLICIT) &&
814 !(p->type&CONST_IMPLICIT)){
815 if(node->type&CONST_EXPLICIT) p->type|=CONST_EXPLICIT;
816 else p->type|=CONST_IMPLICIT;
819 if(p->down){
820 p=p->down;
822 else if(p->right) p=p->right;
823 else{
824 while(1){
825 p=asn1_find_up(p);
826 if(p==node){
827 p=NULL;
828 break;
830 if(p->right){
831 p=p->right;
832 break;
838 return ASN1_SUCCESS;
843 static const char*
844 parse_version_number( const char *s, int *number )
846 int val = 0;
848 if( *s == '0' && isdigit(s[1]) )
849 return NULL; /* leading zeros are not allowed */
850 for ( ; isdigit(*s); s++ ) {
851 val *= 10;
852 val += *s - '0';
854 *number = val;
855 return val < 0? NULL : s;
858 /* The parse version functions were copied from libgcrypt.
860 static const char *
861 parse_version_string( const char *s, int *major, int *minor, int *micro )
863 s = parse_version_number( s, major );
864 if( !s || *s != '.' )
865 return NULL;
866 s++;
867 s = parse_version_number( s, minor );
868 if( !s || *s != '.' )
869 return NULL;
870 s++;
871 s = parse_version_number( s, micro );
872 if( !s )
873 return NULL;
874 return s; /* patchlevel */
878 * asn1_check_version - check for library version
879 * @req_version: Required version number, or NULL.
881 * Check that the the version of the library is at minimum the
882 * requested one and return the version string; return %NULL if the
883 * condition is not satisfied. If a %NULL is passed to this function,
884 * no check is done, but the version string is simply returned.
886 * See %LIBTASN1_VERSION for a suitable @req_version string.
888 * Return value: Version string of run-time library, or %NULL if the
889 * run-time library does not meet the required version number.
891 const char *
892 asn1_check_version( const char *req_version )
894 const char *ver = LIBTASN1_VERSION;
895 int my_major, my_minor, my_micro;
896 int rq_major, rq_minor, rq_micro;
897 const char *my_plvl, *rq_plvl;
899 if ( !req_version )
900 return ver;
902 my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro );
903 if ( !my_plvl )
904 return NULL; /* very strange our own version is bogus */
905 rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor,
906 &rq_micro );
907 if ( !rq_plvl )
908 return NULL; /* req version string is invalid */
910 if ( my_major > rq_major
911 || (my_major == rq_major && my_minor > rq_minor)
912 || (my_major == rq_major && my_minor == rq_minor
913 && my_micro > rq_micro)
914 || (my_major == rq_major && my_minor == rq_minor
915 && my_micro == rq_micro
916 && strcmp( my_plvl, rq_plvl ) >= 0) ) {
917 return ver;
919 return NULL;