> On 19 Jan 2017, at 15:10, Graham Leggett <minf...@sharp.fm> wrote: > > On 19 Jan 2017, at 3:22 PM, Dirk-Willem van Gulik <di...@webweaving.org> > wrote: > >> Any reason we do not have such in APR-2 (as a compagnion to >> apr_pbase64_encode) ? >> >> Dw. >> >> >> >> APR_DECLARE(char *) apr_pbase64_encode_binary(apr_pool_t *p, const unsigned >> char *string, int len) >> { >> char *encoded; >> >> encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(len)); >> len = apr_base64_encode_binary(encoded, string, len); >> encoded[len] = '\0'; /* make binary sequence into string */ >> >> return encoded; >> } > > Ideally we should have a base64 variant here, and deprecate the existing > functions: > > http://apr.apache.org/docs/apr/1.5/group___a_p_r___util___escaping.html
Would you terribly mind me popping below into the tree ? Or is it better to move it to escaping ? Dw. Index: include/apr_base64.h =================================================================== --- include/apr_base64.h (revision 1779018) +++ include/apr_base64.h (working copy) @@ -97,6 +97,18 @@ __attribute__((nonnull(1,2))); /** + * Encode a binary blob into memory allocated from a pool in base 64 format + * @param p The pool to allocate from + * @param plain_src The 'plain' binary data + * @param len_plain_src THe length, in bytes, of the binary data. + * @return The encoded string. + */ +APR_DECLARE(char *) apr_pbase64_encode_binary(apr_pool_t *p, + const unsigned char *plain_src, + int len_plain_src) + __attribute__((nonnull(1,2))); + +/** * Determine the maximum buffer length required to decode the plain text * string given the encoded string. * @param coded_src The encoded string Index: encoding/apr_base64.c =================================================================== --- encoding/apr_base64.c (revision 1779018) +++ encoding/apr_base64.c (working copy) @@ -282,12 +282,18 @@ APR_DECLARE(char *) apr_pbase64_encode(apr_pool_t *p, const char *string) { + return apr_pbase64_encode_binary(p, (const unsigned char *)string, strlen(string)); +} + +APR_DECLARE(char *) apr_pbase64_encode_binary(apr_pool_t *p, const unsigned char *string, int len) +{ char *encoded; - int l = strlen(string); - encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(l)); - l = apr_base64_encode(encoded, string, l); - encoded[l] = '\0'; /* make binary sequence into string */ + encoded = (char *) apr_palloc(p, 1 + apr_base64_encode_len(len)); + len = apr_base64_encode_binary(encoded, string, len); + encoded[len] = '\0'; /* make binary sequence into string */ return encoded; } + + Index: test/testbase64.c =================================================================== --- test/testbase64.c (revision 1779018) +++ test/testbase64.c (working copy) @@ -70,6 +70,10 @@ enc = apr_pbase64_encode(pool, base64_tbl[i].orig); ABTS_ASSERT(tc, "base 64 encoded from pool matches expected output", (strcmp(enc, base64_tbl[i].enc) == 0)) + + enc = apr_pbase64_encode_binary(pool, (unsigned char *)base64_tbl[i].orig, strlen(base64_tbl[i].orig)); + ABTS_ASSERT(tc, "base 64 encoded from pool matches expected output", + (strcmp(enc, base64_tbl[i].enc) == 0)) } }
signature.asc
Description: Message signed with OpenPGP using GPGMail