3 * $Id: hex.c,v 1.1 2002/01/13 13:26:30 mdw Exp $
5 * Hexadecimal encoding and decoding.
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2002/01/13 13:26:30 mdw
34 * New hex encoding stuff.
38 /*----- Header files ------------------------------------------------------*/
47 /*----- Important tables --------------------------------------------------*/
49 static const char encodemap[] = { "0123456789abcdef" };
51 static const signed char decodemap[] = {
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
55 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, -1, -1, -1, -1, -1, /* 3x */
56 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 4x */
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 5x */
58 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 6x */
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 7x */
62 /*----- Main code ---------------------------------------------------------*/
64 /* --- @hex_encode@ --- *
66 * Arguments: @hex_ctx *ctx@ = pointer to a context block
67 * @const void *p@ = pointer to a source buffer
68 * @size_t sz@ = size of the source buffer
69 * @dstr *d@ = pointer to destination string
73 * Use: Encodes a binary string in hex.
76 void hex_encode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
79 const unsigned char *src = p;
84 DPUTC(d, encodemap[(x >> 4) & 0xf]);
85 DPUTC(d, encodemap[(x >> 0) & 0xf]);
87 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
88 dstr_puts(d, ctx->indent);
95 /* --- @hex_decode@ --- *
97 * Arguments: @hex_ctx *ctx@ = pointer to a context block
98 * @const void *p@ = pointer to a source buffer
99 * @size_t sz@ = size of the source buffer
100 * @dstr *d@ = pointer to destination string
104 * Use: Decodes a binary string in hex. Pass in a null source
105 * pointer when you thing you've finished.
108 void hex_decode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
111 unsigned long acc = ctx->acc;
112 unsigned qsz = ctx->qsz;
118 /* --- Get the next character and convert it --- */
121 if (ch >= 128 || ch < 0)
129 /* --- Bung it in the accumulator --- */
131 acc = (acc << 4) | ch;
134 /* --- Maybe write out a completed triplet --- */
137 DPUTC(d, acc & 0xff);
147 DPUTC(d, ctx->acc << 4);
151 /* --- @hex_init@ --- *
153 * Arguments: @hex_ctx *ctx@ = pointer to context block to initialize
157 * Use: Initializes a hex context properly.
160 void hex_init(hex_ctx *ctx)
169 /*----- Test driver code --------------------------------------------------*/
173 int main(int argc, char *argv[])
175 unsigned char buf[BUFSIZ];
178 void (*proc)(hex_ctx *, const unsigned char *, size_t, dstr *);
183 if (argc > 1 && strcmp(argv[1], "-d") == 0)
193 sz = fread(buf, 1, sizeof(buf), stdin);
195 proc(&ctx, buf, sz, &d);
196 dstr_write(&d, stdout);
199 } while (sz == sizeof(buf));
201 proc(&ctx, 0, 0, &d);
202 dstr_write(&d, stdout);
204 if (proc == hex_encode)
212 /*----- That's all, folks -------------------------------------------------*/