libbb: change decode_base32/64 API to return the end of _dst_, not _src_.

function                                             old     new   delta
decode_base64                                        173     178      +5
read_base64                                          222     220      -2
decode_base32                                        186     182      -4
handle_incoming_and_exit                            2263    2239     -24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: 5/-30)             Total: -25 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2020-11-28 13:26:44 +01:00
parent fc6faac84e
commit 885121e25d
3 changed files with 19 additions and 20 deletions

View File

@ -2035,8 +2035,8 @@ enum {
/* Sign-extends to a value which never matches fgetc result: */ /* Sign-extends to a value which never matches fgetc result: */
BASE64_FLAG_NO_STOP_CHAR = 0x80, BASE64_FLAG_NO_STOP_CHAR = 0x80,
}; };
const char *decode_base64(char **pp_dst, const char *src) FAST_FUNC; char *decode_base64(char *dst, const char **pp_src) FAST_FUNC;
const char *decode_base32(char **pp_dst, const char *src) FAST_FUNC; char *decode_base32(char *dst, const char **pp_src) FAST_FUNC;
void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC; void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC;
typedef struct md5_ctx_t { typedef struct md5_ctx_t {

View File

@ -86,9 +86,9 @@ void FAST_FUNC bb_uuencode(char *p, const void *src, int length, const char *tbl
* If points to '\0', then the source was fully decoded. * If points to '\0', then the source was fully decoded.
* (*pp_dst): advanced past the last written byte. * (*pp_dst): advanced past the last written byte.
*/ */
const char* FAST_FUNC decode_base64(char **pp_dst, const char *src) char* FAST_FUNC decode_base64(char *dst, const char **pp_src)
{ {
char *dst = *pp_dst; const char *src = pp_src ? *pp_src : dst; /* for httpd.c, support NULL 2nd param */
unsigned ch = 0; unsigned ch = 0;
unsigned t; unsigned t;
int i = 0; int i = 0;
@ -129,16 +129,17 @@ const char* FAST_FUNC decode_base64(char **pp_dst, const char *src)
ch = 0; ch = 0;
} }
} }
*pp_dst = dst;
/* i is zero here if full 4-char block was decoded */ /* i is zero here if full 4-char block was decoded */
return src - i; /* -i rejects truncations: e.g. "MQ" and "MQ=" (correct encoding is "MQ==" -> "1") */ if (pp_src)
*pp_src = src - i; /* -i rejects truncations: e.g. "MQ" and "MQ=" (correct encoding is "MQ==" -> "1") */
return dst;
} }
#if ENABLE_BASE32 #if ENABLE_BASE32
const char* FAST_FUNC decode_base32(char **pp_dst, const char *src) char* FAST_FUNC decode_base32(char *dst, const char **pp_src)
{ {
char *dst = *pp_dst; const char *src = *pp_src;
int64_t ch = 0; uint64_t ch = 0;
unsigned t; unsigned t;
int i = 0; int i = 0;
@ -169,9 +170,9 @@ const char* FAST_FUNC decode_base32(char **pp_dst, const char *src)
*dst++ = (char) ch; *dst++ = (char) ch;
} }
} }
*pp_dst = dst;
/* i is zero here if full 8-char block was decoded */ /* i is zero here if full 8-char block was decoded */
return src - i; *pp_src = src - i;
return dst;
tail: tail:
{ {
const char *s = src; const char *s = src;
@ -192,8 +193,8 @@ const char* FAST_FUNC decode_base32(char **pp_dst, const char *src)
*dst++ = (char) ch; *dst++ = (char) ch;
dst -= (i+1) * 2 / 3; /* discard last 1, 2, 3 or 4 bytes */ dst -= (i+1) * 2 / 3; /* discard last 1, 2, 3 or 4 bytes */
} }
*pp_dst = dst; *pp_src = src;
return src; return dst;
} }
#endif #endif
@ -249,13 +250,13 @@ void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags)
if (uu_style_end && strcmp(buf, "====") == 0) if (uu_style_end && strcmp(buf, "====") == 0)
return; return;
out_tail = buf; in_tail = buf;
#if ENABLE_BASE32 #if ENABLE_BASE32
if (base32) if (base32)
in_tail = decode_base32(&out_tail, buf); out_tail = decode_base32(buf, &in_tail);
else else
#endif #endif
in_tail = decode_base64(&out_tail, buf); out_tail = decode_base64(buf, &in_tail);
fwrite(buf, (out_tail - buf), 1, dst_stream); fwrite(buf, (out_tail - buf), 1, dst_stream);

View File

@ -1015,11 +1015,9 @@ static char *encodeString(const char *string)
* Parameter: a pointer to a base64 encoded string. * Parameter: a pointer to a base64 encoded string.
* Decoded data is stored in-place. * Decoded data is stored in-place.
*/ */
static void decodeBase64(char *Data) static void decodeBase64(char *data)
{ {
char *eptr = Data; decode_base64(data, NULL)[0] = '\0';
decode_base64(&eptr, Data);
*eptr = '\0';
} }
#endif #endif