summaryrefslogtreecommitdiffstats
path: root/xbmc/filesystem/ZipManager.cpp
blob: 710cfbc53d95ce774ac6bd02a858c3cfa42935a6 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "ZipManager.h"

#include <algorithm>
#include <utility>

#include "File.h"
#include "URL.h"
#if defined(TARGET_POSIX)
#include "PlatformDefs.h"
#endif
#include "utils/CharsetConverter.h"
#include "utils/EndianSwap.h"
#include "utils/log.h"
#include "utils/RegExp.h"
#include "utils/URIUtils.h"

using namespace XFILE;

static const size_t ZC_FLAG_EFS = 1 << 11; // general purpose bit 11 - zip holds utf-8 filenames

CZipManager::CZipManager() = default;

CZipManager::~CZipManager() = default;

bool CZipManager::GetZipList(const CURL& url, std::vector<SZipEntry>& items)
{
  struct __stat64 m_StatData = {};

  std::string strFile = url.GetHostName();

  if (CFile::Stat(strFile,&m_StatData))
  {
    CLog::Log(LOGDEBUG, "CZipManager::GetZipList: failed to stat file {}", url.GetRedacted());
    return false;
  }

  std::map<std::string, std::vector<SZipEntry> >::iterator it = mZipMap.find(strFile);
  if (it != mZipMap.end()) // already listed, just return it if not changed, else release and reread
  {
    std::map<std::string,int64_t>::iterator it2=mZipDate.find(strFile);

    if (m_StatData.st_mtime == it2->second)
    {
      items = it->second;
      return true;
    }
    mZipMap.erase(it);
    mZipDate.erase(it2);
  }

  CFile mFile;
  if (!mFile.Open(strFile))
  {
    CLog::Log(LOGDEBUG, "ZipManager: unable to open file {}!", strFile);
    return false;
  }

  unsigned int hdr;
  if (mFile.Read(&hdr, 4)!=4 || (Endian_SwapLE32(hdr) != ZIP_LOCAL_HEADER &&
                                 Endian_SwapLE32(hdr) != ZIP_DATA_RECORD_HEADER &&
                                 Endian_SwapLE32(hdr) != ZIP_SPLIT_ARCHIVE_HEADER))
  {
    CLog::Log(LOGDEBUG,"ZipManager: not a zip file!");
    mFile.Close();
    return false;
  }

  if (Endian_SwapLE32(hdr) == ZIP_SPLIT_ARCHIVE_HEADER)
    CLog::LogF(LOGWARNING, "ZIP split archive header found. Trying to process as a single archive..");

  // push date for update detection
  mZipDate.insert(make_pair(strFile,m_StatData.st_mtime));


  // Look for end of central directory record
  // Zipfile comment may be up to 65535 bytes
  // End of central directory record is 22 bytes (ECDREC_SIZE)
  // -> need to check the last 65557 bytes
  int64_t fileSize = mFile.GetLength();
  // Don't need to look in the last 18 bytes (ECDREC_SIZE-4)
  // But as we need to do overlapping between blocks (3 bytes),
  // we start the search at ECDREC_SIZE-1 from the end of file
  if (fileSize < ECDREC_SIZE - 1)
  {
    CLog::Log(LOGERROR, "ZipManager: Invalid zip file length: {}", fileSize);
    return false;
  }
  int searchSize = (int) std::min(static_cast<int64_t>(65557), fileSize-ECDREC_SIZE+1);
  int blockSize = (int) std::min(1024, searchSize);
  int nbBlock = searchSize / blockSize;
  int extraBlockSize = searchSize % blockSize;
  // Signature is on 4 bytes
  // It could be between 2 blocks, so we need to read 3 extra bytes
  std::vector<char> buffer(blockSize + 3);
  bool found = false;

  // Loop through blocks starting at the end of the file (minus ECDREC_SIZE-1)
  for (int nb=1; !found && (nb <= nbBlock); nb++)
  {
    mFile.Seek(fileSize-ECDREC_SIZE+1-(blockSize*nb),SEEK_SET);
    if (mFile.Read(buffer.data(), blockSize + 3) != blockSize + 3)
      return false;
    for (int i=blockSize-1; !found && (i >= 0); i--)
    {
      if (Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer.data() + i)) == ZIP_END_CENTRAL_HEADER)
      {
        // Set current position to start of end of central directory
        mFile.Seek(fileSize-ECDREC_SIZE+1-(blockSize*nb)+i,SEEK_SET);
        found = true;
      }
    }
  }

  // If not found, look in the last block left...
  if ( !found && (extraBlockSize > 0) )
  {
    mFile.Seek(fileSize-ECDREC_SIZE+1-searchSize,SEEK_SET);
    if (mFile.Read(buffer.data(), extraBlockSize + 3) != extraBlockSize + 3)
      return false;
    for (int i=extraBlockSize-1; !found && (i >= 0); i--)
    {
      if (Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer.data() + i)) == ZIP_END_CENTRAL_HEADER)
      {
        // Set current position to start of end of central directory
        mFile.Seek(fileSize-ECDREC_SIZE+1-searchSize+i,SEEK_SET);
        found = true;
      }
    }
  }

  buffer.clear();

  if ( !found )
  {
    CLog::Log(LOGDEBUG, "ZipManager: broken file {}!", strFile);
    mFile.Close();
    return false;
  }

  unsigned int cdirOffset, cdirSize;
  // Get size of the central directory
  mFile.Seek(12,SEEK_CUR);
  if (mFile.Read(&cdirSize, 4) != 4)
    return false;
  cdirSize = Endian_SwapLE32(cdirSize);
  // Get Offset of start of central directory with respect to the starting disk number
  if (mFile.Read(&cdirOffset, 4) != 4)
    return false;
  cdirOffset = Endian_SwapLE32(cdirOffset);

  // Go to the start of central directory
  mFile.Seek(cdirOffset,SEEK_SET);

  CRegExp pathTraversal;
  pathTraversal.RegComp(PATH_TRAVERSAL);

  char temp[CHDR_SIZE];
  while (mFile.GetPosition() < cdirOffset + cdirSize)
  {
    SZipEntry ze;
    if (mFile.Read(temp, CHDR_SIZE) != CHDR_SIZE)
      return false;
    readCHeader(temp, ze);
    if (ze.header != ZIP_CENTRAL_HEADER)
    {
      CLog::Log(LOGDEBUG, "ZipManager: broken file {}!", strFile);
      mFile.Close();
      return false;
    }

    // Get the filename just after the central file header
    std::vector<char> bufName(ze.flength);
    if (mFile.Read(bufName.data(), ze.flength) != ze.flength)
      return false;
    std::string strName(bufName.data(), bufName.size());
    bufName.clear();
    if ((ze.flags & ZC_FLAG_EFS) == 0)
    {
      std::string tmp(strName);
      g_charsetConverter.ToUtf8("CP437", tmp, strName);
    }
    memset(ze.name, 0, 255);
    strncpy(ze.name, strName.c_str(), strName.size() > 254 ? 254 : strName.size());

    // Jump after central file header extra field and file comment
    mFile.Seek(ze.eclength + ze.clength,SEEK_CUR);

    if (pathTraversal.RegFind(strName) < 0)
      items.push_back(ze);
  }

  /* go through list and figure out file header lengths */
  for (auto& ze : items)
  {
    // Go to the local file header to get the extra field length
    // !! local header extra field length != central file header extra field length !!
    mFile.Seek(ze.lhdrOffset+28,SEEK_SET);
    if (mFile.Read(&(ze.elength), 2) != 2)
      return false;
    ze.elength = Endian_SwapLE16(ze.elength);

    // Compressed data offset = local header offset + size of local header + filename length + local file header extra field length
    ze.offset = ze.lhdrOffset + LHDR_SIZE + ze.flength + ze.elength;

  }

  mZipMap.insert(make_pair(strFile,items));
  mFile.Close();
  return true;
}

