2012-06-22 13:46:33 +00:00
|
|
|
// Include qpdf-config.h first so off_t is gauaranteed 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>
|
|
|
|
|
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>
|
2008-04-29 12:55:25 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string
|
2012-06-20 15:20:57 +00:00
|
|
|
QUtil::int_to_string(long long num, int fullpad)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// This routine will need to be recompiled if an int can be longer than
|
|
|
|
// 49 digits.
|
|
|
|
char t[50];
|
|
|
|
|
|
|
|
// -2 or -1 to leave space for the possible negative sign and for NUL...
|
|
|
|
if (abs(fullpad) > (int)sizeof(t) - ((num < 0)?2:1))
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error("Util::int_to_string has been called with "
|
|
|
|
"a padding value greater than its internal "
|
|
|
|
"limit");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2012-06-20 15:20:57 +00:00
|
|
|
#ifdef HAVE_PRINTF_LL
|
|
|
|
# define PRINTF_LL "ll"
|
|
|
|
#else
|
|
|
|
# define PRINTF_LL "l"
|
|
|
|
#endif
|
2008-04-29 12:55:25 +00:00
|
|
|
if (fullpad)
|
|
|
|
{
|
2012-06-20 15:20:57 +00:00
|
|
|
sprintf(t, "%0*" PRINTF_LL "d", fullpad, num);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-20 15:20:57 +00:00
|
|
|
sprintf(t, "%" PRINTF_LL "d", num);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-06-20 15:20:57 +00:00
|
|
|
#undef PRINTF_LL
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
return std::string(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QUtil::double_to_string(double num, int decimal_places)
|
|
|
|
{
|
|
|
|
// This routine will need to be recompiled if a double can be longer than
|
|
|
|
// 99 digits.
|
|
|
|
char t[100];
|
|
|
|
|
|
|
|
std::string lhs = int_to_string((int)num);
|
|
|
|
|
|
|
|
// lhs.length() gives us the length of the part on the right hand
|
|
|
|
// side of the dot + 1 for the dot + decimal_places: total size of
|
|
|
|
// the required string. -1 on the sizeof side to allow for NUL at
|
|
|
|
// the end.
|
|
|
|
//
|
|
|
|
// If decimal_places <= 0, it is as if no precision was provided
|
|
|
|
// so trust the buffer is big enough. The following test will
|
|
|
|
// always pass in those cases.
|
|
|
|
if (decimal_places + 1 + (int)lhs.length() > (int)sizeof(t) - 1)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error("Util::double_to_string has been called with "
|
|
|
|
"a number and a decimal places specification "
|
|
|
|
"that would break an internal limit");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (decimal_places)
|
|
|
|
{
|
|
|
|
sprintf(t, "%.*f", decimal_places, num);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(t, "%f", num);
|
|
|
|
}
|
|
|
|
return std::string(t);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
void
|
|
|
|
QUtil::throw_system_error(std::string const& description)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(description + ": " + strerror(errno));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 23:32:21 +00:00
|
|
|
QUtil::fseek_off_t(FILE* stream, qpdf_offset_t offset, int whence)
|
2012-06-20 15:20:57 +00:00
|
|
|
{
|
|
|
|
#if HAVE_FSEEKO
|
2012-06-21 23:32:21 +00:00
|
|
|
return fseeko(stream, (off_t)offset, whence);
|
2012-06-20 15:20:57 +00:00
|
|
|
#else
|
2012-06-21 23:32:21 +00:00
|
|
|
return fseek(stream, (long)offset, whence);
|
2012-06-20 15:20:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t
|
2012-06-20 15:20:57 +00:00
|
|
|
QUtil::ftell_off_t(FILE* stream)
|
|
|
|
{
|
|
|
|
#if HAVE_FSEEKO
|
2012-06-21 23:32:21 +00:00
|
|
|
return (qpdf_offset_t)ftello(stream);
|
2012-06-20 15:20:57 +00:00
|
|
|
#else
|
2012-06-21 23:32:21 +00:00
|
|
|
return (qpdf_offset_t)ftell(stream);
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
setvbuf(f, (char *) NULL, _IOLBF, 0);
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
result += (char)(uval);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
*cur_byte = (unsigned char) (0x80 + (uval & 0x3f));
|
|
|
|
uval >>= 6;
|
|
|
|
// Maximum that will fit in high byte now shrinks by one bit
|
|
|
|
maxval >>= 1;
|
|
|
|
// Slide to the left one byte
|
|
|
|
--cur_byte;
|
|
|
|
if (cur_byte < bytes)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error("QUtil::toUTF8: overflow error");
|
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.
|
|
|
|
*cur_byte = (unsigned char)((0xff - (1 + (maxval << 1))) + uval);
|
|
|
|
|
|
|
|
result += (char*)cur_byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|