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