summaryrefslogtreecommitdiff
path: root/src/archive.cpp
blob: 468efed5c125227b1e292a09b06e52be37f42474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "archive.hpp"

#include "main_window.hpp"

#include <algorithm>

namespace getsuyomi {

using fud::FudStatus;

ZipArchive::ZipArchive(const QString& filename)
{
    auto filenameUtf8 = filename.toUtf8();
    qDebug("Open file %s", filenameUtf8.data());
    int err;
    m_archive = zip_open(filenameUtf8.data(), 0, &err);

    if (m_archive == nullptr) {
        zip_error_t error;
        zip_error_init_with_code(&error, err);
        qCritical("%s: cannot open zip archive '%s': %s", AppName, filenameUtf8.data(), zip_error_strerror(&error));
        zip_error_fini(&error);
        return;
    }

    auto status = populate();
    if (status != FudStatus::Success) {
        cleanup();
        return;
    }
}

ZipArchive::ZipArchive(ZipArchive&& rhs)
    :m_archive{rhs.m_archive},
     m_sortedIndices{std::move(rhs.m_sortedIndices)},
     m_filenames{std::move(rhs.m_filenames)},
     m_fileSizes{std::move(rhs.m_fileSizes)},
     m_pages{std::move(rhs.m_pages)}
{
    rhs.m_archive = nullptr;
}

ZipArchive::~ZipArchive()
{
    cleanup();
}

ZipArchive& ZipArchive::operator=(ZipArchive&& rhs)
{
    m_archive = rhs.m_archive;
    m_sortedIndices = std::move(rhs.m_sortedIndices);
    m_filenames = std::move(rhs.m_filenames);
    m_fileSizes = std::move(rhs.m_fileSizes);
    m_pages = std::move(rhs.m_pages);

    rhs.m_archive = nullptr;

    return *this;
}

bool ZipArchive::valid() const {
    return m_archive != nullptr;
}

void ZipArchive::cleanup() {
    if (m_archive != nullptr) {
        zip_discard(m_archive);
        m_archive = nullptr;
    }
}

fud::FudStatus ZipArchive::populate()
{
    if (!valid()) {
        qCritical("Invalid zipArchive");
        return FudStatus::ObjectInvalid;
    }

    auto numEntriesResult = zip_get_num_entries(m_archive, ZIP_FL_UNCHANGED);
    if (numEntriesResult < 0) {
        return FudStatus::Failure;
    }
    size_t numEntries = static_cast<size_t>(numEntriesResult);

    struct NameIndex {
        std::string name;
        size_t index;
        size_t filesize;
    };

    std::vector<NameIndex> nameIndices{};
    nameIndices.reserve(numEntries);
    m_pages.reserve(numEntries);

    for (size_t idx = 0; idx < numEntries; ++idx) {
        struct zip_stat stats;

        auto status = zip_stat_index(m_archive, idx, 0, &stats);
        if (status < 0) {
            return FudStatus::Failure;
        }
        static_assert(ZIP_STAT_NAME != 0);
        static_assert(ZIP_STAT_SIZE != 0);
        static_assert(ZIP_STAT_INDEX != 0);

        auto* nameCString = stats.name;
        if (nameCString == nullptr) {
            zip_error_t* error = zip_get_error(m_archive);
            qWarning("cannot get name %s", zip_error_strerror(error));
            continue;
        }
        std::string name{nameCString};
        if (name.empty() || name.back() == '/') {
            continue;
        }

        nameIndices.emplace_back(NameIndex{name, idx, stats.size});
        m_pages.emplace_back(std::nullopt);
    }

    if (m_pages.empty()) {
        return FudStatus::Empty;
    }

    std::sort(nameIndices.begin(), nameIndices.end(), [](const auto& lhs, const auto& rhs) {
        return std::lexicographical_compare(lhs.name.begin(), lhs.name.end(), rhs.name.begin(), rhs.name.end());
    });

    m_sortedIndices.reserve(nameIndices.size());
    m_filenames.reserve(nameIndices.size());
    m_fileSizes.reserve(nameIndices.size());

    for (const auto& nameIndex : nameIndices) {
        m_sortedIndices.push_back(nameIndex.index);
        m_filenames.push_back(nameIndex.name);
        m_fileSizes.push_back(nameIndex.filesize);
    }

    return FudStatus::Success;
}

ArchiveResult ZipArchive::getPage(size_t page)
{
    if (page > m_sortedIndices.size()) {
        return ArchiveResult::error(ArchiveError::BadIndex);
    }

    if (m_pages[page] != std::nullopt) {
        return ArchiveResult::okay(std::cref(*m_pages[page]));
    }

    auto* file = zip_fopen_index(m_archive, m_sortedIndices[page], 0);
    if (file == nullptr) {
        zip_error_t* error = zip_get_error(m_archive);
        qWarning("cannot get file name %s: %s", m_filenames[page].data(), zip_error_strerror(error));
        return ArchiveResult::error(ArchiveError::ZipError);
    }

    QByteArray data;
    data.resize(static_cast<qsizetype>(m_fileSizes[page]));

    auto bytesRead = zip_fread(file, data.data(), static_cast<size_t>(data.size()));
    zip_fclose(file);
    file = nullptr;

    if (bytesRead != data.size()) {
        return ArchiveResult::error(ArchiveError::ZipError);
    }

    m_pages[page] = QImage();
    auto loaded = m_pages[page]->loadFromData(data);
    if (!loaded) {
        qWarning("Failed to load QImage");
        m_pages[page] = std::nullopt;
        return ArchiveResult::error(ArchiveError::BadData);
    }

    return ArchiveResult::okay(std::cref(*m_pages[page]));
}

size_t ZipArchive::numPages() const {
    return m_pages.size();
}

} // namespace getsuyomi