Skip to content

Commit

Permalink
json and plain presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pruzko committed Mar 16, 2019
1 parent 774aea9 commit 8f31017
Show file tree
Hide file tree
Showing 17 changed files with 501 additions and 46 deletions.
23 changes: 15 additions & 8 deletions include/retdec/fileformat/types/resource_table/resource_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define RETDEC_FILEFORMAT_TYPES_RESOURCE_TABLE_RESOURCE_TABLE_H

#include <memory>
#include <utility>
#include <vector>

#include "retdec/fileformat/types/resource_table/resource.h"
Expand All @@ -25,14 +26,16 @@ class ResourceTable
{
private:
using resourcesIterator = std::vector<std::unique_ptr<Resource>>::const_iterator;
std::vector<std::unique_ptr<Resource>> table; ///< stored resources
std::vector<ResourceVersion *> resourceVersions; ///< icon groups
std::vector<ResourceIconGroup *> iconGroups; ///< icon groups
std::vector<ResourceIcon *> icons; ///< icons
std::string iconHashCrc32; ///< iconhash CRC32
std::string iconHashMd5; ///< iconhash MD5
std::string iconHashSha256; ///< iconhash SHA256
std::string iconPerceptualAvgHash; ///< icon perceptual hash AvgHash
std::vector<std::unique_ptr<Resource>> table; ///< stored resources
std::vector<ResourceVersion *> resourceVersions; ///< icon groups
std::vector<ResourceIconGroup *> iconGroups; ///< icon groups
std::vector<ResourceIcon *> icons; ///< icons
std::vector<std::pair<std::string, std::string>> languages; ///< supported languages, LCID and code page
std::vector<std::pair<std::string, std::string>> strings; ///< version info strings
std::string iconHashCrc32; ///< iconhash CRC32
std::string iconHashMd5; ///< iconhash MD5
std::string iconHashSha256; ///< iconhash SHA256
std::string iconPerceptualAvgHash; ///< icon perceptual hash AvgHash

std::string computePerceptualAvgHash(const ResourceIcon &icon) const;
bool parseVersionInfo(const std::vector<std::uint8_t> &bytes);
Expand All @@ -47,9 +50,13 @@ class ResourceTable
/// @name Getters
/// @{
std::size_t getNumberOfResources() const;
std::size_t getNumberOfLanguages() const;
std::size_t getNumberOfStrings() const;
std::size_t getSizeInFile() const;
std::size_t getLoadedSize() const;
const Resource* getResource(std::size_t rIndex) const;
const std::pair<std::string, std::string>* getLanguage(std::size_t rIndex) const;
const std::pair<std::string, std::string>* getString(std::size_t rIndex) const;
const Resource* getResourceWithName(const std::string &rName) const;
const Resource* getResourceWithName(std::size_t rId) const;
const Resource* getResourceWithType(const std::string &rType) const;
Expand Down
4 changes: 2 additions & 2 deletions src/fileformat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ set(FILEFORMAT_SOURCES
types/relocation_table/relocation_table.cpp
types/relocation_table/relocation.cpp
types/resource_table/resource.cpp
types/resource_table/resource_icon.cpp
types/resource_table/resource_icon_group.cpp
types/resource_table/resource_table.cpp
types/resource_table/resource_tree.cpp
types/resource_table/resource_version.cpp
types/resource_table/resource_icon.cpp
types/resource_table/resource_icon_group.cpp
types/resource_table/bitmap_image.cpp
types/certificate_table/certificate.cpp
types/certificate_table/certificate_table.cpp
Expand Down
89 changes: 55 additions & 34 deletions src/fileformat/types/resource_table/resource_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct FixedFileInfo
std::uint32_t signature; ///< signature FFI_SIGNATURE
std::uint16_t strucVersionMaj; ///< binary major version number
std::uint16_t strucVersionMin; ///< binary minor version number
std::uint64_t fileVersion; ///< file version number
std::uint32_t fileVersionMaj; ///< file major version number
std::uint32_t fileVersionMin; ///< file minor version number
std::uint64_t productVersion; ///< product version number
std::uint32_t fileFlagsMask; ///< validity mask of fileFalgs member
std::uint32_t fileFlags; ///< file flags
Expand All @@ -47,9 +48,9 @@ struct FixedFileInfo
static std::size_t structSize()
{
return
sizeof(signature) + sizeof(strucVersionMaj) + sizeof(strucVersionMin) + sizeof(fileVersion) +
sizeof(productVersion) + sizeof(fileFlagsMask) + sizeof(fileFlags) + sizeof(fileOS) +
sizeof(fileType) + sizeof(fileSubtype) + sizeof(timestamp);
sizeof(signature) + sizeof(strucVersionMaj) + sizeof(strucVersionMin) + sizeof(fileVersionMaj) +
sizeof(fileVersionMin) + sizeof(productVersion) + sizeof(fileFlagsMask) + sizeof(fileFlags) +
sizeof(fileOS) + sizeof(fileType) + sizeof(fileSubtype) + sizeof(timestamp);
}
};

Expand Down Expand Up @@ -140,6 +141,24 @@ std::size_t ResourceTable::getNumberOfResources() const
return table.size();
}

