Fix crash when an Atom feed's opening tag doesn't have xmlns attribute.
[claws.git] / src / plugins / rssyl / libfeed / parser.c
1 /*
2  * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program 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 GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <glib.h>
25 #include <curl/curl.h>
26 #include <expat.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30
31 #include <codeconv.h>
32
33 #include "feed.h"
34
35 #include "parser.h"
36
37 enum {
38         FEED_TYPE_NONE,
39         FEED_TYPE_RDF,
40         FEED_TYPE_RSS_20,
41         FEED_TYPE_ATOM_03,
42         FEED_TYPE_ATOM_10,
43         FEED_TYPE_OPML
44 } FeedTypes;
45
46 static void _handler_set(XML_Parser parser, guint type)
47 {
48         if( parser == NULL )
49                 return;
50
51         switch(type) {
52                 case FEED_TYPE_RSS_20:
53                         XML_SetElementHandler(parser,
54                                         feed_parser_rss20_start,
55                                         feed_parser_rss20_end);
56                         break;
57
58                 case FEED_TYPE_RDF:
59                         XML_SetElementHandler(parser,
60                                         feed_parser_rdf_start,
61                                         feed_parser_rdf_end);
62                         break;
63
64                 case FEED_TYPE_ATOM_10:
65                         XML_SetElementHandler(parser,
66                                         feed_parser_atom10_start,
67                                         feed_parser_atom10_end);
68                         break;
69         }
70 }
71
72 static void _elparse_start_chooser(void *data,
73                 const gchar *el, const gchar **attr)
74 {
75         FeedParserCtx *ctx = (FeedParserCtx *)data;
76         guint feedtype = FEED_TYPE_NONE;
77         gchar *version;
78
79         if( ctx->depth == 0 ) {
80
81                 /* RSS 2.0 detected */
82                 if( !strcmp(el, "rss") ) {
83                         feedtype = FEED_TYPE_RSS_20;
84                 } else if( !strcmp(el, "rdf:RDF") ) {
85                         feedtype = FEED_TYPE_RDF;
86                 } else if( !strcmp(el, "feed") ) {
87
88                         /* ATOM feed detected, let's check version */
89                         version = feed_parser_get_attribute_value(attr, "xmlns");
90                         if( version != NULL &&
91                                         (!strcmp(version, "http://www.w3.org/2005/Atom") ||
92                                          !strcmp(version, "https://www.w3.org/2005/Atom")) )
93                                 feedtype = FEED_TYPE_ATOM_10;
94                         else
95                                 feedtype = FEED_TYPE_ATOM_03;
96                 }
97         }
98
99         _handler_set(ctx->parser, feedtype);
100
101         ctx->depth++;
102 }
103
104 static void _elparse_end_dummy(void *data, const gchar *el)
105 {
106         FeedParserCtx *ctx = (FeedParserCtx *)data;
107
108         if( ctx->str != NULL ) {
109                 g_string_free(ctx->str, TRUE);
110                 ctx->str = NULL;
111         }
112
113         ctx->depth--;
114 }
115
116 void libfeed_expat_chparse(void *data, const gchar *s, gint len)
117 {
118         FeedParserCtx *ctx = (FeedParserCtx *)data;
119         gchar *buf = NULL;
120         gint i, xblank = 1;
121
122         buf = malloc(len+1);
123         strncpy(buf, s, len);
124         buf[len] = '\0';
125
126         /* check if the string is blank, ... */
127         for( i = 0; i < strlen(buf); i++ )
128                 if( !isspace(buf[i]) )
129                         xblank = 0;
130
131         /* ...because we do not want the blanks if we're just starting new GString */
132         if( xblank > 0 && ctx->str == NULL ) {
133                 g_free(buf);
134                 return;
135         }
136
137         if( ctx->str == NULL ) {
138                 ctx->str = g_string_sized_new(len + 1);
139         }
140
141         g_string_append(ctx->str, buf);
142         g_free(buf);
143 }
144
145
146 void feed_parser_set_expat_handlers(FeedParserCtx *ctx)
147 {
148         XML_SetUserData(ctx->parser, (void *)ctx);
149
150         XML_SetElementHandler(ctx->parser,
151                         _elparse_start_chooser,
152                         _elparse_end_dummy);
153
154         XML_SetCharacterDataHandler(ctx->parser,
155                 libfeed_expat_chparse);
156
157         XML_SetUnknownEncodingHandler(ctx->parser, feed_parser_unknown_encoding_handler,
158                         NULL);
159 }
160
161 size_t feed_writefunc(void *ptr, size_t size, size_t nmemb, void *data)
162 {
163         gint len = size * nmemb;
164         FeedParserCtx *ctx = (FeedParserCtx *)data;
165         gint status, err;
166
167         status = XML_Parse(ctx->parser, ptr, len, FALSE);
168
169         if( status == XML_STATUS_ERROR ) {
170                 err = XML_GetErrorCode(ctx->parser);
171                 printf("\nExpat: --- %s\n\n", XML_ErrorString(err));
172         }
173
174         return len;
175 }
176
177 gchar *feed_parser_get_attribute_value(const gchar **attr, const gchar *name)
178 {
179         guint i;
180
181         if( attr == NULL || name == NULL )
182                 return NULL;
183
184         for( i = 0; attr[i] != NULL && attr[i+1] != NULL; i += 2 ) {
185                 if( !strcmp( attr[i], name) )
186                         return (gchar *)attr[i+1];
187         }
188
189         /* We haven't found anything. */
190         return NULL;
191 }
192
193 #define CHARSIZEUTF32   4
194
195 enum {
196         LEP_ICONV_OK,
197         LEP_ICONV_FAILED,
198         LEP_ICONV_ILSEQ,
199         LEP_ICONV_INVAL,
200         LEP_ICONV_UNKNOWN
201 };
202
203 static gint giconv_utf32_char(GIConv cd, const gchar *inbuf, size_t insize,
204                 guint32 *p_value)
205 {
206 #ifdef HAVE_ICONV
207         size_t outsize;
208         guchar outbuf[CHARSIZEUTF32];
209         gchar *outbufp;
210         gint r;
211
212         outsize = sizeof(outbuf);
213         outbufp = (gchar *)outbuf;
214 #ifdef HAVE_ICONV_PROTO_CONST
215         r = g_iconv(cd, (const gchar **)&inbuf, &insize,
216                         &outbufp, &outsize);
217 #else
218         r = g_iconv(cd, (gchar **)&inbuf, &insize,
219                         &outbufp, &outsize);
220 #endif
221         if( r == -1 ) {
222                 g_iconv(cd, 0, 0, 0, 0);
223                 switch(errno) {
224                 case EILSEQ:
225                         return LEP_ICONV_ILSEQ;
226                 case EINVAL:
227                         return LEP_ICONV_INVAL;
228                 default:
229                         return LEP_ICONV_UNKNOWN;
230                 }
231         } else {
232                 guint32 value;
233                 guint i;
234
235                 if( (insize > 0) || (outsize > 0) )
236                         return LEP_ICONV_FAILED;
237
238                 value = 0;
239                 for( i = 0; i < sizeof(outbuf); i++ ) {
240                         value = (value << 8) + outbuf[i];
241                 }
242                 *p_value = value;
243                 return LEP_ICONV_OK;
244         }
245 #else
246         return LEP_ICONV_FAILED;
247 #endif
248 }
249
250 static gint feed_parser_setup_unknown_encoding(const gchar *charset,
251                 XML_Encoding *info)
252 {
253         GIConv cd;
254         gint flag, r;
255         gchar buf[4];
256         guint i, j, k;
257         guint32 value;
258
259         cd = g_iconv_open("UTF-32BE", charset);
260         if( cd == (GIConv) -1 )
261                 return -1;
262
263         flag = 0;
264         for( i = 0; i < 256; i++ ) {
265                 /* first char */
266                 buf[0] = i;
267                 info->map[i] = 0;
268                 r = giconv_utf32_char(cd, buf, 1, &value);
269                 if( r == LEP_ICONV_OK) {
270                         info->map[i] = value;
271                 } else if( r != LEP_ICONV_INVAL ) {
272                 } else {
273                         for( j = 0; j < 256; j++ ) {
274                                 /* second char */
275                                 buf[1] = j;
276                                 r = giconv_utf32_char(cd, buf, 2, &value);
277                                 if( r == LEP_ICONV_OK ) {
278                                         flag = 1;
279                                         info->map[i] = -2;
280                                 } else if( r != LEP_ICONV_INVAL ) {
281                                 } else {
282                                         for( k = 0; k < 256; k++ ) {
283                                                 /* third char */
284                                                 buf[2] = k;
285                                                 r = giconv_utf32_char(cd, buf, 3, &value);
286                                                 if( r == LEP_ICONV_OK) {
287                                                         info->map[i] = -3;
288                                                 }
289                                         }
290                                 }
291                         }
292                 }
293         }
294
295         g_iconv_close(cd);
296
297         return flag;
298 }
299
300 struct FeedParserUnknownEncoding {
301         gchar *charset;
302         GIConv cd;
303 };
304
305 static gint feed_parser_unknown_encoding_convert(void *data, const gchar *s)
306 {
307         gint r;
308         struct FeedParserUnknownEncoding *enc_data;
309         size_t insize;
310         guint32 value;
311
312         enc_data = data;
313         insize = 4;
314
315         if( s == NULL )
316                 return -1;
317
318         r = giconv_utf32_char(enc_data->cd, s, insize, &value);
319         if( r != LEP_ICONV_OK )
320                 return -1;
321
322         return 0;
323 }
324
325 static void feed_parser_unknown_encoding_data_free(void *data)
326 {
327         struct FeedParserUnknownEncoding *enc_data;
328
329         enc_data = data;
330         free(enc_data->charset);
331         g_iconv_close(enc_data->cd);
332         free(enc_data);
333 }
334
335 int feed_parser_unknown_encoding_handler(void *encdata, const XML_Char *name,
336                 XML_Encoding *info)
337 {
338         GIConv cd;
339         struct FeedParserUnknownEncoding *data;
340         int result;
341
342         result = feed_parser_setup_unknown_encoding(name, info);
343         if( result == 0 ) {
344                 info->data = NULL;
345                 info->convert = NULL;
346                 info->release = NULL;
347                 return XML_STATUS_OK;
348         }
349
350         cd = g_iconv_open("UTF-32BE", name);
351         if( cd == (GIConv)-1 )
352                 return XML_STATUS_ERROR;
353
354         data = malloc( sizeof(*data) );
355         if( data == NULL ) {
356                 g_iconv_close(cd);
357                 return XML_STATUS_ERROR;
358         }
359
360         data->charset = strdup(name);
361         if( data->charset == NULL ) {
362                 free(data);
363                 g_iconv_close(cd);
364                 return XML_STATUS_ERROR;
365         }
366
367         data->cd = cd;
368         info->data = data;
369         info->convert = feed_parser_unknown_encoding_convert;
370         info->release = feed_parser_unknown_encoding_data_free;
371
372         return XML_STATUS_OK;
373 }