aboutsummaryrefslogtreecommitdiffstats
path: root/src/cms/cms_comp.cpp
blob: a0e7773210a110a1c0c4e9ccf19700d25a3164db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*************************************************
* CMS Compression Source File                    *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/cms_enc.h>
#include <botan/cms_dec.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/oids.h>
#include <botan/pipe.h>

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
  #include <botan/zlib.h>
#endif

namespace Botan {

/*************************************************
* Compress a message                             *
*************************************************/
void CMS_Encoder::compress(const std::string& algo)
   {
   if(!CMS_Encoder::can_compress_with(algo))
      throw Invalid_Argument("CMS_Encoder: Cannot compress with " + algo);

   Filter* compressor = 0;

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
   if(algo == "Zlib") compressor = new Zlib_Compression;
#endif

   if(compressor == 0)
      throw Internal_Error("CMS: Couldn't get ahold of a compressor");

   Pipe pipe(compressor);
   pipe.process_msg(data);
   SecureVector<byte> compressed = pipe.read_all();

   DER_Encoder encoder;
   encoder.start_cons(SEQUENCE).
      encode((u32bit)0).
      encode(AlgorithmIdentifier("Compression." + algo,
                                 MemoryVector<byte>())).
      raw_bytes(make_econtent(compressed, type)).
   end_cons();

   add_layer("CMS.CompressedData", encoder);
   }

/*************************************************
* See if the named compression algo is available *
*************************************************/
bool CMS_Encoder::can_compress_with(const std::string& algo)
   {
   if(algo == "")
      throw Invalid_Algorithm_Name("Empty string to can_compress_with");

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
   if(algo == "Zlib")
      return true;
#endif

   return false;
   }

/*************************************************
* Decompress a message                           *
*************************************************/
void CMS_Decoder::decompress(BER_Decoder& decoder)
   {
   u32bit version;
   AlgorithmIdentifier comp_algo;

   BER_Decoder comp_info = decoder.start_cons(SEQUENCE);

   comp_info.decode(version);
   if(version != 0)
      throw Decoding_Error("CMS: Unknown version for CompressedData");

   comp_info.decode(comp_algo);
   read_econtent(comp_info);
   comp_info.end_cons();

   Filter* decompressor = 0;

   info = comp_algo.oid.as_string();

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
   if(comp_algo.oid == OIDS::lookup("Compression.Zlib"))
      {
      decompressor = new Zlib_Decompression;
      info = "Zlib";
      }
#endif

   if(!decompressor)
      status = FAILURE;

   Pipe pipe(decompressor);
   pipe.process_msg(data);
   data = pipe.read_all();
   }

}