diff options
author | Graham Leggett <minfrin@apache.org> | 2013-06-06 12:06:29 +0000 |
---|---|---|
committer | Graham Leggett <minfrin@apache.org> | 2013-06-06 12:06:29 +0000 |
commit | d60e2ae582cce99fdbe56c1d6a51e9fc0165d0d9 (patch) | |
tree | 1d9844740138e3362084dfea94fa696fb50a7a65 | |
parent | f94785ad1511276a3d429cdcfe8cc8640d77f4f3 (diff) | |
download | apr-d60e2ae582cce99fdbe56c1d6a51e9fc0165d0d9.tar.gz |
Add apr_pbase64_encode() and apr_pbase64_decode() to encode to/from
the pool.
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1490248 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | encoding/apr_base64.c | 24 | ||||
-rw-r--r-- | include/apr_base64.h | 18 | ||||
-rw-r--r-- | test/testbase64.c | 4 |
4 files changed, 49 insertions, 0 deletions
@@ -1,6 +1,9 @@ -*- coding: utf-8 -*- Changes for APR 2.0.0 + *) Add apr_pbase64_encode() and apr_pbase64_decode() to encode to/from + the pool. [Graham Leggett] + *) Add the apr_escape interface. [Graham Leggett] *) Add support to apr_memcache for unix domain sockets. PR 54573 [Remi diff --git a/encoding/apr_base64.c b/encoding/apr_base64.c index d553631dc..82e6e23d1 100644 --- a/encoding/apr_base64.c +++ b/encoding/apr_base64.c @@ -188,6 +188,18 @@ APR_DECLARE(int) apr_base64_decode_binary(unsigned char *bufplain, return nbytesdecoded; } +APR_DECLARE(char *) apr_pbase64_decode(apr_pool_t *p, const char *bufcoded) +{ + char *decoded; + int l; + + decoded = (char *) apr_palloc(p, 1 + apr_base64_decode_len(bufcoded)); + l = apr_base64_decode(decoded, bufcoded); + decoded[l] = '\0'; /* make binary sequence into string */ + + return decoded; +} + static const char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -267,3 +279,15 @@ APR_DECLARE(int) apr_base64_encode_binary(char *encoded, *p++ = '\0'; return (int)(p - encoded); } + +APR_DECLARE(char *) apr_pbase64_encode(apr_pool_t *p, const char *string) +{ + 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 */ + + return encoded; +} diff --git a/include/apr_base64.h b/include/apr_base64.h index 68b30873f..406ca368e 100644 --- a/include/apr_base64.h +++ b/include/apr_base64.h @@ -88,6 +88,15 @@ APR_DECLARE(int) apr_base64_encode_binary(char * coded_dst, __attribute__((nonnull(1,2))); /** + * Encode a string into memory allocated from a pool in base 64 format + * @param p The pool to allocate from + * @param string The plaintext string + * @return The encoded string + */ +APR_DECLARE(char *) apr_pbase64_encode(apr_pool_t *p, const char *string) + __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 @@ -120,6 +129,15 @@ APR_DECLARE(int) apr_base64_decode_binary(unsigned char * plain_dst, const char *coded_src) __attribute__((nonnull(1,2))); +/** + * Decode a base64 encoded string into memory allocated from a pool + * @param p The pool to allocate from + * @param bufcoded The encoded string + * @return The decoded string + */ +APR_DECLARE(char *) apr_pbase64_decode(apr_pool_t *p, const char *bufcoded) + __attribute__((nonnull(1,2))); + /** @} */ #ifdef __cplusplus } diff --git a/test/testbase64.c b/test/testbase64.c index 31d897f4a..d71b8e929 100644 --- a/test/testbase64.c +++ b/test/testbase64.c @@ -66,6 +66,10 @@ static void test_base64(abts_case *tc, void *data) ABTS_ASSERT(tc, "base 64 encoded length", (b64_enc_len == b64_len)); ABTS_ASSERT(tc, "base 64 encoded matches expected output", (memcmp(enc, base64_tbl[i].enc, b64_enc_len) == 0)); + + 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)) } } |