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