moonchen commented on a change in pull request #8736:
URL: https://github.com/apache/trafficserver/pull/8736#discussion_r833753797
##########
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:
If I'm reading this right, you're trying to give a buffer size that is
as small as possible, accounting for the possibility of padding characters at
the end. While I appreciate being frugal with allocation size, I think that
the size savings of possibly one or two bytes may not warrant such an increase
in complexity when computing the buffer size.
--
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]