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  {
24  const auto filename=m_zip_archive.get_filename(index);
25  if(!has_suffix(filename, ".class") || limit.load_class_file(filename))
26  m_name_to_index.emplace(filename, index);
27  }
28 }
29 
34  const std::string &filename):
35  m_zip_archive(filename)
36 {
37  initialize_file_index(limit);
38 }
39 
45  const void *data,
46  size_t size):
47  m_zip_archive(data, size)
48 {
49  initialize_file_index(limit);
50 }
51 
52 // VS: No default move constructors or assigns
53 
55  m_zip_archive(std::move(other.m_zip_archive)),
56  m_name_to_index((other.m_name_to_index)) {}
57 
59 {
60  m_zip_archive=std::move(other.m_zip_archive);
61  m_name_to_index=std::move(other.m_name_to_index);
62  return *this;
63 }
64 
66 {
67  const auto entry=m_name_to_index.find(name);
68  if(entry==m_name_to_index.end())
69  return {};
70 
71  try
72  {
73  return m_zip_archive.extract(entry->second);
74  }
75  catch(const std::runtime_error &)
76  {
77  return {};
78  }
79 }
80 
81 static bool is_space(const char ch)
82 {
83  return std::isspace(ch);
84 }
85 
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 }
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
jar_filet(java_class_loader_limitt &limit, const std::string &filename)
Open java file for reading.
Definition: jar_file.cpp:32
std::vector< std::string > filenames() const
Get list of filenames in the archive.
Definition: jar_file.cpp:123
optionalt< std::string > get_entry(const std::string &filename)
Get contents of a file in the jar archive.
Definition: jar_file.cpp:65
STL namespace.
std::string extract(size_t index)
Get contents of nth file in the archive.
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:65
nonstd::optional< T > optionalt
Definition: optional.h:35
Class representing a .jar archive.
Definition: jar_file.h:24
static bool is_space(const char ch)
Definition: jar_file.cpp:81
bool load_class_file(const std::string &class_file_name)
jar_filet & operator=(const jar_filet &)=delete
mz_zip_archivet m_zip_archive
Definition: jar_file.h:62
std::string get_filename(size_t index)
Get file name of nth file in the archive.
bool has_suffix(const std::string &s, const std::string &suffix)
Definition: suffix.h:15
void initialize_file_index(java_class_loader_limitt &limit)
Loads the fileindex (m_name_to_index) with a map of loaded files to indices.
Definition: jar_file.cpp:19
size_t get_num_files()
Get number of files in the archive.
std::unordered_map< std::string, std::string > get_manifest()
Get contents of the Manifest file in the jar archive.
Definition: jar_file.cpp:99
limit class path loading
Definition: kdev_t.h:24