1f8e2c85aSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3f8e2c85aSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4f8e2c85aSAndrew Rist * or more contributor license agreements. See the NOTICE file 5f8e2c85aSAndrew Rist * distributed with this work for additional information 6f8e2c85aSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7f8e2c85aSAndrew Rist * to you under the Apache License, Version 2.0 (the 8f8e2c85aSAndrew Rist * "License"); you may not use this file except in compliance 9f8e2c85aSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11f8e2c85aSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13f8e2c85aSAndrew Rist * Unless required by applicable law or agreed to in writing, 14f8e2c85aSAndrew Rist * software distributed under the License is distributed on an 15f8e2c85aSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16f8e2c85aSAndrew Rist * KIND, either express or implied. See the License for the 17f8e2c85aSAndrew Rist * specific language governing permissions and limitations 18f8e2c85aSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20f8e2c85aSAndrew Rist *************************************************************/ 21f8e2c85aSAndrew Rist 22f8e2c85aSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_shell.hxx" 26cdf0e10cSrcweir #include "zipexcptn.hxx" 27cdf0e10cSrcweir #include "internal/zipfile.hxx" 28cdf0e10cSrcweir #include "internal/global.hxx" 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <malloc.h> 31cdf0e10cSrcweir #include <algorithm> 32cdf0e10cSrcweir #include <functional> 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include <string.h> 35cdf0e10cSrcweir 36cdf0e10cSrcweir #ifdef OS2 37cdf0e10cSrcweir #include <alloca.h> 38cdf0e10cSrcweir #define _alloca alloca 39cdf0e10cSrcweir #define ERROR_NOT_ENOUGH_MEMORY 8 40cdf0e10cSrcweir #endif 41cdf0e10cSrcweir 42cdf0e10cSrcweir namespace internal 43cdf0e10cSrcweir { 44cdf0e10cSrcweir /* for case in-sensitive string comparison */ 45cdf0e10cSrcweir struct stricmp : public std::unary_function<std::string, bool> 46cdf0e10cSrcweir { 47cdf0e10cSrcweir stricmp(const std::string& str) : str_(str) 48cdf0e10cSrcweir {} 49cdf0e10cSrcweir 50cdf0e10cSrcweir bool operator() (const std::string& other) 51cdf0e10cSrcweir { return (0 == _stricmp(str_.c_str(), other.c_str())); } 52cdf0e10cSrcweir 53cdf0e10cSrcweir std::string str_; 54cdf0e10cSrcweir }; 55cdf0e10cSrcweir } // namespace internal 56cdf0e10cSrcweir 57cdf0e10cSrcweir /** Checks whether a file is a zip file or not 58cdf0e10cSrcweir 59cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 60cdf0e10cSrcweir The file must exist 61cdf0e10cSrcweir The file must be readable for the current user 62cdf0e10cSrcweir 63cdf0e10cSrcweir @returns true if the file is a zip file 64cdf0e10cSrcweir false if the file is not a zip file 65cdf0e10cSrcweir 66cdf0e10cSrcweir @throws ParameterException if the given file name is empty 67cdf0e10cSrcweir IOException if the specified file doesn't exist 68cdf0e10cSrcweir AccessViolationException if read access to the file is denied 69cdf0e10cSrcweir */ 70cdf0e10cSrcweir bool ZipFile::IsZipFile(const std::string& /*FileName*/) 71cdf0e10cSrcweir { 72cdf0e10cSrcweir return true; 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir bool ZipFile::IsZipFile(void* /*stream*/) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir return true; 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir 81cdf0e10cSrcweir /** Returns wheter the version of the specified zip file may be uncompressed with the 82cdf0e10cSrcweir currently used zlib version or not 83cdf0e10cSrcweir 84cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 85cdf0e10cSrcweir The file must exist 86cdf0e10cSrcweir The file must be readable for the current user 87cdf0e10cSrcweir The file must be a valid zip file 88cdf0e10cSrcweir 89cdf0e10cSrcweir @returns true if the file may be uncompressed with the currently used zlib 90cdf0e10cSrcweir false if the file may not be uncompressed with the currently used zlib 91cdf0e10cSrcweir 92cdf0e10cSrcweir @throws ParameterException if the given file name is empty 93cdf0e10cSrcweir IOException if the specified file doesn't exist or is no zip file 94cdf0e10cSrcweir AccessViolationException if read access to the file is denied 95cdf0e10cSrcweir */ 96cdf0e10cSrcweir bool ZipFile::IsValidZipFileVersionNumber(const std::string& /*FileName*/) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir return true; 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir bool ZipFile::IsValidZipFileVersionNumber(void* /* stream*/) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir return true; 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir 107cdf0e10cSrcweir /** Constructs a zip file from a zip file 108cdf0e10cSrcweir 109cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 110cdf0e10cSrcweir The file must exist 111cdf0e10cSrcweir The file must be readable for the current user 112cdf0e10cSrcweir 113cdf0e10cSrcweir @throws ParameterException if the given file name is empty 114cdf0e10cSrcweir IOException if the specified file doesn't exist or is no valid zip file 115cdf0e10cSrcweir AccessViolationException if read access to the file is denied 116cdf0e10cSrcweir WrongZipVersionException if the zip file cannot be uncompressed 117cdf0e10cSrcweir with the used zlib version 118cdf0e10cSrcweir */ 119cdf0e10cSrcweir ZipFile::ZipFile(const std::string& FileName) 120cdf0e10cSrcweir { 121cdf0e10cSrcweir m_uzFile = unzOpen(FileName.c_str()); 122cdf0e10cSrcweir 123cdf0e10cSrcweir if (0 == m_uzFile) 124cdf0e10cSrcweir throw IOException(-1); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir 127cdf0e10cSrcweir ZipFile::ZipFile(void* stream, zlib_filefunc_def* fa) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir fa->opaque = stream; 130cdf0e10cSrcweir m_uzFile = unzOpen2((const char *)NULL, fa); 131cdf0e10cSrcweir 132cdf0e10cSrcweir if (0 == m_uzFile) 133cdf0e10cSrcweir throw IOException(-1); 134cdf0e10cSrcweir } 135cdf0e10cSrcweir 136cdf0e10cSrcweir 137cdf0e10cSrcweir /** Destroys a zip file 138cdf0e10cSrcweir */ 139cdf0e10cSrcweir ZipFile::~ZipFile() 140cdf0e10cSrcweir { 141cdf0e10cSrcweir unzClose(m_uzFile); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir /** Provides an interface to read the uncompressed data of a content of the zip file 145cdf0e10cSrcweir 146cdf0e10cSrcweir @precond The specified content must exist in this file 147cdf0e10cSrcweir ppstm must not be NULL 148cdf0e10cSrcweir */ 149cdf0e10cSrcweir void ZipFile::GetUncompressedContent( 150cdf0e10cSrcweir const std::string& ContentName, /*inout*/ ZipContentBuffer_t& ContentBuffer) 151cdf0e10cSrcweir { 152cdf0e10cSrcweir int rc = unzLocateFile(m_uzFile, ContentName.c_str(), 0); 153cdf0e10cSrcweir 154cdf0e10cSrcweir if (UNZ_END_OF_LIST_OF_FILE == rc) 155cdf0e10cSrcweir throw ZipContentMissException(rc); 156cdf0e10cSrcweir 157cdf0e10cSrcweir unz_file_info finfo; 158cdf0e10cSrcweir unzGetCurrentFileInfo(m_uzFile, &finfo, 0, 0, 0, 0, 0, 0); 159cdf0e10cSrcweir 160cdf0e10cSrcweir ContentBuffer.resize(finfo.uncompressed_size); 161cdf0e10cSrcweir 162cdf0e10cSrcweir rc = unzOpenCurrentFile(m_uzFile); 163cdf0e10cSrcweir 164cdf0e10cSrcweir if (UNZ_OK != rc) 165cdf0e10cSrcweir throw ZipException(rc); 166cdf0e10cSrcweir 167cdf0e10cSrcweir rc = unzReadCurrentFile(m_uzFile, &ContentBuffer[0], finfo.uncompressed_size); 168cdf0e10cSrcweir 169cdf0e10cSrcweir if (rc < 0) 170cdf0e10cSrcweir throw ZipException(rc); 171cdf0e10cSrcweir 172cdf0e10cSrcweir rc = unzCloseCurrentFile(m_uzFile); 173cdf0e10cSrcweir 174cdf0e10cSrcweir if (rc < 0) 175cdf0e10cSrcweir throw ZipException(rc); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir /** Returns a list with the content names contained within this file 179cdf0e10cSrcweir 180cdf0e10cSrcweir */ 181cdf0e10cSrcweir ZipFile::DirectoryPtr_t ZipFile::GetDirectory() const 182cdf0e10cSrcweir { 183cdf0e10cSrcweir DirectoryPtr_t dir(new Directory_t()); 184cdf0e10cSrcweir 185cdf0e10cSrcweir long lmax = GetFileLongestFileNameLength() + 1; 186cdf0e10cSrcweir char* szFileName = reinterpret_cast<char*>(_alloca(lmax)); 187cdf0e10cSrcweir 188cdf0e10cSrcweir if (!szFileName) 189cdf0e10cSrcweir throw ZipException(ERROR_NOT_ENOUGH_MEMORY); 190cdf0e10cSrcweir 191cdf0e10cSrcweir int rc = unzGoToFirstFile(m_uzFile); 192cdf0e10cSrcweir 193cdf0e10cSrcweir while (UNZ_OK == rc && UNZ_END_OF_LIST_OF_FILE != rc) 194cdf0e10cSrcweir { 1955448f169SMichael Stahl unz_file_info finfo; 196cdf0e10cSrcweir unzGetCurrentFileInfo( 1975448f169SMichael Stahl m_uzFile, &finfo, szFileName, lmax, 0, 0, 0, 0); 198cdf0e10cSrcweir 199cdf0e10cSrcweir dir->push_back(szFileName); 200cdf0e10cSrcweir 201cdf0e10cSrcweir rc = unzGoToNextFile(m_uzFile); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir if (UNZ_OK != rc && UNZ_END_OF_LIST_OF_FILE != rc) 205cdf0e10cSrcweir throw ZipException(rc); 206cdf0e10cSrcweir 207cdf0e10cSrcweir return dir; 208cdf0e10cSrcweir } 209cdf0e10cSrcweir 210*30acf5e8Spfg /** Convenience query function may even realized with 211cdf0e10cSrcweir iterating over a ZipFileDirectory returned by 212cdf0e10cSrcweir GetDirectory */ 213cdf0e10cSrcweir bool ZipFile::HasContent(const std::string& ContentName) const 214cdf0e10cSrcweir { 215cdf0e10cSrcweir //#i34314# we need to compare package content names 216cdf0e10cSrcweir //case in-sensitive as it is not defined that such 217cdf0e10cSrcweir //names must be handled case sensitive 218cdf0e10cSrcweir DirectoryPtr_t dir = GetDirectory(); 219cdf0e10cSrcweir Directory_t::iterator iter = 220cdf0e10cSrcweir std::find_if(dir->begin(), dir->end(), internal::stricmp(ContentName)); 221cdf0e10cSrcweir 222cdf0e10cSrcweir return (iter != dir->end()); 223cdf0e10cSrcweir } 224cdf0e10cSrcweir 225cdf0e10cSrcweir 226cdf0e10cSrcweir /** Returns the length of the longest file name 227cdf0e10cSrcweir in the current zip file 228cdf0e10cSrcweir */ 229cdf0e10cSrcweir long ZipFile::GetFileLongestFileNameLength() const 230cdf0e10cSrcweir { 231cdf0e10cSrcweir long lmax = 0; 232cdf0e10cSrcweir unz_file_info finfo; 233cdf0e10cSrcweir 234cdf0e10cSrcweir int rc = unzGoToFirstFile(m_uzFile); 235cdf0e10cSrcweir 236cdf0e10cSrcweir while (UNZ_OK == rc && UNZ_END_OF_LIST_OF_FILE != rc) 237cdf0e10cSrcweir { 238cdf0e10cSrcweir unzGetCurrentFileInfo(m_uzFile, &finfo, 0, 0, 0, 0, 0, 0); 239cdf0e10cSrcweir lmax = std::max<long>(lmax, finfo.size_filename); 240cdf0e10cSrcweir rc = unzGoToNextFile(m_uzFile); 241cdf0e10cSrcweir } 242cdf0e10cSrcweir 243cdf0e10cSrcweir if (UNZ_OK != rc && UNZ_END_OF_LIST_OF_FILE != rc) 244cdf0e10cSrcweir throw ZipException(rc); 245cdf0e10cSrcweir 246cdf0e10cSrcweir return lmax; 247cdf0e10cSrcweir } 248cdf0e10cSrcweir 249