64392456a421ce4dbd6d7c1eb9662e5123e53d99
[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( !strcmp(version, "http://www.w3.org/2005/Atom") ||
91                                         !strcmp(version, "https://www.w3.org/2005/Atom") )
92                                 feedtype = FEED_TYPE_ATOM_10;
93                         else
94                                 feedtype = FEED_TYPE_ATOM_03;
95                 }
96         }
97
98         _handler_set(ctx->parser, feedtype);
99
100         ctx->depth++;
101 }
102
103 static void _elparse_end_dummy(void *data, const gchar *el)
104 {
105         FeedParserCtx *ctx = (FeedParserCtx *)data;
106
107         if( ctx->str != NULL ) {
108                 g_string_free(ctx->str, TRUE);
109                 ctx->str = NULL;
110         }
111
112         ctx->depth--;
113 }
114
115 void libfeed_expat_chparse(void *data, const gchar *s, gint len)
116 {
117         FeedParserCtx *ctx = (FeedParserCtx *)data;
118         gchar *buf = NULL;
119         gint i, xblank = 1;
120
121         buf = malloc(len+1);
122         strncpy(buf, s, len);
123         buf[len] = '\0';
124
125         /* check if the string is blank, ... */
126         for( i = 0; i < strlen(buf); i++ )
127                 if( !isspace(buf[i]) )
128                         xblank = 0;
129
130         /* ...because we do not want the blanks if we're just starting new GString */
131         if( xblank > 0 && ctx->str == NULL ) {
132                 g_free(buf);
133                 return;
134         }
135
136         if( ctx->str == NULL ) {
137                 ctx->str = g_string_sized_new(len + 1);
138         }
139
140         g_string_append(ctx->str, buf);
141         g_free(buf);
142 }
143
144
145 void feed_parser_set_expat_handlers(FeedParserCtx *ctx)
146 {
147         XML_SetUserData(ctx->parser, (void *)ctx);
148
149         XML_SetElementHandler(ctx->parser,
150                         _elparse_start_chooser,
151                         _elparse_end_dummy);
152
153         XML_SetCharacterDataHandler(ctx->parser,
154                 libfeed_expat_chparse);
155
156         XML_SetUnknownEncodingHandler(ctx->parser, feed_parser_unknown_encoding_handler,
157                         NULL);
158 }
159
160 size_t feed_writefunc(void *ptr, size_t size, size_t nmemb, void *data)
161 {
162         gint len = size * nmemb;
163         FeedParserCtx *ctx = (FeedParserCtx *)data;
164         gint status, err;
165
166         status = XML_Parse(ctx->parser, ptr, len, FALSE);
167
168         if( status == XML_STATUS_ERROR ) {
169                 err = XML_GetErrorCode(ctx->parser);
170                 printf("\nExpat: --- %s\n\n", XML_ErrorString(err));
171         }
172
173         return len;
174 }
175
176 gchar *feed_parser_get_attribute_value(const gchar **attr, const gchar *name)
177 {
178         guint i;
179
180         if( attr == NULL || name == NULL )
181                 return NULL;
182
183         for( i = 0; attr[i] != NULL && attr[i+1] != NULL; i += 2 ) {
184                 if( !strcmp( attr[i], name) )
185                         return (gchar *)attr[i+1];
186         }
187
188         /* We haven't found anything. */
189         return NULL;
190 }
191
192 #define CHARSIZEUTF32   4
193
194 enum {
195         LEP_ICONV_OK,
196         LEP_ICONV_FAILED,
197         LEP_ICONV_ILSEQ,
198         LEP_ICONV_INVAL,
199         LEP_ICONV_UNKNOWN
200 };
201
202 static gint giconv_utf32_char(GIConv cd, const gchar *inbuf, size_t insize,
203                 guint32 *p_value)
204 {
205 #ifdef HAVE_ICONV
206         size_t outsize;
207         guchar outbuf[CHARSIZEUTF32];
208         gchar *outbufp;
209         gint r, errno;
210
211         outsize = sizeof(outbuf);
212         outbufp = (gchar *)outbuf;
213 #ifdef HAVE_ICONV_PROTO_CONST
214         r = g_iconv(cd, (const gchar **)&inbuf, &insize,
215                         &outbufp, &outsize);
216 #else
217         r = g_iconv(cd, (gchar **)&inbuf, &insize,
218                         &outbufp, &outsize);
219 #endif
220         if( r == -1 ) {
221                 g_iconv(cd, 0, 0, 0, 0);
222                 switch(errno) {
223                 case EILSEQ:
224                         return LEP_ICONV_ILSEQ;
225                 case EINVAL:
226                         return LEP_ICONV_INVAL;
227                 default:
228                         return LEP_ICONV_UNKNOWN;
229                 }
230         } else {
231                 guint32 value;
232                 guint i;
233
234                 if( (insize > 0) || (outsize > 0) )
235                         return LEP_ICONV_FAILED;
236
237                 value = 0;
238                 for( i = 0; i < sizeof(outbuf); i++ ) {
239                         value = (value << 8) + outbuf[i];
240                 }
241                 *p_value = value;
242                 return LEP_ICONV_OK;
243         }
244 #else
245         return LEP_ICONV_FAILED;
246 #endif
247 }
248
249 static gint feed_parser_setup_unknown_encoding(const gchar *charset,
250                 XML_Encoding *info)
251 {
252         GIConv cd;
253         gint flag, r;
254         gchar buf[4];
255         guint i, j, k;
256         guint32 value;
257
258         cd = g_iconv_open("UTF-32BE", charset);
259         if( cd == (GIConv) -1 )
260                 return -1;
261
262         flag = 0;
263         for( i = 0; i < 256; i++ ) {
264                 /* first char */
265                 buf[0] = i;
266                 info->map[i] = 0;
267                 r = giconv_utf32_char(cd, buf, 1, &value);
268                 if( r == LEP_ICONV_OK) {
269                         info->map[i] = value;
270                 } else if( r != LEP_ICONV_INVAL ) {
271                 } else {
272                         for( j = 0; j < 256; j++ ) {
273                                 /* second char */
274                                 buf[1] = j;
275                                 r = giconv_utf32_char(cd, buf, 2, &value);
276                                 if( r == LEP_ICONV_OK ) {
277                                         flag = 1;
278                                         info->map[i] = -2;
279                                 } else if( r != LEP_ICONV_INVAL ) {
280                                 } else {
281                                         for( k = 0; k < 256; k++ ) {
282                                                 /* third char */
283                                                 buf[2] = k;
284                                                 r = giconv_utf32_char(cd, buf, 3, &value);
285                                                 if( r == LEP_ICONV_OK) {
286                                                         info->map[i] = -3;
287                                                 }
288                                         }
289                                 }
290                         }
291                 }
292         }
293
294         g_iconv_close(cd);
295
296         return flag;
297 }
298
299 struct FeedParserUnknownEncoding {
300         gchar *charset;
301         GIConv cd;
302 };
303
304 static gint feed_parser_unknown_encoding_convert(void *data, const gchar *s)
305 {
306         gint r;
307         struct FeedParserUnknownEncoding *enc_data;
308         size_t insize;
309         guint32 value;
310
311         enc_data = data;
312         insize = 4;
313
314         if( s == NULL )
315                 return -1;
316
317         r = giconv_utf32_char(enc_data->cd, s, insize, &value);
318         if( r != LEP_ICONV_OK )
319                 return -1;
320
321         return 0;
322 }
323
324 static void feed_parser_unknown_encoding_data_free(void *data)
325 {
326         struct FeedParserUnknownEncoding *enc_data;
327
328         enc_data = data;
329         free(enc_data->charset);
330         g_iconv_close(enc_data->cd);
331         free(enc_data);
332 }
333
334 int feed_parser_unknown_encoding_handler(void *encdata, const XML_Char *name,
335                 XML_Encoding *info)
336 {
337         GIConv cd;
338         struct FeedParserUnknownEncoding *data;
339         int result;
340
341         result = feed_parser_setup_unknown_encoding(name, info);
342         if( result == 0 ) {
343                 info->data = NULL;
344                 info->convert = NULL;
345                 info->release = NULL;
346                 return XML_STATUS_OK;
347         }
348
349         cd = g_iconv_open("UTF-32BE", name);
350         if( cd == (GIConv)-1 )
351                 return XML_STATUS_ERROR;
352
353         data = malloc( sizeof(*data) );
354         if( data == NULL ) {
355                 g_iconv_close(cd);
356                 return XML_STATUS_ERROR;
357         }
358
359         data->charset = strdup(name);
360         if( data->charset == NULL ) {
361                 free(data);
362                 g_iconv_close(cd);
363                 return XML_STATUS_ERROR;
364         }
365
366         data->cd = cd;
367         info->data = data;
368         info->convert = feed_parser_unknown_encoding_convert;
369         info->release = feed_parser_unknown_encoding_data_free;
370
371         return XML_STATUS_OK;
372 }