libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
format_codec.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 <functional>
26#include <istream>
27#include <map>
28#include <memory>
29#include <mutex>
30#include <ostream>
31
32#include "libkeepass/export.hh"
33
34namespace keepass {
35
36class Database;
37class Key;
38class KdbFile;
39
40namespace detail {
41
42// Numeric ids must match the values of KeePass::Format (kKdb=1, kKdbx3=2,
43// kKdbx4=3). They are duplicated here to keep this header independent of the
44// unified API header and its include graph.
45constexpr int kFormatKdb = 1;
46constexpr int kFormatKdbx3 = 2;
47constexpr int kFormatKdbx4 = 3;
48
52 bool write_kdbx4 = false;
53
55 bool use_cached_key = true;
56};
57
67public:
68 virtual ~FormatCodec() = default;
69
71 virtual std::unique_ptr<Database> Import(std::istream& src, const Key& key) = 0;
72
74 virtual void Export(std::ostream& dst, const Database& db, const Key& key) = 0;
75};
76
80
81using FormatCodecFactory = std::function<std::unique_ptr<FormatCodec>(const FormatCodecOptions&)>;
82
92public:
95
97 void Register(int format, FormatCodecFactory factory);
98
100 std::unique_ptr<FormatCodec> Create(int format, const FormatCodecOptions& options) const;
101
103 bool Contains(int format) const;
104
105private:
107
108 mutable std::mutex mutex_;
109 std::map<int, FormatCodecFactory> factories_;
110};
111
112} // namespace detail
113} // namespace keepass
Represents a KeePass (KDB/KDBX) database.
Definition database.hh:74
Manages database encryption keys for KDB and KDBX formats.
Definition key.hh:45
Process-wide registry of format codec factories.
Definition format_codec.hh:91
bool Contains(int format) const
Returns whether a factory is registered for format.
void Register(int format, FormatCodecFactory factory)
Registers/overwrites the factory for a numeric format id.
static FormatCodecRegistry & Instance()
Returns the process-wide registry.
std::unique_ptr< FormatCodec > Create(int format, const FormatCodecOptions &options) const
Creates a codec for format, or throws UnsupportedFeatureError.
Imports/exports one database dialect.
Definition format_codec.hh:66
virtual void Export(std::ostream &dst, const Database &db, const Key &key)=0
Exports db to dst using key.
virtual std::unique_ptr< Database > Import(std::istream &src, const Key &key)=0
Imports a database from src using key.
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
Options that influence codec construction.
Definition format_codec.hh:50
bool write_kdbx4
Force KDBX 4 output (KDBX 3 otherwise) for the KDBX codec.
Definition format_codec.hh:52
bool use_cached_key
Allow the KDBX codec to reuse a transformed key cached on the Database.
Definition format_codec.hh:55