1N/A/*
1N/A * rfc2047.c -- decode RFC-2047 header format
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#ifndef lint
1N/Astatic char sccsi2[] = "%W% (Sun) %G%";
1N/A#endif
1N/A
1N/A/*
1N/A * Copyright (c) 1997-1998 Richard Coleman
1N/A * All rights reserved.
1N/A *
1N/A * Permission is hereby granted, without written agreement and without
1N/A * license or royalty fees, to use, copy, modify, and distribute this
1N/A * software and to distribute modified versions of this software for any
1N/A * purpose, provided that the above copyright notice and the following two
1N/A * paragraphs appear in all copies of this software.
1N/A *
1N/A * In no event shall Richard Coleman be liable to any party for direct,
1N/A * indirect, special, incidental, or consequential damages arising out of
1N/A * the use of this software and its documentation, even if Richard Coleman
1N/A * has been advised of the possibility of such damage.
1N/A *
1N/A * Richard Coleman specifically disclaims any warranties, including, but
1N/A * not limited to, the implied warranties of merchantability and fitness
1N/A * for a particular purpose. The software provided hereunder is on an "as
1N/A * is" basis, and Richard Coleman has no obligation to provide maintenance,
1N/A * support, updates, enhancements, or modifications.
1N/A */
1N/A
1N/A/*
1N/A * Parts of this code were derived from metamail, which is ...
1N/A *
1N/A * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
1N/A *
1N/A * Permission to use, copy, modify, and distribute this material
1N/A * for any purpose and without fee is hereby granted, provided
1N/A * that the above copyright notice and this permission notice
1N/A * appear in all copies, and that the name of Bellcore not be
1N/A * used in advertising or publicity pertaining to this
1N/A * material without the specific, prior written permission
1N/A * of an authorized representative of Bellcore. BELLCORE
1N/A * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
1N/A * OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
1N/A * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1N/A */
1N/A
1N/A/*
1N/A * Copyright (c) 1998, by Sun Microsystems, Inc.
1N/A * All rights reserved.
1N/A */
1N/A
1N/A#include <string.h>
1N/A
1N/Atypedef int bool;
1N/A
1N/A#define FALSE 0
1N/A#define TRUE 1
1N/A
1N/Astatic signed char hexindex[] = {
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
1N/A -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
1N/A};
1N/A
1N/Astatic signed char index_64[128] = {
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1N/A -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
1N/A 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
1N/A -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
1N/A 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
1N/A -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
1N/A 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
1N/A};
1N/A
1N/A#define char64(c) (((unsigned char) (c) > 127) ? -1 : \
1N/A index_64[(unsigned char) (c)])
1N/A
1N/Astatic int
1N/Aunqp(unsigned char byte1, unsigned char byte2)
1N/A{
1N/A if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
1N/A return (-1);
1N/A return (hexindex[byte1] << 4 | hexindex[byte2]);
1N/A}
1N/A
1N/A/* Check if character is linear whitespace */
1N/A#define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
1N/A
1N/A/*
1N/A * Decode the string as a RFC-2047 header field
1N/A */
1N/A
1N/Abool
1N/Adecode_rfc2047(char *str, char *dst, char *charset)
1N/A{
1N/A char *p, *q, *pp;
1N/A char *startofmime, *endofmime;
1N/A int c, quoted_printable;
1N/A bool encoding_found = FALSE; /* did we decode anything? */
1N/A bool between_encodings = FALSE; /* are we between two encodings? */
1N/A bool equals_pending = FALSE; /* is there a '=' pending? */
1N/A int whitespace = 0; /* how much whitespace between encodings? */
1N/A
1N/A if (str == NULL)
1N/A return (FALSE);
1N/A
1N/A /*
1N/A * Do a quick and dirty check for the '=' character.
1N/A * This should quickly eliminate many cases.
1N/A */
1N/A if (!strchr(str, '='))
1N/A return (FALSE);
1N/A
1N/A for (p = str, q = dst; *p; p++) {
1N/A /*
1N/A * If we had an '=' character pending from
1N/A * last iteration, then add it first.
1N/A */
1N/A if (equals_pending) {
1N/A *q++ = '=';
1N/A equals_pending = FALSE;
1N/A between_encodings = FALSE; /* we added non-WS text */
1N/A }
1N/A
1N/A if (*p != '=') {
1N/A /* count linear whitespace while between encodings */
1N/A if (between_encodings && is_lws(*p))
1N/A whitespace++;
1N/A else
1N/A between_encodings = FALSE; /* non-WS added */
1N/A *q++ = *p;
1N/A continue;
1N/A }
1N/A
1N/A equals_pending = TRUE; /* we have a '=' pending */
1N/A
1N/A /* Check for initial =? */
1N/A if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
1N/A startofmime = p + 2;
1N/A
1N/A /* Scan ahead for the next '?' character */
1N/A for (pp = startofmime; *pp && *pp != '?'; pp++)
1N/A ;
1N/A
1N/A if (!*pp)
1N/A continue;
1N/A
1N/A strncpy(charset, startofmime, pp - startofmime);
1N/A charset[pp - startofmime] = '\0';
1N/A
1N/A startofmime = pp + 1;
1N/A
1N/A /* Check for valid encoding type */
1N/A if (*startofmime != 'B' && *startofmime != 'b' &&
1N/A *startofmime != 'Q' && *startofmime != 'q')
1N/A continue;
1N/A
1N/A /* Is encoding quoted printable or base64? */
1N/A quoted_printable = (*startofmime == 'Q' ||
1N/A *startofmime == 'q');
1N/A startofmime++;
1N/A
1N/A /* Check for next '?' character */
1N/A if (*startofmime != '?')
1N/A continue;
1N/A startofmime++;
1N/A
1N/A /*
1N/A * Scan ahead for the ending ?=
1N/A *
1N/A * While doing this, we will also check if encoded
1N/A * word has any embedded linear whitespace.
1N/A */
1N/A endofmime = NULL;
1N/A for (pp = startofmime; *pp && *(pp+1); pp++) {
1N/A if (is_lws(*pp))
1N/A break;
1N/A else if (*pp == '?' && pp[1] == '=') {
1N/A endofmime = pp;
1N/A break;
1N/A }
1N/A }
1N/A if (is_lws(*pp) || endofmime == NULL)
1N/A continue;
1N/A
1N/A /*
1N/A * We've found an encoded word, so we can drop
1N/A * the '=' that was pending
1N/A */
1N/A equals_pending = FALSE;
1N/A
1N/A /*
1N/A * If we are between two encoded words separated only
1N/A * by linear whitespace, then we ignore the whitespace.
1N/A * We will roll back the buffer the number of whitespace
1N/A * characters we've seen since last encoded word.
1N/A */
1N/A if (between_encodings)
1N/A q -= whitespace;
1N/A
1N/A /* Now decode the text */
1N/A if (quoted_printable) {
1N/A for (pp = startofmime; pp < endofmime; pp++) {
1N/A if (*pp == '=') {
1N/A c = unqp(pp[1], pp[2]);
1N/A if (c == -1)
1N/A continue;
1N/A if (c != 0)
1N/A *q++ = c;
1N/A pp += 2;
1N/A } else if (*pp == '_')
1N/A *q++ = ' ';
1N/A else
1N/A *q++ = *pp;
1N/A }
1N/A } else {
1N/A /* base64 */
1N/A int c1, c2, c3, c4;
1N/A
1N/A pp = startofmime;
1N/A while (pp < endofmime) {
1N/A /* 6 + 2 bits */
1N/A while ((pp < endofmime) &&
1N/A ((c1 = char64(*pp)) == -1)) {
1N/A pp++;
1N/A }
1N/A if (pp < endofmime)
1N/A pp++;
1N/A while ((pp < endofmime) &&
1N/A ((c2 = char64(*pp)) == -1)) {
1N/A pp++;
1N/A }
1N/A if (pp < endofmime && c1 != -1 &&
1N/A c2 != -1) {
1N/A *q++ = (c1 << 2) | (c2 >> 4);
1N/A pp++;
1N/A }
1N/A /* 4 + 4 bits */
1N/A while ((pp < endofmime) &&
1N/A ((c3 = char64(*pp)) == -1)) {
1N/A pp++;
1N/A }
1N/A if (pp < endofmime && c2 != -1 &&
1N/A c3 != -1) {
1N/A *q++ = ((c2 & 0xF) << 4) |
1N/A (c3 >> 2);
1N/A pp++;
1N/A }
1N/A /* 2 + 6 bits */
1N/A while ((pp < endofmime) &&
1N/A ((c4 = char64(*pp)) == -1)) {
1N/A pp++;
1N/A }
1N/A if (pp < endofmime && c3 != -1 &&
1N/A c4 != -1) {
1N/A *q++ = ((c3 & 0x3) << 6) | (c4);
1N/A pp++;
1N/A }
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * Now that we are done decoding this particular
1N/A * encoded word, advance string to trailing '='.
1N/A */
1N/A p = endofmime + 1;
1N/A
1N/A encoding_found = TRUE; /* found (>= 1) encoded word */
1N/A between_encodings = TRUE; /* just decoded something */
1N/A whitespace = 0; /* re-initialize amount of whitespace */
1N/A }
1N/A }
1N/A
1N/A /* If an equals was pending at end of string, add it now. */
1N/A if (equals_pending)
1N/A *q++ = '=';
1N/A *q = '\0';
1N/A
1N/A return (encoding_found);
1N/A}