|
| 1 | +/* tinyproxy - A fast light-weight HTTP proxy |
| 2 | + * this file Copyright (C) 2016-2018 rofl0r |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along |
| 15 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "base64.h" |
| 20 | + |
| 21 | +static const char base64_tbl[64] = |
| 22 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 23 | + |
| 24 | +/* |
| 25 | + rofl0r's base64 impl (taken from libulz) |
| 26 | + takes count bytes from src, writing base64 encoded string into dst. |
| 27 | + dst needs to be at least BASE64ENC_BYTES(count) + 1 bytes in size. |
| 28 | + the string in dst will be zero-terminated. |
| 29 | + */ |
| 30 | +void base64enc(char *dst, const void* src, size_t count) |
| 31 | +{ |
| 32 | + unsigned const char *s = src; |
| 33 | + char* d = dst; |
| 34 | + while(count) { |
| 35 | + int i = 0, n = *s << 16; |
| 36 | + s++; |
| 37 | + count--; |
| 38 | + if(count) { |
| 39 | + n |= *s << 8; |
| 40 | + s++; |
| 41 | + count--; |
| 42 | + i++; |
| 43 | + } |
| 44 | + if(count) { |
| 45 | + n |= *s; |
| 46 | + s++; |
| 47 | + count--; |
| 48 | + i++; |
| 49 | + } |
| 50 | + *d++ = base64_tbl[(n >> 18) & 0x3f]; |
| 51 | + *d++ = base64_tbl[(n >> 12) & 0x3f]; |
| 52 | + *d++ = i ? base64_tbl[(n >> 6) & 0x3f] : '='; |
| 53 | + *d++ = i == 2 ? base64_tbl[n & 0x3f] : '='; |
| 54 | + } |
| 55 | + *d = 0; |
| 56 | +} |
| 57 | + |
0 commit comments