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