aboutsummaryrefslogtreecommitdiff
path: root/libssh/base64.c
blob: 849c2ca5144446b490d6215e759c153951178ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* base64 contains the needed support for base64 alphabet system, */
/* as described in RFC1521 */
/*
Copyright 2003,04 Aris Adamantiadis

This file is part of the SSH Library

The SSH Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The SSH Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the SSH Library; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

/* just the dirtiest part of code i ever made */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "libssh/priv.h"
static char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                        "abcdefghijklmnopqrstuvwxyz"
                        "0123456789+/" ;

/* transformations */
#define SET_A(n,i) do { n |= (i&63) <<18; } while (0)
#define SET_B(n,i) do { n |= (i&63) <<12; } while (0)
#define SET_C(n,i) do { n |= (i&63) << 6; } while (0)
#define SET_D(n,i) do { n |= (i&63); } while (0)

#define GET_A(n) ((n & 0xff0000) >> 16)
#define GET_B(n) ((n & 0xff00) >> 8)
#define GET_C(n) (n & 0xff)

static int _base64_to_bin(unsigned char dest[3], char *source,int num);
static int get_equals(char *string);

/* first part : base 64 to binary */

/* base64_to_bin translates a base64 string into a binary one. important, if something went wrong (ie incorrect char)*/
/* it returns NULL */
BUFFER *base64_to_bin(char *source){
    int len;
    int equals;
    BUFFER *buffer=buffer_new();
    unsigned char block[3];

    /* get the number of equals signs, which mirrors the padding */
    equals=get_equals(source);
    if(equals>2){
        buffer_free(buffer);
        return NULL;
    }

    len=strlen(source);
    while(len>4){
        if(_base64_to_bin(block,source,3)){
            buffer_free(buffer);
            return NULL;
        }
        buffer_add_data(buffer,block,3);
        len-=4;
        source+=4;
    }
    /* depending of the number of bytes resting, there are 3 possibilities (from the rfc) */
    switch(len){
/* (1) the final quantum of encoding input is an integral
   multiple of 24 bits; here, the final unit of encoded output will be
   an integral multiple of 4 characters with no "=" padding */
        case 4:
            if(equals!=0){
                buffer_free(buffer);
                return NULL;
            }
            if(_base64_to_bin(block,source,3)){
                buffer_free(buffer);
                return NULL;
            }
            buffer_add_data(buffer,block,3);
            return buffer;
/*(2) the final quantum of encoding input is exactly 8 bits; here, the final
   unit of encoded output will be two characters followed by two "="
   padding characters */
        case 2:
            if(equals!=2){
                buffer_free(buffer);
                return NULL;
            }
            if(_base64_to_bin(block,source,1)){
                buffer_free(buffer);
                return NULL;
            }
            buffer_add_data(buffer,block,1);
            return buffer;
/* the final quantum of encoding input is
   exactly 16 bits; here, the final unit of encoded output will be three
   characters followed by one "=" padding character */
        case 3:
            if(equals!=1){
                buffer_free(buffer);
                return NULL;
            }
            if(_base64_to_bin(block,source,2)){
                buffer_free(buffer);
                return NULL;
            }
            buffer_add_data(buffer,block,2);
            return buffer;
        default:
            /* 4,3,2 are the only padding size allowed */
            buffer_free(buffer);
            return NULL;
   }
   return NULL;
}
    
#define BLOCK(letter,n) do { ptr=strchr(alphabet,source[n]);\
                                        if(!ptr) return -1;\
                                        i=ptr-alphabet;\
                                        SET_##letter(*block,i);\
                                        } while(0)
/* returns 0 if ok, -1 if not (ie invalid char into the stuff) */
static int to_block4(unsigned long *block, char *source,int num){
    char *ptr;
    unsigned int i;
    *block=0;
    if(num<1)
        return 0;
    BLOCK(A,0); /* 6 bits */
    BLOCK(B,1); /* 12 */
    if(num<2)
        return 0;
    BLOCK(C,2); /* 18 */
    if(num < 3)
        return 0;
    BLOCK(D,3); /* 24 */
    return 0;
}

/* num = numbers of final bytes to be decoded */ 
static int _base64_to_bin(unsigned char dest[3], char *source,int num){
   unsigned long block;
   if(to_block4(&block,source,num))
       return -1;
   dest[0]=GET_A(block);
   dest[1]=GET_B(block);
   dest[2]=GET_C(block);
   return 0;
}

/* counts the number of "=" signs, and replace them by zeroes */
static int get_equals(char *string){
    char *ptr=string;
    int num=0;
    while((ptr=strchr(ptr,'='))){
        num++;
        *ptr=0;
        ptr++;
    }
    
    return num;
}

/* thanks sysk for debugging my mess :) */
#define BITS(n) ((1<<n)-1)
static void _bin_to_base64(unsigned char *dest, unsigned char source[3], int len){
    switch (len){
        case 1:
            dest[0]=alphabet[(source[0]>>2)];
            dest[1]=alphabet[((source[0] & BITS(2)) << 4)];
            dest[2]='=';
            dest[3]='=';
            break;
        case 2:
            dest[0]=alphabet[source[0]>>2];
            dest[1]=alphabet[(source[1]>>4) | ((source[0] & BITS(2)) << 4)];
            dest[2]=alphabet[(source[1]&BITS(4)) << 2]; 
            dest[3]='=';
            break;
        case 3:
            dest[0]=alphabet[(source[0]>>2)]; 
            dest[1]=alphabet[(source[1]>>4) | ((source[0] & BITS(2)) << 4)];
            dest[2]=alphabet[ (source[2] >> 6) | (source[1]&BITS(4)) << 2]; 
            dest[3]=alphabet[source[2]&BITS(6)];
        break;
    }
}

char *bin_to_base64(unsigned char *source, int len){
    int flen=len + (3 - (len %3)); /* round to upper 3 multiple */
    char *buffer;
    char *ptr;
    flen=(4 * flen)/3 + 1 ;
    ptr=buffer=malloc(flen);
    while(len>0){
        _bin_to_base64(ptr,source,len>3?3:len);
        ptr+=4;
        source +=3;
        len -=3;
    }
    ptr[0]=0;
    return buffer;
}