1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 21:51:17 +01:00
GPXSee/src/map/tar.cpp

135 lines
3.0 KiB
C++
Raw Normal View History

2017-03-21 01:15:29 +01:00
#include <cctype>
#include <QFile>
#include <QFileInfo>
2017-03-21 01:15:29 +01:00
#include "tar.h"
#define BLOCKSIZE 512
#define BLOCKCOUNT(size) \
((size)/BLOCKSIZE + ((size) % BLOCKSIZE > 0 ? 1 : 0))
struct TARHeader
2017-03-21 01:15:29 +01:00
{
char name[100]; /* 0 */
char mode[8]; /* 100 */
char uid[8]; /* 108 */
char gid[8]; /* 116 */
char size[12]; /* 124 */
char mtime[12]; /* 136 */
char chksum[8]; /* 148 */
char typeflag; /* 156 */
char linkname[100]; /* 157 */
char magic[6]; /* 257 */
char version[2]; /* 263 */
char uname[32]; /* 265 */
char gname[32]; /* 297 */
char devmajor[8]; /* 329 */
char devminor[8]; /* 337 */
char prefix[155]; /* 345 */
2017-03-26 15:32:55 +02:00
/* 500 */
2017-03-21 01:15:29 +01:00
};
static quint64 number(const char* data, size_t size, int base = 8)
2017-03-21 01:15:29 +01:00
{
const char *sp;
quint64 val = 0;
for (sp = data; sp < data + size; sp++)
if (isdigit(*sp))
break;
for (; sp < data + size && isdigit(*sp); sp++)
val = val * base + *sp - '0';
2017-03-21 01:15:29 +01:00
return val;
}
2018-03-08 02:24:10 +01:00
bool Tar::open()
2017-03-21 01:15:29 +01:00
{
if (!_file.open(QIODevice::ReadOnly))
return false;
if (!_index.isEmpty())
return true;
2018-03-08 02:24:10 +01:00
QFileInfo fi(_file.fileName());
QString tmiPath = fi.path() + "/" + fi.completeBaseName() + ".tmi";
if (loadTmi(tmiPath))
return true;
else
return loadTar();
}
bool Tar::loadTar()
{
char buffer[BLOCKSIZE];
TARHeader *hdr = (TARHeader*)&buffer;
quint64 size;
qint64 ret;
2017-03-26 18:08:37 +02:00
while ((ret = _file.read(buffer, BLOCKSIZE)) > 0) {
2017-04-14 22:39:33 +02:00
if (ret < BLOCKSIZE) {
_file.close();
2017-06-25 12:34:48 +02:00
_index.clear();
2017-03-26 18:08:37 +02:00
return false;
2017-04-14 22:39:33 +02:00
}
2017-03-26 18:08:37 +02:00
size = number(hdr->size, sizeof(hdr->size));
_index.insert(hdr->name, _file.pos() / BLOCKSIZE - 1);
2017-04-14 22:39:33 +02:00
if (!_file.seek(_file.pos() + BLOCKCOUNT(size) * BLOCKSIZE)) {
_file.close();
2017-06-25 12:34:48 +02:00
_index.clear();
2017-03-26 18:08:37 +02:00
return false;
2017-04-14 22:39:33 +02:00
}
2017-03-21 01:15:29 +01:00
}
return true;
}
bool Tar::loadTmi(const QString &path)
{
quint64 block;
int ln = 1;
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
return false;
while (!file.atEnd()) {
QByteArray line = file.readLine(4096);
int pos = line.indexOf(':');
if (line.size() < 10 || pos < 7 || !line.startsWith("block")) {
2018-09-24 23:07:11 +02:00
qWarning("%s:%d: syntax error", qPrintable(path), ln);
_index.clear();
return false;
}
block = number(line.constData() + 6, line.size() - 6, 10);
QString file(line.mid(pos + 1).trimmed());
_index.insert(file, block);
ln++;
}
return true;
}
2017-03-21 01:15:29 +01:00
QByteArray Tar::file(const QString &name)
{
char buffer[BLOCKSIZE];
TARHeader *hdr = (TARHeader*)&buffer;
quint64 size;
2018-03-29 00:29:08 +02:00
QMap<QString, quint64>::const_iterator it(_index.find(name));
if (it == _index.constEnd())
2017-03-26 15:32:55 +02:00
return QByteArray();
2017-03-21 01:15:29 +01:00
2017-04-14 22:39:33 +02:00
Q_ASSERT(_file.isOpen());
if (_file.seek(it.value() * BLOCKSIZE)) {
if (_file.read(buffer, BLOCKSIZE) < BLOCKSIZE)
return QByteArray();
size = number(hdr->size, sizeof(hdr->size));
return _file.read(size);
} else
2017-03-21 01:15:29 +01:00
return QByteArray();
}