summaryrefslogtreecommitdiff
path: root/support/dktools/dk4a85d.ctr
blob: ac17417f2739ddc9e36fff996418d7fc14818dd3 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
%%	options

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



%%	header

/**	@file	dk4a85d.h	ASCII-85 decoder.

First initialize the decoder structure, use the dk4a85_dec_init() function.

Add bytes to the decoder using the dk4a85_dec_add() function
as long as adding results in DK4_EDSTM_ACCEPT or
DK4_EDSTM_FINISHED. On DK4_EDSTM_FINISHED use dk4a85_dec_output() to obtain
the binary bytes.

A return value DK4_EDSTM_STOP indicates that the end of data (EOD) marker
was found in the data stream, you must not add further characters to the
decoder.

After adding all bytes use dk4a85_dec_finish() to check whether there
is data for a final incomplete sequence stored in the decoder. If the test
results in DK4_EDSTM_FINISHED, use dk4a85_dec_output() to obtain
the binary bytes.

The dk4a85_dec_finish() is also necessary if you stopped
feeding input due to a DK4_EDSTM_STOP result from dk4a85_dec_add().

Note: You can not directly call dk4a85_dec_output() after receiving
DK4_EDSTM_STOP from dk4a85_dec_add(), the call to dk4a85_dec_finish()
is required.
*/

#include "dk4conf.h"
#include "dk4error.h"

/**	ASCII 85 decoder.
*/
typedef struct {
  char		ib[6];		/**< Input bytes, ASCII. */
  unsigned char	ob[4];		/**< Output bytes, binary. */
  size_t	os;		/**< Output buffer size. */
  size_t	is;		/**< Index of next input byte to receive. */
  int		tf;		/**< Flag: Tilde found */
} dk4_a85_dec_t;

#ifdef __cplusplus
extern "C" {
#endif

/**	Initialize decoder before using it.
	@param	dec	Decoder to initialize.
	@param	erp	Error report, may be NULL.
*/
void
dk4a85_dec_init(dk4_a85_dec_t	*dec, dk4_er_t *erp);

/**	Add one text character to the decoder.
	@param	dec	Decoder to use.
	@param	input	Input text character.
	@param	erp	Error report, may be NULL.
	@return	Action to take, one from:
	- DK4_EDSTM_ACCEPT<br>
	  if the input was accepted and stored in the decoder, no action
	  necessary.
	- DK4_EDSTM_FINISHED<br>
	  if a sequence of 5 input text characters was completed, use
	  dk4a85_dec_output() to obtain the corresponding binary 4 bytes.
	- DK4_EDSTM_STOP<br>
	  if an EOD (end of data) marker was found. Use
	  dk4a85_dec_finish() and take appropriate action to process
	  the final byte sequence.
	- DK4_EDSTM_ERROR<br>
	  if an error occured.
*/
int
dk4a85_dec_add(dk4_a85_dec_t *dec, char input, dk4_er_t *erp);

/**	Check whether final bytes are stored in the decoder.
	@param	dec	Decoder to use.
	@param	erp	Error report, may be NULL.
	@return	Action to take, one from:
	- DK4_EDSTM_ACCEPT<br>
	  if there are no final bytes in the decoder, no action necessary.
	- DK4_EDSTM_FINISHED<br>
	  if there are final bytes in the decoder, use dk4a85_dec_output()
	  to retrieve them.
	- DK4_EDSTM_ERROR<br>
	  if an error occured.
*/
int
dk4a85_dec_finish(dk4_a85_dec_t *dec, dk4_er_t *erp);

/**	Retrieve final bytes.
	@param	dptr	Address of buffer pointer to set, should be
			initialized to NULL.
	@param	szptr	Address of size variable to set, should be
			initialized to 0.
	@param	dec	Decoder to use.
	@param	erp	Error report, may be NULL.
	@return	1 on success (bytes available, 0 otherwise).
*/
int
dk4a85_dec_output(
  const unsigned char **dptr, size_t *szptr, dk4_a85_dec_t *dec, dk4_er_t *erp
);


#ifdef __cplusplus
}
#endif



%%	module

#include "dk4a85d.h"
#include "dk4mem.h"
#include "dk4edstm.h"



$!trace-include



/**	Powers of 85.
*/
static const unsigned long	dk4a85e_pow_85[] = {
  (85UL * 85UL * 85UL * 85UL),
  (85UL * 85UL * 85UL),
  (85UL * 85UL),
  85UL
};



