cprover
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 
14 #include <util/invariant.h>
15 #include <util/suffix.h>
16 
18 
20 {
21  const size_t file_count=m_zip_archive.get_num_files();
22  for(size_t index=0; index<file_count; index++)
23  m_name_to_index.emplace(m_zip_archive.get_filename(index), index);
24 }
25 
29  const std::string &filename):
30  m_zip_archive(filename)
31 {
33 }
34 
39  const void *data,
40  size_t size):
41  m_zip_archive(data, size)
42 {
44 }
45 
46 // VS: No default move constructors or assigns
47 
49  : m_zip_archive(std::move(other.m_zip_archive)),
50  m_name_to_index(std::move(other.m_name_to_index))
51 {
52 }
53 
55 {
56  m_zip_archive=std::move(other.m_zip_archive);
57  m_name_to_index=std::move(other.m_name_to_index);
58  return *this;
59 }
60 
62 {
63  const auto entry=m_name_to_index.find(name);
64  if(entry==m_name_to_index.end())
65  return {};
66 
67  try
68  {
69  return m_zip_archive.extract(entry->second);
70  }
71  catch(const std::runtime_error &)
72  {
73  return {};
74  }
75 }
76 
81 static bool is_space(const char ch)
82 {
83  return std::isspace(ch) != 0;
84 }
85 
91 static std::string trim(
92  const std::string::const_iterator begin,
93  const std::string::const_iterator end)
94 {
95  const auto out_begin=std::find_if_not(begin, end, is_space);
96  const auto out_end=std::find_if_not(
97  std::string::const_reverse_iterator(end),
98  std::string::const_reverse_iterator(out_begin),
99  is_space).base();
100  return { out_begin, out_end };
101 }
102 
103 std::unordered_map<std::string, std::string> jar_filet::get_manifest()
104 {
105  const auto entry=get_entry("META-INF/MANIFEST.MF");
106 
107  if(!entry.has_value())
108  return {};
109 
110  std::unordered_map<std::string, std::string> out;
111  std::istringstream in(*entry);
112  std::string line;
113  while(std::getline(in, line))
114  {
115  const auto key_end=std::find(line.cbegin(), line.cend(), ':');
116  if(key_end!=line.cend())
117  {
118  out.emplace(
119  trim(line.cbegin(), key_end),
120  trim(std::next(key_end), line.cend()));
121  }
122  }
123 
124  return out;
125 }
126 
127 std::vector<std::string> jar_filet::filenames() const
128 {
129  std::vector<std::string> out;
130  for(const auto &pair : m_name_to_index)
131  out.emplace_back(pair.first);
132  return out;
133 }
is_space
static bool is_space(const char ch)
Wrapper for std::isspace from cctype
Definition: jar_file.cpp:81
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:63
jar_filet::operator=
jar_filet & operator=(const jar_filet &)=delete
data
Definition: kdev_t.h:24
invariant.h
jar_file.h
jar_filet::filenames
std::vector< std::string > filenames() const
Get list of filenames in the archive.
Definition: jar_file.cpp:127
mz_zip_archivet::extract
std::string extract(size_t index)
Get contents of nth file in the archive.
Definition: mz_zip_archive.cpp:89
java_class_loader_limit.h
limit class path loading
jar_filet::m_zip_archive
mz_zip_archivet m_zip_archive
Definition: jar_file.h:60
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
mz_zip_archivet::get_num_files
size_t get_num_files()
Get number of files in the archive.
Definition: mz_zip_archive.cpp:69
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:91
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:103
suffix.h
jar_filet
Class representing a .jar archive.
Definition: jar_file.h:24
jar_filet::jar_filet
jar_filet(const std::string &filename)
Open java file for reading.
Definition: jar_file.cpp:28
jar_filet::get_entry
optionalt< std::string > get_entry(const std::string &filename)
Get contents of a file in the jar archive.
Definition: jar_file.cpp:61
mz_zip_archivet::get_filename
std::string get_filename(size_t index)
Get file name of nth file in the archive.
Definition: mz_zip_archive.cpp:74
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:19