-
Notifications
You must be signed in to change notification settings - Fork 0
/
QGBArchive.cpp
93 lines (72 loc) · 2.39 KB
/
QGBArchive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "QGBArchive.h"
#include <QtCore/QDebug>
#include <QtCore/QFile>
QGBArchive::QGBArchive(QString romFile, quint64 offset, QString uncompressedFile, QGBArchiveMode mode, QGBDeCompress::QGBBlockSize blockSize, quint64 maxSize)
{
if (mode == QAMExtract)
extract(romFile, offset, uncompressedFile);
else if (mode == QAMInsert)
insert(romFile, offset, uncompressedFile, blockSize, maxSize);
else
{
qDebug() << "something went wrong";
return;
}
}
bool QGBArchive::extract(QString romFile, quint64 offset, QString uncompressedFile)
{
QFile *rom = new QFile(romFile);
QFile uncomp(uncompressedFile);
rom->open(QIODevice::ReadOnly);
quint16 size;
QGBDeCompress *decomp = new QGBDeCompress();
QByteArray *decompressed = decomp->decompress(rom, offset, size);
delete decomp;
rom->close();
delete rom;
if (decompressed != NULL)
{
uncomp.open(QIODevice::WriteOnly);
uncomp.seek(0);
QDataStream out(&uncomp);
for (int i = 0; i < decompressed->size(); i++)
out << (quint8)decompressed->at(i);
delete decompressed;
uncomp.close();
return true;
}
return false;
}
bool QGBArchive::insert(QString romFile, quint64 offset, QString uncompressedFile, QGBDeCompress::QGBBlockSize blockSize, quint64 maxSize)
{
QFile rom(romFile);
QFile *uncomp = new QFile(uncompressedFile);
uncomp->open(QIODevice::ReadOnly);
uncomp->seek(0);
QGBDeCompress *comp = new QGBDeCompress();
QByteArray *compressed = comp->compress(uncomp, blockSize);
delete comp;
uncomp->close();
delete uncomp;
if (compressed != NULL)
{
if (compressed->size() > maxSize)
{
qDebug() << "WARNING: compressed archive too large!";
qDebug() << "nothing written";
qDebug() << QString("max size %1, compressed size %2").arg(maxSize).arg(compressed->size());
delete compressed;
return false;
}
qDebug() << QString("max size %1, compressed size %2").arg(maxSize).arg(compressed->size());
rom.open(QIODevice::ReadWrite);
rom.seek(offset);
QDataStream out(&rom);
for (int i = 0; i < compressed->size(); i++)
out << (quint8)compressed->at(i);
delete compressed;
rom.close();
return true;
}
return false;
}