ywkaras commented on a change in pull request #8736:
URL: https://github.com/apache/trafficserver/pull/8736#discussion_r832638231



##########
File path: include/tscore/ink_base64.h
##########
@@ -42,5 +42,16 @@ bool ats_base64_encode(const unsigned char *inBuffer, size_t 
inBufferSize, char
 bool ats_base64_decode(const char *inBuffer, size_t inBufferSize, unsigned 
char *outBuffer, size_t outBufSize, size_t *length);
 
 // Little helper functions to calculate minimum required output buffer for 
encoding/decoding.
-#define ATS_BASE64_ENCODE_DSTLEN(_length) ((_length * 8) / 6 + 4)
-#define ATS_BASE64_DECODE_DSTLEN(_length) (((_length + 3) / 4) * 3)
+// These sizes include one byte for null termination, because 
ats_base64_encode and ats_base64_decode will always write a null
+// terminator.
+inline constexpr size_t
+ats_base64_encode_dstlen(size_t length)
+{
+  return ((length + 2) / 3) * 4 + 1;
+}
+
+inline constexpr size_t
+ats_base64_decode_dstlen(size_t length)
+{
+  return ((length + 3) / 4) * 3 + 1;
+}

Review comment:
       Why not?
   ```
   inline constexpr size_t
   ats_base64_encode_dstlen(size_t length, bool pad = true)
   {
     size_t num_bit_pairs = 4 * length;
     return (pad ? (((num_bit_pairs + 11) / 12) * 4) : ((num_bit_pairs + 2) / 
3)) + 1;
   }
   
   inline constexpr size_t
   ats_base64_unpadded_decode_dstlen(size_t length)
   {
     return (((length / 4) * 3) + (length % 4)) + 1;
   }
   
   inline size_t
   ats_base64_padded_decode_dstlen(char const *data, size_t length)
   {
     ink_assert(!(length % 4));
   
     if (length && ('=' == data[length - 1])) {
       --length;
     }
     if (length && ('=' == data[length - 1])) {
       --length;
     }
   
     return ats_base64_unpadded_decode_dstlen(length);
   }
   ```




-- 
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