Skip to content

Commit ed008f9

Browse files
dcattaruzzaDegiorgio
authored andcommitted
Add constructors for having memory-loaded jar files
This allows the jar_file class to load from a buffer (c array) as opposed to a file
1 parent 86a34c9 commit ed008f9

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

src/java_bytecode/jar_file.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ Author: Diffblue Ltd
1212
#include <util/invariant.h>
1313
#include "java_class_loader_limit.h"
1414

15-
jar_filet::jar_filet(
16-
java_class_loader_limitt &limit,
17-
const std::string &filename):
18-
m_zip_archive(filename)
15+
void jar_filet::initialize_file_index(java_class_loader_limitt &limit)
1916
{
2017
const size_t file_count=m_zip_archive.get_num_files();
2118
for(size_t index=0; index<file_count; index++)
@@ -26,6 +23,29 @@ jar_filet::jar_filet(
2623
}
2724
}
2825

26+
/// This constructor creates a jar_file object whose contents
27+
/// are extracted from a memory buffer (byte array) as opposed
28+
/// to a jar file.
29+
jar_filet::jar_filet(
30+
java_class_loader_limitt &limit,
31+
const std::string &filename):
32+
m_zip_archive(filename)
33+
{
34+
initialize_file_index(limit);
35+
}
36+
37+
/// This constructor creates a jar_file object whose contents
38+
/// are extracted from a memory buffer (byte array) as opposed
39+
/// to a jar file.
40+
jar_filet::jar_filet(
41+
java_class_loader_limitt &limit,
42+
const void *data,
43+
size_t size):
44+
m_zip_archive(data, size)
45+
{
46+
initialize_file_index(limit);
47+
}
48+
2949
// VS: No default move constructors or assigns
3050

3151
jar_filet::jar_filet(jar_filet &&other):

src/java_bytecode/jar_file.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ class java_class_loader_limitt;
2121
class jar_filet final
2222
{
2323
public:
24-
/// Open java file for reading
24+
/// Open java file for reading.
2525
/// \param limit Object limiting number of loaded .class files
2626
/// \param filename Name of the file
2727
/// \throw Throws std::runtime_error if file cannot be opened
2828
jar_filet(java_class_loader_limitt &limit, const std::string &filename);
29+
30+
/// Open a JAR file of size \p size loaded in memory at address \p data.
31+
/// \param limit Object limiting number of loaded .class files
32+
/// \param data memory buffer with the contents of the jar file
33+
/// \param size size of the memory buffer
34+
/// \throw Throws std::runtime_error if file cannot be opened
35+
jar_filet(java_class_loader_limitt &limit, const void *data, size_t size);
36+
2937
jar_filet(const jar_filet &)=delete;
3038
jar_filet &operator=(const jar_filet &)=delete;
3139
jar_filet(jar_filet &&);
3240
jar_filet &operator=(jar_filet &&);
3341
~jar_filet()=default;
42+
3443
/// Get contents of a file in the jar archive.
3544
/// Terminates the program if file doesn't exist
3645
/// \param filename Name of the file in the archive
@@ -40,8 +49,12 @@ class jar_filet final
4049
/// Get list of filenames in the archive
4150
std::vector<std::string> filenames() const;
4251
private:
52+
/// Loads the fileindex (m_name_to_index) with a map of loaded files to
53+
/// indices.
54+
void initialize_file_index(java_class_loader_limitt &limit);
55+
4356
mz_zip_archivet m_zip_archive;
44-
/// Map of filename to the file index in the zip archive
57+
/// Map of filename to the file index in the zip archive.
4558
std::unordered_map<std::string, size_t> m_name_to_index;
4659
};
4760

src/java_bytecode/mz_zip_archive.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ class mz_zip_archive_statet final:public mz_zip_archive
2525
if(MZ_TRUE!=mz_zip_reader_init_file(this, filename.data(), 0))
2626
throw std::runtime_error("MZT: Could not load a file: "+filename);
2727
}
28+
29+
mz_zip_archive_statet(const void *data, size_t size):
30+
mz_zip_archive({ })
31+
{
32+
if(MZ_TRUE!=mz_zip_reader_init_mem(this, data, size, 0))
33+
throw std::runtime_error("MZT: Could not load data from memory");
34+
}
35+
2836
mz_zip_archive_statet(const mz_zip_archive_statet &)=delete;
2937
mz_zip_archive_statet(mz_zip_archive_statet &&)=delete;
3038
mz_zip_archive_statet &operator=(const mz_zip_archive_statet &)=delete;
@@ -41,6 +49,9 @@ static_assert(sizeof(mz_uint)<=sizeof(size_t),
4149
mz_zip_archivet::mz_zip_archivet(const std::string &filename):
4250
m_state(new mz_zip_archive_statet(filename)) { }
4351

52+
mz_zip_archivet::mz_zip_archivet(const void *data, size_t size):
53+
m_state(new mz_zip_archive_statet(data, size)) { }
54+
4455
// VS Compatibility
4556
mz_zip_archivet::mz_zip_archivet(mz_zip_archivet &&other):
4657
m_state(std::move(other.m_state)) { }

src/java_bytecode/mz_zip_archive.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class mz_zip_archivet final
2424
/// \param filename Path of the zip archive
2525
/// \throw Throws std::runtime_error if file cannot be opened
2626
explicit mz_zip_archivet(const std::string &filename);
27+
28+
/// Loads a zip buffer
29+
/// \param data pointer to the memory buffer
30+
/// \param size size of the buffer
31+
/// \throw Throws std::runtime_error if file cannot be opened
32+
mz_zip_archivet(const void *data, size_t size);
33+
2734
mz_zip_archivet(const mz_zip_archivet &)=delete;
2835
mz_zip_archivet &operator=(const mz_zip_archivet &)=delete;
2936
/// Move constructor. Doesn't throw. Leaves other object invalidated.

0 commit comments

Comments
 (0)