aboutsummaryrefslogtreecommitdiffstats
path: root/gettext.h
blob: 841b072b454cdc02e8ade1e9d217addc1a1974ef (plain)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* Convenience header for conditional use of GNU <libintl.h>.
   Copyright (C) 1995-1998, 2000-2002, 2004-2006, 2009-2016 Free Software
   Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef _LIBGETTEXT_H
#define _LIBGETTEXT_H 1

/* NLS can be disabled through the configure --disable-nls option.  */
#if ENABLE_NLS

/* Get declarations of GNU message catalog functions.  */
# include <libintl.h>

/* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
   the gettext() and ngettext() macros.  This is an alternative to calling
   textdomain(), and is useful for libraries.  */
# ifdef DEFAULT_TEXT_DOMAIN
#  undef gettext
#  define gettext(Msgid) \
     dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
#  undef ngettext
#  define ngettext(Msgid1, Msgid2, N) \
     dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
# endif

#else

/* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
   chokes if dcgettext is defined as a macro.  So include it now, to make
   later inclusions of <locale.h> a NOP.  We don't include <libintl.h>
   as well because people using "gettext.h" will not include <libintl.h>,
   and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
   is OK.  */
#if defined(__sun)
# include <locale.h>
#endif

/* Many header files from the libstdc++ coming with g++ 3.3 or newer include
   <libintl.h>, which chokes if dcgettext is defined as a macro.  So include
   it now, to make later inclusions of <libintl.h> a NOP.  */
#if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
# include <cstdlib>
# if (__GLIBC__ >= 2 && !defined __UCLIBC__) || _GLIBCXX_HAVE_LIBINTL_H
#  include <libintl.h>
# endif
#endif

/* Disabled NLS.
   The casts to 'const char *' serve the purpose of producing warnings
   for invalid uses of the value returned from these functions.
   On pre-ANSI systems without 'const', the config.h file is supposed to
   contain "#define const".  */
# undef gettext
# define gettext(Msgid) ((const char *) (Msgid))
# undef dgettext
# define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
# undef dcgettext
# define dcgettext(Domainname, Msgid, Category) \
    ((void) (Category), dgettext (Domainname, Msgid))
# undef ngettext
# define ngettext(Msgid1, Msgid2, N) \
    ((N) == 1 \
     ? ((void) (Msgid2), (const char *) (Msgid1)) \
     : ((void) (Msgid1), (const char *) (Msgid2)))
# undef dngettext
# define dngettext(Domainname, Msgid1, Msgid2, N) \
    ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
# undef dcngettext
# define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
    ((void) (Category), dngettext (Domainname, Msgid1, Msgid2, N))
# undef textdomain
# define textdomain(Domainname) ((const char *) (Domainname))
# undef bindtextdomain
# define bindtextdomain(Domainname, Dirname) \
    ((void) (Domainname), (const char *) (Dirname))
# undef bind_textdomain_codeset
# define bind_textdomain_codeset(Domainname, Codeset) \
    ((void) (Domainname), (const char *) (Codeset))

#endif

/* Prefer gnulib's setlocale override over libintl's setlocale override.  */
#ifdef GNULIB_defined_setlocale
# undef setlocale
# define setlocale rpl_setlocale
#endif

/* A pseudo function call that serves as a marker for the automated
   extraction of messages, but does not call gettext().  The run-time
   translation is done at a different place in the code.
   The argument, String, should be a literal string.  Concatenated strings
   and other string expressions won't work.
   The macro's expansion is not parenthesized, so that it is suitable as
   initializer for static 'char[]' or 'const char[]' variables.  */
#define gettext_noop(String) String

/* The separator between msgctxt and msgid in a .mo file.  */
#define GETTEXT_CONTEXT_GLUE "\004"

/* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
   MSGID.  MSGCTXT and MSGID must be string literals.  MSGCTXT should be
   short and rarely need to change.
   The letter 'p' stands for 'particular' or 'special'.  */
#ifdef DEFAULT_TEXT_DOMAIN
# define pgettext(Msgctxt, Msgid) \
   pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
#else
# define pgettext(Msgctxt, Msgid) \
   pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
#endif
#define dpgettext(Domainname, Msgctxt, Msgid) \
  pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
#define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
  pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
#ifdef DEFAULT_TEXT_DOMAIN
# define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
   npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
