Add a packed argument to encode_base64.

Reintroduced the '=' padding modulo 3 characters from the original code from the mail kit,
and a packed option to avoid it.
Broadway uses this packed scheme to save bytes when sending many separate values.
TODO: merge this with the mail kit one somewhere?
This commit is contained in:
François Revol 2012-04-02 00:30:27 +02:00
parent 25f1f96111
commit c001665a7c
2 changed files with 7 additions and 2 deletions

View File

@ -25,7 +25,7 @@ static const char kHexAlphabet[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
ssize_t
encode_base64(char *out, const char *in, off_t length)
encode_base64(char *out, const char *in, off_t length, bool packed)
{
uint32 concat;
int i = 0;
@ -45,8 +45,12 @@ encode_base64(char *out, const char *in, off_t length)
out[k++] = kBase64Alphabet[(concat >> 12) & 63];
if ((i+1) < length)
out[k++] = kBase64Alphabet[(concat >> 6) & 63];
else if (!packed)
out[k++] = '=';
if ((i+2) < length)
out[k++] = kBase64Alphabet[concat & 63];
else if (!packed)
out[k++] = '=';
}
return k;

View File

@ -8,7 +8,8 @@
#include <ctype.h>
extern ssize_t encode_base64(char *out, const char *in, off_t length);
extern ssize_t encode_base64(char *out, const char *in, off_t length,
bool packed=false);
extern ssize_t decode_base64(char *out, const char *in, off_t length);