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