Botan 3.3.0
Crypto and TLS for C&
compression.cpp
Go to the documentation of this file.
1/*
2* Compression Factory
3* (C) 2014,2016 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/compression.h>
9
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
12#include <cstdlib>
13
14#if defined(BOTAN_HAS_ZLIB)
15 #include <botan/zlib.h>
16#endif
17
18#if defined(BOTAN_HAS_BZIP2)
19 #include <botan/bzip2.h>
20#endif
21
22#if defined(BOTAN_HAS_LZMA)
23 #include <botan/lzma.h>
24#endif
25
26namespace Botan {
27
28//static
29std::unique_ptr<Compression_Algorithm> Compression_Algorithm::create(std::string_view name) {
30#if defined(BOTAN_HAS_ZLIB)
31 if(name == "Zlib" || name == "zlib") {
32 return std::make_unique<Zlib_Compression>();
33 }
34 if(name == "Gzip" || name == "gzip" || name == "gz") {
35 return std::make_unique<Gzip_Compression>();
36 }
37 if(name == "Deflate" || name == "deflate") {
38 return std::make_unique<Deflate_Compression>();
39 }
40#endif
41
42#if defined(BOTAN_HAS_BZIP2)
43 if(name == "bzip2" || name == "bz2" || name == "Bzip2") {
44 return std::make_unique<Bzip2_Compression>();
45 }
46#endif
47
48#if defined(BOTAN_HAS_LZMA)
49 if(name == "lzma" || name == "xz" || name == "LZMA") {
50 return std::make_unique<LZMA_Compression>();
51 }
52#endif
53
55 return nullptr;
56}
57
58//static
59std::unique_ptr<Compression_Algorithm> Compression_Algorithm::create_or_throw(std::string_view algo) {
60 if(auto compressor = Compression_Algorithm::create(algo)) {
61 return compressor;
62 }
63 throw Lookup_Error("Compression", algo, "");
64}
65
66//static
67std::unique_ptr<Decompression_Algorithm> Decompression_Algorithm::create(std::string_view name) {
68#if defined(BOTAN_HAS_ZLIB)
69 if(name == "Zlib" || name == "zlib") {
70 return std::make_unique<Zlib_Decompression>();
71 }
72 if(name == "Gzip" || name == "gzip" || name == "gz") {
73 return std::make_unique<Gzip_Decompression>();
74 }
75 if(name == "Deflate" || name == "deflate") {
76 return std::make_unique<Deflate_Decompression>();
77 }
78#endif
79
80#if defined(BOTAN_HAS_BZIP2)
81 if(name == "bzip2" || name == "bz2" || name == "Bzip2") {
82 return std::make_unique<Bzip2_Decompression>();
83 }
84#endif
85
86#if defined(BOTAN_HAS_LZMA)
87 if(name == "lzma" || name == "xz" || name == "LZMA") {
88 return std::make_unique<LZMA_Decompression>();
89 }
90#endif
91
93 return nullptr;
94}
95
96//static
97std::unique_ptr<Decompression_Algorithm> Decompression_Algorithm::create_or_throw(std::string_view algo) {
98 if(auto decompressor = Decompression_Algorithm::create(algo)) {
99 return decompressor;
100 }
101 throw Lookup_Error("Decompression", algo, "");
102}
103
104} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
static std::unique_ptr< Compression_Algorithm > create_or_throw(std::string_view algo_spec)
static std::unique_ptr< Compression_Algorithm > create(std::string_view algo_spec)
virtual std::string name() const =0
static std::unique_ptr< Decompression_Algorithm > create(std::string_view algo_spec)
virtual std::string name() const =0
static std::unique_ptr< Decompression_Algorithm > create_or_throw(std::string_view algo_spec)
std::string name