Debian Codegraph @

libs/pplib/pplib-src/src/util/utilbasexx.c

raw
1
2 #include "utilnumber.h"
3 #include "utilmem.h"
4 #include "utilbasexx.h"
5
6 /* filters state structs */
7
8 struct basexx_state {
9 size_t line, maxline;
10 size_t left;
11 int tail[5];
12 int flush;
13 };
14
15 struct runlength_state {
16 int run;
17 int flush;
18 int c1, c2;
19 uint8_t *pos;
20 };
21
22 typedef union { basexx_state *basexxstate; runlength_state *runlengthstate; void *voidstate; } basexx_state_pointer; // to avoid 'dereferencing type-puned ...' warnings
23
24 /* config */
25
26 #if defined(BASEXX_PDF)
27 # define ignored(c) (c == 0x20 || c == 0x0A || c == 0x0C || c == 0x0D || c == 0x09 || c == 0x00)
28 # define base16_eof(c) (c == '>' || c < 0)
29 # define base85_eof(c) (c == '~' || c < 0)
30 #else
31 # define ignored(c) (c == 0x20 || c == 0x0A || c == 0x0D || c == 0x09)
32 # define base16_eof(c) (c < 0)
33 # define base85_eof(c) (c < 0)
34 #endif
35
36 #define base64_eof(c) (c == '=' || c < 0)
37
38 #define basexx_nl '\x0A'
39 //#define put_nl(O, line, maxline, n) ((void)((line += n) > maxline && ((line = n), iof_set(O, basexx_nl)))) // assignment in conditional warning
40 #define put_nl(O, line, maxline, n) do { line += n; if (line > maxline) { line = n; iof_set(O, basexx_nl); }} while (0)
41
42 /* tail macros */
43
44 #define set_tail1(state, c1) (state->left = 1, state->tail[0] = c1)
45 #define set_tail2(state, c1, c2) (state->left = 2, state->tail[0] = c1, state->tail[1] = c2)
46 #define set_tail3(state, c1, c2, c3) (state->left = 3, state->tail[0] = c1, state->tail[1] = c2, state->tail[2] = c3)
47 #define set_tail4(state, c1, c2, c3, c4) (state->left = 4, state->tail[0] = c1, state->tail[1] = c2, state->tail[2] = c3, state->tail[3] = c4)
48 #define set_tail5(state, c1, c2, c3, c4, c5) \
49 (state->left = 5, state->tail[0] = c1, state->tail[1] = c2, state->tail[2] = c3, state->tail[3] = c4, state->tail[4] = c5)
50
51 #define get_tail1(state, c1) (state->left = 0, c1 = state->tail[0])
52 #define get_tail2(state, c1, c2) (state->left = 0, c1 = state->tail[0], c2 = state->tail[1])
53 #define get_tail3(state, c1, c2, c3) (state->left = 0, c1 = state->tail[0], c2 = state->tail[1], c3 = state->tail[2])
54 #define get_tail4(state, c1, c2, c3, c4) (state->left = 0, c1 = state->tail[0], c2 = state->tail[1], c3 = state->tail[2], c4 = state->tail[3])
55
56 /* basexx state initialization */
57
58 void basexx_state_init_ln (basexx_state *state, size_t line, size_t maxline)
59 {
60 state->line = line;
61 state->maxline = maxline;
62 state->left = 0;
63 state->flush = 0;
64 }
65
66 /* base 16; xxxx|xxxx */
67
68 iof_status base16_encoded_uc (const void *data, size_t size, iof *O)
69 {
70 const uint8_t *s, *e;
71 for (s = (const uint8_t *)data, e = s + size; s < e; ++s)
72 {
73 if (!iof_ensure(O, 2))
74 return IOFFULL;
75 iof_set_uc_hex(O, *s);
76 }
77 return IOFEOF;
78 }
79
80 iof_status base16_encoded_lc (const void *data, size_t size, iof *O)
81 {
82 const uint8_t *s, *e;
83 for (s = (const uint8_t *)data, e = s + size; s < e; ++s)
84 {
85 if (!iof_ensure(O, 2))
86 return IOFFULL;
87 iof_set_lc_hex(O, *s);
88 }
89 return IOFEOF;
90 }
91
92 iof_status base16_encoded_uc_ln (const void *data, size_t size, iof *O, size_t line, size_t maxline)
93 {
94 const uint8_t *s, *e;
95 for (s = (const uint8_t *)data, e = s + size; s < e; ++s)
96 {
97 if (!iof_ensure(O, 3))
98 return IOFFULL;
99 put_nl(O, line, maxline, 2);
100 iof_set_uc_hex(O, *s);
101 }
102 return IOFFULL;
103 }
104
105 iof_status base16_encoded_lc_ln (const void *data, size_t size, iof *O, size_t line, size_t maxline)
106 {
107 const uint8_t *s, *e;
108 for (s = (const uint8_t *)data, e = s + size; s < e; ++s)
109 {
110 if (!iof_ensure(O, 3))
111 return IOFFULL;
112 put_nl(O, line, maxline, 2);
113 iof_set_lc_hex(O, *s);
114 }
115 return IOFFULL;
116 }
117
118 iof_status base16_encode_uc (iof *I, iof *O)
119 {
120 register int c;
121 while (iof_ensure(O, 2))
122 {
123 if ((c = iof_get(I)) < 0)
124 return IOFEOF;
125 iof_set_uc_hex(O, c);
126 }
127 return IOFFULL;
128 }
129
130 iof_status base16_encode_state_uc (iof *I, iof *O, basexx_state *state)
131 {
132 register int c;
133 while (iof_ensure(O, 2))
134 {
135 if ((c = iof_get(I)) < 0)
136 return (state->flush ? IOFEOF : IOFEMPTY);
137 iof_set_uc_hex(O, c);
138 }
139 return IOFFULL;
140 }
141
142 iof_status base16_encode_lc (iof *I, iof *O)
143 {
144 register int c;
145 while (iof_ensure(O, 2))
146 {
147 if ((c = iof_get(I)) < 0)
148 return IOFEOF;
149 iof_set_lc_hex(O, c);
150 }
151 return IOFFULL;
152 }
153
154 iof_status base16_encode_state_lc (iof *I, iof *O, basexx_state *state)
155 {
156 register int c;
157 while (iof_ensure(O, 2))
158 {
159 if ((c = iof_get(I)) < 0)
160 return (state->flush ? IOFEOF : IOFEMPTY);
161 iof_set_lc_hex(O, c);
162 }
163 return IOFFULL;
164 }
165
166 iof_status base16_encode_uc_ln (iof *I, iof *O, size_t line, size_t maxline)
167 {
168 register int c;
169 while (iof_ensure(O, 3))
170 {
171 if ((c = iof_get(I)) < 0)
172 return IOFEOF;
173 put_nl(O, line, maxline, 2);
174 iof_set_uc_hex(O, c);
175 }
176 return IOFFULL;
177 }
178
179 iof_status base16_encode_state_uc_ln (iof *I, iof *O, basexx_state *state)
180 {
181 register int c;
182 while (iof_ensure(O, 3))
183 {
184 if ((c = iof_get(I)) < 0)
185 return (state->flush ? IOFEOF : IOFEMPTY);
186 put_nl(O, state->line, state->maxline, 2);
187 iof_set_uc_hex(O, c);
188 }
189 return IOFFULL;
190 }
191
192 iof_status base16_encode_lc_ln (iof *I, iof *O, size_t line, size_t maxline)
193 {
194 register int c;
195 while (iof_ensure(O, 3))
196 {
197 if ((c = iof_get(I)) < 0)
198 return IOFEOF;
199 put_nl(O, line, maxline, 2);
200 iof_set_lc_hex(O, c);
201 }
202 return IOFFULL;
203 }
204
205 iof_status base16_encode_state_lc_ln (iof *I, iof *O, basexx_state *state)
206 {
207 register int c;
208 while (iof_ensure(O, 3))
209 {
210 if ((c = iof_get(I)) < 0)
211 return (state->flush ? IOFEOF : IOFEMPTY);
212 put_nl(O, state->line, state->maxline, 2);
213 iof_set_lc_hex(O, c);
214 }
215 return IOFFULL;
216 }
217
218 int base16_getc (iof *I)
219 {
220 register int c1, c2;
221 do { c1 = iof_get(I); } while (ignored(c1));
222 if (base16_eof(c1))
223 return IOFEOF;
224 do { c2 = iof_get(I); } while (ignored(c2));
225 if (base16_eof(c2))
226 {
227 if ((c1 = base16_value(c1)) < 0)
228 return IOFERR;
229 return c1<<4;
230 }
231 if ((c1 = base16_value(c1)) < 0 || (c2 = base16_value(c2)) < 0)
232 return IOFERR;
233 return (c1<<4)|c2;
234 }
235
236 int base16_lc_putc (iof *O, int c)
237 {
238 if (iof_ensure(O, 2))
239 iof_set_lc_hex(O, c);
240 return IOFFULL;
241 }
242
243 int base16_uc_putc (iof *O, int c)
244 {
245 if (iof_ensure(O, 2))
246 iof_set_uc_hex(O, c);
247 return IOFFULL;
248 }
249
250
251 iof_status base16_decode (iof *I, iof *O)
252 {
253 register int c1, c2;
254 while (iof_ensure(O, 1))
255 {
256 do { c1 = iof_get(I); } while (ignored(c1));
257 if (base16_eof(c1))
258 return IOFEOF;
259 do { c2 = iof_get(I); } while (ignored(c2));
260 if (base16_eof(c2))
261 {
262 if ((c1 = base16_value(c1)) < 0)
263 return IOFERR;
264 iof_set(O, c1<<4); // c2 := '0'
265 return IOFEOF;
266 }
267 if ((c1 = base16_value(c1)) < 0 || (c2 = base16_value(c2)) < 0)
268 return IOFERR;
269 iof_set(O, (c1<<4)|c2);
270 }
271 return IOFFULL;
272 }
273
274 iof_status base16_decode_state (iof *I, iof *O, basexx_state *state)
275 {
276 register int c1, c2, d1, d2;
277 if (!(iof_ensure(O, 1)))
278 return IOFFULL;
279 switch(state->left)
280 {
281 case 0: goto byte0;
282 case 1: get_tail1(state, c1); goto byte1;
283 }
284 while (iof_ensure(O, 1))
285 {
286 byte0:
287 do { c1 = iof_get(I); } while (ignored(c1));
288 if (base16_eof(c1))
289 return (state->flush ? IOFEOF : IOFEMPTY);
290 byte1:
291 do { c2 = iof_get(I); } while (ignored(c2));
292 if (base16_eof(c2))
293 {
294 set_tail1(state, c1); /* set tail to let the caller display invalid chars */
295 if (state->flush)
296 {
297 if ((c1 = base16_value(c1)) < 0)
298 return IOFERR;
299 iof_set(O, c1<<4); // c2 := '0'
300 return IOFEOF;
301 }
302 return IOFEMPTY;
303 }
304 if ((d1 = base16_value(c1)) < 0 || (d2 = base16_value(c2)) < 0)
305 {
306 set_tail2(state, c1, c2);
307 return IOFERR;
308 }
309 iof_set(O, (d1<<4)|d2);
310 }
311 return IOFFULL;
312 }
313
314 /* base 64; xxxxxx|xx xxxx|xxxx xx|xxxxxx */
315
316 const char base64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
317
318 const int base64_lookup[] = {
319 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
320 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
321 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
322 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
323 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,9 ,10,11,12,13,14,
324 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
325 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
326 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
327 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
328 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
329 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
330 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
331 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
332 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
333 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
334 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
335 };
336
337 #define base64_value(c) base64_lookup[(uint8_t)(c)]
338
339 #define base64_digit1(c1) base64_alphabet[c1>>2]
340 #define base64_digit2(c1, c2) base64_alphabet[((c1&3)<<4)|(c2>>4)]
341 #define base64_digit3(c2, c3) base64_alphabet[((c2&15)<<2)|(c3>>6)]
342 #define base64_digit4(c3) base64_alphabet[c3&63]
343
344 #define base64_encode_word(O, c1, c2, c3) \
345 iof_set4(O, base64_digit1(c1), base64_digit2(c1, c2), base64_digit3(c2, c3), base64_digit4(c3))
346
347 #define base64_encode_tail2(O, c1, c2) \
348 iof_set3(O, base64_digit1(c1), base64_digit2(c1, c2), base64_digit3(c2, 0))
349
350 #define base64_encode_tail1(O, c1) \
351 iof_set2(O, base64_digit1(c1), base64_digit2(c1, 0))
352
353 iof_status base64_encoded (const void *data, size_t size, iof *O)
354 {
355 const uint8_t *s, *e;
356 uint8_t c1, c2, c3;
357 for (s = (const uint8_t *)data, e = s + size; s + 2 < e; )
358 {
359 if (!iof_ensure(O, 4))
360 return IOFFULL;
361 c1 = *s++;
362 c2 = *s++;
363 c3 = *s++;
364 base64_encode_word(O, c1, c2, c3);
365 }
366 switch (e - s)
367 {
368 case 0:
369 break;
370 case 1:
371 if (!iof_ensure(O, 2))
372 return IOFFULL;
373 c1 = *s;
374 base64_encode_tail1(O, c1);
375 break;
376 case 2:
377 if (!iof_ensure(O, 3))
378 return IOFFULL;
379 c1 = *s++;
380 c2 = *s;
381 base64_encode_tail2(O, c1, c2);
382 break;
383 }
384 return IOFEOF;
385 }
386
387 iof_status base64_encoded_ln (const void *data, size_t size, iof *O, size_t line, size_t maxline)
388 {
389 const uint8_t *s, *e;
390 uint8_t c1, c2, c3;
391 for (s = (const uint8_t *)data, e = s + size; s + 2 < e; )
392 {
393 if (!iof_ensure(O, 5))
394 return IOFFULL;
395 c1 = *s++;
396 c2 = *s++;
397 c3 = *s++;
398 put_nl(O, line, maxline, 4);
399 base64_encode_word(O, c1, c2, c3);
400 }
401 switch (e - s)
402 {
403 case 0:
404 break;
405 case 1:
406 if (!iof_ensure(O, 3))
407 return IOFFULL;
408 c1 = *s;
409 put_nl(O, line, maxline, 2);
410 base64_encode_tail1(O, c1);
411 break;
412 case 2:
413 if (!iof_ensure(O, 4))
414 return IOFFULL;
415 c1 = *s++;
416 c2 = *s;
417 put_nl(O, line, maxline, 3);
418 base64_encode_tail2(O, c1, c2);
419 break;
420 }
421 return IOFEOF;
422 }
423
424 iof_status base64_encode (iof *I, iof *O)
425 {
426 register int c1, c2, c3;
427 while(iof_ensure(O, 4))
428 {
429 if ((c1 = iof_get(I)) < 0)
430 return IOFEOF;
431 if ((c2 = iof_get(I)) < 0)
432 {
433 base64_encode_tail1(O, c1);
434 return IOFEOF;
435 }
436 if ((c3 = iof_get(I)) < 0)
437 {
438 base64_encode_tail2(O, c1, c2);
439 return IOFEOF;
440 }
441 base64_encode_word(O, c1, c2, c3);
442 }
443 return IOFFULL;
444 }
445
446 iof_status base64_encode_state (iof *I, iof *O, basexx_state *state)
447 {
448 register int c1, c2, c3;
449 if (!(iof_ensure(O, 4)))
450 return IOFFULL;
451 switch(state->left)
452 {
453 case 0: goto byte0;
454 case 1: get_tail1(state, c1); goto byte1;
455 case 2: get_tail2(state, c1, c2); goto byte2;
456 }
457 while(iof_ensure(O, 4))
458 {
459 byte0:
460 if ((c1 = iof_get(I)) < 0)
461 return (state->flush ? IOFEOF : IOFEMPTY);
462 byte1:
463 if ((c2 = iof_get(I)) < 0)
464 return (state->flush ? (base64_encode_tail1(O, c1), IOFEOF) : (set_tail1(state, c1), IOFEMPTY));
465 byte2:
466 if ((c3 = iof_get(I)) < 0)
467 return (state->flush ? (base64_encode_tail2(O, c1, c2), IOFEOF) : (set_tail2(state, c1, c2), IOFEMPTY));
468 base64_encode_word(O, c1, c2, c3);
469 }
470 return IOFFULL;
471 }
472
473 iof_status base64_encode_ln (iof *I, iof *O, size_t line, size_t maxline)
474 {
475 register int c1, c2, c3;
476 while(iof_ensure(O, 5))
477 {
478 if ((c1 = iof_get(I)) < 0)
479 return IOFEOF;
480 if ((c2 = iof_get(I)) < 0)
481 {
482 put_nl(O, line, maxline, 2);
483 base64_encode_tail1(O, c1);
484 return IOFEOF;
485 }
486 if ((c3 = iof_get(I)) < 0)
487 {
488 put_nl(O, line, maxline, 3);
489 base64_encode_tail2(O, c1, c2);
490 return IOFEOF;
491 }
492 put_nl(O, line, maxline, 4);
493 base64_encode_word(O, c1, c2, c3);
494 }
495 return IOFFULL;
496 }
497
498 iof_status base64_encode_state_ln (iof *I, iof *O, basexx_state *state)
499 {
500 register int c1, c2, c3;
501 if (!(iof_ensure(O, 5)))
502 return IOFFULL;
503 switch(state->left)
504 {
505 case 0: goto byte0;
506 case 1: get_tail1(state, c1); goto byte1;
507 case 2: get_tail2(state, c1, c2); goto byte2;
508 }
509 while(iof_ensure(O, 5))
510 {
511 byte0:
512 if ((c1 = iof_get(I)) < 0)
513 return (state->flush ? IOFEOF : IOFEMPTY);
514 byte1:
515 if ((c2 = iof_get(I)) < 0)
516 {
517 if (state->flush)
518 {
519 put_nl(O, state->line, state->maxline, 2);
520 base64_encode_tail1(O, c1);
521 return IOFEOF;
522 }
523 set_tail1(state, c1);
524 return IOFEMPTY;
525 }
526 byte2:
527 if ((c3 = iof_get(I)) < 0)
528 {
529 if (state->flush)
530 {
531 put_nl(O, state->line, state->maxline, 3);
532 base64_encode_tail2(O, c1, c2);
533 return IOFEOF;
534 }
535 set_tail2(state, c1, c2);
536 return IOFEMPTY;
537 }
538 put_nl(O, state->line, state->maxline, 4);
539 base64_encode_word(O, c1, c2, c3);
540 }
541 return IOFFULL;
542 }
543
544 // #define base64_code(c1, c2, c3, c4) ((c1<<18)|(c2<<12)|(c3<<6)|c4)
545
546 #define base64_decode_word(O, c1, c2, c3, c4) \
547 iof_set3(O, (c1<<2)|(c2>>4), ((c2&15)<<4)|(c3>>2), ((c3&3)<<6)|c4)
548
549 #define base64_decode_tail3(O, c1, c2, c3) \
550 iof_set2(O, (c1<<2)|(c2>>4), ((c2&15)<<4)|(c3>>2))
551
552 #define base64_decode_tail2(O, c1, c2) \
553 iof_set(O, (c1<<2)|(c2>>4))
554
555 iof_status base64_decode (iof *I, iof *O)
556 {
557 register int c1, c2, c3, c4;
558 while(iof_ensure(O, 3))
559 {
560 do { c1 = iof_get(I); } while (ignored(c1));
561 if (base64_eof(c1))
562 return IOFEOF;
563 do { c2 = iof_get(I); } while (ignored(c2));
564 if (base64_eof(c2))
565 return IOFERR;
566 do { c3 = iof_get(I); } while (ignored(c3));
567 if (base64_eof(c3))
568 {
569 if ((c1 = base64_value(c1)) < 0 || (c2 = base64_value(c2)) < 0)
570 return IOFERR;
571 base64_decode_tail2(O, c1, c2);
572 return IOFEOF;
573 }
574 do { c4 = iof_get(I); } while (ignored(c4));
575 if (base64_eof(c4))
576 {
577 if ((c1 = base64_value(c1)) < 0 || (c2 = base64_value(c2)) < 0 || (c3 = base64_value(c3)) < 0)
578 return IOFERR;
579 base64_decode_tail3(O, c1, c2, c3);
580 return IOFEOF;
581 }
582 if ((c1 = base64_value(c1)) < 0 || (c2 = base64_value(c2)) < 0 ||
583 (c3 = base64_value(c3)) < 0 || (c4 = base64_value(c4)) < 0)
584 return IOFERR;
585 base64_decode_word(O, c1, c2, c3, c4);
586 }
587 return IOFFULL;
588 }
589
590 iof_status base64_decode_state (iof *I, iof *O, basexx_state *state)
591 {
592 register int c1, c2, c3, c4;
593 register int d1, d2, d3, d4;
594 switch(state->left)
595 {
596 case 0: goto byte0;
597 case 1: get_tail1(state, c1); goto byte1;
598 case 2: get_tail2(state, c1, c2); goto byte2;
599 case 3: get_tail3(state, c1, c2, c3); goto byte3;
600 }
601 while(iof_ensure(O, 3))
602 {
603 byte0:
604 do { c1 = iof_get(I); } while (ignored(c1));
605 if (base64_eof(c1))
606 return (state->flush ? IOFEOF : IOFEMPTY);
607 byte1:
608 do { c2 = iof_get(I); } while (ignored(c2));
609 if (base64_eof(c2))
610 {
611 set_tail1(state, c1); /* set tail to let the caller make padding or display invalid char in case of error */
612 return (state->flush ? IOFERR : IOFEMPTY); /* if state->flush then error; tail must have at least two bytes */
613 }
614 byte2:
615 do { c3 = iof_get(I); } while (ignored(c3));
616 if (base64_eof(c3))
617 {
618 set_tail2(state, c1, c2);
619 if (state->flush)
620 {
621 if ((c1 = base64_value(c1)) < 0 || (c2 = base64_value(c2)) < 0)
622 return IOFERR;
623 base64_decode_tail2(O, c1, c2);
624 return IOFEOF;
625 }
626 else
627 return IOFEMPTY;
628 }
629 byte3:
630 do { c4 = iof_get(I); } while (ignored(c4));
631 if (base64_eof(c4))
632 {
633 set_tail3(state, c1, c2, c3);
634 if (state->flush)
635 {
636 if ((c1 = base64_value(c1)) < 0 || (c2 = base64_value(c2)) < 0 || (c3 = base64_value(c3)) < 0)
637 return IOFERR;
638 base64_decode_tail3(O, c1, c2, c3);
639 return IOFEOF;
640 }
641 else
642 return IOFEMPTY;
643 }
644 if ((d1 = base64_value(c1)) < 0 || (d2 = base64_value(c2)) < 0 ||
645 (d3 = base64_value(c3)) < 0 || (d4 = base64_value(c4)) < 0)
646 {
647 set_tail4(state, c1, c2, c3, c4);
648 return IOFERR;
649 }
650 base64_decode_word(O, d1, d2, d3, d4);
651 }
652 return IOFFULL;
653 }
654
655 /* base85 */
656
657 const char base85_alphabet[] = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu"; /* for completness, not used below */
658
659 const int base85_lookup[] = {
660 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
661 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
662 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
663 15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
664 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,
665 47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,
666 63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,
667 79,80,81,82,83,84,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
668 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
669 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
670 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
671 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
672 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
673 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
674 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
675 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
676 };
677
678 #define base85_value(c) base85_lookup[(uint8_t)(c)]
679
680 #define base85_encode_word(O, code) \
681 (*(O->pos+4) = '!' + code%85, code /= 85, *(O->pos+3) = '!' + code%85, code /= 85, \
682 *(O->pos+2) = '!' + code%85, code /= 85, *(O->pos+1) = '!' + code%85, code /= 85, \
683 *(O->pos) = '!' + code, \
684 O->pos += 5)
685
686 #define base85_encode_tail3(O, code) \
687 (*(O->pos+3) = '!' + code%85, code /= 85, *(O->pos+2) = '!' + code%85, code /= 85, \
688 *(O->pos+1) = '!' + code%85, code /= 85, *(O->pos) = '!' + code, \
689 O->pos += 4)
690
691 #define base85_encode_tail2(O, code) \
692 (*(O->pos+2) = '!' + code%85, code /= 85, *(O->pos+1) = '!' + code%85, code /= 85, \
693 *(O->pos) = '!' + code, \
694 O->pos += 3)
695
696 #define base85_encode_tail1(O, code) \
697 (*(O->pos+1) = '!' + code%85, code /= 85, *(O->pos) = '!' + code, \
698 O->pos += 2)
699
700 iof_status base85_encoded (const void *data, size_t size, iof *O)
701 {
702 unsigned int code;
703 const uint8_t *s, *e;
704 uint8_t c1, c2, c3, c4;
705 for (s = (const uint8_t *)data, e = s + size; s + 3 < e; )
706 {
707 if (!iof_ensure(O, 5))
708 return IOFFULL;
709 c1 = *s++;
710 c2 = *s++;
711 c3 = *s++;
712 c4 = *s++;
713 code = (c1<<24)|(c2<<16)|(c3<<8)|c4;
714 if (code == 0)
715 {
716 iof_set(O, 'z');
717 continue;
718 }
719 base85_encode_word(O, code);
720 }
721 switch (e - s)
722 {
723 case 0:
724 break;
725 case 1:
726 if (!iof_ensure(O, 2))
727 return IOFFULL;
728 c1 = *s;
729 code = (c1<<24)/85/85/85;
730 base85_encode_tail1(O, code);
731 break;
732 case 2:
733 if (!iof_ensure(O, 3))
734 return IOFFULL;
735 c1 = *s++;
736 c2 = *s;
737 code = ((c1<<24)|(c2<<16))/85/85;
738 base85_encode_tail2(O, code);
739 break;
740 case 3:
741 if (!iof_ensure(O, 4))
742 return IOFFULL;
743 c1 = *s++;
744 c2 = *s++;
745 c3 = *s;
746 code = ((c1<<24)|(c2<<16)|(c3<<8))/85;
747 base85_encode_tail3(O, code);
748 break;
749 }
750 return IOFEOF;
751 }
752
753 iof_status base85_encoded_ln (const void *data, size_t size, iof *O, size_t line, size_t maxline)
754 {
755 unsigned int code;
756 const uint8_t *s, *e;
757 uint8_t c1, c2, c3, c4;
758 for (s = (const uint8_t *)data, e = s + size; s + 3 < e; )
759 {
760 if (!iof_ensure(O, 6))
761 return IOFFULL;
762 c1 = *s++;
763 c2 = *s++;
764 c3 = *s++;
765 c4 = *s++;
766 code = (c1<<24)|(c2<<16)|(c3<<8)|c4;
767 if (code == 0)
768 {
769 put_nl(O, line, maxline, 1);
770 iof_set(O, 'z');
771 continue;
772 }
773 put_nl(O, line, maxline, 5);
774 base85_encode_word(O, code);
775 }
776 switch (e - s)
777 {
778 case 0:
779 break;
780 case 1:
781 if (!iof_ensure(O, 3))
782 return IOFFULL;
783 c1 = *s;
784 code = (c1<<24)/85/85/85;
785 put_nl(O, line, maxline, 2);
786 base85_encode_tail1(O, code);
787 break;
788 case 2:
789 if (!iof_ensure(O, 4))
790 return IOFFULL;
791 c1 = *s++;
792 c2 = *s;
793 code = ((c1<<24)|(c2<<16))/85/85;
794 put_nl(O, line, maxline, 3);
795 base85_encode_tail2(O, code);
796 break;
797 case 3:
798 if (!iof_ensure(O, 5))
799 return IOFFULL;
800 c1 = *s++;
801 c2 = *s++;
802 c3 = *s;
803 code = ((c1<<24)|(c2<<16)|(c3<<8))/85;
804 put_nl(O, line, maxline, 4);
805 base85_encode_tail3(O, code);
806 break;
807 }
808 return IOFEOF;
809 }
810
811 iof_status base85_encode (iof *I, iof *O)
812 {
813 register int c1, c2, c3, c4;
814 register unsigned int code;
815 while(iof_ensure(O, 5))
816 {
817 if ((c1 = iof_get(I)) < 0)
818 return IOFEOF;
819 if ((c2 = iof_get(I)) < 0)
820 {
821 code = (c1<<24)/85/85/85;
822 base85_encode_tail1(O, code);
823 return IOFEOF;
824 }
825 if ((c3 = iof_get(I)) < 0)
826 {
827 code = ((c1<<24)|(c2<<16))/85/85;
828 base85_encode_tail2(O, code);
829 return IOFEOF;
830 }
831 if ((c4 = iof_get(I)) < 0)
832 {
833 code = ((c1<<24)|(c2<<16)|(c3<<8))/85;
834 base85_encode_tail3(O, code);
835 return IOFEOF;
836 }
837 code = (c1<<24)|(c2<<16)|(c3<<8)|c4;
838 if (code == 0)
839 {
840 iof_set(O, 'z');
841 continue;
842 }
843 /* in btoa 'y' character stays for 0x20202020, but pdf does not support this */
844 /* if (code == 0x20202020)
845 {
846 iof_set(O, 'y');
847 continue;
848 } */
849 base85_encode_word(O, code);
850 }
851 return IOFFULL;
852 }
853
854 iof_status base85_encode_state (iof *I, iof *O, basexx_state *state)
855 {
856 register int c1, c2, c3, c4;
857 register unsigned int code;
858 if (!(iof_ensure(O, 5)))
859 return IOFFULL;
860 switch(state->left)
861 {
862 case 0: goto byte0;
863 case 1: get_tail1(state, c1); goto byte1;
864 case 2: get_tail2(state, c1, c2); goto byte2;
865 case 3: get_tail3(state, c1, c2, c3); goto byte3;
866 }
867 while(iof_ensure(O, 5))
868 {
869 byte0:
870 if ((c1 = iof_get(I)) < 0)
871 return (state->flush ? IOFEOF : IOFEMPTY);
872 byte1:
873 if ((c2 = iof_get(I)) < 0)
874 {
875 set_tail1(state, c1);
876 if (state->flush)
877 {
878 code = (c1<<24)/85/85/85;
879 base85_encode_tail1(O, code);
880 return IOFEOF;
881 }
882 return IOFEMPTY;
883 }
884 byte2:
885 if ((c3 = iof_get(I)) < 0)
886 {
887 set_tail2(state, c1, c2);
888 if (state->flush)
889 {
890 code = ((c1<<24)|(c2<<16))/85/85;
891 base85_encode_tail2(O, code);
892 return IOFEOF;
893 }
894 return IOFEMPTY;
895 }
896 byte3:
897 if ((c4 = iof_get(I)) < 0)
898 {
899 set_tail3(state, c1, c2, c3);
900 if (state->flush)
901 {
902 code = ((c1<<24)|(c2<<16)|(c3<<8))/85;
903 base85_encode_tail3(O, code);
904 return IOFEOF;
905 }
906 return IOFEMPTY;
907 }
908 code = (c1<<24)|(c2<<16)|(c3<<8)|c4;
909 if (code == 0)
910 {
911 iof_set(O, 'z');
912 continue;
913 }
914 base85_encode_word(O, code);
915 }
916 return IOFFULL;
917 }
918
919 iof_status base85_encode_ln (iof *I, iof *O, size_t line, size_t maxline)
920 {
921 register int c1, c2, c3, c4;
922 register unsigned int code;
923 while(iof_ensure(O, 6))
924 {
925 if ((c1 = iof_get(I)) < 0)
926 return IOFEOF;
927 if ((c2 = iof_get(I)) < 0)
928 {
929 code = (c1<<24)/85/85/85;
930 put_nl(O, line, maxline, 2);
931 base85_encode_tail1(O, code);
932 return IOFEOF;
933 }
934 if ((c3 = iof_get(I)) < 0)
935 {
936 code = ((c1<<24)|(c2<<16))/85/85;
937 put_nl(O, line, maxline, 3);
938 base85_encode_tail2(O, code);
939 return IOFEOF;
940 }
941 if ((c4 = iof_get(I)) < 0)
942 {
943 code = ((c1<<24)|(c2<<16)|(c3<<8))/85;
944 put_nl(O, line, maxline, 4);
945 base85_encode_tail3(O, code);
946 return IOFEOF;
947 }
948 code = (c1<<24)|(c2<<16)|(c3<<8)|c4;
949 if (code == 0)
950 {
951 put_nl(O, line, maxline, 1);
952 iof_set(O, 'z');
953 continue;
954 }
955 put_nl(O, line, maxline, 5);
956 base85_encode_word(O, code);
957 }
958 return IOFFULL;
959 }
960
961 iof_status base85_encode_state_ln (iof *I, iof *O, basexx_state *state)
962 {
963 register int c1, c2, c3, c4;
964 register unsigned int code;
965 if (!(iof_ensure(O, 6)))
966 return IOFFULL;
967 switch(state->left)
968 {
969 case 0: goto byte0;
970 case 1: get_tail1(state, c1); goto byte1;
971 case 2: get_tail2(state, c1, c2); goto byte2;
972 case 3: get_tail3(state, c1, c2, c3); goto byte3;
973 }
974 while(iof_ensure(O, 6))
975 {
976 byte0:
977 if ((c1 = iof_get(I)) < 0)
978 return (state->flush ? IOFEOF : IOFEMPTY);
979 byte1:
980 if ((c2 = iof_get(I)) < 0)
981 {
982 set_tail1(state, c1);
983 if (state->flush)
984 {
985 code = (c1<<24)/85/85/85;
986 put_nl(O, state->line, state->maxline, 2);
987 base85_encode_tail1(O, code);
988 return IOFEOF;
989 }
990 return IOFEMPTY;
991 }
992 byte2:
993 if ((c3 = iof_get(I)) < 0)
994 {
995 set_tail2(state, c1, c2);
996 if (state->flush)
997 {
998 code = ((c1<<24)|(c2<<16))/85/85;
999 put_nl(O, state->line, state->maxline, 3);
1000 base85_encode_tail2(O, code);
1001 return IOFEOF;
1002 }
1003 return IOFEMPTY;
1004 }
1005 byte3:
1006 if ((c4 = iof_get(I)) < 0)
1007 {
1008 set_tail3(state, c1, c2, c3);
1009 if (state->flush)
1010 {
1011 code = ((c1<<24)|(c2<<16)|(c3<<8))/85;
1012 put_nl(O, state->line, state->maxline, 4);
1013 base85_encode_tail3(O, code);
1014 return IOFEOF;
1015 }
1016 return IOFEMPTY;
1017 }
1018 code = (c1<<24)|(c2<<16)|(c3<<8)|c4;
1019 if (code == 0)
1020 {
1021 put_nl(O, state->line, state->maxline, 1);
1022 iof_set(O, 'z');
1023 continue;
1024 }
1025 put_nl(O, state->line, state->maxline, 5);
1026 base85_encode_word(O, code);
1027 }
1028 return IOFFULL;
1029 }
1030
1031 #define base85_code(c1, c2, c3, c4, c5) ((((c1*85+c2)*85+c3)*85+c4)*85+c5)
1032
1033 iof_status base85_decode (iof *I, iof *O)
1034 {
1035 register int c1, c2, c3, c4, c5;
1036 register unsigned int code;
1037 while (iof_ensure(O, 4))
1038 {
1039 do { c1 = iof_get(I); } while (ignored(c1));
1040 if (base85_eof(c1))
1041 return IOFEOF;
1042 switch (c1)
1043 {
1044 case 'z':
1045 iof_set4(O, '\0', '\0', '\0', '\0');
1046 continue;
1047 case 'y':
1048 iof_set4(O, ' ', ' ', ' ', ' ');
1049 continue;
1050 }
1051 do { c2 = iof_get(I); } while (ignored(c2));
1052 if (base85_eof(c2))
1053 return IOFERR;
1054 do { c3 = iof_get(I); } while (ignored(c3));
1055 if (base85_eof(c3))
1056 {
1057 if ((c1 = base85_value(c1)) < 0 || (c2 = base85_value(c2)) < 0)
1058 return IOFERR;
1059 code = base85_code(c1, c2, 84, 84, 84); /* padding with 'u' (117); 117-33 = 84 */
1060 iof_set(O, code>>24);
1061 return IOFEOF;
1062 }
1063 do { c4 = iof_get(I); } while (ignored(c4));
1064 if (base85_eof(c4))
1065 {
1066 if ((c1 = base85_value(c1)) < 0 || (c2 = base85_value(c2)) < 0 || (c3 = base85_value(c3)) < 0)
1067 return IOFERR;
1068 code = base85_code(c1, c2, c3, 84, 84);
1069 iof_set2(O, code>>24, (code>>16)&255);
1070 return IOFEOF;
1071 }
1072 do { c5 = iof_get(I); } while (ignored(c5));
1073 if (base85_eof(c5))
1074 {
1075 if ((c1 = base85_value(c1)) < 0 || (c2 = base85_value(c2)) < 0 ||
1076 (c3 = base85_value(c3)) < 0 || (c4 = base85_value(c4)) < 0)
1077 return IOFERR;
1078 code = base85_code(c1, c2, c3, c4, 84);
1079 iof_set3(O, code>>24, (code>>16)&255, (code>>8)&255);
1080 return IOFEOF;
1081 }
1082 if ((c1 = base85_value(c1)) < 0 || (c2 = base85_value(c2)) < 0 || (c3 = base85_value(c3)) < 0 ||
1083 (c4 = base85_value(c4)) < 0 || (c5 = base85_value(c5)) < 0)
1084 return IOFERR;
1085 code = base85_code(c1, c2, c3, c4, c5);
1086 iof_set4(O, code>>24, (code>>16)&255, (code>>8)&255, code&255);
1087 }
1088 return IOFFULL;
1089 }
1090
1091 iof_status base85_decode_state (iof *I, iof *O, basexx_state *state)
1092 {
1093 register int c1, c2, c3, c4, c5;
1094 register int d1, d2, d3, d4, d5;
1095 register unsigned int code;
1096 if (!(iof_ensure(O, 4)))
1097 return IOFFULL;
1098 switch(state->left)
1099 {
1100 case 0: goto byte0;
1101 case 1: get_tail1(state, c1); goto byte1;
1102 case 2: get_tail2(state, c1, c2); goto byte2;
1103 case 3: get_tail3(state, c1, c2, c3); goto byte3;
1104 case 4: get_tail4(state, c1, c2, c3, c4); goto byte4;
1105 }
1106 while (iof_ensure(O, 4))
1107 {
1108 byte0:
1109 do { c1 = iof_get(I); } while (ignored(c1));
1110 if (base85_eof(c1))
1111 return (state->flush ? IOFEOF : IOFEMPTY);
1112 switch (c1)
1113 {
1114 case 'z':
1115 iof_set4(O, '\0', '\0', '\0', '\0');
1116 continue;
1117 case 'y':
1118 iof_set4(O, ' ', ' ', ' ', ' ');
1119 continue;
1120 }
1121 byte1:
1122 do { c2 = iof_get(I); } while (ignored(c2));
1123 if (base85_eof(c2))
1124 {
1125 set_tail1(state, c1);
1126 return (state->flush ? IOFERR : IOFEMPTY); /* if state->flush then error; tail must have at least two bytes */
1127 }
1128 byte2:
1129 do { c3 = iof_get(I); } while (ignored(c3));
1130 if (base85_eof(c3))
1131 {
1132 set_tail2(state, c1, c2);
1133 if (state->flush)
1134 {
1135 if ((c1 = base85_value(c1)) < 0 || (c2 = base85_value(c2)) < 0)
1136 return IOFERR;
1137 code = base85_code(c1, c2, 84, 84, 84);
1138 iof_set(O, code>>24);
1139 return IOFEOF;
1140 }
1141 return IOFEMPTY;
1142 }
1143 byte3:
1144 do { c4 = iof_get(I); } while (ignored(c4));
1145 if (base85_eof(c4))
1146 {
1147 set_tail3(state, c1, c2, c3);
1148 if (state->flush)
1149 {
1150 if ((c1 = base85_value(c1)) < 0 || (c2 = base85_value(c2)) < 0 || (c3 = base85_value(c3)) < 0)
1151 return IOFERR;
1152 code = base85_code(c1, c2, c3, 84, 84);
1153 iof_set2(O, code>>24, (code>>16)&255);
1154 return IOFEOF;
1155 }
1156 return IOFEMPTY;
1157 }
1158 byte4:
1159 do { c5 = iof_get(I); } while (ignored(c5));
1160 if (base85_eof(c5))
1161 {
1162 set_tail4(state, c1, c2, c3, c4);
1163 if (state->flush)
1164 {
1165 if ((c1 = base85_value(c1)) < 0 || (c2 = base85_value(c2)) < 0 ||
1166 (c3 = base85_value(c3)) < 0 || (c4 = base85_value(c4)) < 0)
1167 return IOFERR;
1168 code = base85_code(c1, c2, c3, c4, 84);
1169 iof_set3(O, code>>24, (code>>16)&255, (code>>8)&255);
1170 return IOFEOF;
1171 }
1172 return IOFEMPTY;
1173 }
1174 if ((d1 = base85_value(c1)) < 0 || (d2 = base85_value(c2)) < 0 || (d3 = base85_value(c3)) < 0 ||
1175 (d4 = base85_value(c4)) < 0 || (d5 = base85_value(c5)) < 0)
1176 {
1177 set_tail5(state, c1, c2, c3, c4, c5);
1178 return IOFERR;
1179 }
1180 code = base85_code(d1, d2, d3, d4, d5);
1181 iof_set4(O, code>>24, (code>>16)&255, (code>>8)&255, code&255);
1182 }
1183 return IOFFULL;
1184 }
1185
1186 /* postscript run length */
1187
1188 void runlength_state_init (runlength_state *state)
1189 {
1190 state->run = -1;
1191 state->flush = 0;
1192 state->c1 = 0;
1193 state->c2 = 0;
1194 state->pos = NULL;
1195 }
1196
1197 iof_status runlength_encode (iof *I, iof *O)
1198 {
1199 register int c1, c2, run = -1;
1200 uint8_t *pos;
1201 c1 = 0, c2 = 0; /* avoid warning */
1202 while (iof_ensure(O, 1+128+1))
1203 { /* ensured space for single length byte, up to 128 bytes to be copied, possible eod marker */
1204 pos = O->pos++;
1205 switch (run)
1206 {
1207 case -1: /* initial state; get first byte */
1208 if ((c1 = iof_get(I)) < 0)
1209 return (*pos = 128, IOFEOF);
1210 run = 0;
1211 FALLTHRU // fall through
1212 case 0: /* `repeat' state; get another byte and compare */
1213 if ((c2 = iof_get(I)) < 0)
1214 return (*pos = 0, iof_set2(O, c1, 128), IOFEOF);
1215 run = (c1 == c2 ? 257-2 : 0);
1216 break;
1217 }
1218 if (run < 128)
1219 { /* single length byte, up to 128 bytes to be copied, possible eod marker */
1220 iof_set(O, c1);
1221 for (c1 = c2, c2 = iof_char(I); c1 != c2 && run < 127; c1 = c2, c2 = iof_next(I))
1222 {
1223 if (c2 < 0) /* O->pos must not change until next call to calling encoder!!! */
1224 return (*pos = (uint8_t)run+1, iof_set2(O, c1, 128), IOFEOF);
1225 iof_set(O, c1);
1226 ++run;
1227 }
1228 }
1229 else // if run > 128
1230 {
1231 for (c2 = iof_get(I); c1 == c2 && run > 129; c2 = iof_get(I))
1232 --run;
1233 if (c2 < 0)
1234 return (*pos = (uint8_t)run, iof_set2(O, c1, 128), IOFEOF);
1235 iof_set(O, c1);
1236 }
1237 *pos = (uint8_t)run;
1238 c1 = c2;
1239 run = 0;
1240 }
1241 return IOFFULL;
1242 }
1243
1244 iof_status runlength_encode_state (iof *I, iof *O, runlength_state *state)
1245 {
1246 while (iof_ensure(O, 3)) /* single length byte, the byte to be repeated and eod */
1247 {
1248 state->pos = O->pos++;
1249 switch (state->run)
1250 {
1251 case -1: /* initial state; get first byte */
1252 if ((state->c1 = iof_get(I)) < 0)
1253 return (state->flush ? (*state->pos = 128, IOFEOF) : IOFEMPTY);
1254 state->run = 0;
1255 FALLTHRU // fall through
1256 case 0: /* `repeat' state; get another byte and compare */
1257 if ((state->c2 = iof_get(I)) < 0)
1258 return (state->flush ? (*state->pos = 0, iof_set2(O, state->c1, 128), IOFEOF) : IOFEMPTY);
1259 state->run = (state->c1 == state->c2 ? 257-2 : 0);
1260 break;
1261 }
1262 if (state->run < 128)
1263 { /* ensure space for single length byte, up to 128 bytes to be copied, plus possible eod marker, minus those already copied */
1264 if (!iof_ensure(O, 1+128+1-state->run))
1265 return IOFFULL;
1266 iof_set(O, state->c1);
1267 for (state->c1 = state->c2, state->c2 = iof_char(I);
1268 state->c1 != state->c2 && state->run < 127;
1269 state->c1 = state->c2, state->c2 = iof_next(I))
1270 {
1271 if (state->c2 < 0) /* O->pos must not change until next call to calling encoder!!! */
1272 return (state->flush ? (*state->pos = (uint8_t)state->run+1, iof_set2(O, state->c1, 128), IOFEOF) : IOFEMPTY);
1273 iof_set(O, state->c1);
1274 ++state->run;
1275 }
1276 }
1277 else // if run > 128
1278 {
1279 for (state->c2 = iof_get(I); state->c1 == state->c2 && state->run > 129; state->c2 = iof_get(I))
1280 --state->run;
1281 if (state->c2 < 0)
1282 return (state->flush ? (*state->pos = (uint8_t)state->run, iof_set2(O, state->c1, 128), IOFEOF) : IOFEMPTY);
1283 iof_set(O, state->c1);
1284 }
1285 *state->pos = (uint8_t)state->run;
1286 state->c1 = state->c2;
1287 state->run = 0;
1288 }
1289 return IOFFULL;
1290 }
1291
1292 iof_status runlength_decode (iof *I, iof *O)
1293 {
1294 register int c, run = -1;
1295 while (1)
1296 {
1297 if (run == -1) /* initial state */
1298 {
1299 if ((run = iof_get(I)) < 0)
1300 {
1301 run = -1; /* don't assume IOFEOF == -1 */
1302 return IOFEOF;
1303 }
1304 }
1305 if (run < 128)
1306 { /* copy (run + 1) following bytes */
1307 while (run > -1)
1308 {
1309 if (iof_ensure(O, 1))
1310 {
1311 if ((c = iof_get(I)) < 0)
1312 return IOFERR;
1313 iof_set(O, c);
1314 --run;
1315 continue;
1316 }
1317 return IOFFULL;
1318 }
1319 }
1320 else if (run > 128)
1321 { /* replicate the following byte (257 - run) times */
1322 if ((c = iof_get(I)) < 0) /* cf. state-wise version; don't change input position until we got this byte */
1323 return IOFERR;
1324 while (run < 257)
1325 {
1326 if (iof_ensure(O, 1))
1327 {
1328 iof_set(O, c);
1329 ++run;
1330 continue;
1331 }
1332 return IOFFULL;
1333 }
1334 run = -1;
1335 }
1336 else // c == 128
1337 return IOFEOF;
1338 }
1339 // return IOFFULL;
1340 }
1341
1342 iof_status runlength_decode_state (iof *I, iof *O, runlength_state *state)
1343 {
1344 register int c;
1345 while (1)
1346 {
1347 if (state->run == -1) /* initial state */
1348 {
1349 if ((state->run = iof_char(I)) < 0)
1350 {
1351 state->run = -1; /* don't assume IOFEOF == -1 */
1352 return (state->flush ? IOFEOF : IOFEMPTY);
1353 }
1354 ++I->pos;
1355 }
1356 if (state->run < 128)
1357 { /* copy (state->run + 1) following bytes */
1358 while (state->run > -1)
1359 {
1360 if (iof_ensure(O, 1))
1361 {
1362 if ((c = iof_char(I)) < 0)
1363 return (state->flush ? IOFERR : IOFEMPTY);
1364 ++I->pos;
1365 iof_set(O, c);
1366 --state->run;
1367 continue;
1368 }
1369 return IOFFULL;
1370 }
1371 }
1372 else if (state->run > 128)
1373 { /* replicate the following byte (257 - state->run) times */
1374 if ((c = iof_char(I)) < 0)
1375 return (state->flush ? IOFERR : IOFEMPTY);
1376 ++I->pos;
1377 while (state->run < 257)
1378 {
1379 if (iof_ensure(O, 1))
1380 {
1381 iof_set(O, c);
1382 ++state->run;
1383 continue;
1384 }
1385 return IOFFULL;
1386 }
1387 state->run = -1;
1388 }
1389 else // c == 128
1390 return IOFEOF;
1391 }
1392 // return IOFFULL;
1393 }
1394
1395 /* filters */
1396
1397 // base16 decoder function
1398
1399 static size_t base16_decoder (iof *F, iof_mode mode)
1400 {
1401 basexx_state *state;
1402 iof_status status;
1403 size_t tail;
1404
1405 switch(mode)
1406 {
1407 case IOFLOAD:
1408 case IOFREAD:
1409 if (F->flags & IOF_STOPPED)
1410 return 0;
1411 tail = iof_tail(F);
1412 F->pos = F->buf + tail;
1413 F->end = F->buf + F->space;
1414 state = iof_filter_state(basexx_state *, F);
1415 do {
1416 status = base16_decode_state(F->next, F, state);
1417 } while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F));
1418 return iof_decoder_retval(F, "base16", status);
1419 case IOFCLOSE:
1420 iof_free(F);
1421 return 0;
1422 default:
1423 break;
1424 }
1425 return 0;
1426 }
1427
1428 // base16 encoder function
1429
1430 static size_t base16_encoder (iof *F, iof_mode mode)
1431 {
1432 basexx_state *state;
1433 iof_status status;
1434
1435 state = iof_filter_state(basexx_state *, F);
1436 switch (mode)
1437 {
1438 case IOFFLUSH:
1439 state->flush = 1;
1440 FALLTHRU // fall through
1441 case IOFWRITE:
1442 F->end = F->pos;
1443 F->pos = F->buf;
1444 status = base16_encode_state_ln(F, F->next, state);
1445 return iof_encoder_retval(F, "base16", status);
1446 case IOFCLOSE:
1447 if (!state->flush)
1448 base16_encoder(F, IOFFLUSH);
1449 iof_free(F);
1450 return 0;
1451 default:
1452 break;
1453 }
1454 return 0;
1455 }
1456
1457 // base64 decoder function
1458
1459 static size_t base64_decoder (iof *F, iof_mode mode)
1460 {
1461 basexx_state *state;
1462 iof_status status;
1463 size_t tail;
1464
1465 switch(mode)
1466 {
1467 case IOFLOAD:
1468 case IOFREAD:
1469 if (F->flags & IOF_STOPPED)
1470 return 0;
1471 tail = iof_tail(F);
1472 F->pos = F->buf + tail;
1473 F->end = F->buf + F->space;
1474 state = iof_filter_state(basexx_state *, F);
1475 do {
1476 status = base64_decode_state(F->next, F, state);
1477 } while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F));
1478 return iof_decoder_retval(F, "base64", status);
1479 case IOFCLOSE:
1480 iof_free(F);
1481 return 0;
1482 default:
1483 break;
1484 }
1485 return 0;
1486 }
1487
1488 // base64 encoder function
1489
1490 static size_t base64_encoder (iof *F, iof_mode mode)
1491 {
1492 basexx_state *state;
1493 iof_status status;
1494
1495 state = iof_filter_state(basexx_state *, F);
1496 switch (mode)
1497 {
1498 case IOFFLUSH:
1499 state->flush = 1;
1500 FALLTHRU // fall through
1501 case IOFWRITE:
1502 F->end = F->pos;
1503 F->pos = F->buf;
1504 status = base64_encode_state_ln(F, F->next, state);
1505 return iof_encoder_retval(F, "base64", status);
1506 case IOFCLOSE:
1507 if (!state->flush)
1508 base64_encoder(F, IOFFLUSH);
1509 iof_free(F);
1510 return 0;
1511 default:
1512 break;
1513 }
1514 return 0;
1515 }
1516
1517 // base85 decoder function
1518
1519 static size_t base85_decoder (iof *F, iof_mode mode)
1520 {
1521 basexx_state *state;
1522 iof_status status;
1523 size_t tail;
1524
1525 switch(mode)
1526 {
1527 case IOFLOAD:
1528 case IOFREAD:
1529 if (F->flags & IOF_STOPPED)
1530 return 0;
1531 tail = iof_tail(F);
1532 F->pos = F->buf + tail;
1533 F->end = F->buf + F->space;
1534 state = iof_filter_state(basexx_state *, F);
1535 do {
1536 status = base85_decode_state(F->next, F, state);
1537 } while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F));
1538 return iof_decoder_retval(F, "base85", status);
1539 case IOFCLOSE:
1540 iof_free(F);
1541 return 0;
1542 default:
1543 break;
1544 }
1545 return 0;
1546 }
1547
1548 // base85 encoder function
1549
1550 static size_t base85_encoder (iof *F, iof_mode mode)
1551 {
1552 basexx_state *state;
1553 iof_status status;
1554
1555 state = iof_filter_state(basexx_state *, F);
1556 switch (mode)
1557 {
1558 case IOFFLUSH:
1559 state->flush = 1;
1560 FALLTHRU // fall through
1561 case IOFWRITE:
1562 F->end = F->pos;
1563 F->pos = F->buf;
1564 status = base85_encode_state_ln(F, F->next, state);
1565 return iof_encoder_retval(F, "base85", status);
1566 case IOFCLOSE:
1567 if (!state->flush)
1568 base85_encoder(F, IOFFLUSH);
1569 iof_free(F);
1570 return 0;
1571 default:
1572 break;
1573 }
1574 return 0;
1575 }
1576
1577 // runlength decoder function
1578
1579 static size_t runlength_decoder (iof *F, iof_mode mode)
1580 {
1581 runlength_state *state;
1582 iof_status status;
1583 size_t tail;
1584
1585 switch(mode)
1586 {
1587 case IOFLOAD:
1588 case IOFREAD:
1589 if (F->flags & IOF_STOPPED)
1590 return 0;
1591 tail = iof_tail(F);
1592 F->pos = F->buf + tail;
1593 F->end = F->buf + F->space;
1594 state = iof_filter_state(runlength_state *, F);
1595 do {
1596 status = runlength_decode_state(F->next, F, state);
1597 } while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F));
1598 return iof_decoder_retval(F, "runlength", status);
1599 case IOFCLOSE:
1600 iof_free(F);
1601 return 0;
1602 default:
1603 break;
1604 }
1605 return 0;
1606 }
1607
1608 // runlength encoder function
1609
1610 static size_t runlength_encoder (iof *F, iof_mode mode)
1611 {
1612 runlength_state *state;
1613 iof_status status;
1614
1615 state = iof_filter_state(runlength_state *, F);
1616 switch (mode)
1617 {
1618 case IOFFLUSH:
1619 state->flush = 1;
1620 FALLTHRU // fall through
1621 case IOFWRITE:
1622 F->end = F->pos;
1623 F->pos = F->buf;
1624 status = runlength_encode_state(F, F->next, state);
1625 return iof_encoder_retval(F, "runlength", status);
1626 case IOFCLOSE:
1627 if (!state->flush)
1628 runlength_encoder(F, IOFFLUSH);
1629 iof_free(F);
1630 return 0;
1631 default:
1632 break;
1633 }
1634 return 0;
1635 }
1636
1637 //
1638
1639 int iof_filter_basexx_encoder_ln (iof *F, size_t line, size_t maxline)
1640 {
1641 basexx_state *state;
1642 if (maxline > 8 && line < maxline)
1643 {
1644 state = iof_filter_state(basexx_state *, F);
1645 state->line = line;
1646 state->maxline = maxline;
1647 return 1;
1648 }
1649 return 0;
1650 }
1651
1652 /* base 16 */
1653
1654 iof * iof_filter_base16_decoder (iof *N)
1655 {
1656 iof *I;
1657 basexx_state_pointer P;
1658 I = iof_filter_reader(base16_decoder, sizeof(basexx_state), &P.voidstate);
1659 iof_setup_next(I, N);
1660 basexx_state_init(P.basexxstate);
1661 P.basexxstate->flush = 1; // means N is supposed to be continuous input
1662 return I;
1663 }
1664
1665 iof * iof_filter_base16_encoder (iof *N)
1666 {
1667 iof *O;
1668 basexx_state_pointer P;
1669 O = iof_filter_writer(base16_encoder, sizeof(basexx_state), &P.voidstate);
1670 iof_setup_next(O, N);
1671 basexx_state_init(P.basexxstate);
1672 return O;
1673 }
1674
1675 /* base 64 */
1676
1677 iof * iof_filter_base64_decoder (iof *N)
1678 {
1679 iof *I;
1680 basexx_state_pointer P;
1681 I = iof_filter_reader(base64_decoder, sizeof(basexx_state), &P.voidstate);
1682 iof_setup_next(I, N);
1683 basexx_state_init(P.basexxstate);
1684 P.basexxstate->flush = 1;
1685 return I;
1686 }
1687
1688 iof * iof_filter_base64_encoder (iof *N)
1689 {
1690 iof *O;
1691 basexx_state_pointer P;
1692 O = iof_filter_writer(base64_encoder, sizeof(basexx_state), &P.voidstate);
1693 iof_setup_next(O, N);
1694 basexx_state_init(P.basexxstate);
1695 return O;
1696 }
1697
1698 /* base 85 */
1699
1700 iof * iof_filter_base85_decoder (iof *N)
1701 {
1702 iof *I;
1703 basexx_state_pointer P;
1704 I = iof_filter_reader(base85_decoder, sizeof(basexx_state), &P.voidstate);
1705 iof_setup_next(I, N);
1706 basexx_state_init(P.basexxstate);
1707 P.basexxstate->flush = 1;
1708 return I;
1709 }
1710
1711 iof * iof_filter_base85_encoder (iof *N)
1712 {
1713 iof *O;
1714 basexx_state_pointer P;
1715 O = iof_filter_writer(base85_encoder, sizeof(basexx_state), &P.voidstate);
1716 iof_setup_next(O, N);
1717 basexx_state_init(P.basexxstate);
1718 return O;
1719 }
1720
1721 /* runlength stream filter */
1722
1723 iof * iof_filter_runlength_decoder (iof *N)
1724 {
1725 iof *I;
1726 basexx_state_pointer P;
1727 I = iof_filter_reader(runlength_decoder, sizeof(runlength_state), &P.voidstate);
1728 iof_setup_next(I, N);
1729 runlength_state_init(P.runlengthstate);
1730 P.runlengthstate->flush = 1;
1731 return I;
1732 }
1733
1734 iof * iof_filter_runlength_encoder (iof *N)
1735 {
1736 iof *O;
1737 basexx_state_pointer P;
1738 O = iof_filter_writer(runlength_encoder, sizeof(runlength_state), &P.voidstate);
1739 iof_setup_next(O, N);
1740 runlength_state_init(P.runlengthstate);
1741 return O;
1742 }