Skip to content

Commit

Permalink
fdt-v2: adapt some changes in qt genenerator
Browse files Browse the repository at this point in the history
Signed-off-by: Bartłomiej Burdukiewicz <[email protected]>
  • Loading branch information
dev-0x7C6 committed Jul 2, 2024
1 parent 679a67d commit 0fe0da8
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 59 deletions.
18 changes: 14 additions & 4 deletions src/fdt/fdt-generator-qt.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "fdt-generator-qt.hpp"
#include "fdt/fdt-parser-v2.hpp"
#include "fdt/fdt-property-types.hpp"
#include <string_view>

qt_tree_fdt_generator::qt_tree_fdt_generator(tree_info &reference, tree_widget *target, string &&name, string &&id) {
m_root = [&]() {
Expand All @@ -18,7 +21,9 @@ qt_tree_fdt_generator::qt_tree_fdt_generator(tree_info &reference, tree_widget *
m_root->setSelected(true);
}

void qt_tree_fdt_generator::begin_node(const QString &name) noexcept {
void qt_tree_fdt_generator::begin_node(std::string_view vname) noexcept {
const auto name = QString::fromUtf8(vname.data(), vname.size());

auto child = [&]() {
if (m_tree_stack.empty())
return m_root;
Expand Down Expand Up @@ -50,11 +55,16 @@ void qt_tree_fdt_generator::end_node() noexcept {
m_tree_stack.pop();
}

void qt_tree_fdt_generator::insert_property(const fdt_property &property) noexcept {
void qt_tree_fdt_generator::insert_property(const fdt::tokenizer::types::property &prop) noexcept {
auto item = new QTreeWidgetItem(m_tree_stack.top());

item->setText(0, property.name);
fdt::qt_wrappers::property property{
.name = QString::fromUtf8(prop.name.data(), prop.name.size()),
.data = QByteArray::fromRawData(prop.data.data(), prop.data.size()),
};

item->setText(0, QString::fromUtf8(prop.name.data(), prop.name.size()));
item->setIcon(0, QIcon::fromTheme("flag-green"));
item->setData(0, QT_ROLE_NODETYPE, QVariant::fromValue(NodeType::Property));
item->setData(0, QT_ROLE_PROPERTY, QVariant::fromValue(property));
item->setData(0, QT_ROLE_PROPERTY, QVariant::fromValue(std::move(property)));
}
8 changes: 5 additions & 3 deletions src/fdt/fdt-generator-qt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include <QMetaType>
#include <QTreeWidgetItem>
#include <QHash>
#include "fdt/fdt-parser-v2.hpp"
#include <stack>
#include <string_view>

Q_DECLARE_METATYPE(fdt_property)
Q_DECLARE_METATYPE(fdt::qt_wrappers::property)

constexpr auto QT_ROLE_PROPERTY = Qt::UserRole;
constexpr auto QT_ROLE_FILEPATH = Qt::UserRole + 1;
Expand Down Expand Up @@ -39,9 +41,9 @@ using tree_map = hash_map<string, tree_info>;
struct qt_tree_fdt_generator : public iface_fdt_generator {
qt_tree_fdt_generator(tree_info &reference, tree_widget *target, string &&name, string &&id);

void begin_node(const QString &name) noexcept final;
void begin_node(std::string_view) noexcept final;
void end_node() noexcept final;
void insert_property(const fdt_property &property) noexcept final;
void insert_property(const fdt::tokenizer::types::property &) noexcept final;

auto root() { return m_root; }

Expand Down
13 changes: 3 additions & 10 deletions src/fdt/fdt-generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
#include <QByteArray>
#include <QString>

struct fdt_property {
QString name;
std::string_view data;

auto clear() noexcept {
name.clear();
}
};
#include "fdt/fdt-parser-v2.hpp"

struct iface_fdt_generator {
virtual void begin_node(const QString &name) noexcept = 0;
virtual void begin_node(std::string_view) noexcept = 0;
virtual void end_node() noexcept = 0;
virtual void insert_property(const fdt_property &property) noexcept = 0;
virtual void insert_property(const fdt::tokenizer::types::property &) noexcept = 0;
};
13 changes: 10 additions & 3 deletions src/fdt/fdt-header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace fdt {

constexpr auto header_magic_value = 0xD00DFEED;
constexpr auto header_support_above = 16;

struct header {
u32 magic;
u32 totalsize;
Expand All @@ -25,6 +22,16 @@ struct property {
u32 nameoff;
};

constexpr auto is_magic_invalid(const header &v) -> bool {
constexpr auto header_magic_value = 0xD00DFEED;
return v.magic != header_magic_value;
}

constexpr auto is_version_unsupported(const header &v) -> bool {
constexpr auto header_support_above = 16;
return v.version <= header_support_above;
}

static_assert(sizeof(header) == 40);
static_assert(sizeof(property) == 8);
}; // namespace fdt
1 change: 1 addition & 0 deletions src/fdt/fdt-parser-v2.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
Expand Down
12 changes: 6 additions & 6 deletions src/fdt/fdt-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ auto foreach_token_type(std::variant<Ts...>, const u32 token_id, fdt::tokenizer:

auto fdt::tokenizer::generator(std::string_view view, std::string_view root_name) -> std::expected<fdt::tokenizer::token_list, error> {
if (view.size() < sizeof(fdt::header))
return std::unexpected(fdt::error::bad_header);
return std::unexpected(fdt::error::invalid_header);

const auto header = read_data_32be<fdt::header>(view.data());

if (fdt::header_magic_value != header.magic)
return std::unexpected(fdt::error::bad_magic);
if (fdt::is_magic_invalid(header))
return std::unexpected(fdt::error::invalid_magic);

if (view.size() < header.totalsize)
return std::unexpected(fdt::error::data_truncated);

if (fdt::header_support_above > header.version)
return std::unexpected(fdt::error::not_supported_version);
if (fdt::is_version_unsupported(header))
return std::unexpected(fdt::error::unsupported_version);

const auto dt_struct = view.data() + header.off_dt_struct;
const auto dt_strings = view.data() + header.off_dt_strings;
Expand Down Expand Up @@ -113,7 +113,7 @@ auto fdt::tokenizer::generator(std::string_view view, std::string_view root_name
ctx.state.skip = 0;

if (!foreach_token_type(token{}, id, ctx))
return std::unexpected(fdt::error::bad_token);
return std::unexpected(fdt::error::invalid_token);

iter += ctx.state.skip;

Expand Down
11 changes: 4 additions & 7 deletions src/fdt/fdt-parser.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#pragma once

#include "fdt/fdt-header.hpp"
#include "fdt/fdt-parser-v2.hpp"

#include <optional>
#include <string>
#include <string_view>
#include <expected>

namespace fdt {

enum class error {
bad_header,
bad_magic,
invalid_header,
invalid_magic,
invalid_token,
data_truncated,
data_unaligned,
not_supported_version,
bad_token,
unsupported_version,
};

namespace tokenizer {
Expand Down
7 changes: 7 additions & 0 deletions src/fdt/fdt-property-types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ enum class word_size {
custom,
};

namespace fdt::qt_wrappers {
struct property {
QString name;
QByteArray data;
};
} // namespace fdt::qt_wrappers

struct property_info {
property_type type{property_type::guess};
word_size word{word_size::_8};
Expand Down
34 changes: 14 additions & 20 deletions src/fdt/fdt-view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QFileInfo>

#include "fdt/fdt-parser-v2.hpp"
#include "fdt/fdt-property-types.hpp"
#include "qnamespace.h"
#include <string_view>

Expand All @@ -31,15 +32,15 @@ string present_u32be(const QByteArray &data) {
return ret;
}

string present(const fdt_property &property) {
auto &&name = property.name;
auto &&data = property.data;
QString present(const fdt::qt_wrappers::property &p) {
auto &&name = p.name;
auto &&data = p.data;

auto result = [&](string &&value) {
auto result = [&](QString &&value) {
return name + " = <" + value + ">;";
};

auto result_str = [&](string &&value) {
auto result_str = [&](QString &&value) {
return name + " = \"" + value + "\";";
};

Expand All @@ -59,7 +60,7 @@ string present(const fdt_property &property) {
return result(string::number(convert(*reinterpret_cast<const u32 *>(data.data()))));

if (names_regexp.match(name).hasMatch()) {
auto lines = QByteArray::fromRawData(data.data(), data.size()).split(0);
auto lines = data.split(0);
lines.removeLast();

string ret;
Expand All @@ -74,9 +75,9 @@ string present(const fdt_property &property) {
}

if (std::count_if(data.begin(), data.end(), [](auto &&value) { return value == 0x00; }) == 1 &&
data.at(data.size() - 1) == 0x00) return result_str(QString{property.data.data()});
data.back() == 0x00) return result_str(data.data());

return result(present_u32be(QByteArray::fromRawData(data.data(), data.size())));
return result(present_u32be(data));
}
} // namespace

Expand Down Expand Up @@ -112,16 +113,9 @@ bool fdt::viewer::load(QByteArray &&data, string &&name, string &&id) {

for (auto &&token : tokens.value())
std::visit(overloaded{
[&](const types::node_begin &arg) { generator.begin_node(QString::fromUtf8(arg.name.data(), arg.name.size())); },
[&](const types::node_end &arg) { generator.end_node(); },
[&](const types::property &arg) {
auto property = fdt_property{
.name = QString::fromUtf8(arg.name.data(), arg.name.size()),
.data = arg.data,
};

generator.insert_property(property);
},
[&](const types::node_begin &arg) { generator.begin_node(arg.name); },
[&](const types::node_end &) { generator.end_node(); },
[&](const types::property &arg) { generator.insert_property(arg); },
[&](const types::nop &) {},
[&](const types::end &) {},
},
Expand Down Expand Up @@ -159,7 +153,7 @@ bool fdt::fdt_content_filter(QTreeWidgetItem *node, const std::function<bool(con
if (isFound)
break;

const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt_property>();
const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt::qt_wrappers::property>();
isFound |= match(property.name) || match(present(property));
}

Expand Down Expand Up @@ -197,7 +191,7 @@ bool fdt::fdt_view_dts(QTreeWidgetItem *item, string &ret, int depth) {
ret += depth_str + item->data(0, Qt::DisplayRole).toString() + " {\n";

for (auto item : properties) {
const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt_property>();
const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt::qt_wrappers::property>();
ret += depth_str + " " + present(property) + "\n";
}

Expand Down
15 changes: 9 additions & 6 deletions src/main-window.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "main-window.hpp"
#include "fdt/fdt-parser-v2.hpp"
#include "fdt/fdt-property-types.hpp"
#include "ui_main-window.h"

#include <QAction>
Expand Down Expand Up @@ -157,7 +159,7 @@ void MainWindow::update_fdt_path(QTreeWidgetItem *item) {
m_ui->path->setText("fdt://" + path);
}

constexpr auto VIEW_TEXT_CACHE_SIZE = 1024 * 1024;
constexpr auto VIEW_TEXT_CACHE_SIZE = 1024 * 1024 * 32;

void MainWindow::update_view() {
m_ui->splitter->setEnabled(m_ui->treeWidget->topLevelItemCount());
Expand All @@ -178,8 +180,8 @@ void MainWindow::update_view() {
m_ui->preview->setCurrentWidget(NodeType::Node == type ? m_ui->text_view_page : m_ui->property_view_page);

if (NodeType::Property == type) {
const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt_property>();
m_hexview->setDocument(QHexDocument::fromMemory<QMemoryBuffer>(QByteArray::fromRawData(property.data.data(), property.data.size())));
const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt::qt_wrappers::property>();
m_hexview->setDocument(QHexDocument::fromMemory<QMemoryBuffer>(property.data));
}

m_ui->text_view->clear();
Expand All @@ -200,8 +202,9 @@ void MainWindow::property_export() {
const auto type = item->data(0, QT_ROLE_NODETYPE).value<NodeType>();

if (NodeType::Property == type) {
const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt_property>();
m_hexview->setDocument(QHexDocument::fromMemory<QMemoryBuffer>(QByteArray::fromRawData(property.data.data(), property.data.size())));
fdt::export_property_file_dialog(this, QByteArray::fromRawData(property.data.data(), property.data.size()), property.name);
const auto property = item->data(0, QT_ROLE_PROPERTY).value<fdt::qt_wrappers::property>();

m_hexview->setDocument(QHexDocument::fromMemory<QMemoryBuffer>(property.data));
fdt::export_property_file_dialog(this, property.data, property.name);
}
}

0 comments on commit 0fe0da8

Please sign in to comment.