fehQlibs 30
Qlibs
Loading...
Searching...
No Matches
base64.c
Go to the documentation of this file.
1#include "base64.h"
2#include "stralloc.h"
3
10static const char *b64alpha =
11 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12#define B64PAD '='
13
14/* returns 0 ok, 1 illegal, -1 problem */
15
16int b64decode(const unsigned char *in,int l,stralloc *out) /* not null terminated */
17{
18 int p = 0;
19 int n;
20 unsigned int x;
21 int i, j;
22 char *s;
23 unsigned char b[3];
24
25 if (l == 0) {
26 if (!stralloc_copys(out,"")) return -1;
27 return 0;
28 }
29
30 while (in[l-1] == B64PAD) {
31 p ++;
32 l--;
33 }
34
35 n = (l + p) / 4;
36 i = (n * 3) - p;
37 if (!stralloc_ready(out,i)) return -1;
38 out->len = i;
39 s = out->s;
40
41 for (i = 0; i < n - 1 ; i++) {
42 x = 0;
43 for (j = 0; j < 4; j++) {
44 if(in[j] >= 'A' && in[j] <= 'Z')
45 x = (x << 6) + (unsigned int)(in[j] - 'A' + 0);
46 else if(in[j] >= 'a' && in[j] <= 'z')
47 x = (x << 6) + (unsigned int)(in[j] - 'a' + 26);
48 else if(in[j] >= '0' && in[j] <= '9')
49 x = (x << 6) + (unsigned int)(in[j] - '0' + 52);
50 else if(in[j] == '+')
51 x = (x << 6) + 62;
52 else if(in[j] == '/')
53 x = (x << 6) + 63;
54 else if(in[j] == '=')
55 x = (x << 6);
56 }
57
58 s[2] = (unsigned char)(x & 255); x >>= 8;
59 s[1] = (unsigned char)(x & 255); x >>= 8;
60 s[0] = (unsigned char)(x & 255); x >>= 8;
61 s += 3; in += 4;
62 }
63
64 x = 0;
65 for (j = 0; j < 4; j++) {
66 if(in[j] >= 'A' && in[j] <= 'Z')
67 x = (x << 6) + (unsigned int)(in[j] - 'A' + 0);
68 else if(in[j] >= 'a' && in[j] <= 'z')
69 x = (x << 6) + (unsigned int)(in[j] - 'a' + 26);
70 else if(in[j] >= '0' && in[j] <= '9')
71 x = (x << 6) + (unsigned int)(in[j] - '0' + 52);
72 else if(in[j] == '+')
73 x = (x << 6) + 62;
74 else if(in[j] == '/')
75 x = (x << 6) + 63;
76 else if(in[j] == '=')
77 x = (x << 6);
78 }
79
80 b[2] = (unsigned char)(x & 255); x >>= 8;
81 b[1] = (unsigned char)(x & 255); x >>= 8;
82 b[0] = (unsigned char)(x & 255); x >>= 8;
83
84 for (i = 0; i < 3 - p; i++)
85 s[i] = b[i];
86
87 return 0;
88}
89
90int b64encode(stralloc *in,stralloc *out) /* not null terminated */
91{
92 unsigned char a, b, c;
93 int i;
94 char *s;
95
96 if (in->len == 0) {
97 if (!stralloc_copys(out,"")) return -1;
98 return 0;
99 }
100
101 i = in->len / 3 * 4 + 4;
102 if (!stralloc_ready(out,i)) return -1;
103 s = out->s;
104
105 for (i = 0; i < in->len; i += 3) {
106 a = in->s[i];
107 b = i + 1 < in->len ? in->s[i + 1] : 0;
108 c = i + 2 < in->len ? in->s[i + 2] : 0;
109
110 *s++ = b64alpha[a >> 2];
111 *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
112
113 if (i + 1 >= in->len) *s++ = B64PAD;
114 else *s++ = b64alpha[((b & 15) << 2) | (c >> 6)];
115
116 if (i + 2 >= in->len) *s++ = B64PAD;
117 else *s++ = b64alpha[c & 63];
118 }
119 out->len = s - out->s;
120
121 return 0;
122}
int stralloc_ready(stralloc *sa, size_t len)
Definition: stralloc.c:47
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
int b64encode(stralloc *in, stralloc *out)
Definition: base64.c:90
#define B64PAD
Definition: base64.c:12
int b64decode(const unsigned char *in, int l, stralloc *out)
Definition: base64.c:16
size_t len
Definition: stralloc.h:19
char * s
Definition: stralloc.h:18