1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/file/ArchiveZipEvent.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,210 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "ArchiveZipEvent.h" 1.11 +#include "ArchiveZipFile.h" 1.12 + 1.13 +#include "nsContentUtils.h" 1.14 +#include "nsCExternalHandlerService.h" 1.15 + 1.16 +using namespace mozilla::dom; 1.17 + 1.18 +USING_FILE_NAMESPACE 1.19 + 1.20 +#ifndef PATH_MAX 1.21 +# define PATH_MAX 65536 // The filename length is stored in 2 bytes 1.22 +#endif 1.23 + 1.24 +ArchiveZipItem::ArchiveZipItem(const char* aFilename, 1.25 + const ZipCentral& aCentralStruct, 1.26 + const nsACString& aEncoding) 1.27 +: mFilename(aFilename), 1.28 + mCentralStruct(aCentralStruct), 1.29 + mEncoding(aEncoding) 1.30 +{ 1.31 + MOZ_COUNT_CTOR(ArchiveZipItem); 1.32 +} 1.33 + 1.34 +ArchiveZipItem::~ArchiveZipItem() 1.35 +{ 1.36 + MOZ_COUNT_DTOR(ArchiveZipItem); 1.37 +} 1.38 + 1.39 +nsresult 1.40 +ArchiveZipItem::ConvertFilename() 1.41 +{ 1.42 + if (mEncoding.IsEmpty()) { 1.43 + return NS_ERROR_FAILURE; 1.44 + } 1.45 + 1.46 + nsString filenameU; 1.47 + nsresult rv = nsContentUtils::ConvertStringFromEncoding( 1.48 + mEncoding, 1.49 + mFilename, filenameU); 1.50 + NS_ENSURE_SUCCESS(rv, rv); 1.51 + 1.52 + if (filenameU.IsEmpty()) { 1.53 + return NS_ERROR_FAILURE; 1.54 + } 1.55 + 1.56 + mFilenameU = filenameU; 1.57 + return NS_OK; 1.58 +} 1.59 + 1.60 +nsresult 1.61 +ArchiveZipItem::GetFilename(nsString& aFilename) 1.62 +{ 1.63 + if (mFilenameU.IsEmpty()) { 1.64 + // Maybe this string is UTF-8: 1.65 + if (IsUTF8(mFilename, false)) { 1.66 + mFilenameU = NS_ConvertUTF8toUTF16(mFilename); 1.67 + } 1.68 + 1.69 + // Let's use the enconding value for the dictionary 1.70 + else { 1.71 + nsresult rv = ConvertFilename(); 1.72 + NS_ENSURE_SUCCESS(rv, rv); 1.73 + } 1.74 + } 1.75 + 1.76 + aFilename = mFilenameU; 1.77 + return NS_OK; 1.78 +} 1.79 + 1.80 +// From zipItem to DOMFile: 1.81 +nsIDOMFile* 1.82 +ArchiveZipItem::File(ArchiveReader* aArchiveReader) 1.83 +{ 1.84 + nsString filename; 1.85 + 1.86 + if (NS_FAILED(GetFilename(filename))) { 1.87 + return nullptr; 1.88 + } 1.89 + 1.90 + return new ArchiveZipFile(filename, 1.91 + NS_ConvertUTF8toUTF16(GetType()), 1.92 + StrToInt32(mCentralStruct.orglen), 1.93 + mCentralStruct, 1.94 + aArchiveReader); 1.95 +} 1.96 + 1.97 +uint32_t 1.98 +ArchiveZipItem::StrToInt32(const uint8_t* aStr) 1.99 +{ 1.100 + return (uint32_t)( (aStr [0] << 0) | 1.101 + (aStr [1] << 8) | 1.102 + (aStr [2] << 16) | 1.103 + (aStr [3] << 24) ); 1.104 +} 1.105 + 1.106 +uint16_t 1.107 +ArchiveZipItem::StrToInt16(const uint8_t* aStr) 1.108 +{ 1.109 + return (uint16_t) ((aStr [0]) | (aStr [1] << 8)); 1.110 +} 1.111 + 1.112 +// ArchiveReaderZipEvent 1.113 + 1.114 +ArchiveReaderZipEvent::ArchiveReaderZipEvent(ArchiveReader* aArchiveReader, 1.115 + const nsACString& aEncoding) 1.116 +: ArchiveReaderEvent(aArchiveReader), 1.117 + mEncoding(aEncoding) 1.118 +{ 1.119 +} 1.120 + 1.121 +// NOTE: this runs in a different thread!! 1.122 +nsresult 1.123 +ArchiveReaderZipEvent::Exec() 1.124 +{ 1.125 + uint32_t centralOffset(0); 1.126 + nsresult rv; 1.127 + 1.128 + nsCOMPtr<nsIInputStream> inputStream; 1.129 + rv = mArchiveReader->GetInputStream(getter_AddRefs(inputStream)); 1.130 + if (NS_FAILED(rv) || !inputStream) { 1.131 + return RunShare(NS_ERROR_UNEXPECTED); 1.132 + } 1.133 + 1.134 + // From the input stream to a seekable stream 1.135 + nsCOMPtr<nsISeekableStream> seekableStream; 1.136 + seekableStream = do_QueryInterface(inputStream); 1.137 + if (!seekableStream) { 1.138 + return RunShare(NS_ERROR_UNEXPECTED); 1.139 + } 1.140 + 1.141 + uint64_t size; 1.142 + rv = mArchiveReader->GetSize(&size); 1.143 + if (NS_FAILED(rv)) { 1.144 + return RunShare(NS_ERROR_UNEXPECTED); 1.145 + } 1.146 + 1.147 + // Reading backward.. looking for the ZipEnd signature 1.148 + for (uint64_t curr = size - ZIPEND_SIZE; curr > 4; --curr) { 1.149 + seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, curr); 1.150 + 1.151 + uint8_t buffer[ZIPEND_SIZE]; 1.152 + uint32_t ret; 1.153 + 1.154 + rv = inputStream->Read((char*)buffer, sizeof(buffer), &ret); 1.155 + if (NS_FAILED(rv) || ret != sizeof(buffer)) { 1.156 + return RunShare(NS_ERROR_UNEXPECTED); 1.157 + } 1.158 + 1.159 + // Here we are: 1.160 + if (ArchiveZipItem::StrToInt32(buffer) == ENDSIG) { 1.161 + centralOffset = ArchiveZipItem::StrToInt32(((ZipEnd*)buffer)->offset_central_dir); 1.162 + break; 1.163 + } 1.164 + } 1.165 + 1.166 + // No central Offset 1.167 + if (!centralOffset || centralOffset >= size - ZIPEND_SIZE) { 1.168 + return RunShare(NS_ERROR_FAILURE); 1.169 + } 1.170 + 1.171 + // Seek to the first central directory: 1.172 + seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, centralOffset); 1.173 + 1.174 + // For each central directory: 1.175 + while (centralOffset <= size - ZIPCENTRAL_SIZE) { 1.176 + ZipCentral centralStruct; 1.177 + uint32_t ret; 1.178 + 1.179 + rv = inputStream->Read((char*)¢ralStruct, ZIPCENTRAL_SIZE, &ret); 1.180 + if (NS_FAILED(rv) || ret != ZIPCENTRAL_SIZE) { 1.181 + return RunShare(NS_ERROR_UNEXPECTED); 1.182 + } 1.183 + 1.184 + uint16_t filenameLen = ArchiveZipItem::StrToInt16(centralStruct.filename_len); 1.185 + uint16_t extraLen = ArchiveZipItem::StrToInt16(centralStruct.extrafield_len); 1.186 + uint16_t commentLen = ArchiveZipItem::StrToInt16(centralStruct.commentfield_len); 1.187 + 1.188 + // Point to the next item at the top of loop 1.189 + centralOffset += ZIPCENTRAL_SIZE + filenameLen + extraLen + commentLen; 1.190 + if (filenameLen == 0 || filenameLen >= PATH_MAX || centralOffset >= size) { 1.191 + return RunShare(NS_ERROR_FILE_CORRUPTED); 1.192 + } 1.193 + 1.194 + // Read the name: 1.195 + nsAutoArrayPtr<char> filename(new char[filenameLen + 1]); 1.196 + rv = inputStream->Read(filename, filenameLen, &ret); 1.197 + if (NS_FAILED(rv) || ret != filenameLen) { 1.198 + return RunShare(NS_ERROR_UNEXPECTED); 1.199 + } 1.200 + 1.201 + filename[filenameLen] = 0; 1.202 + 1.203 + // We ignore the directories: 1.204 + if (filename[filenameLen - 1] != '/') { 1.205 + mFileList.AppendElement(new ArchiveZipItem(filename, centralStruct, mEncoding)); 1.206 + } 1.207 + 1.208 + // Ignore the rest 1.209 + seekableStream->Seek(nsISeekableStream::NS_SEEK_CUR, extraLen + commentLen); 1.210 + } 1.211 + 1.212 + return RunShare(NS_OK); 1.213 +}