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