summaryrefslogtreecommitdiff
path: root/support/dktools/dk4binenc.ctr
blob: a46470ca4c57163e7c92d92ee2cfe9f1bc1ebb19 (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
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd



%%	header

/**	@file	dk4binenc.h	Encode binary data to text.
*/

#include "dk4error.h"

#ifdef __cplusplus
extern "C" {
#endif

/**	Encode binary data to text.
	Data is saved as text string including finalizing 0 byte
	except for DK4_BINARY_TO_TEXT_ENCODING_NONE.
	@param	dptr	Destination buffer address.
	@param	szdptr	Destination buffer size.
	@param	sptr	Source buffer address.
	@param	szsptr	Source buffer size.
	@param	enc	Encoding, one from
	DK4_BINARY_TO_TEXT_ENCODING_NONE,
	DK4_BINARY_TO_TEXT_ENCODING_HEX,
	or DK4_BINARY_TO_TEXT_ENCODING_A85.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or sptr is NULL or szdptr or szsptr is 0 or an illegal
	  enc value was specified,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the provided destination buffer is too small,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size calculation
	  (required destination buffer size too large),
	- DK4_E_ENCODING_FAILED<br>
	  if encoding failed for a byte.
*/
int
dk4binenc(
  char		*dptr,
  size_t	 szdptr,
  const void	*sptr,
  size_t	 szsptr,
  int		 enc,
  dk4_er_t	*erp
);

/**	Find numeric encoding type for an encoding name.
	@param	name	String containing encoding name.
	@param	erp	Error report, may be NULL.
	@return	Non-negative numeric encoding type on success, -1 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if name is NULL,
	- DK4_E_SYNTAX<br>
	  if name is not a valid encoding name.
*/
int
dk4binenc_choose_encoding(const dkChar *name, dk4_er_t *erp);

#ifdef __cplusplus
}
#endif

/**	Message digest encodings.
*/
enum {
					/**	No encoding, use binary
						data directly.
					*/
  DK4_BINARY_TO_TEXT_ENCODING_NONE	= 0,

					/**	ASCII-Hex encoding.
					*/
  DK4_BINARY_TO_TEXT_ENCODING_HEX ,

					/**	ASCII-85 encoding.
					*/
  DK4_BINARY_TO_TEXT_ENCODING_A85
};



%%	module

#include "dk4binenc.h"
#include "dk4mem.h"
#include "dk4memrs.h"
#include "dk4a85e.h"
#include "dk4maasz.h"
#include "dk4strd.h"
#include "dk4edstm.h"



$!trace-include



/**	Hexadecimal digits.
*/
static const char dk4binenc_hex_digits[] = { "0123456789abcdef" };



/**     Message digest type names.
        Order must match the definition order in the enum in dk4md.h.
*/
static const dkChar * const     dk4binenc_encoding_names[] = {
$!string-table  macro=dkT
bin
binary
hex
hexa
hexadecimal
ascii-85
ascii85
a85
$!end
};



int
dk4binenc(
  char		*dptr,
  size_t	 szdptr,
  const void	*sptr,
  size_t	 szsptr,
  int		 enc,
  dk4_er_t	*erp
)
{
  dk4_a85_enc_t	 	 a85;
  dk4_er_t		 maerr;
  const unsigned char	*ucptr;
  const char		*resptr;
  size_t		 ressize;
  size_t		 oused;
  size_t		 noused;
  int			 back = 0;
  if ((NULL != dptr) && (NULL != sptr) && (0 < szdptr) && (0 < szsptr)) {
    switch (enc) {
      case DK4_BINARY_TO_TEXT_ENCODING_NONE : {
        if (szdptr >= szsptr) {
	  DK4_MEMCPY(dptr, sptr, szsptr);
	  back = 1;
	} else {
	  dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
	}
      } break;
      case DK4_BINARY_TO_TEXT_ENCODING_HEX : {
        oused = 0;
	ucptr = (const unsigned char *)sptr;
	while (szsptr--) {
	  dk4error_init(&maerr);
	  noused = dk4ma_size_t_add(oused, 2, &maerr);
	  if (DK4_E_NONE == maerr.ec) {
	    dptr[oused] =
	    dk4binenc_hex_digits[(size_t)((((unsigned)(*ucptr)) >> 4) & 0x0FU)];
	    dptr[oused+1] =
	    dk4binenc_hex_digits[(size_t)(((unsigned)(*ucptr)) & 0x0FU)];
	    oused = noused;
	  } else {
	    dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
	    goto finished;
	  }
	  ucptr++;
	}
	if (oused < szdptr) {
	  dptr[oused] = '\0';
	  back = 1;
	} else {
	  dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
	}
      } break;
      case DK4_BINARY_TO_TEXT_ENCODING_A85 : {
        oused = 0;
        ucptr = (const unsigned char *)sptr;
        dk4a85_enc_init(&a85, 0, erp);
	while(szsptr--) {
	  switch (dk4a85_enc_add(&a85, *(ucptr++), erp)) {
	    case DK4_EDSTM_FINISHED : {
	      resptr = NULL; ressize = 0;
	      (void)dk4a85_enc_output(&resptr, &ressize, &a85, NULL);
	      if ((NULL != resptr) && (0 < ressize)) {
	        dk4error_init(&maerr);
		noused = dk4ma_size_t_add(oused, ressize, &maerr);
		if (DK4_E_NONE == maerr.ec) {
	          if (noused < szdptr) {
		    DK4_MEMCPY(&(dptr[oused]), resptr, ressize);
		    oused = noused;
		  } else {
		    dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
		    goto finished;
		  }
		} else {
		  dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
		  goto finished;
		}
	      }
	    } break;
	    case DK4_EDSTM_ERROR : {
	      dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	      goto finished;
	    } break;
	  }
	}
	switch (dk4a85_enc_finish(&a85, erp)) {
	  case DK4_EDSTM_FINISHED : {
	    resptr = NULL; ressize = 0;
	    (void)dk4a85_enc_output(&resptr, &ressize, &a85, NULL);
	    if ((NULL != resptr) && (0 < ressize)) {
	      dk4error_init(&maerr);
	      noused = dk4ma_size_t_add(oused, ressize, &maerr);
	      if (DK4_E_NONE == maerr.ec) {
	        if (noused < szdptr) {
	          DK4_MEMCPY(&(dptr[oused]), resptr, ressize);
		  oused = noused;
	        } else {
	          dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
		  goto finished;
	        }
	      } else {
	        dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
		goto finished;
	      }
	    }
	  } break;
	  case DK4_EDSTM_ERROR : {
	    dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	    goto finished;
	  } break;
	}
	if (oused < szdptr) {
	  dptr[oused] = '\0';
	  back = 1;
	} else {
	  dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
	}
	(void)dk4mem_reset_secure(&a85, sizeof(dk4_a85_enc_t), NULL);
      } break;
      default : {
        dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
      } break;
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  finished:
  if ((NULL != dptr) && (0 == back)) { *dptr = '\0'; }
  return back;
}



int
dk4binenc_choose_encoding(const dkChar *name, dk4_er_t *erp)
{

  int back = -1;
  if (NULL != name) {
    switch (dk4str_array_index(dk4binenc_encoding_names, name, 0)) {
      case 0: case 1: {
        back = DK4_BINARY_TO_TEXT_ENCODING_NONE;
      } break;
      case 2: case 3: case 4: {
        back = DK4_BINARY_TO_TEXT_ENCODING_HEX;
      } break;
      case 5: case 6: case 7: {
        back = DK4_BINARY_TO_TEXT_ENCODING_A85;
      } break;
    }
    if (-1 == back) {
      dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}