1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2012 Jeffrey Stedfast
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "gmime-table-private.h"
29 #include "gmime-parse-utils.h"
32 #ifdef ENABLE_WARNINGS
36 #endif /* ENABLE_WARNINGS */
42 * g_mime_parse_content_type:
43 * @in: address of input text string
44 * @type: address of the 'type' output string
45 * @subtype: address of the 'subtype' output string
47 * Decodes the simple Content-Type type/subtype tokens and updates @in
48 * to point to the first char after the end of the subtype.
50 * Returns: %TRUE if the string was successfully parsed or %FALSE
54 g_mime_parse_content_type (const char **in, char **type, char **subtype)
56 register const char *inptr;
57 const char *start = *in;
63 while (*inptr && is_ttoken (*inptr))
66 *type = g_strndup (start, (size_t) (inptr - start));
71 /* check for type/subtype delimeter */
72 if (*start++ != '/') {
82 /* decode the subtype */
83 while (*inptr && is_ttoken (*inptr))
86 /* check that the subtype exists */
94 *subtype = g_strndup (start, (size_t) (inptr - start));
96 /* update the input string pointer */
104 * g_mime_decode_lwsp:
105 * @in: address of input text string
107 * Skips past any LWSP or rfc822 comments in *@in and updates @in.
110 g_mime_decode_lwsp (const char **in)
112 const char *inptr = *in;
114 while (*inptr && (*inptr == '(' || is_lwsp (*inptr))) {
115 while (*inptr && is_lwsp (*inptr))
118 /* skip over any comments */
123 while (*inptr && depth) {
124 if (*inptr == '\\' && *(inptr + 1))
126 else if (*inptr == '(')
128 else if (*inptr == ')')
140 decode_quoted_string (const char **in)
142 register const char *inptr = *in;
143 const char *qstring = NULL;
149 while (*inptr && *inptr != '"') {
167 decode_atom (const char **in)
169 register const char *inptr = *in;
170 const char *atom = NULL;
172 if (!is_atom (*inptr))
176 while (is_atom (*inptr))
186 * g_mime_decode_word:
187 * @in: address of input text string
189 * Extracts the next rfc822 'word' token.
191 * Returns: the next rfc822 'word' token or %NULL if non exist.
194 g_mime_decode_word (const char **in)
196 const char *inptr = *in;
198 decode_lwsp (&inptr);
201 return decode_quoted_string (in);
204 return decode_atom (in);
209 decode_subliteral (const char **in, GString *domain)
211 const char *inptr = *in;
212 gboolean got = FALSE;
214 while (*inptr && *inptr != '.' && *inptr != ']') {
215 if (is_dtext (*inptr)) {
216 g_string_append_c (domain, *inptr);
219 } else if (is_lwsp (*inptr))
220 decode_lwsp (&inptr);
231 decode_domain_literal (const char **in, GString *domain)
233 const char *inptr = *in;
235 decode_lwsp (&inptr);
236 while (*inptr && *inptr != ']') {
237 if (decode_subliteral (&inptr, domain) && *inptr == '.') {
238 g_string_append_c (domain, *inptr);
240 } else if (*inptr != ']') {
241 w(g_warning ("Malformed domain-literal, unexpected char '%c': %s",
244 /* try and skip to the next char ?? */
254 * g_mime_decode_domain:
255 * @in: address of input text string
256 * @domain: a #GString to decode the domain into
258 * Extracts the next rfc822 'domain' token and appends it to @domain.
260 * Returns: %TRUE if an rfc822 'domain' token was decoded or %FALSE
264 g_mime_decode_domain (const char **in, GString *domain)
266 const char *inptr, *save, *atom;
267 size_t initial = domain->len;
270 while (inptr && *inptr) {
271 decode_lwsp (&inptr);
274 g_string_append_c (domain, '[');
277 decode_domain_literal (&inptr, domain);
280 g_string_append_c (domain, ']');
283 w(g_warning ("Missing ']' in domain-literal: %s", *in));
285 if (!(atom = decode_atom (&inptr))) {
286 w(g_warning ("Unexpected char '%c' in domain: %s", *inptr, *in));
287 /* remove the last '.' */
288 if (domain->len && domain->str[domain->len - 1] == '.')
289 g_string_truncate (domain, domain->len - 1);
293 g_string_append_len (domain, atom, (size_t) (inptr - atom));
297 decode_lwsp (&inptr);
303 g_string_append_c (domain, '.');
309 return domain->len > initial;