3 * $Id: hex.c,v 1.3 2004/04/08 01:36:11 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 /*----- Header files ------------------------------------------------------*/
39 /*----- Important tables --------------------------------------------------*/
41 static const char encodemap[] = { "0123456789abcdef" };
43 static const signed char decodemap[] = {
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
47 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, -1, -1, -1, -1, -1, /* 3x */
48 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 4x */
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 5x */
50 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 6x */
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 7x */
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @hex_encode@ --- *
58 * Arguments: @hex_ctx *ctx@ = pointer to a context block
59 * @const void *p@ = pointer to a source buffer
60 * @size_t sz@ = size of the source buffer
61 * @dstr *d@ = pointer to destination string
65 * Use: Encodes a binary string in hex.
68 void hex_encode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
71 const unsigned char *src = p;
76 DPUTC(d, encodemap[(x >> 4) & 0xf]);
77 DPUTC(d, encodemap[(x >> 0) & 0xf]);
79 if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
80 dstr_puts(d, ctx->indent);
87 /* --- @hex_decode@ --- *
89 * Arguments: @hex_ctx *ctx@ = pointer to a context block
90 * @const void *p@ = pointer to a source buffer
91 * @size_t sz@ = size of the source buffer
92 * @dstr *d@ = pointer to destination string
96 * Use: Decodes a binary string in hex. Pass in a null source
97 * pointer when you thing you've finished.
100 void hex_decode(hex_ctx *ctx, const void *p, size_t sz, dstr *d)
103 unsigned long acc = ctx->acc;
104 unsigned qsz = ctx->qsz;
110 /* --- Get the next character and convert it --- */
113 if (ch >= 128 || ch < 0)
121 /* --- Bung it in the accumulator --- */
123 acc = (acc << 4) | ch;
126 /* --- Maybe write out a completed triplet --- */
129 DPUTC(d, acc & 0xff);
139 DPUTC(d, ctx->acc << 4);
143 /* --- @hex_init@ --- *
145 * Arguments: @hex_ctx *ctx@ = pointer to context block to initialize
149 * Use: Initializes a hex context properly.
152 void hex_init(hex_ctx *ctx)
161 /*----- Test driver code --------------------------------------------------*/
165 int main(int argc, char *argv[])
167 unsigned char buf[BUFSIZ];
170 void (*proc)(hex_ctx *, const void *, size_t, dstr *);
175 if (argc > 1 && strcmp(argv[1], "-d") == 0)
185 sz = fread(buf, 1, sizeof(buf), stdin);
187 proc(&ctx, buf, sz, &d);
188 dstr_write(&d, stdout);
191 } while (sz == sizeof(buf));
193 proc(&ctx, 0, 0, &d);
194 dstr_write(&d, stdout);
196 if (proc == hex_encode)
204 /*----- That's all, folks -------------------------------------------------*/