/src/perfetto/include/perfetto/ext/base/base64.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2021 The Android Open Source Project |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (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 |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
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. |
15 | | */ |
16 | | |
17 | | #ifndef INCLUDE_PERFETTO_EXT_BASE_BASE64_H_ |
18 | | #define INCLUDE_PERFETTO_EXT_BASE_BASE64_H_ |
19 | | |
20 | | #include <optional> |
21 | | #include <string> |
22 | | |
23 | | #include "perfetto/ext/base/string_view.h" |
24 | | #include "perfetto/ext/base/utils.h" // For ssize_t. |
25 | | |
26 | | namespace perfetto { |
27 | | namespace base { |
28 | | |
29 | | // Returns the length of the destination string (included '=' padding). |
30 | | // Does NOT include the size of the string null terminator. |
31 | 18 | inline size_t Base64EncSize(size_t src_size) { |
32 | 18 | return (src_size + 2) / 3 * 4; |
33 | 18 | } |
34 | | |
35 | | // Returns the upper bound on the length of the destination buffer. |
36 | | // The actual decoded length might be <= the number returned here. |
37 | 0 | inline size_t Base64DecSize(size_t src_size) { |
38 | 0 | return (src_size + 3) / 4 * 3; |
39 | 0 | } |
40 | | |
41 | | // Does NOT null-terminate |dst|. |
42 | | ssize_t Base64Encode(const void* src, |
43 | | size_t src_size, |
44 | | char* dst, |
45 | | size_t dst_size); |
46 | | |
47 | | std::string Base64Encode(const void* src, size_t src_size); |
48 | | |
49 | 0 | inline std::string Base64Encode(StringView sv) { |
50 | 0 | return Base64Encode(sv.data(), sv.size()); |
51 | 0 | } |
52 | | |
53 | | // Returns -1 in case of failure. |
54 | | ssize_t Base64Decode(const char* src, |
55 | | size_t src_size, |
56 | | uint8_t* dst, |
57 | | size_t dst_size); |
58 | | |
59 | | std::optional<std::string> Base64Decode(const char* src, size_t src_size); |
60 | | |
61 | 0 | inline std::optional<std::string> Base64Decode(StringView sv) { |
62 | 0 | return Base64Decode(sv.data(), sv.size()); |
63 | 0 | } |
64 | | |
65 | | } // namespace base |
66 | | } // namespace perfetto |
67 | | |
68 | | #endif // INCLUDE_PERFETTO_EXT_BASE_BASE64_H_ |