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.
18 * @file DTapps2Base64.cpp
19 * @brief This file includes functions relating to Base64 Encode Decode.
22 #include "DTapps2Base64.h"
24 const unsigned char MAP[128] =
26 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
27 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
28 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
29 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
30 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
31 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
32 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
33 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
38 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
39 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
40 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
41 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
42 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
43 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
44 'w', 'x', 'y', 'z', '0', '1', '2', '3',
45 '4', '5', '6', '7', '8', '9', '+', '/'
48 BOOL DTappsB64Encode(unsigned char* in, int in_size, unsigned char* out, int out_size)
51 unsigned char a, b, c;
52 int len = (in_size + 2) / 3 * 4;
57 for( i = 0, j = 0; j < len; i += 3, j += 4 )
61 *(out+j) = CODE[(a >> 2) & 0x3F];
66 *(out+j+1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
67 *(out+j+2) = CODE[((b << 2) & 0x3c) + ((c >> 6) & 0x3)];
68 *(out+j+3) = CODE[c & 0x3F];
70 else if( in_size - i > 1 )
73 *(out+j+1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
74 *(out+j+2) = CODE[((b << 2) & 0x3c) + ((c >> 6) & 0x3)];
79 *(out+j+1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
89 int DTappsB64Decode(unsigned char* in, int in_size, unsigned char* out, int& out_size)
95 for( i=j=k=0; i < in_size; i++ )
99 c[j] = MAP[(unsigned char)in[i]];
107 out[k++] = (c[0]<<2) | ((c[1]>>4) & 0x3);
111 out[k++] = ((c[1]<<4) & 0xf0) | ((c[2]>>2) & 0xf);
115 out[k++] = ((c[2]<<6) & 0xc0) | c[3];