You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Dirk-Willem van Gulik <di...@webweaving.org> on 2017/01/19 13:22:39 UTC

pool based base64 binary encode

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


Re: pool based base64 binary encode

Posted by Dirk-Willem van Gulik <di...@webweaving.org>.
On 22 Jan 2017, at 13:02, bert@qqmail.nl wrote:
> 
> That encoded[len] reference should use the same apr_base64_encode_len result as the first allocation, or the bas64 data is truncated.

I think you are right - but the other way round in this case - I was allocating one byte too many. Fixed in 1780034.

>  Re: followups: I don’t see a reason for deprecating the separate encoder functions, after adding the useful wrapper. There are enough good reasons why applications depending on apr might want to do their own memory management.

Agreed. We want to keep the binary versions as well. 

Dw

RE: pool based base64 binary encode

Posted by be...@qqmail.nl.
That encoded[len] reference should use the same apr_base64_encode_len result as the first allocation, or the bas64 data is truncated.

Re: followups: I don’t see a reason for deprecating the separate encoder functions, after adding the useful wrapper. There are enough good reasons why applications depending on apr might want to do their own memory management.

Bert

Sent from Mail for Windows 10

From: Dirk-Willem van Gulik
Sent: donderdag 19 januari 2017 14:23
To: APR Developer List
Subject: pool based base64 binary encode

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



Re: pool based base64 binary encode

Posted by Dirk-Willem van Gulik <di...@webweaving.org>.
> On 19 Jan 2017, at 15:10, Graham Leggett <mi...@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))
     }
 }



Re: pool based base64 binary encode

Posted by Graham Leggett <mi...@sharp.fm>.
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

Regards,
Graham
—