asn1crt.h 5.29 KB
Newer Older
yoogx's avatar
yoogx committed
1
2
#ifndef ASN1SCC_ASN1CRT_H_
#define ASN1SCC_ASN1CRT_H_
3

Maxime Perrotin's avatar
Maxime Perrotin committed
4
5
#include <stddef.h>

6
7
8

#ifdef  __cplusplus
extern "C" {
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#  include <cstdint>
#  include <cinttypes>
 /* C99 check */
#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || _MSC_VER >= 1900
#  include <stdbool.h>
#  include <stdint.h>
#  include <inttypes.h>
#else /* No C++ nor C99 */
#  ifndef _MSC_VER
typedef unsigned char bool;
#    define true 1u
#    define false 0u
#  endif /* _MSC_VER */

typedef unsigned char uint8_t;

typedef int int32_t;
typedef unsigned int uint32_t;

typedef long long int64_t;
typedef unsigned long long uint64_t;

#endif	/* C++/C99 */
32
33
34
35
36

#ifndef NULL
#define NULL	0
#endif

jdelange's avatar
jdelange committed
37
#ifndef TRUE
yoogx's avatar
yoogx committed
38
#define TRUE	true
jdelange's avatar
jdelange committed
39
#endif
40

jdelange's avatar
jdelange committed
41
#ifndef FALSE
yoogx's avatar
yoogx committed
42
#define FALSE	false
jdelange's avatar
jdelange committed
43
#endif
44
45
46
47
48

#ifndef WORD_SIZE
#define WORD_SIZE	8
#endif

49
50
51
52
#ifndef FP_WORD_SIZE
#define FP_WORD_SIZE	8
#endif

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef PRId32
#define PRId32 "d"
#endif

#ifndef PRId64
#define PRId64 "lld"
#endif

#ifndef PRIu32
#define PRIu32 "u"
#endif

#ifndef PRIu64
#define PRIu64 "llu"
#endif

Maxime Perrotin's avatar
Maxime Perrotin committed
69
#define OBJECT_IDENTIFIER_MAX_LENGTH	20
70

71

72
73
74
typedef float asn1Real32;
typedef double asn1Real64;

75
typedef uint8_t byte;
yoogx's avatar
yoogx committed
76

77
78
typedef int32_t asn1SccSint32;
typedef uint32_t asn1SccUint32;
79

80
81
typedef int64_t asn1SccSint64;
typedef uint64_t asn1SccUint64;
82

83
#if WORD_SIZE==8
84
85
typedef asn1SccUint64 asn1SccUint;
typedef asn1SccSint64 asn1SccSint;
86
87
#define ASN1SCC_PRId PRId64
#define ASN1SCC_PRIu PRIu64
88
89
90
#else
typedef asn1SccUint32 asn1SccUint;
typedef asn1SccSint32 asn1SccSint;
91
92
#define ASN1SCC_PRId PRId32
#define ASN1SCC_PRIu PRIu32
93
#endif
94

95
96
97
asn1SccUint int2uint(asn1SccSint v);
asn1SccSint uint2int(asn1SccUint v, int uintSizeInBytes);

98
99
100
101
#if FP_WORD_SIZE==8
typedef asn1Real64 asn1Real;
#else
typedef asn1Real32 asn1Real;
102
103
#endif

104

yoogx's avatar
yoogx committed
105
106
107
108
109
110
111
112
#ifdef _MSC_VER
#  ifndef INFINITY
#    define INFINITY (DBL_MAX+DBL_MAX)
#  endif
#  ifndef NAN
#    define NAN (INFINITY-INFINITY)
#  endif
#endif
113

yoogx's avatar
yoogx committed
114
typedef bool flag;
115
116
117

typedef char NullType;

118
119
120
121
122
123
struct BitStream_t;

//typedef void(*PushDataFnc)(struct BitStream_t* pThis, void* pushDataPrm);
//typedef void(*FetchDataFnc)(struct BitStream_t* pThis, void* fetchDataPrm);

typedef struct BitStream_t {
124
125
126
	byte* buf;
	long count;
	long currentByte;
127
128
129
	/* Next available bit for writting. 
	Possible vallues 0..7, 0 is most significant 
	bit of current byte*/
130
	int currentBit; 
131
132
133
134
	//PushDataFnc pushData;
	void* pushDataPrm;
	//FetchDataFnc fetchData;
	void* fetchDataPrm;
135
136
} BitStream;

137
138
139
140




141
142
143
144
typedef struct {
	byte* buf;
	long count;
	long currentByte;
jdelange's avatar
jdelange committed
145
	flag EncodeWhiteSpace;
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
} ByteStream;

typedef struct {
	int TokenID;
	char Value[100];
} Token;

typedef struct {
	char Name[50];
	char Value[100];
} XmlAttribute;

typedef struct {
	XmlAttribute attrs[20];
	int nCount;
} XmlAttributeArray;

Maxime Perrotin's avatar
Maxime Perrotin committed
163
164
165
166
typedef struct {
	int nCount;
	asn1SccUint values[OBJECT_IDENTIFIER_MAX_LENGTH];
} Asn1ObjectIdentifier;
167
168
169
170

#define ERR_INSUFFICIENT_DATA	101
#define ERR_INCORRECT_PER_STREAM	102
#define ERR_INVALID_CHOICE_ALTERNATIVE	103
yoogx's avatar
yoogx committed
171
#define ERR_INVALID_ENUM_VALUE	104
172
173
174
175
#define ERR_INVALID_XML_FILE	200
#define ERR_INVALID_BER_FILE	201
#define ERR_BER_LENGTH_MISMATCH	202

176
int GetCharIndex(char ch, byte allowedCharSet[], int setLen);
177

178
179
180
181
void ObjectIdentifier_Init(Asn1ObjectIdentifier *pVal);
flag ObjectIdentifier_equal(const Asn1ObjectIdentifier *pVal1, const Asn1ObjectIdentifier *pVal2);
flag ObjectIdentifier_isValid(const Asn1ObjectIdentifier *pVal);
flag RelativeOID_isValid(const Asn1ObjectIdentifier *pVal);
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Time Classes
	Asn1LocalTime,					// TIME-OF-DAY  ::= TIME(SETTINGS "Basic=Time Time=HMS Local-or-UTC=L")
	Asn1UtcTime,					//                  TIME(SETTINGS "Basic=Time Time=HMS Local-or-UTC=Z")
	Asn1LocalTimeWithTimeZone,		//                  TIME(SETTINGS "Basic=Time Time=HMS Local-or-UTC=LD")
	Asn1Date,						//  DATE ::=        TIME(SETTINGS "Basic=Date Date=YMD Year=Basic")
	Asn1Date_LocalTime,				//  DATE-TIME   ::= TIME(SETTINGS "Basic=Date-Time Date=YMD Year=Basic Time=HMS Local-or-UTC=L")
	Asn1Date_UtcTime,				// 			        TIME(SETTINGS "Basic=Date-Time Date=YMD Year=Basic Time=HMS Local-or-UTC=Z")
	Asn1Date_LocalTimeWithTimeZone	//                  TIME(SETTINGS "Basic=Date-Time Date=YMD Year=Basic Time=HMS Local-or-UTC=LD")
*/
typedef struct {
	int sign;		//-1 or +1
	int hours;
	int mins;
} Asn1TimeZone;
197

198
199
200
201
202
203
204
typedef struct {
	int hours;
	int mins;
	int secs;
	int fraction;
	Asn1TimeZone tz;
} Asn1TimeWithTimeZone;
205

206
207
208
209
210
211
typedef struct {
	int hours;
	int mins;
	int secs;
	int fraction;
} Asn1UtcTime;
212

213
214
215
216
217
218
typedef struct {
	int hours;
	int mins;
	int secs;
	int fraction;
} Asn1LocalTime;
219

220
221
222
223
224
typedef struct {
	int years;
	int months;
	int days;
} Asn1Date;
225

226
227
228
229
typedef struct {
	Asn1Date	   date;
	Asn1LocalTime  time;
} Asn1DateLocalTime;
230

231
232
233
234
typedef struct {
	Asn1Date	 date;
	Asn1UtcTime  time;
} Asn1DateUtcTime;
235

236
237
238
239
typedef struct {
	Asn1Date	 date;
	Asn1TimeWithTimeZone  time;
} Asn1DateTimeWithTimeZone;
240

241
242
243
244
245
typedef enum {
	Asn1TC_LocalTimeStamp,
	Asn1TC_UtcTimeStamp,
	Asn1TC_LocalTimeTZStamp
} Asn1TimeZoneClass;
246

247
typedef asn1SccUint BerTag;
248

249
250
251
252
253
#if WORD_SIZE==8
extern const asn1SccUint64 ber_aux[];
#else
extern const asn1SccUint32 ber_aux[];
#endif
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

#define CHECK_BIT_STREAM(pBitStrm)	assert((pBitStrm)->currentByte*8+(pBitStrm)->currentBit<=(pBitStrm)->count*8)

#ifdef _MSC_VER
#pragma warning( disable : 4127)
#endif

#define ASSERT_OR_RETURN_FALSE(_Expression) do { assert(_Expression); if (!(_Expression)) return FALSE;} while(0)

#ifdef  __cplusplus
}
#endif


#endif