Skip to content

Commit

Permalink
Move zip stream utils to namespace, refactor the code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Aug 29, 2017
1 parent 9b39401 commit 511d587
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 135 deletions.
13 changes: 13 additions & 0 deletions libs/bytestreamutils.h
Expand Up @@ -103,6 +103,19 @@ inline ValueType readLittleEndian(InputStream& stream)
return value;
}

inline void readByte(InputStream& stream, InputStream::byte_type& value)
{
stream.read(&value, 1);
}

inline InputStream::byte_type readByte(InputStream& stream)
{
InputStream::byte_type value;
stream.read(&value, 1);

return value;
}

}

template<typename InputStreamType, typename Type>
Expand Down
2 changes: 1 addition & 1 deletion plugins/archivezip/Makefile.am
Expand Up @@ -4,5 +4,5 @@ modulesdir = $(pkglibdir)/modules
modules_LTLIBRARIES = archivezip.la

archivezip_la_LDFLAGS = -module -avoid-version $(Z_LIBS) $(LIBSIGC_LIBS) $(FILESYSTEM_LIBS)
archivezip_la_SOURCES = ZipArchive.cpp pkzip.cpp plugin.cpp zlibstream.cpp
archivezip_la_SOURCES = ZipArchive.cpp plugin.cpp zlibstream.cpp

54 changes: 28 additions & 26 deletions plugins/archivezip/ZipArchive.cpp
Expand Up @@ -4,11 +4,11 @@
#include "iarchive.h"
#include "archivelib.h"

#include "pkzip.h"
#include "zlibstream.h"
#include "os/fs.h"
#include "os/path.h"

#include "ZipStreamUtils.h"
#include "DeflatedArchiveFile.h"
#include "DeflatedArchiveTextFile.h"

Expand Down Expand Up @@ -39,6 +39,7 @@ ZipArchive::ZipArchive(const std::string& fullPath) :

try
{
// Try loading the zip file, this will throw exceptoions on any problem
loadZipFile();
}
catch (ZipFailureException& ex)
Expand Down Expand Up @@ -71,9 +72,9 @@ ArchiveFilePtr ZipArchive::openFile(const std::string& name)
istream_read_zip_file_header(_istream, file_header);
position = _istream.tell();

if (file_header.z_magic != zip_file_header_magic)
if (file_header.z_magic != ZIP_MAGIC_FILE_HEADER)
{
rError() << "error reading zip file " << _fullPath << std::endl;
rError() << "Error reading zip file " << _fullPath << std::endl;
return ArchiveFilePtr();
}
}
Expand Down Expand Up @@ -106,9 +107,9 @@ ArchiveTextFilePtr ZipArchive::openTextFile(const std::string& name)
zip_file_header file_header;
istream_read_zip_file_header(_istream, file_header);

if (file_header.z_magic != zip_file_header_magic)
if (file_header.z_magic != ZIP_MAGIC_FILE_HEADER)
{
rError() << "error reading zip file " << _fullPath << std::endl;
rError() << "Error reading zip file " << _fullPath << std::endl;
return ArchiveTextFilePtr();
}
}
Expand Down Expand Up @@ -145,17 +146,18 @@ void ZipArchive::forEachFile(VisitorFunc visitor, const std::string& root) {

void ZipArchive::readZipRecord()
{
zip_magic magic;
istream_read_zip_magic(_istream, magic);
ZipMagic magic;
stream::readZipMagic(_istream, magic);

if (!(magic == zip_root_dirent_magic))
if (magic != ZIP_MAGIC_ROOT_DIR_ENTRY)
{
throw ZipFailureException("Invalid Zip directory entry magic");
}
zip_version version_encoder;
istream_read_zip_version(_istream, version_encoder);
zip_version version_extract;
istream_read_zip_version(_istream, version_extract);

ZipVersion version_encoder;
stream::readZipVersion(_istream, version_encoder);
ZipVersion version_extract;
stream::readZipVersion(_istream, version_extract);

//unsigned short flags =
stream::readLittleEndian<int16_t>(_istream);
Expand All @@ -167,26 +169,26 @@ void ZipArchive::readZipRecord()
throw ZipFailureException("Unsupported compression mode");
}

zip_dostime dostime;
istream_read_zip_dostime(_istream, dostime);
ZipDosTime dostime;
stream::readZipDosTime(_istream, dostime);

//unsigned int crc32 =
istream_read_int32_le(_istream);

unsigned int compressed_size = istream_read_uint32_le(_istream);
unsigned int uncompressed_size = istream_read_uint32_le(_istream);
unsigned int namelength = istream_read_uint16_le(_istream);
unsigned short extras = istream_read_uint16_le(_istream);
unsigned short comment = istream_read_uint16_le(_istream);
stream::readLittleEndian<uint32_t>(_istream);
uint32_t compressed_size = stream::readLittleEndian<uint32_t>(_istream);
uint32_t uncompressed_size = stream::readLittleEndian<uint32_t>(_istream);
uint16_t namelength = stream::readLittleEndian<uint16_t>(_istream);
uint16_t extras = stream::readLittleEndian<uint16_t>(_istream);
uint16_t comment = stream::readLittleEndian<uint16_t>(_istream);

//unsigned short diskstart =
istream_read_int16_le(_istream);
stream::readLittleEndian<uint16_t>(_istream);
//unsigned short filetype =
istream_read_int16_le(_istream);
stream::readLittleEndian<uint16_t>(_istream);
//unsigned int filemode =
istream_read_int32_le(_istream);
stream::readLittleEndian<uint32_t>(_istream);

unsigned int position = istream_read_int32_le(_istream);
uint32_t position = stream::readLittleEndian<uint32_t>(_istream);

// greebo: Read the filename directly into a newly constructed std::string.

Expand Down Expand Up @@ -239,7 +241,7 @@ void ZipArchive::loadZipFile()
zip_disk_trailer disk_trailer;
istream_read_zip_disk_trailer(_istream, disk_trailer);

if (disk_trailer.z_magic != zip_disk_trailer_magic)
if (disk_trailer.z_magic != ZIP_MAGIC_DISK_TRAILER)
{
throw ZipFailureException("Invalid Zip Magic, maybe this is not a zip file?");
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/archivezip/ZipArchive.h
Expand Up @@ -30,19 +30,19 @@ class ZipArchive :
eDeflated,
};

ZipRecord(unsigned int position_,
unsigned int compressed_size_,
unsigned int uncompressed_size_,
ZipRecord(uint32_t position_,
uint32_t compressed_size_,
uint32_t uncompressed_size_,
CompressionMode mode_) :
position(position_),
stream_size(compressed_size_),
file_size(uncompressed_size_),
mode(mode_)
{}

unsigned int position;
unsigned int stream_size;
unsigned int file_size;
uint32_t position;
uint32_t stream_size;
uint32_t file_size;
CompressionMode mode;
};
typedef GenericFileSystem<ZipRecord> ZipFileSystem;
Expand Down

0 comments on commit 511d587

Please sign in to comment.