Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/octoshock/file.cpp
2 views
1
///* Mednafen - Multi-system Emulator
2
// *
3
// * This program is free software; you can redistribute it and/or modify
4
// * it under the terms of the GNU General Public License as published by
5
// * the Free Software Foundation; either version 2 of the License, or
6
// * (at your option) any later version.
7
// *
8
// * This program is distributed in the hope that it will be useful,
9
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
// * GNU General Public License for more details.
12
// *
13
// * You should have received a copy of the GNU General Public License
14
// * along with this program; if not, write to the Free Software
15
// * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
// */
17
//
18
//#include <stdarg.h>
19
//#include <string.h>
20
//#include <sys/types.h>
21
//#include <sys/stat.h>
22
//#include <errno.h>
23
//
24
//#include "octoshock.h"
25
//#include "FileStream.h"
26
//
27
//
28
//#ifdef HAVE_MMAP
29
//#include <sys/mman.h>
30
//#include <sys/types.h>
31
//#include <sys/stat.h>
32
//#include <fcntl.h>
33
//#endif
34
//
35
//#include "file.h"
36
//
37
//static const int64 MaxROMImageSize = (int64)1 << 26; // 2 ^ 26 = 64MiB
38
//
39
//enum
40
//{
41
// MDFN_FILETYPE_PLAIN = 0,
42
// MDFN_FILETYPE_GZIP = 1,
43
// MDFN_FILETYPE_ZIP = 2,
44
//};
45
//
46
//void MDFNFILE::MakeMemWrap(void *tz, int type)
47
//{
48
// #ifdef HAVE_MMAP
49
// is_mmap = FALSE;
50
// #endif
51
// location = 0;
52
//
53
// if(type == MDFN_FILETYPE_PLAIN)
54
// {
55
// ::fseek((FILE *)tz, 0, SEEK_END);
56
// f_size = ::ftell((FILE *)tz);
57
// ::fseek((FILE *)tz, 0, SEEK_SET);
58
//
59
//
60
// #ifdef HAVE_MMAP
61
// if((void *)-1 != (f_data = (uint8 *)mmap(NULL, size, PROT_READ, MAP_SHARED, fileno((FILE *)tz), 0)))
62
// {
63
// //puts("mmap'ed");
64
// is_mmap = TRUE;
65
// #ifdef HAVE_MADVISE
66
// madvise(f_data, size, MADV_SEQUENTIAL | MADV_WILLNEED);
67
// #endif
68
// }
69
// else
70
// #endif
71
// {
72
// f_data = (uint8 *)MDFN_malloc_T(size, _("file read buffer"));
73
//
74
// if((int64)::fread(f_data, 1, size, (FILE *)tz) != size)
75
// {
76
// ErrnoHolder ene(errno);
77
//
78
// throw MDFN_Error(ene.Errno(), _("Error reading file: %s"), ene.StrError());
79
// }
80
// }
81
// }
82
// else if(type == MDFN_FILETYPE_GZIP)
83
// {
84
// uint32_t cur_size = 0;
85
// uint32_t cur_alloced = 65536;
86
// int howmany;
87
//
88
// f_data = (uint8 *)MDFN_malloc_T(cur_alloced, _("file read buffer"));
89
//
90
// while((howmany = gzread((gzFile)tz, f_data + cur_size, cur_alloced - cur_size)) > 0)
91
// {
92
// cur_size += howmany;
93
// cur_alloced <<= 1;
94
//
95
// if(cur_size > MaxROMImageSize)
96
// throw MDFN_Error(0, _("ROM image is too large; maximum size allowed is %llu bytes."), (unsigned long long)MaxROMImageSize);
97
//
98
// f_data = (uint8 *)MDFN_realloc_T(f_data, cur_alloced, _("file read buffer"));
99
// }
100
//
101
// f_data = (uint8 *)MDFN_realloc_T(f_data, cur_size, _("file read buffer"));
102
// f_size = cur_size;
103
//
104
// {
105
// int gzerrnum = 0;
106
// const char *gzerrstring;
107
// if((gzerrstring = gzerror((gzFile)tz, &gzerrnum)) && gzerrnum != Z_OK && gzerrnum != Z_STREAM_END)
108
// {
109
// if(gzerrnum != Z_ERRNO)
110
// {
111
// throw MDFN_Error(0, _("Error reading file: zlib error: %s"), gzerrstring);
112
// }
113
// else
114
// {
115
// ErrnoHolder ene(errno);
116
// throw MDFN_Error(ene.Errno(), _("Error reading file: %s"), ene.StrError());
117
// }
118
// }
119
// }
120
// }
121
// else if(type == MDFN_FILETYPE_ZIP)
122
// {
123
// unz_file_info ufo;
124
// unzGetCurrentFileInfo((unzFile)tz, &ufo, 0, 0, 0, 0, 0, 0);
125
//
126
// f_size = ufo.uncompressed_size;
127
//
128
// if(size > MaxROMImageSize)
129
// throw MDFN_Error(0, _("ROM image is too large; maximum size allowed is %llu bytes."), (unsigned long long)MaxROMImageSize);
130
//
131
// f_data = (uint8 *)MDFN_malloc_T(ufo.uncompressed_size, _("file read buffer"));
132
// unzReadCurrentFile((unzFile)tz, f_data, ufo.uncompressed_size);
133
// }
134
//}
135
//
136
//MDFNFILE::MDFNFILE(const char *path, const FileExtensionSpecStruct *known_ext, const char *purpose) : size(f_size), data((const uint8* const &)f_data), ext((const char * const &)f_ext), fbase((const char * const &)f_fbase)
137
//{
138
// f_data = NULL;
139
// f_size = 0;
140
// f_ext = NULL;
141
// f_fbase = NULL;
142
//
143
// location = 0;
144
//
145
// #ifdef HAVE_MMAP
146
// is_mmap = 0;
147
// #endif
148
//
149
// Open(path, known_ext, purpose);
150
//}
151
//
152
//MDFNFILE::~MDFNFILE()
153
//{
154
// Close();
155
//}
156
//
157
//
158
//void MDFNFILE::Open(const char *path, const FileExtensionSpecStruct *known_ext, const char *purpose)
159
//{
160
// unzFile tz = NULL;
161
// FILE *fp = NULL;
162
// gzFile gzp = NULL;
163
//
164
// try
165
// {
166
// //
167
// // Try opening it as a zip file first
168
// //
169
// if((tz = unzOpen(path)))
170
// {
171
// char tempu[1024];
172
// int errcode;
173
//
174
// if((errcode = unzGoToFirstFile(tz)) != UNZ_OK)
175
// {
176
// throw MDFN_Error(0, _("Could not seek to first file in ZIP archive: %s"), unzErrorString(errcode));
177
// }
178
//
179
// if(known_ext)
180
// {
181
// bool FileFound = FALSE;
182
// while(!FileFound)
183
// {
184
// size_t tempu_strlen;
185
// const FileExtensionSpecStruct *ext_search = known_ext;
186
//
187
// if((errcode = unzGetCurrentFileInfo(tz, 0, tempu, 1024, 0, 0, 0, 0)) != UNZ_OK)
188
// {
189
// throw MDFN_Error(0, _("Could not get file information in ZIP archive: %s"), unzErrorString(errcode));
190
// }
191
//
192
// tempu[1023] = 0;
193
// tempu_strlen = strlen(tempu);
194
//
195
// while(ext_search->extension && !FileFound)
196
// {
197
// size_t ttmeow = strlen(ext_search->extension);
198
// if(tempu_strlen >= ttmeow)
199
// {
200
// if(!strcasecmp(tempu + tempu_strlen - ttmeow, ext_search->extension))
201
// FileFound = TRUE;
202
// }
203
// ext_search++;
204
// }
205
//
206
// if(FileFound)
207
// break;
208
//
209
// if((errcode = unzGoToNextFile(tz)) != UNZ_OK)
210
// {
211
// if(errcode != UNZ_END_OF_LIST_OF_FILE)
212
// {
213
// throw MDFN_Error(0, _("Error seeking to next file in ZIP archive: %s"), unzErrorString(errcode));
214
// }
215
//
216
// if((errcode = unzGoToFirstFile(tz)) != UNZ_OK)
217
// {
218
// throw MDFN_Error(0, _("Could not seek to first file in ZIP archive: %s"), unzErrorString(errcode));
219
// }
220
// break;
221
// }
222
// } // end to while(!FileFound)
223
// } // end to if(ext)
224
//
225
// if((errcode = unzOpenCurrentFile(tz)) != UNZ_OK)
226
// {
227
// throw MDFN_Error(0, _("Could not open file in ZIP archive: %s"), unzErrorString(errcode));
228
// }
229
//
230
// MakeMemWrap(tz, MDFN_FILETYPE_ZIP);
231
//
232
// {
233
// char *ld = strrchr(tempu, '.');
234
//
235
// f_ext = strdup(ld ? ld + 1 : "");
236
// f_fbase = strdup(tempu);
237
// if(ld)
238
// f_fbase[ld - tempu] = 0;
239
// }
240
// }
241
// else // If it's not a zip file, handle it as...another type of file!
242
// {
243
// if(!(fp = fopen(path, "rb")))
244
// {
245
// ErrnoHolder ene(errno);
246
//
247
// throw MDFN_Error(ene.Errno(), _("Error opening \"%s\": %s"), path, ene.StrError());
248
// }
249
//
250
// const char *path_fnp = GetFNComponent(path);
251
//
252
// uint32 gzmagic;
253
//
254
// gzmagic = ::fgetc(fp);
255
// gzmagic |= ::fgetc(fp) << 8;
256
// gzmagic |= ::fgetc(fp) << 16;
257
//
258
// if(gzmagic != 0x088b1f) /* Not gzip... */
259
// {
260
// ::fseek(fp, 0, SEEK_SET);
261
//
262
// MakeMemWrap(fp, MDFN_FILETYPE_PLAIN);
263
//
264
// {
265
// const char *ld = strrchr(path_fnp, '.');
266
// f_ext = strdup(ld ? ld + 1 : "");
267
// f_fbase = strdup(path_fnp);
268
// if(ld)
269
// f_fbase[ld - path_fnp] = 0;
270
// }
271
// }
272
// else /* Probably gzip */
273
// {
274
// fclose(fp);
275
// fp = NULL;
276
//
277
// // Clear errno so we can see if the error occurred within zlib or the C lib
278
// errno = 0;
279
// if(!(gzp = gzopen(path, "rb")))
280
// {
281
// if(errno != 0)
282
// {
283
// ErrnoHolder ene(errno);
284
//
285
// throw MDFN_Error(ene.Errno(), _("Error opening \"%s\": %s"), path, ene.StrError());
286
// }
287
// else
288
// throw MDFN_Error(0, _("Error opening \"%s\": %s"), path, _("zlib error"));
289
// }
290
//
291
// MakeMemWrap(gzp, MDFN_FILETYPE_GZIP);
292
//
293
// char *tmp_path = strdup(path_fnp);
294
// char *ld = strrchr(tmp_path, '.');
295
//
296
// if(ld && ld > tmp_path)
297
// {
298
// char *last_ld = ld;
299
// *ld = 0;
300
// ld = strrchr(tmp_path, '.');
301
// if(!ld) { ld = last_ld; }
302
// else *ld = 0;
303
// }
304
// f_ext = strdup(ld ? ld + 1 : "");
305
// f_fbase = tmp_path;
306
// } // End gzip handling
307
// } // End normal and gzip file handling else to zip
308
// }
309
// catch(...)
310
// {
311
// if(tz != NULL)
312
// {
313
// unzCloseCurrentFile(tz);
314
// unzClose(tz);
315
// }
316
//
317
// if(fp != NULL)
318
// {
319
// fclose(fp);
320
// fp = NULL;
321
// }
322
//
323
// if(gzp != NULL)
324
// {
325
// gzclose(gzp);
326
// gzp = NULL;
327
// }
328
//
329
// Close();
330
// throw;
331
// }
332
//
333
// if(tz != NULL)
334
// {
335
// unzCloseCurrentFile(tz);
336
// unzClose(tz);
337
// }
338
//
339
// if(fp != NULL)
340
// {
341
// fclose(fp);
342
// fp = NULL;
343
// }
344
//
345
// if(gzp != NULL)
346
// {
347
// gzclose(gzp);
348
// gzp = NULL;
349
// }
350
//}
351
//
352
//void MDFNFILE::Close(void) throw()
353
//{
354
// if(f_ext)
355
// {
356
// free(f_ext);
357
// f_ext = NULL;
358
// }
359
//
360
// if(f_fbase)
361
// {
362
// free(f_fbase);
363
// f_fbase = NULL;
364
// }
365
//
366
// if(f_data)
367
// {
368
// #if HAVE_MMAP
369
// if(is_mmap)
370
// munmap(f_data, size);
371
// else
372
// #endif
373
// free(f_data);
374
// f_data = NULL;
375
// }
376
//}
377
//
378
//uint64 MDFNFILE::fread(void *ptr, size_t element_size, size_t nmemb)
379
//{
380
// uint32 total = element_size * nmemb;
381
//
382
// if(location >= f_size)
383
// return 0;
384
//
385
// if((location + total) > f_size)
386
// {
387
// int64 ak = f_size - location;
388
//
389
// memcpy((uint8*)ptr, f_data + location, ak);
390
//
391
// location = f_size;
392
//
393
// return(ak / element_size);
394
// }
395
// else
396
// {
397
// memcpy((uint8*)ptr, f_data + location, total);
398
//
399
// location += total;
400
//
401
// return nmemb;
402
// }
403
//}
404
//
405
//int MDFNFILE::fseek(int64 offset, int whence)
406
//{
407
// switch(whence)
408
// {
409
// case SEEK_SET:if(offset >= f_size)
410
// return(-1);
411
// location = offset;
412
// break;
413
//
414
// case SEEK_CUR:if((offset + location) > f_size)
415
// return(-1);
416
//
417
// location += offset;
418
// break;
419
// }
420
// return 0;
421
//}
422
//
423
//int MDFNFILE::read16le(uint16 *val)
424
//{
425
// if((location + 2) > size)
426
// return 0;
427
//
428
// *val = MDFN_de16lsb(data + location);
429
//
430
// location += 2;
431
//
432
// return(1);
433
//}
434
//
435
//int MDFNFILE::read32le(uint32 *val)
436
//{
437
// if((location + 4) > size)
438
// return 0;
439
//
440
// *val = MDFN_de32lsb(data + location);
441
//
442
// location += 4;
443
//
444
// return(1);
445
//}
446
//
447
//char *MDFNFILE::fgets(char *s, int buffer_size)
448
//{
449
// int pos = 0;
450
//
451
// if(!buffer_size)
452
// return(NULL);
453
//
454
// if(location >= buffer_size)
455
// return(NULL);
456
//
457
// while(pos < (buffer_size - 1) && location < buffer_size)
458
// {
459
// int v = data[location];
460
// s[pos] = v;
461
// location++;
462
// pos++;
463
// if(v == '\n') break;
464
// }
465
//
466
// if(buffer_size)
467
// s[pos] = 0;
468
//
469
// return(s);
470
//}
471
//
472
//static INLINE bool MDFN_DumpToFileReal(const char *filename, int compress, const std::vector<PtrLengthPair> &pearpairs)
473
//{
474
// if(MDFN_GetSettingB("filesys.disablesavegz"))
475
// compress = 0;
476
//
477
// if(compress)
478
// {
479
// char mode[64];
480
// gzFile gp;
481
//
482
// trio_snprintf(mode, 64, "wb%d", compress);
483
//
484
// gp = gzopen(filename, mode);
485
//
486
// if(!gp)
487
// {
488
// ErrnoHolder ene(errno);
489
//
490
// MDFN_PrintError(_("Error opening \"%s\": %s"), filename, ene.StrError());
491
// return(0);
492
// }
493
//
494
// for(unsigned int i = 0; i < pearpairs.size(); i++)
495
// {
496
// const void *data = pearpairs[i].GetData();
497
// const int64 length = pearpairs[i].GetLength();
498
//
499
// if(gzwrite(gp, data, length) != length)
500
// {
501
// int errnum;
502
//
503
// MDFN_PrintError(_("Error writing to \"%s\": %s"), filename, gzerror(gp, &errnum));
504
// gzclose(gp);
505
// return(0);
506
// }
507
// }
508
//
509
// if(gzclose(gp) != Z_OK) // FIXME: Huhm, how should we handle this?
510
// {
511
// MDFN_PrintError(_("Error closing \"%s\""), filename);
512
// return(0);
513
// }
514
// }
515
// else
516
// {
517
// FILE *fp = fopen(filename, "wb");
518
// if(!fp)
519
// {
520
// ErrnoHolder ene(errno);
521
//
522
// MDFN_PrintError(_("Error opening \"%s\": %s"), filename, ene.StrError());
523
// return(0);
524
// }
525
//
526
// for(unsigned int i = 0; i < pearpairs.size(); i++)
527
// {
528
// const void *data = pearpairs[i].GetData();
529
// const uint64 length = pearpairs[i].GetLength();
530
//
531
// if(fwrite(data, 1, length, fp) != length)
532
// {
533
// ErrnoHolder ene(errno);
534
//
535
// MDFN_PrintError(_("Error writing to \"%s\": %s"), filename, ene.StrError());
536
// fclose(fp);
537
// return(0);
538
// }
539
// }
540
//
541
// if(fclose(fp) == EOF)
542
// {
543
// ErrnoHolder ene(errno);
544
//
545
// MDFN_PrintError(_("Error closing \"%s\": %s"), filename, ene.StrError());
546
// return(0);
547
// }
548
// }
549
// return(1);
550
//}
551
//
552
//bool MDFN_DumpToFile(const char *filename, int compress, const std::vector<PtrLengthPair> &pearpairs)
553
//{
554
// return(MDFN_DumpToFileReal(filename, compress, pearpairs));
555
//}
556
//
557
//bool MDFN_DumpToFile(const char *filename, int compress, const void *data, uint64 length)
558
//{
559
// std::vector<PtrLengthPair> tmp_pairs;
560
// tmp_pairs.push_back(PtrLengthPair(data, length));
561
// return(MDFN_DumpToFileReal(filename, compress, tmp_pairs));
562
//}
563
564