Skip to content

Commit cc9c88f

Browse files
myst6resithlord48
authored andcommitted
Lgp: Fix lookup with a dot for the second character
1 parent c6711a6 commit cc9c88f

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

src/formats/Lgp_p.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ LgpToc::~LgpToc()
235235

236236
bool LgpToc::addEntry(LgpHeaderEntry *entry)
237237
{
238-
qint32 v = lookupValue(entry->fileName());
238+
qint16 v = lookupValue(entry->fileName());
239239
if (v < 0) {
240240
return false;
241241
}
@@ -244,20 +244,20 @@ bool LgpToc::addEntry(LgpHeaderEntry *entry)
244244
return false;
245245
}
246246

247-
_header.insert(v, entry);
247+
_header.insert(quint16(v), entry);
248248

249249
return true;
250250
}
251251

252252
LgpHeaderEntry *LgpToc::entry(const QString &filePath) const
253253
{
254-
qint32 v = lookupValue(filePath);
254+
qint16 v = lookupValue(filePath);
255255
if (v < 0) {
256256
qDebug() << "LgpToc::entry invalid lookup" << filePath;
257257
return nullptr; // invalid file name
258258
}
259-
260-
return entry(filePath, v);
259+
260+
return entry(filePath, quint16(v));
261261
}
262262

263263
QList<LgpHeaderEntry *> LgpToc::entries(quint16 id) const
@@ -288,7 +288,7 @@ LgpHeaderEntry *LgpToc::entry(const QString &filePath, quint16 id) const
288288

289289
bool LgpToc::removeEntry(const QString &filePath)
290290
{
291-
qint32 v = lookupValue(filePath);
291+
qint16 v = lookupValue(filePath);
292292
if (v < 0) {
293293
return false; // invalid file name
294294
}
@@ -297,8 +297,8 @@ bool LgpToc::removeEntry(const QString &filePath)
297297
if (e == nullptr) {
298298
return false; // file not found
299299
}
300-
301-
bool ok = _header.remove(v, e) > 0;
300+
301+
bool ok = _header.remove(quint16(v), e) > 0;
302302

303303
delete e;
304304

@@ -314,27 +314,27 @@ bool LgpToc::renameEntry(const QString &filePath, const QString &newFilePath)
314314
{
315315
// Get file
316316

317-
qint32 v = lookupValue(filePath);
317+
qint16 v = lookupValue(filePath);
318318
if (v < 0) {
319319
qWarning() << "LgpToc::renameEntry invalid filename" << filePath;
320320
return false; // invalid file name
321321
}
322-
323-
LgpHeaderEntry *e = entry(filePath, v);
322+
323+
LgpHeaderEntry *e = entry(filePath, quint16(v));
324324
if (e == nullptr) {
325325
qWarning() << "LgpToc::renameEntry file not found" << filePath;
326326
return false; // file not found
327327
}
328328

329329
// Get new file
330330

331-
qint32 newV = lookupValue(newFilePath);
331+
qint16 newV = lookupValue(newFilePath);
332332
if (newV < 0) {
333333
qWarning() << "LgpToc::renameEntry invalid new filename" << newFilePath;
334334
return false; // invalid file name
335335
}
336336

337-
if (entry(newFilePath, newV) != nullptr) {
337+
if (entry(newFilePath, quint16(newV)) != nullptr) {
338338
qWarning() << "LgpToc::renameEntry new file exists" << newFilePath;
339339
return false; // file found
340340
}
@@ -397,7 +397,7 @@ LgpToc &LgpToc::operator=(const LgpToc &other)
397397
return *this;
398398
}
399399

400-
qint32 LgpToc::lookupValue(const QString &filePath)
400+
qint16 LgpToc::lookupValue(const QString &filePath)
401401
{
402402
int index = filePath.lastIndexOf('/');
403403

@@ -411,39 +411,31 @@ qint32 LgpToc::lookupValue(const QString &filePath)
411411
return -1;
412412
}
413413

414-
char c1 = lookupValue(filePath.at(index));
415-
416-
if (c1 > LOOKUP_VALUE_MAX) {
417-
return -1;
418-
}
414+
qint16 ret = lookupValue(filePath.at(index)) * LOOKUP_VALUE_MAX;
419415

420-
char c2 = lookupValue(filePath.at(index + 1));
416+
QChar secondChar = filePath.at(index + 1);
421417

422-
if (c2 > LOOKUP_VALUE_MAX) {
423-
return -1;
418+
if (secondChar != '.') {
419+
ret += lookupValue(secondChar) + 1;
424420
}
425421

426-
return c1 * LOOKUP_VALUE_MAX + c2 + 1;
422+
return ret >= LOOKUP_TABLE_ENTRIES ? -1 : ret;
427423
}
428424

429-
quint8 LgpToc::lookupValue(const QChar &qc)
425+
qint8 LgpToc::lookupValue(const QChar &qc)
430426
{
431427
char c = qc.toLower().toLatin1();
432428

433-
if (c == '.') {
434-
return 255;
435-
}
436-
437429
if (c >= '0' && c <= '9') {
438-
c += 'a' - '0';
430+
return qint8(c - '0'); // Returns 0 to 9
439431
}
440-
441432
if (c == '_') {
442-
c = 'k';
433+
return 10;
443434
}
444435
if (c == '-') {
445-
c = 'l';
436+
return 11;
446437
}
447438

448-
return c - 'a';
439+
// Returns -97 to 158, only 0 ('a') to 25 ('z') and 26 ('{'), 27 ('|'), 28 ('}') and 29 ('~') are valid
440+
return qint8(c - 'a');
449441
}

src/formats/Lgp_p.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class FF7TKFORMATS_EXPORT LgpToc
135135
LgpToc &operator=(const LgpToc &other);
136136
private:
137137
LgpHeaderEntry *entry(const QString &filePath, quint16 id) const;
138-
static qint32 lookupValue(const QString &filePath);
139-
static quint8 lookupValue(const QChar &qc);
138+
static qint16 lookupValue(const QString &filePath);
139+
static qint8 lookupValue(const QChar &qc);
140140
QMultiHash<quint16, LgpHeaderEntry *> _header;
141141
};

src/utils/QLockedFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ bool QLockedFile::open(OpenMode mode)
6363
handle = CreateFileA(QDir::toNativeSeparators(fileName()).toLatin1().data(),
6464
GENERIC_READ,
6565
FILE_SHARE_READ,
66-
NULL,
66+
nullptr,
6767
OPEN_EXISTING,
6868
FILE_ATTRIBUTE_NORMAL,
69-
NULL);
69+
nullptr);
7070
if (handle == INVALID_HANDLE_VALUE) {
7171
qWarning() << "QLockedFile::open error lock";
7272
return false;

0 commit comments

Comments
 (0)