Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[r4]: cbrpager-maemo / branches / MAEMO_0_9_20 / src / garchivereaderunzip.c Maximize Restore History

Download this file

garchivereaderunzip.c    317 lines (280 with data), 8.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* garchivereaderunzip.c - routines to manipulate archive files
* using libunzip code.
*
* Copyright (C) 2008 Benoit Goudreault-Emond (bgoudreaultemond@gmail.com).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <glib.h>
#include <string.h>
#include <stdio.h>
#include "unzip.h"
#include "garchivereaderunzip.h"
extern int debug;
static gint
compare_pages(gconstpointer s1, gconstpointer s2)
{
return strcmp((const char*)s1, (const char*)s2);
}
static gboolean extension_handled(const char* filename)
{
static const char* extensions[] = {
".jpg", ".jpeg", ".png", ".gif",
".JPG", ".JPEG", ".PNG", ".GIF"
};
static const int nr_extensions = 8;
int i;
for(i = 0; i < nr_extensions; ++i)
{
if(strstr(filename, extensions[i]) != NULL)
return TRUE;
}
return FALSE;
}
static void set_last_error(GArchiveReaderUnzip* reader, gchar* data)
{
if(reader->last_error)
g_free(reader->last_error);
reader->last_error = data;
}
static void free_string(gpointer data, gpointer user_data)
{
g_free(data);
}
static void
free_page_list(GArchiveReaderUnzip* reader)
{
if(!reader->page_list)
return;
g_list_foreach(reader->page_list, free_string, NULL);
g_list_free(reader->page_list);
reader->page_list = NULL;
}
static int
register_path(GArchiveReader* reader_iface,
const gchar* path)
{
unzFile file;
GArchiveReaderUnzip* reader = G_ARCHIVE_READER_UNZIP(reader_iface);
int check_result = g_archive_reader_check_magic(path, &reader->magic);
if(debug)
printf(_("Check result for type %s -> %d\n"), reader->magic.sign, check_result);
if(check_result <= 0)
return check_result;
file = unzOpen(path);
if(!file)
return -1;
if(reader->current_file)
{
unzClose(reader->current_file);
free_page_list(reader);
}
reader->current_file = file;
return 1;
}
#define BUFSIZE 2049
static GList*
get_file_list(GArchiveReader* reader_iface)
{
GArchiveReaderUnzip* reader = G_ARCHIVE_READER_UNZIP(reader_iface);
static char filename[BUFSIZE]; /* should be enough */
if(reader->page_list)
return reader->page_list;
if(!reader->current_file)
return NULL;
unzGoToFirstFile(reader->current_file);
do
{
unz_file_info file_info;
unzGetCurrentFileInfo(reader->current_file,
&file_info,
filename,
BUFSIZE - 1,
NULL,
0,
NULL,
0);
/* terminate with NUL just in case */
filename[file_info.size_filename] = '\0';
if(extension_handled(filename))
{
reader->page_list = g_list_append(reader->page_list, g_strdup(filename));
}
} while(unzGoToNextFile(reader->current_file) == UNZ_OK);
if(!reader->page_list)
{
set_last_error(reader, g_strdup(_("No pages in archive")));
}
else
{
reader->page_list = g_list_sort(reader->page_list, compare_pages);
}
return reader->page_list;
}
static GdkPixbuf*
get_page(GArchiveReader* reader_iface,
int page_number,
gdouble* w,
gdouble* h)
{
GdkPixbuf* retval = NULL;
GArchiveReaderUnzip* reader = G_ARCHIVE_READER_UNZIP(reader_iface);
const gchar* filename;
GdkPixbufLoader* loader;
unz_file_info file_info;
void* buffer;
gboolean result;
int sz;
FILE* tmpfile;
if(w)
*w = 0;
if(h)
*h = 0;
filename = (const gchar*)g_list_nth_data(get_file_list(reader_iface), page_number);
if(!filename)
{
set_last_error(reader, g_strdup_printf(_("Page number %d out of range"), page_number));
return NULL;
}
if(unzLocateFile(reader->current_file, (const char*)filename, 1) != UNZ_OK)
{
set_last_error(reader, g_strdup_printf(_("File %s not found in archive"), filename));
return NULL;
}
unzGetCurrentFileInfo(reader->current_file,
&file_info,
NULL,
0,
NULL,
0,
NULL,
0);
if(unzOpenCurrentFile(reader->current_file) != UNZ_OK)
{
set_last_error(reader, g_strdup_printf(_("Error reading %s from archive"), filename));
return NULL;
}
buffer = g_try_malloc(file_info.uncompressed_size);
sz = unzReadCurrentFile(reader->current_file,
buffer,
file_info.uncompressed_size);
unzCloseCurrentFile(reader->current_file);
if(sz != file_info.uncompressed_size)
{
g_free(buffer);
set_last_error(reader, g_strdup_printf(_("Error reading %s from archive"), filename));
return NULL;
}
/* dump to the file */
if(!(tmpfile = fopen(reader->tmpf, "wb")))
{
g_free(buffer);
set_last_error(reader, g_strdup_printf(_("Unable to open temp file %s"), reader->tmpf));
return NULL;
}
sz = fwrite(buffer, 1, file_info.uncompressed_size, tmpfile);
g_free(buffer);
if(fclose(tmpfile) == EOF || sz != file_info.uncompressed_size)
{
set_last_error(reader, g_strdup(_("Out of disk space!")));
return NULL;
}
retval = gdk_pixbuf_new_from_file(reader->tmpf, NULL);
if(retval)
{
if(w)
*w = gdk_pixbuf_get_width(retval);
if(h)
*h = gdk_pixbuf_get_height(retval);
}
else
{
set_last_error(reader, g_strdup_printf(_("Unable to load page %d"), page_number));
}
return retval;
}
static gchar*
get_last_error(GArchiveReader* reader_iface)
{
GArchiveReaderUnzip* reader = G_ARCHIVE_READER_UNZIP(reader_iface);
gchar* last_error = reader->last_error;
reader->last_error = NULL;
return last_error;
}
/* class instance housekeeping stuff */
static void
g_archive_reader_unzip_interface_init(gpointer g_iface,
gpointer iface_data)
{
GArchiveReaderClass* klass = (GArchiveReaderClass*)g_iface;
klass->register_path = register_path;
klass->get_file_list = get_file_list;
klass->get_page = get_page;
klass->get_last_error = get_last_error;
}
static void
g_archive_reader_unzip_init(GArchiveReaderUnzip* reader)
{
reader->magic.offs = 0;
reader->magic.len = 0;
reader->magic.fn_offs = 0;
reader->magic.sign = NULL;
reader->tmpf = NULL;
reader->current_file = NULL;
reader->last_error = NULL;
reader->page_list = NULL;
}
G_DEFINE_TYPE_EXTENDED(GArchiveReaderUnzip,
g_archive_reader_unzip,
G_TYPE_OBJECT,
0,
G_IMPLEMENT_INTERFACE(G_TYPE_ARCHIVE_READER,
g_archive_reader_unzip_interface_init));
static void
g_archive_reader_unzip_finalize (GObject* object)
{
GArchiveReaderUnzip* reader = G_ARCHIVE_READER_UNZIP(object);
g_free(reader->tmpf);
if(reader->current_file)
unzClose(reader->current_file);
if(reader->last_error)
g_free(reader->last_error);
free_page_list(reader);
}
static void
g_archive_reader_unzip_class_init(GArchiveReaderUnzipClass* klass)
{
GObjectClass* object_class = (GObjectClass*)klass;
object_class->finalize = g_archive_reader_unzip_finalize;
}
/* public interface implementation */
GArchiveReader* g_archive_reader_unzip_new(const gchar* tmpfile,
magic_sign magic)
{
gpointer data = NULL;
GArchiveReaderUnzip* reader = NULL;
/* copy properties from the parameters */
data = g_object_new(G_TYPE_ARCHIVE_READER_UNZIP, NULL);
reader = G_ARCHIVE_READER_UNZIP(data);
reader->magic = magic;
reader->current_file = NULL;
reader->last_error = NULL;
reader->page_list = NULL;
/* figure out tmp file */
reader->tmpf = g_strdup(tmpfile);
return G_ARCHIVE_READER(data);
}