1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3
/* Copyright (C) 2001-2004 Novell, Inc.
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of version 2 of the GNU Lesser General Public
7
* License as published by the Free Software Foundation.
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 GNU
12
* General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
24
#include "e2k-encoding-utils.h"
28
static const char *b64_alphabet =
29
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34
* @len: the length of @data
36
* Base64-encodes @len bytes of data at @data.
38
* Return value: the base64-encoded representation of @data, which
39
* the caller must free.
42
e2k_base64_encode (const guint8 *data, int len)
46
p = buf = g_malloc (((len + 2) / 3) * 4 + 1);
48
p[0] = b64_alphabet[ (data[0] >> 2) & 0x3f];
49
p[1] = b64_alphabet[((data[0] << 4) & 0x30) +
50
((data[1] >> 4) & 0x0f)];
51
p[2] = b64_alphabet[((data[1] << 2) & 0x3c) +
52
((data[2] >> 6) & 0x03)];
53
p[3] = b64_alphabet[ data[2] & 0x3f];
62
p[0] = b64_alphabet[ (data[0] >> 2) & 0x3f];
63
p[1] = b64_alphabet[((data[0] << 4) & 0x30) +
64
((data[1] >> 4) & 0xf)];
65
p[2] = b64_alphabet[ (data[1] << 2) & 0x3c];
70
p[0] = b64_alphabet[ (data[0] >> 2) & 0x3f];
71
p[1] = b64_alphabet[ (data[0] << 4) & 0x30];
82
static guint8 base64_unphabet[] = {
83
-1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1,
84
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
85
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
86
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
87
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
88
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
89
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
90
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
92
#define E2K_B64_SPACE ((guint8)-2)
93
#define E2K_B64_BAD ((guint8)-1)
97
* @string: base64-encoded data
99
* Decodes the base64-encoded data in @string.
101
* Return value: the binary data encoded in @string
104
e2k_base64_decode (const char *string)
107
int bits, length, qw = 0;
110
rc = g_byte_array_new ();
112
length = strlen (string);
116
g_byte_array_set_size (rc, ((length / 4) + 1) * 3);
119
for (; *string; string++) {
120
if ((unsigned char)*string > 127)
122
bits = base64_unphabet[(unsigned char)*string];
123
if (bits == E2K_B64_BAD)
125
else if (bits == E2K_B64_SPACE)
130
data[0] = (bits << 2) & 0xfc;
133
data[0] |= (bits >> 4) & 0x03;
134
data[1] = (bits << 4) & 0xf0;
137
data[1] |= (bits >> 2) & 0x0f;
138
data[2] = (bits << 6) & 0xc0;
141
data[2] |= bits & 0x3f;
148
rc->len = data - rc->data;