3 * This file is part of the ARM-Crypto-Lib.
4 * Copyright (C) 2006-2010 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"
34 #define USE_GCC_EXTENSION
37 #ifdef USE_GCC_EXTENSION
40 int ascii2bit6(char a){
62 uint8_t ascii2bit6(char a){
103 int base64_binlength(char* str, uint8_t strict){
109 if(*str=='\n' || *str=='\r'){
123 if(ascii2bit6(*str)==-1){
150 |543210543210543210543210|
151 |765432107654321076543210|
154 |54321054|32105432|10543210|
155 |76543210|76543210|76543210|
159 int base64dec(void* dest, const char* b64str, uint8_t strict){
164 buffer[idx]= ascii2bit6(*b64str);
166 if(buffer[idx]==0xFF){
171 goto finalize; /* definitly the end */
174 goto finalize; /* definitly the end */
176 if(*b64str == '\r' || *b64str == '\n' || !(strict)){
177 b64str++; /* charcters that we simply ignore */
185 return -1; /* this happens if we get a '=' in the stream */
190 ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;
191 ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2;
192 ((uint8_t*)dest)[2] = buffer[2]<<6 | buffer[3];
193 dest = (uint8_t*)dest +3;
198 /* the final touch */
202 ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;
203 ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2;
207 ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4;