1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 /** base character of Base64. */
24 static const char g_b64TransTbl[] =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"\
26 "ghijklmnopqrstuvwxyz0123456789+/";
29 * base64 block encode function.
31 * @param in is the octet stream, max 3 byte.
32 * @param out is the Base64 encoded stream, 4 byte.
33 * @param len is the byte-length of octet stream.
35 * @return ::B64_OK for Success, otherwise some error value.
37 static B64Result b64EncodeBlk(const uint8_t* in, char* out, uint32_t len)
39 if (NULL == in || NULL == out || 0 == len )
41 return B64_INVALID_PARAM;
44 out[0] = g_b64TransTbl[in[0] >> 2];
48 out[1] = g_b64TransTbl[((in[0] & 0x03) << 4)];
52 out[1] = g_b64TransTbl[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
57 out[2] = g_b64TransTbl[((in[1] & 0x0f) << 2)];
61 out[2] = g_b64TransTbl[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
70 out[3] = g_b64TransTbl[in[2] & 0x3f];
80 B64Result b64Encode(const uint8_t* in, const size_t inLen,
81 char* outBuf, const size_t outBufSize, uint32_t* outLen)
83 if (NULL == in || 0 == inLen || NULL == outBuf || NULL == outLen )
85 return B64_INVALID_PARAM;
88 *outLen = ((inLen / 3) * 3 == inLen) ?
90 (((inLen / 3) + 1) * 4);
91 uint32_t minBufSize = (*outLen + 1);
92 if (outBufSize < minBufSize)
94 return B64_OUTPUT_BUFFER_TOO_SMALL;
98 for (i = 0; i < inLen / 3; i++)
100 if(B64_OK != b64EncodeBlk(in + i * 3, outBuf + i * 4, 3))
102 return B64_INVALID_PARAM;
106 if (((size_t)i * 3) != inLen)
108 if (B64_OK != b64EncodeBlk(in + i * 3, outBuf + i * 4, inLen - i * 3))
110 return B64_INVALID_PARAM;
114 outBuf[*outLen] = '\0';
122 * @param c is the Base64 encoded character.
124 * @return decoded value, 6-bit.
126 static uint32_t b64GetVal(char c)
128 if (('A' <= c) && ('Z' >= c))
132 else if (('a' <= c) && ('z' >= c))
136 else if (('0' <= c) && ('9' >= c))
157 * Base64 block decode function.
159 * @param in is the Base64 encoded stream, 4 bytes.
160 * @param out is the Octet stream, 3 bytes.
162 * @return ::B64_OK for Success, otherwise some error value.
164 static B64Result b64DecodeBlk(const char* in, uint8_t* out)
166 if (NULL == in || NULL == out)
168 return B64_INVALID_PARAM;
171 uint32_t val = (b64GetVal(in[0]) << 18) | (b64GetVal(in[1]) << 12) |
172 (b64GetVal(in[2]) << 6) | (b64GetVal(in[3]));
174 out[0] = (val >> 16) & 0xff;
178 out[1] = (val >> 8) & 0xff;
188 B64Result b64Decode(const char* in, const size_t inLen,
189 uint8_t* outBuf, size_t outBufSize, uint32_t* outLen)
191 if (NULL == in || 0 == inLen || 0 != (inLen & 0x03) || NULL == outBuf || NULL == outLen)
193 return B64_INVALID_PARAM;
196 *outLen = (inLen / 4) * 3;
197 uint32_t minBufSize = (inLen / 4) * 3;
198 if ('=' == in[inLen - 1])
203 if ('=' == in[inLen - 2])
208 if (outBufSize < minBufSize)
210 return B64_OUTPUT_BUFFER_TOO_SMALL;
213 for (uint32_t i = 0; i < inLen / 4; i++)
215 if(B64_OK != b64DecodeBlk(in + i * 4, outBuf + i * 3))
217 return B64_INVALID_PARAM;