ee393574 |
1 | /* -*-c-*- |
ee393574 |
2 | * |
3 | * Hexadecimal encoding and decoding. |
4 | * |
5 | * (c) 2001 Straylight/Edgeware |
6 | */ |
7 | |
d4efbcd9 |
8 | /*----- Licensing notice --------------------------------------------------* |
ee393574 |
9 | * |
10 | * This file is part of the mLib utilities library. |
11 | * |
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. |
d4efbcd9 |
16 | * |
ee393574 |
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. |
d4efbcd9 |
21 | * |
ee393574 |
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, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
ee393574 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <stdio.h> |
31 | #include <stdlib.h> |
32 | #include <string.h> |
33 | |
34 | #include "dstr.h" |
35 | #include "hex.h" |
36 | |
37 | /*----- Important tables --------------------------------------------------*/ |
38 | |
39 | static const char encodemap[] = { "0123456789abcdef" }; |
40 | |
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 */ |
d4efbcd9 |
50 | }; |
ee393574 |
51 | |
52 | /*----- Main code ---------------------------------------------------------*/ |
53 | |
54 | /* --- @hex_encode@ --- * |
55 | * |
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 |
60 | * |
61 | * Returns: --- |
62 | * |
63 | * Use: Encodes a binary string in hex. |
64 | */ |
65 | |
66 | void hex_encode(hex_ctx *ctx, const void *p, size_t sz, dstr *d) |
67 | { |
68 | if (p) { |
69 | const unsigned char *src = p; |
70 | |
71 | while (sz) { |
72 | unsigned x = *src++; |
73 | sz--; |
74 | DPUTC(d, encodemap[(x >> 4) & 0xf]); |
75 | DPUTC(d, encodemap[(x >> 0) & 0xf]); |
76 | ctx->lnlen += 2; |
77 | if (ctx->maxline && ctx->lnlen >= ctx->maxline) { |
78 | dstr_puts(d, ctx->indent); |
79 | ctx->lnlen = 0; |
80 | } |
81 | } |
82 | } |
83 | } |
84 | |
85 | /* --- @hex_decode@ --- * |
86 | * |
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 |
91 | * |
92 | * Returns: --- |
93 | * |
94 | * Use: Decodes a binary string in hex. Pass in a null source |
95 | * pointer when you thing you've finished. |
96 | */ |
97 | |
98 | void hex_decode(hex_ctx *ctx, const void *p, size_t sz, dstr *d) |
99 | { |
100 | if (p) { |
101 | unsigned long acc = ctx->acc; |
102 | unsigned qsz = ctx->qsz; |
103 | const char *src = p; |
104 | int ch; |
105 | |
106 | while (sz) { |
107 | |
108 | /* --- Get the next character and convert it --- */ |
109 | |
110 | ch = *src++; |
111 | if (ch >= 128 || ch < 0) |
112 | ch = -1; |
113 | else |
114 | ch = decodemap[ch]; |
115 | sz--; |
116 | if (ch == -1) |
117 | continue; |
118 | |
119 | /* --- Bung it in the accumulator --- */ |
120 | |
121 | acc = (acc << 4) | ch; |
122 | qsz++; |
123 | |
124 | /* --- Maybe write out a completed triplet --- */ |
125 | |
126 | if (qsz == 2) { |
127 | DPUTC(d, acc & 0xff); |
128 | acc = 0; |
129 | qsz = 0; |
130 | } |
131 | } |
132 | |
133 | ctx->acc = acc; |
134 | ctx->qsz = qsz; |
135 | } else { |
136 | if (ctx->qsz) |
137 | DPUTC(d, ctx->acc << 4); |
138 | } |
139 | } |
140 | |
141 | /* --- @hex_init@ --- * |
142 | * |
143 | * Arguments: @hex_ctx *ctx@ = pointer to context block to initialize |
144 | * |
145 | * Returns: --- |
146 | * |
147 | * Use: Initializes a hex context properly. |
148 | */ |
149 | |
150 | void hex_init(hex_ctx *ctx) |
151 | { |
152 | ctx->acc = 0; |
153 | ctx->qsz = 0; |
154 | ctx->lnlen = 0; |
155 | ctx->indent = "\n"; |
156 | ctx->maxline = 72; |
157 | } |
158 | |
159 | /*----- Test driver code --------------------------------------------------*/ |
160 | |
161 | #ifdef TEST_RIG |
162 | |
163 | int main(int argc, char *argv[]) |
164 | { |
165 | unsigned char buf[BUFSIZ]; |
166 | dstr d = DSTR_INIT; |
167 | hex_ctx ctx; |
d5679ed0 |
168 | void (*proc)(hex_ctx *, const void *, size_t, dstr *); |
ee393574 |
169 | size_t sz; |
170 | |
171 | hex_init(&ctx); |
172 | |
173 | if (argc > 1 && strcmp(argv[1], "-d") == 0) |
174 | proc = hex_decode; |
175 | else { |
176 | proc = hex_encode; |
177 | putchar('\t'); |
178 | ctx.indent = "\n\t"; |
179 | ctx.maxline = 64; |
180 | } |
181 | |
182 | do { |
183 | sz = fread(buf, 1, sizeof(buf), stdin); |
184 | if (sz) { |
185 | proc(&ctx, buf, sz, &d); |
186 | dstr_write(&d, stdout); |
187 | dstr_destroy(&d); |
188 | } |
189 | } while (sz == sizeof(buf)); |
190 | |
191 | proc(&ctx, 0, 0, &d); |
192 | dstr_write(&d, stdout); |
193 | |
194 | if (proc == hex_encode) |
195 | putchar('\n'); |
196 | |
197 | return (0); |
198 | } |
199 | |
200 | #endif |
201 | |
202 | /*----- That's all, folks -------------------------------------------------*/ |