encrypt.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2017, Open Source Robotics Foundation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #include <iostream>
36 
37 #include <boost/scoped_ptr.hpp>
38 #include <boost/program_options.hpp>
39 #include <boost/progress.hpp>
40 #include <boost/regex.hpp>
41 
42 #include <ros/ros.h>
43 
44 #include "rosbag/bag.h"
45 #include "rosbag/view.h"
46 
47 namespace po = boost::program_options;
48 
50 {
52 
53  void buildOutbagName();
54 
55  bool quiet;
56  std::string plugin;
57  std::string param;
59  std::string inbag;
60  std::string outbag;
61 };
62 
64 {
65  if (!outbag.empty())
66  return;
67  if (inbag.empty())
68  throw ros::Exception("Input bag is not specified.");
69  std::string::size_type pos = inbag.find_last_of('.');
70  if (pos == std::string::npos)
71  throw ros::Exception("Input bag name has no extension.");
72  outbag = inbag.substr(0, pos) + std::string(".out") + inbag.substr(pos);
73 }
74 
76 EncryptorOptions parseOptions(int argc, char** argv)
77 {
78  EncryptorOptions opts;
79 
80  po::options_description desc("Allowed options");
81 
82  desc.add_options()
83  ("help,h", "produce help message")
84  ("quiet,q", "suppress console output")
85  ("plugin,p", po::value<std::string>()->default_value("rosbag/AesCbcEncryptor"), "encryptor name")
86  ("param,r", po::value<std::string>()->default_value("*"), "encryptor parameter")
87  ("bz2,j", "use BZ2 compression")
88  ("lz4", "use lz4 compression")
89  ("inbag", po::value<std::string>(), "bag file to encrypt")
90  ("outbag,o", po::value<std::string>(), "bag file encrypted")
91  ;
92 
93  po::positional_options_description p;
94  p.add("inbag", -1);
95 
96  po::variables_map vm;
97 
98  try
99  {
100  po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
101  }
102  catch (const boost::program_options::invalid_command_line_syntax& e)
103  {
104  throw ros::Exception(e.what());
105  }
106  catch (const boost::program_options::unknown_option& e)
107  {
108  throw ros::Exception(e.what());
109  }
110 
111  if (vm.count("help"))
112  {
113  std::cout << desc << std::endl;
114  exit(0);
115  }
116 
117  if (vm.count("quiet"))
118  opts.quiet = true;
119  if (vm.count("plugin"))
120  opts.plugin = vm["plugin"].as<std::string>();
121  if (vm.count("param"))
122  opts.param = vm["param"].as<std::string>();
123  if (vm.count("bz2"))
125  if (vm.count("lz4"))
127  if (vm.count("inbag"))
128  opts.inbag = vm["inbag"].as<std::string>();
129  else
130  throw ros::Exception("You must specify bag to encrypt.");
131  if (vm.count("outbag"))
132  opts.outbag = vm["outbag"].as<std::string>();
133  opts.buildOutbagName();
134 
135  return opts;
136 }
137 
139 {
140  switch(compression)
141  {
142  case rosbag::compression::Uncompressed: return "none";
143  case rosbag::compression::BZ2: return "bz2";
144  case rosbag::compression::LZ4: return "lz4";
145  default: return "Unknown";
146  }
147 }
148 
149 int encrypt(EncryptorOptions const& options)
150 {
151  if (!options.quiet)
152  {
153  std::cout << "Output bag: " << options.outbag << "\n";
154  std::cout << "Encryption: " << options.plugin << ":" << options.param << "\n";
155  std::cout << "Compression: " << getStringCompressionType(options.compression) << "\n";
156  }
159  // Compression type is per chunk, and cannot be retained.
160  // If chunk-by-chunk encryption is implemented, compression type could be honored.
161  outbag.setEncryptorPlugin(options.plugin, options.param);
162  outbag.setCompression(options.compression);
163  rosbag::View view(inbag);
164  boost::scoped_ptr<boost::progress_display> progress;
165  if (!options.quiet)
166  progress.reset(new boost::progress_display(view.size(), std::cout, "Progress:\n ", " ", " "));
167  for (rosbag::View::const_iterator it = view.begin(); it != view.end(); ++it)
168  {
169  outbag.write(it->getTopic(), it->getTime(), *it, it->getConnectionHeader());
170  if (progress)
171  ++(*progress);
172  }
173  outbag.close();
174  inbag.close();
175  return 0;
176 }
177 
178 int main(int argc, char** argv)
179 {
180  // Parse the command-line options
181  EncryptorOptions opts;
182  try
183  {
184  opts = parseOptions(argc, argv);
185  }
186  catch (const ros::Exception& ex)
187  {
188  ROS_ERROR("Error reading options: %s", ex.what());
189  return 1;
190  }
191  catch (const boost::regex_error& ex)
192  {
193  ROS_ERROR("Error reading options: %s\n", ex.what());
194  return 1;
195  }
196 
197  return encrypt(opts);
198 }
rosbag::CompressionType compression
Definition: encrypt.cpp:58
void run(class_loader::ClassLoader *loader)
std::string param
Definition: encrypt.cpp:57
int main(int argc, char **argv)
Definition: encrypt.cpp:178
def default_value(type_)
void close()
uint32_t size()
void buildOutbagName()
Definition: encrypt.cpp:63
void setCompression(CompressionType compression)
std::string outbag
Definition: encrypt.cpp:60
std::string getStringCompressionType(rosbag::CompressionType compression)
Definition: encrypt.cpp:138
iterator const_iterator
iterator end()
int encrypt(EncryptorOptions const &options)
Definition: encrypt.cpp:149
iterator begin()
EncryptorOptions parseOptions(int argc, char **argv)
Parse the command-line arguments for encrypt options.
Definition: encrypt.cpp:76
std::string inbag
Definition: encrypt.cpp:59
Uncompressed
void setEncryptorPlugin(const std::string &plugin_name, const std::string &plugin_param=std::string())
#define ROS_ERROR(...)
void write(std::string const &topic, ros::MessageEvent< T > const &event)
std::string plugin
Definition: encrypt.cpp:56


rosbag
Author(s): Tim Field, Jeremy Leibs, James Bowman, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:34:21