#else
# define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
   npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
#endif
#define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
#define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
  npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)

#ifdef __GNUC__
__inline
#else
#ifdef __cplusplus
inline
#endif
#endif
static const char *
pgettext_aux (const char *domain,
              const char *msg_ctxt_id, const char *msgid,
              int category)
{
  const char *translation = dcgettext (domain, msg_ctxt_id, category);
  if (translation == msg_ctxt_id)
    return msgid;
  else
    return translation;
}

#ifdef __GNUC__
__inline
#else
#ifdef __cplusplus
inline
#endif
#endif
static const char *
npgettext_aux (const char *domain,
               const char *msg_ctxt_id, const char *msgid,
               const char *msgid_plural, unsigned long int n,
               int category)
{
  const char *translation =
    dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
  if (translation == msg_ctxt_id || translation == msgid_plural)
    return (n == 1 ? msgid : msgid_plural);
  else
    return translation;
}

/* The same thing extended for non-constant arguments.  Here MSGCTXT and MSGID
   can be arbitrary expressions.  But for string literals these macros are
   less efficient than those above.  */

#include <string.h>

#if (((__GNUC__ >= 3 || __GNUG__ >= 2) && !defined __STRICT_ANSI__) \
     /* || __STDC_VERSION__ >= 199901L */ )
# define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 1
#else
# define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 0
#endif

#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
#include <stdlib.h>
#endif

#define pgettext_expr(Msgctxt, Msgid) \
  dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
#define dpgettext_expr(Domainname, Msgctxt, Msgid) \
  dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)

#ifdef __GNUC__
__inline
#else
#ifdef __cplusplus
inline
#endif
#endif
static const char *
dcpgettext_expr (const char *domain,
                 const char *msgctxt, const char *msgid,
                 int category)
{
  size_t msgctxt_len = strlen (msgctxt) + 1;
  size_t msgid_len = strlen (msgid) + 1;
  const char *translation;
#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  char msg_ctxt_id[msgctxt_len + msgid_len];
#else
  char buf[1024];
  char *msg_ctxt_id =
    (msgctxt_len + msgid_len <= sizeof (buf)
     ? buf
     : (char *) malloc (msgctxt_len + msgid_len));
  if (msg_ctxt_id != NULL)
#endif
    {
      int found_translation;
      memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
      msg_ctxt_id[msgctxt_len - 1] = '\004';
      memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
      translation = dcgettext (domain, msg_ctxt_id, category);
      found_translation = (translation != msg_ctxt_id);
#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
      if (msg_ctxt_id != buf)
        free (msg_ctxt_id);
#endif
      if (found_translation)
        return translation;
    }
  return msgid;
}

#define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
  dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
#define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
  dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)

#ifdef __GNUC__
__inline
#else
#ifdef __cplusplus
inline
#endif
#endif
static const char *
dcnpgettext_expr (const char *domain,
                  const char *msgctxt, const char *msgid,
                  const char *msgid_plural, unsigned long int n,
                  int category)
{
  size_t msgctxt_len = strlen (msgctxt) + 1;
  size_t msgid_len = strlen (msgid) + 1;
  const char *translation;
#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
  char msg_ctxt_id[msgctxt_len + msgid_len];
#else
  char buf[1024];
  char *msg_ctxt_id =
    (msgctxt_len + msgid_len <= sizeof (buf)
     ? buf
     : (char *) malloc (msgctxt_len + msgid_len));
  if (msg_ctxt_id != NULL)
#endif
    {
      int found_translation;
      memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
      msg_ctxt_id[msgctxt_len - 1] = '\004';
      memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
      translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
      found_translation = !(translation == msg_ctxt_id || translation == msgid_plural);
#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
      if (msg_ctxt_id != buf)
        free (msg_ctxt_id);
#endif
      if (found_translation)
        return translation;
    }
  return (n == 1 ? msgid : msgid_plural);
}

