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