3 * Hexadecimal encoding and decoding.
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 /*----- Important tables --------------------------------------------------*/
39 static const char encodemap[] = { "0123456789abcdef" };
41 static const signed char decodemap[] = {
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
45 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, -1, -1, -1, -1, -1, /* 3x */
46 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 4x */
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 5x */
48 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 6x */
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 7x */
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @hex_encode@ --- *
56 * Arguments: @hex_ctx *ctx@ = pointer to a context block
57 * @const void *p@ = pointer to a source buffer
58 * @size_t sz@ = size of the source buffer
59 * @dstr *d@ = pointer to destination string
63 * Use: Encodes a binary string in hex.
66 void hex_encode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
69 const unsigned char *src = p;
74 DPUTC(d, encodemap[(x >> 4) & 0xf]);
75 DPUTC(d, encodemap[(x >> 0) & 0xf]);
77 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
78 dstr_puts(d, ctx->indent);
85 /* --- @hex_decode@ --- *
87 * Arguments: @hex_ctx *ctx@ = pointer to a context block
88 * @const void *p@ = pointer to a source buffer
89 * @size_t sz@ = size of the source buffer
90 * @dstr *d@ = pointer to destination string
94 * Use: Decodes a binary string in hex. Pass in a null source
95 * pointer when you thing you've finished.
98 void hex_decode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
101 unsigned long acc = ctx->acc;
102 unsigned qsz = ctx->qsz;
108 /* --- Get the next character and convert it --- */
111 if (ch >= 128 || ch < 0)
119 /* --- Bung it in the accumulator --- */
121 acc = (acc << 4) | ch;
124 /* --- Maybe write out a completed triplet --- */
127 DPUTC(d, acc & 0xff);
137 DPUTC(d, ctx->acc << 4);
141 /* --- @hex_init@ --- *
143 * Arguments: @hex_ctx *ctx@ = pointer to context block to initialize
147 * Use: Initializes a hex context properly.
150 void hex_init(hex_ctx *ctx)
159 /*----- Test driver code --------------------------------------------------*/
163 int main(int argc, char *argv[])
165 unsigned char buf[BUFSIZ];
168 void (*proc)(hex_ctx *, const void *, size_t, dstr *);
173 if (argc > 1 && strcmp(argv[1], "-d") == 0)
183 sz = fread(buf, 1, sizeof(buf), stdin);
185 proc(&ctx, buf, sz, &d);
186 dstr_write(&d, stdout);
189 } while (sz == sizeof(buf));
191 proc(&ctx, 0, 0, &d);
192 dstr_write(&d, stdout);
194 if (proc == hex_encode)
202 /*----- That's all, folks -------------------------------------------------*/