Skip to content
Snippets Groups Projects
Commit d591c1b6 authored by Matt O'Gorman's avatar Matt O'Gorman
Browse files

changes for base64 to work in multiline instances

as well as being more efficient, patch from jcollie's
base64 branch


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@31492 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent fa339925
No related branches found
No related tags found
No related merge requests found
...@@ -157,6 +157,7 @@ void ast_md5_hash(char *output, char *input); ...@@ -157,6 +157,7 @@ void ast_md5_hash(char *output, char *input);
\brief Produces SHA1 hash based on input string */ \brief Produces SHA1 hash based on input string */
void ast_sha1_hash(char *output, char *input); void ast_sha1_hash(char *output, char *input);
int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks);
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max); int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max);
int ast_base64decode(unsigned char *dst, const char *src, int max); int ast_base64decode(unsigned char *dst, const char *src, int max);
......
...@@ -324,17 +324,11 @@ int ast_base64decode(unsigned char *dst, const char *src, int max) ...@@ -324,17 +324,11 @@ int ast_base64decode(unsigned char *dst, const char *src, int max)
unsigned int byte = 0; unsigned int byte = 0;
unsigned int bits = 0; unsigned int bits = 0;
int incnt = 0; int incnt = 0;
#if 0
unsigned char *odst = dst;
#endif
while(*src && (cnt < max)) { while(*src && (cnt < max)) {
/* Shift in 6 bits of input */ /* Shift in 6 bits of input */
byte <<= 6; byte <<= 6;
byte |= (b2a[(int)(*src)]) & 0x3f; byte |= (b2a[(int)(*src)]) & 0x3f;
bits += 6; bits += 6;
#if 0
printf("Add: %c %s\n", *src, binary(b2a[(int)(*src)] & 0x3f, 6));
#endif
src++; src++;
incnt++; incnt++;
/* If we have at least 8 bits left over, take that character /* If we have at least 8 bits left over, take that character
...@@ -342,66 +336,71 @@ int ast_base64decode(unsigned char *dst, const char *src, int max) ...@@ -342,66 +336,71 @@ int ast_base64decode(unsigned char *dst, const char *src, int max)
if (bits >= 8) { if (bits >= 8) {
bits -= 8; bits -= 8;
*dst = (byte >> bits) & 0xff; *dst = (byte >> bits) & 0xff;
#if 0
printf("Remove: %02x %s\n", *dst, binary(*dst, 8));
#endif
dst++; dst++;
cnt++; cnt++;
} }
} }
#if 0
dump(odst, cnt);
#endif
/* Dont worry about left over bits, they're extra anyway */ /* Dont worry about left over bits, they're extra anyway */
return cnt; return cnt;
} }
/*! \brief encode text to BASE64 coding */ /*! \brief encode text to BASE64 coding */
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max) int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
{ {
int cnt = 0; int cnt = 0;
int col = 0;
unsigned int byte = 0; unsigned int byte = 0;
int bits = 0; int bits = 0;
int index;
int cntin = 0; int cntin = 0;
#if 0 /* Reserve space for null byte at end of string */
char *odst = dst;
dump(src, srclen);
#endif
/* Reserve one bit for end */
max--; max--;
while((cntin < srclen) && (cnt < max)) { while((cntin < srclen) && (cnt < max)) {
byte <<= 8; byte <<= 8;
#if 0
printf("Add: %02x %s\n", *src, binary(*src, 8));
#endif
byte |= *(src++); byte |= *(src++);
bits += 8; bits += 8;
cntin++; cntin++;
while((bits >= 6) && (cnt < max)) { if ((bits == 24) && (cnt + 4 < max)) {
bits -= 6; *dst++ = base64[(byte >> 18) & 0x3f];
/* We want only the top */ *dst++ = base64[(byte >> 12) & 0x3f];
index = (byte >> bits) & 0x3f; *dst++ = base64[(byte >> 6) & 0x3f];
*dst = base64[index]; *dst++ = base64[byte & 0x3f];
#if 0 cnt += 4;
printf("Remove: %c %s\n", *dst, binary(index, 6)); col += 4;
#endif bits = 0;
dst++; byte = 0;
}
if (linebreaks && (cnt < max) && (col == 64)) {
*dst++ = '\n';
cnt++; cnt++;
col = 0;
} }
} }
if (bits && (cnt < max)) { if (bits && (cnt + 4 < max)) {
/* Add one last character for the remaining bits, /* Add one last character for the remaining bits,
padding the rest with 0 */ padding the rest with 0 */
byte <<= (6 - bits); byte <<= 24 - bits;
index = (byte) & 0x3f; *dst++ = base64[(byte >> 18) & 0x3f];
*(dst++) = base64[index]; *dst++ = base64[(byte >> 12) & 0x3f];
if (bits == 16)
*dst++ = base64[(byte >> 6) & 0x3f];
else
*dst++ = '=';
*dst++ = '=';
cnt += 4;
}
if (linebreaks && (cnt < max)) {
*dst++ = '\n';
cnt++; cnt++;
} }
*dst = '\0'; *dst = '\0';
return cnt; return cnt;
} }
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
{
return ast_base64encode_full(dst, src, srclen, max, 0);
}
static void base64_init(void) static void base64_init(void)
{ {
int x; int x;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment