]> git.cryptolib.org Git - arm-crypto-lib.git/blob - base64/base64_enc.c
more precise type for arguments of bcal_nessie_multiple()
[arm-crypto-lib.git] / base64 / base64_enc.c
1 /* base64_enc.c */
2 /*
3  *   This file is part of the ARM-Crypto-Lib.
4  *   Copyright (C) 2006-2010  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 /**
22  * base64 encoder (RFC3548)
23  * Author: Daniel Otte
24  * License: GPLv3
25  * 
26  * 
27  */
28
29 #include <stdint.h>
30 #include "base64_enc.h"
31
32 const char base64_alphabet[64] = {
33         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
34         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 
35         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
36         'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 
37         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
38         'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
39         'w', 'x', 'y', 'z', '0', '1', '2', '3', 
40         '4', '5', '6', '7', '8', '9', '+', '/' }; 
41
42
43 static 
44 char bit6toAscii(uint8_t a){
45         a &= (uint8_t)0x3F;
46         return base64_alphabet[a];
47 }
48
49 void base64enc(char* dest,const void* src, uint16_t length){
50         uint16_t i,j;
51         uint8_t a[4];
52         for(i=0; i<length/3; ++i){
53                 a[0]= (((uint8_t*)src)[i*3+0])>>2;
54                 a[1]= (((((uint8_t*)src)[i*3+0])<<4) | ((((uint8_t*)src)[i*3+1])>>4)) & 0x3F;
55                 a[2]= (((((uint8_t*)src)[i*3+1])<<2) | ((((uint8_t*)src)[i*3+2])>>6)) & 0x3F;
56                 a[3]= (((uint8_t*)src)[i*3+2]) & 0x3F;
57                 for(j=0; j<4; ++j){
58                         *dest++=bit6toAscii(a[j]);
59                 }
60         }
61         /* now we do the rest */
62         switch(length%3){
63                 case 0: 
64                         break;
65                 case 1:
66                         a[0]=(((uint8_t*)src)[i*3+0])>>2;
67                         a[1]=((((uint8_t*)src)[i*3+0])<<4)&0x3F;
68                         *dest++ = bit6toAscii(a[0]);
69                         *dest++ = bit6toAscii(a[1]);
70                         *dest++ = '=';
71                         *dest++ = '=';
72                         break;
73                 case 2:         
74                         a[0]= (((uint8_t*)src)[i*3+0])>>2;
75                         a[1]= (((((uint8_t*)src)[i*3+0])<<4) | ((((uint8_t*)src)[i*3+1])>>4)) & 0x3F;
76                         a[2]= ((((uint8_t*)src)[i*3+1])<<2) & 0x3F;
77                         *dest++ = bit6toAscii(a[0]);
78                         *dest++ = bit6toAscii(a[1]);
79                         *dest++ = bit6toAscii(a[2]);
80                         *dest++ = '=';
81                         break;
82                 default: /* this will not happen! */
83                         break;  
84         }
85 /*  finalize: */
86         *dest='\0';
87 }
88