summaryrefslogtreecommitdiff
path: root/support/dktools/dk3enc.h
blob: 14aa7514cfc9b8abc7e51874c36d2e39cd5ffc6c (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk3enc.ctr
*/

/*
Copyright (C) 2011-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk3enc.h Header file for the dk3enc module.
*/

#ifndef DK3ENC_H_INCLUDED
/** Avoid multiple inclusions. */
#define DK3ENC_H_INCLUDED 1


#line 10 "dk3enc.ctr"

#include "dk3conf.h"
#include "dk3types.h"

#ifdef __cplusplus
extern "C" {
#endif

/**	Convert long number from net to host representation.
	@param	x	Number to convert.
	@return Conversion result.
*/
unsigned long
dk3enc_ntohl(unsigned long x);

/**	Convert long number from host to net representation.
	@param	x	Number to convert.
	@return Conversion result.
*/
unsigned long
dk3enc_htonl(unsigned long x);

/**	Convert short  number from net to host representation.
	@param	x	Number to convert.
	@return Conversion result.
*/
unsigned short
dk3enc_ntohs(unsigned short x);

/**	Convert short  number from host to net representation.
	@param	x	Number to convert.
	@return Conversion result.
*/
unsigned short
dk3enc_htons(unsigned short x);

/**	Convert 32-bit character to UTF-8.
	@param	c	32-bit character to convert.
	@param	u8p	Pointer to result buffer.
	@param	u8l	Size of result buffer, should be 8 at least.
	@return	Number of bytes used in result buffer.
*/
size_t
dk3enc_uc2utf8(dk3_c32_t c, unsigned char *u8p, size_t u8l);

/**	Convert UTF-8 encoded text to 32-bit character.
	@param	ucp	Pointer to variable for result.
	@param	u8p	Buffer containing UTF-8 encoded text.
	@param	u8l	Number of bytes in buffer.
	@param	u8u	Pointer to variable to receive number of bytes used.
	@return	1 on success, 0 on error.
*/
int
dk3enc_utf82uc(dk3_c32_t *ucp, unsigned char const *u8p,size_t u8l,size_t *u8u);

/**	Convert IP address from 8-bit text string to long number
	in host representation.
	@param	str	Text string containing the IP address.
	@param	ul	Pointer to result variable.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk3enc_c8_ipaddr_to_ul_app(char const *str, unsigned long *ul, dk3_app_t *app);

/**	Convert IP address from string to long number
	in host representation.
	@param	str	Text string containing the IP address.
	@param	ul	Pointer to result variable.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk3enc_ipaddr_to_ul_app(dkChar const *str, unsigned long *ul, dk3_app_t *app);

/**	Find size for result buffer needed to convert binary data to
	text using ASCII-85 encoding.
	@param	s	Size of binary data.
	@return	The required buffer length (includeing finalizing 0x00 byte).
*/
size_t
dk3enc_size_bin_to_a85(size_t s);

/**	Convert binary data to text using reverse ASCII-85 encoding.
	@param	dp	Destination buffer pointer.
	@param	ds	Size of \a dp.
	@param	sp	Binary (source) data.
	@param	ss	Number of bytes in \a sp.
	@param	app	Application structure, for diagnostics, may be NULL.
*/
int
dk3enc_bin_to_ra85_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
);

/**	Find size for result buffer needed to convert ASCII-85 encoded
	data back to binary data.
	@param	s	Number of ASCII-85 text characters.
	@return	Length of resulting binary data.
*/
size_t
dk3enc_size_a85_to_bin(size_t s);

/**	Convert reverse ASCII-85 encoded data back to binary.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size in bytes.
	@param	sp	Source buffer pointer.
	@param	ss	Source buffer length.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Number of binary bytes decoded.
*/
size_t
dk3enc_ra85_to_bin_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
);

#if 0
/**	Convert reverse ASCII-85 encoded data back to binary.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size in bytes.
	@param	sp	Source buffer pointer.
	@param	ss	Source buffer length.
	@return	Number of binary bytes decoded.
*/
size_t
dk3enc_ra85_to_bin(char *dp, size_t ds, char const *sp, size_t ss);
#endif

/**	Convert reverse ASCII-85 encoded data string back to binary.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size in bytes.
	@param	sp	Source buffer pointer.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Number of binary bytes decoded.
*/
size_t
dk3enc_ra85string_to_bin_app(
  char *dp, size_t ds, char const *sp, dk3_app_t *app
);

/**	Apply ASCII-85 encoding to binary data.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size.
	@param	sp	Source buffer pointer.
	@param	ss	Source buffer size.
	@param	app	Application structure for diagnostics, may be NULL.
*/
int
dk3enc_bin_to_a85_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
);