bool CZipManager::GetZipEntry(const CURL& url, SZipEntry& item)
{
  const std::string& strFile = url.GetHostName();

  std::map<std::string, std::vector<SZipEntry> >::iterator it = mZipMap.find(strFile);
  std::vector<SZipEntry> items;
  if (it == mZipMap.end()) // we need to list the zip
  {
    GetZipList(url,items);
  }
  else
  {
    items = it->second;
  }

  const std::string& strFileName = url.GetFileName();
  for (const auto& it2 : items)
  {
    if (std::string(it2.name) == strFileName)
    {
      item = it2;
      return true;
    }
  }
  return false;
}

bool CZipManager::ExtractArchive(const std::string& strArchive, const std::string& strPath)
{
  const CURL pathToUrl(strArchive);
  return ExtractArchive(pathToUrl, strPath);
}

bool CZipManager::ExtractArchive(const CURL& archive, const std::string& strPath)
{
  std::vector<SZipEntry> entry;
  CURL url = URIUtils::CreateArchivePath("zip", archive);
  GetZipList(url, entry);
  for (const auto& it : entry)
  {
    if (it.name[strlen(it.name) - 1] == '/') // skip dirs
      continue;
    std::string strFilePath(it.name);

    CURL zipPath = URIUtils::CreateArchivePath("zip", archive, strFilePath);
    const CURL pathToUrl(strPath + strFilePath);
    if (!CFile::Copy(zipPath, pathToUrl))
      return false;
  }
  return true;
}

// Read local file header
void CZipManager::readHeader(const char* buffer, SZipEntry& info)
{
  info.header = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer));
  info.version = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 4));
  info.flags = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 6));
  info.method = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 8));
  info.mod_time = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 10));
  info.mod_date = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 12));
  info.crc32 = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer + 14));
  info.csize = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer + 18));
  info.usize = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer + 22));
  info.flength = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 26));
  info.elength = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 28));
}

// Read central file header (from central directory)
void CZipManager::readCHeader(const char* buffer, SZipEntry& info)
{
  info.header = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer));
  // Skip version made by
  info.version = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 6));
  info.flags = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 8));
  info.method = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 10));
  info.mod_time = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 12));
  info.mod_date = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 14));
  info.crc32 = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer + 16));
  info.csize = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer + 20));
  info.usize = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer + 24));
  info.flength = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 28));
  info.eclength = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 30));
  info.clength = Endian_SwapLE16(ReadUnaligned<uint16_t>(buffer + 32));
  // Skip disk number start, internal/external file attributes
  info.lhdrOffset = Endian_SwapLE32(ReadUnaligned<uint32_t>(buffer + 42));
}

void CZipManager::release(const std::string& strPath)
{
  CURL url(strPath);
  std::map<std::string, std::vector<SZipEntry> >::iterator it= mZipMap.find(url.GetHostName());
  if (it != mZipMap.end())
  {
    std::map<std::string,int64_t>::iterator it2=mZipDate.find(url.GetHostName());
    mZipMap.erase(it);
    mZipDate.erase(it2);
  }
}