-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzip.cpp
More file actions
169 lines (156 loc) · 4.42 KB
/
zip.cpp
File metadata and controls
169 lines (156 loc) · 4.42 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
/*
Purpose of this file is to simulate the fopen/fclose/fread/fseek stdio functions
for use with compressed N64 rom images.
*/
#include <stdio.h>
#include <string.h>
#include "unzip.h"
#include "zconf.h"
#include "zlib.h"
unz_file_info file_info;
int FindN64Rom (unzFile fp) {
char szFileName[256];
if (fp == NULL)
return -1;
if (unzGoToFirstFile(fp) == UNZ_OK) {
do {
if (unzGetCurrentFileInfo(fp,
&file_info,
szFileName,
256,
NULL,
0,
NULL,
0) == UNZ_OK) {
if(stricmp(&szFileName[strlen(szFileName) - 4], ".bin") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".v64") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".n64") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".u64") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".z64") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".pal") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".usa") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".jap") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".eur") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".rom") == 0) {
if(unzOpenCurrentFile(fp)== UNZ_OK) {
return 0;
}
}
} else {
return -3;
}
} while(unzGoToNextFile(fp) == UNZ_OK);
} else {
return -4;
}
return -5;
}
FILE *zipopen (const char *fname, const char *flags) {
FILE *fp;
fp = (FILE *)unzOpen (fname);
if (FindN64Rom ((unzFile)fp))
return NULL;
return fp;
}
int zipclose (FILE *fp) {
unzClose ((unzFile)fp);
return 0;
}
int zipseek (FILE *fp, long offset, int flags) {
if (flags == SEEK_SET) {
unzOpenCurrentFile(fp);
} else if (flags == SEEK_END) {
unzGetCurrentFileInfo(fp,
&file_info,
NULL,
0,
NULL,
0,
NULL,
0);
}
return 0;
}
long ziptell (FILE *fp) {
return (file_info.uncompressed_size);
}
size_t zipread (void *data, size_t size, size_t count, FILE *fp) {
return (unzReadCurrentFile(fp, data, size*count));
}
/*
void ReadZippedRomData(char* RomPath)
{
unzFile fp;
unsigned long gROMLength; //size in bytes of the ROM
free(ROM_Image);
if(fp = unzOpen(RomPath))
{
char szFileName[256];
if(unzGoToFirstFile(fp) == UNZ_OK)
{
do
{
unz_file_info file_info;
if(unzGetCurrentFileInfo(fp,
&file_info,
szFileName,
256,
NULL,
0,
NULL,
0) == UNZ_OK)
{
if(stricmp(&szFileName[strlen(szFileName) - 4], ".bin") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".v64") == 0
|| stricmp(&szFileName[strlen(szFileName) - 4], ".rom") == 0)
{
gROMLength = file_info.uncompressed_size; //get size of ROM
if (((gROMLength & 0xFFFF)) == 0)
gAllocationLength = gROMLength;
else
gAllocationLength = gROMLength + (0x10000 - (gROMLength & 0xFFFF));
if (( ROM_Image = (uint8*)calloc( gAllocationLength, sizeof( uint8 ))) == NULL )
{
DisplayError("Error: Could not allocate rom image buffer.\n" );
unzClose(fp);
exit( 0 );
}
InitMemoryLookupTables();
if(unzOpenCurrentFile(fp)== UNZ_OK)
{
if(unzReadCurrentFile(fp, ROM_Image, sizeof( uint8 ) * gROMLength) == (int)(sizeof( uint8 ) * gROMLength))
{
ByteSwap(gAllocationLength, ROM_Image);
memcpy((uint8*)&SP_DMEM, ROM_Image, 0x1000);
unzClose(fp);
return(1);
}
else
{
DisplayError("File could not be read. gROMLength = %08X\n", gROMLength );
unzClose(fp);
exit(0);
}
unzCloseCurrentFile(fp);
}
else
{
DisplayError("File could not be read: CRC Error in zip." );
unzClose(fp);
exit(0);
}
}
}
else
{
DisplayError("File could not unzipped." );
unzClose(fp);
exit(0);
}
}
while(unzGoToNextFile(fp) == UNZ_OK);
}
unzClose(fp);
}
}
*/