File: ctparse.c

package info (click to toggle)
mailutils 1%3A3.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,912 kB
  • sloc: ansic: 187,772; sh: 111,430; yacc: 7,463; cpp: 3,834; makefile: 3,166; lex: 1,972; python: 1,617; exp: 1,563; awk: 152; lisp: 132; sed: 31
file content (244 lines) | stat: -rw-r--r-- 5,731 bytes parent folder | download | duplicates (2)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Content-Type (RFC 2045) parser for GNU Mailutils
   Copyright (C) 2016-2025 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/mime.h>
#include <mailutils/assoc.h>
#include <mailutils/util.h>
#include <mailutils/errno.h>
#include <mailutils/opool.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>

/* Parse the content type header value in INPUT.  If CHARSET is not
   NULL, convert textual parameters to this charset.

   Store the result in CT.

   In case of error, CT is left partially constructed.  The caller
   must free it.
   
   Parsing of the type/subtype value is relaxed: any characters are
   allowed in either part (except for "/", which can't appear in type).
   Although RFC 2045 forbids that, mails with such content types reportedly
   exist (see conversation with Karl Berry on 2020-07-21, particularly
   <202007220115.06M1FuTh001462@freefriends.org> and my reply
   <20200722133251.8412@ulysses.gnu.org.ua>).

   Type must not be empty, but empty subtype is allowed.
*/   
static int
content_type_parse (const char *input, const char *charset,
		    int flags,
		    mu_content_type_t ct)
{
  int rc = 0;
  char *value, *p;

  if (flags & MU_CONTENT_TYPE_PARAM)
    {
      rc = mu_mime_header_parse (input, charset, &value, &ct->param);
      if (rc)
	return rc;
    }
  else
    {
      size_t len;

      input = mu_str_skip_class (input, MU_CTYPE_SPACE);
      len = strcspn (input, ";");
      value = malloc (len + 1);
      if (!value)
	{
	  rc = errno;
	  goto end;
	}
      memcpy (value, input, len);
      value[len] = 0;
      
      ct->param = NULL;
    }
  
  p = strchr (value, '/');
  if (p)
    {
      size_t len = p - value;
      while (len > 0 && mu_isspace (value[len-1]))
	len--;
      if (len == 0)
	{
	  rc = MU_ERR_PARSE;
	  goto end;
	}
      
      p = mu_str_skip_class (p + 1, MU_CTYPE_SPACE);
      
      ct->type = malloc (len + 1);
      if (!ct->type)
	{
	  rc = errno;
	  goto end;
	}
      
      memcpy (ct->type, value, len);
      ct->type[len] = 0;

      ct->subtype = strdup (p);
      if (!ct->subtype)
	rc = errno;
    }
  else if (flags & MU_CONTENT_TYPE_RELAXED)
    {
      ct->type = strdup (value);
      if (!ct->type)
	{
	  rc = errno;
	  goto end;
	}
      ct->subtype = NULL;
    }
  else
    rc = MU_ERR_PARSE;
 end:
  free (value);
  return rc;
}

int
mu_content_type_parse_ext (const char *input, const char *charset,
			   int flags,
			   mu_content_type_t *retct)
{
  int rc;
  mu_content_type_t ct;

  if (!input)
    return EINVAL;
  if (!retct)
    return MU_ERR_OUT_PTR_NULL;
  
  ct = calloc (1, sizeof (*ct));
  if (!ct)
    return errno;
  rc = content_type_parse (input, charset, flags, ct);
  if (rc)
    mu_content_type_destroy (&ct);
  else
    *retct = ct;

  return rc;
}

int
mu_content_type_parse (const char *input, const char *charset,
		       mu_content_type_t *retct)
{
  return mu_content_type_parse_ext (input, charset,
				    MU_CONTENT_TYPE_STRICT |
				    MU_CONTENT_TYPE_PARAM,
				    retct);
}

void
mu_content_type_destroy (mu_content_type_t *pptr)
{
  if (pptr && *pptr)
    {
      mu_content_type_t ct = *pptr;
      free (ct->type);
      free (ct->subtype);
      free (ct->trailer);
      mu_assoc_destroy (&ct->param);
      free (ct);
      *pptr = NULL;
    }
}

static int
format_param (char const *name, void *data, void *call_data)
{
  struct mu_mime_param *param = data;
  char *value = param->value;
  mu_opool_t pool = call_data;
  char *cp;

  /* Content-Type parameters don't use lang and cset fields of
     struct mu_mime_param, so these are ignored. */
  mu_opool_append (pool, "; ", 2);
  mu_opool_appendz (pool, name);
  mu_opool_append_char (pool, '=');
  if (*(cp = mu_str_skip_class_comp (value, MU_CTYPE_TSPEC)))
    {
      mu_opool_append_char (pool, '"');
      while (*(cp = mu_str_skip_cset_comp (value, "\\\"")))
	{
	  mu_opool_append (pool, value, cp - value);
	  mu_opool_append_char (pool, '\\');
	  mu_opool_append_char (pool, *cp);
	  value = cp + 1;
	}
      if (*value)
	mu_opool_appendz (pool, value);
      mu_opool_append_char (pool, '"');
    }
  else
    mu_opool_appendz (pool, value);
  return 0;
}

int
mu_content_type_format (mu_content_type_t ct, char **return_ptr)
{
  int rc;
  mu_opool_t pool;
  mu_nonlocal_jmp_t jmp;

  if (!ct)
    return EINVAL;
  if (!return_ptr)
    return MU_ERR_OUT_PTR_NULL;

  rc = mu_opool_create (&pool, MU_OPOOL_DEFAULT);
  if (rc)
    return rc;

  if ((rc = setjmp (jmp.buf)) != 0)
    {
      mu_opool_destroy (&pool);
      return rc;
    }
  mu_opool_setjmp (pool, &jmp);

  mu_opool_appendz (pool, ct->type);
  if (ct->subtype)
    {
      mu_opool_append_char (pool, '/');
      mu_opool_appendz (pool, ct->subtype);
    }
  if (!mu_assoc_is_empty (ct->param))
    mu_assoc_foreach (ct->param, format_param, pool);
  mu_opool_append_char (pool, 0);
  *return_ptr = mu_opool_detach (pool, NULL);
  mu_opool_clrjmp (pool);
  mu_opool_destroy (&pool);
  return 0;
}