aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/string_utils/base32/base32.h
blob: 2596bf78a27158e96d6219fa3feadee6676d4b7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once

#include <util/generic/string.h>

#include <string>
#include <string_view>

// Base32 encoding based on RFC 4648 alphabet (incompatible with Crockford and Geohash alphabet)
// https://en.wikipedia.org/wiki/Base32#RFC_4648_Base32_alphabet

///
/// @return Size of the buffer required to decode Base32 encoded data of size `len`.
///
constexpr size_t Base32DecodeBufSize(size_t len) noexcept {
    return (len * 5 + 7) / 8;
}

///
/// @brief Decodes only valid Base32 string, behaviour for invalid data is unspecified.
///
/// @param src a base32 encoded string.
/// @param dst an pointer to allocated memory for writing result.
///
/// @return Count of written bytes.
///
size_t Base32Decode(std::string_view src, char* dst);

///
/// @param src a base32 encoded string.
/// @param dst a decoded string.
///
inline void Base32Decode(std::string_view src, std::string& dst)
{
    ::ResizeUninitialized(dst, Base32DecodeBufSize(src.size()));
    dst.resize(Base32Decode(src, dst.data()));
}

///
/// @param s a base32 encoded string.
///
/// @returns a decoded string.
///
inline std::string Base32Decode(std::string_view s)
{
    std::string ret;
    Base32Decode(s, ret);
    return ret;
}

///
/// @brief Decodes Base32 string with strict verification of invalid symbols,
///        also tries to decode Base32 string with padding inside.
///
/// @throws Throws exceptions on inputs which contain invalid symbols or incorrect padding.
///
/// @param src a base32 encoded string.
/// @param dst an pointer to allocated memory for writing result.
///
/// @return Count of written bytes.
///
size_t Base32StrictDecode(std::string_view src, char* dst);

///
/// @param src a base32 encoded string.
/// @param dst a decoded string.
///
inline void Base32StrictDecode(std::string_view src, std::string& dst)
{
    ::ResizeUninitialized(dst, Base32DecodeBufSize(src.size()));
    dst.resize(Base32StrictDecode(src, dst.data()));
}

///
/// @param s a base32 encoded string.
///
/// @returns a decoded string.
///
inline std::string Base32StrictDecode(std::string_view s)
{
    std::string ret;
    Base32StrictDecode(s, ret);
    return ret;
}

///
/// @return Size of the buffer required to encode Base32 decoded data of size `len`.
///
constexpr size_t Base32EncodeBufSize(size_t len) noexcept {
    return ((len * 8 + 4) / 5 + 7) / 8 * 8;
}

///
/// @param src a base32 decoded string.
/// @param dst an pointer to allocated memory for writing result.
///
/// @return Count of written bytes.
///
size_t Base32Encode(std::string_view src, char* dst);

///
/// @param src a base32 decoded string.
/// @param dst a encoded string.
///
inline void Base32Encode(std::string_view src, std::string& dst)
{
    ::ResizeUninitialized(dst, Base32EncodeBufSize(src.size()));
    dst.resize(Base32Encode(src, dst.data()));
}

///
/// @param s a base32 decoded string.
///
/// @returns a encoded string.
///
inline std::string Base32Encode(std::string_view s)
{
    std::string ret;
    Base32Encode(s, ret);
    return ret;
}