+ ret:
+ *pp_dst = dst;
+ return src_tail;
+}
+
+/*
+ * Decode base64 encoded stream.
+ * Can stop on EOF, specified char, or on uuencode-style "====" line:
+ * flags argument controls it.
+ */
+void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags)
+{
+/* Note that EOF _can_ be passed as exit_char too */
+#define exit_char ((int)(signed char)flags)
+#define uu_style_end (flags & BASE64_FLAG_UU_STOP)
+
+ /* uuencoded files have 61 byte lines. Use 64 byte buffer
+ * to process line at a time.
+ */
+ enum { BUFFER_SIZE = 64 };
+
+ char in_buf[BUFFER_SIZE + 2];
+ char out_buf[BUFFER_SIZE / 4 * 3 + 2];
+ char *out_tail;
+ const char *in_tail;
+ int term_seen = 0;
+ int in_count = 0;
+
+ while (1) {
+ while (in_count < BUFFER_SIZE) {
+ int ch = fgetc(src_stream);
+ if (ch == exit_char) {
+ if (in_count == 0)
+ return;
+ term_seen = 1;
+ break;
+ }
+ if (ch == EOF) {
+ term_seen = 1;
+ break;
+ }
+ /* Prevent "====" line to be split: stop if we see '\n'.
+ * We can also skip other whitespace and skirt the problem
+ * of files with NULs by stopping on any control char or space:
+ */
+ if (ch <= ' ')
+ break;
+ in_buf[in_count++] = ch;
+ }
+ in_buf[in_count] = '\0';
+
+ /* Did we encounter "====" line? */
+ if (uu_style_end && strcmp(in_buf, "====") == 0)
+ return;
+
+ out_tail = out_buf;
+ in_tail = decode_base64(&out_tail, in_buf);
+
+ fwrite(out_buf, (out_tail - out_buf), 1, dst_stream);
+
+ if (term_seen) {
+ /* Did we consume ALL characters? */
+ if (*in_tail == '\0')
+ return;
+ /* No */
+ bb_error_msg_and_die("truncated base64 input");
+ }
+
+ /* It was partial decode */
+ in_count = strlen(in_tail);
+ memmove(in_buf, in_tail, in_count);
+ }