nlib
Base64.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_BASE64_H_
4 #define INCLUDE_NN_NLIB_BASE64_H_
5 
6 #include "nn/nlib/UniquePtr.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 
11  public:
12  enum CharOption {
13  BASE64_PLUS_SLASH = 0,
21  _BASE64_MAX,
22  BASE64_DEFAULT = BASE64_PLUS_SLASH,
23  BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
24  };
25 
26  public:
27  static size_t GetRequiredSize(size_t srcsize) NLIB_NOEXCEPT {
28  return ((srcsize + 2) / 3) * 4 + 1;
29  }
30  static errno_t Encode(char* dst,
31  size_t dstsize,
32  const void* src,
33  size_t srcsize,
34  CharOption char_option = BASE64_DEFAULT,
35  bool padding = false) NLIB_NOEXCEPT NLIB_NONNULL;
36  template<size_t N>
37  static errno_t Encode(char (&dst)[N],
38  const void* src,
39  size_t srcsize,
40  CharOption char_option = BASE64_DEFAULT,
41  bool padding = false) NLIB_NOEXCEPT {
42  return Encode(dst, N, src, srcsize, char_option, padding);
43  }
44 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
45  template<class DUMMY = void>
46 #endif
47  static errno_t Encode(UniquePtr<char[]>& dst, // NOLINT
48  const void* src,
49  size_t srcsize,
50  CharOption char_option = BASE64_DEFAULT,
51  bool padding = false) NLIB_NOEXCEPT {
52  // to avoid crossing the DLL boundary
53  size_t dstsize = GetRequiredSize(srcsize);
54  // do not use UniquePtr to avoid C4714
55  char* p = new (std::nothrow) char[dstsize];
56  if (!p) return ENOMEM;
57  errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
58  if (e == 0)
59  dst.reset(p);
60  else
61  delete[] p;
62  return e;
63  }
64  Base64Encoder() NLIB_NOEXCEPT : rem_(-1) {}
66  errno_t Init(CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT;
67 
68  // note that dst is NOT null terminated
69  errno_t StepEncode(
70  size_t* written,
71  char* dst,
72  size_t dstsize,
73  const void* src,
74  size_t srcsize) NLIB_NOEXCEPT NLIB_NONNULL;
75  // note that dst is NOT null terminated
76  template<size_t N>
78  size_t* written,
79  char (&dst)[N],
80  const void* src,
81  size_t srcsize) NLIB_NOEXCEPT {
82  return StepEncode(written, dst, N, src, srcsize);
83  }
84  // note that dst is NOT null terminated
85  errno_t Close(
86  size_t* written,
87  char* dst,
88  size_t dstsize,
89  bool padding) NLIB_NOEXCEPT NLIB_NONNULL;
90  // note that dst is NOT null terminated
91  template<size_t N>
93  size_t* written,
94  char(&dst)[N],
95  bool padding) NLIB_NOEXCEPT {
96  return Close(written, dst, N, padding);
97  }
98  NLIB_SAFE_BOOL(Base64Encoder, (rem_ >= 0))
99 
100  private:
101  int rem_;
102  uint8_t buf_[2];
103  char table_[64];
104 };
105 
107  public:
108  enum CharOption {
109  BASE64_PLUS_SLASH = 0,
110  BASE64_PLUS_MINUS,
111  BASE64_MINUS_UNDERSCORE,
112  BASE64_DOT_MINUS,
113  BASE64_UNDERSCORE_COLON,
114  BASE64_UNDERSCORE_MINUS,
115  BASE64_DOT_UNDERSCORE,
116  BASE64_EXCLAMATION_MINUS,
117  _BASE64_MAX,
118  BASE64_DEFAULT = BASE64_PLUS_SLASH,
119  BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
120  };
121 
122  public:
123  static size_t GetRequiredSize(size_t srcsize) NLIB_NOEXCEPT {
124  return ((srcsize + 3) / 4) * 3;
125  }
126  static errno_t Decode(size_t* written,
127  uint8_t* dst,
128  size_t dstsize,
129  const char* src,
130  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
131  return Decode_(written, dst, dstsize, src, nlib_strlen(src), char_option);
132  }
133  template<size_t N>
134  static errno_t Decode(size_t* written,
135  uint8_t (&dst)[N],
136  const char* src,
137  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
138  return Decode_(written, dst, N, src, nlib_strlen(src), char_option);
139  }
140 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
141  template<class DUMMY = void>
142 #endif
143  static errno_t Decode(UniquePtr<uint8_t[]>& dst, // NOLINT
144  size_t* dstsize, const char* src,
145  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
146  // to avoid crossing the DLL boundary
147  size_t srcsize = nlib_strlen(src);
148  size_t dstsize_ = GetRequiredSize(srcsize);
149  // do not use UniquePtr to avoid C4714
150  uint8_t* p = new (std::nothrow) uint8_t[dstsize_];
151  if (!p) return ENOMEM;
152  size_t written;
153  errno_t e = Decode_(&written, p, dstsize_, src, srcsize, char_option);
154  if (e == 0) {
155  dst.reset(p);
156  *dstsize = written;
157  } else {
158  delete[] p;
159  }
160  return e;
161  }
162  static errno_t DecodeInplace(
163  size_t* written,
164  char* src,
165  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT NLIB_NONNULL;
166 
167  Base64Decoder() NLIB_NOEXCEPT : rem_(-1) {}
168  ~Base64Decoder() NLIB_NOEXCEPT {}
169  errno_t Init(CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT;
170 
171  errno_t StepDecode(
172  size_t* written,
173  void* dst,
174  size_t dstsize,
175  const char* src,
176  size_t srcsize) NLIB_NOEXCEPT NLIB_NONNULL;
177  template<size_t N>
179  size_t* written,
180  uint8_t (&dst)[N],
181  const char* src,
182  size_t srcsize) NLIB_NOEXCEPT {
183  return StepDecode(written, dst, N, src, srcsize);
184  }
185  errno_t Close(
186  size_t* written,
187  void* dst,
188  size_t dstsize) NLIB_NOEXCEPT NLIB_NONNULL;
189  template<size_t N>
190  errno_t Close(
191  size_t* written,
192  uint8_t (&dst)[N]) NLIB_NOEXCEPT {
193  return Close(written, dst, N);
194  }
195  NLIB_SAFE_BOOL(Base64Decoder, (rem_ >= 0))
196 
197  private:
198  static errno_t Decode_(size_t* written,
199  uint8_t* dst,
200  size_t dstsize,
201  const char* src,
202  size_t srcsize,
203  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT NLIB_NONNULL;
204 
205  int rem_;
206  int8_t buf_[4];
207 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN)
208  char c62_;
209  char c63_;
210 #endif
211  int8_t table_[256];
212 };
213 
214 NLIB_NAMESPACE_END
215 
216 #endif // INCLUDE_NN_NLIB_BASE64_H_
static errno_t Decode(size_t *written, uint8_t(&dst)[N], const char *src, CharOption char_option=BASE64_DEFAULT) noexcept
Runs Decode(written, dst, N, src, srcsize, char_option).
Definition: Base64.h:134
errno_t StepEncode(size_t *written, char(&dst)[N], const void *src, size_t srcsize) noexcept
Runs StepEncode(written, dst, N, src, srcsize).
Definition: Base64.h:77
CharOption
For more information, see Base64Encoder::CharOption.
Definition: Base64.h:108
Specifies &#39;-&#39; for the 62nd character and &#39;_&#39; for the 63rd character.
Definition: Base64.h:15
~Base64Encoder() noexcept
Destructor.
Definition: Base64.h:65
Specifies &#39;!&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:20
Decodes Base64. This class supports various variants of Base64.
Definition: Base64.h:106
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:160
~Base64Decoder() noexcept
Destructor.
Definition: Base64.h:168
static errno_t Encode(char(&dst)[N], const void *src, size_t srcsize, CharOption char_option=BASE64_DEFAULT, bool padding=false) noexcept
Runs Encode(dst, N, src, srcsize, char_option, padding).
Definition: Base64.h:37
static errno_t Decode(UniquePtr< uint8_t[]> &dst, size_t *dstsize, const char *src, CharOption char_option=BASE64_DEFAULT) noexcept
Decodes data in batch.
Definition: Base64.h:143
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:96
Defines that class that is corresponding to std::unique_ptr.
CharOption
Variations for the 62nd and 63rd characters in Base64 can be specified.
Definition: Base64.h:12
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
errno_t Close(size_t *written, char(&dst)[N], bool padding) noexcept
Runs Base64Encoder::Close(written, dst, N, padding).
Definition: Base64.h:92
Specifies &#39;.&#39; for the 62nd character and &#39;_&#39; for the 63rd character.
Definition: Base64.h:19
Encodes Base64. This class supports various variants of Base64.
Definition: Base64.h:10
errno_t StepDecode(size_t *written, uint8_t(&dst)[N], const char *src, size_t srcsize) noexcept
Runs StepDecode(written, dst, N, src, srcsize).
Definition: Base64.h:178
Specifies &#39;_&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:18
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
static size_t GetRequiredSize(size_t srcsize) noexcept
Calculates the size of the memory space required for decoding the data.
Definition: Base64.h:123
static errno_t Decode(size_t *written, uint8_t *dst, size_t dstsize, const char *src, CharOption char_option=BASE64_DEFAULT) noexcept
Decodes data in batch.
Definition: Base64.h:126
static errno_t Encode(UniquePtr< char[]> &dst, const void *src, size_t srcsize, CharOption char_option=BASE64_DEFAULT, bool padding=false) noexcept
Encodes data in batch.
Definition: Base64.h:47
Specifies &#39;+&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:14
Specifies &#39;_&#39; for the 62nd character and &#39;:&#39; for the 63rd character.
Definition: Base64.h:17
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
Base64Decoder() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Base64.h:167
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:76
static size_t GetRequiredSize(size_t srcsize) noexcept
Calculates the size of the memory space required for encoding the data.
Definition: Base64.h:27
Specifies &#39;.&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:16
Base64Encoder() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Base64.h:64
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24