File: bzip.cpp

package info (click to toggle)
botan1.10 1.10.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 12,288 kB
  • sloc: cpp: 61,853; python: 1,687; asm: 1,247; ansic: 252; perl: 89; sh: 52; makefile: 38; lisp: 34
file content (116 lines) | stat: -rw-r--r-- 3,265 bytes parent folder | download | duplicates (4)
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
106
107
108
109
110
111
112
113
114
115
116
/*
* Bzip2 Compression/Decompression
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <iostream>
#include <botan/botan.h>

/*
* If Bzip2 isn't included, we know nothing works at compile time, but
* we wait to fail at runtime. Otherwise I would get 2-3 mails a month
* about how this was failing to compile (even with an informative
* #error message explaining the situation) because bzip2 wasn't
* included in the build.
*/

#if defined(BOTAN_HAS_COMPRESSOR_BZIP2)
  #include <botan/bzip2.h>
#endif

const std::string SUFFIX = ".bz2";

int main(int argc, char* argv[])
   {
   if(argc < 2)
      {
      std::cout << "Usage: " << argv[0]
                << " [-s] [-d] [-1...9] <filenames>" << std::endl;
      return 1;
      }

   Botan::LibraryInitializer init;

   std::vector<std::string> files;
   bool decompress = false, small = false;
   int level = 9;

   for(int j = 1; argv[j] != 0; j++)
      {
      if(std::strcmp(argv[j], "-d") == 0) { decompress = true; continue; }
      if(std::strcmp(argv[j], "-s") == 0) { small = true; continue; }
      if(std::strcmp(argv[j], "-1") == 0) { level = 1; continue; }
      if(std::strcmp(argv[j], "-2") == 0) { level = 2; continue; }
      if(std::strcmp(argv[j], "-3") == 0) { level = 3; continue; }
      if(std::strcmp(argv[j], "-4") == 0) { level = 4; continue; }
      if(std::strcmp(argv[j], "-5") == 0) { level = 5; continue; }
      if(std::strcmp(argv[j], "-6") == 0) { level = 6; continue; }
      if(std::strcmp(argv[j], "-7") == 0) { level = 7; continue; }
      if(std::strcmp(argv[j], "-8") == 0) { level = 8; continue; }
      if(std::strcmp(argv[j], "-9") == 0) { level = 9; continue; }
      files.push_back(argv[j]);
      }

   try {

      Botan::Filter* bzip = 0;
#ifdef BOTAN_HAS_COMPRESSOR_BZIP2
      if(decompress)
         bzip = new Botan::Bzip_Decompression(small);
      else
         bzip = new Botan::Bzip_Compression(level);
#endif

      if(!bzip)
         {
         std::cout << "Sorry, support for bzip2 not compiled into Botan\n";
         return 1;
         }

      Botan::Pipe pipe(bzip);

      for(unsigned int j = 0; j != files.size(); j++)
         {
         std::string infile = files[j], outfile = files[j];
         if(!decompress)
            outfile = outfile += SUFFIX;
         else
            outfile = outfile.replace(outfile.find(SUFFIX),
                                      SUFFIX.length(), "");

         std::ifstream in(infile.c_str(), std::ios::binary);
         std::ofstream out(outfile.c_str(), std::ios::binary);
         if(!in)
            {
            std::cout << "ERROR: could not read " << infile << std::endl;
            continue;
            }
         if(!out)
            {
            std::cout << "ERROR: could not write " << outfile << std::endl;
            continue;
            }

         pipe.start_msg();
         in >> pipe;
         pipe.end_msg();
         pipe.set_default_msg(j);
         out << pipe;

         in.close();
         out.close();
         }
   }
   catch(std::exception& e)
      {
      std::cout << "Exception caught: " << e.what() << std::endl;
      return 1;
      }
   return 0;
   }