aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmail-smtpc/qmailr_utils.c
blob: deeff4666b142483c8b85ee5d73fb012df2ea0a5 (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
/* ISC license. */

#include <string.h>

#include <skalibs/bitarray.h>
#include <skalibs/stralloc.h>

#include "qmailr.h"

int qmailr_memcmp4 (void const *a, void const *b)
{
  return memcmp(a, b, 4) ;
}

int qmailr_memcmp16 (void const *a, void const *b)
{
  return memcmp(a, b, 16) ;
}

static inline int needsquoting (char const *s, size_t len)
{
  static unsigned char const badchar[32] =
  {
    0xff, 0xff, 0xff, 0xff,
    0x05, 0x13, 0x00, 0x5c,
    0x01, 0x00, 0x00, 0x38,
    0x00, 0x00, 0x00, 0x80,
    0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff,
  } ;
  if (!len) return 1 ;
  if (s[0] == '.' || s[len - 1] == '.') return 1 ;
  for (size_t i = 0 ; i < len ; i++)
  {
    if (bitarray_peek(badchar, (unsigned char)s[i])) return 1 ;
    if (i + 2 < len && s[i] == '.' && s[i+1] == '.') return 1 ;
  }
  return 0 ;
}

int qmailr_box_encode (char const *s, size_t len, stralloc *storage)
{
  if (needsquoting(s, len))
  {
    size_t j = storage->len ;
    if (!stralloc_readyplus(storage, 2 + (len << 1))) return 0 ;
    storage->s[j++] = '"' ;
    for (size_t i = 0 ; i < len ; i++)
    {
      if (strchr("\"\\\r\n", s[i])) storage->s[j++] = '\\' ;
      storage->s[j++] = s[i] ;
    }
    storage->s[j++] = '"' ;
    storage->len = j ;
  }
  else if (!stralloc_catb(storage, s, len)) return 0 ;
  return 1 ;
}