| 1 | /* gameplaySP |
| 2 | * |
| 3 | * Copyright (C) 2006 Exophase <exophase@gmail.com> |
| 4 | * Copyright (C) 2006 SiberianSTAR |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | |
| 21 | #include <zlib.h> |
| 22 | #include "common.h" |
| 23 | |
| 24 | #define ZIP_BUFFER_SIZE (128 * 1024) |
| 25 | |
| 26 | struct SZIPFileDataDescriptor |
| 27 | { |
| 28 | s32 CRC32; |
| 29 | s32 CompressedSize; |
| 30 | s32 UncompressedSize; |
| 31 | } __attribute__((packed)); |
| 32 | |
| 33 | struct SZIPFileHeader |
| 34 | { |
| 35 | char Sig[4]; // EDIT: Used to be s32 Sig; |
| 36 | s16 VersionToExtract; |
| 37 | s16 GeneralBitFlag; |
| 38 | s16 CompressionMethod; |
| 39 | s16 LastModFileTime; |
| 40 | s16 LastModFileDate; |
| 41 | struct SZIPFileDataDescriptor DataDescriptor; |
| 42 | s16 FilenameLength; |
| 43 | s16 ExtraFieldLength; |
| 44 | } __attribute__((packed)); |
| 45 | |
| 46 | u32 load_file_zip(char *filename) |
| 47 | { |
| 48 | struct SZIPFileHeader data; |
| 49 | u8 tmp[1024]; |
| 50 | s32 retval = -1; |
| 51 | u8 *buffer = NULL; |
| 52 | u8 *cbuffer; |
| 53 | u8 *ext; |
| 54 | |
| 55 | file_open(fd, filename, read); |
| 56 | |
| 57 | if(!file_check_valid(fd)) |
| 58 | return -1; |
| 59 | |
| 60 | #if 0 // EDIT: Why this while(1) is used is unknown and can cause a crash. |
| 61 | while(1) |
| 62 | #endif |
| 63 | { |
| 64 | file_read(fd, &data, sizeof(struct SZIPFileHeader)); |
| 65 | |
| 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 ) |
| 71 | { |
| 72 | goto outcode; |
| 73 | } |
| 74 | |
| 75 | file_read(fd, tmp, data.FilenameLength); |
| 76 | tmp[data.FilenameLength] = 0; // end string |
| 77 | |
| 78 | if(data.ExtraFieldLength) |
| 79 | file_seek(fd, data.ExtraFieldLength, SEEK_CUR); |
| 80 | |
| 81 | if(data.GeneralBitFlag & 0x0008) |
| 82 | { |
| 83 | file_read(fd, &data.DataDescriptor, |
| 84 | sizeof(struct SZIPFileDataDescriptor)); |
| 85 | } |
| 86 | |
| 87 | ext = strrchr(tmp, '.') + 1; |
| 88 | |
| 89 | // file is too big |
| 90 | if(data.DataDescriptor.UncompressedSize > gamepak_ram_buffer_size) |
| 91 | goto outcode; |
| 92 | |
| 93 | if(!strcasecmp(ext, "bin") || !strcasecmp(ext, "gba")) |
| 94 | { |
| 95 | buffer = gamepak_rom; |
| 96 | |
| 97 | // ok, found |
| 98 | switch(data.CompressionMethod) |
| 99 | { |
| 100 | case 0: |
| 101 | retval = data.DataDescriptor.UncompressedSize; |
| 102 | file_read(fd, buffer, retval); |
| 103 | |
| 104 | goto outcode; |
| 105 | |
| 106 | case 8: |
| 107 | { |
| 108 | z_stream stream; |
| 109 | s32 err; |
| 110 | |
| 111 | cbuffer = malloc(ZIP_BUFFER_SIZE); |
| 112 | |
| 113 | stream.next_in = (Bytef*)cbuffer; |
| 114 | stream.avail_in = (u32)ZIP_BUFFER_SIZE; |
| 115 | |
| 116 | stream.next_out = (Bytef*)buffer; |
| 117 | |
| 118 | // EDIT: Now uses proper conversion of data types for retval. |
| 119 | retval = (u32)data.DataDescriptor.UncompressedSize; |
| 120 | stream.avail_out = data.DataDescriptor.UncompressedSize; |
| 121 | |
| 122 | stream.zalloc = (alloc_func)0; |
| 123 | stream.zfree = (free_func)0; |
| 124 | |
| 125 | err = inflateInit2(&stream, -MAX_WBITS); |
| 126 | |
| 127 | file_read(fd, cbuffer, ZIP_BUFFER_SIZE); |
| 128 | |
| 129 | if(err == Z_OK) |
| 130 | { |
| 131 | while(err != Z_STREAM_END) |
| 132 | { |
| 133 | err = inflate(&stream, Z_SYNC_FLUSH); |
| 134 | if(err == Z_BUF_ERROR) |
| 135 | { |
| 136 | stream.avail_in = ZIP_BUFFER_SIZE; |
| 137 | stream.next_in = (Bytef*)cbuffer; |
| 138 | file_read(fd, cbuffer, ZIP_BUFFER_SIZE); |
| 139 | } |
| 140 | } |
| 141 | err = Z_OK; |
| 142 | inflateEnd(&stream); |
| 143 | } |
| 144 | free(cbuffer); |
| 145 | goto outcode; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | outcode: |
| 152 | file_close(fd); |
| 153 | |
| 154 | return retval; |
| 155 | } |