/**
* Get number of supported languages
* @return Number of supported languages
*/
std::size_t ResourceTable::getNumberOfLanguages() const
{
return languages.size();
}

/**
* Get number of strings
* @return Number of strings
*/
std::size_t ResourceTable::getNumberOfStrings() const
{
return strings.size();
}

/**
* Get total declared size of resources
* @return Total declared size of resources
Expand Down Expand Up @@ -182,6 +201,26 @@ const Resource* ResourceTable::getResource(std::size_t rIndex) const
return (rIndex < getNumberOfResources()) ? table[rIndex].get() : nullptr;
}

/**
* Get selected language
* @param rIndex Index of selected language (indexed from 0)
* @return Pointer to selected language or @c nullptr if language index is invalid
*/
const std::pair<std::string, std::string>* ResourceTable::getLanguage(std::size_t rIndex) const
{
return (rIndex < getNumberOfLanguages()) ? &languages[rIndex] : nullptr;
}

/**
* Get selected string
* @param rIndex Index of selected string (indexed from 0)
* @return Pointer to selected string or @c nullptr if string index is invalid
*/
const std::pair<std::string, std::string>* ResourceTable::getString(std::size_t rIndex) const
{
return (rIndex < getNumberOfStrings()) ? &strings[rIndex] : nullptr;
}

