| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Base64 encoding and decoding. |
| 4 | * |
| 5 | * (c) 1997 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include "base64.h" |
| 35 | #include "dstr.h" |
| 36 | |
| 37 | /*----- Important tables --------------------------------------------------*/ |
| 38 | |
| 39 | static const char encodemap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 40 | "abcdefghijklmnopqrstuvwxyz" |
| 41 | "0123456789+/" }; |
| 42 | |
| 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, 62, -1, -1, -1, 63, /* 2x */ |
| 47 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 3x */ |
| 48 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */ |
| 49 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */ |
| 50 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ,37, 38, 39, 40, /* 6x */ |
| 51 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 7x */ |
| 52 | }; |
| 53 | |
| 54 | /*----- Main code ---------------------------------------------------------*/ |
| 55 | |
| 56 | /* --- @base64_encode@ --- * |
| 57 | * |
| 58 | * Arguments: @base64_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 |
| 62 | * |
| 63 | * Returns: --- |
| 64 | * |
| 65 | * Use: Encodes a binary string in base64. To flush out the final |
| 66 | * few characters (if necessary), pass a null source pointer. |
| 67 | */ |
| 68 | |
| 69 | void base64_encode(base64_ctx *ctx, |
| 70 | const void *p, size_t sz, |
| 71 | dstr *d) |
| 72 | { |
| 73 | if (p) { |
| 74 | unsigned long acc = ctx->acc; |
| 75 | unsigned qsz = ctx->qsz; |
| 76 | const unsigned char *src = p; |
| 77 | |
| 78 | while (sz) { |
| 79 | acc = (acc << 8) | *src++; |
| 80 | qsz++; |
| 81 | sz--; |
| 82 | if (qsz == 3) { |
| 83 | DPUTC(d, encodemap[(acc >> 18) & 0x3f]); |
| 84 | DPUTC(d, encodemap[(acc >> 12) & 0x3f]); |
| 85 | DPUTC(d, encodemap[(acc >> 6) & 0x3f]); |
| 86 | DPUTC(d, encodemap[(acc >> 0) & 0x3f]); |
| 87 | ctx->lnlen += 4; |
| 88 | if (ctx->maxline && ctx->lnlen >= ctx->maxline) { |
| 89 | dstr_puts(d, ctx->indent); |
| 90 | ctx->lnlen = 0; |
| 91 | } |
| 92 | qsz = 0; |
| 93 | acc = 0; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | ctx->acc = acc; |
| 98 | ctx->qsz = qsz; |
| 99 | } else { |
| 100 | unsigned long acc = ctx->acc; |
| 101 | unsigned qsz = ctx->qsz; |
| 102 | |
| 103 | switch (qsz) { |
| 104 | case 0: |
| 105 | break; |
| 106 | case 1: |
| 107 | acc <<= 16; |
| 108 | DPUTC(d, encodemap[(acc >> 18) & 0x3f]); |
| 109 | DPUTC(d, encodemap[(acc >> 12) & 0x3f]); |
| 110 | DPUTC(d, '='); |
| 111 | DPUTC(d, '='); |
| 112 | ctx->lnlen += 4; |
| 113 | break; |
| 114 | case 2: |
| 115 | acc <<= 8; |
| 116 | DPUTC(d, encodemap[(acc >> 18) & 0x3f]); |
| 117 | DPUTC(d, encodemap[(acc >> 12) & 0x3f]); |
| 118 | DPUTC(d, encodemap[(acc >> 6) & 0x3f]); |
| 119 | DPUTC(d, '='); |
| 120 | ctx->lnlen += 4; |
| 121 | break; |
| 122 | } |
| 123 | ctx->qsz = 0; |
| 124 | ctx->acc = 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* --- @base64_decode@ --- * |
| 129 | * |
| 130 | * Arguments: @base64_ctx *ctx@ = pointer to a context block |
| 131 | * @const void *p@ = pointer to a source buffer |
| 132 | * @size_t sz@ = size of the source buffer |
| 133 | * @dstr *d@ = pointer to destination string |
| 134 | * |
| 135 | * Returns: --- |
| 136 | * |
| 137 | * Use: Decodes a binary string in base64. To flush out the final |
| 138 | * few characters (if necessary), pass a null source pointer. |
| 139 | */ |
| 140 | |
| 141 | void base64_decode(base64_ctx *ctx, |
| 142 | const void *p, size_t sz, |
| 143 | dstr *d) |
| 144 | { |
| 145 | if (p) { |
| 146 | unsigned long acc = ctx->acc; |
| 147 | unsigned qsz = ctx->qsz; |
| 148 | const char *src = p; |
| 149 | int ch; |
| 150 | |
| 151 | while (sz) { |
| 152 | |
| 153 | /* --- Get the next character and convert it --- */ |
| 154 | |
| 155 | ch = *src++; |
| 156 | if (ch >= 128 || ch < 0) |
| 157 | ch = -1; |
| 158 | else |
| 159 | ch = decodemap[ch]; |
| 160 | sz--; |
| 161 | if (ch == -1) |
| 162 | continue; |
| 163 | |
| 164 | /* --- Bung it in the accumulator --- */ |
| 165 | |
| 166 | acc = (acc << 6) | ch; |
| 167 | qsz++; |
| 168 | |
| 169 | /* --- Maybe write out a completed triplet --- */ |
| 170 | |
| 171 | if (qsz == 4) { |
| 172 | DPUTC(d, (acc >> 16) & 0xff); |
| 173 | DPUTC(d, (acc >> 8) & 0xff); |
| 174 | DPUTC(d, (acc >> 0) & 0xff); |
| 175 | acc = 0; |
| 176 | qsz = 0; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | ctx->acc = acc; |
| 181 | ctx->qsz = qsz; |
| 182 | } else { |
| 183 | |
| 184 | /* --- Notes about the tail-end bits --- * |
| 185 | * |
| 186 | * Ending Base64 decoding is messy. The reference I'm using to define |
| 187 | * the encoding, RFC1521 section 5.2, is a little hazy on exactly what to |
| 188 | * do at the end. It explains that I'm meant to ignore spurious `=' |
| 189 | * characters, and points out that I'm not guaranteed to see anything |
| 190 | * interesting at the end. I'll play safe here, and ignore all `=' |
| 191 | * characters, relying on my client to work out when to stop feeding me |
| 192 | * data. I'll use the queue size to work out how many tail-end bytes |
| 193 | * I ought to write. |
| 194 | */ |
| 195 | |
| 196 | unsigned long acc = ctx->acc; |
| 197 | unsigned qsz = ctx->qsz; |
| 198 | |
| 199 | /* --- Now fiddle with everything else --- * |
| 200 | * |
| 201 | * There's a bodge here for invalid encodings which have only one hextet |
| 202 | * in the final group. I'm not sure this is really worth having, but it |
| 203 | * might save some unexpected behaviour. (Not that you won't still get |
| 204 | * unexpected behaviour if the stream is completely empty, of course.) |
| 205 | */ |
| 206 | |
| 207 | if (qsz) { |
| 208 | acc <<= 6 * (4 - qsz); |
| 209 | qsz *= 6; |
| 210 | if (qsz < 8) |
| 211 | qsz = 8; |
| 212 | while (qsz >= 8) { |
| 213 | DPUTC(d, (acc >> 16) & 0xff); |
| 214 | acc <<= 8; |
| 215 | qsz -= 8; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* --- That seems to be good enough --- */ |
| 220 | |
| 221 | ctx->qsz = 0; |
| 222 | ctx->acc = 0; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* --- @base64_init@ --- * |
| 227 | * |
| 228 | * Arguments: @base64_ctx *ctx@ = pointer to context block to initialize |
| 229 | * |
| 230 | * Returns: --- |
| 231 | * |
| 232 | * Use: Initializes a base64 context properly. |
| 233 | */ |
| 234 | |
| 235 | void base64_init(base64_ctx *ctx) |
| 236 | { |
| 237 | ctx->acc = 0; |
| 238 | ctx->qsz = 0; |
| 239 | ctx->lnlen = 0; |
| 240 | ctx->indent = "\n"; |
| 241 | ctx->maxline = 72; |
| 242 | } |
| 243 | |
| 244 | /*----- Test driver code --------------------------------------------------*/ |
| 245 | |
| 246 | #ifdef TEST_RIG |
| 247 | |
| 248 | int main(int argc, char *argv[]) |
| 249 | { |
| 250 | unsigned char buf[BUFSIZ]; |
| 251 | dstr d = DSTR_INIT; |
| 252 | base64_ctx ctx; |
| 253 | void (*proc)(base64_ctx *, const void *, size_t, dstr *); |
| 254 | size_t sz; |
| 255 | |
| 256 | base64_init(&ctx); |
| 257 | |
| 258 | if (argc > 1 && strcmp(argv[1], "-d") == 0) |
| 259 | proc = base64_decode; |
| 260 | else { |
| 261 | proc = base64_encode; |
| 262 | putchar('\t'); |
| 263 | ctx.indent = "\n\t"; |
| 264 | ctx.maxline = 64; |
| 265 | } |
| 266 | |
| 267 | do { |
| 268 | sz = fread(buf, 1, sizeof(buf), stdin); |
| 269 | if (sz) { |
| 270 | proc(&ctx, buf, sz, &d); |
| 271 | dstr_write(&d, stdout); |
| 272 | dstr_destroy(&d); |
| 273 | } |
| 274 | } while (sz == sizeof(buf)); |
| 275 | |
| 276 | proc(&ctx, 0, 0, &d); |
| 277 | dstr_write(&d, stdout); |
| 278 | |
| 279 | if (proc == base64_encode) |
| 280 | putchar('\n'); |
| 281 | |
| 282 | return (0); |
| 283 | } |
| 284 | |
| 285 | #endif |
| 286 | |
| 287 | /*----- That's all, folks -------------------------------------------------*/ |