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 
80 static bool is_space(const char ch)
81 {
82  return std::isspace(ch) != 0;
83 }
84 
86 static std::string trim(
87  const std::string::const_iterator begin,
88  const std::string::const_iterator end)
89 {
90  const auto out_begin=std::find_if_not(begin, end, is_space);
91  const auto out_end=std::find_if_not(
92  std::string::const_reverse_iterator(end),
93  std::string::const_reverse_iterator(out_begin),
94  is_space).base();
95  return { out_begin, out_end };
96 }
97 
98 std::unordered_map<std::string, std::string> jar_filet::get_manifest()
99 {
100  const auto entry=get_entry("META-INF/MANIFEST.MF");
101 
102  if(!entry.has_value())
103  return {};
104 
105  std::unordered_map<std::string, std::string> out;
106  std::istringstream in(*entry);
107  std::string line;
108  while(std::getline(in, line))
109  {
110  const auto key_end=std::find(line.cbegin(), line.cend(), ':');
111  if(key_end!=line.cend())
112  {
113  out.emplace(
114  trim(line.cbegin(), key_end),
115  trim(std::next(key_end), line.cend()));
116  }
117  }
118 
119  return out;
120 }
121 
122 std::vector<std::string> jar_filet::filenames() const
123 {
124  std::vector<std::string> out;
125  for(const auto &pair : m_name_to_index)
126  out.emplace_back(pair.first);
127  return out;
128 }
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:86
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:122
optionalt< std::string > get_entry(const std::string &filename)
Get contents of a file in the jar archive.
Definition: jar_file.cpp:64
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:61
nonstd::optional< T > optionalt
Definition: optional.h:35
Class representing a .jar archive.
Definition: jar_file.h:22
jar_filet(const std::string &filename)
Open java file for reading.
Definition: jar_file.cpp:31
static bool is_space(const char ch)
Definition: jar_file.cpp:80
jar_filet & operator=(const jar_filet &)=delete
mz_zip_archivet m_zip_archive
Definition: jar_file.h:58
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.
Definition: jar_file.cpp:98
limit class path loading
Definition: kdev_t.h:24