void
dk4a85_dec_init(dk4_a85_dec_t	*dec, dk4_er_t *erp)
{
  if (NULL != dec) {
    DK4_MEMRES(dec, sizeof(dk4_a85_dec_t));
    dec->os = 0;
    dec->is = 0;
    dec->tf = 0;
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
}



int
dk4a85_dec_add(dk4_a85_dec_t *dec, char input, dk4_er_t *erp)
{
  unsigned long	 val;
  int		 back	=	DK4_EDSTM_ERROR;
  if (NULL != dec) {
    if (0 != dec->tf) {
      if ('>' == input) {
        back = DK4_EDSTM_STOP;
      } else {
	dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
      }
    } else {
      switch (input) {
        case ' ' : case '\t' : case '\r' : case '\n' : case 0x00 : case 0x0C : {
	  back = DK4_EDSTM_ACCEPT;
	} break;
        case '~' : {
	  back = DK4_EDSTM_ACCEPT;
	  dec->tf = 1;
	} break;
	case 'z' : {
	  if (0 == dec->is) {
	    dec->ob[0] = 0x00;
	    dec->ob[1] = 0x00;
	    dec->ob[2] = 0x00;
	    dec->ob[3] = 0x00;
	    dec->os    = 4;
	    back = DK4_EDSTM_FINISHED;
	  } else {
	    dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
	  }
	} break;
	default : {
	  if (((char)32 < input) && ((char)118 > input)) {
	    dec->ib[dec->is] = input;
	    dec->is += 1;
	    back = DK4_EDSTM_ACCEPT;
	    if (5 <= dec->is) {
	      val =
	      dk4a85e_pow_85[0] *
	      ((unsigned long)((unsigned char)(dec->ib[0] - (char)33)) & 0xFFUL)
	      +
	      dk4a85e_pow_85[1] *
	      ((unsigned long)((unsigned char)(dec->ib[1] - (char)33)) & 0xFFUL)
	      +
	      dk4a85e_pow_85[2] *
	      ((unsigned long)((unsigned char)(dec->ib[2] - (char)33)) & 0xFFUL)
	      +
	      dk4a85e_pow_85[3] *
	      ((unsigned long)((unsigned char)(dec->ib[3] - (char)33)) & 0xFFUL)
	      +
	      ((unsigned long)((unsigned char)(dec->ib[4] - (char)33)) & 0xFFUL)
	      ;
	      dec->ob[0] = (unsigned char)((val >> 24) & 0xFFUL);
	      dec->ob[1] = (unsigned char)((val >> 16) & 0xFFUL);
	      dec->ob[2] = (unsigned char)((val >>  8) & 0xFFUL);
	      dec->ob[3] = (unsigned char)((val      ) & 0xFFUL);
	      dec->is = 0;
	      dec->os = 4;
	      back = DK4_EDSTM_FINISHED;
	    }
	  } else {
	    dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
	  }
	} break;
      }
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



/*
	During encoding sequences of 4 binary bytes b1, b2, b3, and b4
	are converted to five base-85 values a1, a2, a3, a4, and a5
	fullfilling the equation
	v=b1*256^3+b2*256^2+b3*256+b4=a1*85^4+a2*85^3+a3*85^2+a4*85+a5

	For complete sequences encoding and decoding are straightforward.

	For an incomplete final sequence of n binary bytes (1<=n<=3),
	only n+1 a values are encoded.
	In the example we assume a 2 byte sequence b1 and b2.
	The value v is calculated as
	v=b1*256^3+b2*256^2+0*256+0=a1*85^4+a2*85^3+a3*85^2+a4*85+a5.
	As we have 2 binary bytes, 3 text coefficients a1, a2, and a3
	are written to encoded output, a4 and a5 are skipped.
	From these 3 coefficients the decoder can calculate
	v'=a1*85^4+a2*85^3+a3*85^2+0*85+0
	which we can split into
	v'=b1'*256^3+b2'*256^2+b3'*256+b4'
	Obviously v' is less than or equal to v because v'=v-a4*85-a5.
	We have v=v' only if a4=0 and a5=0, this results in
	b3=0 and b4=0. Otherwise we have v>v'.
	As the final 2 bytes of v are zero, decreasing v to v' results
	in a non-zero value in the final 2 bytes and a decrease by
	1 in the first 2 bytes.
	So if v' & 0x0000FFFF is nonzero we have to calculate
	v = v' + 0x00010000 before splitting v into b1, b2, b3, and b4.
*/



int
dk4a85_dec_finish(dk4_a85_dec_t *dec, dk4_er_t *erp)
{
  unsigned long	 val;					/* 32 bit value */
  int		 back	=	DK4_EDSTM_ERROR;
  $? "+ dk4a85_dec_finish"
  if (NULL != dec) {
    if (0 == dec->is) {
      back = DK4_EDSTM_ACCEPT;
    } else {
      switch (dec->is) {
        case 4: {			$? ". 4 unused input bytes"
	  /*	Calculate 32 bit value from encoded values.
	  */
	  val =
	  dk4a85e_pow_85[0] *
	  ((unsigned long)((unsigned char)(dec->ib[0] - (char)33)) & 0xFFUL)
	  +
	  dk4a85e_pow_85[1] *
	  ((unsigned long)((unsigned char)(dec->ib[1] - (char)33)) & 0xFFUL)
	  +
	  dk4a85e_pow_85[2] *
	  ((unsigned long)((unsigned char)(dec->ib[2] - (char)33)) & 0xFFUL)
	  +
	  dk4a85e_pow_85[3] *
	  ((unsigned long)((unsigned char)(dec->ib[3] - (char)33)) & 0xFFUL)
	  ;
	  $? ". val = %lu %lx", val, val
	  /*	Check whether truncation of a5 modified the 32 bit
	  	value, correct least significant used byte if necessary.
	  */
	  if (0UL != (val & 0x000000FFUL)) {
	    val += 0x00000100UL;
	  }
	  /*	Split 32 bit value into bytes.
	  */
	  $? ". val = %lu %lx", val, val
	  dec->ob[0] = (unsigned char)((val >> 24) & 0xFFUL);
	  dec->ob[1] = (unsigned char)((val >> 16) & 0xFFUL);
	  dec->ob[2] = (unsigned char)((val >>  8) & 0xFFUL);
	  /*	Set input size, output size and result.
	  */
	  dec->is = 0;
	  dec->os = 3;
	  back = DK4_EDSTM_FINISHED;
	} break;
	case 3: {			$? ". 3 unused input bytes"
	  /*	Calculate 32 bit value from encoded values.
	  */
	  val =
	  dk4a85e_pow_85[0] *
	  ((unsigned long)((unsigned char)(dec->ib[0] - (char)33)) & 0xFFUL)
	  +
	  dk4a85e_pow_85[1] *
	  ((unsigned long)((unsigned char)(dec->ib[1] - (char)33)) & 0xFFUL)
	  +
	  dk4a85e_pow_85[2] *
	  ((unsigned long)((unsigned char)(dec->ib[2] - (char)33)) & 0xFFUL)
	  ;
	  $? ". val = %lu %lx", val, val
	  /*	Check whether truncation of a4 and a5 modified the 32 bit
	  	value, correct least significant used byte if necessary.
	  */
	  if (0UL != (val & 0x0000FFFFUL)) {
	    val += 0x00010000UL;
	  }
	  $? ". val = %lu %lx", val, val
	  /*	Split 32 bit value into bytes.
	  */
	  dec->ob[0] = (unsigned char)((val >> 24) & 0xFFUL);
	  dec->ob[1] = (unsigned char)((val >> 16) & 0xFFUL);
	  /*	Set input size, output size and result.
	  */
	  dec->is = 0;
	  dec->os = 2;
	  back = DK4_EDSTM_FINISHED;
	} break;
	case 2: {				$? ". 2 unused input bytes"
	  /*	Calculate 32 bit value from encoded values.
	  */
	  val =
	  dk4a85e_pow_85[0] *
	  ((unsigned long)((unsigned char)(dec->ib[0] - (char)33)) & 0xFFUL)
	  +
	  dk4a85e_pow_85[1] *
	  ((unsigned long)((unsigned char)(dec->ib[1] - (char)33)) & 0xFFUL)
	  ;
	  $? ". val = %lu %lx", val, val
	  /*	Check whether truncation of a3, a4, and a5 modified the 32 bit
	  	value, correct least significant used byte if necessary.
	  */
	  if (0UL != (val & 0x00FFFFFFUL)) {
	    val += 0x01000000UL;
	  }
	  $? ". val = %lu %lx", val, val
	  /*	Split 32 bit value into bytes.
	  */
	  dec->ob[0] = (unsigned char)((val >> 24) & 0xFFUL);
	  /*	Set input size, output size and result.
	  */
	  dec->is = 0;
	  dec->os = 1;
	  back = DK4_EDSTM_FINISHED;
	} break;
	default: {
	  dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
	} break;
      }
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  } $? "- dk4a85_dec_finish"
  return back;
}



int
dk4a85_dec_output(
  const unsigned char **dptr, size_t *szptr, dk4_a85_dec_t *dec, dk4_er_t *erp
)
{
  int		 back	=	0;
  if ((NULL != dec) && (NULL != dptr) && (NULL != szptr)) {
    if (0 < dec->os) {
      *dptr = &(dec->ob[0]);
      *szptr = dec->os;
      back = 1;
    } else {
      *dptr = NULL;
      *szptr = 0;
      back = 0;
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}