Updated package version
[lms] / lightmediascanner / 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
98 struct plugin {
99     struct lms_plugin plugin;
100     lms_db_image_t *img_db;
101 };
102
103 static void *
104 _match(struct plugin *p, const char *path, int len, int base)
105 {
106     int i;
107
108     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
109     if (i < 0)
110         return NULL;
111     else
112         return (void*)(i + 1);
113 }
114
115 static int
116 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
117 {
118     struct lms_image_info info = {0};
119     int fd, r;
120
121     fd = open(finfo->path, O_RDONLY);
122     if (fd < 0) {
123         perror("open");
124         return -1;
125     }
126
127     if (_png_data_get(fd, &info) != 0) {
128         r = -2;
129         goto done;
130     }
131
132     if (info.date == 0)
133         info.date = finfo->mtime;
134
135     if (!info.title.str) {
136       int ext_idx;
137
138       ext_idx = ((int)match) - 1;
139       info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
140       info.title.str = malloc((info.title.len + 1) * sizeof(char));
141       memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
142       info.title.str[info.title.len] = '\0';
143     }
144
145     if (info.title.str)
146       lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
147     if (info.artist.str)
148       lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
149
150     info.id = finfo->id;
151     r = lms_db_image_add(plugin->img_db, &info);
152
153   done:
154     if (info.title.str)
155         free(info.title.str);
156     if (info.artist.str)
157         free(info.artist.str);
158
159     posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
160     close(fd);
161
162     return r;
163 }
164
165 static int
166 _setup(struct plugin *plugin, struct lms_context *ctxt)
167 {
168     plugin->img_db = lms_db_image_new(ctxt->db);
169     if (!plugin->img_db)
170         return -1;
171
172     return 0;
173 }
174
175 static int
176 _start(struct plugin *plugin, struct lms_context *ctxt)
177 {
178     return lms_db_image_start(plugin->img_db);
179 }
180
181 static int
182 _finish(struct plugin *plugin, struct lms_context *ctxt)
183 {
184     if (plugin->img_db)
185         return lms_db_image_free(plugin->img_db);
186
187     return 0;
188 }
189
190
191 static int
192 _close(struct plugin *plugin)
193 {
194     free(plugin);
195     return 0;
196 }
197
198 API struct lms_plugin *
199 lms_plugin_open(void)
200 {
201     struct plugin *plugin;
202
203     plugin = malloc(sizeof(*plugin));
204     plugin->plugin.name = _name;
205     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
206     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
207     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
208     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
209     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
210     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
211
212     return (struct lms_plugin *)plugin;
213 }