Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / security / include / base64.h
1  /******************************************************************
2   *
3   * Copyright 2015 Samsung Electronics All Rights Reserved.
4   *
5   *
6   *
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
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
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.
18   *
19   ******************************************************************/
20
21 #ifndef _IOTVT_B64_H_
22 #define _IOTVT_B64_H_
23
24 #include <stdint.h>
25 #include <stdio.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**
32  * Macro to calculate the size of 'output' buffer required for
33  * a 'input' buffer of length x during Base64 encoding operation.
34  */
35 #define B64ENCODE_OUT_SAFESIZE(x) ((((x) + 3 - 1)/3) * 4 + 1)
36
37 /**
38  * Macro to calculate the size of 'output' buffer required for
39  * a 'input' buffer of length x during Base64 decoding operation.
40  */
41 #define B64DECODE_OUT_SAFESIZE(x) (((x)*3)/4)
42
43 /**
44  * Result code of base64 functions
45  */
46 typedef enum {
47     B64_OK = 0,
48     B64_INVALID_PARAM,
49     B64_OUTPUT_BUFFER_TOO_SMALL,
50     B64_ERROR
51 }B64Result;
52
53 /**
54  * Encode the plain message in base64.
55  *
56  * @param[in] in  Plain message
57  * @param[in] inLen  Byte length of 'in'
58  * @param[in,out] outBuf Output buffer
59  *                Base64 encoded message will be written into 'outBuf'
60  *                NOTE : This method adds a NULL to the string configuration
61  * @param[in] outBufSize Size of output buffer
62  * @param[out] outLen  Byte length of encoded message
63  *
64  * @return  B64_OK for Success, otherwise some error value
65  */
66 B64Result b64Encode(const uint8_t* in, const size_t inLen,
67                char* outBuf, const size_t outBufSize, uint32_t *outLen);
68
69 /**
70  * Decode the encoded message in base64.
71  *
72  * @param[in] in  Base64 encoded message
73  * @param[in] inLen  Byte lenth of 'in'
74  * @param[in, out] outBuf  Output buffer
75  *                 Base64 decoded message will be written into 'outBuf'
76  * @param[in] outBufSize Size of output buffer
77  * @param[out] outLen  Byte length of decoded message
78  *
79  * @return  B64_OK for Success, otherwise some error value
80  */
81 B64Result b64Decode(const char* in, const size_t inLen,
82                uint8_t* outBuf, size_t outBufSize, uint32_t *outLen);
83
84 #ifdef __cplusplus
85 }
86 #endif
87
88 #endif  //IOTVT_B64_H