summaryrefslogtreecommitdiff
path: root/filter/ahxe.c
blob: e092653b343c594dc25c3e12cc3b6059a44a409d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <fitz.h>

typedef struct fz_ahxe_s fz_ahxe;

struct fz_ahxe_s
{
	fz_filter super;
	int c;
};

static const char tohex[16] = "0123456789ABCDEF";

fz_error *
fz_newahxe(fz_filter **fp, fz_obj *params)
{
	FZ_NEWFILTER(fz_ahxe, f, ahxe);
	f->c = 0;
	return nil;
}

void
fz_freeahxe(fz_filter *f)
{
	fz_free(f);
}

fz_error *
fz_processahxe(fz_filter *filter, fz_buffer *in, fz_buffer *out)
{
	fz_ahxe *f = (fz_ahxe*)filter;
	int a, b, c;

	while (1)
	{
		if (in->rp == in->wp)
			goto needinput;

		if (out->wp + 2 >= out->ep) /* can write 3 bytes from 1 */
			return fz_ioneedout;

		c = *in->rp++;
		a = tohex[(c >> 4) & 0x0f];
		b = tohex[c & 0x0f];

		*out->wp++ = a;
		*out->wp++ = b;

		f->c += 2;
		if (f->c == 60) {
			*out->wp++ = '\n';
			f->c = 0;
		}
	}

needinput:
	if (in->eof) {
		if (out->wp == out->ep)
			return fz_ioneedout;
		*out->wp++ = '>';
		out->eof = 1;
		return fz_iodone;
	}
	return fz_ioneedin;
}