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 #define BOOST_TIMER_ENABLE_DEPRECATED
35 #include <iostream>
36 
37 #include <boost/scoped_ptr.hpp>
38 #include <boost/program_options.hpp>
39 #if BOOST_VERSION < 107200
40 #include <boost/progress.hpp>
41 #else
42 #include <boost/timer/progress_display.hpp>
43 #endif
44 #include <boost/regex.hpp>
45 
46 #include <ros/ros.h>
47 
48 #include "rosbag/bag.h"
49 #include "rosbag/view.h"
50 
51 namespace po = boost::program_options;
52 
54 {
56 
57  void buildOutbagName();
58 
59  bool quiet;
60  std::string plugin;
61  std::string param;
63  std::string inbag;
64  std::string outbag;
65 };
66 
68 {
69  if (!outbag.empty())
70  return;
71  if (inbag.empty())
72  throw ros::Exception("Input bag is not specified.");
73  std::string::size_type pos = inbag.find_last_of('.');
74  if (pos == std::string::npos)
75  throw ros::Exception("Input bag name has no extension.");
76  outbag = inbag.substr(0, pos) + std::string(".out") + inbag.substr(pos);
77 }
78 
80 EncryptorOptions parseOptions(int argc, char** argv)
81 {
82  EncryptorOptions opts;
83 
84  po::options_description desc("Allowed options");
85 
86  desc.add_options()
87  ("help,h", "produce help message")
88  ("quiet,q", "suppress console output")
89  ("plugin,p", po::value<std::string>()->default_value("rosbag/AesCbcEncryptor"), "encryptor name")
90  ("param,r", po::value<std::string>()->default_value("*"), "encryptor parameter")
91  ("bz2,j", "use BZ2 compression")
92  ("lz4", "use lz4 compression")
93  ("inbag", po::value<std::string>(), "bag file to encrypt")
94  ("outbag,o", po::value<std::string>(), "bag file encrypted")
95  ;
96 
97  po::positional_options_description p;
98  p.add("inbag", -1);
99 
100  po::variables_map vm;
101 
102  try
103  {
104  po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
105  }
106  catch (const boost::program_options::invalid_command_line_syntax& e)
107  {
108  throw ros::Exception(e.what());
109  }
110  catch (const boost::program_options::unknown_option& e)
111  {
112  throw ros::Exception(e.what());
113  }
114 
115  if (vm.count("help"))
116  {
117  std::cout << desc << std::endl;
118  exit(0);
119  }
120 
121  if (vm.count("quiet"))
122  opts.quiet = true;
123  if (vm.count("plugin"))
124  opts.plugin = vm["plugin"].as<std::string>();
125  if (vm.count("param"))
126  opts.param = vm["param"].as<std::string>();
127  if (vm.count("bz2"))
129  if (vm.count("lz4"))
131  if (vm.count("inbag"))
132  opts.inbag = vm["inbag"].as<std::string>();
133  else
134  throw ros::Exception("You must specify bag to encrypt.");
135  if (vm.count("outbag"))
136  opts.outbag = vm["outbag"].as<std::string>();
137  opts.buildOutbagName();
138 
139  return opts;
140 }
141 
143 {
144  switch(compression)
145  {
146  case rosbag::compression::Uncompressed: return "none";
147  case rosbag::compression::BZ2: return "bz2";
148  case rosbag::compression::LZ4: return "lz4";
149  default: return "Unknown";
150  }
151 }
152 
153 int encrypt(EncryptorOptions const& options)
154 {
155  if (!options.quiet)
156  {
157  std::cout << "Output bag: " << options.outbag << "\n";
158  std::cout << "Encryption: " << options.plugin << ":" << options.param << "\n";
159  std::cout << "Compression: " << getStringCompressionType(options.compression) << "\n";
160  }
161  rosbag::Bag inbag(options.inbag, rosbag::bagmode::Read);
162  rosbag::Bag outbag(options.outbag, rosbag::bagmode::Write);
163  // Compression type is per chunk, and cannot be retained.
164  // If chunk-by-chunk encryption is implemented, compression type could be honored.
165  outbag.setEncryptorPlugin(options.plugin, options.param);
166  outbag.setCompression(options.compression);
167  rosbag::View view(inbag);
168 #if BOOST_VERSION < 107200
169  boost::scoped_ptr<boost::progress_display> progress;
170 #else
171  boost::scoped_ptr<boost::timer::progress_display> progress;
172 #endif
173  if (!options.quiet)
174 #if BOOST_VERSION < 107200
175  progress.reset(new boost::progress_display(view.size(), std::cout, "Progress:\n ", " ", " "));
176 #else
177  progress.reset(new boost::timer::progress_display(view.size(), std::cout, "Progress:\n ", " ", " "));
178 #endif
179  for (rosbag::View::const_iterator it = view.begin(); it != view.end(); ++it)
180  {
181  outbag.write(it->getTopic(), it->getTime(), *it, it->getConnectionHeader());
182  if (progress)
183  ++(*progress);
184  }
185  outbag.close();
186  inbag.close();
187  return 0;
188 }
189 
190 int main(int argc, char** argv)
191 {
192  // Parse the command-line options
193  EncryptorOptions opts;
194  try
195  {
196  opts = parseOptions(argc, argv);
197  }
198  catch (const ros::Exception& ex)
199  {
200  ROS_ERROR("Error reading options: %s", ex.what());
201  return 1;
202  }
203  catch (const boost::regex_error& ex)
204  {
205  ROS_ERROR("Error reading options: %s\n", ex.what());
206  return 1;
207  }
208 
209  return encrypt(opts);
210 }
EncryptorOptions::plugin
std::string plugin
Definition: encrypt.cpp:60
rosbag::Bag
run
void run(class_loader::ClassLoader *loader)
rosbag::View::size
uint32_t size()
parseOptions
EncryptorOptions parseOptions(int argc, char **argv)
Parse the command-line arguments for encrypt options.
Definition: encrypt.cpp:80
EncryptorOptions::param
std::string param
Definition: encrypt.cpp:61
getStringCompressionType
std::string getStringCompressionType(rosbag::CompressionType compression)
Definition: encrypt.cpp:142
ros.h
rosbag::Bag::setEncryptorPlugin
void setEncryptorPlugin(const std::string &plugin_name, const std::string &plugin_param=std::string())
rosbag::compression::LZ4
LZ4
EncryptorOptions::buildOutbagName
void buildOutbagName()
Definition: encrypt.cpp:67
EncryptorOptions::compression
rosbag::CompressionType compression
Definition: encrypt.cpp:62
encrypt
int encrypt(EncryptorOptions const &options)
Definition: encrypt.cpp:153
ros::Exception
rosbag::Bag::close
void close()
bag.h
rosbag::compression::BZ2
BZ2
rosbag::bagmode::Read
Read
EncryptorOptions::outbag
std::string outbag
Definition: encrypt.cpp:64
rosbag::Bag::setCompression
void setCompression(CompressionType compression)
EncryptorOptions
Definition: encrypt.cpp:53
rosbag::View::const_iterator
iterator const_iterator
rosbag::bagmode::Write
Write
main
int main(int argc, char **argv)
Definition: encrypt.cpp:190
EncryptorOptions::EncryptorOptions
EncryptorOptions()
Definition: encrypt.cpp:55
rosbag::compression::CompressionType
CompressionType
view.h
rosbag::Bag::write
void write(std::string const &topic, ros::MessageEvent< T > const &event)
rosbag::View
ROS_ERROR
#define ROS_ERROR(...)
Uncompressed
Uncompressed
rosbag
rosbag::View::end
iterator end()
EncryptorOptions::quiet
bool quiet
Definition: encrypt.cpp:59
rosbag::View::begin
iterator begin()
EncryptorOptions::inbag
std::string inbag
Definition: encrypt.cpp:63


rosbag
Author(s): Tim Field, Jeremy Leibs, James Bowman, Dirk Thomas , Jacob Perron
autogenerated on Tue May 20 2025 03:00:39