Skip to content

Commit

Permalink
src: move all base64.h inline methods into -inl.h header file
Browse files Browse the repository at this point in the history
Refs: #34944 (comment)

PR-URL: #35432
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
addaleax authored and nodejs-github-bot committed Oct 3, 2020
1 parent a8556da commit fdf0a84
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 77 deletions.
85 changes: 85 additions & 0 deletions src/base64-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "base64.h"
#include "util.h"

namespace node {
Expand Down Expand Up @@ -91,6 +92,90 @@ size_t base64_decode_fast(char* const dst, const size_t dstlen,
return k;
}


template <typename TypeName>
size_t base64_decoded_size(const TypeName* src, size_t size) {
// 1-byte input cannot be decoded
if (size < 2)
return 0;

if (src[size - 1] == '=') {
size--;
if (src[size - 1] == '=')
size--;
}
return base64_decoded_size_fast(size);
}


template <typename TypeName>
size_t base64_decode(char* const dst, const size_t dstlen,
const TypeName* const src, const size_t srclen) {
const size_t decoded_size = base64_decoded_size(src, srclen);
return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
}


inline size_t base64_encode(const char* src,
size_t slen,
char* dst,
size_t dlen) {
// We know how much we'll write, just make sure that there's space.
CHECK(dlen >= base64_encoded_size(slen) &&
"not enough space provided for base64 encode");

dlen = base64_encoded_size(slen);

unsigned a;
unsigned b;
unsigned c;
unsigned i;
unsigned k;
unsigned n;

static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

i = 0;
k = 0;
n = slen / 3 * 3;

while (i < n) {
a = src[i + 0] & 0xff;
b = src[i + 1] & 0xff;
c = src[i + 2] & 0xff;

dst[k + 0] = table[a >> 2];
dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
dst[k + 3] = table[c & 0x3f];

i += 3;
k += 4;
}

switch (slen - n) {
case 1:
a = src[i + 0] & 0xff;
dst[k + 0] = table[a >> 2];
dst[k + 1] = table[(a & 3) << 4];
dst[k + 2] = '=';
dst[k + 3] = '=';
break;
case 2:
a = src[i + 0] & 0xff;
b = src[i + 1] & 0xff;
dst[k + 0] = table[a >> 2];
dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
dst[k + 2] = table[(b & 0x0f) << 2];
dst[k + 3] = '=';
break;
}

return dlen;
}

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
78 changes: 4 additions & 74 deletions src/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "util.h"
#include "base64-inl.h"

#include <cstddef>
#include <cstdint>
Expand All @@ -24,85 +23,16 @@ static inline constexpr size_t base64_decoded_size_fast(size_t size) {
inline uint32_t ReadUint32BE(const unsigned char* p);

template <typename TypeName>
size_t base64_decoded_size(const TypeName* src, size_t size) {
// 1-byte input cannot be decoded
if (size < 2)
return 0;

if (src[size - 1] == '=') {
size--;
if (src[size - 1] == '=')
size--;
}
return base64_decoded_size_fast(size);
}
size_t base64_decoded_size(const TypeName* src, size_t size);

template <typename TypeName>
size_t base64_decode(char* const dst, const size_t dstlen,
const TypeName* const src, const size_t srclen) {
const size_t decoded_size = base64_decoded_size(src, srclen);
return base64_decode_fast(dst, dstlen, src, srclen, decoded_size);
}
const TypeName* const src, const size_t srclen);

static size_t base64_encode(const char* src,
inline size_t base64_encode(const char* src,
size_t slen,
char* dst,
size_t dlen) {
// We know how much we'll write, just make sure that there's space.
CHECK(dlen >= base64_encoded_size(slen) &&
"not enough space provided for base64 encode");

dlen = base64_encoded_size(slen);

unsigned a;
unsigned b;
unsigned c;
unsigned i;
unsigned k;
unsigned n;

static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

i = 0;
k = 0;
n = slen / 3 * 3;

while (i < n) {
a = src[i + 0] & 0xff;
b = src[i + 1] & 0xff;
c = src[i + 2] & 0xff;

dst[k + 0] = table[a >> 2];
dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)];
dst[k + 3] = table[c & 0x3f];

i += 3;
k += 4;
}

switch (slen - n) {
case 1:
a = src[i + 0] & 0xff;
dst[k + 0] = table[a >> 2];
dst[k + 1] = table[(a & 3) << 4];
dst[k + 2] = '=';
dst[k + 3] = '=';
break;
case 2:
a = src[i + 0] & 0xff;
b = src[i + 1] & 0xff;
dst[k + 0] = table[a >> 2];
dst[k + 1] = table[((a & 3) << 4) | (b >> 4)];
dst[k + 2] = table[(b & 0x0f) << 2];
dst[k + 3] = '=';
break;
}

return dlen;
}
size_t dlen);
} // namespace node


Expand Down
2 changes: 1 addition & 1 deletion src/inspector_socket.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "inspector_socket.h"
#include "llhttp.h"

#include "base64.h"
#include "base64-inl.h"
#include "util-inl.h"

#include "openssl/sha.h" // Sha-1 hash
Expand Down
2 changes: 1 addition & 1 deletion src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "string_bytes.h"

#include "base64.h"
#include "base64-inl.h"
#include "env-inl.h"
#include "node_buffer.h"
#include "node_errors.h"
Expand Down
2 changes: 1 addition & 1 deletion test/cctest/test_base64.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "base64.h"
#include "base64-inl.h"

#include <cstddef>
#include <cstring>
Expand Down

0 comments on commit fdf0a84

Please sign in to comment.