| 1 |
// uniso.cpp: a simple ISO image extractor program (Unicode version) |
| 2 |
|
| 3 |
// Copyright Takeshi Mouri 2008-2018. |
| 4 |
// Distributed under the Boost Software License, Version 1.0. |
| 5 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 6 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 7 |
|
| 8 |
// See http://hamigaki.sourceforge.jp/libs/archivers for library home page. |
| 9 |
|
| 10 |
|
| 11 |
// Security warning: |
| 12 |
// This program never check the validity of paths in the archive. |
| 13 |
// See http://www.forest.impress.co.jp/article/2004/07/30/arcsecurity.html . |
| 14 |
// (The above link is Japanese site) |
| 15 |
|
| 16 |
|
| 17 |
#include <hamigaki/archivers/iso_file.hpp> |
| 18 |
#include <boost/filesystem/convenience.hpp> |
| 19 |
#include <boost/filesystem/fstream.hpp> |
| 20 |
#include <boost/iostreams/copy.hpp> |
| 21 |
#include <boost/version.hpp> |
| 22 |
#include <clocale> |
| 23 |
#include <exception> |
| 24 |
#include <functional> |
| 25 |
#include <iostream> |
| 26 |
|
| 27 |
namespace ar = hamigaki::archivers; |
| 28 |
namespace io_ex = hamigaki::iostreams; |
| 29 |
namespace fs = boost::filesystem; |
| 30 |
namespace io = boost::iostreams; |
| 31 |
|
| 32 |
template<class Path> |
| 33 |
inline bool has_parent_path(const Path& ph) |
| 34 |
{ |
| 35 |
#if BOOST_VERSION < 103600 |
| 36 |
return ph.has_branch_path(); |
| 37 |
#else |
| 38 |
return ph.has_parent_path(); |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
template<class Path> |
| 43 |
bool is_valid_path(const Path& ph) |
| 44 |
{ |
| 45 |
#if !defined(HAMIGAKI_ALLOW_DIRECTORY_TRAVERSAL) |
| 46 |
if (ph.has_root_name() || ph.has_root_directory()) |
| 47 |
return false; |
| 48 |
for (typename Path::iterator it = ph.begin(); it != ph.end(); ++it) |
| 49 |
{ |
| 50 |
if (*it == L"..") |
| 51 |
return false; |
| 52 |
} |
| 53 |
#endif |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
int main(int argc, char* argv[]) |
| 58 |
{ |
| 59 |
try |
| 60 |
{ |
| 61 |
if (argc != 2) |
| 62 |
{ |
| 63 |
std::cerr << "Usage: uniso (archive)" << std::endl; |
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
std::setlocale(LC_ALL, ""); |
| 68 |
|
| 69 |
ar::wiso_file_source iso(argv[1]); |
| 70 |
|
| 71 |
typedef std::vector<ar::iso::wvolume_desc> descs_type; |
| 72 |
const descs_type& descs = iso.volume_descs(); |
| 73 |
|
| 74 |
descs_type::const_iterator rr = std::find_if( |
| 75 |
descs.begin(), descs.end(), |
| 76 |
std::mem_fun_ref(&ar::iso::wvolume_desc::is_rock_ridge) |
| 77 |
); |
| 78 |
|
| 79 |
descs_type::const_iterator jol = std::find_if( |
| 80 |
descs.begin(), descs.end(), |
| 81 |
std::mem_fun_ref(&ar::iso::wvolume_desc::is_joliet) |
| 82 |
); |
| 83 |
|
| 84 |
if (jol != descs.end()) |
| 85 |
iso.select_volume_desc(jol - descs.begin()); |
| 86 |
else if (rr != descs.end()) |
| 87 |
iso.select_volume_desc(rr - descs.begin()); |
| 88 |
else |
| 89 |
iso.select_volume_desc(0); |
| 90 |
|
| 91 |
while (iso.next_entry()) |
| 92 |
{ |
| 93 |
const ar::iso::wheader& head = iso.header(); |
| 94 |
|
| 95 |
std::wcout << head.path << '\n'; |
| 96 |
if (!is_valid_path(head.path)) |
| 97 |
{ |
| 98 |
std::cerr << "Warning: invalid path" << '\n'; |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if (head.is_symlink()) |
| 103 |
std::wcout << "-> " << head.link_path << '\n'; |
| 104 |
else if (head.is_directory()) |
| 105 |
fs::create_directories(head.path); |
| 106 |
else if (head.is_regular()) |
| 107 |
{ |
| 108 |
if (::has_parent_path(head.path)) |
| 109 |
fs::create_directories(head.path.branch_path()); |
| 110 |
|
| 111 |
fs::ofstream os(head.path, std::ios_base::binary); |
| 112 |
io::copy(iso, os); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
catch (const std::exception& e) |
| 119 |
{ |
| 120 |
std::cerr << "Error: " << e.what() << std::endl; |
| 121 |
} |
| 122 |
return 1; |
| 123 |
} |