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