MCPcopy Create free account
hub / github.com/OSGeo/gdal / CPLBase64Encode

Function CPLBase64Encode

port/cpl_base64.cpp:196–255  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

194/** Base64 encode a buffer. */
195
196char *CPLBase64Encode(int nDataLen, const GByte *pabyBytesToEncode)
197{
198 constexpr char base64Chars[] =
199 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
200
201 const int kCharArray3Size = 3;
202 const int kCharArray4Size = 4;
203 unsigned char charArray3[kCharArray3Size] = {};
204
205 std::string result("");
206 int array3_idx = 0;
207 while (nDataLen--)
208 {
209 charArray3[array3_idx++] = *(pabyBytesToEncode++);
210
211 if (array3_idx == kCharArray3Size)
212 {
213 const unsigned char charArray4[kCharArray4Size] = {
214 static_cast<unsigned char>((charArray3[0] & 0xfc) >> 2),
215 static_cast<unsigned char>(((charArray3[0] & 0x03) << 4) +
216 ((charArray3[1] & 0xf0) >> 4)),
217 static_cast<unsigned char>(((charArray3[1] & 0x0f) << 2) +
218 ((charArray3[2] & 0xc0) >> 6)),
219 static_cast<unsigned char>(charArray3[2] & 0x3f)};
220
221 for (int idx = 0; idx < kCharArray4Size; ++idx)
222 {
223 result += base64Chars[charArray4[idx]];
224 }
225
226 array3_idx = 0;
227 }
228 }
229
230 if (array3_idx)
231 {
232 for (int idx = array3_idx; idx < kCharArray3Size; ++idx)
233 {
234 charArray3[idx] = '\0';
235 }
236
237 const unsigned char charArray4[kCharArray4Size] = {
238 static_cast<unsigned char>((charArray3[0] & 0xfc) >> 2),
239 static_cast<unsigned char>(((charArray3[0] & 0x03) << 4) +
240 ((charArray3[1] & 0xf0) >> 4)),
241 static_cast<unsigned char>(((charArray3[1] & 0x0f) << 2) +
242 ((charArray3[2] & 0xc0) >> 6)),
243 static_cast<unsigned char>(charArray3[2] & 0x3f)};
244
245 for (int idx = 0; idx < (array3_idx + 1); ++idx)
246 {
247 result += base64Chars[charArray4[idx]];
248 }
249
250 while (array3_idx++ < kCharArray3Size)
251 result += '=';
252 }
253

Callers 15

AddToContainerFunction · 0.85
ReadListFunction · 0.85
BuildJSonFromFeatureMethod · 0.85
WriteFieldRegularMethod · 0.85
GetBinaryAsBase64Function · 0.85
GetSignatureFunction · 0.85
GetGSHeadersFunction · 0.85
GetSignedURLMethod · 0.85
DeleteObjectsMethod · 0.85
SetFileMetadataMethod · 0.85

Calls 2

CPLStrdupFunction · 0.85
c_strMethod · 0.45

Tested by

no test coverage detected