Speed up id3v1 genre usage.
[platform/upstream/lightmediascanner.git] / src / plugins / id3 / id3.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  * id3 file parser.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #define _GNU_SOURCE
32 #define _XOPEN_SOURCE 600
33 #include <lightmediascanner_plugin.h>
34 #include <lightmediascanner_db.h>
35 #include <lightmediascanner_charset_conv.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <ctype.h>
44
45 #define ID3V2_HEADER_SIZE       10
46 #define ID3V2_FOOTER_SIZE       10
47
48 enum ID3Encodings {
49     ID3_ENCODING_LATIN1 = 0,
50     ID3_ENCODING_UTF16,
51     ID3_ENCODING_UTF16BE,
52     ID3_ENCODING_UTF8,
53     ID3_ENCODING_UTF16LE,
54     ID3_ENCODING_LAST
55 };
56 #define ID3_NUM_ENCODINGS ID3_ENCODING_LAST
57
58
59 #include "id3v1_genres.c"
60
61 struct id3_info {
62     struct lms_string_size title;
63     struct lms_string_size artist;
64     struct lms_string_size album;
65     struct lms_string_size genre;
66     unsigned char trackno;
67     int cur_artist_priority;
68 };
69
70 struct id3v2_frame_header {
71     char frame_id[4];
72     unsigned int frame_size;
73     int compression;
74     int data_length_indicator;
75 };
76
77 struct id3v1_tag {
78     char title[30];
79     char artist[30];
80     char album[30];
81     char year[4];
82     char comments[30];
83     char genre;
84 } __attribute__((packed));
85
86 struct plugin {
87     struct lms_plugin plugin;
88     lms_db_audio_t *audio_db;
89     lms_charset_conv_t *cs_convs[ID3_NUM_ENCODINGS];
90 };
91
92 static const char _name[] = "id3";
93 static const struct lms_string_size _exts[] = {
94     LMS_STATIC_STRING_SIZE(".mp3"),
95     LMS_STATIC_STRING_SIZE(".aac")
96 };
97
98 static unsigned int
99 _to_uint(const char *data, int data_size)
100 {
101     unsigned int sum = 0;
102     unsigned int last, i;
103
104     last = data_size > 4 ? 3 : data_size - 1;
105
106     for (i = 0; i <= last; i++)
107         sum |= ((unsigned char) data[i]) << ((last - i) * 8);
108
109     return sum;
110 }
111
112 static inline int
113 _is_id3v2_second_synch_byte(unsigned char byte)
114 {
115     if (byte == 0xff)
116         return 0;
117     if ((byte & 0xE0) == 0xE0)
118         return 1;
119     return 0;
120 }
121
122 static long
123 _find_id3v2(int fd)
124 {
125     long buffer_offset = 0;
126     char buffer[3], *p;
127     int buffer_size = sizeof(buffer);
128     const char pattern[] = "ID3";
129     ssize_t nread;
130
131     /* These variables are used to keep track of a partial match that happens at
132      * the end of a buffer. */
133     int previous_partial_match = -1;
134     int previous_partial_synch_match = 0;
135     int first_synch_byte;
136
137     /* Start the search at the beginning of the file. */
138     lseek(fd, 0, SEEK_SET);
139
140     if ((nread = read(fd, &buffer, buffer_size)) != buffer_size)
141         return -1;
142
143     /* check if pattern is in the beggining of the file */
144     if (memcmp(buffer, pattern, 3) == 0)
145         return 0;
146
147     /* This loop is the crux of the find method.  There are three cases that we
148      * want to account for:
149      * (1) The previously searched buffer contained a partial match of the search
150      * pattern and we want to see if the next one starts with the remainder of
151      * that pattern.
152      *
153      * (2) The search pattern is wholly contained within the current buffer.
154      *
155      * (3) The current buffer ends with a partial match of the pattern.  We will
156      * note this for use in the next iteration, where we will check for the rest
157      * of the pattern. */
158     while (1) {
159         /* (1) previous partial match */
160         if (previous_partial_synch_match && _is_id3v2_second_synch_byte(buffer[0]))
161             return -1;
162
163         if (previous_partial_match >= 0 && previous_partial_match < buffer_size) {
164             const int pattern_offset = buffer_size - previous_partial_match;
165
166             if (memcmp(buffer, pattern + pattern_offset, 3 - pattern_offset) == 0)
167                 return buffer_offset - buffer_size + previous_partial_match;
168         }
169
170         /* (2) pattern contained in current buffer */
171         p = buffer;
172         while ((p = memchr(p, 'I', buffer_size - (p - buffer)))) {
173             if (buffer_size - (p - buffer) < 3)
174                 break;
175
176             if (memcmp(p, pattern, 3) == 0)
177                 return buffer_offset + (p - buffer);
178
179             p += 1;
180         }
181
182         p = memchr(buffer, 255, buffer_size);
183         if (p)
184             first_synch_byte = p - buffer;
185         else
186             first_synch_byte = -1;
187
188         /* Here we have to loop because there could be several of the first
189          * (11111111) byte, and we want to check all such instances until we find
190          * a full match (11111111 111) or hit the end of the buffer. */
191         while (first_synch_byte >= 0) {
192             /* if this *is not* at the end of the buffer */
193             if (first_synch_byte < buffer_size - 1) {
194                 if(_is_id3v2_second_synch_byte(buffer[first_synch_byte + 1]))
195                     /* We've found the frame synch pattern. */
196                     return -1;
197                 else
198                     /* We found 11111111 at the end of the current buffer indicating a
199                      * partial match of the synch pattern.  The find() below should
200                      * return -1 and break out of the loop. */
201                     previous_partial_synch_match = 1;
202             }
203
204             /* Check in the rest of the buffer. */
205             p = memchr(p + 1, 255, buffer_size - (p + 1 - buffer));
206             if (p)
207                 first_synch_byte = p - buffer;
208             else
209                 first_synch_byte = -1;
210         }
211
212         /* (3) partial match */
213         if (buffer[nread - 1] == pattern[1])
214             previous_partial_match = nread - 1;
215         else if (memcmp(&buffer[nread - 2], pattern, 2) == 0)
216             previous_partial_match = nread - 2;
217         buffer_offset += buffer_size;
218
219         if ((nread = read(fd, &buffer, sizeof(buffer))) == -1)
220             return -1;
221     }
222
223     return -1;
224 }
225
226 static unsigned int
227 _get_id3v2_frame_header_size(unsigned int version)
228 {
229     switch (version) {
230     case 0:
231     case 1:
232     case 2:
233         return 6;
234     case 3:
235     case 4:
236     default:
237         return 10;
238     }
239 }
240
241 static void
242 _parse_id3v2_frame_header(char *data, unsigned int version, struct id3v2_frame_header *fh)
243 {
244     switch (version) {
245     case 0:
246     case 1:
247     case 2:
248         memcpy(fh->frame_id, data, 3);
249         fh->frame_id[3] = 0;
250         fh->frame_size = _to_uint(data + 3, 3);
251         fh->compression = 0;
252         fh->data_length_indicator = 0;
253         break;
254     case 3:
255         memcpy(fh->frame_id, data, 4);
256         fh->frame_size = _to_uint(data + 4, 4);
257         fh->compression = data[9] & 0x40;
258         fh->data_length_indicator = 0;
259         break;
260     case 4:
261     default:
262         memcpy(fh->frame_id, data, 4);
263         fh->frame_size = _to_uint(data + 4, 4);
264         fh->compression = data[9] & 0x4;
265         fh->data_length_indicator = data[9] & 0x1;
266         break;
267     }
268 }
269
270 static inline void
271 _get_id3v2_frame_info(const char *frame_data, unsigned int frame_size, struct lms_string_size *s, lms_charset_conv_t *cs_conv)
272 {
273     if (frame_size > s->len)
274         s->str = realloc(s->str, sizeof(char) * (frame_size + 1));
275     memcpy(s->str, frame_data, frame_size);
276     s->str[frame_size] = '\0';
277     s->len = frame_size;
278     if (cs_conv)
279         lms_charset_conv(cs_conv, &s->str, &s->len);
280 }
281
282 static int
283 _get_id3v1_genre(unsigned int genre, struct lms_string_size *out)
284 {
285     if (genre < ID3V1_NUM_GENRES) {
286         unsigned int size, base;
287
288         base = id3v1_genres_offsets[genre];
289         size = id3v1_genres_offsets[genre + 1] - base;
290         out->str = malloc(size);
291         out->len = size - 1;
292         memcpy(out->str, id3v1_genres_mem + base, size);
293
294         return 0;
295     }
296     return -1;
297 }
298
299 static inline int
300 _parse_id3v1_genre(const char *str_genre, struct lms_string_size *out)
301 {
302     return _get_id3v1_genre(atoi(str_genre), out);
303 }
304
305 static inline void
306 _get_id3v2_genre(const char *frame_data, unsigned int frame_size, struct lms_string_size *out, lms_charset_conv_t *cs_conv)
307 {
308     int i, is_number;
309     struct lms_string_size genre = {0};
310
311     _get_id3v2_frame_info(frame_data, frame_size, &genre, cs_conv);
312
313     if (!genre.str)
314         return;
315
316     if (out->str)
317         free(out->str);
318
319     is_number = (genre.len != 0 && genre.str[0] != '(');
320     if (is_number) {
321         for (i = 0; i < genre.len; ++i) {
322             if (!isdigit(genre.str[i])) {
323                 is_number = 0;
324                 break;
325             }
326         }
327     }
328
329     if (is_number && _parse_id3v1_genre(genre.str, out) == 0) {
330         /* id3v1 genre found */
331         free(genre.str);
332         return;
333     }
334
335     /* ID3v2.3 "content type" can contain a ID3v1 genre number in parenthesis at
336      * the beginning of the field. If this is all that the field contains, do a
337      * translation from that number to the name and return that.  If there is a
338      * string folloing the ID3v1 genre number, that is considered to be
339      * authoritative and we return that instead. Or finally, the field may
340      * simply be free text, in which case we just return the value. */
341
342     if (genre.len > 1 && genre.str[0] == '(') {
343         char *closing = NULL;
344
345         if (genre.str[genre.len - 1] == ')') {
346             closing = strchr(genre.str, ')');
347             if (closing == genre.str + genre.len - 1) {
348                 /* ) is the last character and only appears once in the string */
349                 /* get the id3v1 genre enclosed by parentheses */
350                 if (_parse_id3v1_genre(genre.str + 1, out) == 0) {
351                     free(genre.str);
352                     return;
353                 }
354             }
355         }
356
357         /* get the string followed by the id3v1 genre */
358         if (!closing)
359             closing = strchr(genre.str, ')');
360
361         if (closing) {
362             out->str = strdup(closing + 1);
363             out->len = genre.len - (closing + 1 - genre.str);
364             free(genre.str);
365             return;
366         }
367     }
368
369     /* pure text */
370     *out = genre;
371 }
372
373 static void
374 _parse_id3v2_frame(struct id3v2_frame_header *fh, const char *frame_data, struct id3_info *info, lms_charset_conv_t **cs_convs)
375 {
376     lms_charset_conv_t *cs_conv = NULL;
377     unsigned int text_encoding, frame_size;
378     static const int artist_priorities[] = { 3, 4, 2, 1 };
379
380 #if 0
381     fprintf(stderr, "frame id = %.4s frame size = %d text encoding = %d\n", fh->frame_id, fh->frame_size, frame_data[0]);
382 #endif
383
384     /* Latin1  = 0
385      * UTF16   = 1
386      * UTF16BE = 2
387      * UTF8    = 3
388      * UTF16LE = 4
389      */
390     text_encoding = frame_data[0];
391
392     /* skip first byte - text encoding */
393     frame_data += 1;
394     frame_size = fh->frame_size - 1;
395
396     if (text_encoding >= 0 && text_encoding < ID3_NUM_ENCODINGS) {
397         if (text_encoding == ID3_ENCODING_UTF16) {
398             if (memcmp(frame_data, "\xfe\xff", 2) == 0)
399                 text_encoding = ID3_ENCODING_UTF16BE;
400             else
401                 text_encoding = ID3_ENCODING_UTF16LE;
402             frame_data += 2;
403             frame_size -= 2;
404         }
405         cs_conv = cs_convs[text_encoding];
406     }
407
408     /* ID3v2.2 used 3 bytes for the frame id, so let's check it */
409     if (memcmp(fh->frame_id, "TIT2", 4) == 0 ||
410         memcmp(fh->frame_id, "TT2", 3) == 0)
411         _get_id3v2_frame_info(frame_data, frame_size, &info->title, cs_conv);
412     else if (memcmp(fh->frame_id, "TP", 2) == 0) {
413         int index = -1;
414         struct lms_string_size artist = {0};
415
416         if (memcmp(fh->frame_id, "TPE", 3) == 0) {
417             /* this check shouldn't be needed, but let's make sure */
418             if (fh->frame_id[3] >= '1' && fh->frame_id[3] <= '4')
419                 index = fh->frame_id[3] - '1';
420         }
421         else {
422             /* ignore TPA, TPB */
423             if (fh->frame_id[2] >= '1' && fh->frame_id[2] <= '4')
424                 index = fh->frame_id[2] - '1';
425         }
426
427         if (index != -1 &&
428             artist_priorities[index] > info->cur_artist_priority) {
429             info->cur_artist_priority = artist_priorities[index];
430             _get_id3v2_frame_info(frame_data, frame_size, &artist, cs_conv);
431             lms_string_size_strip_and_free(&artist);
432             if (artist.str) {
433                 if (info->artist.str)
434                     free(info->artist.str);
435                 info->artist = artist;
436             }
437         }
438     }
439     /* TALB, TAL */
440     else if (memcmp(fh->frame_id, "TAL", 3) == 0)
441         _get_id3v2_frame_info(frame_data, frame_size, &info->album, cs_conv);
442     /* TCON, TCO */
443     else if (memcmp(fh->frame_id, "TCO", 3) == 0)
444         _get_id3v2_genre(frame_data, frame_size, &info->genre, cs_conv);
445     else if (memcmp(fh->frame_id, "TRCK", 4) == 0 ||
446              memcmp(fh->frame_id, "TRK", 3) == 0) {
447         struct lms_string_size trackno = {0};
448         _get_id3v2_frame_info(frame_data, frame_size, &trackno, cs_conv);
449         info->trackno = atoi(trackno.str);
450         free(trackno.str);
451     }
452 }
453
454 static int
455 _parse_id3v2(int fd, long id3v2_offset, struct id3_info *info, lms_charset_conv_t **cs_convs)
456 {
457     char header_data[10], frame_header_data[10];
458     unsigned int tag_size, major_version, frame_data_pos, frame_data_length, frame_header_size;
459     int extended_header, footer_present;
460     struct id3v2_frame_header fh;
461     size_t nread;
462
463     lseek(fd, id3v2_offset, SEEK_SET);
464
465     /* parse header */
466     if (read(fd, header_data, ID3V2_HEADER_SIZE) != ID3V2_HEADER_SIZE)
467         return -1;
468
469     tag_size = _to_uint(header_data + 6, 4);
470     if (tag_size == 0)
471         return -1;
472
473     /* parse frames */
474     major_version = header_data[3];
475
476     frame_data_pos = 0;
477     frame_data_length = tag_size;
478
479     /* check for extended header */
480     extended_header = header_data[5] & 0x20; /* bit 6 */
481     if (extended_header) {
482         /* skip extended header */
483         unsigned int extended_header_size;
484         char extended_header_data[4];
485
486         if (read(fd, extended_header_data, 4) != 4)
487             return -1;
488         extended_header_size = _to_uint(extended_header_data, 4);
489         lseek(fd, extended_header_size - 4, SEEK_CUR);
490         frame_data_pos += extended_header_size;
491         frame_data_length -= extended_header_size;
492     }
493
494     footer_present = header_data[5] & 0x8;   /* bit 4 */
495     if (footer_present && frame_data_length > ID3V2_FOOTER_SIZE)
496         frame_data_length -= ID3V2_FOOTER_SIZE;
497
498     frame_header_size = _get_id3v2_frame_header_size(major_version);
499     while (frame_data_pos < frame_data_length - frame_header_size) {
500         nread = read(fd, frame_header_data, frame_header_size);
501         if (nread == 0)
502             break;
503
504         if (nread != frame_header_size)
505             return -1;
506
507         if (frame_header_data[0] == 0)
508             break;
509
510         _parse_id3v2_frame_header(frame_header_data, major_version, &fh);
511         if (!fh.frame_size)
512             break;
513
514         if (!fh.compression &&
515             fh.frame_id[0] == 'T' &&
516             memcmp(fh.frame_id, "TXXX", 4) != 0) {
517             char *frame_data;
518
519             if (fh.data_length_indicator)
520                 lseek(fd, 4, SEEK_CUR);
521
522             frame_data = malloc(sizeof(char) * fh.frame_size);
523             if (read(fd, frame_data, fh.frame_size) != fh.frame_size) {
524                 free(frame_data);
525                 return -1;
526             }
527
528             _parse_id3v2_frame(&fh, frame_data, info, cs_convs);
529             free(frame_data);
530         }
531         else {
532             if (fh.data_length_indicator)
533                 lseek(fd, fh.frame_size + 4, SEEK_CUR);
534             else
535                 lseek(fd, fh.frame_size, SEEK_CUR);
536         }
537
538         frame_data_pos += fh.frame_size + frame_header_size;
539     }
540
541     return 0;
542 }
543
544 static int
545 _parse_id3v1(int fd, struct id3_info *info, lms_charset_conv_t *cs_conv)
546 {
547     struct id3v1_tag tag;
548     if (read(fd, &tag, sizeof(struct id3v1_tag)) == -1)
549         return -1;
550
551     info->title.str = strndup(tag.title, 30);
552     info->title.len = strlen(info->title.str);
553     lms_charset_conv(cs_conv, &info->title.str, &info->title.len);
554     info->artist.str = strndup(tag.artist, 30);
555     info->artist.len = strlen(info->artist.str);
556     lms_charset_conv(cs_conv, &info->artist.str, &info->artist.len);
557     info->album.str = strndup(tag.album, 30);
558     info->album.len = strlen(info->album.str);
559     lms_charset_conv(cs_conv, &info->album.str, &info->album.len);
560     _get_id3v1_genre(tag.genre, &info->genre);
561     if (tag.comments[28] == 0 && tag.comments[29] != 0)
562         info->trackno = (unsigned char) tag.comments[29];
563
564     return 0;
565 }
566
567 static void *
568 _match(struct plugin *p, const char *path, int len, int base)
569 {
570     int i;
571
572     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
573     if (i < 0)
574       return NULL;
575     else
576       return (void*)(i + 1);
577 }
578
579 static int
580 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
581 {
582     struct id3_info info = {{0}, {0}, {0}, {0}, 0, -1};
583     struct lms_audio_info audio_info = {0, {0}, {0}, {0}, {0}, 0, 0, 0};
584     int r, fd;
585     long id3v2_offset;
586
587     fd = open(finfo->path, O_RDONLY);
588     if (fd < 0) {
589         perror("open");
590         return -1;
591     }
592
593     id3v2_offset = _find_id3v2(fd);
594     if (id3v2_offset >= 0) {
595 #if 0
596         fprintf(stderr, "id3v2 tag found in file %s with offset %ld\n",
597                 finfo->path, id3v2_offset);
598 #endif
599         if (_parse_id3v2(fd, id3v2_offset, &info, plugin->cs_convs) != 0) {
600             r = -2;
601             goto done;
602         }
603     }
604     else {
605         char tag[3];
606 #if 0
607         fprintf(stderr, "id3v2 tag not found in file %s. trying id3v1\n", finfo->path);
608 #endif
609         /* check for id3v1 tag */
610         if (lseek(fd, -128, SEEK_END) == -1) {
611             r = -3;
612             goto done;
613         }
614
615         if (read(fd, &tag, 3) == -1) {
616             r = -4;
617             goto done;
618         }
619
620         if (memcmp(tag, "TAG", 3) == 0) {
621 #if 0
622             fprintf(stderr, "id3v1 tag found in file %s\n", finfo->path);
623 #endif
624             if (_parse_id3v1(fd, &info, ctxt->cs_conv) != 0) {
625                 r = -5;
626                 goto done;
627             }
628         }
629     }
630
631     lms_string_size_strip_and_free(&info.title);
632     lms_string_size_strip_and_free(&info.artist);
633     lms_string_size_strip_and_free(&info.album);
634     lms_string_size_strip_and_free(&info.genre);
635
636     if (!info.title.str) {
637         int ext_idx;
638         ext_idx = ((int)match) - 1;
639         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
640         info.title.str = malloc((info.title.len + 1) * sizeof(char));
641         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
642         info.title.str[info.title.len] = '\0';
643         lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
644     }
645
646 #if 0
647     fprintf(stderr, "file %s info\n", finfo->path);
648     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
649     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
650     fprintf(stderr, "\talbum='%s'\n", info.album.str);
651     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
652     fprintf(stderr, "\ttrack number='%d'\n", info.trackno);
653 #endif
654
655     audio_info.id = finfo->id;
656     audio_info.title = info.title;
657     audio_info.artist = info.artist;
658     audio_info.album = info.album;
659     audio_info.genre = info.genre;
660     audio_info.trackno = info.trackno;
661     r = lms_db_audio_add(plugin->audio_db, &audio_info);
662
663   done:
664     posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
665     close(fd);
666
667     if (info.title.str)
668         free(info.title.str);
669     if (info.artist.str)
670         free(info.artist.str);
671     if (info.album.str)
672         free(info.album.str);
673     if (info.genre.str)
674         free(info.genre.str);
675
676     return r;
677 }
678
679 static int
680 _setup(struct plugin *plugin, struct lms_context *ctxt)
681 {
682     int i;
683     const char *id3_encodings[ID3_NUM_ENCODINGS] = {
684         "Latin1",
685         NULL, /* UTF-16 */
686         "UTF-16BE",
687         NULL, /* UTF-8 */
688         "UTF-16LE",
689     };
690
691     plugin->audio_db = lms_db_audio_new(ctxt->db);
692     if (!plugin->audio_db)
693         return -1;
694
695     for (i = 0; i < ID3_NUM_ENCODINGS; ++i) {
696         /* do not create charset conv for UTF-8 encoding */
697         if (!id3_encodings[i]) {
698             plugin->cs_convs[i] = NULL;
699             continue;
700         }
701         plugin->cs_convs[i] = lms_charset_conv_new_full(0, 0);
702         if (!plugin->cs_convs[i])
703             return -1;
704         lms_charset_conv_add(plugin->cs_convs[i], id3_encodings[i]);
705     }
706
707     return 0;
708 }
709
710 static int
711 _start(struct plugin *plugin, struct lms_context *ctxt)
712 {
713     return lms_db_audio_start(plugin->audio_db);
714 }
715
716 static int
717 _finish(struct plugin *plugin, struct lms_context *ctxt)
718 {
719     int i;
720
721     if (plugin->audio_db)
722         lms_db_audio_free(plugin->audio_db);
723
724     for (i = 0; i < ID3_NUM_ENCODINGS; ++i) {
725         if (plugin->cs_convs[i])
726             lms_charset_conv_free(plugin->cs_convs[i]);
727     }
728
729     return 0;
730 }
731
732 static int
733 _close(struct plugin *plugin)
734 {
735     free(plugin);
736     return 0;
737 }
738
739 API struct lms_plugin *
740 lms_plugin_open(void)
741 {
742     struct plugin *plugin;
743
744     plugin = (struct plugin *)malloc(sizeof(*plugin));
745     plugin->plugin.name = _name;
746     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
747     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
748     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
749     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
750     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
751     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
752
753     return (struct lms_plugin *)plugin;
754 }