Botan 2.19.5 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
25 namespace Botan {
26
27 Compression_Algorithm* make_compressor(const std::string& name)
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
48 BOTAN_UNUSED(name);
49 return nullptr;
50 }
51
52//static
53std::unique_ptr<Compression_Algorithm>
54 Compression_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>
62 Compression_Algorithm::create_or_throw(const std::string& algo)
63 {
64 if(auto compressor = Compression_Algorithm::create(algo))
65 {
66 return compressor;
67 }
68 throw Lookup_Error("Compression", algo, "");
69 }
70
71 Decompression_Algorithm* make_decompressor(const std::string& name)
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
92 BOTAN_UNUSED(name);
93 return nullptr;
94 }
95
96//static
97std::unique_ptr<Decompression_Algorithm>
98 Decompression_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>
106 Decompression_Algorithm::create_or_throw(const std::string& algo)
107 {
108 if(auto decompressor = Decompression_Algorithm::create(algo))
109 {
110 return decompressor;
111 }
112 throw Lookup_Error("Decompression", algo, "");
113 }
114
115}
116
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142
Botan::Bzip2_Compression
Definition: bzip2.h:20
Botan::Bzip2_Decompression
Definition: bzip2.h:31
Botan::Compression_Algorithm
Definition: compression.h:21
Botan::Compression_Algorithm::create
static std::unique_ptr< Compression_Algorithm > create(const std::string &algo_spec)
Definition: compression.cpp:54
Botan::Compression_Algorithm::create_or_throw
static std::unique_ptr< Compression_Algorithm > create_or_throw(const std::string &algo_spec)
Definition: compression.cpp:62
Botan::Decompression_Algorithm
Definition: compression.h:82
Botan::Decompression_Algorithm::create
static std::unique_ptr< Decompression_Algorithm > create(const std::string &algo_spec)
Definition: compression.cpp:98
Botan::Decompression_Algorithm::create_or_throw
static std::unique_ptr< Decompression_Algorithm > create_or_throw(const std::string &algo_spec)
Definition: compression.cpp:106
Botan::Deflate_Compression
Definition: zlib.h:42
Botan::Deflate_Decompression
Definition: zlib.h:53
Botan::Gzip_Compression
Definition: zlib.h:64
Botan::Gzip_Decompression
Definition: zlib.h:80
Botan::LZMA_Compression
Definition: lzma.h:21
Botan::LZMA_Decompression
Definition: lzma.h:33
Botan::Lookup_Error
Definition: exceptn.h:248
Botan::Zlib_Compression
Definition: zlib.h:20
Botan::Zlib_Decompression
Definition: zlib.h:31
name
std::string name
Definition: commoncrypto_hash.cpp:24
Botan
Definition: alg_id.cpp:13
Botan::make_decompressor
Decompression_Algorithm * make_decompressor(const std::string &name)
Definition: compression.cpp:71
Botan::make_compressor
Compression_Algorithm * make_compressor(const std::string &name)
Definition: compression.cpp:27