[7ed3199] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Base64 handling functions. encode_real() is mostly based on the y64 en- * |
---|
| 5 | * coder from libyahoo2. Moving it to a new file because it's getting big. * |
---|
| 6 | * * |
---|
| 7 | * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
| 8 | * * |
---|
| 9 | * This program is free software; you can redistribute it and/or modify * |
---|
| 10 | * it under the terms of the GNU General Public License as published by * |
---|
| 11 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 12 | * (at your option) any later version. * |
---|
| 13 | * * |
---|
| 14 | * This program is distributed in the hope that it will be useful, * |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 17 | * GNU General Public License for more details. * |
---|
| 18 | * * |
---|
| 19 | * You should have received a copy of the GNU General Public License along * |
---|
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
| 22 | * * |
---|
| 23 | \***************************************************************************/ |
---|
| 24 | |
---|
| 25 | #include <glib.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | #include "base64.h" |
---|
| 28 | |
---|
| 29 | char *tobase64(const char *text) |
---|
| 30 | { |
---|
[3b6eadc] | 31 | return base64_encode((const unsigned char *)text, strlen(text)); |
---|
[7ed3199] | 32 | } |
---|
| 33 | |
---|
[3b6eadc] | 34 | char *base64_encode(const unsigned char *in, int len) |
---|
[7ed3199] | 35 | { |
---|
[b38d399] | 36 | return g_base64_encode(in, len); |
---|
[7ed3199] | 37 | } |
---|
| 38 | |
---|
| 39 | /* Just a simple wrapper, but usually not very convenient because of zero |
---|
| 40 | termination. */ |
---|
| 41 | char *frombase64(const char *in) |
---|
| 42 | { |
---|
| 43 | unsigned char *out; |
---|
| 44 | base64_decode(in, &out); |
---|
| 45 | return (char*) out; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | int base64_decode(const char *in, unsigned char **out) |
---|
| 49 | { |
---|
[b38d399] | 50 | gsize len; |
---|
| 51 | *out = g_base64_decode(in, &len); |
---|
[7ed3199] | 52 | |
---|
[b38d399] | 53 | /* Some silly functions expect it to be zero terminated */ |
---|
| 54 | *out = g_realloc(*out, len + 1); |
---|
| 55 | out[0][len] = 0; |
---|
| 56 | |
---|
| 57 | return len; |
---|
[7ed3199] | 58 | } |
---|