-
Notifications
You must be signed in to change notification settings - Fork 1
/
sander-base-encode.c
37 lines (35 loc) · 1 KB
/
sander-base-encode.c
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
#include <stdio.h>
#include <stdlib.h>
static char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void base64_encode(const unsigned char *input,int len, unsigned char *output){
do{
*output++ = base64[(input[0] & 0xFC ) >> 2];
if(len==1){
*output++ = base64[((input[0] & 0x03) << 4 ) ];
*output++ = '=';
*output++ = '=';
break;
}
*output++ = base64[
((input[0] & 0x03 ) << 4) | ((input[1] & 0xF0) >> 4)];
if(len==2){
*output++ = base64[((input[1] & 0x0F) << 2)];
*output++ = '=';
break;
}
*output++ = base64[
((input[1] & 0x0F) << 2) | ((input[2] & 0xC0) >> 6)];
*output++ = base64[(input[2] & 0x3F)];
input +=3;
}while(len -=3);
*output = '\0';
*output = '\n';
}
int main(int argc, char *argv[]){
char *buffer = (char *) malloc(4/3 * sizeof(argv[1])/sizeof(char));
if(argc == 2){
base64_encode(argv[1],sizeof(argv[1]),&buffer[0]);
printf(buffer);
}
return 0;
}