11cb0ef41Sopenharmony_ci#ifndef SRC_BASE64_H_
21cb0ef41Sopenharmony_ci#define SRC_BASE64_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "util.h"
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <cmath>
91cb0ef41Sopenharmony_ci#include <cstddef>
101cb0ef41Sopenharmony_ci#include <cstdint>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace node {
131cb0ef41Sopenharmony_ci//// Base 64 ////
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cienum class Base64Mode {
161cb0ef41Sopenharmony_ci  NORMAL,
171cb0ef41Sopenharmony_ci  URL
181cb0ef41Sopenharmony_ci};
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cistatic inline constexpr size_t base64_encoded_size(
211cb0ef41Sopenharmony_ci    size_t size,
221cb0ef41Sopenharmony_ci    Base64Mode mode = Base64Mode::NORMAL) {
231cb0ef41Sopenharmony_ci  return mode == Base64Mode::NORMAL ? ((size + 2) / 3 * 4)
241cb0ef41Sopenharmony_ci                                    : static_cast<size_t>(std::ceil(
251cb0ef41Sopenharmony_ci                                          static_cast<double>(size * 4) / 3));
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci// Doesn't check for padding at the end.  Can be 1-2 bytes over.
291cb0ef41Sopenharmony_cistatic inline constexpr size_t base64_decoded_size_fast(size_t size) {
301cb0ef41Sopenharmony_ci  // 1-byte input cannot be decoded
311cb0ef41Sopenharmony_ci  return size > 1 ? (size / 4) * 3 + (size % 4 + 1) / 2 : 0;
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciinline uint32_t ReadUint32BE(const unsigned char* p);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_citemplate <typename TypeName>
371cb0ef41Sopenharmony_cisize_t base64_decoded_size(const TypeName* src, size_t size);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_citemplate <typename TypeName>
401cb0ef41Sopenharmony_cisize_t base64_decode(char* const dst, const size_t dstlen,
411cb0ef41Sopenharmony_ci                     const TypeName* const src, const size_t srclen);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciinline size_t base64_encode(const char* src,
441cb0ef41Sopenharmony_ci                            size_t slen,
451cb0ef41Sopenharmony_ci                            char* dst,
461cb0ef41Sopenharmony_ci                            size_t dlen,
471cb0ef41Sopenharmony_ci                            Base64Mode mode = Base64Mode::NORMAL);
481cb0ef41Sopenharmony_ci}  // namespace node
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci#endif  // SRC_BASE64_H_
54