|
| 1 | +/* |
| 2 | + * This file is based on roken from the FreeBSD source. It has been modified |
| 3 | + * to not use malloc() and instead expect static buffers, and tabs have been |
| 4 | + * replaced with spaces. Also, instead of strlen() on the resulting string, |
| 5 | + * pointer arithmitic is done, as p represents the end of the buffer. |
| 6 | + */ |
| 7 | + |
| 8 | +/* |
| 9 | + * Copyright (c) 1995-2001 Kungliga Tekniska Högskolan |
| 10 | + * (Royal Institute of Technology, Stockholm, Sweden). |
| 11 | + * All rights reserved. |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without |
| 14 | + * modification, are permitted provided that the following conditions |
| 15 | + * are met: |
| 16 | + * |
| 17 | + * 1. Redistributions of source code must retain the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer. |
| 19 | + * |
| 20 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | + * notice, this list of conditions and the following disclaimer in the |
| 22 | + * documentation and/or other materials provided with the distribution. |
| 23 | + * |
| 24 | + * 3. Neither the name of the Institute nor the names of its contributors |
| 25 | + * may be used to endorse or promote products derived from this software |
| 26 | + * without specific prior written permission. |
| 27 | + * |
| 28 | + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 29 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 32 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 33 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 34 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 35 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 36 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 37 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 38 | + * SUCH DAMAGE. |
| 39 | + */ |
| 40 | + |
| 41 | +#include <stdlib.h> |
| 42 | +#include <string.h> |
| 43 | + |
| 44 | +#include <stdio.h> |
| 45 | + |
| 46 | +#include <base64/base64.h> |
| 47 | + |
| 48 | +static const char base64_chars[] = |
| 49 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 50 | + |
| 51 | +static int |
| 52 | +pos(char c) |
| 53 | +{ |
| 54 | + const char *p; |
| 55 | + for (p = base64_chars; *p; p++) |
| 56 | + if (*p == c) |
| 57 | + return p - base64_chars; |
| 58 | + return -1; |
| 59 | +} |
| 60 | + |
| 61 | +int |
| 62 | +base64_encode(const void *data, int size, char *s, uint8_t should_pad) |
| 63 | +{ |
| 64 | + char *p; |
| 65 | + int i; |
| 66 | + int c; |
| 67 | + const unsigned char *q; |
| 68 | + char *last; |
| 69 | + int diff; |
| 70 | + |
| 71 | + p = s; |
| 72 | + |
| 73 | + q = (const unsigned char *) data; |
| 74 | + last = NULL; |
| 75 | + i = 0; |
| 76 | + while (i < size) { |
| 77 | + c = q[i++]; |
| 78 | + c *= 256; |
| 79 | + if (i < size) |
| 80 | + c += q[i]; |
| 81 | + i++; |
| 82 | + c *= 256; |
| 83 | + if (i < size) |
| 84 | + c += q[i]; |
| 85 | + i++; |
| 86 | + p[0] = base64_chars[(c & 0x00fc0000) >> 18]; |
| 87 | + p[1] = base64_chars[(c & 0x0003f000) >> 12]; |
| 88 | + p[2] = base64_chars[(c & 0x00000fc0) >> 6]; |
| 89 | + p[3] = base64_chars[(c & 0x0000003f) >> 0]; |
| 90 | + last = p; |
| 91 | + p += 4; |
| 92 | + } |
| 93 | + |
| 94 | + if (last) { |
| 95 | + diff = i - size; |
| 96 | + if (diff > 0) { |
| 97 | + if (should_pad) { |
| 98 | + memset(last + (4 - diff), '=', diff); |
| 99 | + } else { |
| 100 | + p = last + (4 - diff); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + *p = 0; |
| 106 | + |
| 107 | + return (p - s); |
| 108 | +} |
| 109 | + |
| 110 | +int |
| 111 | +base64_pad(char *buf, int len) |
| 112 | +{ |
| 113 | + int remainder; |
| 114 | + |
| 115 | + remainder = len % 4; |
| 116 | + if (remainder == 0) { |
| 117 | + return (0); |
| 118 | + } |
| 119 | + |
| 120 | + memset(buf, '=', 4 - remainder); |
| 121 | + |
| 122 | + return (4 - remainder); |
| 123 | +} |
| 124 | + |
| 125 | +#define DECODE_ERROR -1 |
| 126 | + |
| 127 | +static unsigned int |
| 128 | +token_decode(const char *token) |
| 129 | +{ |
| 130 | + int i; |
| 131 | + unsigned int val = 0; |
| 132 | + int marker = 0; |
| 133 | + if (strlen(token) < 4) |
| 134 | + return DECODE_ERROR; |
| 135 | + for (i = 0; i < 4; i++) { |
| 136 | + val *= 64; |
| 137 | + if (token[i] == '=') |
| 138 | + marker++; |
| 139 | + else if (marker > 0) |
| 140 | + return DECODE_ERROR; |
| 141 | + else |
| 142 | + val += pos(token[i]); |
| 143 | + } |
| 144 | + if (marker > 2) |
| 145 | + return DECODE_ERROR; |
| 146 | + return (marker << 24) | val; |
| 147 | +} |
| 148 | + |
| 149 | +int |
| 150 | +base64_decode(const char *str, void *data) |
| 151 | +{ |
| 152 | + const char *p; |
| 153 | + unsigned char *q; |
| 154 | + |
| 155 | + q = data; |
| 156 | + for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) { |
| 157 | + unsigned int val = token_decode(p); |
| 158 | + unsigned int marker = (val >> 24) & 0xff; |
| 159 | + if (val == DECODE_ERROR) |
| 160 | + return -1; |
| 161 | + *q++ = (val >> 16) & 0xff; |
| 162 | + if (marker < 2) |
| 163 | + *q++ = (val >> 8) & 0xff; |
| 164 | + if (marker < 1) |
| 165 | + *q++ = val & 0xff; |
| 166 | + } |
| 167 | + return q - (unsigned char *) data; |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +int |
| 172 | +base64_decode_len(const char *str) |
| 173 | +{ |
| 174 | + int len; |
| 175 | + |
| 176 | + len = strlen(str); |
| 177 | + while (len && str[len - 1] == '=') { |
| 178 | + len--; |
| 179 | + } |
| 180 | + return len * 3 / 4; |
| 181 | +} |
0 commit comments