Botan 2.19.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#include <botan/mem_ops.h>
10#include <botan/exceptn.h>
11#include <cstdlib>
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
28 {
29#if defined(BOTAN_HAS_ZLIB)
30 if(name == "Zlib" || name == "zlib")
31 return new Zlib_Compression;
32 if(name == "Gzip" || name == "gzip" || name == "gz")
33 return new Gzip_Compression;
34 if(name == "Deflate" || name == "deflate")
35 return new Deflate_Compression;
36#endif
37
38#if defined(BOTAN_HAS_BZIP2)
39 if(name == "bzip2" || name == "bz2" || name == "Bzip2")
40 return new Bzip2_Compression;
41#endif
42
43#if defined(BOTAN_HAS_LZMA)
44 if(name == "lzma" || name == "xz" || name == "LZMA")
45 return new LZMA_Compression;
46#endif
47
49 return nullptr;
50 }
51
52//static
53std::unique_ptr<Compression_Algorithm>
54Compression_Algorithm::create(const std::string& algo)
55 {
56 std::unique_ptr<Compression_Algorithm> compressor(make_compressor(algo));
57 return compressor;
58 }
59
60//static
61std::unique_ptr<Compression_Algorithm>
63 {
64 if(auto compressor = Compression_Algorithm::create(algo))
65 {
66 return compressor;
67 }
68 throw Lookup_Error("Compression", algo, "");
69 }
70
72 {
73#if defined(BOTAN_HAS_ZLIB)
74 if(name == "Zlib" || name == "zlib")
75 return new Zlib_Decompression;
76 if(name == "Gzip" || name == "gzip" || name == "gz")
77 return new Gzip_Decompression;
78 if(name == "Deflate" || name == "deflate")
79 return new Deflate_Decompression;
80#endif
81
82#if defined(BOTAN_HAS_BZIP2)
83 if(name == "bzip2" || name == "bz2" || name == "Bzip2")
84 return new Bzip2_Decompression;
85#endif
86
87#if defined(BOTAN_HAS_LZMA)
88 if(name == "lzma" || name == "xz" || name == "LZMA")
89 return new LZMA_Decompression;
90#endif
91
93 return nullptr;
94 }
95
96//static
97std::unique_ptr<Decompression_Algorithm>
98Decompression_Algorithm::create(const std::string& algo)
99 {
100 std::unique_ptr<Decompression_Algorithm> decompressor(make_decompressor(algo));
101 return decompressor;
102 }
103
104//static
105std::unique_ptr<Decompression_Algorithm>
107 {
108 if(auto decompressor = Decompression_Algorithm::create(algo))
109 {
110 return decompressor;
111 }
112 throw Lookup_Error("Decompression", algo, "");
113 }
114
115}
116
#define BOTAN_UNUSED(...)
Definition: assert.h:142
static std::unique_ptr< Compression_Algorithm > create(const std::string &algo_spec)
Definition: compression.cpp:54
static std::unique_ptr< Compression_Algorithm > create_or_throw(const std::string &algo_spec)
Definition: compression.cpp:62
static std::unique_ptr< Decompression_Algorithm > create(const std::string &algo_spec)
Definition: compression.cpp:98
static std::unique_ptr< Decompression_Algorithm > create_or_throw(const std::string &algo_spec)
std::string name
Definition: alg_id.cpp:13
Decompression_Algorithm * make_decompressor(const std::string &name)
Definition: compression.cpp:71
Compression_Algorithm * make_compressor(const std::string &name)
Definition: compression.cpp:27