2
* base64.c - support for base64 alphabet system, described in RFC1521
4
* This file is part of the SSH Library
6
* Copyright (c) 2005-2005 by Aris Adamantiadis
8
* The SSH Library is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as published by
10
* the Free Software Foundation; either version 2.1 of the License, or (at your
11
* option) any later version.
13
* The SSH Library is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16
* License for more details.
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with the SSH Library; see the file COPYING. If not, write to
20
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24
/* just the dirtiest part of code i ever made */
29
#include "libssh/priv.h"
30
#include "libssh/buffer.h"
32
static char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33
"abcdefghijklmnopqrstuvwxyz"
37
#define SET_A(n, i) do { (n) |= ((i) & 63) <<18; } while (0)
38
#define SET_B(n, i) do { (n) |= ((i) & 63) <<12; } while (0)
39
#define SET_C(n, i) do { (n) |= ((i) & 63) << 6; } while (0)
40
#define SET_D(n, i) do { (n) |= ((i) & 63); } while (0)
42
#define GET_A(n) (unsigned char) (((n) & 0xff0000) >> 16)
43
#define GET_B(n) (unsigned char) (((n) & 0xff00) >> 8)
44
#define GET_C(n) (unsigned char) ((n) & 0xff)
46
static int _base64_to_bin(unsigned char dest[3], const char *source, int num);
47
static int get_equals(char *string);
49
/* First part: base64 to binary */
54
* @brief Translates a base64 string into a binary one.
56
* @returns A buffer containing the decoded string, NULL if something went
57
* wrong (e.g. incorrect char).
59
ssh_buffer base64_to_bin(const char *source) {
60
ssh_buffer buffer = NULL;
61
unsigned char block[3];
67
base64 = strdup(source);
73
/* Get the number of equals signs, which mirrors the padding */
74
equals = get_equals(ptr);
80
buffer = ssh_buffer_new();
88
if (_base64_to_bin(block, ptr, 3) < 0) {
91
if (buffer_add_data(buffer, block, 3) < 0) {
99
* Depending on the number of bytes resting, there are 3 possibilities
104
* (1) The final quantum of encoding input is an integral multiple of
105
* 24 bits. Here, the final unit of encoded output will be an integral
106
* multiple of 4 characters with no "=" padding
112
if (_base64_to_bin(block, ptr, 3) < 0) {
115
if (buffer_add_data(buffer, block, 3) < 0) {
122
* (2) The final quantum of encoding input is exactly 8 bits; here, the
123
* final unit of encoded output will be two characters followed by
124
* two "=" padding characters.
131
if (_base64_to_bin(block, ptr, 1) < 0) {
134
if (buffer_add_data(buffer, block, 1) < 0) {
141
* The final quantum of encoding input is exactly 16 bits. Here, the final
142
* unit of encoded output will be three characters followed by one "="
149
if (_base64_to_bin(block, ptr, 2) < 0) {
152
if (buffer_add_data(buffer,block,2) < 0) {
159
/* 4,3,2 are the only padding size allowed */
165
ssh_buffer_free(buffer);
169
#define BLOCK(letter, n) do {ptr = strchr(alphabet, source[n]); \
170
if(!ptr) return -1; \
171
i = ptr - alphabet; \
172
SET_##letter(*block, i); \
175
/* Returns 0 if ok, -1 if not (ie invalid char into the stuff) */
176
static int to_block4(unsigned long *block, const char *source, int num) {
185
BLOCK(A, 0); /* 6 bit */
186
BLOCK(B,1); /* 12 bit */
192
BLOCK(C, 2); /* 18 bit */
198
BLOCK(D, 3); /* 24 bit */
203
/* num = numbers of final bytes to be decoded */
204
static int _base64_to_bin(unsigned char dest[3], const char *source, int num) {
207
if (to_block4(&block, source, num) < 0) {
210
dest[0] = GET_A(block);
211
dest[1] = GET_B(block);
212
dest[2] = GET_C(block);
217
/* Count the number of "=" signs and replace them by zeroes */
218
static int get_equals(char *string) {
222
while ((ptr=strchr(ptr,'=')) != NULL) {
231
/* thanks sysk for debugging my mess :) */
232
#define BITS(n) ((1 << (n)) - 1)
233
static void _bin_to_base64(unsigned char *dest, const unsigned char source[3],
237
dest[0] = alphabet[(source[0] >> 2)];
238
dest[1] = alphabet[((source[0] & BITS(2)) << 4)];
243
dest[0] = alphabet[source[0] >> 2];
244
dest[1] = alphabet[(source[1] >> 4) | ((source[0] & BITS(2)) << 4)];
245
dest[2] = alphabet[(source[1] & BITS(4)) << 2];
249
dest[0] = alphabet[(source[0] >> 2)];
250
dest[1] = alphabet[(source[1] >> 4) | ((source[0] & BITS(2)) << 4)];
251
dest[2] = alphabet[ (source[2] >> 6) | (source[1] & BITS(4)) << 2];
252
dest[3] = alphabet[source[2] & BITS(6)];
260
* @brief Converts binary data to a base64 string.
262
* @returns the converted string
264
unsigned char *bin_to_base64(const unsigned char *source, int len) {
265
unsigned char *base64;
267
int flen = len + (3 - (len % 3)); /* round to upper 3 multiple */
268
flen = (4 * flen) / 3 + 1;
270
base64 = malloc(flen);
271
if (base64 == NULL) {
277
_bin_to_base64(ptr, source, len > 3 ? 3 : len);
287
/* vim: set ts=2 sw=2 et cindent: */