Removed some memory leaks and some coding style fixes.
[platform/upstream/lightmediascanner.git] / src / plugins / asf / asf.c
1 /**
2  * Copyright (C) 2008 by INdT
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * @author Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * asf/wma file parser.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <lightmediascanner_plugin.h>
32 #include <lightmediascanner_db.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 enum StreamTypes {
41     STREAM_TYPE_UNKNOWN = 0,
42     STREAM_TYPE_AUDIO,
43     STREAM_TYPE_VIDEO
44 };
45
46 enum AttributeTypes {
47     ATTR_TYPE_UNICODE = 0,
48     ATTR_TYPE_BYTES,
49     ATTR_TYPE_BOOL,
50     ATTR_TYPE_DWORD,
51     ATTR_TYPE_QWORD,
52     ATTR_TYPE_WORD,
53     ATTR_TYPE_GUID
54 };
55
56 struct asf_info {
57     struct lms_string_size title;
58     struct lms_string_size artist;
59     struct lms_string_size album;
60     struct lms_string_size genre;
61     unsigned char trackno;
62 };
63
64 struct plugin {
65     struct lms_plugin plugin;
66     lms_db_audio_t *audio_db;
67     lms_db_video_t *video_db;
68     lms_charset_conv_t *cs_conv;
69 };
70
71 static const char _name[] = "asf";
72 static const struct lms_string_size _exts[] = {
73     LMS_STATIC_STRING_SIZE(".wma"),
74     LMS_STATIC_STRING_SIZE(".wmv"),
75     LMS_STATIC_STRING_SIZE(".asf")
76 };
77
78 /* ASF GUIDs
79  *
80  * Microsoft defines these 16-byte (128-bit) GUIDs as:
81  * first 8 bytes are in little-endian order
82  * next 8 bytes are in big-endian order
83  *
84  * Eg.: AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp:
85  *
86  * to convert to byte string do as follow:
87  *
88  * $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
89  *
90  * See http://www.microsoft.com/windows/windowsmedia/forpros/format/asfspec.aspx
91  */
92 static const char header_guid[16] = "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
93 static const char file_properties_guid[16] = "\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65";
94 static const char stream_properties_guid[16] = "\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65";
95 static const char stream_type_audio_guid[16] = "\x40\x9E\x69\xF8\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
96 static const char stream_type_video_guid[16] = "\xC0\xEF\x19\xBC\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
97 static const char content_description_guid[16] = "\x33\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
98 static const char extended_content_description_guid[16] = "\x40\xA4\xD0\xD2\x07\xE3\xD2\x11\x97\xF0\x00\xA0\xC9\x5E\xA8\x50";
99 static const char header_extension_guid[16] = "\xb5\x03\xbf_.\xa9\xcf\x11\x8e\xe3\x00\xc0\x0c Se";
100 static const char metadata_guid[16] = "\xEA\xCB\xF8\xC5\xAF[wH\204g\xAA\214D\xFAL\xCA";
101 static const char metadata_library_guid[16] = "\224\034#D\230\224\321I\241A\x1d\x13NEpT";
102 static const char content_encryption_object_guid[16] = "\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E";
103 static const char extended_content_encryption_object_guid[16] = "\x14\xE6\x8A\x29\x22\x26\x17\x4C\xB9\x35\xDA\xE0\x7E\xE9\x28\x9C";
104
105 static long long
106 _to_number(const char *data, unsigned int type_size, unsigned int data_size)
107 {
108     long long sum = 0;
109     unsigned int last, i;
110
111     last = data_size > type_size ? type_size : data_size;
112
113     for (i = 0; i < last; i++)
114         sum |= (unsigned char) (data[i]) << (i * 8);
115
116     return sum;
117 }
118
119 static short
120 _read_word(int fd)
121 {
122     char v[2];
123     if (read(fd, &v, 2) != 2)
124         return 0;
125     return (short) _to_number(v, sizeof(unsigned short), 2);
126 }
127
128 static unsigned int
129 _read_dword(int fd)
130 {
131     char v[4];
132     if (read(fd, &v, 4) != 4)
133         return 0;
134     return (unsigned int) _to_number(v, sizeof(unsigned int), 4);
135 }
136
137 static long long
138 _read_qword(int fd)
139 {
140     char v[8];
141     if (read(fd, &v, 8) != 8)
142         return 0;
143     return _to_number(v, sizeof(unsigned long long), 8);
144 }
145
146 static int
147 _read_string(int fd, size_t count, char **str, size_t *len)
148 {
149     char *data;
150     size_t data_size, size;
151
152     if (!str) {
153         lseek(fd, count, SEEK_CUR);
154         return 0;
155     }
156
157     data = malloc(sizeof(char) * count);
158     data_size = read(fd, data, count);
159     if (data_size == -1) {
160         free(data);
161         return -1;
162     }
163
164     size = data_size;
165     while (size >= 2) {
166         if (data[size - 1] != '\0' || data[size - 2] != '\0')
167             break;
168         size -= 2;
169     }
170
171     *str = data;
172     *len = size;
173
174     return 0;
175 }
176
177 static void
178 _parse_content_description(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
179 {
180     int title_length = _read_word(fd);
181     int artist_length = _read_word(fd);
182     int copyright_length = _read_word(fd);
183     int comment_length = _read_word(fd);
184     int rating_length = _read_word(fd);
185     int len;
186     char *copyright, *comment, *rating;
187
188     _read_string(fd, title_length, &info->title.str, &info->title.len);
189     lms_charset_conv_force(cs_conv, &info->title.str, &info->title.len);
190     _read_string(fd, artist_length, &info->artist.str, &info->artist.len);
191     lms_charset_conv_force(cs_conv, &info->artist.str, &info->artist.len);
192     /* ignore copyright, comment and rating */
193     _read_string(fd, copyright_length, NULL, NULL);
194     _read_string(fd, comment_length, NULL, NULL);
195     _read_string(fd, rating_length, NULL, NULL);
196 }
197
198 static void
199 _parse_attribute_name(int fd,
200                       int kind,
201                       char **attr_name,
202                       size_t *attr_name_len,
203                       int *attr_type,
204                       int *attr_size)
205 {
206     int attr_name_length;
207
208     /* extended content descriptor */
209     if (kind == 0) {
210         attr_name_length = _read_word(fd);
211         _read_string(fd, attr_name_length, attr_name, attr_name_len);
212         *attr_type = _read_word(fd);
213         *attr_size = _read_word(fd);
214     }
215     /* metadata & metadata library */
216     else {
217         lseek(fd, 2, SEEK_CUR); /* language */
218         lseek(fd, 2, SEEK_CUR); /* stream */
219         attr_name_length = _read_word(fd);
220         *attr_type = _read_word(fd);
221         *attr_size = _read_dword(fd);
222         _read_string(fd, attr_name_length, attr_name, attr_name_len);
223     }
224 }
225
226 static void
227 _parse_attribute_string_data(lms_charset_conv_t *cs_conv,
228                              int fd,
229                              int attr_size,
230                              char **attr_data,
231                              size_t *attr_data_len)
232 {
233     _read_string(fd, attr_size, attr_data, attr_data_len);
234     lms_charset_conv_force(cs_conv, attr_data, attr_data_len);
235 }
236
237 static void
238 _skip_attribute_data(int fd, int kind, int attr_type, int attr_size)
239 {
240     switch (attr_type) {
241     case ATTR_TYPE_WORD:
242         lseek(fd, 2, SEEK_CUR);
243         break;
244
245     case ATTR_TYPE_BOOL:
246         if (kind == 0)
247             lseek(fd, 4, SEEK_CUR);
248         else
249             lseek(fd, 2, SEEK_CUR);
250         break;
251
252     case ATTR_TYPE_DWORD:
253         lseek(fd, 4, SEEK_CUR);
254         break;
255
256     case ATTR_TYPE_QWORD:
257         lseek(fd, 8, SEEK_CUR);
258         break;
259
260     case ATTR_TYPE_UNICODE:
261     case ATTR_TYPE_BYTES:
262     case ATTR_TYPE_GUID:
263         lseek(fd, attr_size, SEEK_CUR);
264         break;
265
266     default:
267         break;
268     }
269 }
270
271 static void
272 _skip_attribute(int fd, int kind)
273 {
274     int attr_type, attr_size;
275     _parse_attribute_name(fd, kind, NULL, NULL, &attr_type, &attr_size);
276     _skip_attribute_data(fd, kind, attr_type, attr_size);
277 }
278
279 static void
280 _parse_extended_content_description_object(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
281 {
282     int count = _read_word(fd);
283     char *attr_name;
284     size_t attr_name_len;
285     int attr_type, attr_size;
286     while (count--) {
287         attr_name = NULL;
288         _parse_attribute_name(fd, 0,
289                               &attr_name, &attr_name_len,
290                               &attr_type, &attr_size);
291         if (attr_type == ATTR_TYPE_UNICODE) {
292             lms_charset_conv_force(cs_conv, &attr_name, &attr_name_len);
293             if (strcmp(attr_name, "WM/AlbumTitle") == 0)
294                 _parse_attribute_string_data(cs_conv,
295                                              fd, attr_size,
296                                              &info->album.str,
297                                              &info->album.len);
298             else if (strcmp(attr_name, "WM/Genre") == 0)
299                 _parse_attribute_string_data(cs_conv,
300                                              fd, attr_size,
301                                              &info->genre.str,
302                                              &info->genre.len);
303             else if (strcmp(attr_name, "WM/TrackNumber") == 0) {
304                 char *trackno;
305                 size_t trackno_len;
306                 _parse_attribute_string_data(cs_conv,
307                                              fd, attr_size,
308                                              &trackno,
309                                              &trackno_len);
310                 if (trackno) {
311                     info->trackno = atoi(trackno);
312                     free(trackno);
313                 }
314             }
315             else
316                 _skip_attribute_data(fd, 0, attr_type, attr_size);
317         }
318         else
319             _skip_attribute_data(fd, 0, attr_type, attr_size);
320         if (attr_name)
321             free(attr_name);
322     }
323 }
324
325 static void
326 _skip_metadata(int fd)
327 {
328     int count = _read_word(fd);
329     while (count--)
330         _skip_attribute(fd, 1);
331 }
332
333 static void
334 _skip_metadata_library(int fd)
335 {
336     int count = _read_word(fd);
337     while (count--)
338         _skip_attribute(fd, 2);
339 }
340
341 static void
342 _skip_header_extension(int fd)
343 {
344     char guid[16];
345     long long size, data_size, data_pos;
346
347     lseek(fd, 18, SEEK_CUR);
348     data_size = _read_dword(fd);
349     data_pos = 0;
350     while (data_pos < data_size) {
351         read(fd, &guid, 16);
352         size = _read_qword(fd);
353         if (memcmp(guid, metadata_guid, 16) == 0)
354             _skip_metadata(fd);
355         else if (memcmp(guid, metadata_library_guid, 16) == 0)
356             _skip_metadata_library(fd);
357         else
358             lseek(fd, size - 24, SEEK_CUR);
359         data_pos += size;
360     }
361 }
362
363 static void
364 _strstrip(char **str, unsigned int *p_len)
365 {
366     if (*str)
367         lms_strstrip(*str, p_len);
368
369     if (*p_len == 0 && *str) {
370         free(*str);
371         *str = NULL;
372     }
373 }
374
375 static void *
376 _match(struct plugin *p, const char *path, int len, int base)
377 {
378     int i;
379
380     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
381     if (i < 0)
382       return NULL;
383     else
384       return (void*)(i + 1);
385 }
386
387 static int
388 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
389 {
390     struct asf_info info = {0};
391     struct lms_audio_info audio_info = {0};
392     struct lms_video_info video_info = {0};
393     int r, fd, num_objects, i;
394     char guid[16];
395     unsigned int size;
396     int stream_type = STREAM_TYPE_UNKNOWN;
397
398     fd = open(finfo->path, O_RDONLY);
399     if (fd < 0) {
400         perror("open");
401         return -1;
402     }
403
404     if (read(fd, &guid, 16) != 16) {
405         perror("read");
406         r = -2;
407         goto done;
408     }
409     if (memcmp(guid, header_guid, 16) != 0) {
410         fprintf(stderr, "ERROR: invalid header (%s).\n", finfo->path);
411         r = -3;
412         goto done;
413     }
414
415     size = _read_qword(fd);
416     num_objects = _read_dword(fd);
417
418     lseek(fd, 2, SEEK_CUR);
419
420     for (i = 0; i < num_objects; ++i) {
421         read(fd, &guid, 16);
422         size = _read_qword(fd);
423
424         if (memcmp(guid, file_properties_guid, 16) == 0)
425             lseek(fd, size - 24, SEEK_CUR);
426         else if (memcmp(guid, stream_properties_guid, 16) == 0) {
427             read(fd, &guid, 16);
428             if (memcmp(guid, stream_type_audio_guid, 16) == 0)
429                 stream_type = STREAM_TYPE_AUDIO;
430             else if (memcmp(guid, stream_type_video_guid, 16) == 0)
431                 stream_type = STREAM_TYPE_VIDEO;
432             lseek(fd, size - 40, SEEK_CUR);
433         }
434         else if (memcmp(guid, content_description_guid, 16) == 0)
435             _parse_content_description(plugin->cs_conv, fd, &info);
436         else if (memcmp(guid, extended_content_description_guid, 16) == 0)
437             _parse_extended_content_description_object(plugin->cs_conv, fd, &info);
438         else if (memcmp(guid, header_extension_guid, 16) == 0)
439             _skip_header_extension(fd);
440         else if (memcmp(guid, content_encryption_object_guid, 16) == 0 ||
441                  memcmp(guid, extended_content_encryption_object_guid, 16) == 0) {
442             /* ignore DRM'd files */
443             fprintf(stderr, "ERROR: ignoring DRM'd file %s\n", finfo->path);
444             r = -4;
445             goto done;
446         }
447         else
448             lseek(fd, size - 24, SEEK_CUR);
449     }
450
451     /* try to define stream type by extension */
452     if (stream_type == STREAM_TYPE_UNKNOWN) {
453         int ext_idx = ((int)match) - 1;
454         if (strcmp(_exts[ext_idx].str, ".wma") == 0)
455             stream_type = STREAM_TYPE_AUDIO;
456         /* consider wmv and asf as video */
457         else
458             stream_type = STREAM_TYPE_VIDEO;
459     }
460
461     _strstrip(&info.title.str, &info.title.len);
462     _strstrip(&info.artist.str, &info.genre.len);
463     _strstrip(&info.album.str, &info.album.len);
464     _strstrip(&info.genre.str, &info.genre.len);
465
466     if (!info.title.str) {
467         int ext_idx;
468         ext_idx = ((int)match) - 1;
469         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
470         info.title.str = malloc((info.title.len + 1) * sizeof(char));
471         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
472         info.title.str[info.title.len] = '\0';
473         lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
474     }
475
476 #if 0
477     fprintf(stderr, "file %s info\n", finfo->path);
478     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
479     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
480     fprintf(stderr, "\talbum='%s'\n", info.album.str);
481     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
482     fprintf(stderr, "\ttrackno=%d\n", info.trackno);
483 #endif
484
485     if (stream_type == STREAM_TYPE_AUDIO) {
486         audio_info.id = finfo->id;
487         audio_info.title = info.title;
488         audio_info.artist = info.artist;
489         audio_info.album = info.album;
490         audio_info.genre = info.genre;
491         audio_info.trackno = info.trackno;
492         r = lms_db_audio_add(plugin->audio_db, &audio_info);
493     }
494     else {
495         video_info.id = finfo->id;
496         video_info.title = info.title;
497         video_info.artist = info.artist;
498         r = lms_db_video_add(plugin->video_db, &video_info);
499     }
500
501   done:
502     if (info.title.str)
503         free(info.title.str);
504     if (info.artist.str)
505         free(info.artist.str);
506     if (info.album.str)
507         free(info.album.str);
508     if (info.genre.str)
509         free(info.genre.str);
510
511     posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
512     close(fd);
513
514     return r;
515 }
516
517 static int
518 _setup(struct plugin *plugin, struct lms_context *ctxt)
519 {
520     plugin->audio_db = lms_db_audio_new(ctxt->db);
521     if (!plugin->audio_db)
522         return -1;
523     plugin->video_db = lms_db_video_new(ctxt->db);
524     if (!plugin->video_db)
525         return -1;
526     plugin->cs_conv = lms_charset_conv_new();
527     if (!plugin->cs_conv)
528         return -1;
529     lms_charset_conv_add(plugin->cs_conv, "UTF-16LE");
530
531     return 0;
532 }
533
534 static int
535 _start(struct plugin *plugin, struct lms_context *ctxt)
536 {
537     int r;
538     r = lms_db_audio_start(plugin->audio_db);
539     r |= lms_db_video_start(plugin->video_db);
540     return r;
541 }
542
543 static int
544 _finish(struct plugin *plugin, struct lms_context *ctxt)
545 {
546     if (plugin->audio_db)
547         lms_db_audio_free(plugin->audio_db);
548     if (plugin->video_db)
549         lms_db_video_free(plugin->video_db);
550     if (plugin->cs_conv)
551         lms_charset_conv_free(plugin->cs_conv);
552
553     return 0;
554 }
555
556 static int
557 _close(struct plugin *plugin)
558 {
559     free(plugin);
560     return 0;
561 }
562
563 API struct lms_plugin *
564 lms_plugin_open(void)
565 {
566     struct plugin *plugin;
567
568     plugin = (struct plugin *)malloc(sizeof(*plugin));
569     plugin->plugin.name = _name;
570     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
571     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
572     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
573     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
574     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
575     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
576
577     return (struct lms_plugin *)plugin;
578 }