~ubuntu-branches/ubuntu/saucy/mupen64plus/saucy

« back to all changes in this revision

Viewing changes to .pc/system-libbz2.patch/main/rom.c

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2011-07-24 14:23:26 UTC
  • mfrom: (10.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110724142326-x9z5qu8j9jecrmod
Tags: 1.99.4+2
* Upload to unstable
* Remove overrides for lintian warning about change to native package
* Update Vcs-* fields to new anonscm.debian.org URLs in debian/control
* Fix spelling of "Flexible" in debian/control (Closes: #633693)
* Mark all targets in debian/rules as phony
* Add some information about the mupen64plus 2.0 vision in debian/NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 
 *   Mupen64plus - rom.c                                                   *
3
 
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4
 
 *   Copyright (C) 2008 Tillin9                                            *
5
 
 *   Copyright (C) 2002 Hacktarux                                          *
6
 
 *                                                                         *
7
 
 *   This program is free software; you can redistribute it and/or modify  *
8
 
 *   it under the terms of the GNU General Public License as published by  *
9
 
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 
 *   (at your option) any later version.                                   *
11
 
 *                                                                         *
12
 
 *   This program is distributed in the hope that it will be useful,       *
13
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 
 *   GNU General Public License for more details.                          *
16
 
 *                                                                         *
17
 
 *   You should have received a copy of the GNU General Public License     *
18
 
 *   along with this program; if not, write to the                         *
19
 
 *   Free Software Foundation, Inc.,                                       *
20
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
21
 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22
 
 
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
 
#include <string.h>
26
 
#include <ctype.h>
27
 
#include <limits.h>
28
 
#include <zlib.h>
29
 
 
30
 
#include "zip/unzip.h"
31
 
#include "bzip2/bzlib.h"
32
 
#include "lzma/lzmadec.h"
33
 
#include "7zip/7zExtract.h"
34
 
#include "7zip/7zCrc.h"
35
 
 
36
 
#include "md5.h"
37
 
#include "rom.h"
38
 
#include "romcache.h"
39
 
#include "translate.h"
40
 
#include "main.h"
41
 
#include "util.h"
42
 
 
43
 
#include "../memory/memory.h"
44
 
#include "../opengl/osd.h"
45
 
 
46
 
#define CHUNKSIZE 1024*128 /* Read files 128KB at a time. */
47
 
 
48
 
#ifndef NO_GUI
49
 
#include "gui.h"
50
 
#endif
51
 
 
52
 
/* Global loaded rom memory space. */
53
 
unsigned char* rom;
54
 
/* Global loaded rom size. */
55
 
int taille_rom;
56
 
 
57
 
/* TODO: Replace with a glocal cache_entry. */
58
 
rom_header* ROM_HEADER;
59
 
rom_settings ROM_SETTINGS;
60
 
 
61
 
/* Tests if a file is a valid N64 rom by checking the first 4 bytes. */
62
 
int is_valid_rom(unsigned char buffer[4])
63
 
{
64
 
    /* Test if rom is a native .z64 image with header 0x80371240. [ABCD] */
65
 
    if((buffer[0]==0x80)&&(buffer[1]==0x37)&&(buffer[2]==0x12)&&(buffer[3]==0x40))
66
 
        return 1;
67
 
    /* Test if rom is a byteswapped .v64 image with header 0x37804012. [BADC] */
68
 
    else if((buffer[0]==0x37)&&(buffer[1]==0x80)&&(buffer[2]==0x40)&&(buffer[3]==0x12))
69
 
        return 1;
70
 
    /* Test if rom is a wordswapped .n64 image with header  0x40123780. [DCBA] */
71
 
    else if((buffer[0]==0x40)&&(buffer[1]==0x12)&&(buffer[2]==0x37)&&(buffer[3]==0x80))
72
 
        return 1;
73
 
    else
74
 
        return 0;
75
 
}
76
 
 
77
 
/* If rom is a .v64 or .n64 image, byteswap or wordswap loadlength amount of
78
 
 * rom data to native .z64 before forwarding. Makes sure that data extraction
79
 
 * and MD5ing routines always deal with a .z64 image.
80
 
 */
81
 
void swap_rom(unsigned char* localrom, unsigned char* imagetype, int loadlength)
82
 
{
83
 
    unsigned char temp;
84
 
    int i;
85
 
 
86
 
    /* Btyeswap if .v64 image. */
87
 
    if(localrom[0]==0x37)
88
 
        {
89
 
        *imagetype = V64IMAGE;
90
 
        for (i = 0; i < loadlength; i+=2)
91
 
            {
92
 
            temp=localrom[i];
93
 
            localrom[i]=localrom[i+1];
94
 
            localrom[i+1]=temp;
95
 
            }
96
 
        }
97
 
    /* Wordswap if .n64 image. */
98
 
    else if(localrom[0]==0x40)
99
 
        {
100
 
        *imagetype = N64IMAGE;
101
 
        for (i = 0; i < loadlength; i+=4)
102
 
            {
103
 
            temp=localrom[i];
104
 
            localrom[i]=localrom[1+3];
105
 
            localrom[i+3]=temp;
106
 
            temp=localrom[i+1];
107
 
            localrom[i+1]=localrom[i+2];
108
 
            localrom[i+2]=temp;
109
 
            }
110
 
        }
111
 
    else
112
 
        *imagetype = Z64IMAGE;
113
 
}
114
 
 
115
 
/* Open a file and test if its an uncompressed rom, bzip2ed rom, lzma stream compressed
116
 
 * rom, or gzipped rom. If so, set compressiontype and load *loadlength of the rom
117
 
 * into the returned pointer. On failure return NULL.
118
 
 */
119
 
unsigned char* load_single_rom(const char* filename, int* romsize, unsigned char* compressiontype, int* loadlength)
120
 
{
121
 
    int i;
122
 
    unsigned short romread = 0;
123
 
    unsigned char buffer[4];
124
 
    unsigned char* localrom;
125
 
 
126
 
    FILE* romfile;
127
 
    /* Uncompressed roms. */
128
 
    romfile=fopen(filename, "rb");
129
 
    if(romfile!=NULL)
130
 
        {
131
 
        fread(buffer, 1, 4, romfile);
132
 
        if(is_valid_rom(buffer))
133
 
            {
134
 
            *compressiontype = UNCOMPRESSED;
135
 
            fseek(romfile, 0L, SEEK_END);
136
 
            *romsize=ftell(romfile);
137
 
            fseek(romfile, 0L, SEEK_SET);
138
 
            localrom = (unsigned char*)malloc(*loadlength*sizeof(unsigned char));
139
 
            if(localrom==NULL)
140
 
                {
141
 
                fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
142
 
                return NULL;
143
 
                }
144
 
            fread(localrom, 1, *loadlength, romfile); 
145
 
            romread = 1;
146
 
            }
147
 
        if(romread==0)
148
 
            {
149
 
            /* Bzip2 roms. */
150
 
            BZFILE* bzromfile;
151
 
            int bzerror;
152
 
            fseek(romfile, 0L, SEEK_SET);
153
 
            bzromfile = BZ2_bzReadOpen(&bzerror, romfile, 0, 0, NULL, 0);
154
 
            if(bzerror==BZ_OK)
155
 
                BZ2_bzRead(&bzerror, bzromfile, buffer, 4);
156
 
            if(bzerror==BZ_OK)
157
 
                {
158
 
                if(is_valid_rom(buffer))
159
 
                    {
160
 
                    *compressiontype = BZIP2_COMPRESSION;
161
 
                    fseek(romfile, 0L, SEEK_SET);
162
 
                    bzromfile = BZ2_bzReadOpen(&bzerror, romfile, 0, 0, NULL, 0);
163
 
                    *romsize=0;
164
 
                    localrom=NULL;
165
 
                    for( i = 0; bzerror==BZ_OK; ++i )
166
 
                        {
167
 
                        localrom = (unsigned char*)realloc(localrom, (i+1)*CHUNKSIZE*sizeof(unsigned char));
168
 
                        if(localrom==NULL)
169
 
                           {
170
 
                           fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
171
 
                           return NULL;
172
 
                           }
173
 
                        *romsize += BZ2_bzRead(&bzerror, bzromfile, localrom+(i*CHUNKSIZE), CHUNKSIZE); 
174
 
                        }
175
 
                    if(bzerror==BZ_STREAM_END)
176
 
                       {
177
 
                       localrom = (unsigned char*)realloc(localrom,*loadlength*sizeof(unsigned char));
178
 
                       romread = 1; 
179
 
                       }
180
 
                    else
181
 
                       free(localrom);
182
 
                    }
183
 
                }
184
 
            BZ2_bzReadClose(&bzerror, bzromfile);
185
 
            }
186
 
        if(romread==0)
187
 
            {
188
 
            /* LZMA roms. */
189
 
            fseek(romfile, 0L, SEEK_SET);
190
 
            int lzmastatus;
191
 
            lzmadec_stream stream;
192
 
            stream.lzma_alloc = NULL;
193
 
            stream.lzma_free = NULL;
194
 
            stream.opaque = NULL;
195
 
            stream.avail_in = 0;
196
 
            stream.next_in = NULL;
197
 
 
198
 
            /* Minimum size to get decoded blocks back is 45.
199
 
           LZMA has 13 byte headers, likely 32 byte internal buffer. */
200
 
            unsigned char* buffer_in = (unsigned char*)malloc(45*sizeof(unsigned char));
201
 
            unsigned char* buffer_out = (unsigned char*)malloc(45*128*sizeof(unsigned char));
202
 
            if(buffer_in==NULL||buffer_out==NULL||lzmadec_init(&stream)!=LZMADEC_OK)
203
 
                {
204
 
                fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
205
 
                return NULL;
206
 
                }
207
 
 
208
 
            fread(buffer_in, sizeof(unsigned char), 45, romfile);
209
 
 
210
 
            stream.next_in = buffer_in;
211
 
            stream.avail_in = 45;
212
 
            stream.next_out = buffer_out;
213
 
            stream.avail_out = 45;
214
 
 
215
 
            lzmastatus = lzmadec_decode (&stream, (stream.avail_in==0));
216
 
            if(lzmastatus==LZMADEC_OK&&is_valid_rom(buffer_out))
217
 
                {
218
 
                *compressiontype = LZMA_COMPRESSION;
219
 
                int oldsize;
220
 
                *romsize = 45 - stream.avail_out;
221
 
 
222
 
                buffer_in = (unsigned char*)realloc(buffer_in,CHUNKSIZE*sizeof(unsigned char));
223
 
                buffer_out = (unsigned char*)realloc(buffer_out,CHUNKSIZE*128*sizeof(unsigned char));
224
 
                localrom = (unsigned char*)malloc(*romsize*sizeof(unsigned char));
225
 
                if(buffer_in==NULL||buffer_out==NULL||localrom==NULL)
226
 
                    {
227
 
                    fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
228
 
                    return NULL;
229
 
                    }
230
 
 
231
 
                memcpy(localrom,buffer_out, *romsize);
232
 
                while(lzmastatus==LZMADEC_OK)
233
 
                    {
234
 
                    fread(buffer_in, sizeof(unsigned char), CHUNKSIZE, romfile);
235
 
                    stream.next_in = buffer_in;
236
 
                    stream.avail_in = CHUNKSIZE;
237
 
                    stream.next_out = buffer_out;
238
 
                    stream.avail_out = CHUNKSIZE*128;
239
 
                    lzmastatus = lzmadec_decode (&stream, (stream.avail_in==0));
240
 
 
241
 
                    oldsize = *romsize;
242
 
                    *romsize += CHUNKSIZE*128-stream.avail_out;
243
 
 
244
 
                    localrom = (unsigned char*)realloc(localrom,*romsize*sizeof(unsigned char));
245
 
                    if(localrom==NULL)
246
 
                        {
247
 
                        fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
248
 
                        return NULL;
249
 
                        }
250
 
                    memcpy(localrom+oldsize,buffer_out,CHUNKSIZE*128-stream.avail_out);
251
 
                    }
252
 
 
253
 
                if(lzmastatus==LZMADEC_STREAM_END) 
254
 
                    {
255
 
                    lzmadec_end(&stream);
256
 
                    localrom = (unsigned char*)realloc(localrom,*loadlength*sizeof(unsigned char));
257
 
                    romread = 1;
258
 
                    }
259
 
                }
260
 
            free(buffer_in);
261
 
            free(buffer_out);
262
 
            }
263
 
        fclose(romfile);
264
 
        }
265
 
 
266
 
    /* Gzipped roms. */
267
 
    if(romread==0)
268
 
        {
269
 
        gzFile* gzromfile;
270
 
        gzromfile=gzopen(filename, "rb");
271
 
        if(gzromfile!=NULL)
272
 
            {
273
 
            gzread(gzromfile, buffer, 4);
274
 
            if(is_valid_rom(buffer))
275
 
                {
276
 
                *compressiontype = GZIP_COMPRESSION;
277
 
                gzseek(gzromfile, 0L, SEEK_SET);
278
 
                *romsize=0;
279
 
                localrom=NULL;
280
 
                for( i = 0; !gzeof(gzromfile); ++i )
281
 
                    {
282
 
                    localrom = (unsigned char*)realloc(localrom, (i+1)*CHUNKSIZE*sizeof(unsigned char));
283
 
                    if(localrom==NULL)
284
 
                       {
285
 
                       fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
286
 
                       return NULL;
287
 
                       }
288
 
                    *romsize += gzread(gzromfile, localrom+(i*CHUNKSIZE), CHUNKSIZE);
289
 
                    }
290
 
                if(gzeof(gzromfile))
291
 
                       {
292
 
                       localrom = (unsigned char*)realloc(localrom,*loadlength*sizeof(unsigned char));
293
 
                       romread = 1; 
294
 
                       }
295
 
                    else
296
 
                       free(localrom);
297
 
                gzclose(gzromfile);
298
 
                }
299
 
            }
300
 
        }
301
 
 
302
 
    /* File invalid, or valid rom not found in file. */
303
 
    if(romread==0)
304
 
        return NULL;
305
 
 
306
 
    return localrom;
307
 
}
308
 
 
309
 
/* Open a file and test if its a zip or 7zip archive. If so load check if *archivefile
310
 
 * is a rom and load *loadlength into the returned pointer. If *archivefile is not a rom
311
 
 * in a many file archive, function will keep checking until a rom is found or the 
312
 
 * end of the archive is reached. Returns NULL if unable to find a rom. The large function
313
 
 * API is to allow for persistent 7zip memory space.
314
 
 */
315
 
unsigned char* load_archive_rom(const char* filename, int* romsize, unsigned char* compressiontype, int* loadlength, unsigned int* archivefile, UInt32* blockIndex, Byte** outBuffer, size_t* outBufferSize, CFileInStream* archiveStream, CArchiveDatabaseEx* db)
316
 
{
317
 
    int status;
318
 
    unsigned int filecounter = 0;
319
 
    unsigned short romread = 0;
320
 
    unsigned char* localrom;
321
 
 
322
 
    /* Zip roms. */
323
 
    unsigned char buffer[4];
324
 
    unzFile zipromfile;
325
 
    unz_file_info fileinfo;
326
 
    char szFileName[256], szExtraField[256], szComment[256];
327
 
    zipromfile = unzOpen(filename);
328
 
    if(zipromfile!=NULL) 
329
 
        {
330
 
        if(unzGoToFirstFile(zipromfile)==UNZ_OK)
331
 
            {
332
 
            /* Get first valid rom in archive. */
333
 
            do
334
 
                {
335
 
                if(unzGetCurrentFileInfo(zipromfile, &fileinfo, szFileName, 255, 
336
 
                szExtraField, 255, szComment, 255)!=UNZ_OK)
337
 
                    break;
338
 
                if(unzOpenCurrentFile(zipromfile)!=UNZ_OK)
339
 
                    break;
340
 
 
341
 
                if(fileinfo.uncompressed_size>=4&&filecounter>=*archivefile)
342
 
                    {
343
 
                    if(unzReadCurrentFile(zipromfile, buffer, 4)!=4)
344
 
                        break;
345
 
                    if(is_valid_rom(buffer))
346
 
                        {
347
 
                        unzOpenCurrentFile(zipromfile);
348
 
                        *romsize = fileinfo.uncompressed_size;
349
 
                        localrom = (unsigned char*)malloc(*loadlength*sizeof(unsigned char));
350
 
                        if(localrom==NULL)
351
 
                            {
352
 
                            fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
353
 
                            return NULL;
354
 
                            }
355
 
 
356
 
                        if(unzReadCurrentFile(zipromfile, localrom, *loadlength)==*loadlength)
357
 
                            {
358
 
                            *compressiontype = ZIP_COMPRESSION;
359
 
                            unzCloseCurrentFile(zipromfile);
360
 
                            romread = 1;
361
 
                            break;
362
 
                            }
363
 
                        else
364
 
                            free(localrom);
365
 
                        }
366
 
                    }
367
 
                ++filecounter;
368
 
                }
369
 
            while ((status=unzGoToNextFile(zipromfile)) != UNZ_END_OF_LIST_OF_FILE);
370
 
 
371
 
            unzClose(zipromfile);
372
 
            }
373
 
        }
374
 
    else
375
 
        {
376
 
        /* 7zip */
377
 
        ISzAlloc allocImp;
378
 
        allocImp.Free = SzFree;
379
 
        allocImp.Alloc = SzAlloc;
380
 
 
381
 
        ISzAlloc allocTempImp;
382
 
        allocTempImp.Alloc = SzAllocTemp;
383
 
        allocTempImp.Free = SzFreeTemp;
384
 
 
385
 
        if(archiveStream->File==NULL)
386
 
            {
387
 
            archiveStream->File = fopen(filename, "rb");
388
 
            if(archiveStream->File==NULL)
389
 
                return NULL;
390
 
            status = SzArchiveOpen(&(archiveStream->InStream), db, &allocImp, &allocTempImp);
391
 
            if(status==SZ_OK)
392
 
                printf("Deflating 7zip archive.\n");
393
 
            }
394
 
        else
395
 
            status = SZ_OK;
396
 
 
397
 
        if(status==SZ_OK)
398
 
            {
399
 
            for (filecounter = *archivefile; filecounter < db->Database.NumFiles; ++filecounter)
400
 
                {
401
 
                size_t offset;
402
 
                size_t outSizeProcessed;
403
 
 
404
 
                CFileItem *f = db->Database.Files + filecounter;
405
 
                status = SzExtract(&(archiveStream->InStream), db, filecounter, blockIndex, outBuffer, outBufferSize, &offset, &outSizeProcessed, &allocImp, &allocTempImp);
406
 
                if(status!=SZ_OK)
407
 
                    {
408
 
                    if(status==SZE_NOTIMPL)
409
 
                        printf("7zip decoder doesn't support this archive.");
410
 
                    else if(status==SZE_OUTOFMEMORY)
411
 
                        printf("7zip decoder can not allocate memory\n");
412
 
                    else if(status==SZE_CRC_ERROR)
413
 
                       printf("7zip CRC error");
414
 
                    else
415
 
                       printf("7zip Error# %d\n", status);
416
 
                    break; 
417
 
                    }
418
 
                if(is_valid_rom(*outBuffer+offset))
419
 
                    {
420
 
                    *romsize=f->Size;
421
 
                    *compressiontype = SZIP_COMPRESSION;
422
 
                    localrom = (unsigned char*)malloc(*loadlength*sizeof(unsigned char));
423
 
                    if(localrom==NULL)
424
 
                        {
425
 
                        fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
426
 
                        return NULL;
427
 
                        }
428
 
                    memcpy(localrom,*outBuffer+offset,*loadlength);
429
 
                    romread = 1;
430
 
                    break;
431
 
                    }
432
 
                }
433
 
            }
434
 
        }
435
 
 
436
 
    /* File invalid, or valid rom not found in file. */
437
 
    if(romread==0)
438
 
        return NULL;
439
 
 
440
 
    *archivefile = filecounter;
441
 
    return localrom;
442
 
}
443
 
 
444
 
static int ask_bad(void)
445
 
{
446
 
#ifndef NO_GUI
447
 
    if(!g_Noask)
448
 
        return gui_message(GUI_MESSAGE_CONFIRM,
449
 
                           tr("The rom you are trying to load is probably a bad dump!\n"
450
 
                              "Be warned that this will probably give unexpected results.\n"
451
 
                              "Do you still want to run it?"));
452
 
    else
453
 
#endif
454
 
        printf(tr("The rom you are trying to load is probably a bad dump!\n"
455
 
                  "Be warned that this will probably give unexpected results.\n"));
456
 
 
457
 
    return 1;
458
 
}
459
 
 
460
 
static int ask_hack(void)
461
 
{
462
 
#ifndef NO_GUI
463
 
    if(!g_Noask)
464
 
        return gui_message(GUI_MESSAGE_CONFIRM, tr("The rom you are trying to load is probably a hack!\n"
465
 
                                 "Be warned that this will probably give unexpected results.\n"
466
 
                                 "Do you still want to run it?"));
467
 
    else
468
 
#endif
469
 
        printf(tr("The rom you are trying to load is probably a hack!\n"
470
 
                  "Be warned that this will probably give unexpected results.\n"));
471
 
 
472
 
    return 1;
473
 
}
474
 
 
475
 
int open_rom(const char* filename, unsigned int archivefile)
476
 
{
477
 
    if(g_EmulatorRunning)
478
 
         {
479
 
#ifndef NO_GUI
480
 
         if(!g_Noask)
481
 
             {
482
 
             if(!gui_message(GUI_MESSAGE_CONFIRM,
483
 
                             tr("Emulation is running. Do you want to\n"
484
 
                                "stop it and load the selected rom?")))
485
 
                 return -1;
486
 
             }
487
 
#endif
488
 
         stopEmulation();
489
 
         }
490
 
 
491
 
    md5_state_t state;
492
 
    md5_byte_t digest[16];
493
 
    romdatabase_entry* entry;
494
 
    char buffer[PATH_MAX];
495
 
    unsigned char compressiontype, imagetype;
496
 
    int i;
497
 
 
498
 
    if(rom)
499
 
        free(rom);
500
 
 
501
 
    /* Clear Byte-swapped flag, since ROM is now deleted. */
502
 
    g_MemHasBeenBSwapped = 0;
503
 
 
504
 
    UInt32 blockIndex = 0xFFFFFFFF;
505
 
    Byte* outBuffer = NULL;
506
 
    size_t outBufferSize = 0;
507
 
 
508
 
    CFileInStream archiveStream;
509
 
    archiveStream.File = NULL;
510
 
    archiveStream.InStream.Read = SzFileReadImp;
511
 
    archiveStream.InStream.Seek = SzFileSeekImp;
512
 
 
513
 
    CArchiveDatabaseEx db;
514
 
    SzArDbExInit(&db);
515
 
    CrcGenerateTable();
516
 
 
517
 
    strncpy(buffer, filename, PATH_MAX-1);
518
 
    buffer[PATH_MAX-1] = 0;
519
 
    if ((rom=load_single_rom(filename, &taille_rom, &compressiontype, &taille_rom))==NULL)
520
 
        {
521
 
        if((rom=load_archive_rom(filename, &taille_rom, &compressiontype, &taille_rom, &archivefile, &blockIndex, &outBuffer, &outBufferSize, &archiveStream, &db))==NULL)
522
 
            {
523
 
            error_message(tr("Couldn't load Rom!")); 
524
 
            return -1;
525
 
            }
526
 
        }
527
 
 
528
 
    if(outBuffer)
529
 
        free(outBuffer);
530
 
    if(archiveStream.File)
531
 
        fclose(archiveStream.File);
532
 
    SzArDbExFree(&db, free);
533
 
 
534
 
    swap_rom(rom, &imagetype, taille_rom);
535
 
 
536
 
    compressionstring(compressiontype, buffer);
537
 
    printf("Compression: %s\n", buffer);
538
 
 
539
 
    imagestring(imagetype, buffer);
540
 
    printf("Imagetype: %s\n", buffer);
541
 
 
542
 
    printf("Rom size: %d bytes (or %d Mb or %d Megabits)\n",
543
 
    taille_rom, taille_rom/1024/1024, taille_rom/1024/1024*8);
544
 
 
545
 
    /* TODO: Replace the following validation code with fill_entry(). */
546
 
 
547
 
    /* Load rom settings and check if it's a good dump. */
548
 
    md5_init(&state);
549
 
    md5_append(&state, (const md5_byte_t*)rom, taille_rom);
550
 
    md5_finish(&state, digest);
551
 
    for ( i = 0; i < 16; ++i ) 
552
 
        sprintf(buffer+i*2, "%02X", digest[i]);
553
 
    buffer[32] = '\0';
554
 
    strcpy(ROM_SETTINGS.MD5, buffer);
555
 
    printf("MD5: %s\n", buffer);
556
 
 
557
 
    if(ROM_HEADER)
558
 
        free(ROM_HEADER);
559
 
    ROM_HEADER = malloc(sizeof(rom_header));
560
 
    if(ROM_HEADER==NULL)
561
 
        {
562
 
        fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
563
 
        return 0;
564
 
        }
565
 
    memcpy(ROM_HEADER, rom, sizeof(rom_header));
566
 
    trim((char*)ROM_HEADER->nom); /* Remove trailing whitespace from Rom name. */
567
 
 
568
 
    printf("%x %x %x %x\n", ROM_HEADER->init_PI_BSB_DOM1_LAT_REG,
569
 
                            ROM_HEADER->init_PI_BSB_DOM1_PGS_REG,
570
 
                            ROM_HEADER->init_PI_BSB_DOM1_PWD_REG,
571
 
                            ROM_HEADER->init_PI_BSB_DOM1_PGS_REG2);
572
 
    printf("ClockRate = %x\n", sl((unsigned int)ROM_HEADER->ClockRate));
573
 
    printf("Version: %x\n", sl((unsigned int)ROM_HEADER->Release));
574
 
    printf("CRC: %x %x\n", sl((unsigned int)ROM_HEADER->CRC1), sl((unsigned int)ROM_HEADER->CRC2));
575
 
    printf ("Name: %s\n", ROM_HEADER->nom);
576
 
    if(sl(ROM_HEADER->Manufacturer_ID) == 'N')
577
 
        printf ("Manufacturer: Nintendo\n");
578
 
    else
579
 
        printf("Manufacturer: %x\n", (unsigned int)(ROM_HEADER->Manufacturer_ID));
580
 
    printf("Cartridge_ID: %x\n", ROM_HEADER->Cartridge_ID);
581
 
 
582
 
    countrycodestring(ROM_HEADER->Country_code, buffer);
583
 
    printf("Country: %s\n", buffer);
584
 
    printf ("PC = %x\n", sl((unsigned int)ROM_HEADER->PC));
585
 
 
586
 
    if((entry=ini_search_by_md5(digest))==&empty_entry)
587
 
        {
588
 
        if((entry=ini_search_by_crc(sl(ROM_HEADER->CRC1),sl(ROM_HEADER->CRC2)))==&empty_entry)
589
 
            {
590
 
            strcpy(ROM_SETTINGS.goodname, (char*)ROM_HEADER->nom);
591
 
            strcat(ROM_SETTINGS.goodname, " (unknown rom)");
592
 
            printf("%s\n", ROM_SETTINGS.goodname);
593
 
            ROM_SETTINGS.eeprom_16kb = 0;
594
 
            return 0;
595
 
            }
596
 
        }
597
 
 
598
 
    unsigned short close = 0;
599
 
    char* s = entry->goodname;
600
 
    if(s!=NULL)
601
 
        {
602
 
        for ( i = strlen(s); i > 1; --i )
603
 
        if(i!=0)
604
 
            {
605
 
            if(s[i-1]=='['&&(s[i]=='T'||s[i]=='t'||s[i]=='h'||s[i]=='f'))
606
 
                {
607
 
                if(!ask_hack())
608
 
                    close = 1;
609
 
                }
610
 
            else if(s[i-1]=='['&&s[i]=='b')
611
 
                {
612
 
                if(!ask_bad())
613
 
                    close = 1;
614
 
                }
615
 
            }
616
 
        }
617
 
 
618
 
    if(close)
619
 
        {
620
 
        free(rom);
621
 
        rom = NULL;
622
 
        free(ROM_HEADER);
623
 
        ROM_HEADER = NULL;
624
 
        main_message(0, 1, 0, OSD_BOTTOM_LEFT, tr("Rom closed."));
625
 
        return -3;
626
 
        }
627
 
 
628
 
    strncpy(ROM_SETTINGS.goodname, entry->goodname, 255);
629
 
    ROM_SETTINGS.goodname[255] = '\0';
630
 
 
631
 
    if(entry->savetype==EEPROM_16KB)
632
 
        ROM_SETTINGS.eeprom_16kb = 1;
633
 
    printf("EEPROM type: %d\n", ROM_SETTINGS.eeprom_16kb);
634
 
    return 0;
635
 
}
636
 
 
637
 
int close_rom(void)
638
 
{
639
 
    if(g_EmulatorRunning)
640
 
        stopEmulation();
641
 
 
642
 
    if(ROM_HEADER)
643
 
        {
644
 
        free(ROM_HEADER);
645
 
        ROM_HEADER = NULL;
646
 
        }
647
 
 
648
 
    if(rom)
649
 
        {
650
 
        free(rom);
651
 
        rom = NULL;
652
 
        }
653
 
     else
654
 
        return -1;
655
 
 
656
 
    /* Clear Byte-swapped flag, since ROM is now deleted. */
657
 
    g_MemHasBeenBSwapped = 0;
658
 
    main_message(0, 1, 0, OSD_BOTTOM_LEFT, tr("Rom closed."));
659
 
 
660
 
    return 0;
661
 
}
662