10#ifndef PSTSDK_UTIL_UTIL_H
11#define PSTSDK_UTIL_UTIL_H
21#include <boost/utility.hpp>
26#if defined(_WIN32) || defined(__MINGW32__)
44class file :
private boost::noncopyable
80 std::wstring m_filename;
167 throw std::runtime_error(
"fopen failed");
172 if (!m_pfile)
return;
185 throw std::out_of_range(
"fseek failed");
191 throw std::out_of_range(
"fread failed");
197inline size_t pstsdk::file::write(
const std::vector<byte>&
buffer,
ulonglong offset)
205 throw std::out_of_range(
"fseek failed");
208 size_t write = fwrite(&buffer[0], 1, buffer.size(), m_pfile);
210 if(write != buffer.size())
211 throw std::out_of_range(
"fwrite failed");
247 return (*(
pbytes + (
bit >> 3)) & (0x80 >> (
bit & 7))) != 0;
250#if defined(_WIN32) || defined(__MINGW32__)
256 if(bytes.size() == 0)
257 return std::wstring();
259 return std::wstring(
reinterpret_cast<const wchar_t *
>(&bytes[0]), bytes.size()/
sizeof(
wchar_t));
265 return std::vector<byte>();
267 const byte *begin =
reinterpret_cast<const byte *
>(&wstr[0]);
268 return std::vector<byte>(begin, begin + wstr.size()*
sizeof(
wchar_t));
276 size_t utf8_sz = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), NULL, 0, NULL, NULL);
278 std::vector<char> utf8_str(utf8_sz);
279 size_t copied = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), &utf8_str.data()[0], utf8_sz, NULL, NULL);
281 if (copied != utf8_sz)
282 throw std::runtime_error(
"Expected to copy " + std::to_string(utf8_sz) +
" bytes but copied " + std::to_string(copied));
284 return { utf8_str.begin(), utf8_str.end() };
298static_assert(
sizeof(wchar_t) == 4,
"expected a 4 byte wchar_t off Windows");
299#define PSTSDK_WCHAR_ENCODING "UTF-32LE"
303 if(
bytes.size() == 0)
304 return std::wstring();
307 if(
bytes.size() % 2 != 0)
308 throw std::runtime_error(
"Cannot interpret odd number of bytes as UTF-16LE");
309 std::wstring
out(
bytes.size() / 2,
L'\0');
313 perror(
"bytes_to_wstring");
314 throw std::runtime_error(
"Unable to convert from UTF-16LE to wstring");
317 const char *
inbuf =
reinterpret_cast<const char *
>(&
bytes[0]);
319 char *
outbuf =
reinterpret_cast<char *
>(&
out[0]);
325 throw std::runtime_error(
"Failed to convert from UTF-16LE to wstring: "
326 + std::string(std::strerror(
err)) +
", "
328 + std::to_string(
bytes.size()) +
" bytes left");
337 if(
bytes.size() == 0)
338 return std::string();
341 if(
bytes.size() % 2 != 0)
342 throw std::runtime_error(
"Cannot interpret odd number of bytes as UTF-16LE");
345 std::string
out(
bytes.size() * 2,
L'\0');
347 iconv_t cd(::iconv_open(
"UTF-8",
"UTF-16LE"));
349 perror(
"bytes_to_string");
350 throw std::runtime_error(
"Unable to convert from UTF-16LE to string");
353 const char *
inbuf =
reinterpret_cast<const char *
>(&
bytes[0]);
355 char *
outbuf =
reinterpret_cast<char *
>(&
out[0]);
361 throw std::runtime_error(
"Failed to convert from UTF-16LE to string");
371 return std::vector<byte>();
374 std::vector<byte>
out(
wstr.size() * 4);
378 perror(
"wstring_to_bytes");
379 throw std::runtime_error(
"Unable to convert from wstring to UTF-16LE");
382 const char *
inbuf =
reinterpret_cast<const char *
>(&
wstr[0]);
384 char *
outbuf =
reinterpret_cast<char *
>(&
out[0]);
389 throw std::runtime_error(
"Failed to convert from wstring to UTF-16LE");
Contains references to other bth_node allocations.
const_iterator end() const
Returns a STL style iterator positioned at the "end" entry.
const_iterator begin() const
Returns a STL style iterator positioned at the first entry.
A generic class to read and write to a file.
virtual size_t read(std::vector< byte > &buffer, ulonglong offset) const
Read from the file.
This function or method has not been implemented.
The exceptions used by pstsdk.
boost::uint64_t ulonglong
ulonglong time_t_to_filetime(time_t time)
Convert from a time_t to filetime.
time_t filetime_to_time_t(ulonglong filetime)
Convert from a filetime to time_t.
std::wstring bytes_to_wstring(const std::vector< byte > &bytes)
Convert an array of bytes to a std::wstring.
double time_t_to_vt_date(time_t time)
Convert from a time_t to a VT_DATE.
time_t vt_date_to_time_t(double vt_time)
Convert from a VT_DATE to a time_t.
bool test_bit(const byte *pbytes, ulong bit)
Test to see if the specified bit in the buffer is set.
std::string bytes_to_string(const std::vector< byte > &bytes)
Convert an array of bytes to a std::wstring.
std::vector< byte > wstring_to_bytes(const std::wstring &wstr)
Convert a std::wstring to an array of bytes.
Contains the definition of all in memory representations of disk structures.
Primitive structures defined by MS-PST and MAPI.
#define PSTSDK_WCHAR_ENCODING