w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

utilbasexx.c
Go to the documentation of this file.
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 
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 
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 
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 
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 
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 
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 
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 
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 
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;
375  break;
376  case 2:
377  if (!iof_ensure(O, 3))
378  return IOFFULL;
379  c1 = *s++;
380  c2 = *s;
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);
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);
419  break;
420  }
421  return IOFEOF;
422 }
423 
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  {
434  return IOFEOF;
435  }
436  if ((c3 = iof_get(I)) < 0)
437  {
439  return IOFEOF;
440  }
441  base64_encode_word(O, c1, c2, c3);
442  }
443  return IOFFULL;
444 }
445 
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);
484  return IOFEOF;
485  }
486  if ((c3 = iof_get(I)) < 0)
487  {
488  put_nl(O, line, maxline, 3);
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 
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);
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);
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 
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;
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 
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;
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  }
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;
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;
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;
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);
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);
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);
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);
806  break;
807  }
808  return IOFEOF;
809 }
810 
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;
823  return IOFEOF;
824  }
825  if ((c3 = iof_get(I)) < 0)
826  {
827  code = ((c1<<24)|(c2<<16))/85/85;
829  return IOFEOF;
830  }
831  if ((c4 = iof_get(I)) < 0)
832  {
833  code = ((c1<<24)|(c2<<16)|(c3<<8))/85;
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  } */
850  }
851  return IOFFULL;
852 }
853 
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;
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;
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;
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  }
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);
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);
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);
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);
957  }
958  return IOFFULL;
959 }
960 
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);
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);
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);
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);
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 
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 
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 
1189 {
1190  state->run = -1;
1191  state->flush = 0;
1192  state->c1 = 0;
1193  state->c2 = 0;
1194  state->pos = NULL;
1195 }
1196 
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 
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 
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 
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 {
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;
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 {
1434 
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)
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 {
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;
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 {
1494 
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)
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 {
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;
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 {
1554 
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)
1569  iof_free(F);
1570  return 0;
1571  default:
1572  break;
1573  }
1574  return 0;
1575 }
1576 
1577 // runlength decoder function
1578 
1580 {
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;
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 
1611 {
1614 
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)
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 {
1642  if (maxline > 8 && line < maxline)
1643  {
1645  state->line = line;
1646  state->maxline = maxline;
1647  return 1;
1648  }
1649  return 0;
1650 }
1651 
1652 /* base 16 */
1653 
1655 {
1656  iof *I;
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 
1666 {
1667  iof *O;
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 
1678 {
1679  iof *I;
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 
1689 {
1690  iof *O;
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 
1701 {
1702  iof *I;
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 
1712 {
1713  iof *O;
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 
1724 {
1725  iof *I;
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 
1735 {
1736  iof *O;
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 }
int code
Definition: aftopl.c:52
#define state
Definition: aptex-macros.h:996
#define tail
Definition: aptex-macros.h:514
#define mode
Definition: aptex-macros.h:510
struct rect data
Definition: dvipdfm.c:64
#define s
Definition: afcover.h:80
paragraph P
#define c(n)
Definition: gpos-common.c:150
#define NULL
Definition: ftobjs.h:61
#define F(x, y, z)
Definition: md5.c:51
#define I(x, y, z)
Definition: md5.c:55
unsigned char uint8_t
Definition: stdint.h:78
const int * pos
Definition: combiners.h:905
#define O(_t, _a, _f)
Definition: makeint.h:501
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
static int size
Definition: ppmlabel.c:24
char line[1024]
Definition: process_score.c:29
#define status
iof_status runlength_decode_state(iof *I, iof *O, runlength_state *state)
Definition: utilbasexx.c:1342
const char base85_alphabet[]
Definition: utilbasexx.c:657
iof_status base16_encode_state_lc(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:154
iof * iof_filter_base64_encoder(iof *N)
Definition: utilbasexx.c:1688
iof * iof_filter_base16_encoder(iof *N)
Definition: utilbasexx.c:1665
iof_status runlength_encode_state(iof *I, iof *O, runlength_state *state)
Definition: utilbasexx.c:1244
iof_status base85_encode(iof *I, iof *O)
Definition: utilbasexx.c:811
iof_status base64_encoded_ln(const void *data, size_t size, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:387
iof_status base16_encoded_lc_ln(const void *data, size_t size, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:105
iof_status base16_encode_uc_ln(iof *I, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:166
iof_status base85_decode_state(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:1091
void basexx_state_init_ln(basexx_state *state, size_t line, size_t maxline)
Definition: utilbasexx.c:58
iof * iof_filter_base85_encoder(iof *N)
Definition: utilbasexx.c:1711
iof_status base16_encode_lc(iof *I, iof *O)
Definition: utilbasexx.c:142
iof * iof_filter_base64_decoder(iof *N)
Definition: utilbasexx.c:1677
iof_status runlength_encode(iof *I, iof *O)
Definition: utilbasexx.c:1197
iof_status base85_encode_state_ln(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:961
iof * iof_filter_base85_decoder(iof *N)
Definition: utilbasexx.c:1700
iof_status base16_encoded_uc_ln(const void *data, size_t size, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:92
int iof_filter_basexx_encoder_ln(iof *F, size_t line, size_t maxline)
Definition: utilbasexx.c:1639
iof_status base16_decode_state(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:274
iof_status runlength_decode(iof *I, iof *O)
Definition: utilbasexx.c:1292
iof_status base85_encode_state(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:854
iof_status base16_encode_state_uc(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:130
iof * iof_filter_runlength_encoder(iof *N)
Definition: utilbasexx.c:1734
iof_status base64_encode_state(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:446
iof_status base64_decode_state(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:590
iof_status base16_encoded_uc(const void *data, size_t size, iof *O)
Definition: utilbasexx.c:68
iof_status base16_encode_state_uc_ln(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:179
int base16_uc_putc(iof *O, int c)
Definition: utilbasexx.c:243
const char base64_alphabet[]
Definition: utilbasexx.c:316
iof * iof_filter_base16_decoder(iof *N)
Definition: utilbasexx.c:1654
iof_status base16_encode_uc(iof *I, iof *O)
Definition: utilbasexx.c:118
iof * iof_filter_runlength_decoder(iof *N)
Definition: utilbasexx.c:1723
iof_status base85_encode_ln(iof *I, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:919
iof_status base85_decode(iof *I, iof *O)
Definition: utilbasexx.c:1033
iof_status base16_decode(iof *I, iof *O)
Definition: utilbasexx.c:251
iof_status base64_encoded(const void *data, size_t size, iof *O)
Definition: utilbasexx.c:353
iof_status base85_encoded(const void *data, size_t size, iof *O)
Definition: utilbasexx.c:700
const int base64_lookup[]
Definition: utilbasexx.c:318
int base16_getc(iof *I)
Definition: utilbasexx.c:218
iof_status base64_decode(iof *I, iof *O)
Definition: utilbasexx.c:555
iof_status base16_encoded_lc(const void *data, size_t size, iof *O)
Definition: utilbasexx.c:80
void runlength_state_init(runlength_state *state)
Definition: utilbasexx.c:1188
const int base85_lookup[]
Definition: utilbasexx.c:659
iof_status base16_encode_lc_ln(iof *I, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:192
iof_status base64_encode_state_ln(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:498
iof_status base16_encode_state_lc_ln(iof *I, iof *O, basexx_state *state)
Definition: utilbasexx.c:205
iof_status base85_encoded_ln(const void *data, size_t size, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:753
iof_status base64_encode_ln(iof *I, iof *O, size_t line, size_t maxline)
Definition: utilbasexx.c:473
iof_status base64_encode(iof *I, iof *O)
Definition: utilbasexx.c:424
int base16_lc_putc(iof *O, int c)
Definition: utilbasexx.c:236
#define base16_encode_state_ln
Definition: utilbasexx.h:49
#define basexx_state_init(state)
Definition: utilbasexx.h:17
size_t iof_encoder_retval(iof *O, const char *type, iof_status status)
Definition: utiliof.c:2343
size_t iof_decoder_retval(iof *I, const char *type, iof_status status)
Definition: utiliof.c:2322
void iof_free(iof *F)
Definition: utiliof.c:2261
#define iof_set4(O, c1, c2, c3, c4)
Definition: utiliof.h:456
#define iof_char(I)
Definition: utiliof.h:389
#define iof_set_lc_hex(O, c)
Definition: utiliof.h:474
#define iof_ensure(O, n)
Definition: utiliof.h:352
#define iof_filter_state(statetype, F)
Definition: utiliof.h:635
#define iof_set2(O, c1, c2)
Definition: utiliof.h:454
#define iof_filter_reader(handler, statesize, pstate)
Definition: utiliof.h:627
iof_status
Definition: utiliof.h:25
@ IOFEOF
Definition: utiliof.h:26
@ IOFFULL
Definition: utiliof.h:28
@ IOFERR
Definition: utiliof.h:29
@ IOFEMPTY
Definition: utiliof.h:27
#define iof_setup_next(I, N)
Definition: utiliof.h:293
#define iof_set3(O, c1, c2, c3)
Definition: utiliof.h:455
#define iof_set(O, c)
Definition: utiliof.h:453
#define iof_set_uc_hex(O, c)
Definition: utiliof.h:473
iof_mode
Definition: utiliof.h:15
@ IOFCLOSE
Definition: utiliof.h:20
@ IOFREAD
Definition: utiliof.h:16
@ IOFLOAD
Definition: utiliof.h:17
@ IOFFLUSH
Definition: utiliof.h:19
@ IOFWRITE
Definition: utiliof.h:18
#define IOF_STOPPED
Definition: utiliof.h:111
#define iof_filter_writer(handler, statesize, pstate)
Definition: utiliof.h:631
#define iof_resize_buffer(O)
Definition: utiliof.h:641
#define iof_next(I)
Definition: utiliof.h:393
#define iof_get(I)
Definition: utiliof.h:385
#define iof_tail(I)
Definition: utiliof.h:197
#define base16_value(c)
Definition: utilnumber.h:100
#define FALLTHRU
Definition: utilplat.h:28
static size_t base64_encoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1490
static size_t base16_decoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1399
#define base64_encode_tail2(O, c1, c2)
Definition: utilbasexx.c:347
#define set_tail5(state, c1, c2, c3, c4, c5)
Definition: utilbasexx.c:48
#define base16_eof(c)
Definition: utilbasexx.c:28
static size_t base16_encoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1430
#define base85_encode_tail2(O, code)
Definition: utilbasexx.c:691
#define set_tail1(state, c1)
Definition: utilbasexx.c:44
#define get_tail3(state, c1, c2, c3)
Definition: utilbasexx.c:53
#define get_tail1(state, c1)
Definition: utilbasexx.c:51
#define base64_encode_word(O, c1, c2, c3)
Definition: utilbasexx.c:344
#define set_tail3(state, c1, c2, c3)
Definition: utilbasexx.c:46
#define base64_encode_tail1(O, c1)
Definition: utilbasexx.c:350
#define base85_encode_word(O, code)
Definition: utilbasexx.c:680
#define base85_code(c1, c2, c3, c4, c5)
Definition: utilbasexx.c:1031
static size_t runlength_encoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1610
#define base85_encode_tail3(O, code)
Definition: utilbasexx.c:686
static size_t base64_decoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1459
#define get_tail2(state, c1, c2)
Definition: utilbasexx.c:52
#define base85_value(c)
Definition: utilbasexx.c:678
#define put_nl(O, line, maxline, n)
Definition: utilbasexx.c:40
#define ignored(c)
Definition: utilbasexx.c:27
#define base64_decode_word(O, c1, c2, c3, c4)
Definition: utilbasexx.c:546
#define base64_value(c)
Definition: utilbasexx.c:337
#define base64_eof(c)
Definition: utilbasexx.c:36
#define base64_decode_tail3(O, c1, c2, c3)
Definition: utilbasexx.c:549
#define set_tail4(state, c1, c2, c3, c4)
Definition: utilbasexx.c:47
static size_t base85_decoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1519
#define base64_decode_tail2(O, c1, c2)
Definition: utilbasexx.c:552
static size_t runlength_decoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1579
#define base85_eof(c)
Definition: utilbasexx.c:29
#define base85_encode_tail1(O, code)
Definition: utilbasexx.c:696
#define get_tail4(state, c1, c2, c3, c4)
Definition: utilbasexx.c:54
static size_t base85_encoder(iof *F, iof_mode mode)
Definition: utilbasexx.c:1550
#define set_tail2(state, c1, c2)
Definition: utilbasexx.c:45
d1
Definition: sec_div.c:81
ShellFileEnvironment e
Definition: sh6.c:388
#define uint8_t
Definition: stdint.in.h:154
size_t line
Definition: utilbasexx.c:9
size_t maxline
Definition: utilbasexx.c:9
int tail[5]
Definition: utilbasexx.c:11
size_t left
Definition: utilbasexx.c:10
Definition: inftrees.h:24
Definition: utiliof.h:83
Definition: bdf.c:133
uint8_t * pos
Definition: utilbasexx.c:19
#define c2
Definition: t1io.c:53
#define c1
Definition: t1io.c:52
long unsigned N
Definition: tex4ht.c:2765
int run(char *cmd)
Definition: texdocc.c:233