/**
* Get resource by name
* @param rName Name of the resource to get
Expand Down Expand Up @@ -410,14 +449,7 @@ void ResourceTable::parseVersionInfoResources()
{
continue;
}
if (parseVersionInfo(bytes))
{
std::cerr << "SUCCESS\n\n";
}
else
{
std::cerr << "FAILED\n\n";
}
parseVersionInfo(bytes);
}
}

Expand Down Expand Up @@ -459,10 +491,13 @@ bool ResourceTable::parseVersionInfo(const std::vector<std::uint8_t> &bytes)
}

ffi.signature = structContent.read<std::uint32_t>(offset); offset += sizeof(ffi.signature);
ffi.strucVersionMaj = structContent.read<std::uint16_t>(offset); offset += sizeof(ffi.strucVersionMaj);
ffi.strucVersionMin = structContent.read<std::uint16_t>(offset); offset += sizeof(ffi.strucVersionMin);
ffi.fileVersion = structContent.read<std::uint64_t>(offset); offset += sizeof(ffi.fileVersion);
ffi.productVersion = structContent.read<std::uint32_t>(offset); offset += sizeof(ffi.productVersion);
ffi.strucVersionMaj = structContent.read<std::uint16_t>(offset); offset += sizeof(ffi.strucVersionMaj);
std::uint32_t t1 = structContent.read<std::uint32_t>(offset);
ffi.fileVersionMaj = t1 >> 16; offset += sizeof(ffi.fileVersionMaj);
ffi.fileVersionMin = t1 & 0xFFFF; offset += sizeof(ffi.fileVersionMin);
std::uint64_t t2 = structContent.read<std::uint64_t>(offset);
ffi.productVersion = t2 >> 16; offset += sizeof(ffi.productVersion);
ffi.fileFlagsMask = structContent.read<std::uint32_t>(offset); offset += sizeof(ffi.fileFlagsMask);
ffi.fileFlags = structContent.read<std::uint32_t>(offset); offset += sizeof(ffi.fileFlags);
ffi.fileOS = structContent.read<std::uint32_t>(offset); offset += sizeof(ffi.fileOS);
Expand Down Expand Up @@ -587,8 +622,7 @@ bool ResourceTable::parseVarFileInfoChild(const std::vector<std::uint8_t> &bytes
std::uint32_t lang = structContent.read<uint32_t>(offset); offset += sizeof(lang);
std::uint16_t lcid = lang & 0xFFFF;
std::uint16_t codePage = lang >> 16;
std::cerr << ">>> code page:\t" << codePageToStr(codePage) << "\n";
std::cerr << ">>> lcid:\t" << lcidToStr(lcid) << "\n";
languages.emplace_back(std::make_pair(lcidToStr(lcid), codePageToStr(codePage)));
}

offset = retdec::utils::alignUp(offset, sizeof(std::uint32_t));
Expand Down Expand Up @@ -617,22 +651,11 @@ bool ResourceTable::parseStringFileInfoChild(const std::vector<std::uint8_t> &by

std::size_t nRead;
std::string key = retdec::utils::unicodeToAscii(&bytes.data()[offset], bytes.size() - offset, nRead);
std::cerr << "key: " << key << "\n";

if (nRead != STRTAB_KEY_SIZE)
{
return false;
}

std::uint32_t lang;
if (retdec::utils::strToNum(key, lang, std::hex))
{
std::uint16_t lcid = lang >> 16;
std::uint16_t codePage = lang & 0xFFFF;
std::cerr << ">>> code page:\t" << codePageToStr(codePage) << "\n";
std::cerr << ">>> lcid:\t" << lcidToStr(lcid) << "\n";
}

offset += STRTAB_KEY_SIZE;
offset = retdec::utils::alignUp(offset, sizeof(std::uint32_t));

Expand Down Expand Up @@ -682,27 +705,25 @@ bool ResourceTable::parseVarString(const std::vector<std::uint8_t> &bytes, std::

std::size_t nToRead = targetOffset - offset;
std::size_t nRead;
std::string key = retdec::utils::unicodeToAscii(&bytes.data()[offset], nToRead, nRead);
std::string name = retdec::utils::unicodeToAscii(&bytes.data()[offset], nToRead, nRead);
offset += nRead;
offset = retdec::utils::alignUp(offset, sizeof(std::uint32_t));
std::cerr << "stringKey: " << key << "\n";

if (offset > targetOffset)
{
return false;
}

nToRead = targetOffset - offset;
std::string value;
if (nToRead > 0)
{
std::string value = retdec::utils::unicodeToAscii(&bytes.data()[offset], nToRead, nRead);
value = retdec::utils::unicodeToAscii(&bytes.data()[offset], nToRead, nRead);
offset += nRead;

std::cerr << "stringValue: " << value << "\n";

offset = retdec::utils::alignUp(offset, sizeof(std::uint32_t));
}

strings.emplace_back(std::make_pair(name, value));
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/fileinfo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ set(FILEINFO_SOURCES
file_presentation/getters/iterative_getter/iterative_distribution_getter/relocation_tables_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/resource_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/rich_header_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/visual_basic_extern_table_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/section_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/segment_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/strings_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/symbol_tables_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/typeref_table_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/version_info_language_table_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/version_info_string_table_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_distribution_getter/visual_basic_extern_table_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_getter.cpp
file_presentation/getters/iterative_getter/iterative_simple_getter/certificate_table_plain_getter.cpp
file_presentation/getters/iterative_getter/iterative_simple_getter/iterative_simple_getter.cpp
Expand Down
58 changes: 58 additions & 0 deletions src/fileinfo/file_information/file_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,24 @@ std::size_t FileInformation::getNumberOfStoredResources() const
return resourceTable.getNumberOfResources();
}

/**
* Get number of supported version info languages
* @return Number of supported version info languages
*/
std::size_t FileInformation::getNumberOfVersionInfoLanguages() const
{
return resourceTable.getNumberOfLanguages();
}

