Open Session - check fileType for native file formats while identifying the file format from filename; also find size of metadata and use it when parsing just the metadata instead of the whole file
This commit is contained in:
parent
97ad497480
commit
ba754c1043
@ -276,8 +276,10 @@ bool MainWindow::openSession(QString fileName, QString &error)
|
|||||||
OstProto::SessionContent session;
|
OstProto::SessionContent session;
|
||||||
SessionFileFormat *fmt = SessionFileFormat::fileFormatFromFile(fileName);
|
SessionFileFormat *fmt = SessionFileFormat::fileFormatFromFile(fileName);
|
||||||
|
|
||||||
if (fmt == NULL)
|
if (fmt == NULL) {
|
||||||
|
error = tr("Unknown session file format");
|
||||||
goto _fail;
|
goto _fail;
|
||||||
|
}
|
||||||
|
|
||||||
if ((optDialog = fmt->openOptionsDialog()))
|
if ((optDialog = fmt->openOptionsDialog()))
|
||||||
{
|
{
|
||||||
|
@ -551,8 +551,10 @@ bool Port::openStreams(QString fileName, bool append, QString &error)
|
|||||||
OstProto::StreamConfigList streams;
|
OstProto::StreamConfigList streams;
|
||||||
AbstractFileFormat *fmt = AbstractFileFormat::fileFormatFromFile(fileName);
|
AbstractFileFormat *fmt = AbstractFileFormat::fileFormatFromFile(fileName);
|
||||||
|
|
||||||
if (fmt == NULL)
|
if (fmt == NULL) {
|
||||||
|
error = tr("Unknown streams file format");
|
||||||
goto _fail;
|
goto _fail;
|
||||||
|
}
|
||||||
|
|
||||||
if ((optDialog = fmt->openOptionsDialog()))
|
if ((optDialog = fmt->openOptionsDialog()))
|
||||||
{
|
{
|
||||||
|
@ -140,11 +140,9 @@ bool NativeFileFormat::open(
|
|||||||
goto _cksum_verify_fail;
|
goto _cksum_verify_fail;
|
||||||
|
|
||||||
// Parse the metadata first before we parse the full contents
|
// Parse the metadata first before we parse the full contents
|
||||||
// FIXME: metadata size is not known beforehand, so we end up
|
|
||||||
// parsing till EOF
|
|
||||||
if (!meta.ParseFromArray(
|
if (!meta.ParseFromArray(
|
||||||
(void*)(buf.constData() + kFileMetaDataOffset),
|
(void*)(buf.constData() + kFileMetaDataOffset),
|
||||||
size - kFileMetaDataOffset))
|
fileMetaSize((quint8*)buf.constData(), size)))
|
||||||
{
|
{
|
||||||
goto _metadata_parse_fail;
|
goto _metadata_parse_fail;
|
||||||
}
|
}
|
||||||
@ -420,15 +418,21 @@ bool NativeFileFormat::isNativeFileFormat(
|
|||||||
if (!file.open(QIODevice::ReadOnly))
|
if (!file.open(QIODevice::ReadOnly))
|
||||||
goto _exit;
|
goto _exit;
|
||||||
|
|
||||||
buf = file.peek(kFileMagicOffset + kFileMagicSize);
|
// Assume tag/length for MetaData will fit in 8 bytes
|
||||||
|
buf = file.peek(kFileMagicOffset + kFileMagicSize + 8);
|
||||||
if (!magic.ParseFromArray((void*)(buf.constData() + kFileMagicOffset),
|
if (!magic.ParseFromArray((void*)(buf.constData() + kFileMagicOffset),
|
||||||
kFileMagicSize))
|
kFileMagicSize))
|
||||||
goto _close_exit;
|
goto _close_exit;
|
||||||
|
|
||||||
if (magic.value() == kFileMagicValue)
|
if (magic.value() == kFileMagicValue) {
|
||||||
ret = true;
|
OstProto::FileMetaData meta;
|
||||||
|
if (!meta.ParseFromArray(
|
||||||
// TODO: check fileType
|
(void*)(buf.constData() + kFileMetaDataOffset),
|
||||||
|
fileMetaSize((quint8*)buf.constData(), buf.size())))
|
||||||
|
goto _close_exit;
|
||||||
|
if (meta.file_type() == fileType)
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
|
||||||
_close_exit:
|
_close_exit:
|
||||||
file.close();
|
file.close();
|
||||||
@ -451,6 +455,50 @@ void NativeFileFormat::initFileMetaData(OstProto::FileMetaData &metaData)
|
|||||||
qApp->property("revision").toString().toUtf8().constData());
|
qApp->property("revision").toString().toUtf8().constData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int NativeFileFormat::fileMetaSize(const quint8* file, int size)
|
||||||
|
{
|
||||||
|
int i = kFileMetaDataOffset;
|
||||||
|
uint result, shift;
|
||||||
|
const int kWireTypeLengthDelimited = 2;
|
||||||
|
|
||||||
|
// An embedded Message field is encoded as
|
||||||
|
// <Key> <Length> <Serialized-Value>
|
||||||
|
// See Protobuf Encoding for more details
|
||||||
|
|
||||||
|
// Decode 'Key' varint
|
||||||
|
result = 0;
|
||||||
|
shift = 0;
|
||||||
|
while (i < size) {
|
||||||
|
quint8 byte = file[i++];
|
||||||
|
result |= (byte & 0x7f) << shift;
|
||||||
|
if (!(byte & 0x80)) // MSB == 0?
|
||||||
|
break;
|
||||||
|
shift += 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i >= size)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
Q_ASSERT(result == ((OstProto::File::kMetaDataFieldNumber << 3)
|
||||||
|
| kWireTypeLengthDelimited));
|
||||||
|
|
||||||
|
// Decode 'Length' varint
|
||||||
|
result = 0;
|
||||||
|
shift = 0;
|
||||||
|
while (i < size) {
|
||||||
|
quint8 byte = file[i++];
|
||||||
|
result |= (byte & 0x7f) << shift;
|
||||||
|
if (!(byte & 0x80)) // MSB == 0?
|
||||||
|
break;
|
||||||
|
shift += 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i >= size)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return int(result+(i-kFileMetaDataOffset));
|
||||||
|
}
|
||||||
|
|
||||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||||
/*! Fixup content to what is expected in the native version */
|
/*! Fixup content to what is expected in the native version */
|
||||||
void NativeFileFormat::postParseFixup(OstProto::FileMetaData metaData,
|
void NativeFileFormat::postParseFixup(OstProto::FileMetaData metaData,
|
||||||
|
@ -57,6 +57,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void initFileMetaData(OstProto::FileMetaData &metaData);
|
void initFileMetaData(OstProto::FileMetaData &metaData);
|
||||||
|
int fileMetaSize(const quint8* file, int size);
|
||||||
|
|
||||||
static const int kFileMagicSize = 12;
|
static const int kFileMagicSize = 12;
|
||||||
static const int kFileChecksumSize = 5;
|
static const int kFileChecksumSize = 5;
|
||||||
|
Loading…
Reference in New Issue
Block a user