3 * $Id: hex.c,v 1.2 2003/05/16 00:22:58 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.2 2003/05/16 00:22:58 mdw
34 * Test base64 and hex encoding.
36 * Revision 1.1 2002/01/13 13:26:30 mdw
37 * New hex encoding stuff.
41 /*----- Header files ------------------------------------------------------*/
50 /*----- Important tables --------------------------------------------------*/
52 static const char encodemap[] = { "0123456789abcdef" };
54 static const signed char decodemap[] = {
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
58 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, -1, -1, -1, -1, -1, /* 3x */
59 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 4x */
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 5x */
61 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 6x */
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 7x */
65 /*----- Main code ---------------------------------------------------------*/
67 /* --- @hex_encode@ --- *
69 * Arguments: @hex_ctx *ctx@ = pointer to a context block
70 * @const void *p@ = pointer to a source buffer
71 * @size_t sz@ = size of the source buffer
72 * @dstr *d@ = pointer to destination string
76 * Use: Encodes a binary string in hex.
79 void hex_encode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
82 const unsigned char *src = p;
87 DPUTC(d, encodemap[(x >> 4) & 0xf]);
88 DPUTC(d, encodemap[(x >> 0) & 0xf]);
90 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
91 dstr_puts(d, ctx->indent);
98 /* --- @hex_decode@ --- *
100 * Arguments: @hex_ctx *ctx@ = pointer to a context block
101 * @const void *p@ = pointer to a source buffer
102 * @size_t sz@ = size of the source buffer
103 * @dstr *d@ = pointer to destination string
107 * Use: Decodes a binary string in hex. Pass in a null source
108 * pointer when you thing you've finished.
111 void hex_decode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
114 unsigned long acc = ctx->acc;
115 unsigned qsz = ctx->qsz;
121 /* --- Get the next character and convert it --- */
124 if (ch >= 128 || ch < 0)
132 /* --- Bung it in the accumulator --- */
134 acc = (acc << 4) | ch;
137 /* --- Maybe write out a completed triplet --- */
140 DPUTC(d, acc & 0xff);
150 DPUTC(d, ctx->acc << 4);
154 /* --- @hex_init@ --- *
156 * Arguments: @hex_ctx *ctx@ = pointer to context block to initialize
160 * Use: Initializes a hex context properly.
163 void hex_init(hex_ctx *ctx)
172 /*----- Test driver code --------------------------------------------------*/
176 int main(int argc, char *argv[])
178 unsigned char buf[BUFSIZ];
181 void (*proc)(hex_ctx *, const void *, size_t, dstr *);
186 if (argc > 1 && strcmp(argv[1], "-d") == 0)
196 sz = fread(buf, 1, sizeof(buf), stdin);
198 proc(&ctx, buf, sz, &d);
199 dstr_write(&d, stdout);
202 } while (sz == sizeof(buf));
204 proc(&ctx, 0, 0, &d);
205 dstr_write(&d, stdout);
207 if (proc == hex_encode)
215 /*----- That's all, folks -------------------------------------------------*/