phongn commented on code in PR #13166:
URL: https://github.com/apache/trafficserver/pull/13166#discussion_r3284545018


##########
CMakeLists.txt:
##########
@@ -281,6 +281,7 @@ include(CheckOpenSSLIsBoringSSL)
 include(CheckOpenSSLIsQuictls)
 include(CheckOpenSSLIsAwsLc)
 find_package(OpenSSL REQUIRED)
+auto_option(SIMDUTF FEATURE_VAR TS_USE_SIMDUTF PACKAGE_DEPENDS simdutf)

Review Comment:
   Addressed in 0b36457fe — pinned `simdutf >= 7.0.0`, the version that 
introduced both `base64_default_or_url` and the `decode_up_to_bad_char` 
parameter on `base64_to_binary_safe`. `ENABLE_SIMDUTF=AUTO` with an older 
simdutf falls back to the scalar path with a STATUS message; 
`ENABLE_SIMDUTF=ON` hard-errors so an explicit opt-in cannot silently misbehave.



##########
src/tscore/ink_base64.cc:
##########
@@ -163,6 +174,69 @@ ats_base64_decode(const char *inBuffer, size_t 
inBufferSize, unsigned char *outB
   if (length) {
     *length = decodedBytes;
   }
+}
+
+} // namespace
+
+bool
+ats_base64_encode(const unsigned char *inBuffer, size_t inBufferSize, char 
*outBuffer, size_t outBufSize, size_t *length)
+{
+  if (outBufSize < ats_base64_encode_dstlen(inBufferSize)) {
+    return false;
+  }
+
+#if TS_USE_SIMDUTF
+  if (inBufferSize > BASE64_ENCODE_SIMD_THRESHOLD) {
+    size_t written     = simdutf::binary_to_base64(reinterpret_cast<const char 
*>(inBuffer), inBufferSize, outBuffer);
+    outBuffer[written] = '\0';
+    if (length) {
+      *length = written;
+    }
+    return true;
+  }
+#endif
+
+  encode_scalar(inBuffer, inBufferSize, outBuffer, length);
+  return true;
+}
+
+bool
+ats_base64_encode(const char *inBuffer, size_t inBufferSize, char *outBuffer, 
size_t outBufSize, size_t *length)
+{
+  return ats_base64_encode(reinterpret_cast<const unsigned char *>(inBuffer), 
inBufferSize, outBuffer, outBufSize, length);
+}
+
+bool
+ats_base64_decode(const char *inBuffer, size_t inBufferSize, unsigned char 
*outBuffer, size_t outBufSize, size_t *length)
+{
+  if (outBufSize < ats_base64_decode_dstlen(inBufferSize)) {
+    return false;
+  }
+
+#if TS_USE_SIMDUTF
+  if (inBufferSize > BASE64_DECODE_SIMD_THRESHOLD) {
+    // Reserve one byte for the trailing NUL we always emit.
+    size_t out_len = outBufSize - 1;
+    auto   r       = simdutf::base64_to_binary_safe(inBuffer, inBufferSize, 
reinterpret_cast<char *>(outBuffer), out_len,
+                                                    
simdutf::base64_default_or_url, simdutf::last_chunk_handling_options::loose,
+                                                    
/*decode_up_to_bad_char=*/true);

Review Comment:
   Addressed in 0b36457fe by pre-scanning the input with the same 
`printableToSixBit` table the scalar path uses and truncating `inBufferSize` at 
the first non-alphabet byte before either implementation runs. Both paths now 
see the same prefix of alphabet bytes, so `TSBase64Decode` semantics no longer 
depend on build configuration or input size relative to the threshold. The 
legacy scalar stop-at-first-non-alphabet behavior is preserved.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to