wget: fix bug in base64 encoding (bug 1404). +10 bytes.

This commit is contained in:
Denis Vlasenko 2007-06-27 21:40:07 +00:00
parent c965f4b141
commit 12d2129d50

View File

@ -42,22 +42,24 @@ enum {
STALLTIME = 5 /* Seconds when xfer considered "stalled" */ STALLTIME = 5 /* Seconds when xfer considered "stalled" */
}; };
#else #else
static void progressmeter(int flag) {} static ALWAYS_INLINE void progressmeter(int flag) {}
#endif #endif
/* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the /* Read NMEMB bytes into PTR from STREAM. Returns the number of bytes read,
* number of elements read, and a short count if an eof or non-interrupt * and a short count if an eof or non-interrupt error is encountered. */
* error is encountered. */ static size_t safe_fread(void *ptr, size_t nmemb, FILE *stream)
static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{ {
size_t ret = 0; size_t ret;
char *p = (char*)ptr;
do { do {
clearerr(stream); clearerr(stream);
ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream); ret = fread(p, 1, nmemb, stream);
} while (ret < nmemb && ferror(stream) && errno == EINTR); p += ret;
nmemb -= ret;
} while (nmemb && ferror(stream) && errno == EINTR);
return ret; return p - (char*)ptr;
} }
/* Read a line or SIZE-1 bytes into S, whichever is less, from STREAM. /* Read a line or SIZE-1 bytes into S, whichever is less, from STREAM.
@ -75,10 +77,13 @@ static char *safe_fgets(char *s, int size, FILE *stream)
} }
#if ENABLE_FEATURE_WGET_AUTHENTICATION #if ENABLE_FEATURE_WGET_AUTHENTICATION
/* Base64-encode character string and return the string. */ /* Base64-encode character string. buf is assumed to be char buf[512]. */
static char *base64enc(const unsigned char *p, char *buf, int len) static char *base64enc_512(char buf[512], const char *str)
{ {
bb_uuencode(buf, p, len, bb_uuenc_tbl_base64); unsigned len = strlen(str);
if (len > 512/4*3 - 10) /* paranoia */
len = 512/4*3 - 10;
bb_uuencode(buf, str, len, bb_uuenc_tbl_base64);
return buf; return buf;
} }
#endif #endif
@ -265,12 +270,12 @@ int wget_main(int argc, char **argv)
#if ENABLE_FEATURE_WGET_AUTHENTICATION #if ENABLE_FEATURE_WGET_AUTHENTICATION
if (target.user) { if (target.user) {
fprintf(sfp, "Authorization: Basic %s\r\n", fprintf(sfp, "Proxy-Authorization: Basic %s\r\n"+6,
base64enc((unsigned char*)target.user, buf, sizeof(buf))); base64enc_512(buf, target.user));
} }
if (use_proxy && server.user) { if (use_proxy && server.user) {
fprintf(sfp, "Proxy-Authorization: Basic %s\r\n", fprintf(sfp, "Proxy-Authorization: Basic %s\r\n",
base64enc((unsigned char*)server.user, buf, sizeof(buf))); base64enc_512(buf, server.user));
} }
#endif #endif
@ -459,7 +464,7 @@ int wget_main(int argc, char **argv)
unsigned rdsz = sizeof(buf); unsigned rdsz = sizeof(buf);
if (content_len < sizeof(buf) && (chunked || got_clen)) if (content_len < sizeof(buf) && (chunked || got_clen))
rdsz = (unsigned)content_len; rdsz = (unsigned)content_len;
n = safe_fread(buf, 1, rdsz, dfp); n = safe_fread(buf, rdsz, dfp);
if (n <= 0) if (n <= 0)
break; break;
if (full_write(output_fd, buf, n) != n) { if (full_write(output_fd, buf, n) != n) {
@ -775,7 +780,7 @@ progressmeter(int flag)
putc('\n', stderr); putc('\n', stderr);
} }
} }
#endif #endif /* FEATURE_WGET_STATUSBAR */
/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff, /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
* much of which was blatantly stolen from openssh. */ * much of which was blatantly stolen from openssh. */