Skip to content

Commit 6bee122

Browse files
authored
Merge pull request #4698 from NathanJPhillips/cleanup/jar-loading
Cleanups in loading of jar files
2 parents 6c39035 + 72936d1 commit 6bee122

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

jbmc/src/java_bytecode/jar_file.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ void jar_filet::initialize_file_index()
2020
{
2121
const size_t file_count=m_zip_archive.get_num_files();
2222
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-
}
23+
m_name_to_index.emplace(m_zip_archive.get_filename(index), index);
2724
}
2825

2926
/// This constructor creates a jar_file object whose contents

jbmc/src/java_bytecode/jar_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class jar_filet final
3131
/// Open a JAR file of size \p size loaded in memory at address \p data.
3232
/// \param data: memory buffer with the contents of the jar file
3333
/// \param size: size of the memory buffer
34-
/// \throw Throws std::runtime_error if file cannot be opened
34+
/// \throw Throws std::runtime_error if data is not in correct format
3535
jar_filet(const void *data, size_t size);
3636

3737
jar_filet(const jar_filet &)=delete;

jbmc/src/java_bytecode/jar_pool.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ jar_filet &jar_poolt::operator()(const std::string &file_name)
1313
{
1414
const auto it = m_archives.find(file_name);
1515
if(it == m_archives.end())
16-
{
17-
// VS: Can't construct in place
18-
auto file = jar_filet(file_name);
19-
return m_archives.emplace(file_name, std::move(file)).first->second;
20-
}
16+
return m_archives.emplace(file_name, jar_filet(file_name)).first->second;
2117
else
2218
return it->second;
2319
}

jbmc/src/java_bytecode/mz_zip_archive.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ size_t mz_zip_archivet::get_num_files()
7373

7474
std::string mz_zip_archivet::get_filename(const size_t index)
7575
{
76-
const auto id=static_cast<mz_uint>(index);
77-
std::vector<char> buffer;
78-
buffer.resize(mz_zip_reader_get_filename(m_state.get(), id, nullptr, 0));
79-
mz_zip_reader_get_filename(m_state.get(), id, buffer.data(), buffer.size());
80-
// Buffer may contain junk returned after \0
81-
const auto null_char_it=std::find(buffer.cbegin(), buffer.cend(), '\0');
82-
return { buffer.cbegin(), null_char_it };
76+
const auto id = static_cast<mz_uint>(index);
77+
mz_uint name_size = mz_zip_reader_get_filename(m_state.get(), id, nullptr, 0);
78+
if(name_size == 0)
79+
return {}; // Failure
80+
// It is valid to directly write to a string's buffer (see C++11 standard,
81+
// basic_string general requirements [string.require], 21.4.1.5)
82+
std::string buffer(name_size, '\0');
83+
mz_zip_reader_get_filename(m_state.get(), id, &buffer[0], buffer.size());
84+
// Buffer contains trailing \0
85+
buffer.resize(name_size - 1);
86+
return buffer;
8387
}
8488

8589
std::string mz_zip_archivet::extract(const size_t index)
@@ -89,12 +93,26 @@ std::string mz_zip_archivet::extract(const size_t index)
8993
const mz_bool stat_ok=mz_zip_reader_file_stat(m_state.get(), id, &file_stat);
9094
if(stat_ok==MZ_TRUE)
9195
{
92-
std::vector<char> buffer(file_stat.m_uncomp_size);
93-
const mz_bool read_ok=mz_zip_reader_extract_to_mem(
94-
m_state.get(), id, buffer.data(), buffer.size(), 0);
95-
if(read_ok==MZ_TRUE)
96-
return { buffer.cbegin(), buffer.cend() };
96+
// It is valid to directly write to a string's buffer (see C++11 standard,
97+
// basic_string general requirements [string.require], 21.4.1.5)
98+
std::string buffer(file_stat.m_uncomp_size, '\0');
99+
const mz_bool read_ok = mz_zip_reader_extract_to_mem(
100+
m_state.get(), id, &buffer[0], buffer.size(), 0);
101+
if(read_ok == MZ_TRUE)
102+
return buffer;
97103
}
98104
throw std::runtime_error("Could not extract the file");
99105
}
100106

107+
void mz_zip_archivet::extract_to_file(
108+
const size_t index,
109+
const std::string &path)
110+
{
111+
const auto id = static_cast<mz_uint>(index);
112+
if(
113+
mz_zip_reader_extract_to_file(m_state.get(), id, path.c_str(), 0) !=
114+
MZ_TRUE)
115+
{
116+
throw std::runtime_error("Could not extract the file");
117+
}
118+
}

jbmc/src/java_bytecode/mz_zip_archive.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class mz_zip_archivet final
2828
/// Loads a zip buffer
2929
/// \param data: pointer to the memory buffer
3030
/// \param size: size of the buffer
31-
/// \throw Throws std::runtime_error if file cannot be opened
31+
/// \throw Throws std::runtime_error if data is not in correct format
3232
mz_zip_archivet(const void *data, size_t size);
3333

3434
mz_zip_archivet(const mz_zip_archivet &)=delete;
@@ -51,6 +51,12 @@ class mz_zip_archivet final
5151
/// \throw Throws std::runtime_error if file cannot be extracted
5252
/// \return Contents of the file in the archive
5353
std::string extract(size_t index);
54+
/// Write contents of nth file in the archive to a file
55+
/// \param index: id of the file in the archive
56+
/// \param path: path to which to write the contents of the file
57+
/// \throw Throws std::runtime_error if file cannot be written
58+
void extract_to_file(size_t index, const std::string &path);
59+
5460
private:
5561
std::unique_ptr<mz_zip_archive_statet> m_state;
5662
};

0 commit comments

Comments
 (0)