summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2017-02-23 23:02:52 +0300
committerAndrei Karas <akaras@inbox.ru>2017-02-23 23:02:52 +0300
commitb882d8c240a1667a820adb703d3f7c84dfcd61c2 (patch)
tree6151b93d6c8d9c39447ac37d6e109be5a2a741c9
parente5071983c6d0281a92d79daae2f40252b67c1296 (diff)
downloadplus-b882d8c240a1667a820adb703d3f7c84dfcd61c2.tar.gz
plus-b882d8c240a1667a820adb703d3f7c84dfcd61c2.tar.bz2
plus-b882d8c240a1667a820adb703d3f7c84dfcd61c2.tar.xz
plus-b882d8c240a1667a820adb703d3f7c84dfcd61c2.zip
Add support for parsing zip archives.
Also add unit tests for test it.
-rw-r--r--data/test/CMakeLists.txt3
-rw-r--r--data/test/Makefile.am3
-rw-r--r--data/test/test2.zipbin0 -> 4550 bytes
-rw-r--r--data/test/test3.zipbin0 -> 531 bytes
-rw-r--r--data/test/test4.zipbin0 -> 22 bytes
-rw-r--r--src/CMakeLists.txt6
-rw-r--r--src/Makefile.am4
-rw-r--r--src/fs/virtfs_unittest.cc8
-rw-r--r--src/fs/virtfsdir_unittest.cc8
-rw-r--r--src/fs/zip.cpp161
-rw-r--r--src/fs/zip.h37
-rw-r--r--src/fs/zip_unittest.cc147
-rw-r--r--src/fs/ziplocalheader.h46
13 files changed, 415 insertions, 8 deletions
diff --git a/data/test/CMakeLists.txt b/data/test/CMakeLists.txt
index 2141da3cc..7e76a7b99 100644
--- a/data/test/CMakeLists.txt
+++ b/data/test/CMakeLists.txt
@@ -15,6 +15,9 @@ SET(FILES
test.txt
test2.txt
test.zip
+ test2.zip
+ test3.zip
+ test4.zip
testintmap.xml
units.xml
)
diff --git a/data/test/Makefile.am b/data/test/Makefile.am
index 3d4fa7f8e..8288f9426 100644
--- a/data/test/Makefile.am
+++ b/data/test/Makefile.am
@@ -19,6 +19,9 @@ test_DATA = \
test.txt \
test2.txt \
test.zip \
+ test2.zip \
+ test3.zip \
+ test4.zip \
testintmap.xml \
units.xml
diff --git a/data/test/test2.zip b/data/test/test2.zip
new file mode 100644
index 000000000..d225c1e84
--- /dev/null
+++ b/data/test/test2.zip
Binary files differ
diff --git a/data/test/test3.zip b/data/test/test3.zip
new file mode 100644
index 000000000..1d56fd7bf
--- /dev/null
+++ b/data/test/test3.zip
Binary files differ
diff --git a/data/test/test4.zip b/data/test/test4.zip
new file mode 100644
index 000000000..15cb0ecb3
--- /dev/null
+++ b/data/test/test4.zip
Binary files differ
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b70bf269d..91fc76c9f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -897,6 +897,9 @@ SET(SRCS
fs/virtlist.h
fs/virtfs.cpp
fs/virtfs.h
+ fs/zip.cpp
+ fs/zip.h
+ fs/ziplocalheader.h
utils/process.cpp
utils/process.h
utils/sdl2helper.cpp
@@ -1787,6 +1790,9 @@ SET(DYE_CMD_SRCS
fs/virtlist.h
fs/virtfs.cpp
fs/virtfs.h
+ fs/zip.cpp
+ fs/zip.h
+ fs/ziplocalheader.h
utils/sdl2helper.cpp
utils/sdl2helper.h
utils/sdlcheckutils.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 6c9b56543..2b53079f1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -561,6 +561,9 @@ BASE_SRC += events/actionevent.h \
fs/virtlist.h \
fs/virtfs.cpp \
fs/virtfs.h \
+ fs/zip.cpp \
+ fs/zip.h \
+ fs/ziplocalheader.h \
utils/process.cpp \
utils/process.h \
utils/sdl2helper.cpp \
@@ -1924,6 +1927,7 @@ manaplustests_SOURCES = ${SRC} \
utils/dumplibs_unittest.cc \
utils/checkutils_unittest.cc \
fs/virtfs_unittest.cc \
+ fs/zip_unittest.cc \
fs/virtfsdir_unittest.cc \
utils/xml_unittest.cc \
utils/timer_unittest.cc \
diff --git a/src/fs/virtfs_unittest.cc b/src/fs/virtfs_unittest.cc
index b26381f08..a79f01aee 100644
--- a/src/fs/virtfs_unittest.cc
+++ b/src/fs/virtfs_unittest.cc
@@ -115,8 +115,8 @@ TEST_CASE("VirtFs enumerateFiles1")
VirtList *list = nullptr;
- const int cnt1 = VirtFs::exists("test/test2.txt") ? 24 : 23;
- const int cnt2 = 24;
+ const int cnt1 = VirtFs::exists("test/test2.txt") ? 27 : 26;
+ const int cnt2 = 27;
VirtFs::permitLinks(false);
list = VirtFs::enumerateFiles("test");
@@ -363,8 +363,8 @@ TEST_CASE("VirtFs permitLinks")
VirtFs::addDirToSearchPath("data", Append_false);
VirtFs::addDirToSearchPath("../data", Append_false);
- const int cnt1 = VirtFs::exists("test/test2.txt") ? 22 : 21;
- const int cnt2 = 22;
+ const int cnt1 = VirtFs::exists("test/test2.txt") ? 25 : 24;
+ const int cnt2 = 25;
StringVect list;
VirtFs::permitLinks(false);
diff --git a/src/fs/virtfsdir_unittest.cc b/src/fs/virtfsdir_unittest.cc
index 91c573f61..df596712b 100644
--- a/src/fs/virtfsdir_unittest.cc
+++ b/src/fs/virtfsdir_unittest.cc
@@ -387,8 +387,8 @@ TEST_CASE("VirtFsDir enumerateFiles1")
VirtList *list = nullptr;
- const int cnt1 = VirtFsDir::exists("test/test2.txt") ? 24 : 23;
- const int cnt2 = 24;
+ const int cnt1 = VirtFsDir::exists("test/test2.txt") ? 27 : 26;
+ const int cnt2 = 27;
VirtFsDir::permitLinks(false);
list = VirtFsDir::enumerateFiles("test");
@@ -616,8 +616,8 @@ TEST_CASE("VirtFsDir permitLinks")
Append_false,
SkipError_false);
- const int cnt1 = VirtFsDir::exists("test/test2.txt") ? 22 : 21;
- const int cnt2 = 22;
+ const int cnt1 = VirtFsDir::exists("test/test2.txt") ? 25 : 24;
+ const int cnt2 = 25;
StringVect list;
VirtFsDir::permitLinks(false);
diff --git a/src/fs/zip.cpp b/src/fs/zip.cpp
new file mode 100644
index 000000000..2164a4303
--- /dev/null
+++ b/src/fs/zip.cpp
@@ -0,0 +1,161 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2017 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "fs/zip.h"
+
+#include "fs/ziplocalheader.h"
+
+#include "utils/checkutils.h"
+#include "utils/stringutils.h"
+
+#include <iostream>
+#include <unistd.h>
+
+#include "debug.h"
+
+#define readVal(val, sz, msg) \
+ cnt = fread(static_cast<void*>(val), 1, sz, arcFile); \
+ if (cnt != sz) \
+ { \
+ reportAlways("Error reading " msg " in file %s", \
+ archiveName.c_str()); \
+ delete header; \
+ delete [] buf; \
+ fclose(arcFile); \
+ return false; \
+ } \
+
+namespace Zip
+{
+ bool readArchiveInfo(const std::string &restrict archiveName,
+ std::vector<ZipLocalHeader*> &restrict headers)
+ {
+ FILE *restrict const arcFile = fopen(archiveName.c_str(),
+ "r");
+ if (arcFile == nullptr)
+ {
+ reportAlways("Can't open zip file %s",
+ archiveName.c_str());
+ return false;
+ }
+ size_t cnt = 0U;
+ uint8_t *const buf = new uint8_t[65535 + 10];
+ uint16_t val16 = 0U;
+ ZipLocalHeader *header = nullptr;
+
+ logger->log("Read archive: %s", archiveName.c_str());
+ // format source https://en.wikipedia.org/wiki/Zip_%28file_format%29
+ while (feof(arcFile) == 0)
+ {
+ // file header pointer on 0
+ // read file header signature
+ readVal(buf, 4, "zip file header"); // + 4
+ // pointer on 4
+
+ if (buf[0] == 0x50 &&
+ buf[1] == 0x4B &&
+ buf[2] == 0x03 &&
+ buf[3] == 0x04)
+ { // local file header
+ header = new ZipLocalHeader;
+ header->archiveName = archiveName;
+ // skip useless fields
+ fseek(arcFile, 14, SEEK_CUR); // + 14
+ // file header pointer on 18
+ readVal(&header->compressSize, 4,
+ "zip compressed size") // + 4
+ // file header pointer on 22
+ // +++ need add endian specific decoding for val32
+ readVal(&header->uncompressSize, 4,
+ "zip uncompressed size") // + 4
+ // file header pointer on 26
+ // +++ need add endian specific decoding for val32
+ readVal(&val16, 2, "file name length") // + 2
+ // file header pointer on 28
+ const uint32_t fileNameLen = CAST_U32(val16);
+ if (fileNameLen > 1000)
+ {
+ reportAlways("Error too long file name in file %s",
+ archiveName.c_str());
+ delete header;
+ delete [] buf;
+ fclose(arcFile);
+ return false;
+ }
+ readVal(&val16, 2, "extra field length") // + 2
+ // file header pointer on 30
+ const uint32_t extraFieldLen = CAST_U32(val16);
+ readVal(buf, fileNameLen, "file name");
+ // file header pointer on 30 + fileNameLen
+ buf[fileNameLen] = 0;
+ header->fileName = std::string(
+ reinterpret_cast<char*>(buf));
+ header->dataOffset = ftell(arcFile) + extraFieldLen;
+ fseek(arcFile, extraFieldLen + header->compressSize, SEEK_CUR);
+ // pointer on 30 + fileNameLen + extraFieldLen + compressSize
+ if (findLast(header->fileName, "/") == false)
+ {
+ headers.push_back(header);
+ logger->log(" file name: %s",
+ header->fileName.c_str());
+ logger->log(" compressed size: %u", header->compressSize);
+ logger->log(" uncompressed size: %u", header->uncompressSize);
+ }
+ }
+ else if (buf[0] == 0x50 &&
+ buf[1] == 0x4B &&
+ buf[2] == 0x01 &&
+ buf[3] == 0x02)
+ { // central directory file header
+ // !!! This is quick way for read zip archives. !!!
+ // !!! It ignore modified files in archive. !!!
+ // ignoring central directory entries because all data already read.
+ break;
+ }
+ else if (buf[0] == 0x50 &&
+ buf[1] == 0x4B &&
+ buf[2] == 0x05 &&
+ buf[3] == 0x06)
+ { // end of central directory
+ // !!! This is quick way for read zip archives. !!!
+ // !!! It ignore modified files in archive. !!!
+ // ignoring central directory entries because all data already read.
+ break;
+ }
+ else
+ {
+ reportAlways("Error in header signature (0x%02x%02x%02x%02x)"
+ " in file %s",
+ buf[0],
+ buf[1],
+ buf[2],
+ buf[3],
+ archiveName.c_str());
+ delete [] buf;
+ fclose(arcFile);
+ return false;
+ }
+ }
+ delete [] buf;
+ fclose(arcFile);
+ return true;
+ }
+
+} // namespace Zip
diff --git a/src/fs/zip.h b/src/fs/zip.h
new file mode 100644
index 000000000..145be5c88
--- /dev/null
+++ b/src/fs/zip.h
@@ -0,0 +1,37 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2017 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef UTILS_ZIP_H
+#define UTILS_ZIP_H
+
+#include "localconsts.h"
+
+#include <string>
+#include <vector>
+
+struct ZipLocalHeader;
+
+namespace Zip
+{
+ bool readArchiveInfo(const std::string &restrict archiveName,
+ std::vector<ZipLocalHeader*> &restrict headers);
+} // namespace Zip
+
+#endif // UTILS_ZIP_H
diff --git a/src/fs/zip_unittest.cc b/src/fs/zip_unittest.cc
new file mode 100644
index 000000000..3f0c49cdf
--- /dev/null
+++ b/src/fs/zip_unittest.cc
@@ -0,0 +1,147 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2017 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "catch.hpp"
+
+#include "logger.h"
+
+#include "fs/files.h"
+#include "fs/zip.h"
+#include "fs/ziplocalheader.h"
+
+#include "utils/delete2.h"
+#include "utils/dtor.h"
+
+#include "debug.h"
+
+TEST_CASE("Zip readArchiveInfo")
+{
+ logger = new Logger();
+ std::string name("data/test/test.zip");
+ std::string prefix;
+ std::vector<ZipLocalHeader*> headers;
+ if (Files::existsLocal(name) == false)
+ prefix = "../";
+
+ SECTION("test 1")
+ {
+ name = prefix + "data/test/test.zip";
+
+ REQUIRE(Zip::readArchiveInfo(name, headers));
+ REQUIRE(headers.size() == 2);
+ REQUIRE(headers[0]->archiveName == name);
+ REQUIRE(headers[0]->fileName == "dir/hide.png");
+ REQUIRE(headers[0]->compressSize == 365);
+ REQUIRE(headers[0]->uncompressSize == 368);
+ REQUIRE(headers[1]->archiveName == name);
+ REQUIRE(headers[1]->fileName == "dir/brimmedhat.png");
+ REQUIRE(headers[1]->compressSize == 1959);
+ REQUIRE(headers[1]->uncompressSize == 1959);
+ }
+
+ SECTION("test 2")
+ {
+ name = prefix + "data/test/test2.zip";
+
+ REQUIRE(Zip::readArchiveInfo(name, headers));
+ REQUIRE(headers.size() == 11);
+ REQUIRE(headers[0]->archiveName == name);
+ REQUIRE(headers[0]->fileName == "test.txt");
+ REQUIRE(headers[0]->compressSize == 17);
+ REQUIRE(headers[0]->uncompressSize == 23);
+
+ REQUIRE(headers[1]->archiveName == name);
+ REQUIRE(headers[1]->fileName == "dir2/hide.png");
+ REQUIRE(headers[1]->compressSize == 365);
+ REQUIRE(headers[1]->uncompressSize == 368);
+
+ REQUIRE(headers[2]->archiveName == name);
+ REQUIRE(headers[2]->fileName == "dir2/test.txt");
+ REQUIRE(headers[2]->compressSize == 17);
+ REQUIRE(headers[2]->uncompressSize == 23);
+
+ REQUIRE(headers[3]->archiveName == name);
+ REQUIRE(headers[3]->fileName == "dir2/paths.xml");
+ REQUIRE(headers[3]->compressSize == 154);
+ REQUIRE(headers[3]->uncompressSize == 185);
+
+ REQUIRE(headers[4]->archiveName == name);
+ REQUIRE(headers[4]->fileName == "dir2/units.xml");
+ REQUIRE(headers[4]->compressSize == 202);
+ REQUIRE(headers[4]->uncompressSize == 306);
+
+ REQUIRE(headers[5]->archiveName == name);
+ REQUIRE(headers[5]->fileName == "dir/hide.png");
+ REQUIRE(headers[5]->compressSize == 365);
+ REQUIRE(headers[5]->uncompressSize == 368);
+
+ REQUIRE(headers[6]->archiveName == name);
+ REQUIRE(headers[6]->fileName == "dir/1/test.txt");
+ REQUIRE(headers[6]->compressSize == 17);
+ REQUIRE(headers[6]->uncompressSize == 23);
+
+ REQUIRE(headers[7]->archiveName == name);
+ REQUIRE(headers[7]->fileName == "dir/1/file1.txt");
+ REQUIRE(headers[7]->compressSize == 17);
+ REQUIRE(headers[7]->uncompressSize == 23);
+
+ REQUIRE(headers[8]->archiveName == name);
+ REQUIRE(headers[8]->fileName == "dir/gpl/palette.gpl");
+ REQUIRE(headers[8]->compressSize == 128);
+ REQUIRE(headers[8]->uncompressSize == 213);
+
+ REQUIRE(headers[9]->archiveName == name);
+ REQUIRE(headers[9]->fileName == "dir/dye.png");
+ REQUIRE(headers[9]->compressSize == 794);
+ REQUIRE(headers[9]->uncompressSize == 794);
+
+ REQUIRE(headers[10]->archiveName == name);
+ REQUIRE(headers[10]->fileName == "units.xml");
+ REQUIRE(headers[10]->compressSize == 202);
+ REQUIRE(headers[10]->uncompressSize == 306);
+ }
+
+ SECTION("test 3")
+ {
+ name = prefix + "data/test/test3.zip";
+
+ REQUIRE(Zip::readArchiveInfo(name, headers));
+ REQUIRE(headers.size() == 2);
+ REQUIRE(headers[0]->archiveName == name);
+ REQUIRE(headers[0]->fileName == "test.txt");
+ REQUIRE(headers[0]->compressSize == 17);
+ REQUIRE(headers[0]->uncompressSize == 23);
+ REQUIRE(headers[1]->archiveName == name);
+ REQUIRE(headers[1]->fileName == "units.xml");
+ REQUIRE(headers[1]->compressSize == 202);
+ REQUIRE(headers[1]->uncompressSize == 306);
+ }
+
+ SECTION("test 4")
+ {
+ name = prefix + "data/test/test4.zip";
+
+ REQUIRE(Zip::readArchiveInfo(name, headers));
+ REQUIRE(headers.size() == 0);
+ }
+
+ delete_all(headers);
+ delete2(logger);
+}
diff --git a/src/fs/ziplocalheader.h b/src/fs/ziplocalheader.h
new file mode 100644
index 000000000..1b00c3b54
--- /dev/null
+++ b/src/fs/ziplocalheader.h
@@ -0,0 +1,46 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2017 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef UTILS_ZIPLOCALHEADER_H
+#define UTILS_ZIPLOCALHEADER_H
+
+#include "localconsts.h"
+
+#include <string>
+
+struct ZipLocalHeader
+{
+ ZipLocalHeader() :
+ archiveName(),
+ fileName(),
+ dataOffset(0U),
+ compressSize(0U),
+ uncompressSize(0U)
+ {
+ }
+
+ std::string archiveName;
+ std::string fileName;
+ uint32_t dataOffset;
+ uint32_t compressSize;
+ uint32_t uncompressSize;
+};
+
+#endif // UTILS_ZIPLOCALHEADER_H