nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

quote.c (2026B)


      1 #include "quote.h"
      2 
      3 #include "error.h"
      4 #include "oflops.h"
      5 #include "stralloc.h"
      6 #include "str.h"
      7 
      8 /*
      9 quote() encodes a box as per rfc 821 and rfc 822,
     10 while trying to do as little quoting as possible.
     11 no, 821 and 822 don't have the same encoding. they're not even close.
     12 no special encoding here for bytes above 127.
     13 */
     14 
     15 static char ok[128] = {
     16  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
     17 ,0,7,0,7,7,7,7,7,0,0,7,7,0,7,7,7 ,7,7,7,7,7,7,7,7,7,7,0,0,0,7,0,7
     18 ,0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,0,0,0,7,7
     19 ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,0
     20 } ;
     21 
     22 static int doit(saout,sain)
     23 stralloc *saout;
     24 stralloc *sain;
     25 {
     26  char ch;
     27  int i;
     28  int j;
     29  unsigned int nlen;
     30 
     31  /* make sure the size calculation below does not overflow */
     32  if (__builtin_mul_overflow(sain->len, 2, &nlen) ||
     33      __builtin_add_overflow(nlen, 2, &nlen)) {
     34    errno = error_nomem;
     35    return 0;
     36  }
     37  if (!stralloc_ready(saout,nlen)) return 0;
     38  j = 0;
     39  saout->s[j++] = '"';
     40  for (i = 0;i < sain->len;++i)
     41   {
     42    ch = sain->s[i];
     43    if ((ch == '\r') || (ch == '\n') || (ch == '"') || (ch == '\\'))
     44      saout->s[j++] = '\\';
     45    saout->s[j++] = ch;
     46   }
     47  saout->s[j++] = '"';
     48  saout->len = j;
     49  return 1;
     50 }
     51 
     52 int quote_need(s,n)
     53 char *s;
     54 unsigned int n;
     55 {
     56  unsigned char uch;
     57  int i;
     58  if (!n) return 1;
     59  for (i = 0;i < n;++i)
     60   {
     61    uch = s[i];
     62    if (uch >= 128) return 1;
     63    if (!ok[uch]) return 1;
     64   }
     65  if (s[0] == '.') return 1;
     66  if (s[n - 1] == '.') return 1;
     67  for (i = 0;i < n - 1;++i) if (s[i] == '.') if (s[i + 1] == '.') return 1;
     68  return 0;
     69 }
     70 
     71 int quote(saout,sain)
     72 stralloc *saout;
     73 stralloc *sain;
     74 {
     75  if (quote_need(sain->s,sain->len)) return doit(saout,sain);
     76  return stralloc_copy(saout,sain);
     77 }
     78 
     79 static stralloc foo = {0};
     80 
     81 int quote2(sa,s)
     82 stralloc *sa;
     83 char *s;
     84 {
     85  int j;
     86  if (!*s) return stralloc_copys(sa,s);
     87  j = str_rchr(s,'@');
     88  if (!stralloc_copys(&foo,s)) return 0;
     89  if (!s[j]) return quote(sa,&foo);
     90  foo.len = j;
     91  if (!quote(sa,&foo)) return 0;
     92  return stralloc_cats(sa,s + j);
     93 }