1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2003-2008, Evan Battaglia <gtoevan@gmx.net>
6 * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
7 * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include "compression.h"
40 #include <glib/gstdio.h>
43 /* return size of unzip data or 0 if failed */
44 static guint uncompress_data(void *uncompressed_buffer, guint uncompressed_size, void *compressed_data, guint compressed_size)
49 stream.next_in = compressed_data;
50 stream.avail_in = compressed_size;
51 stream.next_out = uncompressed_buffer;
52 stream.avail_out = uncompressed_size;
53 stream.zalloc = (alloc_func)0;
54 stream.zfree = (free_func)0;
55 stream.opaque = (voidpf)0;
57 /* negative windowBits to inflateInit2 means "no header" */
58 if ((err = inflateInit2(&stream, -MAX_WBITS)) != Z_OK) {
59 g_warning("%s(): inflateInit2 failed", __PRETTY_FUNCTION__);
63 err = inflate(&stream, Z_FINISH);
64 if ((err != Z_OK) && (err != Z_STREAM_END) && stream.msg) {
65 g_warning("%s() inflate failed err=%d \"%s\"", __PRETTY_FUNCTION__, err, stream.msg == NULL ? "unknown" : stream.msg);
71 return(stream.total_out);
77 * @zip_file: pointer to start of compressed data
78 * @unzip_size: the size of the compressed data block
80 * Returns a pointer to uncompressed data (maybe NULL)
82 void *unzip_file(gchar *zip_file, gulong *unzip_size)
84 void *unzip_data = NULL;
89 // See http://en.wikipedia.org/wiki/Zip_(file_format)
92 guint16 extract_version;
98 guint32 compressed_size;
99 guint32 uncompressed_size;
100 guint16 filename_len;
101 guint16 extra_field_len;
102 } __attribute__ ((gcc_struct,__packed__)) *local_file_header = NULL;
104 if ( sizeof(struct _lfh) != 30 ) {
105 g_critical ("Incorrect internal zip header size, should be 30 but is %zd", sizeof(struct _lfh) );
109 local_file_header = (struct _lfh *) zip_file;
110 if (GUINT32_FROM_LE(local_file_header->sig) != 0x04034b50) {
111 g_warning("%s(): wrong format (%d)", __PRETTY_FUNCTION__, GUINT32_FROM_LE(local_file_header->sig));
116 zip_data = zip_file + sizeof(struct _lfh)
117 + GUINT16_FROM_LE(local_file_header->filename_len)
118 + GUINT16_FROM_LE(local_file_header->extra_field_len);
119 gulong uncompressed_size = GUINT32_FROM_LE(local_file_header->uncompressed_size);
120 unzip_data = g_malloc(uncompressed_size);
122 // Protection against malloc failures
123 // ATM not normally been checking malloc failures in Viking but sometimes using zip files can be quite large
124 // (e.g. when using DEMs) so more potential for failure.
128 g_debug ("%s: method %d: from size %d to %ld", __FUNCTION__, GUINT16_FROM_LE(local_file_header->comp_method), GUINT32_FROM_LE(local_file_header->compressed_size), uncompressed_size);
130 if ( GUINT16_FROM_LE(local_file_header->comp_method) == 0 &&
131 (uncompressed_size == GUINT32_FROM_LE(local_file_header->compressed_size)) ) {
132 // Stored only - no need to 'uncompress'
134 memcpy ( unzip_data, zip_data, uncompressed_size );
135 *unzip_size = uncompressed_size;
139 if (!(*unzip_size = uncompress_data(unzip_data, uncompressed_size, zip_data, GUINT32_FROM_LE(local_file_header->compressed_size)))) {
152 * @name: The name of the file to attempt to decompress
154 * Returns: The name of the uncompressed file (in a temporary location) or NULL
155 * free the returned name after use.
157 * Also see: http://www.bzip.org/1.0.5/bzip2-manual-1.0.5.html
159 gchar* uncompress_bzip2 ( gchar *name )
162 g_debug ( "%s: bzip2 %s", __FUNCTION__, BZ2_bzlibVersion() );
164 FILE *ff = g_fopen ( name, "rb" );
169 BZFILE* bf = BZ2_bzReadOpen ( &bzerror, ff, 0, 0, NULL, 0 ); // This should take care of the bz2 file header
170 if ( bzerror != BZ_OK ) {
171 BZ2_bzReadClose ( &bzerror, bf );
173 g_warning ( "%s: BZ ReadOpen error on %s", __FUNCTION__, name );
178 GError *error = NULL;
179 gchar *tmpname = NULL;
180 #if GLIB_CHECK_VERSION(2,32,0)
181 GFile *gf = g_file_new_tmp ( "vik-bz2-tmp.XXXXXX", &gios, &error );
182 tmpname = g_file_get_path (gf);
184 gint fd = g_file_open_tmp ( "vik-bz2-tmp.XXXXXX", &tmpname, &error );
186 g_warning ( error->message );
187 g_error_free ( error );
190 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
192 g_warning ( error->message );
193 g_error_free ( error );
198 GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) );
200 // Process in arbitary sized chunks
204 // Now process the actual compression data
205 while ( bzerror == BZ_OK ) {
206 nBuf = BZ2_bzRead ( &bzerror, bf, buf, 4096 );
207 if ( bzerror == BZ_OK || bzerror == BZ_STREAM_END) {
208 // do something with buf[0 .. nBuf-1]
209 if ( g_output_stream_write ( gos, buf, nBuf, NULL, &error ) < 0 ) {
210 g_critical ( "Couldn't write bz2 tmp %s file due to %s", tmpname, error->message );
211 g_error_free (error);
212 BZ2_bzReadClose ( &bzerror, bf );
217 if ( bzerror != BZ_STREAM_END ) {
219 g_warning ( "%s: BZ error :( %d. read %d", __FUNCTION__, bzerror, nBuf );
221 BZ2_bzReadClose ( &bzerror, bf );
222 g_output_stream_close ( gos, NULL, &error );
225 g_object_unref ( gios );