2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_zip_archive.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
2021-06-17 22:08:01 +02:00
|
|
|
#include <memory>
|
2014-07-11 22:10:05 +02:00
|
|
|
|
|
|
|
#include "include/base/cef_logging.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_stream.h"
|
|
|
|
#include "include/cef_zip_reader.h"
|
|
|
|
#include "include/wrapper/cef_byte_read_handler.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include <wctype.h>
|
|
|
|
#endif
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
// Convert |str| to lowercase in a Unicode-friendly manner.
|
|
|
|
CefString ToLower(const CefString& str) {
|
|
|
|
std::wstring wstr = str;
|
|
|
|
std::transform(wstr.begin(), wstr.end(), wstr.begin(), towlower);
|
|
|
|
return wstr;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
class CefZipFile : public CefZipArchive::File {
|
|
|
|
public:
|
2014-07-15 19:19:43 +02:00
|
|
|
CefZipFile() : data_size_(0) {}
|
|
|
|
|
2021-12-06 22:59:14 +01:00
|
|
|
CefZipFile(const CefZipFile&) = delete;
|
|
|
|
CefZipFile& operator=(const CefZipFile&) = delete;
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
bool Initialize(size_t data_size) {
|
2022-06-30 09:32:03 +02:00
|
|
|
data_.reset(new (std::nothrow) unsigned char[data_size]);
|
2014-07-15 19:19:43 +02:00
|
|
|
if (data_) {
|
|
|
|
data_size_ = data_size;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
DLOG(ERROR) << "Failed to allocate " << data_size << " bytes of memory";
|
|
|
|
data_size_ = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
virtual const unsigned char* GetData() const override { return data_.get(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
virtual size_t GetDataSize() const override { return data_size_; }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
virtual CefRefPtr<CefStreamReader> GetStreamReader() const override {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefReadHandler> handler(new CefByteReadHandler(
|
|
|
|
data_.get(), data_size_, const_cast<CefZipFile*>(this)));
|
2012-04-03 03:34:16 +02:00
|
|
|
return CefStreamReader::CreateForHandler(handler);
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
unsigned char* data() { return data_.get(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
private:
|
2014-07-15 19:19:43 +02:00
|
|
|
size_t data_size_;
|
2021-06-17 22:08:01 +02:00
|
|
|
std::unique_ptr<unsigned char[]> data_;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefZipFile);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// CefZipArchive implementation
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefZipArchive::CefZipArchive() {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefZipArchive::~CefZipArchive() {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
size_t CefZipArchive::Load(CefRefPtr<CefStreamReader> stream,
|
2013-06-10 20:23:48 +02:00
|
|
|
const CefString& password,
|
2012-04-03 03:34:16 +02:00
|
|
|
bool overwriteExisting) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefZipReader> reader(CefZipReader::Create(stream));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!reader.get()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!reader->MoveToFirstFile()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
FileMap::iterator it;
|
2014-07-15 19:19:43 +02:00
|
|
|
size_t count = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
do {
|
2014-07-15 19:19:43 +02:00
|
|
|
const size_t size = static_cast<size_t>(reader->GetFileSize());
|
2012-04-03 03:34:16 +02:00
|
|
|
if (size == 0) {
|
|
|
|
// Skip directories and empty files.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!reader->OpenFile(password)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
const CefString& name = ToLower(reader->GetFileName());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
it = contents_.find(name);
|
|
|
|
if (it != contents_.end()) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (overwriteExisting) {
|
2012-04-03 03:34:16 +02:00
|
|
|
contents_.erase(it);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else { // Skip files that already exist.
|
2012-04-03 03:34:16 +02:00
|
|
|
continue;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
CefRefPtr<CefZipFile> contents = new CefZipFile();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!contents->Initialize(size)) {
|
2014-07-15 19:19:43 +02:00
|
|
|
continue;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-07-15 19:19:43 +02:00
|
|
|
unsigned char* data = contents->data();
|
|
|
|
size_t offset = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Read the file contents.
|
|
|
|
do {
|
2017-05-17 11:29:28 +02:00
|
|
|
offset += reader->ReadFile(data + offset, size - offset);
|
2012-04-03 03:34:16 +02:00
|
|
|
} while (offset < size && !reader->Eof());
|
|
|
|
|
|
|
|
DCHECK(offset == size);
|
|
|
|
|
|
|
|
reader->CloseFile();
|
|
|
|
count++;
|
|
|
|
|
|
|
|
// Add the file to the map.
|
|
|
|
contents_.insert(std::make_pair(name, contents.get()));
|
|
|
|
} while (reader->MoveToNextFile());
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefZipArchive::Clear() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
contents_.clear();
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
size_t CefZipArchive::GetFileCount() const {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return contents_.size();
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
bool CefZipArchive::HasFile(const CefString& fileName) const {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-15 19:19:43 +02:00
|
|
|
FileMap::const_iterator it = contents_.find(ToLower(fileName));
|
2012-04-03 03:34:16 +02:00
|
|
|
return (it != contents_.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefZipArchive::File> CefZipArchive::GetFile(
|
2014-07-15 19:19:43 +02:00
|
|
|
const CefString& fileName) const {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-15 19:19:43 +02:00
|
|
|
FileMap::const_iterator it = contents_.find(ToLower(fileName));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != contents_.end()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return it->second;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefZipArchive::RemoveFile(const CefString& fileName) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-15 19:19:43 +02:00
|
|
|
FileMap::iterator it = contents_.find(ToLower(fileName));
|
2012-04-03 03:34:16 +02:00
|
|
|
if (it != contents_.end()) {
|
|
|
|
contents_.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
size_t CefZipArchive::GetFiles(FileMap& map) const {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
map = contents_;
|
|
|
|
return contents_.size();
|
|
|
|
}
|