2012-07-28 00:21:34 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=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"
|
2012-11-06 23:23:13 +00:00
|
|
|
#include "nsIPlatformCharset.h"
|
|
|
|
#include "nsNativeCharsetUtils.h"
|
2012-07-28 00:21:34 +00:00
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
|
2012-11-06 23:23:13 +00:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2012-07-28 00:21:34 +00:00
|
|
|
USING_FILE_NAMESPACE
|
|
|
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
# define PATH_MAX 65536 // The filename length is stored in 2 bytes
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ArchiveZipItem::ArchiveZipItem(const char* aFilename,
|
2012-11-06 23:23:13 +00:00
|
|
|
const ZipCentral& aCentralStruct,
|
2013-12-17 10:47:25 +00:00
|
|
|
const nsACString& aEncoding)
|
2012-07-28 00:21:34 +00:00
|
|
|
: mFilename(aFilename),
|
2012-11-06 23:23:13 +00:00
|
|
|
mCentralStruct(aCentralStruct),
|
2013-03-10 07:54:42 +00:00
|
|
|
mEncoding(aEncoding)
|
2012-07-28 00:21:34 +00:00
|
|
|
{
|
2012-08-08 18:11:15 +00:00
|
|
|
MOZ_COUNT_CTOR(ArchiveZipItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArchiveZipItem::~ArchiveZipItem()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ArchiveZipItem);
|
2012-07-28 00:21:34 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 23:23:13 +00:00
|
|
|
nsresult
|
|
|
|
ArchiveZipItem::ConvertFilename()
|
2012-07-28 00:21:34 +00:00
|
|
|
{
|
2013-03-10 07:54:42 +00:00
|
|
|
if (mEncoding.IsEmpty()) {
|
2012-11-06 23:23:13 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString filenameU;
|
2013-12-17 10:47:25 +00:00
|
|
|
nsresult rv = nsContentUtils::ConvertStringFromEncoding(
|
|
|
|
mEncoding,
|
2012-11-06 23:23:13 +00:00
|
|
|
mFilename, filenameU);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (filenameU.IsEmpty()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFilenameU = filenameU;
|
|
|
|
return NS_OK;
|
2012-07-28 00:21:34 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 23:23:13 +00:00
|
|
|
nsresult
|
|
|
|
ArchiveZipItem::GetFilename(nsString& aFilename)
|
2012-07-28 00:21:34 +00:00
|
|
|
{
|
2012-11-06 23:23:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2012-07-28 00:21:34 +00:00
|
|
|
|
2012-11-06 23:23:13 +00:00
|
|
|
aFilename = mFilenameU;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-28 00:21:34 +00:00
|
|
|
|
|
|
|
// From zipItem to DOMFile:
|
|
|
|
nsIDOMFile*
|
|
|
|
ArchiveZipItem::File(ArchiveReader* aArchiveReader)
|
|
|
|
{
|
2012-11-06 23:23:13 +00:00
|
|
|
nsString filename;
|
|
|
|
|
|
|
|
if (NS_FAILED(GetFilename(filename))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ArchiveZipFile(filename,
|
2012-07-28 00:21:34 +00:00
|
|
|
NS_ConvertUTF8toUTF16(GetType()),
|
|
|
|
StrToInt32(mCentralStruct.orglen),
|
|
|
|
mCentralStruct,
|
|
|
|
aArchiveReader);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
|
|
|
ArchiveZipItem::StrToInt32(const uint8_t* aStr)
|
2012-07-28 00:21:34 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
return (uint32_t)( (aStr [0] << 0) |
|
2012-07-28 00:21:34 +00:00
|
|
|
(aStr [1] << 8) |
|
|
|
|
(aStr [2] << 16) |
|
|
|
|
(aStr [3] << 24) );
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t
|
|
|
|
ArchiveZipItem::StrToInt16(const uint8_t* aStr)
|
2012-07-28 00:21:34 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
return (uint16_t) ((aStr [0]) | (aStr [1] << 8));
|
2012-07-28 00:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ArchiveReaderZipEvent
|
|
|
|
|
2012-11-06 23:23:13 +00:00
|
|
|
ArchiveReaderZipEvent::ArchiveReaderZipEvent(ArchiveReader* aArchiveReader,
|
2013-12-17 10:47:25 +00:00
|
|
|
const nsACString& aEncoding)
|
2012-11-06 23:23:13 +00:00
|
|
|
: ArchiveReaderEvent(aArchiveReader),
|
2013-03-10 07:54:42 +00:00
|
|
|
mEncoding(aEncoding)
|
2012-07-28 00:21:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: this runs in a different thread!!
|
|
|
|
nsresult
|
|
|
|
ArchiveReaderZipEvent::Exec()
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t centralOffset(0);
|
2012-07-28 00:21:34 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
rv = mArchiveReader->GetInputStream(getter_AddRefs(inputStream));
|
2012-08-18 01:04:03 +00:00
|
|
|
if (NS_FAILED(rv) || !inputStream) {
|
2012-07-28 00:21:34 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t size;
|
2012-07-28 00:21:34 +00:00
|
|
|
rv = mArchiveReader->GetSize(&size);
|
2012-08-18 01:04:03 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-28 00:21:34 +00:00
|
|
|
return RunShare(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading backward.. looking for the ZipEnd signature
|
2012-11-06 23:23:13 +00:00
|
|
|
for (uint64_t curr = size - ZIPEND_SIZE; curr > 4; --curr) {
|
2012-07-28 00:21:34 +00:00
|
|
|
seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, curr);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t buffer[ZIPEND_SIZE];
|
|
|
|
uint32_t ret;
|
2012-07-28 00:21:34 +00:00
|
|
|
|
|
|
|
rv = inputStream->Read((char*)buffer, sizeof(buffer), &ret);
|
2012-08-18 01:04:03 +00:00
|
|
|
if (NS_FAILED(rv) || ret != sizeof(buffer)) {
|
2012-07-28 00:21:34 +00:00
|
|
|
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;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t ret;
|
2012-07-28 00:21:34 +00:00
|
|
|
|
|
|
|
rv = inputStream->Read((char*)¢ralStruct, ZIPCENTRAL_SIZE, &ret);
|
2012-08-18 01:04:03 +00:00
|
|
|
if (NS_FAILED(rv) || ret != ZIPCENTRAL_SIZE) {
|
2012-07-28 00:21:34 +00:00
|
|
|
return RunShare(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t filenameLen = ArchiveZipItem::StrToInt16(centralStruct.filename_len);
|
|
|
|
uint16_t extraLen = ArchiveZipItem::StrToInt16(centralStruct.extrafield_len);
|
|
|
|
uint16_t commentLen = ArchiveZipItem::StrToInt16(centralStruct.commentfield_len);
|
2012-07-28 00:21:34 +00:00
|
|
|
|
|
|
|
// 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:
|
2012-12-05 07:55:37 +00:00
|
|
|
nsAutoArrayPtr<char> filename(new char[filenameLen + 1]);
|
2012-07-28 00:21:34 +00:00
|
|
|
rv = inputStream->Read(filename, filenameLen, &ret);
|
2012-08-18 01:04:03 +00:00
|
|
|
if (NS_FAILED(rv) || ret != filenameLen) {
|
2012-07-28 00:21:34 +00:00
|
|
|
return RunShare(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
filename[filenameLen] = 0;
|
|
|
|
|
|
|
|
// We ignore the directories:
|
|
|
|
if (filename[filenameLen - 1] != '/') {
|
2013-03-10 07:54:42 +00:00
|
|
|
mFileList.AppendElement(new ArchiveZipItem(filename, centralStruct, mEncoding));
|
2012-07-28 00:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the rest
|
|
|
|
seekableStream->Seek(nsISeekableStream::NS_SEEK_CUR, extraLen + commentLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RunShare(NS_OK);
|
|
|
|
}
|