1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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] in octet stream, max 3 byte
32 * @param[out] out base64 encoded stream, 4 byte
33 * @param[in] len byte-length of in
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];
81 * Encode the plain message in base64.
83 * @param[in] in Plain message
84 * @param[in] inLen Byte length of 'in'
85 * @param[in,out] outBuf Output buffer
86 * Base64 encoded message will be written into 'out'
87 * NOTE : This method adds a NULL to the string configuration
88 * @param[in] outBufSize Size of output buffer
89 * @param[out] outLen Byte length of encoded message
91 * @return B64_OK for Success, otherwise some error value
93 B64Result b64Encode(const uint8_t* in, const size_t inLen,
94 char* outBuf, const size_t outBufSize, uint32_t* outLen)
99 if (NULL == in || 0 == inLen || NULL == outBuf || NULL == outLen )
101 return B64_INVALID_PARAM;
104 *outLen = ((inLen / 3) * 3 == inLen) ?
106 (((inLen / 3) + 1) * 4);
107 minBufSize = (*outLen + 1);
108 if(outBufSize < minBufSize)
110 return B64_OUTPUT_BUFFER_TOO_SMALL;
113 for (i = 0; i < inLen / 3; i++)
115 if(B64_OK != b64EncodeBlk(in + i * 3, outBuf + i * 4, 3))
117 return B64_INVALID_PARAM;
123 if(B64_OK != b64EncodeBlk(in + i * 3, outBuf + i * 4, inLen - i * 3))
125 return B64_INVALID_PARAM;
129 outBuf[*outLen] = '\0';
137 * @param[in] c Base64 encoded charactor
139 * @return decoded value, 6-bit
141 static uint32_t b64GetVal(char c)
143 if (('A' <= c) && ('Z' >= c))
147 else if (('a' <= c) && ('z' >= c))
151 else if (('0' <= c) && ('9' >= c))
172 * base64 block decode function
174 * @param[in] in Base64 encoded stream, 4 bytes
175 * @param[out] out Octet stream, 3 bytes
177 * @return B64_OK for Success, otherwise some error value
179 static B64Result b64DecodeBlk(const char* in, uint8_t* out)
183 if(NULL == in || NULL == out)
185 return B64_INVALID_PARAM;
188 val = (b64GetVal(in[0]) << 18) | (b64GetVal(in[1]) << 12) |
189 (b64GetVal(in[2]) << 6) | (b64GetVal(in[3]));
191 out[0] = (val >> 16) & 0xff;
195 out[1] = (val >> 8) & 0xff;
206 * Decode the encoded message in base64.
208 * @param[in] in Base64 encoded message
209 * @param[in] inLen Byte lenth of 'in'
210 * @param[in, out] outBuf Output buffer
211 * Base64 decoded message will be written into 'out'
212 * @param[in] outBufSize Size of output buffer
213 * @param[out] outLen Byte length of decoded message
215 * @return B64_OK for Success, otherwise some error value
217 B64Result b64Decode(const char* in, const size_t inLen,
218 uint8_t* outBuf, size_t outBufSize, uint32_t* outLen)
223 if (NULL == in || 0 == inLen || 0 != (inLen & 0x03) || NULL == outBuf || NULL == outLen)
225 return B64_INVALID_PARAM;
228 *outLen = (inLen / 4) * 3;
229 minBufSize = (inLen / 4) * 3;
230 if('=' == in[inLen - 1])
235 if('=' == in[inLen - 2])
240 if(outBufSize < minBufSize)
242 return B64_OUTPUT_BUFFER_TOO_SMALL;
245 for (i = 0; i < inLen / 4; i++)
247 if(B64_OK != b64DecodeBlk(in + i * 4, outBuf + i * 3))
249 return B64_INVALID_PARAM;