cprover
Loading...
Searching...
No Matches
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#include <algorithm>
13#define _LARGEFILE64_SOURCE 1
14#include <miniz/miniz.h>
15
16// Original struct is an anonymous struct with a typedef, This is
17// required to remove internals from the header file
19{
20public:
21 explicit mz_zip_archive_statet(const std::string &filename):
23 {
24 if(MZ_TRUE!=mz_zip_reader_init_file(this, filename.data(), 0))
25 throw std::runtime_error("MZT: Could not load a file: "+filename);
26 }
27
28 mz_zip_archive_statet(const void *data, size_t size):
30 {
31 if(MZ_TRUE!=mz_zip_reader_init_mem(this, data, size, 0))
32 throw std::runtime_error("MZT: Could not load data from memory");
33 }
34
43};
44
45static_assert(sizeof(mz_uint)<=sizeof(size_t),
46 "size_t cannot store mz_zip file ids, choose a larger type");
47
48mz_zip_archivet::mz_zip_archivet(const std::string &filename):
49 m_state(new mz_zip_archive_statet(filename)) { }
50
51mz_zip_archivet::mz_zip_archivet(const void *data, size_t size):
52 m_state(new mz_zip_archive_statet(data, size)) { }
53
54// VS Compatibility
56 m_state(std::move(other.m_state)) { }
57
58// Has to be defined here because header is incomplete
60
61// VS Compatibility
63{
64 m_state=std::move(other.m_state);
65 return *this;
66}
67
72
73std::string mz_zip_archivet::get_filename(const size_t index)
74{
75 const auto id = static_cast<mz_uint>(index);
76 mz_uint name_size = mz_zip_reader_get_filename(m_state.get(), id, nullptr, 0);
77 if(name_size == 0)
78 return {}; // Failure
79 // It is valid to directly write to a string's buffer (see C++11 standard,
80 // basic_string general requirements [string.require], 21.4.1.5)
81 std::string buffer(name_size, '\0');
82 mz_zip_reader_get_filename(m_state.get(), id, &buffer[0], buffer.size());
83 // Buffer contains trailing \0
84 buffer.resize(name_size - 1);
85 return buffer;
86}
87
88std::string mz_zip_archivet::extract(const size_t index)
89{
90 const auto id=static_cast<mz_uint>(index);
93 if(stat_ok==MZ_TRUE)
94 {
95 // It is valid to directly write to a string's buffer (see C++11 standard,
96 // basic_string general requirements [string.require], 21.4.1.5)
97 std::string buffer(file_stat.m_uncomp_size, '\0');
99 m_state.get(), id, &buffer[0], buffer.size(), 0);
100 if(read_ok == MZ_TRUE)
101 return buffer;
102 }
103 throw std::runtime_error("Could not extract the file");
104}
105
107 const size_t index,
108 const std::string &path)
109{
110 const auto id = static_cast<mz_uint>(index);
111 if(
112 mz_zip_reader_extract_to_file(m_state.get(), id, path.c_str(), 0) !=
113 MZ_TRUE)
114 {
115 throw std::runtime_error("Could not extract the file");
116 }
117}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:564
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:3742
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip)
Definition miniz.cpp:7133
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags)
Definition miniz.cpp:3821
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags)
Definition miniz.cpp:3774
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:4416
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat)
Definition miniz.cpp:7188
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:4695
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:7167
unsigned int mz_uint
Definition miniz.h:538
#define MZ_TRUE
Definition miniz.h:544
STL namespace.
Definition kdev_t.h:24