CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
jar_file.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Jar file reader
4
5Author: Diffblue Ltd
6
7\*******************************************************************/
8
9#include "jar_file.h"
10
11#include <algorithm>
12#include <cctype>
13#include <sstream>
14
16{
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
25 const std::string &filename):
26 m_zip_archive(filename)
27{
29}
30
35 const void *data,
36 size_t size):
37 m_zip_archive(data, size)
38{
40}
41
42// VS: No default move constructors or assigns
43
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
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
57std::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
77static bool is_space(const char ch)
78{
79 return std::isspace(ch) != 0;
80}
81
87static 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
99std::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
123std::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}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Class representing a .jar archive.
Definition jar_file.h:22
mz_zip_archivet m_zip_archive
Definition jar_file.h:58
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
std::optional< std::string > get_entry(const std::string &filename)
Get contents of a file in the jar archive.
Definition jar_file.cpp:57
std::vector< std::string > filenames() const
Get list of filenames in the archive.
Definition jar_file.cpp:123
jar_filet(const std::string &filename)
Open java file for reading.
Definition jar_file.cpp:24
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
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=(const jar_filet &)=delete
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.
std::string get_filename(size_t index)
Get file name of nth file in the archive.
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
static bool is_space(const char ch)
Wrapper for std::isspace from cctype
Definition jar_file.cpp:77
STL namespace.