gecko/dom/archivereader/ArchiveZipEvent.cpp

213 lines
5.7 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ArchiveZipEvent.h"
#include "ArchiveZipFile.h"
#include "nsContentUtils.h"
#include "nsCExternalHandlerService.h"
using namespace mozilla::dom;
Bug 975696 - Split ArchiveReader and FileHandle implementation into separate dirs. r=bent f=baku --HG-- rename : dom/file/ArchiveEvent.cpp => dom/archivereader/ArchiveEvent.cpp rename : dom/file/ArchiveEvent.h => dom/archivereader/ArchiveEvent.h rename : dom/file/ArchiveReader.cpp => dom/archivereader/ArchiveReader.cpp rename : dom/file/ArchiveReader.h => dom/archivereader/ArchiveReader.h rename : dom/file/ArchiveRequest.cpp => dom/archivereader/ArchiveRequest.cpp rename : dom/file/ArchiveRequest.h => dom/archivereader/ArchiveRequest.h rename : dom/file/ArchiveZipEvent.cpp => dom/archivereader/ArchiveZipEvent.cpp rename : dom/file/ArchiveZipEvent.h => dom/archivereader/ArchiveZipEvent.h rename : dom/file/ArchiveZipFile.cpp => dom/archivereader/ArchiveZipFile.cpp rename : dom/file/ArchiveZipFile.h => dom/archivereader/ArchiveZipFile.h rename : dom/file/test/test_archivereader.html => dom/archivereader/test/test_basic.html rename : dom/file/test/test_archivereader_nonUnicode.html => dom/archivereader/test/test_nonUnicode.html rename : dom/file/test/test_archivereader_zip_in_zip.html => dom/archivereader/test/test_zip_in_zip.html rename : dom/file/test/test_bug_793311.html => dom/base/test/test_bug793311.html rename : dom/file/AsyncHelper.cpp => dom/filehandle/AsyncHelper.cpp rename : dom/file/AsyncHelper.h => dom/filehandle/AsyncHelper.h rename : dom/file/File.cpp => dom/filehandle/File.cpp rename : dom/file/File.h => dom/filehandle/File.h rename : dom/file/FileHandle.cpp => dom/filehandle/FileHandle.cpp rename : dom/file/FileHandle.h => dom/filehandle/FileHandle.h rename : dom/file/FileHelper.cpp => dom/filehandle/FileHelper.cpp rename : dom/file/FileHelper.h => dom/filehandle/FileHelper.h rename : dom/file/FileRequest.cpp => dom/filehandle/FileRequest.cpp rename : dom/file/FileRequest.h => dom/filehandle/FileRequest.h rename : dom/file/FileService.cpp => dom/filehandle/FileService.cpp rename : dom/file/FileService.h => dom/filehandle/FileService.h rename : dom/file/FileStreamWrappers.cpp => dom/filehandle/FileStreamWrappers.cpp rename : dom/file/FileStreamWrappers.h => dom/filehandle/FileStreamWrappers.h rename : dom/file/LockedFile.cpp => dom/filehandle/LockedFile.cpp rename : dom/file/LockedFile.h => dom/filehandle/LockedFile.h rename : dom/file/MemoryStreams.cpp => dom/filehandle/MemoryStreams.cpp rename : dom/file/MemoryStreams.h => dom/filehandle/MemoryStreams.h rename : dom/file/MetadataHelper.cpp => dom/filehandle/MetadataHelper.cpp rename : dom/file/MetadataHelper.h => dom/filehandle/MetadataHelper.h rename : dom/file/moz.build => dom/filehandle/moz.build rename : dom/file/nsIFileStorage.h => dom/filehandle/nsIFileStorage.h rename : dom/file/test/dummy_worker.js => dom/filehandle/test/dummy_worker.js rename : dom/file/test/helpers.js => dom/filehandle/test/helpers.js rename : dom/file/test/mochitest.ini => dom/filehandle/test/mochitest.ini rename : dom/file/test/moz.build => dom/filehandle/test/moz.build rename : dom/file/test/test_append_read_data.html => dom/filehandle/test/test_append_read_data.html rename : dom/file/test/test_getFile.html => dom/filehandle/test/test_getFile.html rename : dom/file/test/test_getFileId.html => dom/filehandle/test/test_getFileId.html rename : dom/file/test/test_location.html => dom/filehandle/test/test_location.html rename : dom/file/test/test_lockedfile_lifetimes.html => dom/filehandle/test/test_lockedfile_lifetimes.html rename : dom/file/test/test_lockedfile_lifetimes_nested.html => dom/filehandle/test/test_lockedfile_lifetimes_nested.html rename : dom/file/test/test_lockedfile_ordering.html => dom/filehandle/test/test_lockedfile_ordering.html rename : dom/file/test/test_overlapping_lockedfiles.html => dom/filehandle/test/test_overlapping_lockedfiles.html rename : dom/file/test/test_progress_events.html => dom/filehandle/test/test_progress_events.html rename : dom/file/test/test_readonly_lockedfiles.html => dom/filehandle/test/test_readonly_lockedfiles.html rename : dom/file/test/test_request_readyState.html => dom/filehandle/test/test_request_readyState.html rename : dom/file/test/test_stream_tracking.html => dom/filehandle/test/test_stream_tracking.html rename : dom/file/test/test_success_events_after_abort.html => dom/filehandle/test/test_success_events_after_abort.html rename : dom/file/test/test_truncate.html => dom/filehandle/test/test_truncate.html rename : dom/file/test/test_workers.html => dom/filehandle/test/test_workers.html rename : dom/file/test/test_write_read_data.html => dom/filehandle/test/test_write_read_data.html
2014-05-07 07:32:12 -07:00
USING_ARCHIVEREADER_NAMESPACE
#ifndef PATH_MAX
# define PATH_MAX 65536 // The filename length is stored in 2 bytes
#endif
ArchiveZipItem::ArchiveZipItem(const char* aFilename,
const ZipCentral& aCentralStruct,
const nsACString& aEncoding)
: mFilename(aFilename),
mCentralStruct(aCentralStruct),
mEncoding(aEncoding)
{
MOZ_COUNT_CTOR(ArchiveZipItem);
}
ArchiveZipItem::~ArchiveZipItem()
{
MOZ_COUNT_DTOR(ArchiveZipItem);
}
nsresult
ArchiveZipItem::ConvertFilename()
{
if (mEncoding.IsEmpty()) {
return NS_ERROR_FAILURE;
}
nsString filenameU;
nsresult rv = nsContentUtils::ConvertStringFromEncoding(
mEncoding,
mFilename, filenameU);
NS_ENSURE_SUCCESS(rv, rv);
if (filenameU.IsEmpty()) {
return NS_ERROR_FAILURE;
}
mFilenameU = filenameU;
return NS_OK;
}
nsresult
ArchiveZipItem::GetFilename(nsString& aFilename)
{
if (mFilenameU.IsEmpty()) {
// Maybe this string is UTF-8:
if (IsUTF8(mFilename, false)) {
mFilenameU = NS_ConvertUTF8toUTF16(mFilename);
}
// Let's use the enconding value for the dictionary
else {
nsresult rv = ConvertFilename();
NS_ENSURE_SUCCESS(rv, rv);
}
}
aFilename = mFilenameU;
return NS_OK;
}
// From zipItem to File:
already_AddRefed<File>
ArchiveZipItem::GetFile(ArchiveReader* aArchiveReader)
{
nsString filename;
if (NS_FAILED(GetFilename(filename))) {
return nullptr;
}
nsRefPtr<dom::File> file = dom::File::Create(aArchiveReader,
new ArchiveZipBlobImpl(filename,
NS_ConvertUTF8toUTF16(GetType()),
StrToInt32(mCentralStruct.orglen),
mCentralStruct, aArchiveReader->GetBlobImpl()));
MOZ_ASSERT(file);
return file.forget();
}
uint32_t
ArchiveZipItem::StrToInt32(const uint8_t* aStr)
{
return (uint32_t)( (aStr [0] << 0) |
(aStr [1] << 8) |
(aStr [2] << 16) |
(aStr [3] << 24) );
}
uint16_t
ArchiveZipItem::StrToInt16(const uint8_t* aStr)
{
return (uint16_t) ((aStr [0]) | (aStr [1] << 8));
}
// ArchiveReaderZipEvent
ArchiveReaderZipEvent::ArchiveReaderZipEvent(ArchiveReader* aArchiveReader,
const nsACString& aEncoding)
: ArchiveReaderEvent(aArchiveReader),
mEncoding(aEncoding)
{
}
// NOTE: this runs in a different thread!!
nsresult
ArchiveReaderZipEvent::Exec()
{
uint32_t centralOffset(0);
nsresult rv;
nsCOMPtr<nsIInputStream> inputStream;
rv = mArchiveReader->GetInputStream(getter_AddRefs(inputStream));
if (NS_FAILED(rv) || !inputStream) {
return RunShare(NS_ERROR_UNEXPECTED);
}
// From the input stream to a seekable stream
nsCOMPtr<nsISeekableStream> seekableStream;
seekableStream = do_QueryInterface(inputStream);
if (!seekableStream) {
return RunShare(NS_ERROR_UNEXPECTED);
}
uint64_t size;
rv = mArchiveReader->GetSize(&size);
if (NS_FAILED(rv)) {
return RunShare(NS_ERROR_UNEXPECTED);
}
// Reading backward.. looking for the ZipEnd signature
for (uint64_t curr = size - ZIPEND_SIZE; curr > 4; --curr) {
seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, curr);
uint8_t buffer[ZIPEND_SIZE];
uint32_t ret;
rv = inputStream->Read((char*)buffer, sizeof(buffer), &ret);
if (NS_FAILED(rv) || ret != sizeof(buffer)) {
return RunShare(NS_ERROR_UNEXPECTED);
}
// Here we are:
if (ArchiveZipItem::StrToInt32(buffer) == ENDSIG) {
centralOffset = ArchiveZipItem::StrToInt32(((ZipEnd*)buffer)->offset_central_dir);
break;
}
}
// No central Offset
if (!centralOffset || centralOffset >= size - ZIPEND_SIZE) {
return RunShare(NS_ERROR_FAILURE);
}
// Seek to the first central directory:
seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, centralOffset);
// For each central directory:
while (centralOffset <= size - ZIPCENTRAL_SIZE) {
ZipCentral centralStruct;
uint32_t ret;
rv = inputStream->Read((char*)&centralStruct, ZIPCENTRAL_SIZE, &ret);
if (NS_FAILED(rv) || ret != ZIPCENTRAL_SIZE) {
return RunShare(NS_ERROR_UNEXPECTED);
}
uint16_t filenameLen = ArchiveZipItem::StrToInt16(centralStruct.filename_len);
uint16_t extraLen = ArchiveZipItem::StrToInt16(centralStruct.extrafield_len);
uint16_t commentLen = ArchiveZipItem::StrToInt16(centralStruct.commentfield_len);
// Point to the next item at the top of loop
centralOffset += ZIPCENTRAL_SIZE + filenameLen + extraLen + commentLen;
if (filenameLen == 0 || filenameLen >= PATH_MAX || centralOffset >= size) {
return RunShare(NS_ERROR_FILE_CORRUPTED);
}
// Read the name:
nsAutoArrayPtr<char> filename(new char[filenameLen + 1]);
rv = inputStream->Read(filename, filenameLen, &ret);
if (NS_FAILED(rv) || ret != filenameLen) {
return RunShare(NS_ERROR_UNEXPECTED);
}
filename[filenameLen] = 0;
// We ignore the directories:
if (filename[filenameLen - 1] != '/') {
mFileList.AppendElement(new ArchiveZipItem(filename, centralStruct, mEncoding));
}
// Ignore the rest
seekableStream->Seek(nsISeekableStream::NS_SEEK_CUR, extraLen + commentLen);
}
return RunShare(NS_OK);
}