summaryrefslogtreecommitdiff
path: root/support/dktools/dk4a85d.c
blob: 611255b73f3ba3314d6bd552b23f4689f03611fe (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
/*
	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: dk4a85d.ctr
*/

/*
Copyright (C) 2015-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 dk4a85d.c The dk4a85d module.
*/


#line 119 "dk4a85d.ctr"

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





#line 127 "dk4a85d.ctr"



/**	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;
  

#line 269 "dk4a85d.ctr"
  if (NULL != dec) {
    if (0 == dec->is) {
      back = DK4_EDSTM_ACCEPT;
    } else {
      switch (dec->is) {
        case 4: {			

#line 275 "dk4a85d.ctr"
	  /*	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)
	  ;
	  

#line 291 "dk4a85d.ctr"
	  /*	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.
	  */
	  

#line 300 "dk4a85d.ctr"
	  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: {			

#line 310 "dk4a85d.ctr"
	  /*	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)
	  ;
	  

#line 323 "dk4a85d.ctr"
	  /*	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;
	  }
	  

#line 330 "dk4a85d.ctr"
	  /*	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: {				

#line 341 "dk4a85d.ctr"
	  /*	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)
	  ;
	  

#line 351 "dk4a85d.ctr"
	  /*	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;
	  }
	  

#line 358 "dk4a85d.ctr"
	  /*	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);
  } 

#line 375 "dk4a85d.ctr"
  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;
}