]> git.cryptolib.org Git - avr-crypto-lib.git/blob - base64/base64_dec.c
5e22d566c0f1a864967a3556c4de6e65ec1f4f76
[avr-crypto-lib.git] / base64 / base64_dec.c
1 /* base64_dec.c */
2 /*
3  *   This file is part of the AVR-Crypto-Lib.
4  *   Copyright (C) 2006, 2007, 2008  Daniel Otte (daniel.otte@rub.de)
5  *
6  *   This program is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * base64 decoder (RFC3548)
22  * Author: Daniel Otte
23  * License: GPLv3
24  * 
25  * 
26  */
27
28 #include <stdint.h>
29 #include "base64_dec.h"
30
31 #include "cli.h"
32
33 /*
34  #define USE_GCC_EXTENSION
35  */
36 #if 1
37
38 #ifdef USE_GCC_EXTENSION
39
40 static
41 int ascii2bit6(char a) {
42     switch(a) {
43         case 'A'...'Z':
44         return a-'A';
45         case 'a'...'z':
46         return a-'a'+26;
47         case '0'...'9':
48         return a-'0'+52;
49         case '+':
50         case '-':
51         return 62;
52         case '/':
53         case '_':
54         return 63;
55         default:
56         return -1;
57     }
58 }
59
60 #else
61
62 static uint8_t ascii2bit6(char a)
63 {
64     int r;
65     switch (a >> 4) {
66     case 0x5:
67         case 0x4:
68         r = a - 'A';
69         if (r < 0 || r > 25) {
70             return -1;
71         } else {
72             return r;
73         }
74     case 0x7:
75         case 0x6:
76         r = a - 'a';
77         if (r < 0 || r > 25) {
78             return -1;
79         } else {
80             return r + 26;
81         }
82         break;
83     case 0x3:
84         if (a > '9')
85             return -1;
86         return a - '0' + 52;
87     default:
88         break;
89     }
90     switch (a) {
91     case '+':
92         case '-':
93         return 62;
94     case '/':
95         case '_':
96         return 63;
97     default:
98         return 0xff;
99     }
100 }
101
102 #endif
103
104 #else
105
106 static
107 uint8_t ascii2bit6(uint8_t a) {
108     if(a>='A' && a<='Z') {
109         return a-'A';
110     } else {
111         if(a>='a' && a<= 'z') {
112             return a-'a'+26;
113         } else {
114             if(a>='0' && a<='9') {
115                 return a-'0'+52;
116             } else {
117                 if(a=='+' || a=='-') {
118                     return 62;
119                 } else {
120                     if(a=='/' || a=='_') {
121                         return 63;
122                     } else {
123                         return 0xff;
124                     }
125                 }
126             }
127         }
128     }
129 }
130
131 #endif
132
133 int base64_binlength(char *str, uint8_t strict)
134 {
135     int l = 0;
136     uint8_t term = 0;
137     for (;;) {
138         if (*str == '\0')
139             break;
140         if (*str == '\n' || *str == '\r') {
141             str++;
142             continue;
143         }
144         if (*str == '=') {
145             term++;
146             str++;
147             if (term == 2) {
148                 break;
149             }
150             continue;
151         }
152         if (term)
153             return -1;
154         if (ascii2bit6(*str) == -1) {
155             if (strict)
156                 return -1;
157         } else {
158             l++;
159         }
160         str++;
161     }
162     switch (term) {
163     case 0:
164         if (l % 4 != 0)
165             return -1;
166         return l / 4 * 3;
167     case 1:
168         if (l % 4 != 3)
169             return -1;
170         return (l + 1) / 4 * 3 - 1;
171     case 2:
172         if (l % 4 != 2)
173             return -1;
174         return (l + 2) / 4 * 3 - 2;
175     default:
176         return -1;
177     }
178 }
179
180 /*
181  |543210543210543210543210|
182  |765432107654321076543210|
183
184  .      .      .     .
185  |54321054|32105432|10543210|
186  |76543210|76543210|76543210|
187
188  */
189
190 int base64dec(void *dest, const char *b64str, uint8_t strict)
191 {
192     uint8_t buffer[4];
193     uint8_t idx = 0;
194     uint8_t term = 0;
195     for (;;) {
196 //              cli_putstr_P(PSTR("\r\n  DBG: got 0x"));
197 //              cli_hexdump(b64str, 1);
198         buffer[idx] = ascii2bit6(*b64str);
199 //              cli_putstr_P(PSTR(" --> 0x"));
200 //              cli_hexdump(buffer+idx, 1);
201
202         if (buffer[idx] == 0xFF) {
203             if (*b64str == '=') {
204                 term++;
205                 b64str++;
206                 if (term == 2)
207                     goto finalize;
208                 /* definitly the end */
209             } else {
210                 if (*b64str == '\0') {
211                     goto finalize;
212                     /* definitly the end */
213                 } else {
214                     if (*b64str == '\r' || *b64str == '\n' || !(strict)) {
215                         b64str++; /* charcters that we simply ignore */
216                     } else {
217                         return -1;
218                     }
219                 }
220             }
221         } else {
222             if (term)
223                 return -1; /* this happens if we get a '=' in the stream */
224             idx++;
225             b64str++;
226         }
227         if (idx == 4) {
228             ((uint8_t*) dest)[0] = buffer[0] << 2 | buffer[1] >> 4;
229             ((uint8_t*) dest)[1] = buffer[1] << 4 | buffer[2] >> 2;
230             ((uint8_t*) dest)[2] = buffer[2] << 6 | buffer[3];
231             dest = (uint8_t*) dest + 3;
232             idx = 0;
233         }
234     }
235     finalize:
236     /* the final touch */
237     if (idx == 0)
238         return 0;
239     if (term == 1) {
240         ((uint8_t*) dest)[0] = buffer[0] << 2 | buffer[1] >> 4;
241         ((uint8_t*) dest)[1] = buffer[1] << 4 | buffer[2] >> 2;
242         return 0;
243     }
244     if (term == 2) {
245         ((uint8_t*) dest)[0] = buffer[0] << 2 | buffer[1] >> 4;
246         return 0;
247     }
248     return -1;
249 }