Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions DefinedStructs/ConstDataHolder.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "ConstDataHolder.hpp"
#include "DescriptorStruct.hpp"


DataHolder* DataHolder::holder = nullptr;

Expand Down Expand Up @@ -26,6 +28,7 @@ DataHolder::DataHolder()
TRANSFER_LEFTOVER_DATA = Qt::UserRole + 1;
TRANSFER_OPTIONAL_HEADER = Qt::UserRole + 2;
USBPCAP_HEADER_DATA = Qt::UserRole + 3;
DescriptorPath = std::filesystem::current_path().string() + "\\Descriptors\\";
}

/// <summary>
Expand Down Expand Up @@ -53,6 +56,28 @@ void DataHolder::FillDataColorsMap()
DataColors.insert(std::pair<HeaderDataType, DataTypeColor>(UNKNOWN_TRANSFER, { 0,0,255,255 }));
}

/// <summary>
/// Tries to load new descriptor from file if description for given descriptor exists
/// </summary>
/// <param name="descType">Type of descriptor we want to get</param>
/// <returns>Pointer to loaded descriptor if exists, else nullptr</returns>
DescriptorStruct* DataHolder::TryLoadNewDescriptor(BYTE descType)
{
std::stringstream stream(DescriptorPath, std::ios_base::app | std::ios_base::out);

stream << "Desc";
stream << std::setw(2) << std::setfill('0') << (int)descType;
stream << ".txt";

if (std::filesystem::exists(stream.str().c_str()))
{
descriptors.emplace_back(std::make_unique<DescriptorStruct>(stream.str().c_str(), descType));
return descriptors[descriptors.size() - 1].get();
}

return nullptr;
}

/// <summary>
/// Get string representation for USBPcap transfer.
/// </summary>
Expand Down
15 changes: 14 additions & 1 deletion DefinedStructs/ConstDataHolder.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#ifndef CONSTDATAHOLDER_HPP
#define CONSTDATAHOLDER_HPP

#include "PacketExternStructs.hpp"
#include <string>
#include <map>
#include <sstream>
#include <iomanip>
#include <filesystem>
#include "PacketExternStructs.hpp"

class DescriptorStruct;

/// <summary>
/// Class used for holding global variables and for converting data constants to string.
Expand All @@ -24,6 +29,8 @@ class DataHolder
std::string GetUsage(const BYTE globalUsage, const BYTE value);
std::string GetGenericDesktopUsage(const BYTE value);

DescriptorStruct* TryLoadNewDescriptor(BYTE descType);

/// <summary>
/// Delete copy constructor due to Singleton pattern
/// </summary>
Expand Down Expand Up @@ -54,6 +61,10 @@ class DataHolder
/// Map that associate HeaderDataType to its color highlightion
/// </summary>
std::map<HeaderDataType, DataTypeColor> DataColors;
/// <summary>
/// Vector holding all yet known descriptor structs
/// </summary>
std::vector<std::unique_ptr<DescriptorStruct>> descriptors;
private:
DataHolder();
void FillDataColorsMap();
Expand All @@ -62,6 +73,8 @@ class DataHolder
/// Instance of this class.
/// </summary>
static DataHolder* holder;

std::string DescriptorPath;
};

#endif // !CONSTDATAHOLDER_HPP
98 changes: 98 additions & 0 deletions DefinedStructs/DescriptorStruct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "DescriptorStruct.hpp"
#include <sstream>


/// <summary>
/// Fills up fields for this concrete descriptor struct
/// </summary>
void DescriptorStruct::FillUpFields()
{
std::ifstream input;
input.open(filename);
std::string line;
//read first line to get descriptor type and name
if (input.good())
{
if (std::getline(input, line))
{
std::istringstream ss(line);
std::string bDescType;
ss >> bDescType;
if (std::stoi(bDescType) != descriptorType)
{
return;
}
std::string descName = ss.str();
fields.emplace_back(std::make_unique<DescriptorField<std::string>>(descName));
}
}
while (input.good())
{
if (std::getline(input, line))
{
//last added field has more detailed structure (on bit level of detail)
//with given range and values (representing more fields) so fill them up
if (line == "<")
{
fields[fields.size() - 1]->FillUpField(input);
}
else
{
std::istringstream ss(line);
std::string firstWord;
ss >> firstWord;
if (!std::isdigit(firstWord[0]))
{
//means input will be read by the end and has no fixed length (e.g. wstring in String Descriptor)
if (firstWord == "..")
{
std::string type;
ss >> type;
if (type == "wstring")
{
fields.emplace_back(std::make_unique<DescriptorField<std::wstring>>(ss.str()));
}
}
}
else
{
//according to length determine exact type
//currently supporting 2 most common - BYTE and USHORT
switch (std::stoi(firstWord))
{
case 1:
{
fields.emplace_back(std::make_unique<DescriptorField<BYTE>>(ss.str()));
break;
}
case 2:
{
fields.emplace_back(std::make_unique<DescriptorField<USHORT>>(ss.str()));
break;
}
default:
break;
}
}
}
}
}
}

/// <summary>
/// Interpret data according to concrete descriptor
/// </summary>
/// <param name="rootItem">Root tree item of tree view</param>
/// <param name="data">Data to be interpreted</param>
/// <param name="additionalDataModel">Pointer to AdditionalDataModel</param>
void DescriptorStruct::InterpretData(TreeItem* rootItem, const QByteArray& data, AdditionalDataModel* additionalDataModel)
{
const char* packet = data.constData();
std::size_t dataLeft = data.size();
for (int i = 0; i < fields.size(); i++)
{
std::size_t value = fields[i]->InterpretField(rootItem, (const unsigned char*)packet, dataLeft, additionalDataModel);
packet += value;
dataLeft -= value;
}
}
Loading