2018-01-14 01:25:44 +00:00
|
|
|
// Copyright (c) 2005-2018 Jay Berkenbilt
|
2008-04-29 12:55:25 +00:00
|
|
|
//
|
2017-09-14 16:44:31 +00:00
|
|
|
// This file is part of qpdf.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Versions of qpdf prior to version 7 were released under the terms
|
|
|
|
// of version 2.0 of the Artistic License. At your option, you may
|
|
|
|
// continue to consider qpdf to be licensed under those terms. Please
|
|
|
|
// see the manual for additional information.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2018-08-12 18:07:22 +00:00
|
|
|
#ifndef QUTIL_HH
|
|
|
|
#define QUTIL_HH
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2009-10-19 20:17:14 +00:00
|
|
|
#include <qpdf/DLL.h>
|
2012-06-20 15:20:57 +00:00
|
|
|
#include <qpdf/Types.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <stdio.h>
|
2015-05-24 19:34:56 +00:00
|
|
|
#include <time.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2013-11-30 17:02:56 +00:00
|
|
|
class RandomDataProvider;
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
namespace QUtil
|
|
|
|
{
|
|
|
|
// This is a collection of useful utility functions that don't
|
|
|
|
// really go anywhere else.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2012-06-20 15:20:57 +00:00
|
|
|
std::string int_to_string(long long, int length = 0);
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2013-02-28 21:20:45 +00:00
|
|
|
std::string int_to_string_base(long long, int base, int length = 0);
|
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string double_to_string(double, int decimal_places = 0);
|
|
|
|
|
2017-08-29 16:21:29 +00:00
|
|
|
// These string to number methods throw std::runtime_error on
|
|
|
|
// underflow/overflow.
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDF_DLL
|
|
|
|
long long string_to_ll(char const* str);
|
2017-08-29 16:21:29 +00:00
|
|
|
QPDF_DLL
|
|
|
|
int string_to_int(char const* str);
|
2012-06-21 23:32:21 +00:00
|
|
|
|
2013-02-24 02:46:21 +00:00
|
|
|
// Pipeline's write method wants unsigned char*, but we often have
|
|
|
|
// some other type of string. These methods do combinations of
|
|
|
|
// const_cast and reinterpret_cast to give us an unsigned char*.
|
|
|
|
// They should only be used when it is known that it is safe.
|
|
|
|
// None of the pipelines in qpdf modify the data passed to them,
|
|
|
|
// so within qpdf, it should always be safe.
|
|
|
|
QPDF_DLL
|
|
|
|
unsigned char* unsigned_char_pointer(std::string const& str);
|
|
|
|
QPDF_DLL
|
|
|
|
unsigned char* unsigned_char_pointer(char const* str);
|
|
|
|
|
2018-08-13 23:48:02 +00:00
|
|
|
// Throw QPDFSystemError, which is derived from
|
|
|
|
// std::runtime_error, with a string formed by appending to
|
2009-09-26 18:36:04 +00:00
|
|
|
// "description: " the standard string corresponding to the
|
2018-08-13 23:48:02 +00:00
|
|
|
// current value of errno. You can retrieve the value of errno by
|
|
|
|
// calling getErrno() on the QPDFSystemError. Prior to qpdf 8.2.0,
|
|
|
|
// this method threw system::runtime_error directly, but since
|
|
|
|
// QPDFSystemError is derived from system::runtime_error, old code
|
|
|
|
// that specifically catches std::runtime_error will still work.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-26 18:36:04 +00:00
|
|
|
void throw_system_error(std::string const& description);
|
|
|
|
|
|
|
|
// The status argument is assumed to be the return value of a
|
|
|
|
// standard library call that sets errno when it fails. If status
|
|
|
|
// is -1, convert the current value of errno to a
|
|
|
|
// std::runtime_error that includes the standard error string.
|
|
|
|
// Otherwise, return status.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-26 18:36:04 +00:00
|
|
|
int os_wrapper(std::string const& description, int status);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2013-02-28 21:45:11 +00:00
|
|
|
// If the open fails, throws std::runtime_error. Otherwise, the
|
|
|
|
// FILE* is returned.
|
|
|
|
QPDF_DLL
|
|
|
|
FILE* safe_fopen(char const* filename, char const* mode);
|
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
// The FILE* argument is assumed to be the return of fopen. If
|
|
|
|
// null, throw std::runtime_error. Otherwise, return the FILE*
|
|
|
|
// argument.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-26 18:36:04 +00:00
|
|
|
FILE* fopen_wrapper(std::string const&, FILE*);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-06-20 15:20:57 +00:00
|
|
|
// Wrap around off_t versions of fseek and ftell if available
|
|
|
|
QPDF_DLL
|
2012-06-27 03:09:21 +00:00
|
|
|
int seek(FILE* stream, qpdf_offset_t offset, int whence);
|
2012-06-20 15:20:57 +00:00
|
|
|
QPDF_DLL
|
2012-06-27 03:09:21 +00:00
|
|
|
qpdf_offset_t tell(FILE* stream);
|
2012-06-20 15:20:57 +00:00
|
|
|
|
2017-07-30 00:19:54 +00:00
|
|
|
QPDF_DLL
|
|
|
|
bool same_file(char const* name1, char const* name2);
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
char* copy_string(std::string const&);
|
|
|
|
|
2013-01-25 13:59:55 +00:00
|
|
|
// Returns lower-case hex-encoded version of the string, treating
|
|
|
|
// each character in the input string as unsigned. The output
|
|
|
|
// string will be twice as long as the input string.
|
|
|
|
QPDF_DLL
|
|
|
|
std::string hex_encode(std::string const&);
|
|
|
|
|
2018-01-14 14:04:13 +00:00
|
|
|
// Returns a string that is the result of decoding the input
|
|
|
|
// string. The input string may consist of mixed case hexadecimal
|
|
|
|
// digits. Any characters that are not hexadecimal digits will be
|
|
|
|
// silently ignored. If there are an odd number of hexadecimal
|
|
|
|
// digits, a trailing 0 will be assumed.
|
|
|
|
QPDF_DLL
|
|
|
|
std::string hex_decode(std::string const&);
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
// Set stdin, stdout to binary mode
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-15 03:47:44 +00:00
|
|
|
void binary_stdout();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-15 04:26:32 +00:00
|
|
|
void binary_stdin();
|
2009-10-21 02:30:15 +00:00
|
|
|
// Set stdout to line buffered
|
|
|
|
QPDF_DLL
|
|
|
|
void setLineBuf(FILE*);
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
|
|
|
|
// May modify argv0
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-15 04:26:32 +00:00
|
|
|
char* getWhoami(char* argv0);
|
2009-07-15 03:47:44 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Get the value of an environment variable in a portable fashion.
|
|
|
|
// Returns true iff the variable is defined. If `value' is
|
|
|
|
// non-null, initializes it with the value of the variable.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
bool get_env(std::string const& var, std::string* value = 0);
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-12 22:52:13 +00:00
|
|
|
time_t get_current_time();
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Return a string containing the byte representation of the UTF-8
|
|
|
|
// encoding for the unicode value passed in.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string toUTF8(unsigned long uval);
|
2012-12-28 21:37:46 +00:00
|
|
|
|
2018-06-21 17:05:48 +00:00
|
|
|
// Return a string containing the byte representation of the
|
|
|
|
// UTF-16 BE encoding for the unicode value passed in.
|
|
|
|
// Unrepresentable code points are converted to U+FFFD.
|
|
|
|
QPDF_DLL
|
|
|
|
std::string toUTF16(unsigned long uval);
|
|
|
|
|
2013-10-05 21:36:33 +00:00
|
|
|
// If secure random number generation is supported on your
|
|
|
|
// platform and qpdf was not compiled with insecure random number
|
2015-10-31 22:55:37 +00:00
|
|
|
// generation, this returns a cryptographically secure random
|
2013-10-05 21:36:33 +00:00
|
|
|
// number. Otherwise it falls back to random from stdlib and
|
|
|
|
// calls srandom automatically the first time it is called.
|
2012-12-28 21:37:46 +00:00
|
|
|
QPDF_DLL
|
|
|
|
long random();
|
|
|
|
|
2013-10-05 21:36:33 +00:00
|
|
|
// Wrapper around srandom from stdlib. Seeds the standard library
|
|
|
|
// weak random number generator, which is not used if secure
|
|
|
|
// random number generation is being used. You never need to call
|
|
|
|
// this method as it is called automatically if needed.
|
2012-12-28 21:37:46 +00:00
|
|
|
QPDF_DLL
|
|
|
|
void srandom(unsigned int seed);
|
|
|
|
|
2013-11-30 17:02:56 +00:00
|
|
|
// Initialize a buffer with random bytes. By default, qpdf tries
|
|
|
|
// to use a secure random number source. It can be configured at
|
|
|
|
// compile time to use an insecure random number source (from
|
|
|
|
// stdlib). You can also call setRandomDataProvider with a
|
|
|
|
// RandomDataProvider, in which case this method will get its
|
|
|
|
// random bytes from that.
|
|
|
|
|
2012-12-28 21:37:46 +00:00
|
|
|
QPDF_DLL
|
|
|
|
void initializeWithRandomBytes(unsigned char* data, size_t len);
|
2013-11-30 17:02:56 +00:00
|
|
|
|
|
|
|
// Supply a random data provider. If not supplied, depending on
|
|
|
|
// compile time options, qpdf will either use the operating
|
|
|
|
// system's secure random number source or an insecure random
|
|
|
|
// source from stdlib. The caller is responsible for managing the
|
|
|
|
// memory for the RandomDataProvider. This method modifies a
|
|
|
|
// static variable. If you are providing your own random data
|
|
|
|
// provider, you should call this at the beginning of your program
|
2013-12-16 21:03:54 +00:00
|
|
|
// before creating any QPDF objects. Passing a null to this
|
|
|
|
// method will reset the library back to whichever of the built-in
|
2015-10-31 22:55:37 +00:00
|
|
|
// random data handlers is appropriate based on how qpdf was
|
2013-12-16 21:03:54 +00:00
|
|
|
// compiled.
|
2013-11-30 17:02:56 +00:00
|
|
|
QPDF_DLL
|
|
|
|
void setRandomDataProvider(RandomDataProvider*);
|
2013-12-16 21:03:54 +00:00
|
|
|
|
|
|
|
// This returns the random data provider that would be used the
|
|
|
|
// next time qpdf needs random data. It will never return null.
|
|
|
|
// If no random data provider has been provided and the library
|
|
|
|
// was not compiled with any random data provider available, an
|
|
|
|
// exception will be thrown.
|
|
|
|
QPDF_DLL
|
|
|
|
RandomDataProvider* getRandomDataProvider();
|
2017-07-22 23:23:52 +00:00
|
|
|
|
2017-07-30 02:23:21 +00:00
|
|
|
QPDF_DLL
|
|
|
|
std::list<std::string> read_lines_from_file(char const* filename);
|
|
|
|
QPDF_DLL
|
|
|
|
std::list<std::string> read_lines_from_file(std::istream&);
|
|
|
|
|
2017-08-05 14:08:11 +00:00
|
|
|
QPDF_DLL
|
|
|
|
int strcasecmp(char const *, char const *);
|
|
|
|
|
2017-07-22 23:23:52 +00:00
|
|
|
// These routines help the tokenizer recognize certain character
|
|
|
|
// classes without using ctype, which we avoid because of locale
|
|
|
|
// considerations.
|
|
|
|
QPDF_DLL
|
|
|
|
bool is_hex_digit(char);
|
|
|
|
|
|
|
|
QPDF_DLL
|
|
|
|
bool is_space(char);
|
|
|
|
|
|
|
|
QPDF_DLL
|
|
|
|
bool is_digit(char);
|
|
|
|
|
|
|
|
QPDF_DLL
|
|
|
|
bool is_number(char const*);
|
2008-04-29 12:55:25 +00:00
|
|
|
};
|
|
|
|
|
2018-08-12 18:07:22 +00:00
|
|
|
#endif // QUTIL_HH
|