Mercurial > libbase64
diff base64.h @ 24:90760aa2e7ed
base64: support binary data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 26 Mar 2020 08:41:12 +0100 |
parents | 9205a516264d |
children | 3172e43c9b43 |
line wrap: on
line diff
--- a/base64.h Wed Mar 25 16:54:36 2020 +0100 +++ b/base64.h Thu Mar 26 08:41:12 2020 +0100 @@ -34,49 +34,6 @@ #endif /** - * \page Base64 Base64 - * \brief Base64 encoding and decoding. - * - * The C functions does not allocate memory by itself and therefore user must - * repeat the calls to the functions until all data has been processed. - * - * ## Encoding - * - * You can encode like this: - * - * ```c - * const char *data = "hello"; - * char tmpbuf[5]; - * - * // Note: data will be incremented, use a temporary variable if you need to - * // reuse it - * while (b64_encode_step(&data, tmpbuf)) - * printf("%s", tmpbuf); - * - * printf("\n"); - * ``` - * - * ## Decoding - * - * And you can decode like this: - * - * ```c - * const char *data = "aGVsbG8="; - * char tmpbuf[4]; - * - * // Note: data will be incremented, use a temporary variable if you need to - * // reuse it - * while (b64_decode_step(&data, tmpbuf)) - * printf("%s", tmpbuf); - * - * printf("\n"); - * - * if (errno != 0) - * perror("invalid sequence"); - * ``` - */ - -/** * Check if the character is a %base64 character, A-Za-z0-9 and +/. * * \param ch the character to test @@ -156,16 +113,21 @@ * This function will write at most dstsz bytes into the buffer, including the * NUL terminator. * + * If srcsz is 0 then strlen(src) is called to determine the number of bytes in + * src. Note that binary data may contain embedded 0 and therefore specifying + * the source size would be required in that case. + * * \pre src != NULL * \pre dst != NULL * \param src the source to encode + * \param srcsz the source size or 0 to read until NUL terminator * \param dst the destination buffer * \param dstsz the destination size * \return The number of bytes written or -1 on error and sets errno. * \see \ref b64_encode_length */ size_t -b64_encode(const char src[], char dst[], size_t dstsz); +b64_encode(const char src[], size_t srcsz, char dst[], size_t dstsz); /** * Decode the given src into the destination buffer. @@ -173,16 +135,20 @@ * This function will write at most dstsz bytes into the buffer, including the * NUL terminator. * + * If srcsz is 0 then strlen(src) is called to determine the number of bytes in + * src. + * * \pre src != NULL * \pre dst != NULL * \param src the source to encode + * \param srcsz the source size or 0 to read until NUL terminator * \param dst the destination buffer * \param dstsz the destination size * \return The number of bytes written or -1 on error and sets errno. * \see \ref b64_decode_length */ size_t -b64_decode(const char src[], char dst[], size_t dstsz); +b64_decode(const char src[], size_t srcsz, char dst[], size_t dstsz); #if defined(__cplusplus) }