]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/base64.h
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 * Base64 utility functions
42 #include "spdk/stdinc.h"
49 * Following the Base64 part in RFC4648:
50 * https://tools.ietf.org/html/rfc4648.html
54 * Calculate strlen of encoded Base64 string based on raw buffer length.
56 * \param raw_len Length of raw buffer.
57 * \return Encoded Base64 string length, excluding the terminating null byte ('\0').
59 static inline size_t spdk_base64_get_encoded_strlen(size_t raw_len
)
61 return (raw_len
+ 2) / 3 * 4;
65 * Calculate length of raw buffer based on strlen of encoded Base64.
67 * \param encoded_strlen Length of enocded Base64 string, excluding terminating null byte ('\0').
68 * \return Length of raw buffer.
70 static inline size_t spdk_base64_get_decoded_len(size_t encoded_strlen
)
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);
77 * Base 64 Encoding with Standard Base64 Alphabet defined in RFC4684.
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.
85 * \return 0 on success.
86 * \return -EINVAL if dst or src is NULL, or binary_len <= 0.
88 int spdk_base64_encode(char *dst
, const void *src
, size_t src_len
);
91 * Base 64 Encoding with URL and Filename Safe Alphabet.
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.
99 * \return 0 on success.
100 * \return -EINVAL if dst or src is NULL, or binary_len <= 0.
102 int spdk_base64_urlsafe_encode(char *dst
, const void *src
, size_t src_len
);
105 * Base 64 Decoding with Standard Base64 Alphabet defined in RFC4684.
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.
114 * \return 0 on success.
115 * \return -EINVAL if dst or src is NULL, or content of src is illegal.
117 int spdk_base64_decode(void *dst
, size_t *dst_len
, const char *src
);
120 * Base 64 Decoding with URL and Filename Safe Alphabet.
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.
129 * \return 0 on success.
130 * \return -EINVAL if dst or src is NULL, or content of src is illegal.
132 int spdk_base64_urlsafe_decode(void *dst
, size_t *dst_len
, const char *src
);
138 #endif /* SPDK_BASE64_H */