2014-07-21 05:22:06 +02:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// Copyright (C) 2013-2014 OpenVPN Technologies, Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Basic file-handling methods.
|
|
|
|
|
2011-10-06 19:22:37 +02:00
|
|
|
#ifndef OPENVPN_COMMON_FILE_H
|
|
|
|
#define OPENVPN_COMMON_FILE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
|
2012-11-12 02:52:03 +01:00
|
|
|
#include <boost/cstdint.hpp> // for boost::uint64_t
|
|
|
|
|
2011-10-06 19:22:37 +02:00
|
|
|
#include <openvpn/common/exception.hpp>
|
2012-11-12 02:52:03 +01:00
|
|
|
#include <openvpn/common/unicode.hpp>
|
2011-11-26 12:33:39 +01:00
|
|
|
#include <openvpn/buffer/buffer.hpp>
|
2011-10-06 19:22:37 +02:00
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_UNTAGGED_EXCEPTION(file_exception);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, open_file_error);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, file_too_large);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, file_is_binary);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, file_not_utf8);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read text from file via stream approach that doesn't require that we
|
|
|
|
// establish the length of the file in advance.
|
2012-11-12 02:52:03 +01:00
|
|
|
inline std::string read_text_simple(const std::string& filename)
|
2011-10-06 19:22:37 +02:00
|
|
|
{
|
2011-10-07 07:47:49 +02:00
|
|
|
std::ifstream ifs(filename.c_str());
|
2011-10-06 19:22:37 +02:00
|
|
|
if (!ifs)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot open: " << filename);
|
2011-10-06 19:22:37 +02:00
|
|
|
const std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
2011-10-07 07:47:49 +02:00
|
|
|
if (!ifs)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot read: " << filename);
|
2011-10-06 19:22:37 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read a file (may be text or binary).
|
2012-11-12 02:52:03 +01:00
|
|
|
inline BufferPtr read_binary(const std::string& filename,
|
|
|
|
const boost::uint64_t max_size = 0,
|
|
|
|
const unsigned int buffer_flags = 0)
|
2011-11-26 12:33:39 +01:00
|
|
|
{
|
|
|
|
std::ifstream ifs(filename.c_str(), std::ios::binary);
|
|
|
|
if (!ifs)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot open: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
|
|
|
// get length of file
|
|
|
|
ifs.seekg (0, std::ios::end);
|
|
|
|
const std::streamsize length = ifs.tellg();
|
2012-11-12 02:52:03 +01:00
|
|
|
if (max_size && boost::uint64_t(length) > max_size)
|
|
|
|
OPENVPN_THROW(file_too_large, "file too large [" << length << '/' << max_size << "]: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
ifs.seekg (0, std::ios::beg);
|
|
|
|
|
|
|
|
// allocate buffer
|
2012-01-25 09:32:27 +01:00
|
|
|
BufferPtr b = new BufferAllocated(size_t(length), buffer_flags | BufferAllocated::ARRAY);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
|
|
|
// read data
|
|
|
|
ifs.read((char *)b->data(), length);
|
|
|
|
|
|
|
|
// check for errors
|
|
|
|
if (ifs.gcount() != length)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "read length inconsistency: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
if (!ifs)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot read: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read a text file as a std::string, throw error if file is binary
|
2012-11-12 02:52:03 +01:00
|
|
|
inline std::string read_text(const std::string& filename, const boost::uint64_t max_size = 0)
|
|
|
|
{
|
|
|
|
BufferPtr bp = read_binary(filename, max_size);
|
|
|
|
if (bp->contains_null())
|
|
|
|
OPENVPN_THROW(file_is_binary, "file is binary: " << filename);
|
|
|
|
return std::string((const char *)bp->c_data(), bp->size());
|
|
|
|
}
|
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read a UTF-8 file as a std::string, throw errors if file is binary or malformed UTF-8
|
2012-11-12 02:52:03 +01:00
|
|
|
inline std::string read_text_utf8(const std::string& filename, const boost::uint64_t max_size = 0)
|
2012-08-12 02:32:15 +02:00
|
|
|
{
|
2012-11-12 02:52:03 +01:00
|
|
|
BufferPtr bp = read_binary(filename, max_size);
|
2013-01-30 14:36:53 +01:00
|
|
|
|
|
|
|
// check if binary
|
2012-11-12 02:52:03 +01:00
|
|
|
if (bp->contains_null())
|
|
|
|
OPENVPN_THROW(file_is_binary, "file is binary: " << filename);
|
2013-01-30 14:36:53 +01:00
|
|
|
|
|
|
|
// remove Windows UTF-8 BOM if present
|
|
|
|
if (bp->size() >= 3)
|
|
|
|
{
|
|
|
|
const unsigned char *data = bp->c_data();
|
|
|
|
if (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
|
|
|
|
bp->advance(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that file is valid UTF-8
|
2012-11-12 02:52:03 +01:00
|
|
|
if (!Unicode::is_valid_utf8(bp->c_data(), bp->size()))
|
|
|
|
OPENVPN_THROW(file_not_utf8, "file is not UTF8: " << filename);
|
2013-01-30 14:36:53 +01:00
|
|
|
|
2012-08-12 02:32:15 +02:00
|
|
|
return std::string((const char *)bp->c_data(), bp->size());
|
|
|
|
}
|
2011-10-06 19:22:37 +02:00
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif // OPENVPN_COMMON_FILE_H
|