3 * Copyright (C) 2006 Exophase <exophase@gmail.com>
4 * Copyright (C) 2006 SiberianSTAR
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #define ZIP_BUFFER_SIZE (128 * 1024)
26 struct SZIPFileDataDescriptor
31 } __attribute__((packed));
35 char Sig[4]; // EDIT: Used to be s32 Sig;
38 s16 CompressionMethod;
41 struct SZIPFileDataDescriptor DataDescriptor;
44 } __attribute__((packed));
46 u32 load_file_zip(char *filename)
48 struct SZIPFileHeader data;
55 file_open(fd, filename, read);
57 if(!file_check_valid(fd))
60 #if 0 // EDIT: Why this while(1) is used is unknown and can cause a crash.
64 file_read(fd, &data, sizeof(struct SZIPFileHeader));
66 // EDIT: Check if this is a zip file without worrying about endian
67 // It checks for the following: 0x50 0x4B 0x03 0x04 (PK..)
68 // Used to be: if(data.Sig != 0x04034b50) break;
69 if( data.Sig[0] != 0x50 || data.Sig[1] != 0x4B ||
70 data.Sig[2] != 0x03 || data.Sig[3] != 0x04 )
75 file_read(fd, tmp, data.FilenameLength);
76 tmp[data.FilenameLength] = 0; // end string
78 if(data.ExtraFieldLength)
79 file_seek(fd, data.ExtraFieldLength, SEEK_CUR);
81 if(data.GeneralBitFlag & 0x0008)
83 file_read(fd, &data.DataDescriptor,
84 sizeof(struct SZIPFileDataDescriptor));
87 ext = strrchr(tmp, '.') + 1;
90 if(data.DataDescriptor.UncompressedSize > gamepak_ram_buffer_size)
93 if(!strcasecmp(ext, "bin") || !strcasecmp(ext, "gba"))
98 switch(data.CompressionMethod)
101 retval = data.DataDescriptor.UncompressedSize;
102 file_read(fd, buffer, retval);
111 cbuffer = malloc(ZIP_BUFFER_SIZE);
113 stream.next_in = (Bytef*)cbuffer;
114 stream.avail_in = (u32)ZIP_BUFFER_SIZE;
116 stream.next_out = (Bytef*)buffer;
118 // EDIT: Now uses proper conversion of data types for retval.
119 retval = (u32)data.DataDescriptor.UncompressedSize;
120 stream.avail_out = data.DataDescriptor.UncompressedSize;
122 stream.zalloc = (alloc_func)0;
123 stream.zfree = (free_func)0;
125 err = inflateInit2(&stream, -MAX_WBITS);
127 file_read(fd, cbuffer, ZIP_BUFFER_SIZE);
131 while(err != Z_STREAM_END)
133 err = inflate(&stream, Z_SYNC_FLUSH);
134 if(err == Z_BUF_ERROR)
136 stream.avail_in = ZIP_BUFFER_SIZE;
137 stream.next_in = (Bytef*)cbuffer;
138 file_read(fd, cbuffer, ZIP_BUFFER_SIZE);