From 2487aaf56ce8b89071bbd5757d2e6a3aa6ecec33 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 12 Feb 2024 10:08:47 +0200 Subject: Review --- libbutl/base64.cxx | 18 +++++++----------- libbutl/base64.hxx | 10 +++++----- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/libbutl/base64.cxx b/libbutl/base64.cxx index 03191c8..f764aaf 100644 --- a/libbutl/base64.cxx +++ b/libbutl/base64.cxx @@ -16,6 +16,9 @@ namespace butl static const char codes[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + static const char codes_url[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + // base64-encode the data in the iterator range [i, e). Write the encoded // data starting at the iterator position o. If url is true, encode using // base64url. @@ -49,9 +52,6 @@ namespace butl i4 = c & 0x3F; } - // @@ TMP Lots of redundant branches. Would making it a template - // parameter help? - // if (!url) { if (n && n % 19 == 0) @@ -67,15 +67,11 @@ namespace butl // else { - auto code = [] (size_t i) - { - return i == 62 ? '-' : i == 63 ? '_' : codes[i]; - }; - *o++ = code (i1); - *o++ = code (i2); - if (i3 != un) *o++ = code (i3); - if (i4 != un) *o++ = code (i4); + *o++ = codes_url[i1]; + *o++ = codes_url[i2]; + if (i3 != un) *o++ = codes_url[i3]; + if (i4 != un) *o++ = codes_url[i4]; } } } diff --git a/libbutl/base64.hxx b/libbutl/base64.hxx index 6f8ef02..a0d1450 100644 --- a/libbutl/base64.hxx +++ b/libbutl/base64.hxx @@ -27,13 +27,13 @@ namespace butl LIBBUTL_SYMEXPORT std::string base64_encode (const std::vector&); - // Encode a stream or a buffer using base64url (RFC 4648), a base64 variant + // Encode a stream or a buffer using base64url (RFC4648), a base64 variant // with different 62nd and 63rd alphabet characters (- and _ instead of ~ - // and .) to make it filesystem safe, and optional padding because the + // and .; to make it filesystem safe) and optional padding because the // padding character `=` would have to be percent-encoded to be safe in - // URLs. This implementation does not output any padding, newlines or any - // other whitespace in order to conform to RFC7519: JSON Web Token (JWT) and - // RFC7515: JSON Web Signature (JWS). + // URLs. This implementation does not output any padding, newlines or any + // other whitespace (which is required, for example, by RFC7519: JSON Web + // Token (JWT) and RFC7515: JSON Web Signature (JWS)). // // Note that base64url decoding has not yet been implemented. // -- cgit v1.1