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