3 * This file is part of the AVR-Crypto-Lib.
4 * Copyright (C) 2006, 2007, 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
22 * base64 decoder (RFC3548)
30 #include "base64_dec.h"
32 #include "test_src/cli.h"
35 #define USE_GCC_EXTENSION
39 #ifdef USE_GCC_EXTENSION
42 int ascii2bit6(char a){
64 uint8_t ascii2bit6(char a){
108 uint8_t ascii2bit6(uint8_t a){
109 if(a>='A' && a<='Z'){
112 if(a>='a' && a<= 'z'){
115 if(a>='0' && a<='9'){
118 if(a=='+' || a=='-'){
121 if(a=='/' || a=='_'){
134 int base64_binlength(char* str, uint8_t strict){
140 if(*str=='\n' || *str=='\r'){
154 if(ascii2bit6(*str)==-1){
181 |543210543210543210543210|
182 |765432107654321076543210|
185 |54321054|32105432|10543210|
186 |76543210|76543210|76543210|
190 int base64dec(void* dest, const char* b64str, uint8_t strict){
195 // cli_putstr_P(PSTR("\r\n DBG: got 0x"));
196 // cli_hexdump(b64str, 1);
197 buffer[idx]= ascii2bit6(*b64str);
198 // cli_putstr_P(PSTR(" --> 0x"));
199 // cli_hexdump(buffer+idx, 1);
201 if(buffer[idx]==0xFF){
206 goto finalize; /* definitly the end */
209 goto finalize; /* definitly the end */
211 if(*b64str == '\r' || *b64str == '\n' || !(strict)){
212 b64str++; /* charcters that we simply ignore */
220 return -1; /* this happens if we get a '=' in the stream */
225 ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;
226 ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2;
227 ((uint8_t*)dest)[2] = buffer[2]<<6 | buffer[3];
228 dest = (uint8_t*)dest +3;
233 /* the final touch */
237 ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;
238 ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2;
242 ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;