apps/netutils/codecs/base64.c: Remove the big stack array
This commit is contained in:
parent
dc54f28ff3
commit
4d2f580568
@ -68,14 +68,14 @@ extern "C"
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifdef CONFIG_CODECS_BASE64
|
#ifdef CONFIG_CODECS_BASE64
|
||||||
unsigned char *base64_encode(const unsigned char *src, size_t len,
|
FAR void *base64_encode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
unsigned char *dst, size_t *out_len);
|
FAR size_t *out_len);
|
||||||
unsigned char *base64_decode(const unsigned char *src, size_t len,
|
FAR void *base64_decode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
unsigned char *dst, size_t *out_len);
|
FAR size_t *out_len);
|
||||||
unsigned char *base64w_encode(const unsigned char *src, size_t len,
|
FAR void *base64w_encode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
unsigned char *dst, size_t *out_len);
|
FAR size_t *out_len);
|
||||||
unsigned char *base64w_decode(const unsigned char *src, size_t len,
|
FAR void *base64w_decode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
unsigned char *dst, size_t *out_len);
|
FAR size_t *out_len);
|
||||||
#endif /* CONFIG_CODECS_BASE64 */
|
#endif /* CONFIG_CODECS_BASE64 */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@ -69,22 +69,14 @@
|
|||||||
* Name: base64_tab
|
* Name: base64_tab
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static void base64_tab(unsigned char *tab, size_t len, bool websafe)
|
static FAR const char *base64_tab(bool websafe)
|
||||||
{
|
{
|
||||||
static FAR const char *_tab =
|
static FAR const char *_tab =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
static FAR const char *_tab_w =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__";
|
||||||
|
|
||||||
memset(tab, 0, len);
|
return websafe ? _tab_w : _tab;
|
||||||
if (len >= 64)
|
|
||||||
{
|
|
||||||
memcpy(tab, _tab, 64);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (websafe)
|
|
||||||
{
|
|
||||||
tab[62] = '-';
|
|
||||||
tab[63] = '_';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -108,15 +100,15 @@ static void base64_tab(unsigned char *tab, size_t len, bool websafe)
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static unsigned char *_base64_encode(FAR const unsigned char *src,
|
static FAR char *_base64_encode(FAR const char *src, size_t len,
|
||||||
size_t len, FAR unsigned char *dst,
|
FAR char *dst, FAR size_t *out_len,
|
||||||
FAR size_t *out_len, bool websafe)
|
bool websafe)
|
||||||
{
|
{
|
||||||
FAR unsigned char *out;
|
FAR char *out;
|
||||||
FAR unsigned char *pos;
|
FAR char *pos;
|
||||||
FAR const unsigned char *end;
|
FAR const char *end;
|
||||||
FAR const unsigned char *in;
|
FAR const char *in;
|
||||||
unsigned char base64_table[64];
|
FAR const char *base64_table;
|
||||||
size_t olen;
|
size_t olen;
|
||||||
char ch = '=';
|
char ch = '=';
|
||||||
|
|
||||||
@ -125,8 +117,8 @@ static unsigned char *_base64_encode(FAR const unsigned char *src,
|
|||||||
ch = '.';
|
ch = '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
base64_tab(base64_table, sizeof(base64_table), websafe);
|
base64_table = base64_tab(websafe);
|
||||||
olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
|
olen = (len + 2) / 3 * 4 + 1; /* 3-byte blocks to 4-byte */
|
||||||
|
|
||||||
end = src + len;
|
end = src + len;
|
||||||
in = src;
|
in = src;
|
||||||
@ -198,19 +190,16 @@ static unsigned char *_base64_encode(FAR const unsigned char *src,
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static unsigned char *_base64_decode(FAR const unsigned char *src,
|
static char *_base64_decode(FAR const char *src, size_t len, FAR char *dst,
|
||||||
size_t len, FAR unsigned char *dst,
|
|
||||||
FAR size_t *out_len, bool websafe)
|
FAR size_t *out_len, bool websafe)
|
||||||
{
|
{
|
||||||
unsigned char dtable[256];
|
FAR char *out;
|
||||||
FAR unsigned char *out;
|
FAR char *pos;
|
||||||
FAR unsigned char *pos;
|
FAR char block[4];
|
||||||
unsigned char in[4];
|
FAR char *tmp;
|
||||||
unsigned char block[4];
|
FAR const char *base64_table;
|
||||||
unsigned char tmp;
|
|
||||||
size_t count;
|
size_t count;
|
||||||
size_t i;
|
size_t i;
|
||||||
unsigned char base64_table[64];
|
|
||||||
char ch = '=';
|
char ch = '=';
|
||||||
|
|
||||||
if (websafe)
|
if (websafe)
|
||||||
@ -218,29 +207,7 @@ static unsigned char *_base64_decode(FAR const unsigned char *src,
|
|||||||
ch = '.';
|
ch = '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
base64_tab(base64_table, sizeof(base64_table), websafe);
|
base64_table = base64_tab(websafe);
|
||||||
|
|
||||||
memset(dtable, 0x80, 256);
|
|
||||||
for (i = 0; i < sizeof(base64_table); i++)
|
|
||||||
{
|
|
||||||
dtable[base64_table[i]] = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
dtable[(int)ch] = 0; /* dtable['='] = 0; */
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
for (i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (dtable[src[i]] != 0x80)
|
|
||||||
{
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count % 4)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dst)
|
if (dst)
|
||||||
{
|
{
|
||||||
@ -248,7 +215,7 @@ static unsigned char *_base64_decode(FAR const unsigned char *src,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pos = out = malloc(count);
|
pos = out = malloc(len / 4 * 3);
|
||||||
if (out == NULL)
|
if (out == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -258,26 +225,20 @@ static unsigned char *_base64_decode(FAR const unsigned char *src,
|
|||||||
count = 0;
|
count = 0;
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
tmp = dtable[src[i]];
|
tmp = strchr(base64_table, src[i]);
|
||||||
if (tmp == 0x80)
|
block[count] = tmp ? tmp - base64_table : 0;
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
in[count] = src[i];
|
|
||||||
block[count] = tmp;
|
|
||||||
count++;
|
count++;
|
||||||
|
|
||||||
if (count == 4)
|
if (count == 4)
|
||||||
{
|
{
|
||||||
*pos++ = (block[0] << 2) | (block[1] >> 4);
|
*pos++ = (block[0] << 2) | (block[1] >> 4);
|
||||||
if (in[2] == ch) /* if (in[2] == '=') */
|
if (src[i-1] == ch)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
*pos++ = (block[1] << 4) | (block[2] >> 2);
|
*pos++ = (block[1] << 4) | (block[2] >> 2);
|
||||||
if (in[3] == ch) /* if (in[3] == '=') */
|
if (src[i] == ch)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -299,8 +260,8 @@ static unsigned char *_base64_decode(FAR const unsigned char *src,
|
|||||||
* Name: base64_encode
|
* Name: base64_encode
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
unsigned char *base64_encode(FAR const unsigned char *src, size_t len,
|
FAR void *base64_encode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
FAR unsigned char *dst, FAR size_t *out_len)
|
FAR size_t *out_len)
|
||||||
{
|
{
|
||||||
return _base64_encode(src, len, dst, out_len, false);
|
return _base64_encode(src, len, dst, out_len, false);
|
||||||
}
|
}
|
||||||
@ -309,8 +270,8 @@ unsigned char *base64_encode(FAR const unsigned char *src, size_t len,
|
|||||||
* Name: base64_decode
|
* Name: base64_decode
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
unsigned char *base64_decode(FAR const unsigned char *src, size_t len,
|
FAR void *base64_decode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
FAR unsigned char *dst, FAR size_t *out_len)
|
FAR size_t *out_len)
|
||||||
{
|
{
|
||||||
return _base64_decode(src, len, dst, out_len, false);
|
return _base64_decode(src, len, dst, out_len, false);
|
||||||
}
|
}
|
||||||
@ -319,8 +280,8 @@ unsigned char *base64_decode(FAR const unsigned char *src, size_t len,
|
|||||||
* Name: base64w_encode
|
* Name: base64w_encode
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
unsigned char *base64w_encode(FAR const unsigned char *src, size_t len,
|
FAR void *base64w_encode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
FAR unsigned char *dst, FAR size_t *out_len)
|
FAR size_t *out_len)
|
||||||
{
|
{
|
||||||
return _base64_encode(src, len, dst, out_len, true);
|
return _base64_encode(src, len, dst, out_len, true);
|
||||||
}
|
}
|
||||||
@ -329,8 +290,8 @@ unsigned char *base64w_encode(FAR const unsigned char *src, size_t len,
|
|||||||
* Name: base64w_decode
|
* Name: base64w_decode
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
unsigned char *base64w_decode(FAR const unsigned char *src, size_t len,
|
FAR void *base64w_decode(FAR const void *src, size_t len, FAR void *dst,
|
||||||
FAR unsigned char *dst, FAR size_t *out_len)
|
FAR size_t *out_len)
|
||||||
{
|
{
|
||||||
return _base64_decode(src, len, dst, out_len, true);
|
return _base64_decode(src, len, dst, out_len, true);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user