~ubuntu-branches/debian/experimental/mednafen/experimental

« back to all changes in this revision

Viewing changes to src/file.cpp

  • Committer: Package Import Robot
  • Author(s): Stephen Kitt
  • Date: 2012-01-31 07:21:35 UTC
  • mfrom: (1.2.8)
  • Revision ID: package-import@ubuntu.com-20120131072135-es3dj12y00xcnrsk
Tags: 0.9.19-1
* New upstream WIP version.
* Update copyright information.
* Refresh use-system-tremor.patch and remove psx-big-endian-only.patch.
* Add spelling-fixes.patch based on Lintian's recommendations.
* Build-depend on debhelper 9 or later and remove corresponding Lintian
  override.

Show diffs side-by-side

added added

removed removed

Lines of Context:
230
230
 
231
231
// This function should ALWAYS close the system file "descriptor"(gzip library, zip library, or FILE *) it's given,
232
232
// even if it errors out.
233
 
bool MDFNFILE::MakeMemWrap(void *tz, int type)
 
233
bool MDFNFILE::MakeMemWrapAndClose(void *tz, int type)
234
234
{
235
235
 bool ret = FALSE;
236
236
 
272
272
   }
273
273
   if((int64)::fread(f_data, 1, size, (FILE *)tz) != size)
274
274
   {
 
275
    ErrnoHolder ene(errno);
 
276
    MDFN_PrintError(_("Error reading file: %s"), ene.StrError());
 
277
 
275
278
    free(f_data);
276
279
    goto doret;
277
280
   }
294
297
  {
295
298
   cur_size += howmany;
296
299
   cur_alloced <<= 1;
 
300
 
 
301
   if(cur_size > MaxROMImageSize)
 
302
   {
 
303
    MDFN_PrintError(_("ROM image is too large; maximum size allowed is %llu bytes."), (unsigned long long)MaxROMImageSize);
 
304
    goto doret;
 
305
   }
 
306
 
297
307
   if(!(f_data = (uint8 *)MDFN_realloc(f_data, cur_alloced, _("file read buffer")))) 
298
308
   {
299
309
    goto doret;
306
316
  }
307
317
 
308
318
  f_size = cur_size;
 
319
 
 
320
  {
 
321
   int gzerrnum = 0;
 
322
   const char *gzerrstring;
 
323
   if((gzerrstring = gzerror(tz, &gzerrnum)) && gzerrnum != Z_OK && gzerrnum != Z_STREAM_END)
 
324
   {
 
325
    if(gzerrnum != Z_ERRNO)
 
326
    {
 
327
     MDFN_PrintError(_("Error reading file: zlib error: %s"), gzerrstring);
 
328
    }
 
329
    else
 
330
    {
 
331
     ErrnoHolder ene(errno);
 
332
     MDFN_PrintError(_("Error reading file: %s"), ene.StrError());
 
333
    }
 
334
    goto doret;
 
335
   }
 
336
  }
309
337
 }
310
338
 else if(type == MDFN_FILETYPE_ZIP)
311
339
 {
378
406
 
379
407
bool MDFNFILE::Open(const char *path, const FileExtensionSpecStruct *known_ext, const char *purpose, const bool suppress_notfound_pe)
380
408
{
381
 
 void *t;
382
409
 unzFile tz;
383
410
 
384
411
 local_errno = 0;
461
488
   return(NULL);
462
489
  }
463
490
 
464
 
  if(!MakeMemWrap(tz, MDFN_FILETYPE_ZIP))
 
491
  if(!MakeMemWrapAndClose(tz, MDFN_FILETYPE_ZIP))
465
492
   return(0);
466
493
 
467
494
  char *ld = strrchr(tempu, '.');
470
497
 }
471
498
 else // If it's not a zip file, handle it as...another type of file!
472
499
 {
473
 
  if(!(t = fopen(path,"rb")))
 
500
  FILE *fp;
 
501
 
 
502
  if(!(fp = fopen(path, "rb")))
474
503
  {
475
504
   ErrnoHolder ene(errno);
476
505
   local_errno = ene.Errno();
480
509
    local_errno = ene.Errno();
481
510
    error_code = MDFNFILE_EC_NOTFOUND;
482
511
   }
483
 
   
 
512
 
484
513
   if(ene.Errno() != ENOENT || !suppress_notfound_pe)
485
514
    MDFN_PrintError(_("Error opening \"%s\": %s"), path, ene.StrError());
486
515
 
489
518
 
490
519
  uint32 gzmagic;
491
520
 
492
 
  gzmagic = ::fgetc((FILE *)t);
493
 
  gzmagic |= ::fgetc((FILE *)t)<<8;
494
 
  gzmagic |= ::fgetc((FILE *)t)<<16;
 
521
  gzmagic = ::fgetc(fp);
 
522
  gzmagic |= ::fgetc(fp) << 8;
 
523
  gzmagic |= ::fgetc(fp) << 16;
495
524
 
496
 
  if(gzmagic!=0x088b1f)   /* Not gzip... */
 
525
  if(gzmagic != 0x088b1f)   /* Not gzip... */
497
526
  {
498
 
   ::fseek((FILE *)t,0,SEEK_SET);
 
527
   ::fseek(fp, 0, SEEK_SET);
499
528
 
500
 
   if(!MakeMemWrap(t, 0))
 
529
   if(!MakeMemWrapAndClose(fp, MDFN_FILETYPE_PLAIN))
501
530
    return(0);
502
531
 
503
532
   const char *ld = strrchr(path, '.');
505
534
  }
506
535
  else                  /* Probably gzip */
507
536
  {
508
 
   int fd;
509
 
 
510
 
   fd = dup(fileno( (FILE *)t));
511
 
   lseek(fd, 0, SEEK_SET);
512
 
 
513
 
   if(!(t=gzdopen(fd, "rb")))
514
 
   {
515
 
    close(fd);
516
 
    return(0);
517
 
   }
518
 
 
519
 
   if(!MakeMemWrap(t, 1))
520
 
   {
521
 
    gzclose(t);
522
 
    return(0);
523
 
   }
 
537
   gzFile gzp;
 
538
 
 
539
   fclose(fp);
 
540
 
 
541
   // Clear errno so we can see if the error occurred within zlib or the C lib
 
542
   errno = 0;
 
543
   if(!(gzp = gzopen(path, "rb")))
 
544
   {
 
545
    if(errno != 0)
 
546
    {
 
547
     ErrnoHolder ene(errno);
 
548
     local_errno = ene.Errno();
 
549
 
 
550
     if(ene.Errno() == ENOENT)
 
551
     {
 
552
      local_errno = ene.Errno();
 
553
      error_code = MDFNFILE_EC_NOTFOUND;
 
554
     }
 
555
 
 
556
     if(ene.Errno() != ENOENT || !suppress_notfound_pe)
 
557
      MDFN_PrintError(_("Error opening \"%s\": %s"), path, ene.StrError());
 
558
    }
 
559
    else
 
560
     MDFN_PrintError(_("Error opening \"%s\": %s"), path, _("zlib error"));
 
561
 
 
562
    return(0);
 
563
   }
 
564
 
 
565
   if(!MakeMemWrapAndClose(gzp, MDFN_FILETYPE_GZIP))
 
566
    return(0);
524
567
 
525
568
   char *tmp_path = strdup(path);
526
569
   char *ld = strrchr(tmp_path, '.');