/**
* Get number of version info strings
* @return Number of version info strings
*/
std::size_t FileInformation::getNumberOfVersionInfoStrings() const
{
return resourceTable.getNumberOfStrings();
}

/**
* Get CRC32 of selected resource
* @param index Index of selected resource (indexed from 0)
Expand Down Expand Up @@ -1461,6 +1479,46 @@ std::string FileInformation::getResourceLanguage(std::size_t index) const
return resourceTable.getResourceLanguage(index);
}

/**
* Get LCID of selected version info language
* @param index Index of selected version info language (indexed from 0)
* @return LCID of selected version info language
*/
std::string FileInformation::getVersionInfoLanguageLcid(std::size_t index) const
{
return resourceTable.getLanguageLcid(index);
}

/**
* Get code page of selected version info language
* @param index Index of selected version info language (indexed from 0)
* @return Code page of selected version info language
*/
std::string FileInformation::getVersionInfoLanguageCodePage(std::size_t index) const
{
return resourceTable.getLanguageCodePage(index);
}

/**
* Get name of selected version info string
* @param index Index of selected version info string (indexed from 0)
* @return Name of selected version info string
*/
std::string FileInformation::getVersionInfoStringName(std::size_t index) const
{
return resourceTable.getStringName(index);
}

/**
* Get value of selected version info string
* @param index Index of selected version info string (indexed from 0)
* @return Value of selected version info string
*/
std::string FileInformation::getVersionInfoStringValue(std::size_t index) const
{
return resourceTable.getStringValue(index);
}

/**
* Get name ID of selected resource
* @param index Index of selected resource (indexed from 0)
Expand Down
6 changes: 6 additions & 0 deletions src/fileinfo/file_information/file_information.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ class FileInformation
/// @name Getters of @a resourceTable
/// @{
std::size_t getNumberOfStoredResources() const;
std::size_t getNumberOfVersionInfoLanguages() const;
std::size_t getNumberOfVersionInfoStrings() const;
std::string getResourceCrc32(std::size_t index) const;
std::string getResourceMd5(std::size_t index) const;
std::string getResourceSha256(std::size_t index) const;
Expand All @@ -244,6 +246,10 @@ class FileInformation
std::string getResourceName(std::size_t index) const;
std::string getResourceType(std::size_t index) const;
std::string getResourceLanguage(std::size_t index) const;
std::string getVersionInfoLanguageLcid(std::size_t index) const;
std::string getVersionInfoLanguageCodePage(std::size_t index) const;
std::string getVersionInfoStringName(std::size_t index) const;
std::string getVersionInfoStringValue(std::size_t index) const;
std::string getResourceNameIdStr(std::size_t index, std::ios_base &(* format)(std::ios_base &)) const;
std::string getResourceTypeIdStr(std::size_t index, std::ios_base &(* format)(std::ios_base &)) const;
std::string getResourceLanguageIdStr(std::size_t index, std::ios_base &(* format)(std::ios_base &)) const;
Expand Down
Loading

0 comments on commit 8f31017

Please sign in to comment.