Skip to content

Commit

Permalink
Cleanup utils.hpp header
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Aug 17, 2024
1 parent ab85865 commit f864b71
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 73 deletions.
21 changes: 0 additions & 21 deletions include/LIEF/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@
*/
#ifndef LIEF_UTILS_HEADER
#define LIEF_UTILS_HEADER
#include <vector>
#include <string>

#include "LIEF/span.hpp"
#include "LIEF/types.hpp"
#include "LIEF/visibility.h"

#include "LIEF/errors.hpp"


namespace LIEF {
inline uint64_t align(uint64_t value, uint64_t align_on) {
if (align_on == 0) {
Expand Down Expand Up @@ -74,32 +70,15 @@ constexpr size_t operator ""_GB(unsigned long long gbs)
return 1024LLU * 1024LLU * 1024LLU * gbs;
}


//! Convert a UTF-16 string to a UTF-8 one
LIEF_API std::string u16tou8(const std::u16string& string, bool remove_null_char = false);

//! Convert a UTF-8 string to a UTF-16 one
LIEF_API result<std::u16string> u8tou16(const std::string& string);

LIEF_API std::string hex_str(uint8_t c);

LIEF_API std::string hex_dump(const std::vector<uint8_t>& data,
const std::string& sep = ":");

LIEF_API std::string hex_dump(span<const uint8_t> data,
const std::string& sep = ":");

//! Check if the given number is a hex-like string
LIEF_API bool is_hex_number(const std::string& nb);

//! Whether this version of LIEF includes extended features
LIEF_API bool is_extended();
}

namespace LIEF {
namespace LEB128 {
std::vector<uint8_t> uencode(uint64_t value);
}
}

#endif
2 changes: 1 addition & 1 deletion src/DEX/Header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
#include "LIEF/DEX/Header.hpp"
#include "LIEF/DEX/hash.hpp"
#include "LIEF/utils.hpp"
#include "internal_utils.hpp"

#include <numeric>
#include <iomanip>
Expand Down
1 change: 1 addition & 0 deletions src/PE/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "logging.hpp"
#include "hash_stream.hpp"
#include "internal_utils.hpp"

#include "LIEF/utils.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/PE/ResourcesParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "LIEF/BinaryStream/BinaryStream.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
#include "LIEF/utils.hpp"
#include "internal_utils.hpp"

#include "LIEF/PE/ResourcesManager.hpp"
#include "LIEF/PE/ResourceData.hpp"
Expand Down
3 changes: 2 additions & 1 deletion src/PE/signature/SpcIndirectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/
#include "LIEF/Visitor.hpp"
#include "LIEF/utils.hpp"
#include "LIEF/PE/signature/SpcIndirectData.hpp"
#include "LIEF/PE/EnumToString.hpp"

#include "internal_utils.hpp"

#include <spdlog/fmt/fmt.h>

namespace LIEF {
Expand Down
2 changes: 1 addition & 1 deletion src/PE/signature/attributes/PKCS9MessageDigest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
#include "LIEF/Visitor.hpp"
#include "LIEF/PE/signature/attributes/PKCS9MessageDigest.hpp"
#include "LIEF/utils.hpp"
#include "internal_utils.hpp"

namespace LIEF {
namespace PE {
Expand Down
17 changes: 17 additions & 0 deletions src/internal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ std::string printable_string(const std::string& str) {
}
}
return out;
}

template<class T>
std::string hex_dump_impl(T data, const std::string& sep) {
std::vector<std::string> hexdigits;
hexdigits.reserve(data.size());
std::transform(data.begin(), data.end(), std::back_inserter(hexdigits),
[] (uint8_t x) { return fmt::format("{:02x}", x); });
return fmt::to_string(fmt::join(hexdigits, sep));
}

std::string hex_dump(const std::vector<uint8_t>& data, const std::string& sep) {
return hex_dump_impl(data, sep);
}

std::string hex_dump(span<const uint8_t> data, const std::string& sep) {
return hex_dump_impl(data, sep);
}

}
14 changes: 14 additions & 0 deletions src/internal_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ auto make_empty_iterator() {
);
}

inline bool is_hex_number(const std::string& str) {
return std::all_of(std::begin(str), std::end(str), ::isxdigit);
}

inline std::string hex_str(uint8_t c) {
return fmt::format("{:02x}", c);
}

std::string hex_dump(const std::vector<uint8_t>& data,
const std::string& sep = ":");

std::string hex_dump(span<const uint8_t> data,
const std::string& sep = ":");

}

#endif
49 changes: 0 additions & 49 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
* limitations under the License.
*/
#include <algorithm>
#include <iomanip>
#include <iterator>
#include <locale>
#include <numeric>
#include <sstream>
#include <string>

#include <spdlog/fmt/fmt.h>
Expand All @@ -31,22 +27,6 @@
#include "LIEF/config.h"

namespace LIEF {
namespace LEB128 {
std::vector<uint8_t> uencode(uint64_t value) {
std::vector<uint8_t> result;
do {
uint8_t b = value & 0x7F;
value >>= 7;
if (value > 0) {
b |= 0x80;
}
result.push_back(b);
} while (value != 0);
return result;
}

}


template <typename octet_iterator>
result<uint32_t> next(octet_iterator& it, octet_iterator end) {
Expand Down Expand Up @@ -106,35 +86,6 @@ result<std::u16string> u8tou16(const std::string& string) {
return name;
}

std::string hex_str(uint8_t c) {
std::stringstream ss;
ss << std::setw(2) << std::setfill('0') << std::hex << static_cast<uint32_t>(c);
return ss.str();
}

template<class T>
std::string hex_dump_impl(T data, const std::string& sep) {
std::vector<std::string> hexdigits;
hexdigits.reserve(data.size());
std::transform(data.begin(), data.end(), std::back_inserter(hexdigits),
[] (uint8_t x) { return fmt::format("{:02x}", x); });
return fmt::to_string(fmt::join(hexdigits, sep));
}

std::string hex_dump(const std::vector<uint8_t>& data, const std::string& sep) {
return hex_dump_impl(data, sep);
}

std::string hex_dump(span<const uint8_t> data, const std::string& sep) {
return hex_dump_impl(data, sep);
}


bool is_hex_number(const std::string& str) {
return std::all_of(std::begin(str), std::end(str), isxdigit);
}


bool is_extended() {
return lief_extended;
}
Expand Down

0 comments on commit f864b71

Please sign in to comment.