2 * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * @file DTapps2Base64.cpp
17 * @brief Base64 Encode Decode.
20 #include "DTapps2Base64.h"
22 const unsigned char MAP[128] = {
23 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
24 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
25 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
26 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
27 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
28 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
29 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
30 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
33 const char CODE[64] = {
34 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
35 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
36 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
37 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
38 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
39 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
40 'w', 'x', 'y', 'z', '0', '1', '2', '3',
41 '4', '5', '6', '7', '8', '9', '+', '/'
44 BOOL DTappsB64Encode(unsigned char *in, int in_size, unsigned char *out,
48 unsigned char a, b, c;
49 int len = (in_size + 2) / 3 * 4;
54 for (i = 0, j = 0; j < len; i += 3, j += 4) {
57 *(out + j) = CODE[(a >> 2) & 0x3F];
59 if (in_size - i > 2) {
62 *(out + j + 1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
63 *(out + j + 2) = CODE[((b << 2) & 0x3c) + ((c >> 6) & 0x3)];
64 *(out + j + 3) = CODE[c & 0x3F];
65 } else if (in_size - i > 1) {
67 *(out + j + 1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
68 *(out + j + 2) = CODE[((b << 2) & 0x3c) + ((c >> 6) & 0x3)];
71 *(out + j + 1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
81 int DTappsB64Decode(unsigned char *in, int in_size, unsigned char *out,
88 for (i = j = k = 0; i < in_size; i++) {
92 c[j] = MAP[(unsigned char)in[i]];
101 out[k++] = (c[0] << 2) | ((c[1] >> 4) & 0x3);
105 out[k++] = ((c[1] << 4) & 0xf0) | ((c[2] >> 2) & 0xf);
109 out[k++] = ((c[2] << 6) & 0xc0) | c[3];