Botan 1.10.17
cms_comp.cpp
Go to the documentation of this file.
1/*
2* CMS Compression
3* (C) 1999-2007 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/cms_enc.h>
9#include <botan/cms_dec.h>
10#include <botan/der_enc.h>
11#include <botan/ber_dec.h>
12#include <botan/oids.h>
13#include <botan/pipe.h>
14
15#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
16 #include <botan/zlib.h>
17#endif
18
19namespace Botan {
20
21/*
22* Compress a message
23*/
24void CMS_Encoder::compress(const std::string& algo)
25 {
27 throw Invalid_Argument("CMS_Encoder: Cannot compress with " + algo);
28
29 Filter* compressor = 0;
30
31#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
32 if(algo == "Zlib") compressor = new Zlib_Compression;
33#endif
34
35 if(compressor == 0)
36 throw Internal_Error("CMS: Couldn't get ahold of a compressor");
37
38 Pipe pipe(compressor);
39 pipe.process_msg(data);
40 SecureVector<byte> compressed = pipe.read_all();
41
42 DER_Encoder encoder;
43 encoder.start_cons(SEQUENCE).
44 encode(static_cast<size_t>(0)).
45 encode(AlgorithmIdentifier("Compression." + algo,
47 raw_bytes(make_econtent(compressed, type)).
48 end_cons();
49
50 add_layer("CMS.CompressedData", encoder);
51 }
52
53/*
54* See if the named compression algo is available
55*/
56bool CMS_Encoder::can_compress_with(const std::string& algo)
57 {
58 if(algo == "")
59 throw Invalid_Algorithm_Name("Empty string to can_compress_with");
60
61#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
62 if(algo == "Zlib")
63 return true;
64#endif
65
66 return false;
67 }
68
69/*
70* Decompress a message
71*/
72void CMS_Decoder::decompress(BER_Decoder& decoder)
73 {
74 size_t version;
75 AlgorithmIdentifier comp_algo;
76
77 BER_Decoder comp_info = decoder.start_cons(SEQUENCE);
78
79 comp_info.decode(version);
80 if(version != 0)
81 throw Decoding_Error("CMS: Unknown version for CompressedData");
82
83 comp_info.decode(comp_algo);
84 read_econtent(comp_info);
85 comp_info.end_cons();
86
87 Filter* decompressor = 0;
88
89 info = comp_algo.oid.as_string();
90
91#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
92 if(comp_algo.oid == OIDS::lookup("Compression.Zlib"))
93 {
94 decompressor = new Zlib_Decompression;
95 info = "Zlib";
96 }
97#endif
98
99 if(!decompressor)
100 status = FAILURE;
101
102 Pipe pipe(decompressor);
103 pipe.process_msg(data);
104 data = pipe.read_all();
105 }
106
107}
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition ber_dec.cpp:238
BER_Decoder & decode(bool &)
Definition ber_dec.cpp:344
BER_Decoder & end_cons()
Definition ber_dec.cpp:252
void compress(const std::string &)
Definition cms_comp.cpp:24
static bool can_compress_with(const std::string &)
Definition cms_comp.cpp:56
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition der_enc.cpp:135
std::string as_string() const
Definition asn1_oid.cpp:50
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition pipe_rw.cpp:105
void process_msg(const byte in[], size_t length)
Definition pipe.cpp:116
std::string lookup(const OID &oid)
Definition oids.cpp:31
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
@ SEQUENCE
Definition asn1_int.h:35
Decoding_Error(const std::string &name)
Definition exceptn.h:140
Internal_Error(const std::string &err)
Definition exceptn.h:47
Invalid_Algorithm_Name(const std::string &name)
Definition exceptn.h:121