Esempio n. 1
0
      void go() override
         {
         const std::string comp_type = get_arg("type");

         std::unique_ptr<Botan::Transform> compress;

#if defined(BOTAN_HAS_COMPRESSION)
         compress.reset(Botan::make_compressor(comp_type, get_arg_sz("level")));
#endif

         if(!compress)
            {
            throw CLI_Error_Unsupported("Compression", comp_type);
            }

         const std::string in_file = get_arg("file");
         std::ifstream in(in_file);

         if(!in.good())
            {
            throw CLI_IO_Error("reading", in_file);
            }

         const std::string out_file = output_filename(in_file, comp_type);
         std::ofstream out(out_file);
         if(!in.good())
            {
            throw CLI_IO_Error("writing", out_file);
            }

         do_compress(*compress, in, out, get_arg_sz("buf-size"));
         }
Esempio n. 2
0
      void go() override
         {
         const std::string in_file = get_arg("file");
         std::string out_file, suffix;
         parse_extension(in_file, out_file, suffix);

         std::ifstream in(in_file);

         if(!in.good())
            throw CLI_IO_Error("reading", in_file);

         std::unique_ptr<Botan::Transform> decompress;

#if defined(BOTAN_HAS_COMPRESSION)
         decompress.reset(Botan::make_decompressor(suffix));
#endif

         if(!decompress)
            throw CLI_Error_Unsupported("Decompression", suffix);

         std::ofstream out(out_file);
         if(!out.good())
            throw CLI_IO_Error("writing", out_file);

         do_compress(*decompress, in, out, get_arg_sz("buf-size"));
         }
Esempio n. 3
0
int Command::run(const std::vector<std::string>& params)
   {
   try
      {
      m_args.reset(new Argument_Parser(m_spec,
                                       {"verbose", "help"},
                                       {"output", "error-output", "rng-type", "drbg-seed"}));

      m_args->parse_args(params);

      if(m_args->has_arg("output"))
         {
         const std::string output_file = get_arg("output");

         if(output_file != "")
            {
            m_output_stream.reset(new std::ofstream(output_file, std::ios::binary));
            if(!m_output_stream->good())
               throw CLI_IO_Error("opening", output_file);
            }
         }

      if(m_args->has_arg("error-output"))
         {
         const std::string output_file = get_arg("error-output");

         if(output_file != "")
            {
            m_error_output_stream.reset(new std::ofstream(output_file, std::ios::binary));
            if(!m_error_output_stream->good())
               throw CLI_IO_Error("opening", output_file);
            }
         }

      if(flag_set("help"))
         {
         output() << help_text() << "\n";
         return 2;
         }

      this->go();
      return m_return_code;
      }
   catch(CLI_Usage_Error& e)
      {
      error_output() << "Usage error: " << e.what() << "\n";
      error_output() << help_text() << "\n";
      return 1;
      }
   catch(std::exception& e)
      {
      error_output() << "Error: " << e.what() << "\n";
      return 2;
      }
   catch(...)
      {
      error_output() << "Error: unknown exception\n";
      return 2;
      }
   }
Esempio n. 4
0
      void go() override
         {
         const std::string comp_type = get_arg("type");
         const size_t buf_size = get_arg_sz("buf-size");
         const size_t comp_level = get_arg_sz("level");

         std::unique_ptr<Botan::Compression_Algorithm> compress;

         compress.reset(Botan::make_compressor(comp_type));

         if(!compress)
            {
            throw CLI_Error_Unsupported("Compression", comp_type);
            }

         const std::string in_file = get_arg("file");
         std::ifstream in(in_file, std::ios::binary);

         if(!in.good())
            {
            throw CLI_IO_Error("reading", in_file);
            }

         const std::string out_file = output_filename(in_file, comp_type);
         std::ofstream out(out_file, std::ios::binary);
         if(!in.good())
            {
            throw CLI_IO_Error("writing", out_file);
            }

         Botan::secure_vector<uint8_t> buf;

         compress->start(comp_level);

         while(in.good())
            {
            buf.resize(buf_size);
            in.read(reinterpret_cast<char*>(&buf[0]), buf.size());
            buf.resize(in.gcount());

            compress->update(buf);

            out.write(reinterpret_cast<const char*>(&buf[0]), buf.size());
            }

         buf.clear();
         compress->finish(buf);
         out.write(reinterpret_cast<const char*>(&buf[0]), buf.size());
         out.close();
         }
Esempio n. 5
0
      void go() override
         {
         const size_t buf_size = get_arg_sz("buf-size");
         const std::string in_file = get_arg("file");
         std::string out_file, suffix;
         parse_extension(in_file, out_file, suffix);

         std::ifstream in(in_file, std::ios::binary);

         if(!in.good())
            {
            throw CLI_IO_Error("reading", in_file);
            }

         std::unique_ptr<Botan::Decompression_Algorithm> decompress;

         decompress.reset(Botan::make_decompressor(suffix));

         if(!decompress)
            {
            throw CLI_Error_Unsupported("Decompression", suffix);
            }

         std::ofstream out(out_file, std::ios::binary);
         if(!out.good())
            {
            throw CLI_IO_Error("writing", out_file);
            }

         Botan::secure_vector<uint8_t> buf;

         decompress->start();

         while(in.good())
            {
            buf.resize(buf_size);
            in.read(reinterpret_cast<char*>(&buf[0]), buf.size());
            buf.resize(in.gcount());

            decompress->update(buf);

            out.write(reinterpret_cast<const char*>(&buf[0]), buf.size());
            }

         buf.clear();
         decompress->finish(buf);
         out.write(reinterpret_cast<const char*>(&buf[0]), buf.size());
         out.close();
         }
Esempio n. 6
0
void Command::read_file(const std::string& input_file,
                        std::function<void (uint8_t[], size_t)> consumer_fn,
                        size_t buf_size) const
   {
   if(input_file == "-")
      {
      do_read_file(std::cin, consumer_fn, buf_size);
      }
   else
      {
      std::ifstream in(input_file, std::ios::binary);
      if(!in)
         {
         throw CLI_IO_Error("reading file", input_file);
         }
      do_read_file(in, consumer_fn, buf_size);
      }
   }