#endif /* _LIBGETTEXT_H */
== '?') { enc = tolower((unsigned char)*(p+1)); p_in = p+3; state = S_COPY_MIME; } else state = S_COPY_PLAIN; } else state = S_COPY_PLAIN; /* Invalid data */ break; case S_COPY_MIME: p = strstr(p_in, MIMEHDR_END); /* Find end of coded data */ if (p == NULL) p = p_in + strlen(p_in); for (; (p_in < p); ) { /* Decode all encoded data */ if (enc == 'q') { if (*p_in == '=') { /* Decode one char qp-coded at (p_in+1) and (p_in+2) */ if (qp_char(*(p_in+1), *(p_in+2), p_out) == 0) p_in += 3; else { /* Invalid QP data - pass through unchanged. */ *p_out = *p_in; p_in++; } } else if (*p_in == '_') { /* * RFC 2047: '_' inside encoded word represents 0x20. * NOT a space - always the value 0x20. */ *p_out = 0x20; p_in++; } else { /* Copy unchanged */ *p_out = *p_in; p_in++; } p_out++; } else if (enc == 'b') { /* Decode base64 encoded data */ char delimsave; int decoded_count; delimsave = *p; *p = '\r'; decoded_count = from64tobits(p_out, p_in, 0); *p = delimsave; if (decoded_count > 0) p_out += decoded_count; p_in = p; } else { /* Copy unchanged */ *p_out = *p_in; p_in++; p_out++; } } if (*p_in) p_in += 2; /* Skip the MIMEHDR_END delimiter */ /* * We've completed decoding one encoded sequence. But another * may follow immediately, in which case whitespace before the * new MIMEHDR_INIT delimiter must be discarded. * See if that is the case */ p = strstr(p_in, MIMEHDR_INIT); state = S_COPY_PLAIN; if (p != NULL) { /* * There is more MIME data later on. Is there * whitespace only before the delimiter? */ char *q; int wsp_only = 1; for (q=p_in; (wsp_only && (q < p)); q++) wsp_only = isspace((unsigned char)*q); if (wsp_only) { /* * Whitespace-only before the MIME delimiter. OK, * just advance p_in to past the new MIMEHDR_INIT, * and prepare to process the new MIME charset/encoding * header. */ p_in = p + sizeof(MIMEHDR_INIT) - 1; state = S_SKIP_MIMEINIT; } } break; } } *p_out = '\0'; } /* * Routines for decoding body-parts of a message. * * Since the "fetch" part of fetchmail gets a message body * one line at a time, we need to maintain some state variables * across multiple invokations of the UnMimeBodyline() routine. * The driver routine should call MimeBodyType() when all * headers have been received, and then UnMimeBodyline() for * every line in the message body. * */ #define S_BODY_DATA 0 #define S_BODY_HDR 1 /* * Flag indicating if we are currently processing * the headers or the body of a (multipart) message. */ static int BodyState = S_BODY_DATA; /* * Flag indicating if we are in the process of decoding * a quoted-printable body part. */ static int CurrEncodingIsQP = 0; static int CurrTypeNeedsDecode = 0; /* * Delimiter for multipart messages. RFC 2046 states that this must * NEVER be longer than 70 characters. Add 3 for the two hyphens * at the beginning, and a terminating null. */ #define MAX_DELIM_LEN 70 static char MultipartDelimiter[MAX_DELIM_LEN+3]; /* This string replaces the "Content-Transfer-Encoding: quoted-printable" * string in all headers, including those in body-parts. The replacement * must be no longer than the original string. */ static const char ENC8BIT[] = "Content-Transfer-Encoding: 8bit"; static void SetEncoding8bit(char *XferEncOfs) { char *p; if (XferEncOfs != NULL) { memcpy(XferEncOfs, ENC8BIT, sizeof(ENC8BIT) - 1); /* If anything left, in this header, replace with whitespace */ for (p=XferEncOfs+sizeof(ENC8BIT)-1; ((unsigned char)*p >= ' '); p++) *p=' '; } } static char *GetBoundary(char *CntType) { char *p1, *p2; int flag; /* Find the "boundary" delimiter. It must be preceded with a ';' * and optionally some whitespace. */ p1 = CntType; do { p2 = strchr(p1, ';'); if (p2) for (p2++; isspace((unsigned char)*p2); p2++) { } p1 = p2; } while ((p1) && (strncasecmp(p1, "boundary", 8) != 0)); if (p1 == NULL) /* No boundary delimiter */ return NULL; /* Skip "boundary", whitespace and '='; check that we do have a '=' */ for (p1+=8, flag=0; (isspace((unsigned char)*p1) || (*p1 == '=')); p1++) flag |= (*p1 == '='); if (!flag) return NULL; /* Find end of boundary delimiter string */ if (*p1 == '\"') { /* The delimiter is inside quotes */ p1++; p2 = strchr(p1, '\"'); if (p2 == NULL) return NULL; /* No closing '"' !?! */ } else { /* There might be more text after the "boundary" string. */ p2 = strchr(p1, ';'); /* Safe - delimiter with ';' must be in quotes */ } /* Zero-terminate the boundary string */ if (p2 != NULL) *p2 = '\0'; return (p1 && strlen(p1)) ? p1 : NULL; } static int CheckContentType(char *CntType) { /* * Static array of Content-Type's for which we will do * quoted-printable decoding, if requested. * It is probably wise to do this only on known text-only types; * be really careful if you change this. */ static const char *DecodedTypes[] = { "text/", /* Will match ALL content-type's starting with 'text/' */ "message/rfc822", NULL }; char *p = CntType; int i; /* If no Content-Type header, it isn't MIME - don't touch it */ if (CntType == NULL) return 0; /* Skip whitespace, if any */ for (; isspace((unsigned char)*p); p++) ; for (i=0; (DecodedTypes[i] && (strncasecmp(p, DecodedTypes[i], strlen(DecodedTypes[i])))); i++) ; return (DecodedTypes[i] != NULL); } /* * This routine does three things: * 1) It determines - based on the message headers - whether the * message body is a MIME message that may hold 8 bit data. * - A message that has a "quoted-printable" or "8bit" transfer * encoding is assumed to contain 8-bit data (when decoded). * - A multipart message is assumed to contain 8-bit data * when decoded (there might be quoted-printable body-parts). * - All other messages are assumed NOT to include 8-bit data. * 2) It determines the delimiter-string used in multi-part message * bodies. * 3) It sets the initial values of the CurrEncodingIsQP, * CurrTypeNeedsDecode, and BodyState variables, from the header * contents. * * The return value is a bitmask. */ int MimeBodyType(char *hdrs, int WantDecode) { char *NxtHdr = hdrs; char *XferEnc, *XferEncOfs, *CntType, *MimeVer, *p; int HdrsFound = 0; /* We only look for three headers */ int BodyType; /* Return value */ /* Setup for a standard (no MIME, no QP, 7-bit US-ASCII) message */ MultipartDelimiter[0] = '\0'; CurrEncodingIsQP = CurrTypeNeedsDecode = 0; BodyState = S_BODY_DATA; BodyType = 0; /* Just in case ... */ if (hdrs == NULL) return BodyType; XferEnc = XferEncOfs = CntType = MimeVer = NULL; do { if (strncasecmp("Content-Transfer-Encoding:", NxtHdr, 26) == 0) { XferEncOfs = NxtHdr; p = nxtaddr(NxtHdr); if (p != NULL) { xfree(XferEnc); XferEnc = xstrdup(p); HdrsFound++; } } else if (strncasecmp("Content-Type:", NxtHdr, 13) == 0) { /* * This one is difficult. We cannot use the standard * nxtaddr() routine, since the boundary-delimiter is * (probably) enclosed in quotes - and thus appears * as an rfc822 comment, and nxtaddr() "eats" up any * spaces in the delimiter. So, we have to do this * by hand. */ /* Skip the "Content-Type:" part and whitespace after it */ for (NxtHdr += 13; ((*NxtHdr == ' ') || (*NxtHdr == '\t')); NxtHdr++) { } /* * Get the full value of the Content-Type header; * it might span multiple lines. So search for * a newline char, but ignore those that have a * have a TAB or space just after the NL (continued * lines). */ p = NxtHdr-1; do { p=strchr((p+1),'\n'); } while ( (p != NULL) && ((*(p+1) == '\t') || (*(p+1) == ' ')) ); if (p == NULL) p = NxtHdr + strlen(NxtHdr); xfree(CntType); CntType = (char *)xmalloc(p-NxtHdr+1); strlcpy(CntType, NxtHdr, p-NxtHdr+1); HdrsFound++; } else if (strncasecmp("MIME-Version:", NxtHdr, 13) == 0) { p = nxtaddr(NxtHdr); if (p != NULL) { xfree(MimeVer); MimeVer = xstrdup(p); HdrsFound++; } } NxtHdr = (strchr(NxtHdr, '\n')); if (NxtHdr != NULL) NxtHdr++; } while ((NxtHdr != NULL) && (*NxtHdr) && (HdrsFound != 3)); /* Done looking through the headers, now check what they say */ if ((MimeVer != NULL) && (strcmp(MimeVer, "1.0") == 0)) { CurrTypeNeedsDecode = CheckContentType(CntType); /* Check Content-Type to see if this is a multipart message */ if ( (CntType != NULL) && ((strncasecmp(CntType, "multipart/mixed", 15) == 0) || (strncasecmp(CntType, "message/", 8) == 0)) ) { char *p1 = GetBoundary(CntType); if (p1 != NULL) { /* The actual delimiter is "--" followed by the boundary string */ strcpy(MultipartDelimiter, "--"); strlcat(MultipartDelimiter, p1, sizeof(MultipartDelimiter)); BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE); } } /* * Check Content-Transfer-Encoding, but * ONLY for non-multipart messages (BodyType == 0). */ if ((XferEnc != NULL) && (BodyType == 0)) { if (strcasecmp(XferEnc, "quoted-printable") == 0) { CurrEncodingIsQP = 1; BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE); if (WantDecode && CurrTypeNeedsDecode) { SetEncoding8bit(XferEncOfs); } } else if (strcasecmp(XferEnc, "7bit") == 0) { CurrEncodingIsQP = 0; BodyType = (MSG_IS_7BIT); } else if (strcasecmp(XferEnc, "8bit") == 0) { CurrEncodingIsQP = 0; BodyType = (MSG_IS_8BIT); } } } xfree(XferEnc); xfree(CntType); xfree(MimeVer); return BodyType; } /* * Decode one line of data containing QP data. * Return flag set if this line ends with a soft line-break. * 'bufp' is modified to point to the end of the output buffer. */ static int DoOneQPLine(char **bufp, flag delimited, flag issoftline) { char *buf = *bufp; char *p_in, *p_out, *p; int n; int ret = 0; /* * Special case: line consists of a single =2E and messages are * dot-terminated. Line has to be dot-stuffed after decoding. */ if (delimited && !issoftline && buf[0]=='=' && !strncmp(*bufp, "=2E\r\n", 5)) { strcpy(buf, "..\r\n"); *bufp += 5; return(FALSE); } p_in = buf; if (delimited && issoftline && (strncmp(buf, "..", 2) == 0)) p_in++; for (p_out = buf; (*p_in); ) { p = strchr(p_in, '='); if (p == NULL) { /* No more QP data, just move remainder into place */ n = strlen(p_in); memmove(p_out, p_in, n); p_in += n; p_out += n; } else { if (p > p_in) { /* There are some uncoded chars at the beginning. */ n = (p - p_in); memmove(p_out, p_in, n); p_out += n; } switch (*(p+1)) { case '\0': case '\r': case '\n': /* Soft line break, skip '=' */ p_in = p+1; if (*p_in == '\r') p_in++; if (*p_in == '\n') p_in++; ret = 1; break; default: /* There is a QP encoded byte */ if (qp_char(*(p+1), *(p+2), p_out) == 0) { p_in = p+3; } else { /* Invalid QP data - pass through unchanged. */ *p_out = '='; p_in = p+1; } p_out++; break; } } } *p_out = '\0'; *bufp = p_out; return ret; } /* This is called once per line in the message body. We need to scan * all lines in the message body for the multipart delimiter string, * and handle any body-part headers in such messages (these can toggle * qp-decoding on and off). * * Note: Messages that are NOT multipart-messages go through this * routine quickly, since BodyState will always be S_BODY_DATA, * and MultipartDelimiter is NULL. * * Return flag set if this line ends with a soft line-break. * 'bufp' is modified to point to the end of the output buffer. */ int UnMimeBodyline(char **bufp, flag delimited, flag softline) { char *buf = *bufp; int ret = 0; switch (BodyState) { case S_BODY_HDR: UnMimeHeader(buf); /* Headers in body-parts can be encoded, too! */ if ((*buf == '\0') || (*buf == '\n') || (strcmp(buf, "\r\n") == 0)) { BodyState = S_BODY_DATA; } else if (strncasecmp("Content-Transfer-Encoding:", buf, 26) == 0) { char *XferEnc; XferEnc = nxtaddr(buf); if ((XferEnc != NULL) && (strcasecmp(XferEnc, "quoted-printable") == 0)) { CurrEncodingIsQP = 1; /* * Hmm ... we cannot be really sure that CurrTypeNeedsDecode * has been set - we may not have seen the Content-Type header * yet. But *usually* the Content-Type header comes first, so * this will work. And there is really no way of doing it * "right" as long as we stick with the line-by-line processing. */ if (CurrTypeNeedsDecode) SetEncoding8bit(buf); } } else if (strncasecmp("Content-Type:", buf, 13) == 0) { CurrTypeNeedsDecode = CheckContentType(nxtaddr(buf)); } *bufp = (buf + strlen(buf)); break; case S_BODY_DATA: if ((*MultipartDelimiter) && (strncmp(buf, MultipartDelimiter, strlen(MultipartDelimiter)) == 0)) { BodyState = S_BODY_HDR; CurrEncodingIsQP = CurrTypeNeedsDecode = 0; } if (CurrEncodingIsQP && CurrTypeNeedsDecode) ret = DoOneQPLine(bufp, delimited, softline); else *bufp = (buf + strlen(buf)); break; } return ret; } #ifdef STANDALONE #include <stdio.h> #include <unistd.h> const char *program_name = "unmime"; int outlevel = 0; #define BUFSIZE_INCREMENT 4096 #ifdef DEBUG #define DBG_FWRITE(B,L,BS,FD) do { if (fwrite((B), (L), (BS), (FD))) { } } while(0) #else #define DBG_FWRITE(B,L,BS,FD) #endif int main(int argc, char *argv[]) { unsigned int BufSize; char *buffer, *buf_p; int nl_count, i, bodytype; /* quench warnings about unused arguments */ (void)argc; (void)argv; #ifdef DEBUG pid_t pid; FILE *fd_orig, *fd_conv; char fnam[100]; /* we don't need snprintf here, but for consistency, we'll use it */ pid = getpid(); snprintf(fnam, sizeof(fnam), "/tmp/i_unmime.%lx", (long)pid); fd_orig = fopen(fnam, "w"); snprintf(fnam, sizeof(fnam), "/tmp/o_unmime.%lx", (long)pid); fd_conv = fopen(fnam, "w"); #endif BufSize = BUFSIZE_INCREMENT; /* Initial size of buffer */ buf_p = buffer = (char *) xmalloc(BufSize); nl_count = 0; do { i = fread(buf_p, 1, 1, stdin); switch (*buf_p) { case '\n': nl_count++; break; case '\r': break; default: nl_count = 0; break; } buf_p++; if ((unsigned)(buf_p - buffer) == BufSize) { /* Buffer is full! Get more room. */ buffer = (char *)xrealloc(buffer, BufSize+BUFSIZE_INCREMENT); buf_p = buffer + BufSize; BufSize += BUFSIZE_INCREMENT; } } while ((i > 0) && (nl_count < 2)); *buf_p = '\0'; DBG_FWRITE(buffer, strlen(buffer), 1, fd_orig); UnMimeHeader(buffer); bodytype = MimeBodyType(buffer, 1); i = strlen(buffer); DBG_FWRITE(buffer, i, 1, fd_conv); if (fwrite(buffer, i, 1, stdout) < 1) { perror("fwrite"); goto barf; } do { buf_p = (buffer - 1); do { buf_p++; i = fread(buf_p, 1, 1, stdin); } while ((i == 1) && (*buf_p != '\n')); if (i == 1) buf_p++; *buf_p = '\0'; DBG_FWRITE(buf, (buf_p - buffer), 1, fd_orig); if (buf_p > buffer) { if (bodytype & MSG_NEEDS_DECODE) { buf_p = buffer; UnMimeBodyline(&buf_p, 0, 0); } DBG_FWRITE(buffer, (buf_p - buffer), 1, fd_conv); if (fwrite(buffer, (buf_p - buffer), 1, stdout) < 1) { perror("fwrite"); goto barf; } } } while (buf_p > buffer); barf: free(buffer); if (EOF == fflush(stdout)) perror("fflush"); #ifdef DEBUG fclose(fd_orig); fclose(fd_conv); #endif return 0; } #endif