00001 #include "rom_metadata.h"
00002
00003 #include <QFile>
00004
00005 #include <mythcontext.h>
00006
00007 #include "unzip.h"
00008
00009 static int calcOffset(QString GameType, uLong filesize) {
00010 int result;
00011 uLong rom_size;
00012
00013 result = 0;
00014
00015 if (GameType == "NES")
00016 {
00017 result = 16;
00018 }
00019 else if (GameType == "SNES")
00020 {
00021 rom_size = (filesize / 0x2000) * 0x2000;
00022
00023 if (rom_size < filesize)
00024 result = filesize - rom_size;
00025 }
00026 else if (GameType == "PCE")
00027 {
00028 if (filesize & 0x0FFF)
00029 result = filesize & 0x0FFF;
00030
00031 }
00032
00033 return result;
00034 }
00035
00036 static QString crcStr(uLong crc)
00037 {
00038 QString tmpcrc("");
00039
00040 tmpcrc = QString("%1").arg( crc, 0, 16 );
00041 if (tmpcrc == "0")
00042 tmpcrc = "";
00043 else
00044 tmpcrc = tmpcrc.rightJustified(8, '0');
00045
00046 return tmpcrc;
00047 }
00048
00049
00050 QString crcinfo(QString romname, QString GameType, QString *key, RomDBMap *romDB)
00051 {
00052
00053 char block[32768];
00054 uLong crc = crc32(0, Z_NULL, 0);
00055 QString crcRes;
00056 char filename_inzip[256];
00057 unz_file_info file_info;
00058 int offset;
00059 unzFile zf;
00060 int blocksize;
00061
00062 blocksize = 8192;
00063 #if 0
00064 LOG(VB_GENERAL, LOG_DEBUG,
00065 QString("crcinfo : %1 : %2 :").arg(romname).arg(GameType));
00066 #endif
00067
00068 if ((zf = unzOpen(qPrintable(romname))))
00069 {
00070 int FoundFile;
00071 for (FoundFile = unzGoToFirstFile(zf); FoundFile == UNZ_OK;
00072 FoundFile = unzGoToNextFile(zf))
00073 {
00074 if (unzOpenCurrentFile(zf) == UNZ_OK)
00075 {
00076 unzGetCurrentFileInfo(zf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
00077
00078 offset = calcOffset(GameType, file_info.uncompressed_size);
00079
00080 if (offset > 0)
00081 unzReadCurrentFile(zf, block, offset);
00082
00083
00084 int count;
00085 while ((count = unzReadCurrentFile(zf, block, blocksize)) > 0)
00086 {
00087 crc = crc32(crc, (Bytef *)block, (uInt)count);
00088 }
00089 crcRes = crcStr(crc);
00090 *key = QString("%1:%2")
00091 .arg(crcRes)
00092 .arg(filename_inzip);
00093
00094 if (romDB->contains(*key))
00095 {
00096 unzCloseCurrentFile(zf);
00097 break;
00098 }
00099
00100 unzCloseCurrentFile(zf);
00101 }
00102 }
00103 unzClose(zf);
00104 }
00105 else
00106 {
00107 QFile f(romname);
00108
00109 if (f.open(QIODevice::ReadOnly))
00110 {
00111 offset = calcOffset(GameType, f.size());
00112
00113 if (offset > 0)
00114 f.read(block, offset);
00115
00116
00117 qint64 count;
00118 while ((count = f.read(block, blocksize)) > 0)
00119 {
00120 crc = crc32(crc, (Bytef *)block, (uInt)count);
00121 }
00122
00123 crcRes = crcStr(crc);
00124 *key = QString("%1:").arg(crcRes);
00125 f.close();
00126 }
00127 }
00128
00129 return crcRes;
00130 }
00131