libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
mapped_file.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
23#pragma once
24
25#include <cstddef>
26#include <streambuf>
27#include <string>
28
29#include "libkeepass/export.hh"
30
31namespace keepass {
32namespace detail {
33
44public:
46
50 explicit MappedFile(const std::string& path);
51
54
55 MappedFile(const MappedFile&) = delete;
56 MappedFile& operator=(const MappedFile&) = delete;
57 MappedFile(MappedFile&&) = delete;
58 MappedFile& operator=(MappedFile&&) = delete;
59
61 const char* data() const noexcept { return static_cast<const char*>(data_); }
62
64 std::size_t size() const noexcept { return size_; }
65
67 bool empty() const noexcept { return size_ == 0; }
68
69private:
70 void* data_ = nullptr;
71 std::size_t size_ = 0;
72
73#ifdef _WIN32
74 void* file_ = nullptr;
75 void* mapping_ = nullptr;
76#else
77 int fd_ = -1;
78#endif
79};
80
87class LIBKEEPASS_API mapped_istreambuf final : public std::streambuf {
88public:
93 mapped_istreambuf(const char* data, std::size_t size) {
94 // A zero-sized mapping has no valid pointer; use a static sentinel so the
95 // stream buffer never forms a pointer past nullptr.
96 static char empty = 0;
97 begin_ = size == 0 ? &empty : const_cast<char*>(data);
98 end_ = begin_ + size;
99 setg(begin_, begin_, end_);
100 }
101
102protected:
103 std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
104 std::ios_base::openmode which) override;
105 std::streampos seekpos(std::streampos sp, std::ios_base::openmode which) override;
106
107private:
108 char* begin_ = nullptr;
109 char* end_ = nullptr;
110};
111
112} // namespace detail
113} // namespace keepass
Read-only memory mapping of a file.
Definition mapped_file.hh:43
bool empty() const noexcept
Returns whether the file is empty.
Definition mapped_file.hh:67
MappedFile(const std::string &path)
Opens and maps the file at path.
std::size_t size() const noexcept
Returns the size of the mapping in bytes.
Definition mapped_file.hh:64
const char * data() const noexcept
Returns a pointer to the first mapped byte (nullptr for an empty file).
Definition mapped_file.hh:61
~MappedFile()
Unmaps the file and releases all handles.
Streambuf over a read-only memory region.
Definition mapped_file.hh:87
mapped_istreambuf(const char *data, std::size_t size)
Definition mapped_file.hh:93
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43