fixes in length calculation in _asn1_extract_der_octet().
[platform/upstream/libtasn1.git] / lib / int.h
1 /*
2  * Copyright (C) 2002-2014 Free Software Foundation, Inc.
3  *
4  * This file is part of LIBTASN1.
5  *
6  * The LIBTASN1 library is free software; you can redistribute it
7  * and/or modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA
20  */
21
22 #ifndef INT_H
23 #define INT_H
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <stdint.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 #include <sys/types.h>
37 #endif
38
39 #include <libtasn1.h>
40
41 #define ASN1_SMALL_VALUE_SIZE 16
42
43 /* This structure is also in libtasn1.h, but then contains less
44    fields.  You cannot make any modifications to these first fields
45    without breaking ABI.  */
46 struct asn1_node_st
47 {
48   /* public fields: */
49   char name[ASN1_MAX_NAME_SIZE + 1];    /* Node name */
50   unsigned int name_hash;
51   unsigned int type;            /* Node type */
52   unsigned char *value;         /* Node value */
53   int value_len;
54   asn1_node down;               /* Pointer to the son node */
55   asn1_node right;              /* Pointer to the brother node */
56   asn1_node left;               /* Pointer to the next list element */
57   /* private fields: */
58   unsigned char small_value[ASN1_SMALL_VALUE_SIZE];     /* For small values */
59 };
60
61 typedef struct tag_and_class_st
62 {
63   unsigned tag;
64   unsigned class;
65   const char *desc;
66 } tag_and_class_st;
67
68 /* the types that are handled in _asn1_tags */
69 #define CASE_HANDLED_ETYPES \
70         case ASN1_ETYPE_NULL: \
71         case ASN1_ETYPE_BOOLEAN: \
72         case ASN1_ETYPE_INTEGER: \
73         case ASN1_ETYPE_ENUMERATED: \
74         case ASN1_ETYPE_OBJECT_ID: \
75         case ASN1_ETYPE_OCTET_STRING: \
76         case ASN1_ETYPE_GENERALSTRING: \
77         case ASN1_ETYPE_NUMERIC_STRING: \
78         case ASN1_ETYPE_IA5_STRING: \
79         case ASN1_ETYPE_TELETEX_STRING: \
80         case ASN1_ETYPE_PRINTABLE_STRING: \
81         case ASN1_ETYPE_UNIVERSAL_STRING: \
82         case ASN1_ETYPE_BMP_STRING: \
83         case ASN1_ETYPE_UTF8_STRING: \
84         case ASN1_ETYPE_VISIBLE_STRING: \
85         case ASN1_ETYPE_BIT_STRING: \
86         case ASN1_ETYPE_SEQUENCE: \
87         case ASN1_ETYPE_SEQUENCE_OF: \
88         case ASN1_ETYPE_SET: \
89         case ASN1_ETYPE_UTC_TIME: \
90         case ASN1_ETYPE_GENERALIZED_TIME: \
91         case ASN1_ETYPE_SET_OF
92
93 #define ETYPE_TAG(etype) (_asn1_tags[etype].tag)
94 #define ETYPE_CLASS(etype) (_asn1_tags[etype].class)
95 #define ETYPE_OK(etype) ((etype != ASN1_ETYPE_INVALID && \
96                           etype <= _asn1_tags_size && \
97                           _asn1_tags[etype].desc != NULL)?1:0)
98
99 extern unsigned int _asn1_tags_size;
100 extern const tag_and_class_st _asn1_tags[];
101
102 #define _asn1_strlen(s) strlen((const char *) s)
103 #define _asn1_strtol(n,e,b) strtol((const char *) n, e, b)
104 #define _asn1_strtoul(n,e,b) strtoul((const char *) n, e, b)
105 #define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b)
106 #define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b)
107 #define _asn1_strcat(a,b) strcat((char *)a, (const char *)b)
108
109 #define MAX_LOG_SIZE 1024       /* maximum number of characters of a log message */
110
111 /* Define used for visiting trees. */
112 #define UP     1
113 #define RIGHT  2
114 #define DOWN   3
115
116 /***********************************************************************/
117 /* List of constants to better specify the type of typedef asn1_node_st.   */
118 /***********************************************************************/
119 /*  Used with TYPE_TAG  */
120 #define CONST_UNIVERSAL   (1<<8)
121 #define CONST_PRIVATE     (1<<9)
122 #define CONST_APPLICATION (1<<10)
123 #define CONST_EXPLICIT    (1<<11)
124 #define CONST_IMPLICIT    (1<<12)
125
126 #define CONST_TAG         (1<<13)       /*  Used in ASN.1 assignement  */
127 #define CONST_OPTION      (1<<14)
128 #define CONST_DEFAULT     (1<<15)
129 #define CONST_TRUE        (1<<16)
130 #define CONST_FALSE       (1<<17)
131
132 #define CONST_LIST        (1<<18)       /*  Used with TYPE_INTEGER and TYPE_BIT_STRING  */
133 #define CONST_MIN_MAX     (1<<19)
134
135 #define CONST_1_PARAM     (1<<20)
136
137 #define CONST_SIZE        (1<<21)
138
139 #define CONST_DEFINED_BY  (1<<22)
140
141 /* Those two are deprecated and used for backwards compatibility */
142 #define CONST_GENERALIZED (1<<23)
143 #define CONST_UTC         (1<<24)
144
145 /* #define CONST_IMPORTS     (1<<25) */
146
147 #define CONST_NOT_USED    (1<<26)
148 #define CONST_SET         (1<<27)
149 #define CONST_ASSIGN      (1<<28)
150
151 #define CONST_DOWN        (1<<29)
152 #define CONST_RIGHT       (1<<30)
153
154
155 #define ASN1_ETYPE_TIME 17
156 /****************************************/
157 /* Returns the first 8 bits.            */
158 /* Used with the field type of asn1_node_st */
159 /****************************************/
160 inline static unsigned int
161 type_field (unsigned int ntype)
162 {
163   return (ntype & 0xff);
164 }
165
166 /* To convert old types from a static structure */
167 inline static unsigned int
168 convert_old_type (unsigned int ntype)
169 {
170   unsigned int type = ntype & 0xff;
171   if (type == ASN1_ETYPE_TIME)
172     {
173       if (ntype & CONST_UTC)
174         type = ASN1_ETYPE_UTC_TIME;
175       else
176         type = ASN1_ETYPE_GENERALIZED_TIME;
177
178       ntype &= ~(CONST_UTC | CONST_GENERALIZED);
179       ntype &= 0xffffff00;
180       ntype |= type;
181
182       return ntype;
183     }
184   else
185     return ntype;
186 }
187
188 #endif /* INT_H */