2015-04-02 16:07:32 +08:00
|
|
|
/* fssops.cpp
|
|
|
|
|
|
|
|
Copyright (c) 2015, Nikolaj Schlej. All rights reserved.
|
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ffsops.h"
|
|
|
|
|
|
|
|
STATUS FfsOperations::extract(const QModelIndex & index, QString & name, QByteArray & extracted, const UINT8 mode)
|
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if (!index.isValid())
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
// Get data from parsing data
|
2015-07-07 21:57:41 +08:00
|
|
|
PARSING_DATA pdata = parsingDataFromQModelIndex(index);
|
2015-04-02 16:07:32 +08:00
|
|
|
|
|
|
|
// Construct a name for extracted data
|
|
|
|
QString itemName = model->name(index);
|
|
|
|
QString itemText = model->text(index);
|
2016-04-17 07:25:44 +08:00
|
|
|
|
|
|
|
// Default name
|
|
|
|
name = itemName.replace(' ', '_').replace('/', '_').replace('-', '_');
|
|
|
|
|
2015-04-02 16:07:32 +08:00
|
|
|
switch (model->type(index)) {
|
2016-04-17 07:25:44 +08:00
|
|
|
case Types::Volume: if (pdata.volume.hasExtendedHeader) name = guidToQString(pdata.volume.extendedHeaderGuid).replace('-', '_'); break;
|
|
|
|
case Types::NvarEntry:
|
|
|
|
case Types::VssEntry:
|
|
|
|
case Types::FsysEntry:
|
|
|
|
case Types::EvsaEntry:
|
|
|
|
case Types::FlashMapEntry:
|
|
|
|
case Types::File: name = itemText.isEmpty() ? itemName : itemText.replace(' ', '_').replace('-', '_'); break;
|
2015-04-02 16:07:32 +08:00
|
|
|
case Types::Section: {
|
|
|
|
// Get parent file name
|
|
|
|
QModelIndex fileIndex = model->findParentOfType(index, Types::File);
|
|
|
|
QString fileText = model->text(fileIndex);
|
2016-04-13 07:35:18 +08:00
|
|
|
name = fileText.isEmpty() ? model->name(fileIndex) : fileText.replace(' ', '_').replace('-', '_');
|
2015-04-02 16:07:32 +08:00
|
|
|
// Append section subtype name
|
|
|
|
name += QChar('_') + itemName.replace(' ', '_');
|
2016-04-17 07:25:44 +08:00
|
|
|
} break;
|
2015-04-02 16:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get extracted data
|
|
|
|
if (mode == EXTRACT_MODE_AS_IS) {
|
|
|
|
// Extract as is, with header body and tail
|
|
|
|
extracted.clear();
|
|
|
|
extracted.append(model->header(index));
|
|
|
|
extracted.append(model->body(index));
|
2016-04-21 04:41:18 +08:00
|
|
|
extracted.append(model->tail(index));
|
2015-04-02 16:07:32 +08:00
|
|
|
}
|
|
|
|
else if (mode == EXTRACT_MODE_BODY) {
|
2016-03-01 15:20:44 +08:00
|
|
|
name += QObject::tr("_body");
|
2015-04-02 16:07:32 +08:00
|
|
|
// Extract without header and tail
|
|
|
|
extracted.clear();
|
|
|
|
extracted.append(model->body(index));
|
|
|
|
}
|
2015-09-01 03:34:42 +08:00
|
|
|
else if (mode == EXTRACT_MODE_BODY_UNCOMPRESSED) {
|
2016-03-01 15:20:44 +08:00
|
|
|
name += QObject::tr("_body_unc");
|
2015-09-01 03:34:42 +08:00
|
|
|
// Extract without header and tail, uncompressed
|
|
|
|
extracted.clear();
|
|
|
|
// There is no need to redo decompression, we can use child items
|
|
|
|
for (int i = 0; i < model->rowCount(index); i++) {
|
|
|
|
QModelIndex childIndex = index.child(i, 0);
|
2016-04-21 04:41:18 +08:00
|
|
|
// Ensure 4-byte alignment of current section
|
|
|
|
extracted.append(QByteArray('\x00', ALIGN4((UINT32)extracted.size()) - (UINT32)extracted.size()));
|
|
|
|
// Add current section header, body and tail
|
2015-09-01 03:34:42 +08:00
|
|
|
extracted.append(model->header(childIndex));
|
|
|
|
extracted.append(model->body(childIndex));
|
2016-04-21 04:41:18 +08:00
|
|
|
extracted.append(model->tail(childIndex));
|
2015-09-01 03:34:42 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-02 16:07:32 +08:00
|
|
|
else
|
|
|
|
return ERR_UNKNOWN_EXTRACT_MODE;
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:20:44 +08:00
|
|
|
STATUS FfsOperations::replace(const QModelIndex & index, const QString & data, const UINT8 mode)
|
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if (!index.isValid())
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
// Get data from parsing data
|
|
|
|
//PARSING_DATA pdata = parsingDataFromQModelIndex(index);
|
|
|
|
|
|
|
|
if (mode == REPLACE_MODE_AS_IS) {
|
|
|
|
return ERR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
else if (mode == REPLACE_MODE_BODY) {
|
|
|
|
return ERR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ERR_UNKNOWN_REPLACE_MODE;
|
|
|
|
|
|
|
|
return ERR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATUS FfsOperations::remove(const QModelIndex & index)
|
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if (!index.isValid())
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
// Set remove action
|
|
|
|
model->setAction(index, Actions::Remove);
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATUS FfsOperations::rebuild(const QModelIndex & index)
|
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if (!index.isValid())
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
// On insert action, set insert action for children
|
|
|
|
//if (action == Actions::Insert)
|
|
|
|
// for (int i = 0; i < item->childCount(); i++)
|
|
|
|
// setAction(index.child(i, 0), Actions::Insert);
|
|
|
|
|
|
|
|
// Set rebuild action
|
|
|
|
model->setAction(index, Actions::Rebuild);
|
|
|
|
|
|
|
|
// Rebuild parent, if it has no action now
|
|
|
|
QModelIndex parent = index.parent();
|
|
|
|
if (parent.isValid() && model->type(parent) != Types::Root
|
|
|
|
&& model->action(parent) == Actions::NoAction)
|
|
|
|
rebuild(parent);
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|