Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / Base64.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2013-2017 Nest Labs, Inc.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *      Base-64 utility functions.
22  *
23  */
24
25 #pragma once
26
27 #include <stdint.h>
28
29 namespace chip {
30
31 typedef char (*Base64ValToCharFunct)(uint8_t val);
32 typedef uint8_t (*Base64CharToValFunct)(uint8_t c);
33
34 // Encode an array of bytes to a base64 string.
35 //
36 // Returns length of generated string.
37 // Output will contain padding characters ('=') as necessary to make length a multiple of 4 characters.
38 // Output DOES NOT include a null terminator.
39 // Output buffer must be at least (inLen + 2) / 3 * 4 bytes long.
40 // Input and output buffers CANNOT overlap.
41 //
42 extern uint16_t Base64Encode(const uint8_t * in, uint16_t inLen, char * out);
43 extern uint16_t Base64URLEncode(const uint8_t * in, uint16_t inLen, char * out);
44 extern uint16_t Base64Encode(const uint8_t * in, uint16_t inLen, char * out, Base64ValToCharFunct valToCharFunct);
45
46 // Decode a base64 string to bytes.
47 //
48 // Returns length of decoded data, or UINT16_MAX if input could not be decoded.
49 // Input MAY contain padding characters ('=') but only at the end of the input string.
50 // Output buffer must be at least inLen * 3 / 4 bytes long, however the actual output
51 //   may be shorter than this due to padding.
52 // Supports decode in place by setting out pointer equal to in.
53 //
54 extern uint16_t Base64Decode(const char * in, uint16_t inLen, uint8_t * out);
55 extern uint16_t Base64URLDecode(const char * in, uint16_t inLen, uint8_t * out);
56 extern uint16_t Base64Decode(const char * in, uint16_t inLen, uint8_t * out, Base64CharToValFunct charToValFunct);
57
58 // Encode/decode functions that take/return 32-bit lengths.
59 //
60 // Similar to the above functions, except Base64Decode32() returns UINT32_MAX if the input cannot be decoded.
61 //
62 extern uint32_t Base64Encode32(const uint8_t * in, uint32_t inLen, char * out);
63 extern uint32_t Base64Encode32(const uint8_t * in, uint32_t inLen, char * out, Base64ValToCharFunct valToCharFunct);
64 extern uint32_t Base64Decode32(const char * in, uint32_t inLen, uint8_t * out);
65 extern uint32_t Base64Decode32(const char * in, uint32_t inLen, uint8_t * out, Base64CharToValFunct charToValFunct);
66
67 /** Computes the base-64 encoded length for a given input length.
68  *
69  * The computed length includes room for padding characters.
70  *
71  * NOTE: The supplied argument must be an integer type.
72  */
73 #define BASE64_ENCODED_LEN(LEN) ((((LEN) + 2) / 3) * 4)
74
75 /** Computes the maximum possible decoded length for a given base-64 string input length.
76  *
77  * NOTE: The actual decoded length may be smaller than this due to padding.
78  */
79 #define BASE64_MAX_DECODED_LEN(LEN) ((LEN) *3 / 4)
80
81 } // namespace chip