Back to index

courier  0.68.2
rfc2045acprep.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 #include      "rfc2045.h"
00007 #include      <ctype.h>
00008 #include      <string.h>
00009 
00010 
00011 static void start_rwprep(struct rfc2045 *);
00012 static void do_rwprep(const char *, size_t);
00013 static void end_rwprep();
00014 
00015 static struct rfc2045ac rfc2045acprep={
00016        &start_rwprep,
00017        &do_rwprep,
00018        &end_rwprep};
00019 
00020 static struct rfc2045 *currwp;
00021 static int curlinepos=0;
00022 
00023 typedef enum {
00024        raw,
00025        quotedprint,
00026        qpseeneq,
00027        qpseeneqh,
00028        base64} state_t;
00029 
00030 static state_t curstate;
00031 static int statechar;
00032 
00033 #define       h2nyb(c) ( (c) >= 'a' && (c) <= 'f' ? (c)-('a'-10): \
00034                  (c) >= 'A' && (c) <= 'F' ? (c)-('A'-10): (c)-'0')
00035 
00036 struct rfc2045 *rfc2045_alloc_ac()
00037 {
00038 struct rfc2045 *p=rfc2045_alloc();
00039 
00040        if (p) p->rfc2045acptr= &rfc2045acprep;
00041        currwp=0;
00042        return (p);
00043 }
00044 
00045 
00046 static void start_rwprep(struct rfc2045 *p)
00047 {
00048        currwp=p;
00049        curlinepos=0;
00050        curstate=raw;
00051        if (p->content_transfer_encoding)
00052        {
00053               if (strcmp(p->content_transfer_encoding,
00054                      "quoted-printable") == 0)
00055                      curstate=quotedprint;
00056               else if (strcmp(p->content_transfer_encoding, "base64") == 0)
00057                      curstate=base64;
00058        }
00059 }
00060 
00061 static void do_rwprep(const char * p, size_t n)
00062 {
00063        if (!currwp)  return;
00064        for ( ; n; --n, ++p)
00065               switch (curstate)    {
00066               case quotedprint:
00067                      if (*p == '=')
00068                      {
00069                             curstate=qpseeneq;
00070                             continue;
00071                      }
00072                      /* FALLTHRU */
00073               case raw:
00074                      if (*p == '\r' || *p == '\n')
00075                             curlinepos=0;
00076                      else if (++curlinepos > 500)
00077                             currwp->haslongline=1;
00078                      if ((unsigned char)*p >= 127)
00079                             currwp->has8bitchars=1;
00080                      break;
00081               case qpseeneq:
00082                      if (*p == '\n')
00083                      {
00084                             curstate=quotedprint;
00085                             continue;
00086                      }
00087                      if (isspace((int)(unsigned char)*p))      continue; /* Ignore WSP */
00088                      statechar=*p;
00089                      curstate=qpseeneqh;
00090                      continue;
00091               case qpseeneqh:
00092                      curstate=quotedprint;
00093                      if ( (unsigned char)
00094                             ( (h2nyb(statechar) << 4) + h2nyb(*p) ) >= 127
00095                             ) currwp->has8bitchars=1;
00096                      if (++curlinepos > 500)
00097                             currwp->haslongline=1;
00098                      continue;
00099               case base64:
00100                      break;
00101               }
00102 }
00103 
00104 static void end_rwprep()
00105 {
00106 }