summaryrefslogtreecommitdiff
path: root/libdigidoc/DigiDocConvert.h
blob: d82886cb2ad8880d9083762638cf92078e3258a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#ifndef __DIGIDOC_CONVERT_H__
#define __DIGIDOC_CONVERT_H__
//==================================================
// FILE:	DigiDocEnc.h
// PROJECT:     Digi Doc Encryption
// DESCRIPTION: DigiDoc character conversion routines
// AUTHOR:  Veiko Sinivee, S|E|B IT Partner Estonia
//==================================================
// Copyright (C) AS Sertifitseerimiskeskus
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// GNU Lesser General Public Licence is available at
// http://www.gnu.org/copyleft/lesser.html
//==========< HISTORY >=============================
//      22.09.2004      Veiko Sinivee
//                      Creation
//==================================================

#include <libdigidoc/DigiDocLib.h>
#include <libdigidoc/DigiDocDefs.h>

#ifdef  __cplusplus
extern "C" {
#endif

//==========< general functions >============

//--------------------------------------------------
// Decodes a single hex digit
// h - hex digit
// return binary value
//--------------------------------------------------
byte h2b(char h);

//--------------------------------------------------
// Converts a single byte to two hex characters
// b - binary value
// dest - destination buffer
//--------------------------------------------------
void b2h(byte b, char* dest);

//--------------------------------------------------
// Converts correct filename to incorrect encoding
// used in formats 1.0, 1.1 and 1.2
// src - input data
// dest - buffer for converted data
// len - length of destination buffer
//--------------------------------------------------
EXP_OPTION void convFNameToWin(const char* src, char* dest, int len);

//--------------------------------------------------
// Converts bad UTF-8 filename used in formats 1.0, 
// 1.1 and 1.2 to correct encoding
// src - input data
// dest - buffer for converted data
// len - length of destination buffer
//--------------------------------------------------
EXP_OPTION void convWinToFName(const char* src, char* dest, int len);

// Converts a hex number (string) to binary value
EXP_OPTION void hex2bin(const char* hex, byte* bin, int* len);

// Converts a binary value to hex string
EXP_OPTION void bin2hex(const byte* bin, int blen, char* hex, int* len);

//============================================================
// Encodes input data in hex format.
// pMBufSrc - input data
// pMBufDest - destination buffer
//============================================================
EXP_OPTION int ddocBin2Hex(const DigiDocMemBuf* pMBufSrc, DigiDocMemBuf* pMBufDest);
    
// Helper function that converts ISO Latin1 to UTF8
EXP_OPTION char* ascii2utf8(const char* ascii, char* utf8out, int* outlen);

// Helper function that converts UTF8 to ISO Latin1
EXP_OPTION char* utf82ascii(const char* utf8in, char* asciiout, int* outlen);

// converts string from unicode to ascii
EXP_OPTION void unicode2ascii(const char* uni, char* dest);
// converts unicode escapes string to UTF8
EXP_OPTION void unicodeEscapes2utf8(const char* uni, char* dest);

    
char* escape2xmlsym(const char* src);

    
int hasUmlauts(const char* str);
int str2asn1time(const SignedDoc* pSigDoc, const char* str, ASN1_GENERALIZEDTIME* asn1tm);

//--------------------------------------------------
// Converts xml symbols to corresponding escapes
// src - string with xml special sybols
// returns string with esacpes
//--------------------------------------------------
char* escape2xmlsym(const char* src);

char* unescapeXmlsym(const char* src);
    
//--------------------------------------------------
// Converts input data to UTF-8
// src - input data
// returns converted string or NULL. Caller must free it.
//--------------------------------------------------
EXP_OPTION int ddocConvertInput(const char* src, char** dest);

// Base64 encode some data
EXP_OPTION void encode(const byte* raw, int rawlen, byte* buf, int* buflen);
// Base64 decode some data
EXP_OPTION void decode(const byte* raw, int rawlen, byte* buf, int* buflen);


//============================================================
// Decodes input data in Base64 format.
// pMBufSrc - input data
// pMBufDest - destination buffer
//============================================================
EXP_OPTION int ddocDecodeBase64(DigiDocMemBuf* pMBufSrc, DigiDocMemBuf* pMBufDest);

//============================================================
// Decodes input data in Base64 format.
// data - input data
// len - length of input data. Use -1 for zero terminated strings
// pMBufDest - destination buffer
//============================================================
EXP_OPTION int ddocDecodeBase64Data(void* data, long lLen, DigiDocMemBuf* pMBufDest);

//============================================================
// Encodes input data in Base64 format.
// pMBufSrc - input data
// pMBufDest - destination buffer
//============================================================
EXP_OPTION int ddocEncodeBase64(const DigiDocMemBuf* pMBufSrc, DigiDocMemBuf* pMBufDest);

//=======< time convesrion >=======================

//--------------------------------------------------
// Decodes an ASN1 generalized time
// tm - ASN1 generalized time
// y - year
// M - month
// d - day of month
// h - hour
// m - minute
// s - second
// returns error code or ERR_OK
//--------------------------------------------------
int decodeGeneralizedTime(ASN1_GENERALIZEDTIME *tm, 
			   int* y, int* M, int* d, 
			  int* h, int* m, int* s);

//--------------------------------------------------
// Decodes an ASN1 UTC time
// tm - ASN1 generalized time
// y - year
// M - month
// d - day of month
// h - hour
// m - minute
// s - second
// returns error code or ERR_OK
//--------------------------------------------------
int decodeUTCTime(ASN1_UTCTIME *tm, 
		  int* y, int* M, int* d, 
		  int* h, int* m, int* s);

//==========================================================
// converts ASN1 time to time_t
//==========================================================
int asn1time2time_t(ASN1_TIME* tm, time_t* pT);

int asn1time2time_t_local(ASN1_TIME* tm, time_t* pT);

//==========================================================
// converts ASN1 time to string
//==========================================================
int asn1time2strYear(const SignedDoc* pSigDoc, ASN1_TIME* tm, char* buf, int year, int len);

//==========================================================
// converts ASN1 time to string
//==========================================================
int asn1time2str(const SignedDoc* pSigDoc, ASN1_TIME* tm, char* buf, int len);

//===================================================================
// converts time_t to timestamp string
// t - time_t input value
// szTimestamp - output buffer
// len - length of buffer
// returns error code or ERR_OK
//===================================================================
  EXP_OPTION int time_t2str(time_t t, char* szTimestamp, int len);

//===================================================================
// converts string to time_t
// szTimestamp - input buffer
// pT - address time_t output value
// returns error code or ERR_OK
//===================================================================
EXP_OPTION int str2time_t(char* szTimestamp, time_t* pT);

//--------------------------------------------------
// Converts a filename according to platform rules
// dest - destination buffer
// destlen - destination buffer length
// src - source filename
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int ddocConvertFileName(char* dest, int destlen, const char* src);

//==========< only win32 fucntions >============

#ifdef WIN32

//--------------------------------------------------
// Converts input OEM charset data to 16 bit unicode.
// oem - 8 bit oem charset input data
// unicode - address of pointer for allocated unicode string. Caller must free() !
// outlen - address of length variable for unicode string
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int oem2unicode(const char* oem, char** unicode, int* outlen);

//--------------------------------------------------
// Converts input 16 bit unicode data to UTF8.
// unicode - 16 bit unicode input data
// utf8 - address of pointer for allocated utf8 string. Caller must free() !
// outlen - address of length variable for utf8 string
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int unicode2utf8(const char* unicode, char** utf8, int* outlen);

//--------------------------------------------------
// Converts input OEM charset data to UTF-8
// oem - 8 bit oem charset input data
// utf8 - address of buffer allocated utf8 string. 
// len - size of buffer
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int oem2utf8(const char* oem, char* utf8, int len);

//--------------------------------------------------
// Converts input UTF-8 data to 16 bit unicode data.
// utf8 - UTF-8 input data
// unicode - address of pointer for allocated unicode string. Caller must free() !
// outlen - address of length variable for unicode string
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int utf82unicode(const char* utf8, char** unicode, int* outlen);

//--------------------------------------------------
// Converts input 16 bit unicode data to oem charset data.
// unicode - 16 bit unicode input data
// oem - address of pointer for allocated oem string. Caller must free() !
// outlen - address of length variable for oem string
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int unicode2oem(const char* unicode, char** oem, int* outlen);

//--------------------------------------------------
// Converts input UTF-8 data to OEM charset data
// utf8 - UTF-8 input data
// oem - address of buffer for oem string. 
// len - size of buffer
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int utf82oem(const char* utf8, char* oem, int len);

//--------------------------------------------------
// Converts input UTF-8 data to OEM charset data
// pSigDoc - signed doc object
// pDf - data file obejct 
// outFileName - output buffer
// len - length of output buffer
// returns error code or ERR_OK
//--------------------------------------------------
EXP_OPTION int getDataFileFileName(SignedDoc* pSigDoc, DataFile* pDf, char* outFileName, int len);

#endif // WIN32

//--------------------------------------------------
// Releases mem-block allocated by lib. In win32
// this must be done since the mem was allocated by dll
// and must also be released by dll that allocated it.
// p - mem to be freed
//--------------------------------------------------
EXP_OPTION void freeLibMem(void* p);


#ifdef  __cplusplus
}
#endif


#endif // __DIGIDOC_CONVERT_H__