]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/base64.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / include / spdk / base64.h
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /**
35 * \file
36 * Base64 utility functions
37 */
38
39 #ifndef SPDK_BASE64_H
40 #define SPDK_BASE64_H
41
42 #include "spdk/stdinc.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 /**
49 * Following the Base64 part in RFC4648:
50 * https://tools.ietf.org/html/rfc4648.html
51 */
52
53 /**
54 * Calculate strlen of encoded Base64 string based on raw buffer length.
55 *
56 * \param raw_len Length of raw buffer.
57 * \return Encoded Base64 string length, excluding the terminating null byte ('\0').
58 */
59 static inline size_t spdk_base64_get_encoded_strlen(size_t raw_len)
60 {
61 return (raw_len + 2) / 3 * 4;
62 }
63
64 /**
65 * Calculate length of raw buffer based on strlen of encoded Base64.
66 *
67 * \param encoded_strlen Length of enocded Base64 string, excluding terminating null byte ('\0').
68 * \return Length of raw buffer.
69 */
70 static inline size_t spdk_base64_get_decoded_len(size_t encoded_strlen)
71 {
72 /* text_strlen and raw_len should be (4n,3n), (4n+2, 3n+1) or (4n+3, 3n+2) */
73 return encoded_strlen / 4 * 3 + ((encoded_strlen % 4 + 1) / 2);
74 }
75
76 /**
77 * Base 64 Encoding with Standard Base64 Alphabet defined in RFC4684.
78 *
79 * \param dst Buffer address of encoded Base64 string. Its length should be enough
80 * to contain Base64 string and the terminating null byte ('\0'), so it needs to be at
81 * least as long as 1 + spdk_base64_get_encoded_strlen(src_len).
82 * \param src Raw data buffer to be encoded.
83 * \param src_len Length of raw data buffer.
84 *
85 * \return 0 on success.
86 * \return -EINVAL if dst or src is NULL, or binary_len <= 0.
87 */
88 int spdk_base64_encode(char *dst, const void *src, size_t src_len);
89
90 /**
91 * Base 64 Encoding with URL and Filename Safe Alphabet.
92 *
93 * \param dst Buffer address of encoded Base64 string. Its length should be enough
94 * to contain Base64 string and the terminating null byte ('\0'), so it needs to be at
95 * least as long as 1 + spdk_base64_get_encoded_strlen(src_len).
96 * \param src Raw data buffer to be encoded.
97 * \param src_len Length of raw data buffer.
98 *
99 * \return 0 on success.
100 * \return -EINVAL if dst or src is NULL, or binary_len <= 0.
101 */
102 int spdk_base64_urlsafe_encode(char *dst, const void *src, size_t src_len);
103
104 /**
105 * Base 64 Decoding with Standard Base64 Alphabet defined in RFC4684.
106 *
107 * \param dst Buffer address of decoded raw data. Its length should be enough
108 * to contain decoded raw data, so it needs to be at least as long as
109 * spdk_base64_get_decoded_len(encoded_strlen).
110 * \param dst_len Output parameter for the length of actual decoded raw data.
111 * If NULL, the actual decoded length won't be returned.
112 * \param src Data buffer for base64 string to be decoded.
113 *
114 * \return 0 on success.
115 * \return -EINVAL if dst or src is NULL, or content of src is illegal.
116 */
117 int spdk_base64_decode(void *dst, size_t *dst_len, const char *src);
118
119 /**
120 * Base 64 Decoding with URL and Filename Safe Alphabet.
121 *
122 * \param dst Buffer address of decoded raw data. Its length should be enough
123 * to contain decoded raw data, so it needs to be at least as long as
124 * spdk_base64_get_decoded_len(encoded_strlen).
125 * \param dst_len Output parameter for the length of actual decoded raw data.
126 * If NULL, the actual decoded length won't be returned.
127 * \param src Data buffer for base64 string to be decoded.
128 *
129 * \return 0 on success.
130 * \return -EINVAL if dst or src is NULL, or content of src is illegal.
131 */
132 int spdk_base64_urlsafe_decode(void *dst, size_t *dst_len, const char *src);
133
134 #ifdef __cplusplus
135 }
136 #endif
137
138 #endif /* SPDK_BASE64_H */