Mercurial > dovecot > core-2.2
annotate src/lib/base64.c @ 23007:36e01285b5b8
lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author | Stephan Bosch <stephan.bosch@dovecot.fi> |
---|---|
date | Mon, 18 Mar 2019 00:52:37 +0100 |
parents | cb108f786fb4 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21390
diff
changeset
|
1 /* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ |
0 | 2 |
3 #include "lib.h" | |
4 #include "base64.h" | |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
5 #include "buffer.h" |
0 | 6 |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
7 static const char b64enc[] = |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
8 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
9 |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
10 static const unsigned char b64dec[256] = { |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
11 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0-7 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
12 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 8-15 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
13 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16-23 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
14 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 24-31 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
15 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32-39 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
16 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* 40-47 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
17 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, /* 48-55 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
18 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 56-63 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
19 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* 64-71 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
20 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, /* 72-79 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
21 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, /* 80-87 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
22 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, /* 88-95 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
23 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* 96-103 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
24 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, /* 104-111 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
25 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, /* 112-119 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
26 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, /* 120-127 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
27 |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
28 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128-255 */ |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
29 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
30 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
31 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
32 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
33 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
34 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
35 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
36 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
37 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
38 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
44 }; |
0 | 45 |
2708
f1e9f3ec8135
Buffer API change: we no longer support limited sized buffers where
Timo Sirainen <tss@iki.fi>
parents:
2628
diff
changeset
|
46 void base64_encode(const void *src, size_t src_size, buffer_t *dest) |
0 | 47 { |
2628
59059ea978b2
Change src from unsigned char* toi void* so callers don't have to do
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
48 const unsigned char *src_c = src; |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
49 unsigned char tmp[4]; |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
50 size_t src_pos; |
0 | 51 |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
52 for (src_pos = 0; src_pos < src_size; ) { |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
53 tmp[0] = b64enc[src_c[src_pos] >> 2]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
54 switch (src_size - src_pos) { |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
55 case 1: |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
56 tmp[1] = b64enc[(src_c[src_pos] & 0x03) << 4]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
57 tmp[2] = '='; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
58 tmp[3] = '='; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
59 src_pos++; |
0 | 60 break; |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
61 case 2: |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
62 tmp[1] = b64enc[((src_c[src_pos] & 0x03) << 4) | |
8380
59fc12b2b08a
Minor base64 code optimization.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
63 (src_c[src_pos+1] >> 4)]; |
59fc12b2b08a
Minor base64 code optimization.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
64 tmp[2] = b64enc[((src_c[src_pos+1] & 0x0f) << 2)]; |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
65 tmp[3] = '='; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
66 src_pos += 2; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
67 break; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
68 default: |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
69 tmp[1] = b64enc[((src_c[src_pos] & 0x03) << 4) | |
8380
59fc12b2b08a
Minor base64 code optimization.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
70 (src_c[src_pos+1] >> 4)]; |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
71 tmp[2] = b64enc[((src_c[src_pos+1] & 0x0f) << 2) | |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
72 ((src_c[src_pos+2] & 0xc0) >> 6)]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
73 tmp[3] = b64enc[src_c[src_pos+2] & 0x3f]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
74 src_pos += 3; |
0 | 75 break; |
76 } | |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
77 buffer_append(dest, tmp, 4); |
0 | 78 } |
79 } | |
80 | |
5508
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
81 #define IS_EMPTY(c) \ |
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
82 ((c) == '\n' || (c) == '\r' || (c) == ' ' || (c) == '\t') |
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
83 |
2628
59059ea978b2
Change src from unsigned char* toi void* so callers don't have to do
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
84 int base64_decode(const void *src, size_t src_size, |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
770
diff
changeset
|
85 size_t *src_pos_r, buffer_t *dest) |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
86 { |
2628
59059ea978b2
Change src from unsigned char* toi void* so callers don't have to do
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
87 const unsigned char *src_c = src; |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
88 size_t src_pos; |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
89 unsigned char input[4], output[3]; |
5509
c46a29e0ea9d
API change: Returns now 1 if ok, 0 if end of base64 marker found.
Timo Sirainen <tss@iki.fi>
parents:
5508
diff
changeset
|
90 int ret = 1; |
0 | 91 |
6605
d81a50101724
Whitespace skipping was broken.
Timo Sirainen <tss@iki.fi>
parents:
6482
diff
changeset
|
92 for (src_pos = 0; src_pos+3 < src_size; ) { |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
93 input[0] = b64dec[src_c[src_pos]]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
94 if (input[0] == 0xff) { |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
95 if (unlikely(!IS_EMPTY(src_c[src_pos]))) { |
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
96 ret = -1; |
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
97 break; |
6712 | 98 } |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
99 src_pos++; |
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
100 continue; |
0 | 101 } |
102 | |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
103 input[1] = b64dec[src_c[src_pos+1]]; |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
104 if (unlikely(input[1] == 0xff)) { |
6713
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
105 ret = -1; |
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
106 break; |
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
107 } |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
108 output[0] = (input[0] << 2) | (input[1] >> 4); |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
109 |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
110 input[2] = b64dec[src_c[src_pos+2]]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
111 if (input[2] == 0xff) { |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
112 if (unlikely(src_c[src_pos+2] != '=' || |
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
113 src_c[src_pos+3] != '=')) { |
6713
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
114 ret = -1; |
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
115 break; |
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
116 } |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
117 buffer_append(dest, output, 1); |
5509
c46a29e0ea9d
API change: Returns now 1 if ok, 0 if end of base64 marker found.
Timo Sirainen <tss@iki.fi>
parents:
5508
diff
changeset
|
118 ret = 0; |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
119 src_pos += 4; |
0 | 120 break; |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
121 } |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
122 |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
123 output[1] = (input[1] << 4) | (input[2] >> 2); |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
124 input[3] = b64dec[src_c[src_pos+3]]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
125 if (input[3] == 0xff) { |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6713
diff
changeset
|
126 if (unlikely(src_c[src_pos+3] != '=')) { |
6713
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
127 ret = -1; |
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
128 break; |
cfdf3b2e3dd3
base64_decode(): Update src_pos_r even if we return -1.
Timo Sirainen <tss@iki.fi>
parents:
6712
diff
changeset
|
129 } |
6482
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
130 buffer_append(dest, output, 2); |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
131 ret = 0; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
132 src_pos += 4; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
133 break; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
134 } |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
135 |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
136 output[2] = ((input[2] << 6) & 0xc0) | input[3]; |
c02c7912fb15
Rewrote base64 functions so they're now MIT licensed.
Timo Sirainen <tss@iki.fi>
parents:
6180
diff
changeset
|
137 buffer_append(dest, output, 3); |
6605
d81a50101724
Whitespace skipping was broken.
Timo Sirainen <tss@iki.fi>
parents:
6482
diff
changeset
|
138 src_pos += 4; |
0 | 139 } |
140 | |
5508
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
141 for (; src_pos < src_size; src_pos++) { |
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
142 if (!IS_EMPTY(src_c[src_pos])) |
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
143 break; |
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
144 } |
d1168da66050
Skip trailing linefeeds/whitespace.
Timo Sirainen <tss@iki.fi>
parents:
2708
diff
changeset
|
145 |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
146 if (src_pos_r != NULL) |
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
147 *src_pos_r = src_pos; |
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
608
diff
changeset
|
148 |
5509
c46a29e0ea9d
API change: Returns now 1 if ok, 0 if end of base64 marker found.
Timo Sirainen <tss@iki.fi>
parents:
5508
diff
changeset
|
149 return ret; |
0 | 150 } |
6180 | 151 |
152 buffer_t *t_base64_decode_str(const char *str) | |
153 { | |
154 buffer_t *buf; | |
155 size_t len = strlen(str); | |
156 | |
157 buf = buffer_create_dynamic(pool_datastack_create(), | |
158 MAX_BASE64_DECODED_SIZE(len)); | |
159 (void)base64_decode(str, len, NULL, buf); | |
160 return buf; | |
161 } | |
12038 | 162 |
163 bool base64_is_valid_char(char c) | |
164 { | |
165 return b64dec[(uint8_t)c] != 0xff; | |
166 } |