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  m_name_to_index.emplace(filename, index);
26  }
27 }
28 
32  const std::string &filename):
33  m_zip_archive(filename)
34 {
36 }
37 
42  const void *data,
43  size_t size):
44  m_zip_archive(data, size)
45 {
47 }
48 
49 // VS: No default move constructors or assigns
50 
52  : m_zip_archive(std::move(other.m_zip_archive)),
53  m_name_to_index(std::move(other.m_name_to_index))
54 {
55 }
56 
58 {
59  m_zip_archive=std::move(other.m_zip_archive);
60  m_name_to_index=std::move(other.m_name_to_index);
61  return *this;
62 }
63 
65 {
66  const auto entry=m_name_to_index.find(name);
67  if(entry==m_name_to_index.end())
68  return {};
69 
70  try
71  {
72  return m_zip_archive.extract(entry->second);
73  }
74  catch(const std::runtime_error &)
75  {
76  return {};
77  }
78 }
79 
84 static bool is_space(const char ch)
85 {
86  return std::isspace(ch) != 0;
87 }
88 
94 static std::string trim(
95  const std::string::const_iterator begin,
96  const std::string::const_iterator end)
97 {
98  const auto out_begin=std::find_if_not(begin, end, is_space);
99  const auto out_end=std::find_if_not(
100  std::string::const_reverse_iterator(end),
101  std::string::const_reverse_iterator(out_begin),
102  is_space).base();
103  return { out_begin, out_end };
104 }
105 
106 std::unordered_map<std::string, std::string> jar_filet::get_manifest()
107 {
108  const auto entry=get_entry("META-INF/MANIFEST.MF");
109 
110  if(!entry.has_value())
111  return {};
112 
113  std::unordered_map<std::string, std::string> out;
114  std::istringstream in(*entry);
115  std::string line;
116  while(std::getline(in, line))
117  {
118  const auto key_end=std::find(line.cbegin(), line.cend(), ':');
119  if(key_end!=line.cend())
120  {
121  out.emplace(
122  trim(line.cbegin(), key_end),
123  trim(std::next(key_end), line.cend()));
124  }
125  }
126 
127  return out;
128 }
129 
130 std::vector<std::string> jar_filet::filenames() const
131 {
132  std::vector<std::string> out;
133  for(const auto &pair : m_name_to_index)
134  out.emplace_back(pair.first);
135  return out;
136 }
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:94
void initialize_file_index()
Loads the fileindex (m_name_to_index) with a map of loaded files to indices.
Definition: jar_file.cpp:19
std::vector< std::string > filenames() const
Get list of filenames in the archive.
Definition: jar_file.cpp:130
optionalt< std::string > get_entry(const std::string &filename)
Get contents of a file in the jar archive.
Definition: jar_file.cpp:64
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:63
nonstd::optional< T > optionalt
Definition: optional.h:35
Class representing a .jar archive.
Definition: jar_file.h:23
jar_filet(const std::string &filename)
Open java file for reading.
Definition: jar_file.cpp:31
static bool is_space(const char ch)
Wrapper for std::isspace from cctype
Definition: jar_file.cpp:84
jar_filet & operator=(const jar_filet &)=delete
mz_zip_archivet m_zip_archive
Definition: jar_file.h:60
std::string get_filename(size_t index)
Get file name of nth file in the archive.
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 as a key-value map (both as strings)
Definition: jar_file.cpp:106
limit class path loading
Definition: kdev_t.h:24