1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/utils/base64.h"
13 const char kBase64Chars[] =
14 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
19 namespace common_installer {
21 // from https://stackoverflow.com/a/13935718
22 std::string EncodeBase64(unsigned char* val, size_t len) {
26 unsigned char char_array_3[3];
27 unsigned char char_array_4[4];
30 char_array_3[i++] = *(val++);
32 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
33 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
34 ((char_array_3[1] & 0xf0) >> 4);
35 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
36 ((char_array_3[2] & 0xc0) >> 6);
37 char_array_4[3] = char_array_3[2] & 0x3f;
39 for(i = 0; (i <4) ; i++)
40 ret += kBase64Chars[char_array_4[i]];
46 for(j = i; j < 3; j++)
47 char_array_3[j] = '\0';
49 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
50 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
51 ((char_array_3[1] & 0xf0) >> 4);
52 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
53 ((char_array_3[2] & 0xc0) >> 6);
54 char_array_4[3] = char_array_3[2] & 0x3f;
56 for (j = 0; (j < i + 1); j++)
57 ret += kBase64Chars[char_array_4[j]];
66 } // namespace common_installer