Mercurial > hg > orthanc
annotate OrthancFramework/Resources/ThirdParty/base64/base64.cpp @ 5858:66fd63b8601e find-refactoring tip
StorageAccessOnFind: cont
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Mon, 04 Nov 2024 22:34:14 +0100 |
parents | d25f4c0fa160 |
children |
rev | line source |
---|---|
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
1 /* |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
2 base64.cpp and base64.h |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
3 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
4 Copyright (C) 2004-2008 René Nyffenegger |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
5 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
6 This source code is provided 'as-is', without any express or implied |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
7 warranty. In no event will the author be held liable for any damages |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
8 arising from the use of this software. |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
9 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
10 Permission is granted to anyone to use this software for any purpose, |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
11 including commercial applications, and to alter it and redistribute it |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
12 freely, subject to the following restrictions: |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
13 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
14 1. The origin of this source code must not be misrepresented; you must not |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
15 claim that you wrote the original source code. If you use this source code |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
16 in a product, an acknowledgment in the product documentation would be |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
17 appreciated but is not required. |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
18 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
19 2. Altered source versions must be plainly marked as such, and must not be |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
20 misrepresented as being the original source code. |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
21 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
22 3. This notice may not be removed or altered from any source distribution. |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
23 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
24 René Nyffenegger rene.nyffenegger@adp-gmbh.ch |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
25 |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
26 ------------------------------ |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
27 This version has been modified (changed the interface + use another decoding algorithm |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
28 inspired from https://stackoverflow.com/a/34571089 which was faster) |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
29 */ |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
30 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
31 #include "base64.h" |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
32 #include <string.h> |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
33 #include <vector> |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
34 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
35 static const std::string base64_chars = |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
37 "abcdefghijklmnopqrstuvwxyz" |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
38 "0123456789+/"; |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
39 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
40 static inline bool is_base64(unsigned char c) { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
41 return (isalnum(c) || (c == '+') || (c == '/')); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
42 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
43 |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
44 void base64_encode(std::string& result, const std::string& stringToEncode) |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
45 { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
46 const unsigned char* bytes_to_encode = reinterpret_cast<const unsigned char*> |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
47 (stringToEncode.size() > 0 ? &stringToEncode[0] : NULL); |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
48 size_t in_len = stringToEncode.size(); |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
49 |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
50 result.reserve(result.size() + in_len * 4 / 3 + 10); |
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
51 |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
52 int i = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
53 int j = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
54 unsigned char char_array_3[3]; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
55 unsigned char char_array_4[4]; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
56 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
57 while (in_len--) { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
58 char_array_3[i++] = *(bytes_to_encode++); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
59 if (i == 3) { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
60 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
61 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
62 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
63 char_array_4[3] = char_array_3[2] & 0x3f; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
64 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
65 for(i = 0; (i <4) ; i++) |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
66 result += base64_chars[char_array_4[i]]; |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
67 i = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
68 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
69 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
70 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
71 if (i) |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
72 { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
73 for(j = i; j < 3; j++) |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
74 char_array_3[j] = '\0'; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
75 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
76 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
77 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
78 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
79 char_array_4[3] = char_array_3[2] & 0x3f; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
80 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
81 for (j = 0; (j < i + 1); j++) |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
82 result += base64_chars[char_array_4[j]]; |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
83 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
84 while((i++ < 3)) |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
85 result += '='; |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
86 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
87 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
88 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
89 |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
90 // old code from René Nyffenegger. This code is slower |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
91 void base64_decode_old(std::string& result, const std::string& encoded_string) { |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
92 size_t in_len = encoded_string.size(); |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
93 int i = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
94 int j = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
95 int in_ = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
96 unsigned char char_array_4[4], char_array_3[3]; |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
97 |
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
98 result.reserve(result.size() + in_len * 3 / 4 + 10); |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
99 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
100 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
101 char_array_4[i++] = encoded_string[in_]; in_++; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
102 if (i ==4) { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
103 for (i = 0; i <4; i++) |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
104 char_array_4[i] = base64_chars.find(char_array_4[i]); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
105 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
106 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
107 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
108 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
109 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
110 for (i = 0; (i < 3); i++) |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
111 result += char_array_3[i]; |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
112 i = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
113 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
114 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
115 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
116 if (i) { |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
117 for (j = i; j <4; j++) |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
118 char_array_4[j] = 0; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
119 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
120 for (j = 0; j <4; j++) |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
121 char_array_4[j] = base64_chars.find(char_array_4[j]); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
122 |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
123 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
124 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
125 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
126 |
3325
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
127 for (j = 0; (j < i - 1); j++) |
2e7c5c15ba25
reserve size for base64 decoding + avoid copy. In a test in WASM, encoding 3MB took 110ms instead of 1400ms previously
Alain Mazy <alain@mazy.be>
parents:
2102
diff
changeset
|
128 result += char_array_3[j]; |
2102
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
129 } |
f0544ab9396b
fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
950
diff
changeset
|
130 } |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
131 |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
132 |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
133 // new code from https://stackoverflow.com/a/34571089 |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
134 // note that the encoding algorithm from this page was slower (and bugged !) |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
135 // this code is not using std::vector::find |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
136 |
3329 | 137 // static init equivalent to: |
138 // decode_indexes.assign(256, -1); | |
139 // for (int i=0; i<64; ++i) | |
140 // decode_indexes[base64_chars[i]] = i; | |
141 | |
142 static const int decode_indexes[] = { | |
143 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
144 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
145 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, | |
146 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, | |
147 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
148 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, | |
149 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
150 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, | |
151 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
152 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
153 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
154 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
155 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
156 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
157 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
158 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 | |
159 }; | |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
160 |
3332 | 161 |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
162 void base64_decode(std::string& result, const std::string &stringToDecode) { |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
163 |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
164 result.reserve(result.size() + stringToDecode.size() * 3 / 4 + 10); |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
165 |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
166 int val=0, valb=-8; |
3332 | 167 for (std::string::const_iterator c = stringToDecode.begin(); c != stringToDecode.end(); ++c) |
168 { | |
169 size_t index = static_cast<size_t>(*c); | |
170 if (decode_indexes[index] == -1) | |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
171 break; |
3332 | 172 val = (val<<6) + decode_indexes[index]; |
3326
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
173 valb += 6; |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
174 if (valb>=0) { |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
175 result.push_back(char((val>>valb)&0xFF)); |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
176 valb-=8; |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
177 } |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
178 } |
b21d4cc8e5d1
speed up base64 decoding + added tests
Alain Mazy <alain@mazy.be>
parents:
3325
diff
changeset
|
179 } |