1999-03-09 05:13:56 +03:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-09-20 04:02:59 +04:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Netscape Public
|
1999-11-06 06:43:54 +03:00
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
2001-09-20 04:02:59 +04:00
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-03-09 05:13:56 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-03-09 05:13:56 +03:00
|
|
|
*
|
2001-09-20 04:02:59 +04:00
|
|
|
* The Original Code is Mozilla Communicator client code,
|
|
|
|
* released March 31, 1998.
|
1999-03-09 05:13:56 +03:00
|
|
|
*
|
2001-09-20 04:02:59 +04:00
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
1999-11-06 06:43:54 +03:00
|
|
|
* Rights Reserved.
|
1999-03-09 05:13:56 +03:00
|
|
|
*
|
2001-09-20 04:02:59 +04:00
|
|
|
* Contributor(s):
|
1999-03-09 05:13:56 +03:00
|
|
|
* Daniel Veditz <dveditz@netscape.com>
|
1999-06-23 10:16:28 +04:00
|
|
|
* Samir Gehani <sgehani@netscape.com>
|
1999-10-26 23:43:26 +04:00
|
|
|
* Mitch Stoltz <mstoltz@netscape.com>
|
2002-05-03 18:50:46 +04:00
|
|
|
* Jeroen Dobbelaere <jeroen.dobbelaere@acunia.com>
|
2001-09-20 04:02:59 +04:00
|
|
|
*/
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This module implements a simple archive extractor for the PKZIP format.
|
1999-05-19 07:21:03 +04:00
|
|
|
*
|
|
|
|
* The underlying nsZipArchive is NOT thread-safe. Do not pass references
|
|
|
|
* or pointers to it across thread boundaries.
|
1999-03-09 05:13:56 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
#ifndef STANDALONE
|
1999-09-01 05:28:22 +04:00
|
|
|
|
2001-03-21 07:08:12 +03:00
|
|
|
#include "nsWildCard.h"
|
1999-06-03 22:15:53 +04:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "plstr.h"
|
2001-03-21 07:08:12 +03:00
|
|
|
#include "prlog.h"
|
1999-06-03 22:15:53 +04:00
|
|
|
#define ZFILE_CREATE PR_WRONLY | PR_CREATE_FILE
|
|
|
|
#define READTYPE PRInt32
|
1999-09-08 02:03:53 +04:00
|
|
|
#include "zlib.h"
|
1999-11-16 04:52:29 +03:00
|
|
|
#include "nsISupportsUtils.h"
|
2002-01-24 10:00:07 +03:00
|
|
|
#include "nsRecyclingAllocator.h"
|
2002-01-04 08:46:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Globals
|
|
|
|
*
|
|
|
|
* Global allocator used with zlib. Destroyed in module shutdown.
|
|
|
|
*/
|
2002-01-24 10:00:07 +03:00
|
|
|
#define NBUCKETS 6
|
|
|
|
#define BY4ALLOC_ITEMS 320
|
|
|
|
nsRecyclingAllocator *gZlibAllocator = NULL;
|
1999-09-01 05:28:22 +04:00
|
|
|
|
2002-03-11 23:18:35 +03:00
|
|
|
// For placement new used for arena allocations of zip file list
|
|
|
|
#include <new.h>
|
|
|
|
|
2001-03-21 07:08:12 +03:00
|
|
|
#else /* STANDALONE */
|
1999-09-01 05:28:22 +04:00
|
|
|
|
2001-01-22 23:15:49 +03:00
|
|
|
#ifdef XP_WIN
|
1999-08-06 11:58:15 +04:00
|
|
|
#include "windows.h"
|
1999-08-10 03:01:03 +04:00
|
|
|
#endif
|
1999-09-08 02:03:53 +04:00
|
|
|
|
1999-08-26 05:23:10 +04:00
|
|
|
#undef MOZILLA_CLIENT // undoes prtypes damage in zlib.h
|
1999-06-03 22:15:53 +04:00
|
|
|
#define ZFILE_CREATE "wb"
|
|
|
|
#define READTYPE PRUint32
|
1999-09-08 02:03:53 +04:00
|
|
|
#include "zlib.h"
|
|
|
|
#undef PR_PUBLIC_API
|
|
|
|
#include "zipstub.h"
|
1999-09-01 05:28:22 +04:00
|
|
|
|
2000-01-29 07:16:36 +03:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
char * strdup(const char *src);
|
|
|
|
char * strdup(const char *src)
|
|
|
|
{
|
|
|
|
long len = strlen(src);
|
|
|
|
char *dup = (char *)malloc(len+1 * sizeof(char));
|
|
|
|
memcpy(dup, src, len+1);
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
#endif /* STANDALONE */
|
1999-04-28 04:34:30 +04:00
|
|
|
|
1999-11-03 02:46:09 +03:00
|
|
|
#ifdef XP_UNIX
|
1999-11-02 23:37:28 +03:00
|
|
|
#include <sys/stat.h>
|
2001-09-21 03:33:23 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
1999-11-03 01:36:59 +03:00
|
|
|
#elif defined(XP_PC)
|
1999-11-02 23:37:28 +03:00
|
|
|
#include <io.h>
|
|
|
|
#endif
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2001-09-21 03:33:23 +04:00
|
|
|
#ifndef XP_UNIX /* we need to have some constant defined in limits.h and unistd.h */
|
|
|
|
# ifndef S_IFMT
|
|
|
|
# define S_IFMT 0170000
|
|
|
|
# endif
|
|
|
|
# ifndef S_IFLNK
|
|
|
|
# define S_IFLNK 0120000
|
|
|
|
# endif
|
|
|
|
# ifndef PATH_MAX
|
|
|
|
# define PATH_MAX 1024
|
|
|
|
# endif
|
|
|
|
#endif /* XP_UNIX */
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
#include "zipfile.h"
|
|
|
|
#include "zipstruct.h"
|
|
|
|
#include "nsZipArchive.h"
|
|
|
|
|
|
|
|
static PRUint16 xtoint(unsigned char *ii);
|
|
|
|
static PRUint32 xtolong(unsigned char *ll);
|
1999-11-02 23:37:28 +03:00
|
|
|
static PRUint16 ExtractMode(PRUint32 ext_attr);
|
2001-09-21 03:33:23 +04:00
|
|
|
static PRBool IsSymlink(PRUint32 ext_attr);
|
2000-03-28 07:38:06 +04:00
|
|
|
static void dosdate(char *aOutDateStr, PRUint16 aDate);
|
|
|
|
static void dostime(char *aOutTimeStr, PRUint16 aTime);
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
/*---------------------------------------------
|
|
|
|
* C API wrapper for nsZipArchive
|
|
|
|
*--------------------------------------------*/
|
|
|
|
|
1999-06-23 10:16:28 +04:00
|
|
|
#ifdef STANDALONE
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
/**
|
|
|
|
* ZIP_OpenArchive
|
|
|
|
*
|
|
|
|
* opens the named zip/jar archive and returns a handle that
|
|
|
|
* represents the archive in other ZIP_ calls.
|
|
|
|
*
|
|
|
|
* @param zipname archive filename
|
|
|
|
* @param hZip receives handle if archive opened OK
|
|
|
|
* @return status code
|
|
|
|
*/
|
|
|
|
PR_PUBLIC_API(PRInt32) ZIP_OpenArchive( const char * zipname, void** hZip )
|
|
|
|
{
|
|
|
|
PRInt32 status;
|
|
|
|
|
|
|
|
/*--- error check args ---*/
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( hZip == 0 )
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/*--- NULL output to prevent use by bozos who don't check errors ---*/
|
1999-05-19 07:21:03 +04:00
|
|
|
*hZip = 0;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
/*--- create and open the archive ---*/
|
|
|
|
nsZipArchive* zip = new nsZipArchive();
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( zip == 0 )
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_MEMORY;
|
|
|
|
|
|
|
|
status = zip->OpenArchive(zipname);
|
|
|
|
|
|
|
|
if ( status == ZIP_OK )
|
|
|
|
*hZip = NS_STATIC_CAST(void*,zip);
|
1999-04-28 04:34:30 +04:00
|
|
|
else
|
|
|
|
delete zip;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
/**
|
|
|
|
* ZIP_TestArchive
|
|
|
|
*
|
|
|
|
* Tests the integrity of this open zip archive by extracting each
|
|
|
|
* item to memory and performing a CRC check.
|
|
|
|
*
|
|
|
|
* @param hZip handle obtained from ZIP_OpenArchive
|
|
|
|
* @return status code (success indicated by ZIP_OK)
|
|
|
|
*/
|
|
|
|
PR_PUBLIC_API(PRInt32) ZIP_TestArchive( void *hZip )
|
|
|
|
{
|
|
|
|
/*--- error check args ---*/
|
|
|
|
if ( hZip == 0 )
|
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
|
|
|
nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,hZip);
|
|
|
|
if ( zip->kMagic != ZIP_MAGIC )
|
|
|
|
return ZIP_ERR_PARAM; /* whatever it is isn't one of ours! */
|
|
|
|
|
|
|
|
/*--- test the archive ---*/
|
|
|
|
return zip->Test(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
/**
|
|
|
|
* ZIP_CloseArchive
|
|
|
|
*
|
|
|
|
* closes zip archive and frees memory
|
|
|
|
* @param hZip handle obtained from ZIP_OpenArchive
|
|
|
|
* @return status code
|
|
|
|
*/
|
|
|
|
PR_PUBLIC_API(PRInt32) ZIP_CloseArchive( void** hZip )
|
|
|
|
{
|
|
|
|
/*--- error check args ---*/
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( hZip == 0 || *hZip == 0 )
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
|
|
|
nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,*hZip);
|
|
|
|
if ( zip->kMagic != ZIP_MAGIC )
|
|
|
|
return ZIP_ERR_PARAM; /* whatever it is isn't one of ours! */
|
|
|
|
|
|
|
|
/*--- close the archive ---*/
|
1999-05-19 07:21:03 +04:00
|
|
|
*hZip = 0;
|
1999-03-09 05:13:56 +03:00
|
|
|
delete zip;
|
|
|
|
|
|
|
|
return ZIP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ZIP_ExtractFile
|
|
|
|
*
|
|
|
|
* extracts named file from an opened archive
|
|
|
|
*
|
|
|
|
* @param hZip handle obtained from ZIP_OpenArchive
|
|
|
|
* @param filename name of file in archive
|
|
|
|
* @param outname filename to extract to
|
|
|
|
*/
|
|
|
|
PR_PUBLIC_API(PRInt32) ZIP_ExtractFile( void* hZip, const char * filename, const char * outname )
|
|
|
|
{
|
|
|
|
/*--- error check args ---*/
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( hZip == 0 )
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
|
|
|
nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,hZip);
|
|
|
|
if ( zip->kMagic != ZIP_MAGIC )
|
|
|
|
return ZIP_ERR_PARAM; /* whatever it is isn't one of ours! */
|
|
|
|
|
|
|
|
/*--- extract the file ---*/
|
|
|
|
return zip->ExtractFile( filename, outname );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/**
|
|
|
|
* ZIP_FindInit
|
|
|
|
*
|
|
|
|
* Initializes an enumeration of files in the archive
|
|
|
|
*
|
|
|
|
* @param hZip handle obtained from ZIP_OpenArchive
|
|
|
|
* @param pattern regexp to match files in archive, the usual shell expressions.
|
|
|
|
* NULL pattern also matches all files, faster than "*"
|
|
|
|
*/
|
1999-05-19 07:21:03 +04:00
|
|
|
PR_PUBLIC_API(void*) ZIP_FindInit( void* hZip, const char * pattern )
|
1999-04-28 04:34:30 +04:00
|
|
|
{
|
|
|
|
/*--- error check args ---*/
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( hZip == 0 )
|
|
|
|
return 0;
|
1999-04-28 04:34:30 +04:00
|
|
|
|
|
|
|
nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,hZip);
|
|
|
|
if ( zip->kMagic != ZIP_MAGIC )
|
1999-05-19 07:21:03 +04:00
|
|
|
return 0; /* whatever it is isn't one of ours! */
|
1999-04-28 04:34:30 +04:00
|
|
|
|
|
|
|
/*--- initialize the pattern search ---*/
|
|
|
|
return zip->FindInit( pattern );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
1999-06-23 10:16:28 +04:00
|
|
|
* ZIP_FindNext
|
1999-04-28 04:34:30 +04:00
|
|
|
*
|
|
|
|
* Puts the next name in the passed buffer. Returns ZIP_ERR_SMALLBUF when
|
|
|
|
* the name is too large for the buffer, and ZIP_ERR_FNF when there are no
|
|
|
|
* more files that match the pattern
|
|
|
|
*
|
1999-05-19 07:21:03 +04:00
|
|
|
* @param hFind handle obtained from ZIP_FindInit
|
1999-04-28 04:34:30 +04:00
|
|
|
* @param outbuf buffer to receive next filename
|
|
|
|
* @param bufsize size of allocated buffer
|
|
|
|
*/
|
1999-05-19 07:21:03 +04:00
|
|
|
PR_PUBLIC_API(PRInt32) ZIP_FindNext( void* hFind, char * outbuf, PRUint16 bufsize )
|
1999-04-28 04:34:30 +04:00
|
|
|
{
|
1999-06-23 10:16:28 +04:00
|
|
|
PRInt32 status;
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/*--- error check args ---*/
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( hFind == 0 )
|
1999-04-28 04:34:30 +04:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
nsZipFind* find = NS_STATIC_CAST(nsZipFind*,hFind);
|
|
|
|
if ( find->kMagic != ZIPFIND_MAGIC )
|
1999-04-28 04:34:30 +04:00
|
|
|
return ZIP_ERR_PARAM; /* whatever it is isn't one of ours! */
|
|
|
|
|
|
|
|
/*--- return next filename file ---*/
|
1999-06-23 10:16:28 +04:00
|
|
|
nsZipItem* item;
|
2000-08-19 02:09:26 +04:00
|
|
|
status = find->GetArchive()->FindNext( find, &item );
|
1999-06-23 10:16:28 +04:00
|
|
|
if ( status == ZIP_OK )
|
|
|
|
{
|
2001-12-07 04:16:20 +03:00
|
|
|
PRUint16 namelen = (PRUint16)PL_strlen(item->name);
|
|
|
|
|
|
|
|
if ( bufsize > namelen )
|
1999-06-23 10:16:28 +04:00
|
|
|
{
|
|
|
|
PL_strcpy( outbuf, item->name );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = ZIP_ERR_SMALLBUF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
1999-04-28 04:34:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
/**
|
|
|
|
* ZIP_FindFree
|
|
|
|
*
|
|
|
|
* Releases allocated memory associated with the find token
|
|
|
|
*
|
|
|
|
* @param hFind handle obtained from ZIP_FindInit
|
|
|
|
*/
|
|
|
|
PR_PUBLIC_API(PRInt32) ZIP_FindFree( void* hFind )
|
|
|
|
{
|
|
|
|
/*--- error check args ---*/
|
|
|
|
if ( hFind == 0 )
|
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
|
|
|
nsZipFind* find = NS_STATIC_CAST(nsZipFind*,hFind);
|
|
|
|
if ( find->kMagic != ZIPFIND_MAGIC )
|
|
|
|
return ZIP_ERR_PARAM; /* whatever it is isn't one of ours! */
|
|
|
|
|
|
|
|
/* free the find structure */
|
2000-08-19 02:09:26 +04:00
|
|
|
return find->GetArchive()->FindFree( find );
|
1999-05-19 07:21:03 +04:00
|
|
|
}
|
|
|
|
|
2001-01-22 23:15:49 +03:00
|
|
|
#if defined XP_WIN
|
|
|
|
void ProcessWindowsMessages()
|
|
|
|
{
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
2001-01-22 23:53:31 +03:00
|
|
|
#endif /* XP_WIN */
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2002-01-04 08:46:48 +03:00
|
|
|
#else /* STANDALONE */
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2001-12-15 09:31:49 +03:00
|
|
|
//***********************************************************
|
|
|
|
// Allocators for use with zlib
|
|
|
|
//
|
|
|
|
// These are allocators that are performance tuned for
|
|
|
|
// use with zlib. Our use of zlib for every file we read from
|
|
|
|
// the jar file when running navigator, we do these allocation.
|
|
|
|
// alloc 24
|
|
|
|
// alloc 64
|
|
|
|
// alloc 11520
|
|
|
|
// alloc 32768
|
|
|
|
// alloc 1216 [304x4] max
|
|
|
|
// alloc 76 [19x4]
|
|
|
|
// free 76 [19x4]
|
|
|
|
// alloc 1152 [288x4]
|
|
|
|
// free 1152 [288x4]
|
|
|
|
// free 1216 [304x4]
|
|
|
|
// alloc 28
|
|
|
|
// free 28
|
|
|
|
// free 32768
|
|
|
|
// free 11520
|
|
|
|
// free 64
|
|
|
|
// free 24
|
|
|
|
//
|
|
|
|
// The pool will allocate these as:
|
|
|
|
//
|
|
|
|
// 32,768
|
|
|
|
// 11,520
|
|
|
|
// 1,280 [320x4] - shared by first x4 alloc, 28
|
|
|
|
// 1,280 [320x4] - shared by second and third x4 alloc
|
|
|
|
// 64
|
|
|
|
// 24
|
|
|
|
// ------
|
|
|
|
// 46,936
|
|
|
|
//
|
|
|
|
// And almost all of the file reads happen serially. Hence this
|
|
|
|
// allocator tries to keep one set of memory needed for one file around
|
|
|
|
// and reused the same blocks for other file reads.
|
|
|
|
//
|
|
|
|
// The interesting question is when should be free this ?
|
|
|
|
// - memory pressure should be one.
|
|
|
|
// - after startup of navigator
|
|
|
|
// - after startup of mail
|
|
|
|
// In general, this allocator should be enabled before
|
|
|
|
// we startup and disabled after we startup if memory is a concern.
|
|
|
|
//***********************************************************
|
|
|
|
|
2001-12-15 16:15:24 +03:00
|
|
|
PR_STATIC_CALLBACK(void *)
|
|
|
|
zlibAlloc(void *opaque, uInt items, uInt size)
|
2001-12-15 09:31:49 +03:00
|
|
|
{
|
2002-01-24 10:00:07 +03:00
|
|
|
nsRecyclingAllocator *zallocator = (nsRecyclingAllocator *)opaque;
|
|
|
|
if (zallocator) {
|
|
|
|
// Bump up x4 allocations
|
|
|
|
PRUint32 realitems = items;
|
|
|
|
if (size == 4 && items < BY4ALLOC_ITEMS)
|
|
|
|
realitems = BY4ALLOC_ITEMS;
|
|
|
|
return zallocator->Calloc(realitems, size);
|
|
|
|
}
|
2001-12-15 09:31:49 +03:00
|
|
|
else
|
|
|
|
return calloc(items, size);
|
|
|
|
}
|
|
|
|
|
2001-12-15 16:15:24 +03:00
|
|
|
PR_STATIC_CALLBACK(void)
|
|
|
|
zlibFree(void *opaque, void *ptr)
|
2001-12-15 09:31:49 +03:00
|
|
|
{
|
2002-01-24 10:00:07 +03:00
|
|
|
nsRecyclingAllocator *zallocator = (nsRecyclingAllocator *)opaque;
|
2001-12-15 09:31:49 +03:00
|
|
|
if (zallocator)
|
2002-01-24 10:00:07 +03:00
|
|
|
zallocator->Free(ptr);
|
2001-12-15 09:31:49 +03:00
|
|
|
else
|
|
|
|
free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2002-01-04 08:46:48 +03:00
|
|
|
#endif /* STANDALONE */
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
//***********************************************************
|
|
|
|
// nsZipArchive -- public methods
|
|
|
|
//***********************************************************
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::OpenArchive
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::OpenArchive( const char * aArchiveName )
|
|
|
|
{
|
|
|
|
//-- validate arguments
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( aArchiveName == 0 || *aArchiveName == '\0')
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
|
|
|
//-- not allowed to do two opens on the same object!
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( mFd != 0 )
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_GENERAL;
|
|
|
|
|
|
|
|
//-- open the physical file
|
|
|
|
mFd = PR_Open( aArchiveName, PR_RDONLY, 0 );
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( mFd == 0 )
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_DISK;
|
|
|
|
|
|
|
|
//-- get table of contents for archive
|
|
|
|
return BuildFileList();
|
|
|
|
}
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
PRInt32 nsZipArchive::OpenArchiveWithFileDesc(PRFileDesc* fd)
|
|
|
|
{
|
|
|
|
//-- validate arguments
|
|
|
|
if (fd == 0)
|
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
|
|
|
//-- not allowed to do two opens on the same object!
|
2000-01-29 03:03:57 +03:00
|
|
|
if ( mFd != 0 )
|
2000-01-25 00:28:28 +03:00
|
|
|
return ZIP_ERR_GENERAL;
|
|
|
|
|
|
|
|
mFd = fd;
|
|
|
|
|
|
|
|
//-- get table of contents for archive
|
|
|
|
return BuildFileList();
|
|
|
|
}
|
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::Test
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::Test(const char *aEntryName)
|
|
|
|
{
|
|
|
|
PRInt32 rv = ZIP_OK;
|
|
|
|
nsZipItem *currItem = 0;
|
|
|
|
|
|
|
|
if (aEntryName) // only test specified item
|
|
|
|
{
|
|
|
|
currItem = GetFileItem(aEntryName);
|
|
|
|
if(!currItem)
|
|
|
|
return ZIP_ERR_FNF;
|
|
|
|
|
|
|
|
rv = TestItem(currItem);
|
|
|
|
}
|
|
|
|
else // test all items in archive
|
|
|
|
{
|
|
|
|
nsZipFind *iterator = FindInit( NULL );
|
|
|
|
if (!iterator)
|
|
|
|
return ZIP_ERR_GENERAL;
|
|
|
|
|
|
|
|
// iterate over items in list
|
|
|
|
while (ZIP_OK == FindNext(iterator, &currItem))
|
|
|
|
{
|
|
|
|
rv = TestItem(currItem);
|
|
|
|
|
|
|
|
// check if crc check failed
|
|
|
|
if (rv != ZIP_OK)
|
|
|
|
break;
|
2001-01-22 23:15:49 +03:00
|
|
|
|
|
|
|
#if defined STANDALONE && defined XP_WIN
|
|
|
|
ProcessWindowsMessages();
|
|
|
|
#endif
|
2000-12-27 10:05:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FindFree(iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-11-12 09:13:13 +03:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::CloseArchive
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::CloseArchive()
|
|
|
|
{
|
|
|
|
// close the file if open
|
|
|
|
if ( mFd != 0 ) {
|
|
|
|
PR_Close(mFd);
|
2000-06-07 13:05:22 +04:00
|
|
|
mFd = 0;
|
1999-11-12 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
2002-03-11 23:18:35 +03:00
|
|
|
#ifndef STANDALONE
|
|
|
|
// delete arena for filelist
|
|
|
|
PL_FinishArenaPool(&mArena);
|
|
|
|
|
|
|
|
// CAUTION:
|
|
|
|
// We dont need to delete each of the nsZipItem as the memory for
|
|
|
|
// the zip item and the filename it holds are both allocated from the Arena.
|
|
|
|
// Hence, destroying the Arena is like destroying all the memory
|
|
|
|
// for all the nsZipItem in one shot. But if the ~nsZipItem is doing
|
|
|
|
// anything more than cleaning up memory, we should start calling it.
|
|
|
|
#else
|
1999-11-12 09:13:13 +03:00
|
|
|
// delete nsZipItems in table
|
|
|
|
nsZipItem* pItem;
|
|
|
|
for ( int i = 0; i < ZIP_TABSIZE; ++i)
|
|
|
|
{
|
|
|
|
pItem = mFiles[i];
|
|
|
|
while ( pItem != 0 )
|
|
|
|
{
|
|
|
|
mFiles[i] = pItem->next;
|
|
|
|
delete pItem;
|
|
|
|
pItem = mFiles[i];
|
|
|
|
}
|
|
|
|
}
|
2002-03-11 23:18:35 +03:00
|
|
|
#endif
|
|
|
|
|
1999-11-12 09:13:13 +03:00
|
|
|
return ZIP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
2000-01-29 03:03:57 +03:00
|
|
|
// nsZipArchive::GetItem
|
1999-11-12 09:13:13 +03:00
|
|
|
//---------------------------------------------
|
2000-01-29 03:03:57 +03:00
|
|
|
PRInt32 nsZipArchive::GetItem( const char * aFilename, nsZipItem **result)
|
1999-11-12 09:13:13 +03:00
|
|
|
{
|
|
|
|
//-- Parameter validity check
|
|
|
|
if (aFilename == 0)
|
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
2000-01-29 03:03:57 +03:00
|
|
|
nsZipItem* item;
|
1999-11-12 09:13:13 +03:00
|
|
|
|
|
|
|
//-- find file information
|
2000-01-29 03:03:57 +03:00
|
|
|
item = GetFileItem( aFilename );
|
|
|
|
if ( item == 0 )
|
1999-11-12 09:13:13 +03:00
|
|
|
{
|
|
|
|
return ZIP_ERR_FNF;
|
|
|
|
}
|
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
*result = item; // Return a pointer to the struct
|
2000-01-29 03:03:57 +03:00
|
|
|
return ZIP_OK;
|
1999-11-12 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
2000-01-29 03:03:57 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::ReadInit
|
|
|
|
//---------------------------------------------
|
2001-12-21 08:57:39 +03:00
|
|
|
PRInt32 nsZipArchive::ReadInit(const char* zipEntry, nsZipRead* aRead)
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
|
|
|
//-- Parameter validity check
|
2000-01-25 00:28:28 +03:00
|
|
|
if (zipEntry == 0 || aRead == 0)
|
1999-10-26 23:43:26 +04:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
1999-11-03 02:46:09 +03:00
|
|
|
PRInt32 result;
|
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- find item
|
|
|
|
nsZipItem* item = GetFileItem(zipEntry);
|
|
|
|
if (!item)
|
|
|
|
return ZIP_ERR_FNF;
|
1999-11-03 02:46:09 +03:00
|
|
|
|
2001-12-21 08:57:39 +03:00
|
|
|
//-- Initialize nsZipRead object
|
|
|
|
aRead->Init(this, item);
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
//-- Read the item into memory
|
|
|
|
// Inflate if necessary and save in mInflatedFileBuffer
|
|
|
|
// for sequential reading.
|
|
|
|
// (nsJAR needs the whole file in memory before passing it on)
|
|
|
|
char* buf = (char*)PR_Malloc(item->realsize);
|
|
|
|
if (!buf) return ZIP_ERR_MEMORY;
|
|
|
|
switch(item->compression)
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
2000-02-14 04:57:01 +03:00
|
|
|
case DEFLATED:
|
|
|
|
result = InflateItem(item, 0, buf);
|
|
|
|
break;
|
|
|
|
case STORED:
|
|
|
|
result = CopyItemToBuffer(item, buf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ZIP_ERR_UNSUPPORTED;
|
1999-10-26 23:43:26 +04:00
|
|
|
}
|
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
if (result == ZIP_OK)
|
2001-12-21 08:57:39 +03:00
|
|
|
aRead->mFileBuffer = buf;
|
2000-02-14 04:57:01 +03:00
|
|
|
return result;
|
1999-10-26 23:43:26 +04:00
|
|
|
}
|
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
//------------------------------------------
|
1999-10-26 23:43:26 +04:00
|
|
|
// nsZipArchive::Read
|
2000-02-14 04:57:01 +03:00
|
|
|
//------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::Read(nsZipRead* aRead, char* aOutBuf,
|
1999-10-26 23:43:26 +04:00
|
|
|
PRUint32 aCount, PRUint32* aBytesRead)
|
|
|
|
{
|
2000-02-14 04:57:01 +03:00
|
|
|
//-- Copies data from aRead->mFileBuffer
|
|
|
|
//-- Parameter check
|
|
|
|
if (aBytesRead == 0 || aRead == 0 || aOutBuf == 0 ||
|
1999-11-03 02:46:09 +03:00
|
|
|
aRead->mArchive != this)
|
1999-10-26 23:43:26 +04:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
if(!aRead->mItem || !aRead->mFileBuffer)
|
|
|
|
return ZIP_ERR_GENERAL;
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
//-- Set up the copy
|
|
|
|
PRUint32 bigBufSize = aRead->mItem->realsize;
|
|
|
|
PRUint32 curPos = aRead->mCurPos;
|
|
|
|
*aBytesRead = (curPos + aCount) < bigBufSize ? aCount : bigBufSize - curPos;
|
|
|
|
char* src = aRead->mFileBuffer + curPos;
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
//-- Do the copy and record number of bytes copied
|
|
|
|
memcpy(aOutBuf, src, *aBytesRead);
|
|
|
|
aRead->mCurPos += *aBytesRead;
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
return ZIP_OK;
|
|
|
|
}
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::Available
|
|
|
|
//---------------------------------------------
|
1999-11-03 02:46:09 +03:00
|
|
|
PRUint32 nsZipArchive::Available(nsZipRead* aRead)
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
1999-11-03 02:46:09 +03:00
|
|
|
if (aRead == 0)
|
1999-10-26 23:43:26 +04:00
|
|
|
return 0;
|
1999-11-03 02:46:09 +03:00
|
|
|
|
|
|
|
nsZipItem* item = aRead->mItem;
|
|
|
|
|
|
|
|
if (item->compression == DEFLATED)
|
|
|
|
return item->realsize - aRead->mCurPos;
|
1999-10-26 23:43:26 +04:00
|
|
|
else
|
1999-11-03 02:46:09 +03:00
|
|
|
return item->size - aRead->mCurPos;
|
1999-10-26 23:43:26 +04:00
|
|
|
}
|
1999-04-28 04:34:30 +04:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::ExtractFile
|
|
|
|
//---------------------------------------------
|
2000-01-25 00:28:28 +03:00
|
|
|
PRInt32 nsZipArchive::ExtractFile(const char* zipEntry, const char* aOutname)
|
|
|
|
{
|
|
|
|
PRFileDesc* fOut = PR_Open(aOutname, ZFILE_CREATE, 0644);
|
|
|
|
if (fOut == 0)
|
|
|
|
return ZIP_ERR_DISK;
|
|
|
|
|
2000-03-28 07:38:06 +04:00
|
|
|
nsZipItem *item = 0;
|
|
|
|
PRInt32 status = ExtractFileToFileDesc(zipEntry, fOut, &item);
|
2000-01-25 00:28:28 +03:00
|
|
|
PR_Close(fOut);
|
|
|
|
|
2001-09-21 03:33:23 +04:00
|
|
|
if (status != ZIP_OK)
|
|
|
|
{
|
|
|
|
PR_Delete(aOutname);
|
2000-01-25 00:28:28 +03:00
|
|
|
}
|
2000-03-28 07:38:06 +04:00
|
|
|
#if defined(XP_UNIX)
|
2001-09-21 03:33:23 +04:00
|
|
|
else
|
|
|
|
{
|
2001-12-07 04:45:19 +03:00
|
|
|
if (ZIFLAG_SYMLINK & item->flags)
|
2001-09-21 03:33:23 +04:00
|
|
|
{
|
|
|
|
status = ResolveSymlink(aOutname,item);
|
|
|
|
}
|
|
|
|
//-- set extracted file permissions
|
|
|
|
chmod(aOutname, item->mode);
|
2000-01-25 00:28:28 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsZipArchive::ExtractFileToFileDesc(const char * zipEntry, PRFileDesc* outFD,
|
2000-03-28 07:38:06 +04:00
|
|
|
nsZipItem **outItem)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
//-- sanity check arguments
|
2000-03-28 07:38:06 +04:00
|
|
|
if ( zipEntry == 0 || outFD == 0 || outItem == 0)
|
1999-03-09 05:13:56 +03:00
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
PRInt32 status;
|
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- Find item in archive
|
|
|
|
nsZipItem* item = GetFileItem( zipEntry );
|
|
|
|
if (!item)
|
|
|
|
return ZIP_ERR_FNF;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
//-- extract the file using the appropriate method
|
1999-03-09 05:13:56 +03:00
|
|
|
switch( item->compression )
|
|
|
|
{
|
|
|
|
case STORED:
|
2000-01-25 00:28:28 +03:00
|
|
|
status = CopyItemToDisk( item, outFD );
|
1999-11-02 23:37:28 +03:00
|
|
|
break;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
case DEFLATED:
|
2000-01-25 00:28:28 +03:00
|
|
|
status = InflateItem( item, outFD, 0 );
|
1999-11-02 23:37:28 +03:00
|
|
|
break;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
//-- unsupported compression type
|
|
|
|
return ZIP_ERR_UNSUPPORTED;
|
|
|
|
}
|
2000-03-28 07:38:06 +04:00
|
|
|
|
|
|
|
*outItem = item;
|
1999-11-02 23:37:28 +03:00
|
|
|
return status;
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::FindInit
|
|
|
|
//---------------------------------------------
|
1999-05-19 07:21:03 +04:00
|
|
|
nsZipFind* nsZipArchive::FindInit( const char * aPattern )
|
1999-04-28 04:34:30 +04:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
PRBool regExp = PR_FALSE;
|
1999-05-19 07:21:03 +04:00
|
|
|
char* pattern = 0;
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
// validate the pattern
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( aPattern )
|
|
|
|
{
|
2001-03-21 07:08:12 +03:00
|
|
|
switch (NS_WildCardValid( (char*)aPattern ))
|
1999-04-28 04:34:30 +04:00
|
|
|
{
|
|
|
|
case INVALID_SXP:
|
1999-05-19 07:21:03 +04:00
|
|
|
return 0;
|
1999-04-28 04:34:30 +04:00
|
|
|
|
|
|
|
case NON_SXP:
|
1999-05-19 07:21:03 +04:00
|
|
|
regExp = PR_FALSE;
|
1999-04-28 04:34:30 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VALID_SXP:
|
1999-05-19 07:21:03 +04:00
|
|
|
regExp = PR_TRUE;
|
1999-04-28 04:34:30 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-05-19 07:21:03 +04:00
|
|
|
// undocumented return value from RegExpValid!
|
1999-04-28 04:34:30 +04:00
|
|
|
PR_ASSERT( PR_FALSE );
|
1999-05-19 07:21:03 +04:00
|
|
|
return 0;
|
1999-04-28 04:34:30 +04:00
|
|
|
}
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
pattern = PL_strdup( aPattern );
|
|
|
|
if ( !pattern )
|
|
|
|
return 0;
|
1999-04-28 04:34:30 +04:00
|
|
|
}
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
return new nsZipFind( this, pattern, regExp );
|
1999-04-28 04:34:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::FindNext
|
|
|
|
//---------------------------------------------
|
1999-06-23 10:16:28 +04:00
|
|
|
PRInt32 nsZipArchive::FindNext( nsZipFind* aFind, nsZipItem** aResult)
|
1999-04-28 04:34:30 +04:00
|
|
|
{
|
|
|
|
PRInt32 status;
|
|
|
|
PRBool found = PR_FALSE;
|
1999-05-19 07:21:03 +04:00
|
|
|
PRUint16 slot = aFind->mSlot;
|
|
|
|
nsZipItem* item = aFind->mItem;
|
|
|
|
|
1999-06-23 10:16:28 +04:00
|
|
|
if ( aFind->mArchive != this )
|
1999-05-19 07:21:03 +04:00
|
|
|
return ZIP_ERR_PARAM;
|
1999-04-28 04:34:30 +04:00
|
|
|
|
|
|
|
// we start from last match, look for next
|
|
|
|
while ( slot < ZIP_TABSIZE && !found )
|
|
|
|
{
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( item != 0 )
|
1999-04-28 04:34:30 +04:00
|
|
|
item = item->next; // move to next in current chain
|
|
|
|
else
|
|
|
|
item = mFiles[slot]; // starting a new slot
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( item == 0 )
|
1999-04-28 04:34:30 +04:00
|
|
|
{ // no more in this chain, move to next slot
|
|
|
|
++slot;
|
|
|
|
continue;
|
|
|
|
}
|
1999-05-19 07:21:03 +04:00
|
|
|
else if ( aFind->mPattern == 0 )
|
1999-04-28 04:34:30 +04:00
|
|
|
found = PR_TRUE; // always match
|
1999-05-19 07:21:03 +04:00
|
|
|
else if ( aFind->mRegExp )
|
2001-03-21 07:08:12 +03:00
|
|
|
found = (NS_WildCardMatch( item->name, aFind->mPattern, PR_FALSE ) == MATCH);
|
1999-04-28 04:34:30 +04:00
|
|
|
else
|
1999-10-26 06:08:56 +04:00
|
|
|
#if defined(STANDALONE) && defined(XP_MAC)
|
|
|
|
// simulate <regexp>* matches
|
|
|
|
found = ( strncmp( item->name, aFind->mPattern, strlen(aFind->mPattern) ) == 0 );
|
|
|
|
#else
|
1999-05-19 07:21:03 +04:00
|
|
|
found = ( PL_strcmp( item->name, aFind->mPattern ) == 0 );
|
1999-10-26 06:08:56 +04:00
|
|
|
#endif
|
1999-04-28 04:34:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( found )
|
|
|
|
{
|
1999-06-23 10:16:28 +04:00
|
|
|
*aResult = item;
|
|
|
|
aFind->mSlot = slot;
|
|
|
|
aFind->mItem = item;
|
1999-04-28 04:34:30 +04:00
|
|
|
status = ZIP_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = ZIP_ERR_FNF;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::FindFree
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::FindFree( nsZipFind* aFind )
|
|
|
|
{
|
|
|
|
if ( aFind->mArchive != this )
|
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
|
|
|
|
delete aFind;
|
|
|
|
return ZIP_OK;
|
|
|
|
}
|
|
|
|
|
2001-09-21 03:33:23 +04:00
|
|
|
#ifdef XP_UNIX
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::ResolveSymlink
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::ResolveSymlink(const char *path, nsZipItem *item)
|
|
|
|
{
|
|
|
|
PRInt32 status = ZIP_OK;
|
2001-12-07 04:16:20 +03:00
|
|
|
if (item->flags & ZIFLAG_SYMLINK)
|
2001-09-21 03:33:23 +04:00
|
|
|
{
|
|
|
|
char buf[PATH_MAX+1];
|
|
|
|
PRFileDesc * fIn = PR_Open(path, PR_RDONLY, 0644);
|
|
|
|
if (fIn)
|
|
|
|
{
|
|
|
|
PRInt32 length = PATH_MAX;
|
|
|
|
length = PR_Read(fIn,(void*)buf,length);
|
|
|
|
PR_Close(fIn);
|
|
|
|
fIn = 0;
|
|
|
|
if ( length <= 0
|
|
|
|
|| (buf[length] = 0, PR_Delete(path)) != 0
|
|
|
|
|| symlink(buf, path) != 0 )
|
|
|
|
{
|
|
|
|
status = ZIP_ERR_DISK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = ZIP_ERR_DISK;
|
|
|
|
}
|
|
|
|
if (fIn)
|
|
|
|
{
|
|
|
|
PR_Close(fIn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
#endif
|
1999-04-28 04:34:30 +04:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//***********************************************************
|
|
|
|
// nsZipArchive -- private implementation
|
|
|
|
//***********************************************************
|
|
|
|
|
2000-08-24 04:48:34 +04:00
|
|
|
#define BR_BUF_SIZE 1024 /* backward read buffer size */
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::BuildFileList
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::BuildFileList()
|
|
|
|
{
|
|
|
|
PRInt32 status = ZIP_OK;
|
2001-04-03 10:35:13 +04:00
|
|
|
PRUint8 buf[4*BR_BUF_SIZE];
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2000-08-24 04:48:34 +04:00
|
|
|
ZipEnd *End;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
//-----------------------------------------------------------------------
|
2001-04-03 10:35:13 +04:00
|
|
|
// locate the central directory via the End record
|
1999-04-28 04:34:30 +04:00
|
|
|
//-----------------------------------------------------------------------
|
1999-03-09 05:13:56 +03:00
|
|
|
PRInt32 pos = 0L;
|
2000-08-24 04:48:34 +04:00
|
|
|
PRInt32 bufsize = 0;
|
|
|
|
|
|
|
|
//-- get archive size using end pos
|
|
|
|
pos = PR_Seek(mFd, 0, PR_SEEK_END);
|
|
|
|
#ifndef STANDALONE
|
|
|
|
if (pos <= 0)
|
|
|
|
#else
|
|
|
|
if (pos || ((pos = ftell(mFd)) <= 0))
|
|
|
|
#endif
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
|
|
|
|
while (status == ZIP_OK)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
2000-08-24 04:48:34 +04:00
|
|
|
//-- read backwards in 1K-sized chunks (unless file is less than 1K)
|
|
|
|
pos > BR_BUF_SIZE ? bufsize = BR_BUF_SIZE : bufsize = pos;
|
|
|
|
pos -= bufsize;
|
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
if ( !ZIP_Seek( mFd, pos, PR_SEEK_SET ) )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-08-24 04:48:34 +04:00
|
|
|
if ( PR_Read( mFd, buf, bufsize ) != (READTYPE)bufsize )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- scan for ENDSIG
|
2000-08-24 04:48:34 +04:00
|
|
|
PRUint8 *endp = buf + bufsize;
|
|
|
|
PRUint32 endsig;
|
|
|
|
PRBool bEndsigFound = PR_FALSE;
|
1999-11-02 23:37:28 +03:00
|
|
|
|
2002-05-03 18:50:46 +04:00
|
|
|
for (endp -= ZIPEND_SIZE; endp >= buf; endp--)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
2000-08-24 04:48:34 +04:00
|
|
|
endsig = xtolong(endp);
|
|
|
|
if (endsig == ENDSIG)
|
|
|
|
{
|
|
|
|
bEndsigFound = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
2000-08-24 04:48:34 +04:00
|
|
|
|
|
|
|
if (bEndsigFound)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
2000-08-24 04:48:34 +04:00
|
|
|
End = (ZipEnd *) endp;
|
|
|
|
|
|
|
|
//-- set pos to start of central directory
|
|
|
|
pos = xtolong(End->offset_central_dir);
|
2001-04-03 10:35:13 +04:00
|
|
|
if ( !ZIP_Seek( mFd, pos, PR_SEEK_SET ) )
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
break;
|
|
|
|
}
|
2000-08-24 04:48:34 +04:00
|
|
|
|
2001-01-22 23:15:49 +03:00
|
|
|
if(pos <= 0)
|
|
|
|
//-- We're at the beginning of the file, and still no sign
|
|
|
|
//-- of the end signiture. File must be corrupted!
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
|
2000-08-24 04:48:34 +04:00
|
|
|
//-- backward read must overlap ZipEnd length
|
2002-05-03 18:50:46 +04:00
|
|
|
pos += ZIPEND_SIZE;
|
2000-08-24 04:48:34 +04:00
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
} /* while looking for end signature */
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-11-02 23:37:28 +03:00
|
|
|
//-------------------------------------------------------
|
|
|
|
// read the central directory headers
|
|
|
|
//-------------------------------------------------------
|
2001-04-03 10:35:13 +04:00
|
|
|
if ( status == ZIP_OK )
|
1999-11-02 23:37:28 +03:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- we think we found the central directory, read in the first chunk
|
|
|
|
pos = 0;
|
|
|
|
bufsize = PR_Read( mFd, &buf, sizeof(buf) );
|
2002-05-03 18:50:46 +04:00
|
|
|
if (bufsize < (PRInt32)(ZIPCENTRAL_SIZE + ZIPEND_SIZE))
|
1999-11-02 23:37:28 +03:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
// We know we read the end sig and got pointed at the central
|
|
|
|
// directory--there should be at least this much
|
|
|
|
//
|
|
|
|
// (technically there can be a completely empty archive with only a
|
|
|
|
// ZipEnd structure; that's pointless and might as well be an error.)
|
|
|
|
status = ZIP_ERR_DISK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- verify it's a central directory record
|
|
|
|
if ( xtolong( buf ) != CENTRALSIG )
|
1999-11-02 23:37:28 +03:00
|
|
|
status = ZIP_ERR_CORRUPT;
|
2001-04-03 10:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-- loop through directory records
|
|
|
|
//
|
|
|
|
//-- we enter the loop positioned at a central directory record
|
|
|
|
//-- with enough valid data in the buffer to contain one
|
|
|
|
while ( status == ZIP_OK )
|
|
|
|
{
|
|
|
|
//-------------------------------------------------------
|
|
|
|
// read the fixed-size data
|
|
|
|
//-------------------------------------------------------
|
|
|
|
ZipCentral* central = (ZipCentral*)(buf+pos);
|
|
|
|
|
|
|
|
PRUint32 namelen = xtoint( central->filename_len );
|
|
|
|
PRUint32 extralen = xtoint( central->extrafield_len );
|
|
|
|
PRUint32 commentlen = xtoint( central->commentfield_len );
|
2002-03-11 23:18:35 +03:00
|
|
|
#ifndef STANDALONE
|
|
|
|
// Arena allocate the nsZipItem
|
|
|
|
void *mem;
|
2002-03-12 00:09:12 +03:00
|
|
|
PL_ARENA_ALLOCATE(mem, &mArena, sizeof(nsZipItem));
|
2002-03-11 23:18:35 +03:00
|
|
|
// Use placement new to arena allcoate the nsZipItem
|
|
|
|
nsZipItem* item = mem ? new (mem) nsZipItem() : nsnull;
|
|
|
|
#else
|
2001-04-03 10:35:13 +04:00
|
|
|
nsZipItem* item = new nsZipItem();
|
2002-03-11 23:18:35 +03:00
|
|
|
#endif
|
2001-04-03 10:35:13 +04:00
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
status = ZIP_ERR_MEMORY;
|
1999-11-02 23:37:28 +03:00
|
|
|
break;
|
|
|
|
}
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2001-12-07 04:16:20 +03:00
|
|
|
item->offset = xtolong( central->localhdr_offset );
|
|
|
|
item->compression = (PRUint8)xtoint( central->method );
|
|
|
|
#if defined(DEBUG)
|
|
|
|
/*
|
|
|
|
* Make sure our space optimization is non lossy.
|
|
|
|
*/
|
|
|
|
PR_ASSERT(xtoint(central->method) == (PRUint16)item->compression);
|
|
|
|
#endif
|
2001-04-03 10:35:13 +04:00
|
|
|
item->size = xtolong( central->size );
|
|
|
|
item->realsize = xtolong( central->orglen );
|
|
|
|
item->crc32 = xtolong( central->crc32 );
|
2001-09-21 03:33:23 +04:00
|
|
|
PRUint32 external_attributes = xtolong( central->external_attributes );
|
|
|
|
item->mode = ExtractMode( external_attributes );
|
2001-12-07 04:16:20 +03:00
|
|
|
if ( IsSymlink( external_attributes ) )
|
|
|
|
{
|
|
|
|
item->flags |= ZIFLAG_SYMLINK;
|
|
|
|
}
|
2001-04-03 10:35:13 +04:00
|
|
|
item->time = xtoint( central->time );
|
|
|
|
item->date = xtoint( central->date );
|
|
|
|
|
2002-05-03 18:50:46 +04:00
|
|
|
pos += ZIPCENTRAL_SIZE;
|
2001-04-03 10:35:13 +04:00
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
// get the item name
|
|
|
|
//-------------------------------------------------------
|
2002-03-11 23:18:35 +03:00
|
|
|
#ifndef STANDALONE
|
|
|
|
PL_ARENA_ALLOCATE(mem, &mArena, (namelen + 1));
|
|
|
|
item->name = (char *) mem;
|
|
|
|
if ( !item->name )
|
|
|
|
{
|
|
|
|
status = ZIP_ERR_MEMORY;
|
|
|
|
// No need to delete name. It gets deleted only when the entire arena
|
|
|
|
// goes away.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
2001-04-03 10:35:13 +04:00
|
|
|
item->name = new char[namelen + 1];
|
2002-03-11 23:18:35 +03:00
|
|
|
#endif
|
2001-04-03 10:35:13 +04:00
|
|
|
if ( !item->name )
|
1999-11-02 23:37:28 +03:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
status = ZIP_ERR_MEMORY;
|
|
|
|
delete item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 leftover = (PRUint32)(bufsize - pos);
|
|
|
|
if ( leftover < namelen )
|
|
|
|
{
|
|
|
|
//-- not enough data left in buffer for the name.
|
|
|
|
//-- move leftover to top of buffer and read more
|
|
|
|
memcpy( buf, buf+pos, leftover );
|
|
|
|
bufsize = leftover + PR_Read( mFd, buf+leftover, bufsize-leftover );
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
//-- make sure we were able to read enough
|
|
|
|
if ( (PRUint32)bufsize < namelen )
|
1999-11-02 23:37:28 +03:00
|
|
|
{
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-04-03 10:35:13 +04:00
|
|
|
memcpy( item->name, buf+pos, namelen );
|
|
|
|
item->name[namelen] = 0;
|
|
|
|
|
|
|
|
//-- add item to file table
|
|
|
|
PRUint32 hash = HashName( item->name );
|
|
|
|
item->next = mFiles[hash];
|
|
|
|
mFiles[hash] = item;
|
1999-11-02 23:37:28 +03:00
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
pos += namelen;
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
// set up to process the next item at the top of loop
|
|
|
|
//-------------------------------------------------------
|
|
|
|
leftover = (PRUint32)(bufsize - pos);
|
2002-05-03 18:50:46 +04:00
|
|
|
if ( leftover < (extralen + commentlen + ZIPCENTRAL_SIZE) )
|
1999-11-02 23:37:28 +03:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- not enough data left to process at top of loop.
|
|
|
|
//-- move leftover and read more
|
|
|
|
memcpy( buf, buf+pos, leftover );
|
|
|
|
bufsize = leftover + PR_Read( mFd, buf+leftover, bufsize-leftover );
|
|
|
|
pos = 0;
|
1999-11-02 23:37:28 +03:00
|
|
|
}
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- set position to start of next ZipCentral record
|
|
|
|
pos += extralen + commentlen;
|
|
|
|
|
|
|
|
PRUint32 sig = xtolong( buf+pos );
|
|
|
|
if ( sig != CENTRALSIG )
|
1999-11-02 23:37:28 +03:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- we must be done or else archive is corrupt
|
|
|
|
if ( sig != ENDSIG )
|
1999-11-02 23:37:28 +03:00
|
|
|
status = ZIP_ERR_CORRUPT;
|
2001-04-03 10:35:13 +04:00
|
|
|
break;
|
1999-11-02 23:37:28 +03:00
|
|
|
}
|
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- make sure we've read enough
|
2002-05-12 21:29:30 +04:00
|
|
|
if ( bufsize < pos + ZIPCENTRAL_SIZE )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
1999-11-02 23:37:28 +03:00
|
|
|
} /* while reading central directory records */
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2002-05-03 18:50:46 +04:00
|
|
|
#if defined(DEBUG)
|
|
|
|
if (status != ZIP_OK) {
|
|
|
|
const char* msgs[] = { "ZIP_OK", "ZIP_ERR_GENERAL", "ZIP_ERR_MEMORY", "ZIP_ERR_DISK", "ZIP_ERR_CORRUPT", "ZIP_ERR_PARAM", "ZIP_ERR_FNF", "ZIP_ERR_UNSUPPORTED", "ZIP_ERR_SMALLBUF", "UNKNOWN" };
|
2002-05-12 21:29:30 +04:00
|
|
|
printf("nsZipArchive::BuildFileList status = %d '%s'\n", (int)status, msgs[(status <= 0 && status >= -8) ? -status : 9]);
|
2002-05-03 18:50:46 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
return status;
|
|
|
|
}
|
1999-10-26 23:43:26 +04:00
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::GetFileItem
|
|
|
|
//---------------------------------------------
|
2000-01-25 00:28:28 +03:00
|
|
|
nsZipItem* nsZipArchive::GetFileItem( const char * zipEntry )
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
2000-01-25 00:28:28 +03:00
|
|
|
PR_ASSERT( zipEntry != 0 );
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
nsZipItem* item = mFiles[ HashName(zipEntry) ];
|
1999-10-26 23:43:26 +04:00
|
|
|
|
|
|
|
for ( ; item != 0; item = item->next )
|
|
|
|
{
|
2000-01-25 00:28:28 +03:00
|
|
|
if ( 0 == PL_strcmp( zipEntry, item->name ) )
|
1999-10-26 23:43:26 +04:00
|
|
|
break; //-- found it
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::HashName
|
|
|
|
//---------------------------------------------
|
|
|
|
PRUint32 nsZipArchive::HashName( const char* aName )
|
|
|
|
{
|
|
|
|
PRUint32 val = 0;
|
|
|
|
PRUint8* c;
|
|
|
|
|
|
|
|
PR_ASSERT( aName != 0 );
|
|
|
|
|
|
|
|
for ( c = (PRUint8*)aName; *c != 0; c++ ) {
|
|
|
|
val = val*37 + *c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (val % ZIP_TABSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
2000-02-14 04:57:01 +03:00
|
|
|
// nsZipArchive::SeekToItem
|
1999-10-26 23:43:26 +04:00
|
|
|
//---------------------------------------------
|
2001-04-03 10:35:13 +04:00
|
|
|
PRInt32 nsZipArchive::SeekToItem(const nsZipItem* aItem)
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
PR_ASSERT (aItem);
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- the first time an item is used we need to calculate its offset
|
2001-12-07 04:16:20 +03:00
|
|
|
if ( !(aItem->flags & ZIFLAG_DATAOFFSET) )
|
2001-04-03 10:35:13 +04:00
|
|
|
{
|
2001-12-07 04:16:20 +03:00
|
|
|
//-- aItem->offset contains the header offset, not the data offset.
|
|
|
|
//-- read local header to get variable length values and calculate
|
|
|
|
//-- the real data offset
|
|
|
|
//--
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- NOTE: extralen is different in central header and local header
|
|
|
|
//-- for archives created using the Unix "zip" utility. To set
|
2001-12-07 04:16:20 +03:00
|
|
|
//-- the offset accurately we need the _local_ extralen.
|
|
|
|
if ( !ZIP_Seek( mFd, aItem->offset, PR_SEEK_SET ) )
|
2001-04-03 10:35:13 +04:00
|
|
|
return ZIP_ERR_CORRUPT;
|
2001-12-07 04:16:20 +03:00
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
ZipLocal Local;
|
2002-05-03 18:50:46 +04:00
|
|
|
if ( PR_Read(mFd, (char*)&Local, ZIPLOCAL_SIZE) != (READTYPE) ZIPLOCAL_SIZE
|
2001-04-03 10:35:13 +04:00
|
|
|
|| xtolong( Local.signature ) != LOCALSIG )
|
2001-12-07 04:16:20 +03:00
|
|
|
{
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- read error or local header not found
|
|
|
|
return ZIP_ERR_CORRUPT;
|
|
|
|
}
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2002-05-03 18:50:46 +04:00
|
|
|
((nsZipItem*)aItem)->offset += ZIPLOCAL_SIZE +
|
2001-12-07 04:16:20 +03:00
|
|
|
xtoint( Local.filename_len ) +
|
|
|
|
xtoint( Local.extrafield_len );
|
|
|
|
((nsZipItem*)aItem)->flags |= ZIFLAG_DATAOFFSET;
|
2001-04-03 10:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-- move to start of file in archive
|
|
|
|
if ( !ZIP_Seek( mFd, aItem->offset, PR_SEEK_SET ) )
|
1999-10-26 23:43:26 +04:00
|
|
|
return ZIP_ERR_CORRUPT;
|
1999-11-12 01:08:08 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
return ZIP_OK;
|
|
|
|
}
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
//---------------------------------------------
|
2000-02-14 04:57:01 +03:00
|
|
|
// nsZipArchive::CopyItemToBuffer
|
1999-10-26 23:43:26 +04:00
|
|
|
//---------------------------------------------
|
2000-02-14 04:57:01 +03:00
|
|
|
PRInt32 nsZipArchive::CopyItemToBuffer(const nsZipItem* aItem, char* aOutBuf)
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
2000-02-14 04:57:01 +03:00
|
|
|
PR_ASSERT(aOutBuf != 0 && aItem != 0);
|
1999-11-03 02:46:09 +03:00
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- move to the start of file's data
|
|
|
|
if ( SeekToItem( aItem ) != ZIP_OK )
|
|
|
|
return ZIP_ERR_CORRUPT;
|
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
//-- Read from file
|
2000-02-14 04:57:01 +03:00
|
|
|
PRUint32 actual = PR_Read( mFd, aOutBuf, aItem->realsize);
|
|
|
|
if (actual != aItem->realsize)
|
|
|
|
return ZIP_ERR_CORRUPT;
|
1999-11-12 01:08:08 +03:00
|
|
|
|
|
|
|
//-- verify crc32
|
2000-02-14 04:57:01 +03:00
|
|
|
PRUint32 calculatedCRC = crc32(0L, Z_NULL, 0);
|
|
|
|
calculatedCRC = crc32( calculatedCRC,(const unsigned char*)aOutBuf,
|
|
|
|
aItem->realsize);
|
|
|
|
if (calculatedCRC != aItem->crc32)
|
|
|
|
return ZIP_ERR_CORRUPT;
|
1999-10-26 23:43:26 +04:00
|
|
|
return ZIP_OK;
|
|
|
|
}
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::CopyItemToDisk
|
|
|
|
//---------------------------------------------
|
2000-01-25 00:28:28 +03:00
|
|
|
PRInt32 nsZipArchive::CopyItemToDisk(const nsZipItem* aItem, PRFileDesc* fOut)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
PRInt32 status = ZIP_OK;
|
1999-11-12 01:08:08 +03:00
|
|
|
PRUint32 chunk, pos, size, crc;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
PR_ASSERT( aItem != 0 && fOut != 0 );
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- move to the start of file's data
|
|
|
|
if ( SeekToItem( aItem ) != ZIP_OK )
|
|
|
|
return ZIP_ERR_CORRUPT;
|
|
|
|
|
2001-12-01 03:50:13 +03:00
|
|
|
char buf[ZIP_BUFLEN];
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-11-12 01:08:08 +03:00
|
|
|
//-- initialize crc
|
|
|
|
crc = crc32(0L, Z_NULL, 0);
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//-- copy chunks until file is done
|
|
|
|
size = aItem->size;
|
|
|
|
for ( pos=0; pos < size; pos += chunk )
|
|
|
|
{
|
|
|
|
chunk = (pos+ZIP_BUFLEN <= size) ? ZIP_BUFLEN : size - pos;
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( PR_Read( mFd, buf, chunk ) != (READTYPE)chunk )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
//-- unexpected end of data in archive
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-11-12 01:08:08 +03:00
|
|
|
//-- incrementally update crc32
|
|
|
|
crc = crc32(crc, (const unsigned char*)buf, chunk);
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( PR_Write( fOut, buf, chunk ) < (READTYPE)chunk )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
//-- Couldn't write all the data (disk full?)
|
|
|
|
status = ZIP_ERR_DISK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-12 01:08:08 +03:00
|
|
|
//-- verify crc32
|
|
|
|
if ( (status == ZIP_OK) && (crc != aItem->crc32) )
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------
|
1999-10-26 23:43:26 +04:00
|
|
|
// nsZipArchive::InflateItem
|
1999-03-09 05:13:56 +03:00
|
|
|
//---------------------------------------------
|
2000-01-25 00:28:28 +03:00
|
|
|
PRInt32 nsZipArchive::InflateItem( const nsZipItem* aItem, PRFileDesc* fOut,
|
1999-10-26 23:43:26 +04:00
|
|
|
char* bigBuf )
|
|
|
|
/*
|
|
|
|
* This function either inflates an archive item to disk, to the
|
|
|
|
* file specified by aOutname, or into a buffer specified by
|
|
|
|
* bigBuf. bigBuf then gets copied into the "real" output
|
|
|
|
* buffer a chunk at a time by ReadInflatedItem(). Memory-wise,
|
|
|
|
* this is inefficient, since it stores an entire copy of the
|
|
|
|
* decompressed item in memory, then copies it to the caller's
|
|
|
|
* buffer. A more memory-efficient implementation is possible,
|
|
|
|
* in which the outbuf in this function could be returned
|
|
|
|
* directly, but implementing it would be complex.
|
|
|
|
*/
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
PRInt32 status = ZIP_OK;
|
1999-11-12 01:08:08 +03:00
|
|
|
PRUint32 chunk, inpos, outpos, size, crc;
|
1999-10-26 23:43:26 +04:00
|
|
|
PRUint32 bigBufSize;
|
1999-03-09 05:13:56 +03:00
|
|
|
z_stream zs;
|
|
|
|
int zerr;
|
|
|
|
PRBool bInflating = PR_FALSE;
|
1999-06-30 03:23:23 +04:00
|
|
|
PRBool bRead;
|
|
|
|
PRBool bWrote;
|
1999-10-26 23:43:26 +04:00
|
|
|
PRBool bToFile;
|
1999-11-17 08:40:00 +03:00
|
|
|
Bytef* old_next_out;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
// -- if aOutname is null, we'll be writing to a buffer instead of a file
|
2000-01-25 00:28:28 +03:00
|
|
|
if (fOut != 0)
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
|
|
|
PR_ASSERT( aItem != 0 );
|
|
|
|
bToFile = PR_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// -- Writing to a buffer, so bigBuf must not be null.
|
|
|
|
PR_ASSERT( aItem != 0 && bigBuf != 0 );
|
|
|
|
bToFile = PR_FALSE;
|
|
|
|
bigBufSize = aItem->realsize;
|
|
|
|
}
|
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- move to the start of file's data
|
|
|
|
if ( SeekToItem( aItem ) != ZIP_OK )
|
|
|
|
return ZIP_ERR_CORRUPT;
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//-- allocate deflation buffers
|
2001-12-01 03:50:13 +03:00
|
|
|
Bytef inbuf[ZIP_BUFLEN];
|
|
|
|
Bytef outbuf[ZIP_BUFLEN];
|
2001-12-15 09:31:49 +03:00
|
|
|
|
2002-01-04 08:46:48 +03:00
|
|
|
//-- set up the inflate
|
|
|
|
memset( &zs, 0, sizeof(zs) );
|
|
|
|
|
|
|
|
#ifndef STANDALONE
|
2001-12-15 09:31:49 +03:00
|
|
|
//-- ensure we have our zlib allocator for better performance
|
|
|
|
if (!gZlibAllocator) {
|
2002-03-11 23:18:35 +03:00
|
|
|
gZlibAllocator = new nsRecyclingAllocator(NBUCKETS, NS_DEFAULT_RECYCLE_TIMEOUT, "libjar");
|
2001-12-15 09:31:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
zs.zalloc = zlibAlloc;
|
|
|
|
zs.zfree = zlibFree;
|
|
|
|
zs.opaque = gZlibAllocator;
|
2002-01-04 08:46:48 +03:00
|
|
|
#endif
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
zerr = inflateInit2( &zs, -MAX_WBITS );
|
|
|
|
if ( zerr != Z_OK )
|
|
|
|
{
|
|
|
|
status = ZIP_ERR_GENERAL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
bInflating = PR_TRUE;
|
|
|
|
|
|
|
|
|
|
|
|
//-- inflate loop
|
|
|
|
size = aItem->size;
|
|
|
|
outpos = inpos = 0;
|
|
|
|
zs.next_out = outbuf;
|
|
|
|
zs.avail_out = ZIP_BUFLEN;
|
1999-11-17 08:40:00 +03:00
|
|
|
crc = crc32(0L, Z_NULL, 0);
|
1999-03-09 05:13:56 +03:00
|
|
|
while ( zerr == Z_OK )
|
|
|
|
{
|
1999-06-30 03:23:23 +04:00
|
|
|
bRead = PR_FALSE;
|
|
|
|
bWrote = PR_FALSE;
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
if ( zs.avail_in == 0 && zs.total_in < size )
|
|
|
|
{
|
|
|
|
//-- no data to inflate yet still more in file:
|
|
|
|
//-- read another chunk of compressed data
|
|
|
|
|
|
|
|
inpos = zs.total_in; // input position
|
|
|
|
chunk = ( inpos + ZIP_BUFLEN <= size ) ? ZIP_BUFLEN : size - inpos;
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
if ( PR_Read( mFd, inbuf, chunk ) != (READTYPE)chunk )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
//-- unexpected end of data
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
1999-11-12 01:08:08 +03:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
zs.next_in = inbuf;
|
1999-06-30 03:23:23 +04:00
|
|
|
zs.avail_in = chunk;
|
|
|
|
bRead = PR_TRUE;
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( zs.avail_out == 0 )
|
|
|
|
{
|
1999-10-26 23:43:26 +04:00
|
|
|
if (bToFile)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
1999-10-26 23:43:26 +04:00
|
|
|
//-- write inflated buffer to disk and make space
|
|
|
|
if ( PR_Write( fOut, outbuf, ZIP_BUFLEN ) < ZIP_BUFLEN )
|
|
|
|
{
|
|
|
|
//-- Couldn't write all the data (disk full?)
|
|
|
|
status = ZIP_ERR_DISK;
|
|
|
|
break;
|
|
|
|
}
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
1999-10-26 23:43:26 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//-- copy inflated buffer to our big buffer
|
|
|
|
// Assertion makes sure we don't overflow bigBuf
|
|
|
|
PR_ASSERT( outpos + ZIP_BUFLEN <= bigBufSize);
|
1999-11-03 02:46:09 +03:00
|
|
|
char* copyStart = bigBuf + outpos;
|
|
|
|
memcpy(copyStart, outbuf, ZIP_BUFLEN);
|
1999-11-12 01:08:08 +03:00
|
|
|
}
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
outpos = zs.total_out;
|
|
|
|
zs.next_out = outbuf;
|
1999-06-30 03:23:23 +04:00
|
|
|
zs.avail_out = ZIP_BUFLEN;
|
|
|
|
bWrote = PR_TRUE;
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
1999-06-30 03:23:23 +04:00
|
|
|
if(bRead || bWrote)
|
1999-11-16 08:43:44 +03:00
|
|
|
{
|
1999-11-17 08:40:00 +03:00
|
|
|
old_next_out = zs.next_out;
|
|
|
|
|
1999-06-30 03:23:23 +04:00
|
|
|
zerr = inflate( &zs, Z_PARTIAL_FLUSH );
|
1999-11-17 08:40:00 +03:00
|
|
|
|
1999-11-16 08:43:44 +03:00
|
|
|
//-- incrementally update crc32
|
1999-11-17 08:40:00 +03:00
|
|
|
crc = crc32(crc, (const unsigned char*)old_next_out, zs.next_out - old_next_out);
|
1999-11-16 08:43:44 +03:00
|
|
|
}
|
1999-06-30 03:23:23 +04:00
|
|
|
else
|
|
|
|
zerr = Z_STREAM_END;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2001-01-22 23:15:49 +03:00
|
|
|
#if defined STANDALONE && defined XP_WIN
|
|
|
|
ProcessWindowsMessages();
|
|
|
|
#endif
|
1999-03-09 05:13:56 +03:00
|
|
|
} // while
|
|
|
|
|
1999-11-12 01:08:08 +03:00
|
|
|
//-- verify crc32
|
|
|
|
if ( (status == ZIP_OK) && (crc != aItem->crc32) )
|
|
|
|
{
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
goto cleanup;
|
1999-11-17 08:40:00 +03:00
|
|
|
}
|
1999-11-12 01:08:08 +03:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//-- write last inflated bit to disk
|
1999-03-10 02:06:11 +03:00
|
|
|
if ( zerr == Z_STREAM_END && outpos < zs.total_out )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
chunk = zs.total_out - outpos;
|
1999-10-26 23:43:26 +04:00
|
|
|
if (bToFile)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
1999-10-26 23:43:26 +04:00
|
|
|
if ( PR_Write( fOut, outbuf, chunk ) < (READTYPE)chunk )
|
|
|
|
status = ZIP_ERR_DISK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PR_ASSERT( (outpos + chunk) <= bigBufSize );
|
1999-11-03 02:46:09 +03:00
|
|
|
char* copyStart = bigBuf + outpos;
|
|
|
|
memcpy(copyStart, outbuf, chunk);
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- convert zlib error to return value
|
|
|
|
if ( status == ZIP_OK && zerr != Z_OK && zerr != Z_STREAM_END )
|
|
|
|
{
|
|
|
|
status = (zerr == Z_MEM_ERROR) ? ZIP_ERR_MEMORY : ZIP_ERR_CORRUPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- if found no errors make sure we've converted the whole thing
|
|
|
|
PR_ASSERT( status != ZIP_OK || zs.total_in == aItem->size );
|
|
|
|
PR_ASSERT( status != ZIP_OK || zs.total_out == aItem->realsize );
|
|
|
|
|
|
|
|
cleanup:
|
1999-10-26 23:43:26 +04:00
|
|
|
if ( bInflating )
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
|
|
|
//-- free zlib internal state
|
|
|
|
inflateEnd( &zs );
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::TestItem
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32 nsZipArchive::TestItem( const nsZipItem* aItem )
|
|
|
|
{
|
2001-12-01 03:50:13 +03:00
|
|
|
Bytef inbuf[ZIP_BUFLEN], outbuf[ZIP_BUFLEN], *old_next_out;
|
2001-04-03 10:35:13 +04:00
|
|
|
PRUint32 size, chunk=0, inpos, crc;
|
2000-12-27 10:05:55 +03:00
|
|
|
PRInt32 status = ZIP_OK;
|
|
|
|
int zerr = Z_OK;
|
|
|
|
z_stream zs;
|
|
|
|
PRBool bInflating = PR_FALSE;
|
|
|
|
PRBool bRead;
|
|
|
|
PRBool bWrote;
|
|
|
|
|
|
|
|
//-- param checks
|
|
|
|
if (!aItem)
|
|
|
|
return ZIP_ERR_PARAM;
|
|
|
|
if (aItem->compression != STORED && aItem->compression != DEFLATED)
|
|
|
|
return ZIP_ERR_UNSUPPORTED;
|
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
//-- move to the start of file's data
|
|
|
|
if ( SeekToItem( aItem ) != ZIP_OK )
|
|
|
|
return ZIP_ERR_CORRUPT;
|
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
//-- set up the inflate if DEFLATED
|
|
|
|
if (aItem->compression == DEFLATED)
|
|
|
|
{
|
|
|
|
memset( &zs, 0, sizeof(zs) );
|
|
|
|
zerr = inflateInit2( &zs, -MAX_WBITS );
|
|
|
|
if ( zerr != Z_OK )
|
|
|
|
{
|
|
|
|
status = ZIP_ERR_GENERAL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
zs.next_out = outbuf;
|
|
|
|
zs.avail_out = ZIP_BUFLEN;
|
|
|
|
}
|
|
|
|
bInflating = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- initialize crc checksum
|
|
|
|
crc = crc32(0L, Z_NULL, 0);
|
|
|
|
|
|
|
|
size = aItem->size;
|
|
|
|
inpos = 0;
|
|
|
|
|
|
|
|
//-- read in ZIP_BUFLEN-sized chunks of item
|
|
|
|
//-- inflating if item is DEFLATED
|
|
|
|
while ( zerr == Z_OK )
|
|
|
|
{
|
|
|
|
bRead = PR_FALSE; // used to check if new data to inflate
|
|
|
|
bWrote = PR_FALSE; // used to reset zs.next_out to outbuf
|
|
|
|
// when outbuf fills up
|
|
|
|
|
|
|
|
//-- read to inbuf
|
|
|
|
if (aItem->compression == DEFLATED)
|
|
|
|
{
|
|
|
|
if ( zs.avail_in == 0 && zs.total_in < size )
|
|
|
|
{
|
|
|
|
//-- no data to inflate yet still more in file:
|
|
|
|
//-- read another chunk of compressed data
|
|
|
|
|
|
|
|
inpos = zs.total_in; // input position
|
|
|
|
chunk = ( inpos + ZIP_BUFLEN <= size ) ? ZIP_BUFLEN : size - inpos;
|
|
|
|
|
|
|
|
if ( PR_Read( mFd, inbuf, chunk ) != (READTYPE)chunk )
|
|
|
|
{
|
|
|
|
//-- unexpected end of data
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
zs.next_in = inbuf;
|
|
|
|
zs.avail_in = chunk;
|
|
|
|
bRead = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( zs.avail_out == 0 )
|
|
|
|
{
|
|
|
|
//-- reuse output buffer
|
|
|
|
zs.next_out = outbuf;
|
|
|
|
zs.avail_out = ZIP_BUFLEN;
|
|
|
|
bWrote = PR_TRUE; // mimic writing to disk/memory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (inpos < size)
|
|
|
|
{
|
|
|
|
//-- read a chunk in
|
|
|
|
chunk = ( inpos + ZIP_BUFLEN <= size ) ? ZIP_BUFLEN : size - inpos;
|
|
|
|
|
|
|
|
if ( PR_Read( mFd, inbuf, chunk ) != (READTYPE)chunk )
|
|
|
|
{
|
|
|
|
//-- unexpected end of data
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
inpos += chunk;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//-- finished reading STORED item
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- inflate if item is DEFLATED
|
|
|
|
if (aItem->compression == DEFLATED)
|
|
|
|
{
|
|
|
|
if (bRead || bWrote)
|
|
|
|
{
|
|
|
|
old_next_out = zs.next_out;
|
|
|
|
|
|
|
|
zerr = inflate( &zs, Z_PARTIAL_FLUSH );
|
|
|
|
|
|
|
|
//-- incrementally update crc checksum
|
|
|
|
crc = crc32(crc, (const unsigned char*)old_next_out, zs.next_out - old_next_out);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
zerr = Z_STREAM_END;
|
|
|
|
}
|
|
|
|
//-- else just use input buffer containing data from STORED item
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//-- incrementally update crc checksum
|
|
|
|
crc = crc32(crc, (const unsigned char*)inbuf, chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- convert zlib error to return value
|
|
|
|
if ( status == ZIP_OK && zerr != Z_OK && zerr != Z_STREAM_END )
|
|
|
|
{
|
|
|
|
status = (zerr == Z_MEM_ERROR) ? ZIP_ERR_MEMORY : ZIP_ERR_CORRUPT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- verify computed crc checksum against header info crc
|
|
|
|
if (status == ZIP_OK && crc != aItem->crc32)
|
|
|
|
{
|
|
|
|
status = ZIP_ERR_CORRUPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if ( bInflating )
|
|
|
|
{
|
|
|
|
//-- free zlib internal state
|
|
|
|
inflateEnd( &zs );
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//------------------------------------------
|
|
|
|
// nsZipArchive constructor and destructor
|
|
|
|
//------------------------------------------
|
|
|
|
|
2001-01-28 02:31:15 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(nsZipArchive)
|
1999-11-16 04:52:29 +03:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
nsZipArchive::nsZipArchive()
|
2002-03-11 23:18:35 +03:00
|
|
|
: kMagic(ZIP_MAGIC), kArenaBlockSize(1*1024), mFd(0)
|
1999-03-09 05:13:56 +03:00
|
|
|
{
|
1999-11-16 04:52:29 +03:00
|
|
|
MOZ_COUNT_CTOR(nsZipArchive);
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
// initialize the table to NULL
|
2002-03-11 23:18:35 +03:00
|
|
|
memset(mFiles, 0, sizeof mFiles);
|
1999-10-26 23:43:26 +04:00
|
|
|
|
2002-03-11 23:18:35 +03:00
|
|
|
#ifndef STANDALONE
|
|
|
|
// Initialize our arena
|
|
|
|
PL_INIT_ARENA_POOL(&mArena, "ZipArena", kArenaBlockSize);
|
|
|
|
#endif
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsZipArchive::~nsZipArchive()
|
|
|
|
{
|
1999-11-12 09:13:13 +03:00
|
|
|
(void)CloseArchive();
|
1999-11-16 04:52:29 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsZipArchive);
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//------------------------------------------
|
|
|
|
// nsZipItem constructor and destructor
|
|
|
|
//------------------------------------------
|
|
|
|
|
2001-12-07 04:16:20 +03:00
|
|
|
nsZipItem::nsZipItem() : name(0), offset(0), next(0), flags(0)
|
1999-11-16 04:52:29 +03:00
|
|
|
{
|
|
|
|
}
|
1999-03-09 05:13:56 +03:00
|
|
|
|
|
|
|
nsZipItem::~nsZipItem()
|
|
|
|
{
|
2002-03-11 23:18:35 +03:00
|
|
|
#ifdef STANDALONE
|
1999-05-19 07:21:03 +04:00
|
|
|
if (name != 0 )
|
2000-03-30 13:52:32 +04:00
|
|
|
{
|
1999-03-09 05:13:56 +03:00
|
|
|
delete [] name;
|
2000-03-30 13:52:32 +04:00
|
|
|
name = 0;
|
|
|
|
}
|
2002-03-11 23:18:35 +03:00
|
|
|
#endif
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
1999-11-03 02:46:09 +03:00
|
|
|
//------------------------------------------
|
2001-12-21 08:57:39 +03:00
|
|
|
// nsZipRead
|
1999-11-03 02:46:09 +03:00
|
|
|
//------------------------------------------
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2001-01-28 02:31:15 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(nsZipRead)
|
1999-11-16 04:52:29 +03:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
//------------------------------------------
|
|
|
|
// nsZipFind constructor and destructor
|
|
|
|
//------------------------------------------
|
|
|
|
|
2001-01-28 02:31:15 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(nsZipFind)
|
1999-11-16 04:52:29 +03:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
nsZipFind::nsZipFind( nsZipArchive* aZip, char* aPattern, PRBool aRegExp )
|
|
|
|
: kMagic(ZIPFIND_MAGIC),
|
|
|
|
mArchive(aZip),
|
|
|
|
mPattern(aPattern),
|
|
|
|
mSlot(0),
|
|
|
|
mItem(0),
|
|
|
|
mRegExp(aRegExp)
|
1999-11-16 04:52:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsZipFind);
|
|
|
|
}
|
1999-05-19 07:21:03 +04:00
|
|
|
|
|
|
|
nsZipFind::~nsZipFind()
|
|
|
|
{
|
|
|
|
if (mPattern != 0 )
|
|
|
|
PL_strfree( mPattern );
|
1999-11-16 04:52:29 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsZipFind);
|
1999-05-19 07:21:03 +04:00
|
|
|
}
|
|
|
|
|
1999-06-23 10:16:28 +04:00
|
|
|
//------------------------------------------
|
|
|
|
// nsZipFind::GetArchive
|
|
|
|
//------------------------------------------
|
|
|
|
nsZipArchive* nsZipFind::GetArchive()
|
|
|
|
{
|
|
|
|
if (!mArchive)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return mArchive;
|
|
|
|
}
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
//------------------------------------------
|
1999-04-28 04:34:30 +04:00
|
|
|
// helper functions
|
1999-03-09 05:13:56 +03:00
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
* x t o i n t
|
|
|
|
*
|
|
|
|
* Converts a two byte ugly endianed integer
|
|
|
|
* to our platform's integer.
|
|
|
|
*/
|
|
|
|
static PRUint16 xtoint (unsigned char *ii)
|
|
|
|
{
|
1999-05-19 07:21:03 +04:00
|
|
|
return (PRUint16) ((ii [0]) | (ii [1] << 8));
|
1999-03-09 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* x t o l o n g
|
|
|
|
*
|
|
|
|
* Converts a four byte ugly endianed integer
|
|
|
|
* to our platform's integer.
|
|
|
|
*/
|
|
|
|
static PRUint32 xtolong (unsigned char *ll)
|
|
|
|
{
|
|
|
|
PRUint32 ret;
|
|
|
|
|
|
|
|
ret = (
|
|
|
|
(((PRUint32) ll [0]) << 0) |
|
|
|
|
(((PRUint32) ll [1]) << 8) |
|
|
|
|
(((PRUint32) ll [2]) << 16) |
|
|
|
|
(((PRUint32) ll [3]) << 24)
|
|
|
|
);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
1999-06-23 10:16:28 +04:00
|
|
|
|
1999-11-02 23:37:28 +03:00
|
|
|
/*
|
|
|
|
* ExtractMode
|
|
|
|
*
|
|
|
|
* Extracts bits 17-24 from a 32-bit unsigned long
|
|
|
|
* representation of the external attributes field.
|
|
|
|
* Subsequently it tacks on the implicit user-read
|
|
|
|
* bit.
|
|
|
|
*/
|
|
|
|
static PRUint16 ExtractMode(PRUint32 ext_attr)
|
|
|
|
{
|
|
|
|
ext_attr &= 0x00FF0000;
|
|
|
|
ext_attr >>= 16;
|
|
|
|
ext_attr |= 0x00000100;
|
|
|
|
|
|
|
|
return (PRUint16) ext_attr;
|
|
|
|
}
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2001-09-21 03:33:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Return true if the attributes are for a symbolic link
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PRBool IsSymlink(PRUint32 ext_attr)
|
|
|
|
{
|
|
|
|
return (((ext_attr>>16) & S_IFMT) == S_IFLNK) ? PR_TRUE : PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2000-03-28 07:38:06 +04:00
|
|
|
/*
|
|
|
|
* d o s d a t e
|
|
|
|
*
|
|
|
|
* Based on standard MS-DOS format date.
|
|
|
|
* Tweaked to be Y2K compliant and NSPR friendly.
|
|
|
|
*/
|
|
|
|
static void dosdate (char *aOutDateStr, PRUint16 aDate)
|
|
|
|
{
|
|
|
|
PRUint16 y2kCompliantYear = (aDate >> 9) + 1980;
|
|
|
|
|
|
|
|
sprintf (aOutDateStr, "%02d/%02d/%02d",
|
|
|
|
((aDate >> 5) & 0x0F), (aDate & 0x1F), y2kCompliantYear);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* d o s t i m e
|
|
|
|
*
|
|
|
|
* Standard MS-DOS format time.
|
|
|
|
*/
|
|
|
|
static void dostime (char *aOutTimeStr, PRUint16 aTime)
|
|
|
|
{
|
|
|
|
sprintf (aOutTimeStr, "%02d:%02d",
|
|
|
|
((aTime >> 11) & 0x1F), ((aTime >> 5) & 0x3F));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
nsZipItem::GetModTime()
|
|
|
|
{
|
|
|
|
char *timestr; /* e.g. 21:07 */
|
2000-03-30 13:52:32 +04:00
|
|
|
char *datestr; /* e.g. 06/20/1995 */
|
|
|
|
char *nsprstr; /* e.g. 06/20/1995 21:07 */
|
2000-03-28 07:38:06 +04:00
|
|
|
/* NSPR bug parsing dd/mm/yyyy hh:mm */
|
|
|
|
/* so we use mm/dd/yyyy hh:mm */
|
|
|
|
|
2000-03-30 13:52:32 +04:00
|
|
|
timestr = (char *) PR_Malloc(6 * sizeof(char));
|
|
|
|
datestr = (char *) PR_Malloc(11 * sizeof(char));
|
|
|
|
nsprstr = (char *) PR_Malloc(17 * sizeof(char));
|
2000-03-28 07:38:06 +04:00
|
|
|
if (!timestr || !datestr || !nsprstr)
|
|
|
|
{
|
2000-03-30 13:52:32 +04:00
|
|
|
PR_FREEIF(timestr);
|
|
|
|
PR_FREEIF(datestr);
|
|
|
|
PR_FREEIF(nsprstr);
|
2000-03-28 07:38:06 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(timestr, 0, 6);
|
2000-03-30 13:52:32 +04:00
|
|
|
memset(datestr, 0, 11);
|
|
|
|
memset(nsprstr, 0, 17);
|
2000-03-28 07:38:06 +04:00
|
|
|
|
|
|
|
dosdate(datestr, this->date);
|
|
|
|
dostime(timestr, this->time);
|
|
|
|
|
|
|
|
sprintf(nsprstr, "%s %s", datestr, timestr);
|
|
|
|
|
2000-03-30 13:52:32 +04:00
|
|
|
PR_FREEIF(timestr);
|
|
|
|
PR_FREEIF(datestr);
|
2000-03-28 07:38:06 +04:00
|
|
|
|
|
|
|
return nsprstr;
|
2000-03-28 09:06:01 +04:00
|
|
|
}
|
|
|
|
|