nlib
Base64.h
Go to the documentation of this file.
1 
2 /*---------------------------------------------------------------------------*
3 
4  Project: CrossRoad
5  Copyright (C)2012-2016 Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12 
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  BASE64_PLUS_SLASH = 0,
34  _BASE64_MAX,
35  BASE64_DEFAULT = BASE64_PLUS_SLASH,
36  BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
37  };
38 
39  public:
40  static size_t GetRequiredSize(size_t srcsize) NLIB_NOEXCEPT {
41  return ((srcsize + 2) / 3) * 4 + 1;
42  }
43  static errno_t Encode(char* dst,
44  size_t dstsize,
45  const void* src,
46  size_t srcsize,
47  CharOption char_option = BASE64_DEFAULT,
48  bool padding = false) NLIB_NOEXCEPT NLIB_NONNULL;
49  template<size_t N>
50  static errno_t Encode(char (&dst)[N],
51  const void* src,
52  size_t srcsize,
53  CharOption char_option = BASE64_DEFAULT,
54  bool padding = false) NLIB_NOEXCEPT {
55  return Encode(dst, N, src, srcsize, char_option, padding);
56  }
57 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
58  template<class DUMMY = void>
59 #endif
60  static errno_t Encode(UniquePtr<char[]>& dst, // NOLINT
61  const void* src,
62  size_t srcsize,
63  CharOption char_option = BASE64_DEFAULT,
64  bool padding = false) NLIB_NOEXCEPT {
65  // to avoid crossing the DLL boundary
66  size_t dstsize = GetRequiredSize(srcsize);
67  // do not use UniquePtr to avoid C4714
68  char* p = new (std::nothrow) char[dstsize];
69  if (!p) return ENOMEM;
70  errno_t e = Encode(p, dstsize, src, srcsize, char_option, padding);
71  if (e == 0)
72  dst.reset(p);
73  else
74  delete[] p;
75  return e;
76  }
77  Base64Encoder() NLIB_NOEXCEPT : rem_(-1) {}
79  errno_t Init(CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT;
80 
81  // note that dst is NOT null terminated
82  errno_t StepEncode(
83  size_t* written,
84  char* dst,
85  size_t dstsize,
86  const void* src,
87  size_t srcsize) NLIB_NOEXCEPT NLIB_NONNULL;
88  // note that dst is NOT null terminated
89  template<size_t N>
91  size_t* written,
92  char (&dst)[N],
93  const void* src,
94  size_t srcsize) NLIB_NOEXCEPT {
95  return StepEncode(written, dst, N, src, srcsize);
96  }
97  // note that dst is NOT null terminated
98  errno_t Close(
99  size_t* written,
100  char* dst,
101  size_t dstsize,
102  bool padding) NLIB_NOEXCEPT NLIB_NONNULL;
103  // note that dst is NOT null terminated
104  template<size_t N>
106  size_t* written,
107  char(&dst)[N],
108  bool padding) NLIB_NOEXCEPT {
109  return Close(written, dst, N, padding);
110  }
111  NLIB_SAFE_BOOL(Base64Encoder, (rem_ >= 0))
112 
113  private:
114  int rem_;
115  uint8_t buf_[2];
116  char table_[64];
117 };
118 
120  public:
121  enum CharOption {
122  BASE64_PLUS_SLASH = 0,
123  BASE64_PLUS_MINUS,
124  BASE64_MINUS_UNDERSCORE,
125  BASE64_DOT_MINUS,
126  BASE64_UNDERSCORE_COLON,
127  BASE64_UNDERSCORE_MINUS,
128  BASE64_DOT_UNDERSCORE,
129  BASE64_EXCLAMATION_MINUS,
130  _BASE64_MAX,
131  BASE64_DEFAULT = BASE64_PLUS_SLASH,
132  BASE64_URL_SAFE = BASE64_MINUS_UNDERSCORE
133  };
134 
135  public:
136  static size_t GetRequiredSize(size_t srcsize) NLIB_NOEXCEPT {
137  return ((srcsize + 3) / 4) * 3;
138  }
139  static errno_t Decode(size_t* written,
140  uint8_t* dst,
141  size_t dstsize,
142  const char* src,
143  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
144  return Decode_(written, dst, dstsize, src, nlib_strlen(src), char_option);
145  }
146  template<size_t N>
147  static errno_t Decode(size_t* written,
148  uint8_t (&dst)[N],
149  const char* src,
150  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
151  return Decode_(written, dst, N, src, nlib_strlen(src), char_option);
152  }
153 #ifdef NLIB_CXX11_DEFAULT_TEMPLATE_ARGUMENT_FOR_FUNCTION_TEMPLATES
154  template<class DUMMY = void>
155 #endif
156  static errno_t Decode(UniquePtr<uint8_t[]>& dst, // NOLINT
157  size_t* dstsize, const char* src,
158  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT {
159  // to avoid crossing the DLL boundary
160  size_t srcsize = nlib_strlen(src);
161  size_t dstsize_ = GetRequiredSize(srcsize);
162  // do not use UniquePtr to avoid C4714
163  uint8_t* p = new (std::nothrow) uint8_t[dstsize_];
164  if (!p) return ENOMEM;
165  size_t written;
166  errno_t e = Decode_(&written, p, dstsize_, src, srcsize, char_option);
167  if (e == 0) {
168  dst.reset(p);
169  *dstsize = written;
170  } else {
171  delete[] p;
172  }
173  return e;
174  }
175  static errno_t DecodeInplace(
176  size_t* written,
177  char* src,
178  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT NLIB_NONNULL;
179 
180  Base64Decoder() NLIB_NOEXCEPT : rem_(-1) {}
181  ~Base64Decoder() NLIB_NOEXCEPT {}
182  errno_t Init(CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT;
183 
184  errno_t StepDecode(
185  size_t* written,
186  void* dst,
187  size_t dstsize,
188  const char* src,
189  size_t srcsize) NLIB_NOEXCEPT NLIB_NONNULL;
190  template<size_t N>
192  size_t* written,
193  uint8_t (&dst)[N],
194  const char* src,
195  size_t srcsize) NLIB_NOEXCEPT {
196  return StepDecode(written, dst, N, src, srcsize);
197  }
198  errno_t Close(
199  size_t* written,
200  void* dst,
201  size_t dstsize) NLIB_NOEXCEPT NLIB_NONNULL;
202  template<size_t N>
203  errno_t Close(
204  size_t* written,
205  uint8_t (&dst)[N]) NLIB_NOEXCEPT {
206  return Close(written, dst, N);
207  }
208  NLIB_SAFE_BOOL(Base64Decoder, (rem_ >= 0))
209 
210  private:
211  static errno_t Decode_(size_t* written,
212  uint8_t* dst,
213  size_t dstsize,
214  const char* src,
215  size_t srcsize,
216  CharOption char_option = BASE64_DEFAULT) NLIB_NOEXCEPT NLIB_NONNULL;
217 
218  int rem_;
219  int8_t buf_[4];
220 #if defined(NLIB_SIMD) && defined(NLIB_LITTLE_ENDIAN)
221  char c62_;
222  char c63_;
223 #endif
224  int8_t table_[256];
225 };
226 
227 NLIB_NAMESPACE_END
228 
229 #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:147
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:90
CharOption
For more information, see Base64Encoder::CharOption.
Definition: Base64.h:121
Specifies &#39;-&#39; for the 62nd character and &#39;_&#39; for the 63rd character.
Definition: Base64.h:28
~Base64Encoder() noexcept
Destructor.
Definition: Base64.h:78
Specifies &#39;!&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:33
Decodes Base64. This class supports various variants of Base64.
Definition: Base64.h:119
#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:173
~Base64Decoder() noexcept
Destructor.
Definition: Base64.h:181
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:50
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:156
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:87
errno_t Close(size_t *written, char(&dst)[N], bool padding) noexcept
Runs Base64Encoder::Close(written, dst, N, padding).
Definition: Base64.h:105
Specifies &#39;.&#39; for the 62nd character and &#39;_&#39; for the 63rd character.
Definition: Base64.h:32
Encodes Base64. This class supports various variants of Base64.
Definition: Base64.h:23
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:191
Specifies &#39;_&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:31
#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:136
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:139
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:60
Specifies &#39;+&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:27
Specifies &#39;_&#39; for the 62nd character and &#39;:&#39; for the 63rd character.
Definition: Base64.h:30
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:224
Base64Decoder() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Base64.h:180
#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:40
Specifies &#39;.&#39; for the 62nd character and &#39;-&#39; for the 63rd character.
Definition: Base64.h:29
Base64Encoder() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Base64.h:77
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37