2012-07-29 18:32:54 +00:00
|
|
|
// Include qpdf-config.h first so off_t is guaranteed to have the right size.
|
2012-06-20 15:20:57 +00:00
|
|
|
#include <qpdf/qpdf-config.h>
|
2012-06-22 13:46:33 +00:00
|
|
|
|
|
|
|
#include <qpdf/QUtil.hh>
|
2013-01-25 13:59:55 +00:00
|
|
|
#include <qpdf/PointerHolder.hh>
|
2012-06-22 13:46:33 +00:00
|
|
|
|
2013-02-26 17:31:00 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
2008-05-04 16:02:53 +00:00
|
|
|
#include <string.h>
|
2009-07-15 03:47:44 +00:00
|
|
|
#include <fcntl.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <direct.h>
|
2009-07-15 03:47:44 +00:00
|
|
|
#include <io.h>
|
2013-10-05 21:36:33 +00:00
|
|
|
#include <Wincrypt.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string
|
2013-02-26 17:31:00 +00:00
|
|
|
QUtil::int_to_string(long long num, int length)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-02-28 21:20:45 +00:00
|
|
|
return int_to_string_base(num, 10, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QUtil::int_to_string_base(long long num, int base, int length)
|
|
|
|
{
|
|
|
|
// Backward compatibility -- int_to_string, which calls this
|
|
|
|
// function, used to use sprintf with %0*d, so we interpret length
|
|
|
|
// such that a negative value appends spaces and a positive value
|
|
|
|
// prepends zeroes.
|
|
|
|
if (! ((base == 8) || (base == 10) || (base == 16)))
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"int_to_string_base called with unsupported base");
|
|
|
|
}
|
2013-02-26 17:31:00 +00:00
|
|
|
std::ostringstream buf;
|
2013-02-28 21:20:45 +00:00
|
|
|
buf << std::setbase(base) << std::nouppercase << num;
|
2013-02-26 17:31:00 +00:00
|
|
|
std::string result;
|
|
|
|
if ((length > 0) &&
|
|
|
|
(buf.str().length() < static_cast<size_t>(length)))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-02-26 17:31:00 +00:00
|
|
|
result.append(length - buf.str().length(), '0');
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-02-26 17:31:00 +00:00
|
|
|
result += buf.str();
|
|
|
|
if ((length < 0) && (buf.str().length() < static_cast<size_t>(-length)))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-02-26 17:31:00 +00:00
|
|
|
result.append(-length - buf.str().length(), ' ');
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-02-26 17:31:00 +00:00
|
|
|
return result;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QUtil::double_to_string(double num, int decimal_places)
|
|
|
|
{
|
2013-02-26 17:31:00 +00:00
|
|
|
// Backward compatibility -- this code used to use sprintf and
|
|
|
|
// treated decimal_places <= 0 to mean to use the default, which
|
2013-04-14 18:36:25 +00:00
|
|
|
// was six decimal places. Also sprintf with %*.f interprets the
|
2013-02-26 17:31:00 +00:00
|
|
|
// length as fixed point rather than significant figures.
|
|
|
|
if (decimal_places <= 0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-02-26 17:31:00 +00:00
|
|
|
decimal_places = 6;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-02-26 17:31:00 +00:00
|
|
|
std::ostringstream buf;
|
|
|
|
buf << std::setprecision(decimal_places) << std::fixed << num;
|
|
|
|
return buf.str();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2012-06-21 23:32:21 +00:00
|
|
|
long long
|
|
|
|
QUtil::string_to_ll(char const* str)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _strtoi64(str, 0, 10);
|
|
|
|
#else
|
|
|
|
return strtoll(str, 0, 10);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-02-24 02:46:21 +00:00
|
|
|
unsigned char*
|
|
|
|
QUtil::unsigned_char_pointer(std::string const& str)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<unsigned char*>(const_cast<char*>(str.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char*
|
|
|
|
QUtil::unsigned_char_pointer(char const* str)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<unsigned char*>(const_cast<char*>(str));
|
|
|
|
}
|
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
void
|
|
|
|
QUtil::throw_system_error(std::string const& description)
|
|
|
|
{
|
2013-02-28 22:10:30 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// "94" is mentioned in the MSVC docs, but it's still safe if the
|
|
|
|
// message is longer. strerror_s is a templated function that
|
|
|
|
// knows the size of buf and truncates.
|
|
|
|
char buf[94];
|
|
|
|
if (strerror_s(buf, errno) != 0)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(description + ": failed with an unknown error");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error(description + ": " + buf);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
throw std::runtime_error(description + ": " + strerror(errno));
|
|
|
|
#endif
|
2009-09-26 18:36:04 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
int
|
2009-09-26 18:36:04 +00:00
|
|
|
QUtil::os_wrapper(std::string const& description, int status)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if (status == -1)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw_system_error(description);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-02-28 21:45:11 +00:00
|
|
|
FILE*
|
|
|
|
QUtil::safe_fopen(char const* filename, char const* mode)
|
|
|
|
{
|
2013-02-28 22:10:30 +00:00
|
|
|
FILE* f = 0;
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
errno_t err = fopen_s(&f, filename, mode);
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
errno = err;
|
|
|
|
throw_system_error(std::string("open ") + filename);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
f = fopen_wrapper(std::string("open ") + filename, fopen(filename, mode));
|
|
|
|
#endif
|
|
|
|
return f;
|
2013-02-28 21:45:11 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
FILE*
|
2009-09-26 18:36:04 +00:00
|
|
|
QUtil::fopen_wrapper(std::string const& description, FILE* f)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if (f == 0)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw_system_error(description);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:20:57 +00:00
|
|
|
int
|
2012-06-27 03:09:21 +00:00
|
|
|
QUtil::seek(FILE* stream, qpdf_offset_t offset, int whence)
|
2012-06-20 15:20:57 +00:00
|
|
|
{
|
|
|
|
#if HAVE_FSEEKO
|
2013-02-24 02:46:21 +00:00
|
|
|
return fseeko(stream, static_cast<off_t>(offset), whence);
|
2012-06-26 20:52:30 +00:00
|
|
|
#elif HAVE_FSEEKO64
|
|
|
|
return fseeko64(stream, offset, whence);
|
2012-06-20 15:20:57 +00:00
|
|
|
#else
|
2012-06-26 20:52:30 +00:00
|
|
|
# ifdef _MSC_VER
|
|
|
|
return _fseeki64(stream, offset, whence);
|
|
|
|
# else
|
2013-02-24 02:46:21 +00:00
|
|
|
return fseek(stream, static_cast<long>(offset), whence);
|
2012-06-26 20:52:30 +00:00
|
|
|
# endif
|
2012-06-20 15:20:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t
|
2012-06-27 03:09:21 +00:00
|
|
|
QUtil::tell(FILE* stream)
|
2012-06-20 15:20:57 +00:00
|
|
|
{
|
|
|
|
#if HAVE_FSEEKO
|
2013-02-24 02:46:21 +00:00
|
|
|
return static_cast<qpdf_offset_t>(ftello(stream));
|
2012-06-26 20:52:30 +00:00
|
|
|
#elif HAVE_FSEEKO64
|
2013-02-24 02:46:21 +00:00
|
|
|
return static_cast<qpdf_offset_t>(ftello64(stream));
|
2012-06-20 15:20:57 +00:00
|
|
|
#else
|
2012-06-26 20:52:30 +00:00
|
|
|
# ifdef _MSC_VER
|
|
|
|
return _ftelli64(stream);
|
|
|
|
# else
|
2013-02-24 02:46:21 +00:00
|
|
|
return static_cast<qpdf_offset_t>(ftell(stream));
|
2012-06-26 20:52:30 +00:00
|
|
|
# endif
|
2012-06-20 15:20:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
char*
|
|
|
|
QUtil::copy_string(std::string const& str)
|
|
|
|
{
|
|
|
|
char* result = new char[str.length() + 1];
|
|
|
|
// Use memcpy in case string contains nulls
|
|
|
|
result[str.length()] = '\0';
|
|
|
|
memcpy(result, str.c_str(), str.length());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-25 13:59:55 +00:00
|
|
|
std::string
|
|
|
|
QUtil::hex_encode(std::string const& input)
|
|
|
|
{
|
2013-02-28 21:20:45 +00:00
|
|
|
std::string result;
|
|
|
|
for (unsigned int i = 0; i < input.length(); ++i)
|
2013-01-25 13:59:55 +00:00
|
|
|
{
|
2013-02-28 21:20:45 +00:00
|
|
|
result += QUtil::int_to_string_base(
|
2013-10-05 23:42:39 +00:00
|
|
|
static_cast<int>(static_cast<unsigned char>(input.at(i))), 16, 2);
|
2013-01-25 13:59:55 +00:00
|
|
|
}
|
2013-02-28 21:20:45 +00:00
|
|
|
return result;
|
2013-01-25 13:59:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-15 03:47:44 +00:00
|
|
|
void
|
|
|
|
QUtil::binary_stdout()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
_setmode(_fileno(stdout), _O_BINARY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
void
|
|
|
|
QUtil::binary_stdin()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
_setmode(_fileno(stdin), _O_BINARY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-21 02:30:15 +00:00
|
|
|
void
|
|
|
|
QUtil::setLineBuf(FILE* f)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2013-02-24 02:46:21 +00:00
|
|
|
setvbuf(f, reinterpret_cast<char *>(NULL), _IOLBF, 0);
|
2009-10-21 02:30:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
char*
|
|
|
|
QUtil::getWhoami(char* argv0)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
char pathsep = '\\';
|
|
|
|
#else
|
|
|
|
char pathsep = '/';
|
|
|
|
#endif
|
|
|
|
char* whoami = 0;
|
|
|
|
if ((whoami = strrchr(argv0, pathsep)) == NULL)
|
|
|
|
{
|
|
|
|
whoami = argv0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++whoami;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
if ((strlen(whoami) > 4) &&
|
|
|
|
(strcmp(whoami + strlen(whoami) - 4, ".exe") == 0))
|
|
|
|
{
|
|
|
|
whoami[strlen(whoami) - 4] = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return whoami;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
bool
|
|
|
|
QUtil::get_env(std::string const& var, std::string* value)
|
|
|
|
{
|
|
|
|
// This was basically ripped out of wxWindows.
|
|
|
|
#ifdef _WIN32
|
|
|
|
// first get the size of the buffer
|
|
|
|
DWORD len = ::GetEnvironmentVariable(var.c_str(), NULL, 0);
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
// this means that there is no such variable
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
char* t = new char[len + 1];
|
|
|
|
::GetEnvironmentVariable(var.c_str(), t, len);
|
|
|
|
*value = t;
|
|
|
|
delete [] t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
char* p = getenv(var.c_str());
|
|
|
|
if (p == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
*value = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-07-12 22:52:13 +00:00
|
|
|
time_t
|
|
|
|
QUtil::get_current_time()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
// The procedure to get local time at this resolution comes from
|
|
|
|
// the Microsoft documentation. It says to convert a SYSTEMTIME
|
|
|
|
// to a FILETIME, and to copy the FILETIME to a ULARGE_INTEGER.
|
|
|
|
// The resulting number is the number of 100-nanosecond intervals
|
|
|
|
// between January 1, 1601 and now. POSIX threads wants a time
|
|
|
|
// based on January 1, 1970, so we adjust by subtracting the
|
|
|
|
// number of seconds in that time period from the result we get
|
|
|
|
// here.
|
|
|
|
SYSTEMTIME sysnow;
|
|
|
|
GetSystemTime(&sysnow);
|
|
|
|
FILETIME filenow;
|
|
|
|
SystemTimeToFileTime(&sysnow, &filenow);
|
|
|
|
ULARGE_INTEGER uinow;
|
|
|
|
uinow.LowPart = filenow.dwLowDateTime;
|
|
|
|
uinow.HighPart = filenow.dwHighDateTime;
|
|
|
|
ULONGLONG now = uinow.QuadPart;
|
|
|
|
return ((now / 10000000LL) - 11644473600LL);
|
|
|
|
#else
|
|
|
|
return time(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QUtil::toUTF8(unsigned long uval)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// A UTF-8 encoding of a Unicode value is a single byte for
|
|
|
|
// Unicode values <= 127. For larger values, the first byte of
|
|
|
|
// the UTF-8 encoding has '1' as each of its n highest bits and
|
|
|
|
// '0' for its (n+1)th highest bit where n is the total number of
|
|
|
|
// bytes required. Subsequent bytes start with '10' and have the
|
|
|
|
// remaining 6 bits free for encoding. For example, an 11-bit
|
2009-02-21 02:54:31 +00:00
|
|
|
// Unicode value can be stored in two bytes where the first is
|
2008-04-29 12:55:25 +00:00
|
|
|
// 110zzzzz, the second is 10zzzzzz, and the z's represent the
|
|
|
|
// remaining bits.
|
|
|
|
|
|
|
|
if (uval > 0x7fffffff)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::runtime_error("bounds error in QUtil::toUTF8");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else if (uval < 128)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
result += static_cast<char>(uval);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned char bytes[7];
|
|
|
|
bytes[6] = '\0';
|
|
|
|
unsigned char* cur_byte = &bytes[5];
|
|
|
|
|
|
|
|
// maximum value that will fit in the current number of bytes
|
|
|
|
unsigned char maxval = 0x3f; // six bits
|
|
|
|
|
|
|
|
while (uval > maxval)
|
|
|
|
{
|
|
|
|
// Assign low six bits plus 10000000 to lowest unused
|
|
|
|
// byte position, then shift
|
2013-02-24 02:46:21 +00:00
|
|
|
*cur_byte = static_cast<unsigned char>(0x80 + (uval & 0x3f));
|
2008-04-29 12:55:25 +00:00
|
|
|
uval >>= 6;
|
|
|
|
// Maximum that will fit in high byte now shrinks by one bit
|
|
|
|
maxval >>= 1;
|
|
|
|
// Slide to the left one byte
|
2013-10-05 09:48:56 +00:00
|
|
|
if (cur_byte <= bytes)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error("QUtil::toUTF8: overflow error");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-10-05 09:48:56 +00:00
|
|
|
--cur_byte;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
// If maxval is k bits long, the high (7 - k) bits of the
|
|
|
|
// resulting byte must be high.
|
2013-02-24 02:46:21 +00:00
|
|
|
*cur_byte = static_cast<unsigned char>(
|
|
|
|
(0xff - (1 + (maxval << 1))) + uval);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2013-02-24 02:46:21 +00:00
|
|
|
result += reinterpret_cast<char*>(cur_byte);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-12-28 21:37:46 +00:00
|
|
|
|
2013-10-05 21:36:33 +00:00
|
|
|
#ifdef USE_INSECURE_RANDOM
|
|
|
|
|
2012-12-28 21:37:46 +00:00
|
|
|
long
|
|
|
|
QUtil::random()
|
|
|
|
{
|
|
|
|
static bool seeded_random = false;
|
|
|
|
if (! seeded_random)
|
|
|
|
{
|
|
|
|
// Seed the random number generator with something simple, but
|
|
|
|
// just to be interesting, don't use the unmodified current
|
2013-02-24 02:46:21 +00:00
|
|
|
// time. It would be better if this were a more secure seed.
|
|
|
|
QUtil::srandom(QUtil::get_current_time() ^ 0xcccc);
|
2012-12-28 21:37:46 +00:00
|
|
|
seeded_random = true;
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:36:33 +00:00
|
|
|
# ifdef HAVE_RANDOM
|
2012-12-28 21:37:46 +00:00
|
|
|
return ::random();
|
2013-10-05 21:36:33 +00:00
|
|
|
# else
|
2012-12-28 21:37:46 +00:00
|
|
|
return rand();
|
2013-10-05 21:36:33 +00:00
|
|
|
# endif
|
2012-12-28 21:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-05 21:36:33 +00:00
|
|
|
QUtil::initializeWithRandomBytes(unsigned char* data, size_t len)
|
2012-12-28 21:37:46 +00:00
|
|
|
{
|
2013-10-05 21:36:33 +00:00
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
data[i] = static_cast<unsigned char>((QUtil::random() & 0xff0) >> 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-28 21:37:46 +00:00
|
|
|
#else
|
2013-10-05 21:36:33 +00:00
|
|
|
|
|
|
|
long
|
|
|
|
QUtil::random()
|
|
|
|
{
|
|
|
|
long result = 0L;
|
|
|
|
initializeWithRandomBytes(
|
|
|
|
reinterpret_cast<unsigned char*>(&result),
|
|
|
|
sizeof(result));
|
|
|
|
return result;
|
2012-12-28 21:37:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 21:36:33 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
class WindowsCryptProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WindowsCryptProvider()
|
|
|
|
{
|
|
|
|
if (! CryptAcquireContext(&crypt_prov, NULL, NULL, PROV_RSA_FULL, 0))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("unable to acquire crypt context");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~WindowsCryptProvider()
|
|
|
|
{
|
|
|
|
// Ignore error
|
|
|
|
CryptReleaseContext(crypt_prov, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
HCRYPTPROV crypt_prov;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-12-28 21:37:46 +00:00
|
|
|
void
|
|
|
|
QUtil::initializeWithRandomBytes(unsigned char* data, size_t len)
|
|
|
|
{
|
2013-10-05 21:36:33 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
// Optimization: make the WindowsCryptProvider static as long as
|
|
|
|
// it can be done in a thread-safe fashion.
|
|
|
|
WindowsCryptProvider c;
|
|
|
|
if (! CryptGenRandom(c.crypt_prov, len, reinterpret_cast<BYTE*>(data)))
|
2012-12-28 21:37:46 +00:00
|
|
|
{
|
2013-10-05 21:36:33 +00:00
|
|
|
throw std::runtime_error("unable to generate secure random data");
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(RANDOM_DEVICE)
|
|
|
|
|
|
|
|
// Optimization: wrap the file open and close in a class so that
|
|
|
|
// the file is closed in a destructor, then make this static to
|
|
|
|
// keep the file handle open. Only do this if it can be done in a
|
|
|
|
// thread-safe fashion.
|
|
|
|
FILE* f = QUtil::safe_fopen(RANDOM_DEVICE, "rb");
|
|
|
|
size_t fr = fread(data, 1, len, f);
|
|
|
|
fclose(f);
|
|
|
|
if (fr != len)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(
|
|
|
|
"unable to read " +
|
|
|
|
QUtil::int_to_string(len) +
|
|
|
|
" bytes from " + std::string(RANDOM_DEVICE));
|
2012-12-28 21:37:46 +00:00
|
|
|
}
|
2013-10-05 21:36:33 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# error "Don't know how to generate secure random numbers on this platform. See random number generation in the top-level README"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
QUtil::srandom(unsigned int seed)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_RANDOM
|
|
|
|
::srandom(seed);
|
|
|
|
#else
|
|
|
|
srand(seed);
|
|
|
|
#endif
|
2012-12-28 21:37:46 +00:00
|
|
|
}
|