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