SmartPDF - lightweight pdf viewer app for rosapps
[reactos.git] / rosapps / smartpdf / fitz / stream / filt_ahxd.c
1 #include "fitz-base.h"
2 #include "fitz-stream.h"
3
4 typedef struct fz_ahxd_s fz_ahxd;
5
6 struct fz_ahxd_s
7 {
8 fz_filter super;
9 int odd;
10 int a;
11 };
12
13 static inline int iswhite(int a)
14 {
15 switch (a) {
16 case '\n': case '\r': case '\t': case ' ':
17 case '\0': case '\f': case '\b': case 0177:
18 return 1;
19 }
20 return 0;
21 }
22
23 static inline int ishex(int a)
24 {
25 return (a >= 'A' && a <= 'F') ||
26 (a >= 'a' && a <= 'f') ||
27 (a >= '0' && a <= '9');
28 }
29
30 static inline int fromhex(int a)
31 {
32 if (a >= 'A' && a <= 'F')
33 return a - 'A' + 0xA;
34 if (a >= 'a' && a <= 'f')
35 return a - 'a' + 0xA;
36 if (a >= '0' && a <= '9')
37 return a - '0';
38 return 0;
39 }
40
41 fz_error *
42 fz_newahxd(fz_filter **fp, fz_obj *params)
43 {
44 FZ_NEWFILTER(fz_ahxd, f, ahxd);
45 f->odd = 0;
46 f->a = 0;
47 return nil;
48 }
49
50 void
51 fz_dropahxd(fz_filter *f)
52 {
53 }
54
55 fz_error *
56 fz_processahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out)
57 {
58 fz_ahxd *f = (fz_ahxd*)filter;
59 int b, c;
60
61 while (1)
62 {
63 if (in->rp == in->wp)
64 return fz_ioneedin;
65
66 if (out->wp == out->ep)
67 return fz_ioneedout;
68
69 c = *in->rp++;
70
71 if (ishex(c)) {
72 if (!f->odd) {
73 f->a = fromhex(c);
74 f->odd = 1;
75 }
76 else {
77 b = fromhex(c);
78 *out->wp++ = (f->a << 4) | b;
79 f->odd = 0;
80 }
81 }
82
83 else if (c == '>') {
84 if (f->odd)
85 *out->wp++ = (f->a << 4);
86 out->eof = 1;
87 return fz_iodone;
88 }
89
90 else if (!iswhite(c)) {
91 return fz_throw("ioerror: bad data in ahxd: '%c'", c);
92 }
93 }
94 }
95
96 void
97 fz_pushbackahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out, int n)
98 {
99 int k;
100
101 assert(filter->process == fz_processahxd);
102 assert(out->wp - n >= out->rp);
103
104 k = 0;
105 while (k < n * 2) {
106 in->rp --;
107 if (ishex(*in->rp))
108 k ++;
109 }
110
111 out->wp -= n;
112 }
113