From 35a4692f53489508bb20e6539728244628c96ce0 Mon Sep 17 00:00:00 2001 From: Domen Vrankar Date: Tue, 17 Jun 2014 00:09:12 +0200 Subject: [PATCH] -(add) initial archive reader wrapper --- CMakeLists.txt | 25 +++++ archive_exception.cpp | 16 +++ archive_exception.hpp | 22 ++++ archive_reader.cpp | 151 ++++++++++++++++++++++++++++ archive_reader.hpp | 83 +++++++++++++++ archive_reader.tpp | 52 ++++++++++ archive_reader_entry.cpp | 84 ++++++++++++++++ archive_reader_entry.hpp | 68 +++++++++++++ archive_reader_entry_buffer.cpp | 65 ++++++++++++ archive_reader_entry_buffer.hpp | 54 ++++++++++ archive_reader_file_container.cpp | 52 ++++++++++ archive_reader_file_container.hpp | 53 ++++++++++ archive_reader_filter.hpp | 54 ++++++++++ archive_reader_format.hpp | 55 ++++++++++ archive_reader_iterator.cpp | 64 ++++++++++++ archive_reader_iterator.hpp | 56 +++++++++++ archive_reader_memory_container.cpp | 54 ++++++++++ archive_reader_memory_container.hpp | 53 ++++++++++ 18 files changed, 1061 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 archive_exception.cpp create mode 100644 archive_exception.hpp create mode 100644 archive_reader.cpp create mode 100644 archive_reader.hpp create mode 100644 archive_reader.tpp create mode 100644 archive_reader_entry.cpp create mode 100644 archive_reader_entry.hpp create mode 100644 archive_reader_entry_buffer.cpp create mode 100644 archive_reader_entry_buffer.hpp create mode 100644 archive_reader_file_container.cpp create mode 100644 archive_reader_file_container.hpp create mode 100644 archive_reader_filter.hpp create mode 100644 archive_reader_format.hpp create mode 100644 archive_reader_iterator.cpp create mode 100644 archive_reader_iterator.hpp create mode 100644 archive_reader_memory_container.cpp create mode 100644 archive_reader_memory_container.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f058cc3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required( VERSION 2.8.12 ) + +project(libarchive_cpp_wrapper) + +set(CMAKE_CXX_FLAGS "-Wall -std=c++11") + +file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") +file(GLOB template_implementations "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp") + +add_library( ${PROJECT_NAME} SHARED + + archive_reader.cpp + archive_reader_entry.cpp + archive_reader_iterator.cpp + archive_reader_file_container.cpp + archive_reader_memory_container.cpp + archive_reader_entry_buffer.cpp + + archive_exception.cpp + + ${headers} + ${template_implementations} +) + +target_link_libraries( ${PROJECT_NAME} /usr/lib/x86_64-linux-gnu/libarchive.so.13 ) diff --git a/archive_exception.cpp b/archive_exception.cpp new file mode 100644 index 0000000..fe7b8fc --- /dev/null +++ b/archive_exception.cpp @@ -0,0 +1,16 @@ +#include "archive_exception.hpp" + +namespace ns_archive { + +archive_exception::archive_exception(const std::string& what) : + _what(what) +{ + // +} + +const char* archive_exception::what() +{ + return _what.c_str(); +} + +} diff --git a/archive_exception.hpp b/archive_exception.hpp new file mode 100644 index 0000000..5b3b76e --- /dev/null +++ b/archive_exception.hpp @@ -0,0 +1,22 @@ +#ifndef ARCHIVE_EXCEPTION_HPP_INCLUDED +#define ARCHIVE_EXCEPTION_HPP_INCLUDED + +#include +#include + +namespace ns_archive { + +class archive_exception : public std::exception +{ +public: + archive_exception(const std::string& what); + + const char* what(); + +private: + std::string _what; +}; + +} + +#endif // ARCHIVE_EXCEPTION_HPP_INCLUDED diff --git a/archive_reader.cpp b/archive_reader.cpp new file mode 100644 index 0000000..dabd822 --- /dev/null +++ b/archive_reader.cpp @@ -0,0 +1,151 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "archive_reader.hpp" + +#include "archive_reader_file_container.hpp" +#include "archive_reader_memory_container.hpp" +#include "archive_exception.hpp" + +#include + +namespace ns_archive { + +reader::reader() : + _archive( archive_read_new(), [](archive* archive){ archive_read_free(archive); } ) // errors in destructor will be silently ignored +{ + // +} + +ns_reader::entry* reader::get_next_entry() +{ + if(!has_next_entry()) + { + throw archive_exception( "get_next_entry was called after all the entries were read" ); + } + + ns_reader::entry* entry = _next_entry; + _next_entry = nullptr; + + return entry; +} + +bool reader::has_next_entry() +{ + bool has_next = true; + + if(_next_entry == nullptr) + { + archive_entry* entry; + has_next = (archive_read_next_header(_archive.get(), &entry) == ARCHIVE_OK); + + if(has_next) + { + _next_entry = new ns_reader::entry(_archive.get(), entry); + } + } + + return has_next; +} + +/// ---------------- init_format ---------------- // + +#define READER_INIT_FORMAT(__FORMAT__, __FUNCTION_SUFFIX__) \ +template<> \ +void reader::init_format()\ +{\ + archive_read_support_format_##__FUNCTION_SUFFIX__(_archive.get());\ +} + +READER_INIT_FORMAT(RAW, raw) +READER_INIT_FORMAT(ALL, all) +READER_INIT_FORMAT(7ZIP, 7zip) +READER_INIT_FORMAT(AR, ar) +READER_INIT_FORMAT(CAB, cab) +READER_INIT_FORMAT(CPIO, cpio) +READER_INIT_FORMAT(ISO9660, iso9660) +READER_INIT_FORMAT(LHA, lha) +READER_INIT_FORMAT(MTREE, mtree) +READER_INIT_FORMAT(RAR, rar) +READER_INIT_FORMAT(TAR, tar) +READER_INIT_FORMAT(XAR, xar) +READER_INIT_FORMAT(ZIP, zip) + +/// ---------------- init_filter ---------------- // + +#define READER_INIT_FILTER(__FILTER__, __FUNCTION_SUFFIX__) \ +template<> \ +void reader::init_filter()\ +{\ + archive_read_support_filter_##__FUNCTION_SUFFIX__(_archive.get());\ +} + +READER_INIT_FILTER(ALL, all) +READER_INIT_FILTER(BZIP2, bzip2) +READER_INIT_FILTER(COMPRESS, compress) +READER_INIT_FILTER(GZIP, gzip) +READER_INIT_FILTER(LZIP, lzip) +READER_INIT_FILTER(LZMA, lzma) +READER_INIT_FILTER(XZ, xz) +READER_INIT_FILTER(UU, uu) +READER_INIT_FILTER(RPM, rpm) +READER_INIT_FILTER(LRZIP, lrzip) +READER_INIT_FILTER(LZOP, lzop) +READER_INIT_FILTER(GRZIP, gzip) + +/// ---------------- init_data ---------------- // +template<> +void reader::init_data(ns_reader::file_container&& container) +{ + if(archive_read_open_filename(_archive.get(), container.get_path().c_str(), container.get_block_size()) != ARCHIVE_OK) + { + throw archive_exception( "Failed to open archive '" + container.get_path() + "'" ); + } +} + +template<> +void reader::init_data(ns_reader::memory_container&& container) +{ + std::vector content = container.move_get_buffer(); + if(archive_read_open_memory(_archive.get(), content.data(), content.size()) != ARCHIVE_OK) + { + throw archive_exception( "Failed to open memory archive" ); + } +} + +ns_reader::iterator reader::begin() +{ + return ns_reader::iterator( this, false ); +} + +ns_reader::iterator reader::end() +{ + return ns_reader::iterator( this, true ); +} + +} diff --git a/archive_reader.hpp b/archive_reader.hpp new file mode 100644 index 0000000..8f78ae8 --- /dev/null +++ b/archive_reader.hpp @@ -0,0 +1,83 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_HPP_INCLUDED +#define ARCHIVE_READER_HPP_INCLUDED + +#include +#include +#include + +#include "archive_reader_format.hpp" +#include "archive_reader_filter.hpp" +#include "archive_reader_entry.hpp" +#include "archive_reader_iterator.hpp" + +namespace ns_archive { + +class reader +{ +public: + reader(reader&&) = default; + + reader(const reader&) = delete; + reader& operator=(const reader&) = delete; + + template + static reader make_reader(DATA_CONTAINER&& container); + + template + static reader make_reader(DATA_CONTAINER&& container); + + ns_reader::entry* get_next_entry(); + bool has_next_entry(); + + ns_reader::iterator begin(); + ns_reader::iterator end(); + +private: + reader(); + + template + void init_format(); + + template + void init_filter(); + + template + void init_data(DATA_CONTAINER&& container); + + std::shared_ptr _archive; + ns_reader::entry *_next_entry = nullptr; +}; + +} + +#include "archive_reader.tpp" + +#endif // ARCHIVE_READER_HPP_INCLUDED diff --git a/archive_reader.tpp b/archive_reader.tpp new file mode 100644 index 0000000..9d98478 --- /dev/null +++ b/archive_reader.tpp @@ -0,0 +1,52 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +namespace ns_archive { + +template +reader reader::make_reader(DATA_CONTAINER&& container) +{ + reader a_reader; + a_reader.init_format(); + a_reader.init_filter(); + a_reader.init_data(std::move(container)); + + return a_reader; +} + +template +reader reader::make_reader(DATA_CONTAINER&& container) +{ + reader a_reader; + a_reader.init_format(); + a_reader.init_data(std::move(container)); + + return a_reader; +} + +} diff --git a/archive_reader_entry.cpp b/archive_reader_entry.cpp new file mode 100644 index 0000000..f6dab93 --- /dev/null +++ b/archive_reader_entry.cpp @@ -0,0 +1,84 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "archive_reader_entry.hpp" + +#include "archive_exception.hpp" + +namespace ns_archive { +namespace ns_reader { + +entry::entry(archive* archive, archive_entry* entry) : + _archive(archive), + _entry(entry), + _entry_buffer(_archive), + _entry_stream(&_entry_buffer) +{ + // +} + +// -------------------------------- // +template<> +std::string entry::get_header_value() const +{ + return archive_entry_hardlink(_entry); +} + +template<> +std::string entry::get_header_value() const +{ + return archive_entry_pathname(_entry); +} + +template<> +std::string entry::get_header_value() const +{ + return archive_entry_sourcepath(_entry); +} + +template<> +std::string entry::get_header_value() const +{ + return archive_entry_symlink(_entry); +} + +// -------------------------------- // +std::istream& entry::get_entry_content_stream() +{ + if(_already_requested_content_stream) + { + throw archive_exception( "get_entry_content_stream requested more than once for the same entry" ); + } + + _already_requested_content_stream = true; + + return _entry_stream; +} + +} +} diff --git a/archive_reader_entry.hpp b/archive_reader_entry.hpp new file mode 100644 index 0000000..b39ab30 --- /dev/null +++ b/archive_reader_entry.hpp @@ -0,0 +1,68 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_ENTRY_HPP_INCLUDED +#define ARCHIVE_READER_ENTRY_HPP_INCLUDED + +#include +#include +#include "archive_entry.h" +#include "archive_reader_entry_buffer.hpp" + +namespace ns_archive { +namespace ns_reader { + +class entry +{ +public: + enum class string_value + { + HARDLINK, + PATHNAME, + SOURCEPATH, + SYMLINK + }; +public: + entry(archive* archive, archive_entry* entry); + + template + std::string get_header_value() const; + + std::istream& get_entry_content_stream(); +private: + archive *_archive; + archive_entry *_entry; + entry_buffer _entry_buffer; + std::istream _entry_stream; + bool _already_requested_content_stream = false; +}; + +} +} + +#endif // ARCHIVE_READER_ENTRY_HPP_INCLUDED diff --git a/archive_reader_entry_buffer.cpp b/archive_reader_entry_buffer.cpp new file mode 100644 index 0000000..2cce216 --- /dev/null +++ b/archive_reader_entry_buffer.cpp @@ -0,0 +1,65 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "archive_reader_entry_buffer.hpp" + +#include "archive_exception.hpp" + +namespace ns_archive { +namespace ns_reader { + +entry_buffer::entry_buffer(archive* archive) : + _archive(archive) +{ + // +} + +int entry_buffer::underflow() +{ + if(gptr() == egptr()) + { + size_t _buff_size = archive_read_data(_archive, _buff, _buff_max_size); + + if( _buff_size < 0 ) + { + throw archive_exception( "Archive reader buffer reading error." ); + } + + if( _buff_size ) + { + setg(_buff, _buff, _buff + _buff_size); + } + } + + return this->gptr() == this->egptr() + ? std::char_traits::eof() + : std::char_traits::to_int_type(*gptr()); +} + +} +} diff --git a/archive_reader_entry_buffer.hpp b/archive_reader_entry_buffer.hpp new file mode 100644 index 0000000..482717d --- /dev/null +++ b/archive_reader_entry_buffer.hpp @@ -0,0 +1,54 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_ENTRY_BUFFER_HPP_INCLUDED +#define ARCHIVE_READER_ENTRY_BUFFER_HPP_INCLUDED + +#include +#include "archive.h" + +namespace ns_archive { +namespace ns_reader { + +class entry_buffer : public std::streambuf +{ +public: + entry_buffer(archive* archive); + + int underflow(); + +private: + archive *_archive; + size_t _buff_max_size = 8192; + char _buff[8192]; +}; + +} +} + +#endif // ARCHIVE_READER_ENTRY_BUFFER_HPP_INCLUDED diff --git a/archive_reader_file_container.cpp b/archive_reader_file_container.cpp new file mode 100644 index 0000000..abdd872 --- /dev/null +++ b/archive_reader_file_container.cpp @@ -0,0 +1,52 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "archive_reader_file_container.hpp" + +namespace ns_archive { +namespace ns_reader { + +file_container::file_container(const std::string& file_path, size_t block_size) : + _file_path(file_path), + _block_size(block_size) +{ + // +} + +const std::string& file_container::get_path() const +{ + return _file_path; +} + +size_t file_container::get_block_size() const +{ + return _block_size; +} + +} +} diff --git a/archive_reader_file_container.hpp b/archive_reader_file_container.hpp new file mode 100644 index 0000000..9d90b67 --- /dev/null +++ b/archive_reader_file_container.hpp @@ -0,0 +1,53 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_FILE_CONTAINER_HPP_INCLUDED +#define ARCHIVE_READER_FILE_CONTAINER_HPP_INCLUDED + +#include + +namespace ns_archive { +namespace ns_reader { + +class file_container +{ +public: + file_container(const std::string& file_path, size_t block_size); + + const std::string& get_path() const; + size_t get_block_size() const; + +private: + std::string _file_path; + size_t _block_size; +}; + +} +} + +#endif // ARCHIVE_READER_FILE_CONTAINER_HPP_INCLUDED diff --git a/archive_reader_filter.hpp b/archive_reader_filter.hpp new file mode 100644 index 0000000..5dce5e7 --- /dev/null +++ b/archive_reader_filter.hpp @@ -0,0 +1,54 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_FILTER_HPP_INCLUDED +#define ARCHIVE_READER_FILTER_HPP_INCLUDED + +namespace ns_archive { +namespace ns_reader { + +enum class filter +{ + _ALL, + _BZIP2, + _COMPRESS, + _GZIP, + _LZIP, + _LZMA, + _XZ, + _UU, + _RPM, + _LRZIP, + _LZOP, + _GRZIP +}; + +} +} + +#endif // ARCHIVE_READER_FILTER_HPP_INCLUDED diff --git a/archive_reader_format.hpp b/archive_reader_format.hpp new file mode 100644 index 0000000..9b8787f --- /dev/null +++ b/archive_reader_format.hpp @@ -0,0 +1,55 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_FORMAT_HPP_INCLUDED +#define ARCHIVE_READER_FORMAT_HPP_INCLUDED + +namespace ns_archive { +namespace ns_reader { + +enum class format +{ + _RAW, + _ALL, + _7ZIP, + _AR, + _CAB, + _CPIO, + _ISO9660, + _LHA, + _MTREE, + _RAR, + _TAR, + _XAR, + _ZIP +}; + +} +} + +#endif // ARCHIVE_READER_FORMAT_HPP_INCLUDED diff --git a/archive_reader_iterator.cpp b/archive_reader_iterator.cpp new file mode 100644 index 0000000..6878031 --- /dev/null +++ b/archive_reader_iterator.cpp @@ -0,0 +1,64 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "archive_reader_iterator.hpp" + +#include "archive_reader.hpp" + +namespace ns_archive { +namespace ns_reader { + +iterator::iterator(reader* p_reader, bool end_pos) : + _end_pos( end_pos ), + _p_reader( p_reader ) +{ + // +} + +bool iterator::operator!=(const iterator& other) const +{ + return _end_pos != other._end_pos; +} + +entry* iterator::operator*() +{ + return _p_reader->get_next_entry(); +} + +const iterator& iterator::operator++() +{ + if(!_p_reader->has_next_entry()) + { + _end_pos = true; + } + + return *this; +} + +} +} diff --git a/archive_reader_iterator.hpp b/archive_reader_iterator.hpp new file mode 100644 index 0000000..5cc24c0 --- /dev/null +++ b/archive_reader_iterator.hpp @@ -0,0 +1,56 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_ITERATOR_HPP_INCLUDED +#define ARCHIVE_READER_ITERATOR_HPP_INCLUDED + +#include "archive_reader_entry.hpp" + +namespace ns_archive { + +class reader; + +namespace ns_reader { + +class iterator +{ +public: + iterator(reader* p_reader, bool end_pos); + bool operator!=(const iterator& other) const; + entry* operator*(); + const iterator& operator++(); + +private: + bool _end_pos; + reader *_p_reader; +}; + +} +} + +#endif // ARCHIVE_READER_ITERATOR_HPP_INCLUDED diff --git a/archive_reader_memory_container.cpp b/archive_reader_memory_container.cpp new file mode 100644 index 0000000..d0782cb --- /dev/null +++ b/archive_reader_memory_container.cpp @@ -0,0 +1,54 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "archive_reader_memory_container.hpp" + +#include +#include + +namespace ns_archive { +namespace ns_reader { + +memory_container::memory_container(std::istream& input_stream) +{ + std::copy(std::istream_iterator(input_stream), std::istream_iterator(), std::back_inserter(_buffer)); +} + +memory_container::memory_container(std::vector&& buffer) : + _buffer(std::forward>(buffer)) +{ + // +} + +std::vector&& memory_container::move_get_buffer() +{ + return std::move(_buffer); +} + +} +} diff --git a/archive_reader_memory_container.hpp b/archive_reader_memory_container.hpp new file mode 100644 index 0000000..7863ef7 --- /dev/null +++ b/archive_reader_memory_container.hpp @@ -0,0 +1,53 @@ +/* +BSD 2-Clause license + +Copyright (c) 2014, Domen Vrankar +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef ARCHIVE_READER_MEMORY_CONTAINER_HPP_INCLUDED +#define ARCHIVE_READER_MEMORY_CONTAINER_HPP_INCLUDED + +#include +#include + +namespace ns_archive { +namespace ns_reader { + +class memory_container +{ +public: + memory_container(std::istream& input_stream); + memory_container(std::vector&& buffer); + + std::vector&& move_get_buffer(); + +private: + std::vector _buffer; +}; + +} +} + +#endif // ARCHIVE_READER_MEMORY_CONTAINER_HPP_INCLUDED