lib/quotedP.c

Go to the documentation of this file.
00001 #include "common.h"
00002 #include "linefile.h"
00003 #include "dystring.h"
00004 #include "quotedP.h"
00005 
00006 char *quotedPrintableEncode(char *input)
00007 /* Use Quoted-Printable standard to encode a string. */
00008 {
00009 struct dyString *dy = dyStringNew(0);
00010 size_t i=0,l=strlen(input);
00011 int width = 0;
00012 for (i=0; i < l; ++i)
00013     {
00014     char c = input[i];
00015     switch (c)
00016         {
00017         case '=':
00018         case '\t':
00019         case '\r':
00020         case '\n':
00021         case ' ':
00022             dyStringAppendC(dy, '=');
00023             dyStringPrintf(dy, "%2x", c);
00024             width += 3;
00025             break;
00026         default:
00027             dyStringAppendC(dy, c);
00028             ++width;
00029         }
00030     if (width > 72)
00031         {
00032         dyStringAppendC(dy, '=');
00033         dyStringAppendC(dy, '\n');
00034         width = 0;
00035         }
00036         
00037     }
00038 /* add terminator to prevent extra newline */
00039 if (lastChar(dy->string) != '=')  
00040     dyStringAppendC(dy, '=');
00041     
00042 return dyStringCannibalize(&dy);
00043 }
00044 
00045 boolean quotedPCollapse(char *line)
00046 /* Use Quoted-Printable standard to decode a string.
00047  * Return true if the line does not end in '='
00048  * which indicate continuation. */
00049 {
00050 size_t i=0,j=0,l=strlen(line);
00051 boolean result = lastChar(line) != '=';
00052 char c1 = ' ', c2 = ' ';
00053 while(i < l)
00054     {
00055     if (line[i] == '=')
00056         {
00057         if (i > (l-3)) 
00058             break;     /* not enough room left for whole char */
00059         ++i;        
00060         c1 = line[i++];
00061         c2 = line[i++];
00062         toupper(c1);
00063         toupper(c2);
00064         if (isdigit(c1))
00065             c1 -= 48;
00066         else
00067             c1 -= 55;
00068         if (isdigit(c2))
00069             c2 -= 48;
00070         else
00071             c2 -= 55;
00072         line[j++] = (c1 * 16) + c2;
00073         }
00074     else
00075         {
00076         line[j++] = line[i++];
00077         }
00078     }
00079 line[j] = 0; /* terminate line */
00080 return result;
00081 }
00082 
00083 char *quotedPrintableDecode(char *input)
00084 /* Use Quoted-Printable standard to decode a string.  Return decoded
00085  * string which will be freeMem'd.  */
00086 {
00087 size_t inplen = strlen(input);
00088 char *result = (char *)needMem(inplen+1);
00089 size_t j=0;
00090 char *line = NULL;
00091 int size = 0;
00092 int i = 0;
00093 boolean newLine = FALSE;
00094 
00095 struct lineFile *lf = lineFileOnString("", TRUE, cloneString(input));
00096 
00097 while (lineFileNext(lf, &line, &size))
00098     {
00099     newLine = quotedPCollapse(line);
00100     size = strlen(line); 
00101     for (i = 0; i < size; )
00102         result[j++] = line[i++];
00103     if (newLine)
00104         result[j++] = '\n';
00105     }
00106 
00107 lineFileClose(&lf);  /* frees cloned string */
00108 
00109 result[j] = 0;  /* terminate text string */
00110      
00111 return result;
00112 }
00113 

Generated on Tue Dec 25 18:39:31 2007 for blat by  doxygen 1.5.2