-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64encdec.h
More file actions
69 lines (60 loc) · 1.26 KB
/
base64encdec.h
File metadata and controls
69 lines (60 loc) · 1.26 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef _BASE64ENCDEC_H_
#define _BASE64ENCDEC_H_
static void base64encode(const unsigned char *in, char *out, int len)
{
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int shift = 0;
int accum = 0;
while (len>0)
{
len--;
accum <<= 8;
shift += 8;
accum |= *in++;
while ( shift >= 6 )
{
shift -= 6;
*out++ = alphabet[(accum >> shift) & 0x3F];
}
}
if (shift == 4)
{
*out++ = alphabet[(accum & 0xF)<<2];
*out++='=';
}
else if (shift == 2)
{
*out++ = alphabet[(accum & 0x3)<<4];
*out++='=';
*out++='=';
}
*out++=0;
}
static int base64decode(const char *src, unsigned char *dest, int destsize)
{
unsigned char *olddest=dest;
int accum=0;
int nbits=0;
while (*src)
{
int x=0;
char c=*src++;
if (c >= 'A' && c <= 'Z') x=c-'A';
else if (c >= 'a' && c <= 'z') x=c-'a' + 26;
else if (c >= '0' && c <= '9') x=c-'0' + 52;
else if (c == '+') x=62;
else if (c == '/') x=63;
else break;
accum <<= 6;
accum |= x;
nbits += 6;
while (nbits >= 8)
{
if (--destsize<0) break;
nbits-=8;
*dest++ = (char)((accum>>nbits)&0xff);
}
}
return dest-olddest;
}
#endif // _BASE64ENCDEC_H_