-/*
- * Set up structures for placing unencoded
- * content when building parts.
- */
-
-static int
-init_decoded_content (CT ct)
-{
- CE ce;
-
- if ((ce = (CE) calloc (1, sizeof(*ce))) == NULL)
- adios (NULL, "out of memory");
-
- ct->c_cefile = ce;
- ct->c_ceopenfnx = open7Bit; /* since unencoded */
- ct->c_ceclosefnx = close_encoding;
- ct->c_cesizefnx = NULL; /* since unencoded */
-
- return OK;
-}
-
-
-/*
- * TRANSFER ENCODINGS
- */
-
-static int
-init_encoding (CT ct, OpenCEFunc openfnx)
-{
- CE ce;
-
- if ((ce = (CE) calloc (1, sizeof(*ce))) == NULL)
- adios (NULL, "out of memory");
-
- ct->c_cefile = ce;
- ct->c_ceopenfnx = openfnx;
- ct->c_ceclosefnx = close_encoding;
- ct->c_cesizefnx = size_encoding;
-
- return OK;
-}
-
-
-static void
-close_encoding (CT ct)
-{
- CE ce;
-
- if (!(ce = ct->c_cefile))
- return;
-
- if (ce->ce_fp) {
- fclose (ce->ce_fp);
- ce->ce_fp = NULL;
- }
-}
-
-
-static unsigned long
-size_encoding (CT ct)
-{
- int fd;
- unsigned long size;
- char *file;
- CE ce;
- struct stat st;
-
- if (!(ce = ct->c_cefile))
- return (ct->c_end - ct->c_begin);
-
- if (ce->ce_fp && fstat (fileno (ce->ce_fp), &st) != NOTOK)
- return (long) st.st_size;
-
- if (ce->ce_file) {
- if (stat (ce->ce_file, &st) != NOTOK)
- return (long) st.st_size;
- else
- return 0L;
- }
-
- if (ct->c_encoding == CE_EXTERNAL)
- return (ct->c_end - ct->c_begin);
-
- file = NULL;
- if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
- return (ct->c_end - ct->c_begin);
-
- if (fstat (fd, &st) != NOTOK)
- size = (long) st.st_size;
- else
- size = 0L;
-
- (*ct->c_ceclosefnx) (ct);
- return size;
-}
-
-
-/*
- * BASE64
- */
-
-static unsigned char b642nib[0x80] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
- 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
- 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
- 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
- 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
- 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
- 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
- 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
- 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
-};
-
-
-static int
-InitBase64 (CT ct)
-{
- return init_encoding (ct, openBase64);
-}
-
-
-static int
-openBase64 (CT ct, char **file)
-{
- int bitno, cc, digested;
- int fd, len, skip;
- unsigned long bits;
- unsigned char value, *b, *b1, *b2, *b3;
- char *cp, *ep, buffer[BUFSIZ];
- CE ce;
- MD5_CTX mdContext;
-
- b = (unsigned char *) &bits;
- b1 = &b[endian > 0 ? 1 : 2];
- b2 = &b[endian > 0 ? 2 : 1];
- b3 = &b[endian > 0 ? 3 : 0];
-
- ce = ct->c_cefile;
- if (ce->ce_fp) {
- fseek (ce->ce_fp, 0L, SEEK_SET);
- goto ready_to_go;
- }
-
- if (ce->ce_file) {
- if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
- content_error (ce->ce_file, ct, "unable to fopen for reading");
- return NOTOK;
- }
- goto ready_to_go;
- }
-
- if (*file == NULL) {
- ce->ce_file = add (m_scratch ("", tmp), NULL);
- ce->ce_unlink = 1;
- } else {
- ce->ce_file = add (*file, NULL);
- ce->ce_unlink = 0;
- }
-
- if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
- content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
- return NOTOK;
- }
-
- if ((len = ct->c_end - ct->c_begin) < 0)
- adios (NULL, "internal error(1)");
-
- if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
- content_error (ct->c_file, ct, "unable to open for reading");
- return NOTOK;
- }
-
- if ((digested = ct->c_digested))
- MD5Init (&mdContext);
-
- bitno = 18;
- bits = 0L;
- skip = 0;
-
- lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
- while (len > 0) {
- switch (cc = read (fd, buffer, sizeof(buffer) - 1)) {
- case NOTOK:
- content_error (ct->c_file, ct, "error reading from");
- goto clean_up;
-
- case OK:
- content_error (NULL, ct, "premature eof");
- goto clean_up;
-
- default:
- if (cc > len)
- cc = len;
- len -= cc;
-
- for (ep = (cp = buffer) + cc; cp < ep; cp++) {
- switch (*cp) {
- default:
- if (isspace (*cp))
- break;
- if (skip || (*cp & 0x80)
- || (value = b642nib[*cp & 0x7f]) > 0x3f) {
- if (debugsw) {
- fprintf (stderr, "*cp=0x%x pos=%ld skip=%d\n",
- *cp,
- (long) (lseek (fd, (off_t) 0, SEEK_CUR) - (ep - cp)),
- skip);
- }
- content_error (NULL, ct,
- "invalid BASE64 encoding -- continuing");
- continue;
- }
-
- bits |= value << bitno;
-test_end:
- if ((bitno -= 6) < 0) {
- putc ((char) *b1, ce->ce_fp);
- if (digested)
- MD5Update (&mdContext, b1, 1);
- if (skip < 2) {
- putc ((char) *b2, ce->ce_fp);
- if (digested)
- MD5Update (&mdContext, b2, 1);
- if (skip < 1) {
- putc ((char) *b3, ce->ce_fp);
- if (digested)
- MD5Update (&mdContext, b3, 1);
- }