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