/**	Decode ASCII-85 encoded data back to binary.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size.
	@param	sp	Source buffer pointer.
	@param	ss	Source buffer size.
	@param	app	Application structure for diagnostics, may be NULL.
*/
size_t
dk3enc_a85_to_bin_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
);

/**	Decode ASCII-85 encoded data string back to binary.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size.
	@param	sp	Source buffer pointer.
	@param	app	Application structure for diagnostics, may be NULL.
*/
size_t
dk3enc_a85string_to_bin_app(char *dp, size_t ds, char const *sp, dk3_app_t *app);

/**	Calculate size for binary to hexadecimal conversion.
	@param	s	Size of binary data.
	@return	Size of hexadecimal encoding.
*/
size_t
dk3enc_size_bin_to_hex(size_t s);

/**	Calculate size for hexadecimal to binary conversion.
	@param	s	Size of hexadecimal data.
	@return	Size of binary data.
*/
size_t
dk3enc_size_hex_to_bin(size_t s);

/**	Apply hexadecimal encoding to binary data.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size.
	@param	sp	Source buffer pointer.
	@param	ss	Source buffer size.
	@param	app	Application structure for diagnostics, may be NULL.
*/
int
dk3enc_bin_to_hex_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
);

/**	Convert hexadecimal encoded data back to binary.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size.
	@param	sp	Source buffer pointer.
	@param	ss	Source buffer size.
	@param	app	Application structure for diagnostics, may be NULL.
*/
size_t
dk3enc_hex_to_bin_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
);

/**	Convert hexadecimal encoded data string back to binary.
	@param	dp	Destination buffer pointer.
	@param	ds	Destination buffer size.
	@param	sp	Source buffer pointer.
	@param	app	Application structure for diagnostics, may be NULL.
*/
size_t
dk3enc_hexstring_to_bin_app(
  char *dp, size_t ds, char const *sp, dk3_app_t *app
);

/**	Calculate size for hex decoding to binary.
	@param	s	String containing hexadecimal data.
	@return	Size of binary data.
*/
size_t
dk3enc_size_hexstring_to_bin(char const *s);

/**	Find size for conversion from ASCII-85 encoded data to binary.
 * 	@param	s	ASCII-85 encoded string.
 * 	@return	Size of binary data.
 */
size_t
dk3enc_size_a85string_to_bin(char const *s);

/**	Convert 32-bit character to UTF-16.
	If the value is 0xFFFF and below, the 16 bits are used ``as is''.
	For values \a c > 0xFFFF we have to build a surrogate pair.
	We substract 0x00010000, the result is a new character with
	20 significant bits. These 20 bits are splitted into two blocks of
	10 bits each. The high surrogate is built using the prefix
	bits 110110 and the higher 10-bits block. The low surrogate is built
	using the prefix bits 110111 and the lower 10-bits block.
	Order of surrogates in a surrogate pair is flexible, this function
	writes the high surrogate first.
	@param	c	The 32-bit character.
	@param	u16p	Buffer to receive UTF-16 encoded result.
	@param	u16l	Length of \a u16l in elements.
	@return	The number of dk3_c16_t characters produced.
*/
size_t
dk3enc_uc2utf16(dk3_c32_t c, dk3_c16_t *u16p, size_t u16l);

/**	Convert UTF-16 encoded characters to 32-bit character
	@param	ucp	Pointer to result variable.
	@param	u16p	Buffer containing UTF-16 encoded text.
	@param	u16l	Number of elements available in \a u16p.
	@param	u16u	Pointer to variable to receive number of dk3_c16_t used.
	@return	1 on success, 0 on error.
*/
int
dk3enc_utf162uc(dk3_c32_t *ucp, dk3_c16_t const *u16p, size_t u16l, size_t *u16u);

/**	Get data encoding type for name.
	@param	n	Data encoding name.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Integer representation of data encoding on success, -1 on error.
*/
int
dk3enc_get_type_app(dkChar const *n, dk3_app_t *app);

/**	Get name for data encoding type.
	@param	t	Numeric type.
	@return	Name for type on success, NULL on error.
*/
dkChar const *
dk3enc_get_data_encoding_name(int t);

/**	Get numeric value for text encoding.
	@param	en	Encoding name.
	@param	app	Application structure, may be NULL.
	@return	Numeric representation of text encoding on success,
	-1 on error.
*/
int
dk3enc_get_text_encoding_app(dkChar const *en, dk3_app_t *app);

/**	Get the used encoding.
	@param	app	Application structure, may be NULL.
	@return	The encoding used for characters in memory.
*/
int
dk3enc_get_encoding(dk3_app_t *app);

#ifdef __cplusplus
}
#endif




#endif