ze-filter  (ze-filter-0.8.0-develop-180218)
ze-qp.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 #include <ze-sys.h>
25 
26 #include "ze-qp.h"
27 
28 /* ****************************************************************************
29  * *
30  * *
31  **************************************************************************** */
32 int
33 qp_decode (out, in, sz)
34  char *out;
35  char *in;
36  size_t sz;
37 {
38  char *p = in;
39  char *q = out;
40  int nb = 0;
41 
42  char *qp = "0123456789ABCDEFabcdef";
43 
44  if ((in == NULL) || (out == NULL))
45  return 0;
46 
47  for (p = in; (*p != '\0') && (nb < sz); p++) {
48  if (*p != '=') {
49  *q++ = *p;
50  nb++;
51  continue;
52  }
53  p++;
54  if (*p == '\r' || *p == '\n')
55  continue;
56  if (strspn (p, qp) > 1) {
57  int c = 0;
58  char *r = NULL;
59 
60 #if 1
61  r = strchr (qp, toupper(*p));
62  if (r != NULL)
63  c = ((int ) (r - qp)) << 4;
64 #else
65  c = (strchr (qp, toupper(*p)) - qp) << 4;
66 #endif
67 
68  p++;
69 #if 1
70  r = strchr (qp, toupper(*p));
71  if (r != NULL)
72  c += ((int ) (r - qp));
73 #else
74  c += (strchr (qp, toupper(*p)) - qp);
75 #endif
76  *q++ = c;
77  nb++;
78  }
79  }
80  *q = '\0';
81 
82  return nb;
83 }
84 
85 /* ****************************************************************************
86  * *
87  * *
88  **************************************************************************** */
89 #define QPCHARS "0123456789ABCDEF"
90 
91 int
92 new_qp_decode (out, in, sz)
93  char *out;
94  char *in;
95  size_t sz;
96 {
97  char *p = in;
98  char *q = out;
99  int nb = 0;
100 
101  if ((in == NULL) || (out == NULL))
102  return 0;
103 
104  p = in;
105  while (*p != '\0' && nb < sz)
106  {
107  while (*p != '\0' && *p != '=' && nb < sz)
108  {
109  *q++ = *p++;
110  nb++;
111  }
112  if (nb >= sz || *p == '\0')
113  break;
114 
115  if (*p == '=')
116  {
117  p++;
118 
119  if (*p == '\r' || *p == '\n')
120  continue;
121 
122  if (*p == '\0')
123  break;
124 
125  if (strlen(p) > 1) {
126  char c, x, *r;
127 
128  x = 0;
129  c = toupper(*p++);
130  if ((r = strchr(QPCHARS, c)) != NULL)
131  x = (r - QPCHARS) << 4;
132  c = toupper(*p++);
133  if ((r = strchr(QPCHARS, c)) != NULL)
134  x |= (r - QPCHARS);
135 
136  *q++ = x;
137  nb++;
138  }
139  }
140  }
141 
142  *q = '\0';
143 
144  return nb;
145 }
146 
int qp_decode(char *out, char *in, size_t sz)
Definition: ze-qp.c:33
#define strchr
Definition: ze-sys.h:218
int nb
Definition: ze-connopen.c:61
int new_qp_decode(char *out, char *in, size_t sz)
Definition: ze-qp.c:92
#define QPCHARS
Definition: ze-qp.c:89