libbb: merge mail and uudecode's base64 decoders
function old new delta read_base64 - 378 +378 uudecode_main 306 315 +9 parse 953 958 +5 read_stduu 250 254 +4 base64_main 217 219 +2 read_base64 358 - -358 decode_base64 371 - -371 ------------------------------------------------------------------------------ (add/remove: 2/2 grow/shrink: 4/0 up/down: 398/-729) Total: -331 bytes Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
This commit is contained in:
@@ -161,73 +161,6 @@ void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
|
||||
#undef src_buf
|
||||
}
|
||||
|
||||
void FAST_FUNC decode_base64(FILE *src_stream, FILE *dst_stream)
|
||||
{
|
||||
int term_count = 1;
|
||||
|
||||
while (1) {
|
||||
char translated[4];
|
||||
int count = 0;
|
||||
|
||||
while (count < 4) {
|
||||
char *table_ptr;
|
||||
int ch;
|
||||
|
||||
/* Get next _valid_ character.
|
||||
* global vector bb_uuenc_tbl_base64[] contains this string:
|
||||
* "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
|
||||
*/
|
||||
do {
|
||||
ch = fgetc(src_stream);
|
||||
if (ch == EOF) {
|
||||
bb_error_msg_and_die(bb_msg_read_error);
|
||||
}
|
||||
// - means end of MIME section
|
||||
if ('-' == ch) {
|
||||
// push it back
|
||||
ungetc(ch, src_stream);
|
||||
return;
|
||||
}
|
||||
table_ptr = strchr(bb_uuenc_tbl_base64, ch);
|
||||
} while (table_ptr == NULL);
|
||||
|
||||
/* Convert encoded character to decimal */
|
||||
ch = table_ptr - bb_uuenc_tbl_base64;
|
||||
|
||||
if (*table_ptr == '=') {
|
||||
if (term_count == 0) {
|
||||
translated[count] = '\0';
|
||||
break;
|
||||
}
|
||||
term_count++;
|
||||
} else if (*table_ptr == '\n') {
|
||||
/* Check for terminating line */
|
||||
if (term_count == 5) {
|
||||
return;
|
||||
}
|
||||
term_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
translated[count] = ch;
|
||||
count++;
|
||||
term_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge 6 bit chars to 8 bit */
|
||||
if (count > 1) {
|
||||
fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
|
||||
}
|
||||
if (count > 2) {
|
||||
fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
|
||||
}
|
||||
if (count > 3) {
|
||||
fputc(translated[2] << 6 | translated[3], dst_stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* get username and password from a file descriptor
|
||||
*/
|
||||
|
@@ -32,4 +32,3 @@ void FAST_FUNC get_cred_or_die(int fd);
|
||||
const FAST_FUNC char *command(const char *fmt, const char *param);
|
||||
|
||||
void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol);
|
||||
void FAST_FUNC decode_base64(FILE *src_stream, FILE *dst_stream);
|
||||
|
@@ -225,7 +225,7 @@ static int parse(const char *boundary, char **argv)
|
||||
// prepare unique string pattern
|
||||
uniq = xasprintf("%%llu.%u.%s", (unsigned)getpid(), safe_gethostname());
|
||||
|
||||
//bb_info_msg("PARSE[%s]", terminator);
|
||||
//bb_info_msg("PARSE[%s]", uniq);
|
||||
|
||||
while ((line = xmalloc_fgets_str(stdin, "\r\n\r\n")) != NULL) {
|
||||
|
||||
@@ -306,7 +306,7 @@ static int parse(const char *boundary, char **argv)
|
||||
|
||||
// dump to fp
|
||||
if (0 == strcasecmp(encoding, "base64")) {
|
||||
decode_base64(stdin, fp);
|
||||
read_base64(stdin, fp, '-');
|
||||
} else if (0 != strcasecmp(encoding, "7bit")
|
||||
&& 0 != strcasecmp(encoding, "8bit")
|
||||
) {
|
||||
|
Reference in New Issue
Block a user