From 55b8c3ee48b690e0b801351ef0819b08b038b9d6 Mon Sep 17 00:00:00 2001 From: masoud bh Date: Thu, 24 Sep 2015 02:39:26 +0330 Subject: change pe_bliss parent directory from /drivers to /tools --- tools/pe_bliss/utils.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tools/pe_bliss/utils.cpp (limited to 'tools/pe_bliss/utils.cpp') diff --git a/tools/pe_bliss/utils.cpp b/tools/pe_bliss/utils.cpp new file mode 100644 index 0000000000..69f013098f --- /dev/null +++ b/tools/pe_bliss/utils.cpp @@ -0,0 +1,86 @@ +#include +#include "utils.h" +#include "pe_exception.h" + +#ifndef PE_BLISS_WINDOWS +#include +#endif + +namespace pe_bliss +{ +const double pe_utils::log_2 = 1.44269504088896340736; //instead of using M_LOG2E + +//Returns stream size +std::streamoff pe_utils::get_file_size(std::istream& file) +{ + //Get old istream offset + std::streamoff old_offset = file.tellg(); + file.seekg(0, std::ios::end); + std::streamoff filesize = file.tellg(); + //Set old istream offset + file.seekg(old_offset); + return filesize; +} + +#ifndef PE_BLISS_WINDOWS +const u16string pe_utils::to_ucs2(const std::wstring& str) +{ + u16string ret; + if(str.empty()) + return ret; + + ret.resize(str.length()); + + iconv_t conv = iconv_open("UCS-2", "WCHAR_T"); + if(conv == reinterpret_cast(-1)) + throw pe_exception("Error opening iconv", pe_exception::encoding_convertion_error); + + size_t inbytesleft = str.length() * sizeof(wchar_t); + size_t outbytesleft = ret.length() * sizeof(unicode16_t); + const wchar_t* in_pos = str.c_str(); + unicode16_t* out_pos = &ret[0]; + + size_t result = iconv(conv, const_cast(reinterpret_cast(&in_pos)), &inbytesleft, reinterpret_cast(&out_pos), &outbytesleft); + iconv_close(conv); + + if(result == static_cast(-1)) + throw pe_exception("Iconv error", pe_exception::encoding_convertion_error); + + return ret; +} + +const std::wstring pe_utils::from_ucs2(const u16string& str) +{ + std::wstring ret; + if(str.empty()) + return ret; + + ret.resize(str.length()); + + iconv_t conv = iconv_open("WCHAR_T", "UCS-2"); + if(conv == reinterpret_cast(-1)) + throw pe_exception("Error opening iconv", pe_exception::encoding_convertion_error); + + size_t inbytesleft = str.length() * sizeof(unicode16_t); + size_t outbytesleft = ret.length() * sizeof(wchar_t); + const unicode16_t* in_pos = str.c_str(); + wchar_t* out_pos = &ret[0]; + + size_t result = iconv(conv, const_cast(reinterpret_cast(&in_pos)), &inbytesleft, reinterpret_cast(&out_pos), &outbytesleft); + iconv_close(conv); + + if(result == static_cast(-1)) + throw pe_exception("Iconv error", pe_exception::encoding_convertion_error); + + return ret; +} +#endif + +bool operator==(const pe_win::guid& guid1, const pe_win::guid& guid2) +{ + return guid1.Data1 == guid2.Data1 + && guid1.Data2 == guid2.Data2 + && guid1.Data3 == guid2.Data3 + && !memcmp(guid1.Data4, guid2.Data4, sizeof(guid1.Data4)); +} +} -- cgit v1.2.3