libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
binary.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
22#pragma once
23
24#include <cstddef>
25#include <memory>
26#include <mutex>
27#include <ostream>
28#include <string>
29
31#include "secure.hh"
32#include "security.hh"
33
34namespace keepass {
35
46private:
47 mutable std::mutex mutex_;
48 mutable protect<secure_string> data_;
49 mutable bool loaded_ = true;
50 bool protected_ = false;
51 bool compress_ = false;
52 std::size_t size_ = 0;
53 std::shared_ptr<const detail::BinarySource> source_;
54
55public:
57 explicit Binary(const protect<secure_string>& data);
58
66 Binary(std::shared_ptr<const detail::BinarySource> source, bool protected_data,
67 bool compress = false);
68
70 Binary(const Binary& other);
71
73 Binary& operator=(const Binary& other);
74
76 bool Empty() const;
77
79 std::size_t Size() const;
80
83
86
88 void load() const;
89
91 bool is_loaded() const { return loaded_; }
92
94 bool is_lazy() const { return source_ != nullptr; }
95
97 bool is_protected() const { return protected_; }
98
100 void WriteTo(std::ostream& dst) const;
101
103 bool compress() const { return compress_; }
104
106 void set_compress(bool compress) { compress_ = compress; }
107
109 bool operator==(const Binary& other) const;
110
112 bool operator!=(const Binary& other) const { return !(*this == other); }
113};
114
115} // namespace keepass
Lazy backing store for binary attachment payloads.
A binary blob (e.g. an attachment payload) with an optional compression flag.
Definition binary.hh:45
bool Empty() const
Returns whether the binary data is empty.
Binary & operator=(const Binary &other)
Copy-assigns the payload/source state.
bool is_loaded() const
Returns whether the payload is currently materialized in memory.
Definition binary.hh:91
void load() const
Loads a lazy payload into secure memory (no-op for materialized data).
Binary(const Binary &other)
Copy-constructs the payload/source state.
Binary(std::shared_ptr< const detail::BinarySource > source, bool protected_data, bool compress=false)
Creates a lazy binary backed by a shared source.
bool operator!=(const Binary &other) const
Inequality comparison.
Definition binary.hh:112
Binary(const protect< secure_string > &data)
Creates a binary from the given protected data.
bool operator==(const Binary &other) const
Equality comparison (materializes lazy payloads).
std::size_t Size() const
Returns the size in bytes of the binary data.
void set_compress(bool compress)
Sets whether the binary is stored compressed.
Definition binary.hh:106
const protect< secure_string > & data() const
Returns the binary data, loading it first if necessary.
bool is_protected() const
Returns whether the payload was stored with the protected flag.
Definition binary.hh:97
bool compress() const
Returns whether the binary is stored compressed.
Definition binary.hh:103
bool is_lazy() const
Returns whether the payload is backed by a lazy source.
Definition binary.hh:94
void set_data(const protect< secure_string > &data)
Sets the binary data, replacing any lazy source.
void WriteTo(std::ostream &dst) const
Writes the payload to dst, streaming it when it is still lazy.
Wrapper that marks a value as sensitive and tracks the protection flag of a KeePass field.
Definition security.hh:41
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
Secure memory handling: zeroization, locked allocations and sensitive value containers.
Wrapper type for protecting sensitive KeePass field values.