David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ |
| 2 | /* vi: set expandtab shiftwidth=4 tabstop=4: */ |
| 3 | /** |
| 4 | * \file |
| 5 | * <PRE> |
| 6 | * MODP_B64 - High performance base64 encoder/decoder |
| 7 | * Version 1.3 -- 17-Mar-2006 |
| 8 | * http://modp.com/release/base64 |
| 9 | * |
| 10 | * Copyright © 2005, 2006 Nick Galbreath -- nickg [at] modp [dot] com |
| 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 are |
| 15 | * met: |
| 16 | * |
| 17 | * Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * |
| 20 | * 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 | * Neither the name of the modp.com nor the names of its |
| 25 | * contributors may be used to endorse or promote products derived from |
| 26 | * this software without specific prior written permission. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 29 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 30 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 31 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 32 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 34 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 36 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | * |
| 40 | * This is the standard "new" BSD license: |
| 41 | * http://www.opensource.org/licenses/bsd-license.php |
| 42 | * </PRE> |
| 43 | */ |
| 44 | |
| 45 | /* public header */ |
| 46 | #include "modp_b64.h" |
| 47 | |
| 48 | /* |
| 49 | * If you are ripping this out of the library, comment out the next |
| 50 | * line and uncomment the next lines as approrpiate |
| 51 | */ |
| 52 | //#include "config.h" |
| 53 | |
| 54 | /* if on motoral, sun, ibm; uncomment this */ |
| 55 | /* #define WORDS_BIGENDIAN 1 */ |
| 56 | /* else for Intel, Amd; uncomment this */ |
| 57 | /* #undef WORDS_BIGENDIAN */ |
| 58 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 59 | #include "modp_b64_data.h" |
| 60 | |
| 61 | #define BADCHAR 0x01FFFFFF |
| 62 | |
| 63 | /** |
| 64 | * you can control if we use padding by commenting out this |
| 65 | * next line. However, I highly recommend you use padding and not |
| 66 | * using it should only be for compatability with a 3rd party. |
| 67 | * Also, 'no padding' is not tested! |
| 68 | */ |
| 69 | #define DOPAD 1 |
| 70 | |
| 71 | /* |
| 72 | * if we aren't doing padding |
| 73 | * set the pad character to NULL |
| 74 | */ |
| 75 | #ifndef DOPAD |
| 76 | #undef CHARPAD |
| 77 | #define CHARPAD '\0' |
| 78 | #endif |
| 79 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 80 | size_t modp_b64_encode(char* dest, const char* str, size_t len) |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 81 | { |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 82 | size_t i = 0; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 83 | uint8_t* p = (uint8_t*) dest; |
| 84 | |
| 85 | /* unsigned here is important! */ |
| 86 | uint8_t t1, t2, t3; |
| 87 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 88 | if (len > 2) { |
| 89 | for (; i < len - 2; i += 3) { |
| 90 | t1 = str[i]; t2 = str[i+1]; t3 = str[i+2]; |
| 91 | *p++ = e0[t1]; |
| 92 | *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
| 93 | *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; |
| 94 | *p++ = e2[t3]; |
| 95 | } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | switch (len - i) { |
| 99 | case 0: |
| 100 | break; |
| 101 | case 1: |
| 102 | t1 = str[i]; |
| 103 | *p++ = e0[t1]; |
| 104 | *p++ = e1[(t1 & 0x03) << 4]; |
| 105 | *p++ = CHARPAD; |
| 106 | *p++ = CHARPAD; |
| 107 | break; |
| 108 | default: /* case 2 */ |
| 109 | t1 = str[i]; t2 = str[i+1]; |
| 110 | *p++ = e0[t1]; |
| 111 | *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
| 112 | *p++ = e2[(t2 & 0x0F) << 2]; |
| 113 | *p++ = CHARPAD; |
| 114 | } |
| 115 | |
| 116 | *p = '\0'; |
| 117 | return p - (uint8_t*)dest; |
| 118 | } |
| 119 | |
| 120 | #ifdef WORDS_BIGENDIAN /* BIG ENDIAN -- SUN / IBM / MOTOROLA */ |
| 121 | int modp_b64_decode(char* dest, const char* src, int len) |
| 122 | { |
| 123 | if (len == 0) return 0; |
| 124 | |
| 125 | #ifdef DOPAD |
| 126 | /* if padding is used, then the message must be at least |
| 127 | 4 chars and be a multiple of 4. |
| 128 | there can be at most 2 pad chars at the end */ |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 129 | if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 130 | if (src[len-1] == CHARPAD) { |
| 131 | len--; |
| 132 | if (src[len -1] == CHARPAD) { |
| 133 | len--; |
| 134 | } |
| 135 | } |
| 136 | #endif /* DOPAD */ |
| 137 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 138 | size_t i; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 139 | int leftover = len % 4; |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 140 | size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 141 | |
| 142 | uint8_t* p = (uint8_t*) dest; |
| 143 | uint32_t x = 0; |
| 144 | uint32_t* destInt = (uint32_t*) p; |
| 145 | uint32_t* srcInt = (uint32_t*) src; |
| 146 | uint32_t y = *srcInt++; |
| 147 | for (i = 0; i < chunks; ++i) { |
| 148 | x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] | |
| 149 | d2[y >> 8 & 0xff] | d3[y & 0xff]; |
| 150 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 151 | if (x >= BADCHAR) return MODP_B64_ERROR; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 152 | *destInt = x << 8; |
| 153 | p += 3; |
| 154 | destInt = (uint32_t*)p; |
| 155 | y = *srcInt++; |
| 156 | } |
| 157 | |
| 158 | switch (leftover) { |
| 159 | case 0: |
| 160 | x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] | |
| 161 | d2[y >> 8 & 0xff] | d3[y & 0xff]; |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 162 | if (x >= BADCHAR) return MODP_B64_ERROR; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 163 | *p++ = ((uint8_t*)&x)[1]; |
| 164 | *p++ = ((uint8_t*)&x)[2]; |
| 165 | *p = ((uint8_t*)&x)[3]; |
| 166 | return (chunks+1)*3; |
| 167 | case 1: |
| 168 | x = d3[y >> 24]; |
| 169 | *p = (uint8_t)x; |
| 170 | break; |
| 171 | case 2: |
| 172 | x = d3[y >> 24] *64 + d3[(y >> 16) & 0xff]; |
| 173 | *p = (uint8_t)(x >> 4); |
| 174 | break; |
| 175 | default: /* case 3 */ |
| 176 | x = (d3[y >> 24] *64 + d3[(y >> 16) & 0xff])*64 + |
| 177 | d3[(y >> 8) & 0xff]; |
| 178 | *p++ = (uint8_t) (x >> 10); |
| 179 | *p = (uint8_t) (x >> 2); |
| 180 | break; |
| 181 | } |
| 182 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 183 | if (x >= BADCHAR) return MODP_B64_ERROR; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 184 | return 3*chunks + (6*leftover)/8; |
| 185 | } |
| 186 | |
| 187 | #else /* LITTLE ENDIAN -- INTEL AND FRIENDS */ |
| 188 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 189 | size_t modp_b64_decode(char* dest, const char* src, size_t len) |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 190 | { |
| 191 | if (len == 0) return 0; |
| 192 | |
| 193 | #ifdef DOPAD |
| 194 | /* |
| 195 | * if padding is used, then the message must be at least |
| 196 | * 4 chars and be a multiple of 4 |
| 197 | */ |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 198 | if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR; /* error */ |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 199 | /* there can be at most 2 pad chars at the end */ |
| 200 | if (src[len-1] == CHARPAD) { |
| 201 | len--; |
| 202 | if (src[len -1] == CHARPAD) { |
| 203 | len--; |
| 204 | } |
| 205 | } |
| 206 | #endif |
| 207 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 208 | size_t i; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 209 | int leftover = len % 4; |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 210 | size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 211 | |
| 212 | uint8_t* p = (uint8_t*)dest; |
| 213 | uint32_t x = 0; |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 214 | const uint8_t* y = (uint8_t*)src; |
| 215 | for (i = 0; i < chunks; ++i, y += 4) { |
| 216 | x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]]; |
| 217 | if (x >= BADCHAR) return MODP_B64_ERROR; |
| 218 | *p++ = ((uint8_t*)(&x))[0]; |
| 219 | *p++ = ((uint8_t*)(&x))[1]; |
| 220 | *p++ = ((uint8_t*)(&x))[2]; |
| 221 | } |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 222 | |
| 223 | switch (leftover) { |
| 224 | case 0: |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 225 | x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]]; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 226 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 227 | if (x >= BADCHAR) return MODP_B64_ERROR; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 228 | *p++ = ((uint8_t*)(&x))[0]; |
| 229 | *p++ = ((uint8_t*)(&x))[1]; |
| 230 | *p = ((uint8_t*)(&x))[2]; |
| 231 | return (chunks+1)*3; |
| 232 | break; |
| 233 | case 1: /* with padding this is an impossible case */ |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 234 | x = d0[y[0]]; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 235 | *p = *((uint8_t*)(&x)); // i.e. first char/byte in int |
| 236 | break; |
| 237 | case 2: // * case 2, 1 output byte */ |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 238 | x = d0[y[0]] | d1[y[1]]; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 239 | *p = *((uint8_t*)(&x)); // i.e. first char |
| 240 | break; |
| 241 | default: /* case 3, 2 output bytes */ |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 242 | x = d0[y[0]] | d1[y[1]] | d2[y[2]]; /* 0x3c */ |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 243 | *p++ = ((uint8_t*)(&x))[0]; |
| 244 | *p = ((uint8_t*)(&x))[1]; |
| 245 | break; |
| 246 | } |
| 247 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 248 | if (x >= BADCHAR) return MODP_B64_ERROR; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 249 | |
| 250 | return 3*chunks + (6*leftover)/8; |
| 251 | } |
| 252 | |
| 253 | #endif /* if bigendian / else / endif */ |