cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
jar_file.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Jar file reader
4
5
Author: Diffblue Ltd
6
7
\*******************************************************************/
8
9
#include "
jar_file.h
"
10
11
#include <algorithm>
12
#include <cctype>
13
#include <sstream>
14
15
void
jar_filet::initialize_file_index
()
16
{
17
const
size_t
file_count=
m_zip_archive
.get_num_files();
18
for
(
size_t
index=0; index<file_count; index++)
19
m_name_to_index
.emplace(
m_zip_archive
.get_filename(index), index);
20
}
21
24
jar_filet::jar_filet
(
25
const
std::string &filename):
26
m_zip_archive
(filename)
27
{
28
initialize_file_index
();
29
}
30
34
jar_filet::jar_filet
(
35
const
void
*
data
,
36
size_t
size):
37
m_zip_archive
(
data
, size)
38
{
39
initialize_file_index
();
40
}
41
42
// VS: No default move constructors or assigns
43
44
jar_filet::jar_filet
(
jar_filet
&&other)
45
:
m_zip_archive
(
std
::move(other.
m_zip_archive
)),
46
m_name_to_index
(
std
::move(other.
m_name_to_index
))
47
{
48
}
49
50
jar_filet
&
jar_filet::operator=
(
jar_filet
&&other)
51
{
52
m_zip_archive
=std::move(other.m_zip_archive);
53
m_name_to_index
=std::move(other.m_name_to_index);
54
return
*
this
;
55
}
56
57
std::optional<std::string>
jar_filet::get_entry
(
const
std::string &name)
58
{
59
const
auto
entry=
m_name_to_index
.find(name);
60
if
(entry==
m_name_to_index
.end())
61
return
{};
62
63
try
64
{
65
return
m_zip_archive
.extract(entry->second);
66
}
67
catch
(
const
std::runtime_error &)
68
{
69
return
{};
70
}
71
}
72
77
static
bool
is_space
(
const
char
ch)
78
{
79
return
std::isspace(ch) != 0;
80
}
81
87
static
std::string
trim
(
88
const
std::string::const_iterator begin,
89
const
std::string::const_iterator end)
90
{
91
const
auto
out_begin=std::find_if_not(begin, end,
is_space
);
92
const
auto
out_end=std::find_if_not(
93
std::string::const_reverse_iterator(end),
94
std::string::const_reverse_iterator(out_begin),
95
is_space
).base();
96
return
{ out_begin, out_end };
97
}
98
99
std::unordered_map<std::string, std::string>
jar_filet::get_manifest
()
100
{
101
const
auto
entry=
get_entry
(
"META-INF/MANIFEST.MF"
);
102
103
if
(!entry.has_value())
104
return
{};
105
106
std::unordered_map<std::string, std::string> out;
107
std::istringstream in(*entry);
108
std::string line;
109
while
(std::getline(in, line))
110
{
111
const
auto
key_end=std::find(line.cbegin(), line.cend(),
':'
);
112
if
(key_end!=line.cend())
113
{
114
out.emplace(
115
trim
(line.cbegin(), key_end),
116
trim
(std::next(key_end), line.cend()));
117
}
118
}
119
120
return
out;
121
}
122
123
std::vector<std::string>
jar_filet::filenames
()
const
124
{
125
std::vector<std::string> out;
126
for
(
const
auto
&pair :
m_name_to_index
)
127
out.emplace_back(pair.first);
128
return
out;
129
}
jar_filet::m_zip_archive
mz_zip_archivet m_zip_archive
Definition
jar_file.h:58
jar_filet::m_name_to_index
std::unordered_map< std::string, size_t > m_name_to_index
Map of filename to the file index in the zip archive.
Definition
jar_file.h:61
jar_filet::get_entry
std::optional< std::string > get_entry(const std::string &filename)
Get contents of a file in the jar archive.
Definition
jar_file.cpp:57
jar_filet::filenames
std::vector< std::string > filenames() const
Get list of filenames in the archive.
Definition
jar_file.cpp:123
jar_filet::jar_filet
jar_filet(const std::string &filename)
Open java file for reading.
Definition
jar_file.cpp:24
jar_filet::get_manifest
std::unordered_map< std::string, std::string > get_manifest()
Get contents of the Manifest file in the jar archive as a key-value map (both as strings).
Definition
jar_file.cpp:99
jar_filet::initialize_file_index
void initialize_file_index()
Loads the fileindex (m_name_to_index) with a map of loaded files to indices.
Definition
jar_file.cpp:15
jar_filet::operator=
jar_filet & operator=(const jar_filet &)=delete
trim
static std::string trim(const std::string::const_iterator begin, const std::string::const_iterator end)
Remove leading and trailing whitespace characters from string.
Definition
jar_file.cpp:87
is_space
static bool is_space(const char ch)
Wrapper for std::isspace from cctype.
Definition
jar_file.cpp:77
jar_file.h
std
STL namespace.
data
Definition
kdev_t.h:24
jbmc
src
java_bytecode
jar_file.cpp
Generated by
1.17.0