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