2 * UCW Library -- I/O Wrapper for Hexadecimal Escaped Debugging Output
4 * (c) 2015 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/chartype.h>
12 #include <ucw/fastbuf.h>
13 #include <ucw/fw-hex.h>
15 #define HEX_BUFSIZE 1024
20 byte buf[HEX_BUFSIZE];
22 #define FB_HEX(f) ((struct fb_hex *)(f))
25 fb_hex_spout(struct fastbuf *f)
27 struct fastbuf *orig = FB_HEX(f)->orig;
29 for (byte *p = f->buffer; p < f->bptr; p++)
32 if (c >= 0x21 && c <= 0x7e && c != '<' && c != '>')
35 bprintf(orig, "<%02x>", c);
41 fb_hex_refill(struct fastbuf *f)
43 struct fastbuf *orig = FB_HEX(f)->orig;
45 f->bptr = f->bstop = f->buffer;
46 while (f->bstop < f->bufend)
54 for (int i=0; i<2; i++)
58 bthrow(f, "fbhex", "fb_hex: Malformed hexadecimal representation");
59 x = (x << 4) | Cxvalue(d);
63 bthrow(f, "fbhex", "fb_hex: Expecting '>'");
69 return (f->bstop > f->bptr);
73 fb_hex_close(struct fastbuf *f)
76 bputc(FB_HEX(f)->orig, '\n');
77 bflush(FB_HEX(f)->orig);
81 struct fastbuf *fb_wrap_hex_out(struct fastbuf *f)
83 struct fastbuf *g = xmalloc_zero(sizeof(struct fb_hex));
85 g->name = "<hex-out>";
86 g->spout = fb_hex_spout;
87 g->close = fb_hex_close;
88 g->buffer = g->bstop = g->bptr = FB_HEX(g)->buf;
89 g->bufend = g->buffer + HEX_BUFSIZE;
93 struct fastbuf *fb_wrap_hex_in(struct fastbuf *f)
95 struct fastbuf *g = xmalloc_zero(sizeof(struct fb_hex));
98 g->refill = fb_hex_refill;
99 g->close = fb_hex_close;
100 g->buffer = g->bstop = g->bptr = FB_HEX(g)->buf;
101 g->bufend = g->buffer + HEX_BUFSIZE;