| 1 |
// wunlha.cpp: a simple LZH decompressing 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/lzh_file.hpp> |
| 18 |
#include <hamigaki/filesystem/operations.hpp> |
| 19 |
#include <boost/filesystem/convenience.hpp> |
| 20 |
#include <boost/filesystem/fstream.hpp> |
| 21 |
#include <boost/iostreams/copy.hpp> |
| 22 |
#include <clocale> |
| 23 |
#include <exception> |
| 24 |
#include <iostream> |
| 25 |
|
| 26 |
namespace ar = hamigaki::archivers; |
| 27 |
namespace fs_ex = hamigaki::filesystem; |
| 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 |
inline fs_ex::timestamp make_timestamp(boost::uint64_t ft) |
| 58 |
{ |
| 59 |
return fs_ex::timestamp::from_windows_file_time(ft); |
| 60 |
} |
| 61 |
|
| 62 |
int main(int argc, char* argv[]) |
| 63 |
{ |
| 64 |
try |
| 65 |
{ |
| 66 |
if (argc != 2) |
| 67 |
{ |
| 68 |
std::cerr << "Usage: unlha (archive)" << std::endl; |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
std::setlocale(LC_ALL, ""); |
| 73 |
|
| 74 |
ar::wlzh_file_source lzh(argv[1]); |
| 75 |
|
| 76 |
while (lzh.next_entry()) |
| 77 |
{ |
| 78 |
const ar::lha::wheader& head = lzh.header(); |
| 79 |
|
| 80 |
std::wcout << head.path << '\n'; |
| 81 |
if (!is_valid_path(head.path)) |
| 82 |
{ |
| 83 |
std::cerr << "Warning: invalid path" << '\n'; |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
if (!head.link_path.empty()) |
| 88 |
{ |
| 89 |
if (::has_parent_path(head.path)) |
| 90 |
fs::create_directories(head.path.branch_path()); |
| 91 |
|
| 92 |
fs_ex::create_symlink(head.link_path, head.path); |
| 93 |
} |
| 94 |
else if (head.is_directory()) |
| 95 |
fs::create_directories(head.path); |
| 96 |
else |
| 97 |
{ |
| 98 |
if (::has_parent_path(head.path)) |
| 99 |
fs::create_directories(head.path.branch_path()); |
| 100 |
|
| 101 |
fs::ofstream os(head.path, std::ios_base::binary); |
| 102 |
io::copy(lzh, os); |
| 103 |
} |
| 104 |
|
| 105 |
// Note: |
| 106 |
// The POSIX chown() clears S_ISUID/S_ISGID bits. |
| 107 |
// So, we must call change_symlink_owner() |
| 108 |
// before calling change_permissions(). |
| 109 |
fs_ex::error_code ec; |
| 110 |
if (head.owner) |
| 111 |
{ |
| 112 |
fs_ex::change_symlink_owner( |
| 113 |
head.path, head.owner->uid, head.owner->gid, ec); |
| 114 |
} |
| 115 |
|
| 116 |
fs_ex::change_attributes(head.path, head.attributes, ec); |
| 117 |
|
| 118 |
if (head.permissions) |
| 119 |
fs_ex::change_permissions(head.path, *(head.permissions), ec); |
| 120 |
|
| 121 |
if (head.timestamp) |
| 122 |
{ |
| 123 |
const ar::lha::windows::timestamp ts(head.timestamp.get()); |
| 124 |
|
| 125 |
fs_ex::last_write_time( |
| 126 |
head.path, make_timestamp(ts.last_write_time)); |
| 127 |
fs_ex::last_access_time( |
| 128 |
head.path, make_timestamp(ts.last_access_time)); |
| 129 |
fs_ex::creation_time( |
| 130 |
head.path, make_timestamp(ts.creation_time)); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |
| 136 |
catch (const std::exception& e) |
| 137 |
{ |
| 138 |
std::cerr << "Error: " << e.what() << std::endl; |
| 139 |
} |
| 140 |
return 1; |
| 141 |
} |