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>
|
2014-07-11 22:10:05 +02:00
|
|
|
|
|
|
|
#include "include/base/cef_logging.h"
|
|
|
|
#include "include/base/cef_macros.h"
|
2014-07-15 19:19:43 +02:00
|
|
|
#include "include/base/cef_scoped_ptr.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) {}
|
|
|
|
|
|
|
|
bool Initialize(size_t data_size) {
|
|
|
|
data_.reset(new unsigned char[data_size]);
|
|
|
|
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
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
virtual const unsigned char* GetData() const OVERRIDE { return data_.get(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
virtual size_t GetDataSize() const OVERRIDE { return data_size_; }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
virtual CefRefPtr<CefStreamReader> GetStreamReader() const OVERRIDE {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefReadHandler> handler(
|
2014-07-15 19:19:43 +02:00
|
|
|
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_;
|
2016-05-25 01:35:43 +02:00
|
|
|
SCOPED_PTR(unsigned char[]) data_;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefZipFile);
|
2014-07-15 19:19:43 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefZipFile);
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// CefZipArchive implementation
|
|
|
|
|
|
|
|
CefZipArchive::CefZipArchive() {
|
|
|
|
}
|
|
|
|
|
|
|
|
CefZipArchive::~CefZipArchive() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
if (!reader.get())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!reader->MoveToFirstFile())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-10 20:23:48 +02:00
|
|
|
if (!reader->OpenFile(password))
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
|
|
|
|
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()) {
|
|
|
|
if (overwriteExisting)
|
|
|
|
contents_.erase(it);
|
|
|
|
else // Skip files that already exist.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
CefRefPtr<CefZipFile> contents = new CefZipFile();
|
|
|
|
if (!contents->Initialize(size))
|
|
|
|
continue;
|
|
|
|
unsigned char* data = contents->data();
|
|
|
|
size_t offset = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Read the file contents.
|
|
|
|
do {
|
2014-07-15 19:19:43 +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));
|
2012-04-03 03:34:16 +02:00
|
|
|
if (it != contents_.end())
|
|
|
|
return it->second;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|