+\f
+/* base64 encode/decode functions.
+
+ Originally based on code from GNU recode. Ported to FSF Emacs by
+ Lars Magne Ingebrigtsen and Karl Heuer. Ported to XEmacs and
+ subsequently heavily hacked by Hrvoje Niksic. */
+
+#define MIME_LINE_LENGTH 72
+
+#define IS_ASCII(Character) \
+ ((Character) < 128)
+#define IS_BASE64(Character) \
+ (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
+
+/* Table of characters coding the 64 values. */
+static char base64_value_to_char[64] =
+{
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 0- 9 */
+ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 10-19 */
+ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', /* 20-29 */
+ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 30-39 */
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', /* 40-49 */
+ 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', /* 50-59 */
+ '8', '9', '+', '/' /* 60-63 */
+};
+
+/* Table of base64 values for first 128 characters. */
+static short base64_char_to_value[128] =
+{
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
+ -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
+ 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
+ -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
+ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
+ 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
+ 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
+ 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
+ 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
+};
+
+/* The following diagram shows the logical steps by which three octets
+ get transformed into four base64 characters.
+
+ .--------. .--------. .--------.
+ |aaaaaabb| |bbbbcccc| |ccdddddd|
+ `--------' `--------' `--------'
+ 6 2 4 4 2 6
+ .--------+--------+--------+--------.
+ |00aaaaaa|00bbbbbb|00cccccc|00dddddd|
+ `--------+--------+--------+--------'
+
+ .--------+--------+--------+--------.
+ |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
+ `--------+--------+--------+--------'
+
+ The octets are divided into 6 bit chunks, which are then encoded into
+ base64 characters. */
+
+#define ADVANCE_INPUT(c, stream) \
+ ((ec = Lstream_get_emchar (stream)) == -1 ? 0 : \
+ ((ec > 255) ? \
+ (signal_simple_error ("Non-ascii character in base64 input", \
+ make_char (ec)), 0) \
+ : (c = (Bufbyte)ec), 1))
+
+static Bytind
+base64_encode_1 (Lstream *istream, Bufbyte *to, int line_break)
+{
+ EMACS_INT counter = 0;
+ Bufbyte *e = to;
+ Emchar ec;
+ unsigned int value;
+
+ while (1)
+ {
+ Bufbyte c;
+ if (!ADVANCE_INPUT (c, istream))
+ break;
+
+ /* Wrap line every 76 characters. */
+ if (line_break)
+ {
+ if (counter < MIME_LINE_LENGTH / 4)
+ counter++;
+ else
+ {
+ *e++ = '\n';
+ counter = 1;
+ }
+ }
+
+ /* Process first byte of a triplet. */
+ *e++ = base64_value_to_char[0x3f & c >> 2];
+ value = (0x03 & c) << 4;
+
+ /* Process second byte of a triplet. */
+ if (!ADVANCE_INPUT (c, istream))
+ {
+ *e++ = base64_value_to_char[value];
+ *e++ = '=';
+ *e++ = '=';
+ break;
+ }
+
+ *e++ = base64_value_to_char[value | (0x0f & c >> 4)];
+ value = (0x0f & c) << 2;
+
+ /* Process third byte of a triplet. */
+ if (!ADVANCE_INPUT (c, istream))
+ {
+ *e++ = base64_value_to_char[value];
+ *e++ = '=';
+ break;
+ }
+
+ *e++ = base64_value_to_char[value | (0x03 & c >> 6)];
+ *e++ = base64_value_to_char[0x3f & c];
+ }
+
+ return e - to;
+}
+#undef ADVANCE_INPUT
+
+/* Get next character from the stream, except that non-base64
+ characters are ignored. This is in accordance with rfc2045. EC
+ should be an Emchar, so that it can hold -1 as the value for EOF. */
+#define ADVANCE_INPUT_IGNORE_NONBASE64(ec, stream, streampos) do { \
+ ec = Lstream_get_emchar (stream); \
+ ++streampos; \
+ /* IS_BASE64 may not be called with negative arguments so check for \
+ EOF first. */ \
+ if (ec < 0 || IS_BASE64 (ec) || ec == '=') \
+ break; \
+} while (1)
+
+#define STORE_BYTE(pos, val, ccnt) do { \
+ pos += set_charptr_emchar (pos, (Emchar)((unsigned char)(val))); \
+ ++ccnt; \
+} while (0)
+
+static Bytind
+base64_decode_1 (Lstream *istream, Bufbyte *to, Charcount *ccptr)
+{
+ Charcount ccnt = 0;
+ Bufbyte *e = to;
+ EMACS_INT streampos = 0;
+
+ while (1)
+ {
+ Emchar ec;
+ unsigned long value;
+
+ /* Process first byte of a quadruplet. */
+ ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ if (ec < 0)
+ break;
+ if (ec == '=')
+ signal_simple_error ("Illegal `=' character while decoding base64",
+ make_int (streampos));
+ value = base64_char_to_value[ec] << 18;
+
+ /* Process second byte of a quadruplet. */
+ ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ if (ec < 0)
+ error ("Premature EOF while decoding base64");
+ if (ec == '=')
+ signal_simple_error ("Illegal `=' character while decoding base64",
+ make_int (streampos));
+ value |= base64_char_to_value[ec] << 12;
+ STORE_BYTE (e, value >> 16, ccnt);
+
+ /* Process third byte of a quadruplet. */
+ ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ if (ec < 0)
+ error ("Premature EOF while decoding base64");
+
+ if (ec == '=')
+ {
+ ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ if (ec < 0)
+ error ("Premature EOF while decoding base64");
+ if (ec != '=')
+ signal_simple_error ("Padding `=' expected but not found while decoding base64",
+ make_int (streampos));
+ continue;
+ }
+
+ value |= base64_char_to_value[ec] << 6;
+ STORE_BYTE (e, 0xff & value >> 8, ccnt);
+
+ /* Process fourth byte of a quadruplet. */
+ ADVANCE_INPUT_IGNORE_NONBASE64 (ec, istream, streampos);
+ if (ec < 0)
+ error ("Premature EOF while decoding base64");
+ if (ec == '=')
+ continue;
+
+ value |= base64_char_to_value[ec];
+ STORE_BYTE (e, 0xff & value, ccnt);
+ }
+
+ *ccptr = ccnt;
+ return e - to;
+}
+#undef ADVANCE_INPUT
+#undef ADVANCE_INPUT_IGNORE_NONBASE64
+#undef STORE_BYTE
+
+
+DEFUN ("base64-encode-region", Fbase64_encode_region, 2, 3, "r", /*
+Base64-encode the region between START and END.
+Return the length of the encoded text.
+Optional third argument NO-LINE-BREAK means do not break long lines
+into shorter lines.
+*/
+ (start, end, no_line_break))
+{
+ Bufbyte *encoded;
+ Bytind encoded_length;
+ Charcount allength, length;
+ struct buffer *buf = current_buffer;
+ Bufpos begv, zv, old_pt = BUF_PT (buf);
+ Lisp_Object input;
+ int speccount = specpdl_depth();
+
+ get_buffer_range_char (buf, start, end, &begv, &zv, 0);
+ barf_if_buffer_read_only (buf, begv, zv);
+
+ /* We need to allocate enough room for encoding the text.
+ We need 33 1/3% more space, plus a newline every 76
+ characters, and then we round up. */
+ length = zv - begv;
+ allength = length + length/3 + 1;
+ allength += allength / MIME_LINE_LENGTH + 1 + 6;
+
+ input = make_lisp_buffer_input_stream (buf, begv, zv, 0);
+ /* We needn't multiply allength with MAX_EMCHAR_LEN because all the
+ base64 characters will be single-byte. */
+ XMALLOC_OR_ALLOCA (encoded, allength, Bufbyte);
+ encoded_length = base64_encode_1 (XLSTREAM (input), encoded,
+ NILP (no_line_break));
+ if (encoded_length > allength)
+ ABORT ();
+ Lstream_delete (XLSTREAM (input));
+
+ /* Now we have encoded the region, so we insert the new contents
+ and delete the old. (Insert first in order to preserve markers.) */
+ buffer_insert_raw_string_1 (buf, begv, encoded, encoded_length, 0);
+ XMALLOC_UNBIND (encoded, allength, speccount);
+ buffer_delete_range (buf, begv + encoded_length, zv + encoded_length, 0);
+
+ /* Simulate FSF Emacs implementation of this function: if point was
+ in the region, place it at the beginning. */
+ if (old_pt >= begv && old_pt < zv)
+ BUF_SET_PT (buf, begv);
+
+ /* We return the length of the encoded text. */
+ return make_int (encoded_length);
+}