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:
Srivats P 2016-05-17 20:47:10 +05:30
parent 97ad497480
commit ba754c1043
4 changed files with 63 additions and 10 deletions

View File

@ -276,8 +276,10 @@ bool MainWindow::openSession(QString fileName, QString &error)
OstProto::SessionContent session;
SessionFileFormat *fmt = SessionFileFormat::fileFormatFromFile(fileName);
if (fmt == NULL)
if (fmt == NULL) {
error = tr("Unknown session file format");
goto _fail;
}
if ((optDialog = fmt->openOptionsDialog()))
{

View File

@ -551,8 +551,10 @@ bool Port::openStreams(QString fileName, bool append, QString &error)
OstProto::StreamConfigList streams;
AbstractFileFormat *fmt = AbstractFileFormat::fileFormatFromFile(fileName);
if (fmt == NULL)
if (fmt == NULL) {
error = tr("Unknown streams file format");
goto _fail;
}
if ((optDialog = fmt->openOptionsDialog()))
{

View File

@ -140,11 +140,9 @@ bool NativeFileFormat::open(
goto _cksum_verify_fail;
// 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(
(void*)(buf.constData() + kFileMetaDataOffset),
size - kFileMetaDataOffset))
fileMetaSize((quint8*)buf.constData(), size)))
{
goto _metadata_parse_fail;
}
@ -420,15 +418,21 @@ bool NativeFileFormat::isNativeFileFormat(
if (!file.open(QIODevice::ReadOnly))
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),
kFileMagicSize))
goto _close_exit;
if (magic.value() == kFileMagicValue)
ret = true;
// TODO: check fileType
if (magic.value() == kFileMagicValue) {
OstProto::FileMetaData meta;
if (!meta.ParseFromArray(
(void*)(buf.constData() + kFileMetaDataOffset),
fileMetaSize((quint8*)buf.constData(), buf.size())))
goto _close_exit;
if (meta.file_type() == fileType)
ret = true;
}
_close_exit:
file.close();
@ -451,6 +455,50 @@ void NativeFileFormat::initFileMetaData(OstProto::FileMetaData &metaData)
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"
/*! Fixup content to what is expected in the native version */
void NativeFileFormat::postParseFixup(OstProto::FileMetaData metaData,

View File

@ -57,6 +57,7 @@ public:
private:
void initFileMetaData(OstProto::FileMetaData &metaData);
int fileMetaSize(const quint8* file, int size);
static const int kFileMagicSize = 12;
static const int kFileChecksumSize = 5;