10#ifndef PSTSDK_UTIL_UTIL_H
11#define PSTSDK_UTIL_UTIL_H
19#include <boost/utility.hpp>
24#if defined(_WIN32) || defined(__MINGW32__)
38class file :
private boost::noncopyable
73 std::wstring m_filename;
148 const char*
mode =
"rb";
158 throw std::runtime_error(
"fopen failed");
163 if (!m_pfile)
return;
176 throw std::out_of_range(
"fseek failed");
182 throw std::out_of_range(
"fread failed");
188inline size_t pstsdk::file::write(
const std::vector<byte>&
buffer,
ulonglong offset)
196 throw std::out_of_range(
"fseek failed");
199 size_t write = fwrite(&buffer[0], 1, buffer.size(), m_pfile);
201 if(write != buffer.size())
202 throw std::out_of_range(
"fwrite failed");
234 return (*(
pbytes + (
bit >> 3)) & (0x80 >> (
bit & 7))) != 0;
237#if defined(_WIN32) || defined(__MINGW32__)
243 if(bytes.size() == 0)
244 return std::wstring();
246 return std::wstring(
reinterpret_cast<const wchar_t *
>(&bytes[0]), bytes.size()/
sizeof(
wchar_t));
252 return std::vector<byte>();
254 const byte *begin =
reinterpret_cast<const byte *
>(&wstr[0]);
255 return std::vector<byte>(begin, begin + wstr.size()*
sizeof(
wchar_t));
263 size_t utf8_sz = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), NULL, 0, NULL, NULL);
265 std::vector<char> utf8_str(utf8_sz);
266 size_t copied = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), &utf8_str.data()[0], utf8_sz, NULL, NULL);
268 if (copied != utf8_sz)
269 throw std::runtime_error(
"Expected to copy " + std::to_string(utf8_sz) +
" bytes but copied " + std::to_string(copied));
271 return { utf8_str.begin(), utf8_str.end() };
282 if(
bytes.size() == 0)
283 return std::wstring();
286 if(
bytes.size() % 2 != 0)
287 throw std::runtime_error(
"Cannot interpret odd number of bytes as UTF-16LE");
288 std::wstring
out(
bytes.size() / 2,
L'\0');
290 iconv_t cd(::iconv_open(
"WCHAR_T",
"UTF-16LE"));
292 perror(
"bytes_to_wstring");
293 throw std::runtime_error(
"Unable to convert from UTF-16LE to wstring");
296 const char *
inbuf =
reinterpret_cast<const char *
>(&
bytes[0]);
298 char *
outbuf =
reinterpret_cast<char *
>(&
out[0]);
303 throw std::runtime_error(
"Failed to convert from UTF-16LE to wstring");
312 if(
bytes.size() == 0)
313 return std::string();
316 if(
bytes.size() % 2 != 0)
317 throw std::runtime_error(
"Cannot interpret odd number of bytes as UTF-16LE");
320 std::string
out(
bytes.size() * 2,
L'\0');
322 iconv_t cd(::iconv_open(
"UTF-8",
"UTF-16LE"));
324 perror(
"bytes_to_string");
325 throw std::runtime_error(
"Unable to convert from UTF-16LE to string");
328 const char *
inbuf =
reinterpret_cast<const char *
>(&
bytes[0]);
330 char *
outbuf =
reinterpret_cast<char *
>(&
out[0]);
336 throw std::runtime_error(
"Failed to convert from UTF-16LE to string");
346 return std::vector<byte>();
349 std::vector<byte>
out(
wstr.size() * 4);
351 iconv_t cd(::iconv_open(
"UTF-16LE",
"WCHAR_T"));
353 perror(
"wstring_to_bytes");
354 throw std::runtime_error(
"Unable to convert from wstring to UTF-16LE");
357 const char *
inbuf =
reinterpret_cast<const char *
>(&
wstr[0]);
359 char *
outbuf =
reinterpret_cast<char *
>(&
out[0]);
364 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.