CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mz_zip_archive.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: mz_zip library wrapper
4
5Author: Diffblue Ltd
6
7\*******************************************************************/
8
9#include "mz_zip_archive.h"
10#include <stdexcept>
11#include <string>
12#define _LARGEFILE64_SOURCE 1
13#include <miniz/miniz.h>
14
15// Original struct is an anonymous struct with a typedef, This is
16// required to remove internals from the header file
18{
19public:
20 explicit mz_zip_archive_statet(const std::string &filename):
22 {
23 if(MZ_TRUE!=mz_zip_reader_init_file(this, filename.data(), 0))
24 throw std::runtime_error("MZT: Could not load a file: "+filename);
25 }
26
27 mz_zip_archive_statet(const void *data, size_t size):
29 {
30 if(MZ_TRUE!=mz_zip_reader_init_mem(this, data, size, 0))
31 throw std::runtime_error("MZT: Could not load data from memory");
32 }
33
42};
43
44static_assert(sizeof(mz_uint)<=sizeof(size_t),
45 "size_t cannot store mz_zip file ids, choose a larger type");
46
47mz_zip_archivet::mz_zip_archivet(const std::string &filename):
48 m_state(new mz_zip_archive_statet(filename)) { }
49
50mz_zip_archivet::mz_zip_archivet(const void *data, size_t size):
51 m_state(new mz_zip_archive_statet(data, size)) { }
52
53// VS Compatibility
55 m_state(std::move(other.m_state)) { }
56
57// Has to be defined here because header is incomplete
59
60// VS Compatibility
62{
63 m_state=std::move(other.m_state);
64 return *this;
65}
66
71
72std::string mz_zip_archivet::get_filename(const size_t index)
73{
74 const auto id = static_cast<mz_uint>(index);
75 mz_uint name_size = mz_zip_reader_get_filename(m_state.get(), id, nullptr, 0);
76 if(name_size == 0)
77 return {}; // Failure
78 // It is valid to directly write to a string's buffer (see C++11 standard,
79 // basic_string general requirements [string.require], 21.4.1.5)
80 std::string buffer(name_size, '\0');
81 mz_zip_reader_get_filename(m_state.get(), id, &buffer[0], buffer.size());
82 // Buffer contains trailing \0
83 buffer.resize(name_size - 1);
84 return buffer;
85}
86
87std::string mz_zip_archivet::extract(const size_t index)
88{
89 const auto id=static_cast<mz_uint>(index);
90 mz_zip_archive_file_stat file_stat={ };
91 const mz_bool stat_ok=mz_zip_reader_file_stat(m_state.get(), id, &file_stat);
92 if(stat_ok==MZ_TRUE)
93 {
94 // It is valid to directly write to a string's buffer (see C++11 standard,
95 // basic_string general requirements [string.require], 21.4.1.5)
96 std::string buffer(file_stat.m_uncomp_size, '\0');
98 m_state.get(), id, &buffer[0], buffer.size(), 0);
99 if(read_ok == MZ_TRUE)
100 return buffer;
101 }
102 throw std::runtime_error("Could not extract the file");
103}
104
106 const size_t index,
107 const std::string &path)
108{
109 const auto id = static_cast<mz_uint>(index);
110 if(
111 mz_zip_reader_extract_to_file(m_state.get(), id, path.c_str(), 0) !=
112 MZ_TRUE)
113 {
114 throw std::runtime_error("Could not extract the file");
115 }
116}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
mz_zip_archive_statet(mz_zip_archive_statet &&)=delete
mz_zip_archive_statet(const std::string &filename)
mz_zip_archive_statet(const void *data, size_t size)
mz_zip_archive_statet(const mz_zip_archive_statet &)=delete
mz_zip_archive_statet & operator=(mz_zip_archive_statet &&)=delete
mz_zip_archive_statet & operator=(const mz_zip_archive_statet &)=delete
Thin object-oriented wrapper around the MZ Zip library Zip file reader and extractor.
size_t get_num_files()
Get number of files in the archive.
std::string extract(size_t index)
Get contents of nth file in the archive.
void extract_to_file(size_t index, const std::string &path)
Write contents of nth file in the archive to a file.
std::string get_filename(size_t index)
Get file name of nth file in the archive.
std::unique_ptr< mz_zip_archive_statet > m_state
mz_zip_archivet(const std::string &filename)
Open a zip archive.
mz_zip_archivet & operator=(const mz_zip_archivet &)=delete
mz_bool mz_zip_reader_end(mz_zip_archive *pZip)
Definition miniz.cpp:4036
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip)
Definition miniz.cpp:7842
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags)
Definition miniz.cpp:4116
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags)
Definition miniz.cpp:4068
mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags)
Definition miniz.cpp:4728
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat)
Definition miniz.cpp:7897
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags)
Definition miniz.cpp:5301
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size)
Definition miniz.cpp:7876
uint32_t mz_uint
Definition miniz.h:621
#define MZ_TRUE
Definition miniz.h:627
STL namespace.
mz_uint64 m_uncomp_size
Definition miniz.h:1096