comparison Resources/ThirdParty/base64/base64.cpp @ 3329:98cd69135999

fix base64 static init
author Alain Mazy <alain@mazy.be>
date Thu, 21 Mar 2019 12:04:39 +0100
parents b21d4cc8e5d1
children 79178122842c
comparison
equal deleted inserted replaced
3328:f0c92ecd09c8 3329:98cd69135999
132 132
133 // new code from https://stackoverflow.com/a/34571089 133 // new code from https://stackoverflow.com/a/34571089
134 // note that the encoding algorithm from this page was slower (and bugged !) 134 // note that the encoding algorithm from this page was slower (and bugged !)
135 // this code is not using std::vector::find 135 // this code is not using std::vector::find
136 136
137 static std::vector<int> decode_indexes; 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 };
138 160
139 void base64_decode(std::string& result, const std::string &stringToDecode) { 161 void base64_decode(std::string& result, const std::string &stringToDecode) {
140 162
141 result.reserve(result.size() + stringToDecode.size() * 3 / 4 + 10); 163 result.reserve(result.size() + stringToDecode.size() * 3 / 4 + 10);
142
143 if (decode_indexes.size() != 256) // initialize the first time we pass here
144 {
145 decode_indexes.assign(256, -1);
146 for (int i=0; i<64; ++i)
147 decode_indexes[base64_chars[i]] = i;
148 }
149 164
150 int val=0, valb=-8; 165 int val=0, valb=-8;
151 for (std::string::const_iterator c = stringToDecode.begin(); c != stringToDecode.end(); ++c) { 166 for (std::string::const_iterator c = stringToDecode.begin(); c != stringToDecode.end(); ++c) {
152 if (decode_indexes[*c] == -1) 167 if (decode_indexes[*c] == -1)
153 break; 168 break;