export parsers and their informations.
[platform/upstream/lightmediascanner.git] / src / plugins / png / png.c
1 /**
2  * Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * Reads PNG images.
25  *
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define _XOPEN_SOURCE 600
33 #include <lightmediascanner_plugin.h>
34 #include <lightmediascanner_utils.h>
35 #include <lightmediascanner_db.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <time.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <strings.h>
45
46 static inline unsigned int
47 _chunk_to_uint(unsigned char *buf)
48 {
49     return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
50 }
51
52 static int
53 _png_data_get(int fd, struct lms_image_info *info)
54 {
55     unsigned char buf[16], *p;
56     const unsigned char sig[8] = {0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa};
57     const unsigned char ihdr[4] = {'I', 'H', 'D', 'R'};
58     unsigned int length;
59
60     if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
61         perror("read");
62         return -1;
63     }
64
65     if (memcmp(buf, sig, sizeof(sig)) != 0) {
66         fprintf(stderr, "ERROR: invalid PNG signature.\n");
67         return -2;
68     }
69
70     p = buf + sizeof(sig) + 4;
71     if (memcmp(p, ihdr, sizeof(ihdr)) != 0) {
72         fprintf(stderr, "ERROR: invalid first chunk: %4.4s.\n", p);
73         return -3;
74     }
75
76     p = buf + sizeof(sig);
77     length = _chunk_to_uint(p);
78     if (length < 13) {
79         fprintf(stderr, "ERROR: IHDR chunk size is too small: %d.\n", length);
80         return -4;
81     }
82
83     if (read(fd, buf, 8) != 8) {
84         perror("read");
85         return -5;
86     }
87     info->width = _chunk_to_uint(buf);
88     info->height = _chunk_to_uint(buf + 4);
89
90     return 0;
91 }
92
93 static const char _name[] = "png";
94 static const struct lms_string_size _exts[] = {
95     LMS_STATIC_STRING_SIZE(".png")
96 };
97 static const char *_cats[] = {
98     "multimedia",
99     "picture",
100     NULL
101 };
102 static const char *_authors[] = {
103     "Gustavo Sverzut Barbieri",
104     NULL
105 };
106
107 struct plugin {
108     struct lms_plugin plugin;
109     lms_db_image_t *img_db;
110 };
111
112 static void *
113 _match(struct plugin *p, const char *path, int len, int base)
114 {
115     int i;
116
117     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
118     if (i < 0)
119         return NULL;
120     else
121         return (void*)(i + 1);
122 }
123
124 static int
125 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
126 {
127     struct lms_image_info info = {0};
128     int fd, r;
129
130     fd = open(finfo->path, O_RDONLY);
131     if (fd < 0) {
132         perror("open");
133         return -1;
134     }
135
136     if (_png_data_get(fd, &info) != 0) {
137         r = -2;
138         goto done;
139     }
140
141     if (info.date == 0)
142         info.date = finfo->mtime;
143
144     if (!info.title.str) {
145       int ext_idx;
146
147       ext_idx = ((int)match) - 1;
148       info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
149       info.title.str = malloc((info.title.len + 1) * sizeof(char));
150       memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
151       info.title.str[info.title.len] = '\0';
152     }
153
154     if (info.title.str)
155       lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
156     if (info.artist.str)
157       lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
158
159     info.id = finfo->id;
160     r = lms_db_image_add(plugin->img_db, &info);
161
162   done:
163     if (info.title.str)
164         free(info.title.str);
165     if (info.artist.str)
166         free(info.artist.str);
167
168     posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
169     close(fd);
170
171     return r;
172 }
173
174 static int
175 _setup(struct plugin *plugin, struct lms_context *ctxt)
176 {
177     plugin->img_db = lms_db_image_new(ctxt->db);
178     if (!plugin->img_db)
179         return -1;
180
181     return 0;
182 }
183
184 static int
185 _start(struct plugin *plugin, struct lms_context *ctxt)
186 {
187     return lms_db_image_start(plugin->img_db);
188 }
189
190 static int
191 _finish(struct plugin *plugin, struct lms_context *ctxt)
192 {
193     if (plugin->img_db)
194         return lms_db_image_free(plugin->img_db);
195
196     return 0;
197 }
198
199
200 static int
201 _close(struct plugin *plugin)
202 {
203     free(plugin);
204     return 0;
205 }
206
207 API struct lms_plugin *
208 lms_plugin_open(void)
209 {
210     struct plugin *plugin;
211
212     plugin = malloc(sizeof(*plugin));
213     plugin->plugin.name = _name;
214     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
215     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
216     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
217     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
218     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
219     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
220
221     return (struct lms_plugin *)plugin;
222 }
223
224 API struct lms_plugin_info *
225 lms_plugin_info(void)
226 {
227     static struct lms_plugin_info info = {
228         _name,
229         _cats,
230         "PNG images",
231         PACKAGE_VERSION,
232         _authors,
233         "http://lms.garage.maemo.org"
234     };
235
236     return &info;
237 }