Botan 3.14.1
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
13#if defined(BOTAN_HAS_ZLIB)
14 #include <botan/zlib.h>
15#endif
16
17#if defined(BOTAN_HAS_BZIP2)
18 #include <botan/bzip2.h>
19#endif
20
21#if defined(BOTAN_HAS_LZMA)
22 #include <botan/lzma.h>
23#endif
24
25namespace Botan {
26
27//static
28std::unique_ptr<Compression_Algorithm> Compression_Algorithm::create(std::string_view name) {
29#if defined(BOTAN_HAS_ZLIB)
30 if(name == "Zlib" || name == "zlib") {
31 return std::make_unique<Zlib_Compression>();
32 }
33 if(name == "Gzip" || name == "gzip" || name == "gz") {
34 return std::make_unique<Gzip_Compression>();
35 }
36 if(name == "Deflate" || name == "deflate") {
37 return std::make_unique<Deflate_Compression>();
38 }
39#endif
40
41#if defined(BOTAN_HAS_BZIP2)
42 if(name == "bzip2" || name == "bz2" || name == "Bzip2") {
43 return std::make_unique<Bzip2_Compression>();
44 }
45#endif
46
47#if defined(BOTAN_HAS_LZMA)
48 if(name == "lzma" || name == "xz" || name == "LZMA") {
49 return std::make_unique<LZMA_Compression>();
50 }
51#endif
52
54 return nullptr;
55}
56
57//static
58std::unique_ptr<Compression_Algorithm> Compression_Algorithm::create_or_throw(std::string_view algo) {
59 if(auto compressor = Compression_Algorithm::create(algo)) {
60 return compressor;
61 }
62 throw Lookup_Error("Compression", algo, "");
63}
64
65//static
66std::unique_ptr<Decompression_Algorithm> Decompression_Algorithm::create(std::string_view name) {
67#if defined(BOTAN_HAS_ZLIB)
68 if(name == "Zlib" || name == "zlib") {
69 return std::make_unique<Zlib_Decompression>();
70 }
71 if(name == "Gzip" || name == "gzip" || name == "gz") {
72 return std::make_unique<Gzip_Decompression>();
73 }
74 if(name == "Deflate" || name == "deflate") {
75 return std::make_unique<Deflate_Decompression>();
76 }
77#endif
78
79#if defined(BOTAN_HAS_BZIP2)
80 if(name == "bzip2" || name == "bz2" || name == "Bzip2") {
81 return std::make_unique<Bzip2_Decompression>();
82 }
83#endif
84
85#if defined(BOTAN_HAS_LZMA)
86 if(name == "lzma" || name == "xz" || name == "LZMA") {
87 return std::make_unique<LZMA_Decompression>();
88 }
89#endif
90
92 return nullptr;
93}
94
95//static
96std::unique_ptr<Decompression_Algorithm> Decompression_Algorithm::create_or_throw(std::string_view algo) {
97 if(auto decompressor = Decompression_Algorithm::create(algo)) {
98 return decompressor;
99 }
100 throw Lookup_Error("Decompression", algo, "");
101}
102
103} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
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)