1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2010-2021 Free Software Foundation, Inc.
3 
4    This library is free software; you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include <config.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <mailutils/stream.h>
22 #include <mailutils/cctype.h>
23 
24 static int
excmp(const void * a,const void * b)25 excmp (const void *a, const void *b)
26 {
27   return strcmp (*(const char **)a, *(const char **)b);
28 }
29 
30 /* Given a NULL-terminated list of header names in NAMES, create a
31    corresponding expect table for use in mu_stream_header_copy. The
32    expect table is an array of strings of equal length. Each element
33    contains a header name from the input array, converted to lowercase,
34    with a ':' appended to it and stuffed with 0 bytes to the maximum
35    length. The maximum length is selected as the length of the longest
36    string from NAMES plus one.
37    The resulting array is sorted in lexicographical order.
38    On success, return the created table. Store the number of entries in
39    PCOUNT and the maximum length in PMAX.
40    On error (ENOMEM) return NULL.
41  */
42 static char **
make_exclusion_list(char ** names,size_t * pcount,size_t * pmax)43 make_exclusion_list (char **names, size_t *pcount, size_t *pmax)
44 {
45   size_t i, j;
46   size_t count = 0;
47   size_t max_len = 0;
48   char **exlist;
49   char *p;
50 
51   for (i = 0; names[i]; i++)
52     {
53       size_t len = strlen (names[i]) + 1;
54       if (len > max_len)
55 	max_len = len;
56     }
57   count = i;
58 
59   exlist = calloc (count, sizeof (exlist[0]) + max_len + 1);
60   if (!exlist)
61     return NULL;
62   p = (char*)(exlist + count);
63   for (i = 0; names[i]; i++, p += max_len + 1)
64     {
65       exlist[i] = p;
66       for (j = 0; names[i][j]; j++)
67 	p[j] = mu_tolower (names[i][j]);
68       p[j++] = ':';
69       memset (p + j, 0, max_len - j + 1);
70     }
71   qsort (exlist, count, sizeof (exlist[0]), excmp);
72 
73   *pcount = count;
74   *pmax = max_len;
75   return exlist;
76 }
77 
78 /* Assuming SRC is a stream of RFC 822 headers, copy it to DST, omitting
79    headers from EXCLUDE_NAMES. Stop copying at the empty line ("\n\n")
80    or end of input, whichever occurs first. The terminating newline is
81    read from SRC, but not written to DST. This allows the caller to append
82    to DST any additional headers.
83    Returns mailutils error code.
84    FIXME: Bail out early with MU_ERR_PARSE if the input is not a well-
85    formed header stream. This can be checked in save_state_init and
86    save_state_expect by ensuring that the character read is in the
87    MU_CTYPE_HEADR class.
88 */
89 int
mu_stream_header_copy(mu_stream_t dst,mu_stream_t src,char ** exclude_names)90 mu_stream_header_copy (mu_stream_t dst, mu_stream_t src, char **exclude_names)
91 {
92   int rc;
93   size_t la_max;
94   char *lookahead;
95   size_t la_idx = 0;
96   enum
97   {
98     save_state_init,
99     save_state_expect,
100     save_state_skip,
101     save_state_copy,
102     save_state_stop
103   } state = save_state_init;
104   int i = 0;
105   int j = 0;
106   char **exclude;
107   size_t excount;
108 
109   exclude = make_exclusion_list (exclude_names, &excount, &la_max);
110   if (!exclude)
111     return ENOMEM;
112   lookahead = malloc (la_max);
113   if (!lookahead)
114     {
115       free (exclude);
116       return ENOMEM;
117     }
118 
119   while (state != save_state_stop)
120     {
121       char c;
122       size_t n;
123 
124       rc = mu_stream_read (src, &c, 1, &n);
125       if (rc || n == 0)
126 	break;
127 
128       if (state == save_state_init || state == save_state_expect)
129 	{
130 	  if (la_idx == la_max)
131 	    state = save_state_copy;
132 	  else
133 	    {
134 	      lookahead[la_idx++] = c;
135 	      c = mu_tolower (c);
136 	    }
137 	}
138 
139       switch (state)
140 	{
141 	case save_state_init:
142 	  if (c == '\n')
143 	    {
144 	      /* End of headers. */
145 	      state = save_state_stop;
146 	      break;
147 	    }
148 
149 	  j = 0;
150 	  state = save_state_copy;
151 	  for (i = 0; i < excount; i++)
152 	    {
153 	      if (exclude[i][j] == c)
154 		{
155 		  j++;
156 		  state = save_state_expect;
157 		  break;
158 		}
159 	    }
160 	  break;
161 
162 	case save_state_expect:
163 	  if (exclude[i][j] != c)
164 	    {
165 	      while (++i < excount)
166 		{
167 		  if (memcmp (exclude[i-1], exclude[i], j))
168 		    {
169 		      state = save_state_copy;
170 		      break;
171 		    }
172 		  if (exclude[i][j] == c)
173 		    break;
174 		}
175 	      if (i == excount)
176 		state = save_state_copy;
177 	      if (state == save_state_copy)
178 		break;
179 	    }
180 
181 	  if (c == ':')
182 	    {
183 	      la_idx = 0;
184 	      state = save_state_skip;
185 	    }
186 	  else
187 	    {
188 	      j++;
189 	      if (exclude[i][j] == 0)
190 		state = save_state_copy;
191 	    }
192 	  break;
193 
194 	case save_state_copy:
195 	  if (la_idx > 0)
196 	    {
197 	      rc = mu_stream_write (dst, lookahead, la_idx, NULL);
198 	      if (rc)
199 		break;
200 	      la_idx = 0;
201 	    }
202 	  rc = mu_stream_write (dst, &c, 1, NULL);
203 	  if (c == '\n')
204 	    state = save_state_init;
205 	  break;
206 
207 	case save_state_skip:
208 	  if (c == '\n')
209 	    state = save_state_init;
210 	  break;
211 
212 	default:
213 	  abort (); /* Should not happen */
214 	}
215     }
216 
217   if (rc == 0)
218     {
219       if (la_idx > 1)
220 	rc = mu_stream_write (dst, lookahead, la_idx - 1, NULL);
221     }
222 
223   free (lookahead);
224   free (exclude);
225 
226   return rc;
227 }
228