os/ossrv/compressionlibs/ziplib/test/oldezlib/zip/ZipFile.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// $Revision: 1.1 $
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
sl@0
    19
sl@0
    20
#include <caf/content.h>
sl@0
    21
#include <caf/data.h>
sl@0
    22
#include "oldzipfile.h"
sl@0
    23
sl@0
    24
using namespace TOLDEZIP;
sl@0
    25
// ====================================================================
sl@0
    26
// CZipFile public exported interface
sl@0
    27
// ====================================================================
sl@0
    28
sl@0
    29
/**
sl@0
    30
Creates a new CZipFile object using the supplied file server session and 
sl@0
    31
a valid file handle. The caller must have sufficient sufficient rights to 
sl@0
    32
access the content of the zipfile, if encrypted/protected.
sl@0
    33
sl@0
    34
@param aFs File server session used for opening the zipfile
sl@0
    35
@param aFile File handle to be used for accessing the zipfile
sl@0
    36
@return CZipFile object associated with the zipfile if it succeeded
sl@0
    37
@leave KZipArchiveError If file cannot be accessed(invalid handle, corrupt file header, etc.)
sl@0
    38
@leave KZipFileIOError If file cannot be read
sl@0
    39
@leave KCentralDirectoryTrailerNotFound If zip file header doesn't contain information about files inside the archive
sl@0
    40
@leave KCentralDirectoryTrailerInvalid If the information about files inside the archive is corrupt
sl@0
    41
@leave KMultiDiskArchivesNotSupported If zipfile is a multi disk archive
sl@0
    42
@leave ... Any one of the system-wide error codes for other errors.
sl@0
    43
*/
sl@0
    44
EXPORT_C CZipFile* CZipFile::NewL(RFs& aFs, RFile& aFile)
sl@0
    45
	{
sl@0
    46
	TFileName file;
sl@0
    47
	aFile.Name(file);
sl@0
    48
	CZipFile* zipFile = new(ELeave) CZipFile(aFs, file);
sl@0
    49
	CleanupStack::PushL(zipFile);
sl@0
    50
	zipFile->ConstructL(aFile);
sl@0
    51
	CleanupStack::Pop(zipFile);
sl@0
    52
	return zipFile;
sl@0
    53
	}
sl@0
    54
	
sl@0
    55
/**
sl@0
    56
Creates a new CZipFile object using the supplied file server session and 
sl@0
    57
file name. The caller must have sufficient capabilities to access the directory. 
sl@0
    58
The caller must also have sufficient rights to access the content of the 
sl@0
    59
zipfile, if encrypted/protected.
sl@0
    60
sl@0
    61
@param aFs File server session used for opening the zipfile
sl@0
    62
@param aFileName Name of the zipfile 
sl@0
    63
@return CZipFile object associated with the zipfile if it succeeded
sl@0
    64
@leave KZipArchiveError If file cannot be accessed(invalid handle, corrupt file header, etc.)
sl@0
    65
@leave KZipFileIOError If file cannot be read
sl@0
    66
@leave KCentralDirectoryTrailerNotFound If zip file header doesn't contain information about files inside the archive
sl@0
    67
@leave KCentralDirectoryTrailerInvalid If the information about files inside the archive is corrupt
sl@0
    68
@leave KMultiDiskArchivesNotSupported If zipfile is a multi disk archive. 
sl@0
    69
@leave ... Any one of the system-wide error codes for other errors.
sl@0
    70
*/
sl@0
    71
EXPORT_C CZipFile* CZipFile::NewL(RFs& aFs, const TDesC& aFileName)
sl@0
    72
	{
sl@0
    73
	CZipFile* zipFile = new(ELeave) CZipFile(aFs, aFileName);
sl@0
    74
	CleanupStack::PushL(zipFile);
sl@0
    75
	zipFile->ConstructL(aFileName);
sl@0
    76
	CleanupStack::Pop(zipFile);
sl@0
    77
	return zipFile;
sl@0
    78
	}
sl@0
    79
sl@0
    80
/**
sl@0
    81
Destructor
sl@0
    82
*/
sl@0
    83
EXPORT_C CZipFile::~CZipFile()
sl@0
    84
	{
sl@0
    85
	DeleteMemberPointers();
sl@0
    86
	}
sl@0
    87
sl@0
    88
/** 
sl@0
    89
@deprecated in 7.0 
sl@0
    90
*/
sl@0
    91
EXPORT_C CZipFile::CZipFile(RFs& aFs, const TDesC& aFileName)
sl@0
    92
	: iFileName(aFileName), iFs(aFs)
sl@0
    93
	{
sl@0
    94
	}
sl@0
    95
 
sl@0
    96
/** 
sl@0
    97
@deprecated in 7.0 
sl@0
    98
*/	
sl@0
    99
EXPORT_C TInt CZipFile::OpenL(void)
sl@0
   100
	{
sl@0
   101
	if (!iMemberPointers)
sl@0
   102
		{
sl@0
   103
		ConstructL(iFileName);
sl@0
   104
		}
sl@0
   105
	return KErrNone;
sl@0
   106
	}
sl@0
   107
	
sl@0
   108
/** 
sl@0
   109
@deprecated in 7.0 
sl@0
   110
*/
sl@0
   111
EXPORT_C void CZipFile::Close(void)
sl@0
   112
	{
sl@0
   113
	DeleteMemberPointers();
sl@0
   114
	}
sl@0
   115
sl@0
   116
/**
sl@0
   117
Second phase of construction. Used by Rfile using NewL overload.
sl@0
   118
sl@0
   119
@leave ... Any one of the system-wide error codes for other errors.
sl@0
   120
*/
sl@0
   121
EXPORT_C void CZipFile::ConstructL(RFile& aFile)
sl@0
   122
	{
sl@0
   123
	// Use the full name derived from the session path
sl@0
   124
	ContentAccess::CContent* content = 
sl@0
   125
		ContentAccess::CContent::NewL(aFile);
sl@0
   126
	CleanupStack::PushL(content);
sl@0
   127
	iData = content->OpenContentL(ContentAccess::EPeek);
sl@0
   128
sl@0
   129
	// Parent content object no longer needed because we only need data
sl@0
   130
	CleanupStack::PopAndDestroy(content);
sl@0
   131
sl@0
   132
	// Seek to the end
sl@0
   133
	TInt length = 0;
sl@0
   134
	User::LeaveIfError(iData->Seek(ESeekEnd, length));
sl@0
   135
	iFileLength = length;
sl@0
   136
sl@0
   137
	TInt status;
sl@0
   138
	TUint32 offset;
sl@0
   139
sl@0
   140
	if ((status = FindCentralDirectoryTrailer(offset)) != KErrNone) 
sl@0
   141
		{
sl@0
   142
		DeleteMemberPointers();
sl@0
   143
		User::Leave(status);
sl@0
   144
		}
sl@0
   145
sl@0
   146
	if ((status = ReadCentralDirectoryTrailer(offset, iTrailer)) != KErrNone) 
sl@0
   147
		{
sl@0
   148
		DeleteMemberPointers();
sl@0
   149
		User::Leave(status);
sl@0
   150
		}
sl@0
   151
sl@0
   152
	if (iTrailer.iStartDiskNumber != iTrailer.iDiskNumber) 
sl@0
   153
		{
sl@0
   154
		DeleteMemberPointers();
sl@0
   155
		User::Leave(KMultiDiskArchivesNotSupported);
sl@0
   156
		}
sl@0
   157
sl@0
   158
	if ((iTrailer.iOffset + iTrailer.iSize) > iFileLength)
sl@0
   159
		{
sl@0
   160
		DeleteMemberPointers();
sl@0
   161
	    User::Leave(KCentralDirectoryTrailerInvalid);
sl@0
   162
		}
sl@0
   163
sl@0
   164
	if (LoadMemberPointersL() != KErrNone)
sl@0
   165
		{
sl@0
   166
		User::Leave(KZipFileIOError);
sl@0
   167
		}	
sl@0
   168
	}
sl@0
   169
sl@0
   170
/**
sl@0
   171
Second phase of construction. Used by filename using NewL overload
sl@0
   172
sl@0
   173
@leave ... Any one of the system-wide error codes for other errors.
sl@0
   174
*/
sl@0
   175
EXPORT_C void CZipFile::ConstructL(const TDesC& aFileName)
sl@0
   176
	{
sl@0
   177
	TInt status;
sl@0
   178
	TUint32 offset;
sl@0
   179
	
sl@0
   180
	TRAP(status, OpenFileL(aFileName));
sl@0
   181
	if (status)
sl@0
   182
		{
sl@0
   183
		User::Leave(KZipArchiveError);
sl@0
   184
		}
sl@0
   185
	else
sl@0
   186
	if ((status = FindCentralDirectoryTrailer(offset)) != KErrNone) 
sl@0
   187
		{
sl@0
   188
		DeleteMemberPointers();
sl@0
   189
		User::Leave(status);
sl@0
   190
		}
sl@0
   191
	else 
sl@0
   192
	if ((status = ReadCentralDirectoryTrailer(offset, iTrailer)) != KErrNone) 
sl@0
   193
		{
sl@0
   194
		DeleteMemberPointers();
sl@0
   195
		User::Leave(status);
sl@0
   196
		}
sl@0
   197
	else 
sl@0
   198
	if (iTrailer.iStartDiskNumber != iTrailer.iDiskNumber) 
sl@0
   199
		{
sl@0
   200
		DeleteMemberPointers();
sl@0
   201
		User::Leave(KMultiDiskArchivesNotSupported);
sl@0
   202
		}
sl@0
   203
	else
sl@0
   204
	if ((iTrailer.iOffset > iFileLength) ||
sl@0
   205
	    ((iTrailer.iOffset + iTrailer.iSize) > iFileLength)) 
sl@0
   206
		{
sl@0
   207
		DeleteMemberPointers();
sl@0
   208
	    User::Leave(KCentralDirectoryTrailerInvalid);
sl@0
   209
		}
sl@0
   210
	else
sl@0
   211
	if (LoadMemberPointersL() != KErrNone)
sl@0
   212
		{
sl@0
   213
		User::Leave(KZipFileIOError);
sl@0
   214
		}	
sl@0
   215
	}
sl@0
   216
sl@0
   217
/**
sl@0
   218
Gets the size of the compressed data contained in the zip file in bytes
sl@0
   219
Each CZipFile object has a compressed and uncompressed size. This method will 
sl@0
   220
return the compressed size of a zip file.
sl@0
   221
sl@0
   222
@param aSize On return, the size of the compressed data in bytes
sl@0
   223
@return KErrNotReady If object hasn't been properly constructed
sl@0
   224
@return KErrCASizeNotDetermined	If size could not be determined 
sl@0
   225
@return ... Any one of the system-wide error codes for other errors.
sl@0
   226
*/
sl@0
   227
EXPORT_C TInt CZipFile::Size(TInt& aSize) const
sl@0
   228
{
sl@0
   229
	TInt err = KErrNotReady;
sl@0
   230
	if (iData)
sl@0
   231
		{
sl@0
   232
		TRAP(err, iData->DataSizeL(aSize));
sl@0
   233
		}
sl@0
   234
	return err;
sl@0
   235
}
sl@0
   236
sl@0
   237
/**
sl@0
   238
Constructs and returns a CZipFileMember object which is used to access 
sl@0
   239
information about a compressed file contained in the CZipFile archive. 
sl@0
   240
The name of the file to be searched for in the zipfile is case-sensitive. 
sl@0
   241
sl@0
   242
@param aName The name of the file to be searched in the zipfile 
sl@0
   243
@return the pointer to CZipFileMember object
sl@0
   244
@return NULL if the file doesn't exist in the zipfile
sl@0
   245
@leave ... Any one of the system-wide error codes for other errors.
sl@0
   246
*/
sl@0
   247
EXPORT_C CZipFileMember* CZipFile::MemberL(const TDesC& aName)
sl@0
   248
	{
sl@0
   249
	TLocalHeader		  header;
sl@0
   250
	const TMemberPointer* memberPointer;
sl@0
   251
	HBufC* localName = aName.AllocL();
sl@0
   252
	TInt loop = 0;
sl@0
   253
	while (loop < localName->Length())
sl@0
   254
		{
sl@0
   255
		if ((*localName)[loop] == '\\')
sl@0
   256
			{
sl@0
   257
			(localName->Des())[loop] = '/';
sl@0
   258
			}
sl@0
   259
		loop++;
sl@0
   260
		}
sl@0
   261
	
sl@0
   262
	memberPointer = FindMemberPointer(*localName, EFalse);
sl@0
   263
	if (memberPointer == NULL)
sl@0
   264
		{
sl@0
   265
		delete localName;
sl@0
   266
		return NULL;
sl@0
   267
		}
sl@0
   268
	else
sl@0
   269
	if (ReadLocalHeader(memberPointer->iLocalHeaderOffset, header) != KErrNone)
sl@0
   270
		{
sl@0
   271
		delete localName;
sl@0
   272
		return NULL;
sl@0
   273
		}
sl@0
   274
	else
sl@0
   275
		{
sl@0
   276
		CleanupStack::PushL(localName);
sl@0
   277
		CZipFileMember* thisMember = MakeMemberL(*memberPointer, header);
sl@0
   278
		CleanupStack::PopAndDestroy();		// localName
sl@0
   279
		return thisMember;
sl@0
   280
		}
sl@0
   281
	}
sl@0
   282
sl@0
   283
/**
sl@0
   284
Constructs and returns a CZipFileMember object which is used to access 
sl@0
   285
information about a compressed file contained in the CZipFile archive. 
sl@0
   286
The name of the file to be searched for in the zipfile is case-insensitive.
sl@0
   287
sl@0
   288
@param aName The name of the file to be searched in the zipfile 
sl@0
   289
@return A pointer to a member object of zip file
sl@0
   290
@return NULL If the file doesn't exist in the zipfile
sl@0
   291
@leave ... Any one of the system-wide error codes for other errors.
sl@0
   292
*/
sl@0
   293
EXPORT_C CZipFileMember* CZipFile::CaseInsensitiveMemberL(const TDesC& aName)
sl@0
   294
{
sl@0
   295
	TLocalHeader		  header;
sl@0
   296
	const TMemberPointer* memberPointer;
sl@0
   297
	HBufC* localName = aName.AllocL();
sl@0
   298
	TInt loop=0;
sl@0
   299
	while (loop < localName->Length())
sl@0
   300
		{
sl@0
   301
		if ((*localName)[loop] == '\\')
sl@0
   302
			{
sl@0
   303
			(localName->Des())[loop] = '/';
sl@0
   304
			}
sl@0
   305
		loop++;
sl@0
   306
		}
sl@0
   307
	
sl@0
   308
	memberPointer = FindMemberPointer(*localName, ETrue);
sl@0
   309
	if (memberPointer == NULL)
sl@0
   310
		{
sl@0
   311
		delete localName;
sl@0
   312
		return NULL;
sl@0
   313
		}
sl@0
   314
	else
sl@0
   315
	if (ReadLocalHeader(memberPointer->iLocalHeaderOffset, header) != KErrNone)
sl@0
   316
		{
sl@0
   317
		delete localName;
sl@0
   318
		return NULL;
sl@0
   319
		}
sl@0
   320
	else
sl@0
   321
		{
sl@0
   322
		CleanupStack::PushL(localName);
sl@0
   323
		CZipFileMember* thisMember = MakeMemberL(*memberPointer, header);
sl@0
   324
		CleanupStack::PopAndDestroy();
sl@0
   325
		return thisMember;
sl@0
   326
		}
sl@0
   327
sl@0
   328
	}
sl@0
   329
sl@0
   330
/**
sl@0
   331
Constructs and returns a CZipFileMember object which is used to access 
sl@0
   332
information about a compressed file contained in the CZipFile archive. 
sl@0
   333
An exact match for the filename is searched for first. If a match is not found, 
sl@0
   334
a case-insensitive search is performed. If both filenames exist in the archive, 
sl@0
   335
the case-sensitive match will be returned.
sl@0
   336
 
sl@0
   337
@param aName The name of the file to be searched in the zipfile 
sl@0
   338
@return A pointer to a member object of zip file
sl@0
   339
@return NULL If the file doesn't exist in the zipfile
sl@0
   340
@leave ... Any one of the system-wide error codes for other errors.
sl@0
   341
*/
sl@0
   342
EXPORT_C CZipFileMember* CZipFile::CaseSensitiveOrCaseInsensitiveMemberL(const TDesC& aName)
sl@0
   343
{
sl@0
   344
	CZipFileMember* member;
sl@0
   345
	member = MemberL(aName);
sl@0
   346
	if (member)
sl@0
   347
		{
sl@0
   348
		return member;
sl@0
   349
		}
sl@0
   350
	else
sl@0
   351
		{
sl@0
   352
		return CaseInsensitiveMemberL(aName);
sl@0
   353
		}
sl@0
   354
}
sl@0
   355
sl@0
   356
/**
sl@0
   357
Creates and returns the input stream for a file in the archive. Only files 
sl@0
   358
compressed with Stored or Deflated compression methods are supported.
sl@0
   359
sl@0
   360
@param aMember The compressed file in the archive
sl@0
   361
@param aStream On return, the stream to be used for reading the contents of the compressed file.  The caller owns this object and is responsible for deleting it.
sl@0
   362
@return KErrNone if successful
sl@0
   363
@return KCompressionMethodNotSupported if compression format unsupported 
sl@0
   364
@return ... Any one of the system-wide error codes for other errors.
sl@0
   365
@leave ... Any one of the system-wide error codes for other errors.
sl@0
   366
*/
sl@0
   367
EXPORT_C TInt CZipFile::GetInputStreamL(const CZipFileMember* aMember, RZipFileMemberReaderStream*& aStream)
sl@0
   368
	{
sl@0
   369
	TUint32 compressionMethod;
sl@0
   370
	
sl@0
   371
	compressionMethod = aMember->iCompressionMethod;
sl@0
   372
	if ((compressionMethod != EStored) && (compressionMethod != EDeflated)) 
sl@0
   373
	    {
sl@0
   374
	    return KCompressionMethodNotSupported;
sl@0
   375
	    }
sl@0
   376
	aStream = RZipFileMemberReaderStream::NewL(
sl@0
   377
								*this,
sl@0
   378
							   aMember->iDataOffset,
sl@0
   379
							   aMember->iCompressedSize,
sl@0
   380
							   aMember->iUncompressedSize,
sl@0
   381
							   compressionMethod);
sl@0
   382
	return KErrNone;
sl@0
   383
	}
sl@0
   384
	
sl@0
   385
sl@0
   386
sl@0
   387
/**
sl@0
   388
Gets the iterator used for iterating through the files contained in the ZIP 
sl@0
   389
file. It is the caller's responsibility to release the iterator when finsihed.
sl@0
   390
 
sl@0
   391
@return Pointer to a newly allocated CZipFileMemberIterator object
sl@0
   392
@leave ... Any one of the system-wide error codes for other errors.
sl@0
   393
*/
sl@0
   394
EXPORT_C CZipFileMemberIterator* CZipFile::GetMembersL()
sl@0
   395
	{
sl@0
   396
	return new (ELeave) CZipFileMemberIterator(this);
sl@0
   397
	}
sl@0
   398
	
sl@0
   399
sl@0
   400
sl@0
   401
// Implementation
sl@0
   402
sl@0
   403
/*
sl@0
   404
 * Find the 'end of central directory record'. This is at the 'end' of
sl@0
   405
 * the file, but since it is not a fixed length structure, we have to
sl@0
   406
 * hunt for it.
sl@0
   407
 *
sl@0
   408
 * We try assuming that the variable length section of the record is
sl@0
   409
 * empty, which usually appears to be the case.
sl@0
   410
 *
sl@0
   411
 * If this does not work we resort to 'walking backwards' through the
sl@0
   412
 * file looking for the signature bytes.
sl@0
   413
 *
sl@0
   414
 */
sl@0
   415
 
sl@0
   416
TInt CZipFile::FindCentralDirectoryTrailer(TUint32& offset)
sl@0
   417
{
sl@0
   418
	TBuf8<KSignatureLength> signature;
sl@0
   419
		
sl@0
   420
    if (iFileLength <= KCentralDirectoryTrailerFixedLength) 
sl@0
   421
		{
sl@0
   422
    	return KZipArchiveError;
sl@0
   423
		}
sl@0
   424
    // Try the obvious place first.Assuming that the comment (variable 
sl@0
   425
    // length section) is empty,try to find the signature at the offset.
sl@0
   426
    offset = iFileLength - KCentralDirectoryTrailerFixedLength;
sl@0
   427
	if (Seek(offset) != KErrNone) 
sl@0
   428
		{
sl@0
   429
    	return KZipFileIOError;
sl@0
   430
		}
sl@0
   431
	TInt err = iData->Read(signature);
sl@0
   432
sl@0
   433
	if ( err != KErrNone) 
sl@0
   434
		{
sl@0
   435
		return KZipFileIOError;
sl@0
   436
		}
sl@0
   437
sl@0
   438
	if ((signature[0] == 0x50) && 
sl@0
   439
		(signature[1] == 0x4b) &&
sl@0
   440
		(signature[2] == 0x05) &&
sl@0
   441
		(signature[3] == 0x06)) 
sl@0
   442
		{
sl@0
   443
		return KErrNone;
sl@0
   444
		}
sl@0
   445
	else 
sl@0
   446
		{
sl@0
   447
		// There must be some comments, hence the central directory 
sl@0
   448
		// record > 22 bytes.
sl@0
   449
		// This is a slow but fairly obvious way of searching 
sl@0
   450
		// backwards through the file starting from the offset.
sl@0
   451
		TUint EndOfTrailerSearch = 0; //Upto beginning of File
sl@0
   452
sl@0
   453
		if(iFileLength > KMaxTrailerSearchLength+KCentralDirectoryTrailerFixedLength)
sl@0
   454
			EndOfTrailerSearch = offset - KMaxTrailerSearchLength; //Upto Last 64K+22 bytes
sl@0
   455
sl@0
   456
		while (offset >= EndOfTrailerSearch) 
sl@0
   457
			{
sl@0
   458
			if (Seek(offset) != KErrNone)
sl@0
   459
				{
sl@0
   460
				return KZipFileIOError;
sl@0
   461
				}
sl@0
   462
			if (iData->Read(signature) != KErrNone)
sl@0
   463
				{
sl@0
   464
				return KZipFileIOError;
sl@0
   465
				}
sl@0
   466
			if ((signature[0] == 0x50) && 
sl@0
   467
				(signature[1] == 0x4b) &&
sl@0
   468
				(signature[2] == 0x05) &&
sl@0
   469
				(signature[3] == 0x06)) 
sl@0
   470
				{			
sl@0
   471
				return KErrNone;
sl@0
   472
				}
sl@0
   473
			--offset;
sl@0
   474
			}
sl@0
   475
		return KCentralDirectoryTrailerNotFound;
sl@0
   476
		}	
sl@0
   477
	}
sl@0
   478
	
sl@0
   479
TInt CZipFile::ReadCentralDirectoryTrailer(TUint32 offset, struct TCentralDirectoryTrailer& r )
sl@0
   480
{
sl@0
   481
    // Skip the signature		    
sl@0
   482
    if (Seek(offset + KSignatureLength) != KErrNone) 
sl@0
   483
    	{
sl@0
   484
    	return KZipFileIOError;
sl@0
   485
    	} 
sl@0
   486
	else
sl@0
   487
	if (Read(r.iDiskNumber) != KErrNone) 
sl@0
   488
		{
sl@0
   489
		return KZipFileIOError;
sl@0
   490
		}
sl@0
   491
	else
sl@0
   492
	if (Read(r.iStartDiskNumber)!= KErrNone) 
sl@0
   493
		{
sl@0
   494
		return KZipFileIOError;
sl@0
   495
		}
sl@0
   496
	else
sl@0
   497
	if (Read(r.iLocalEntryCount) != KErrNone) 
sl@0
   498
		{
sl@0
   499
		return KZipFileIOError;
sl@0
   500
		}
sl@0
   501
	else
sl@0
   502
	if (Read(r.iTotalEntryCount) != KErrNone) 
sl@0
   503
		{
sl@0
   504
		return KZipFileIOError;
sl@0
   505
		}
sl@0
   506
	else
sl@0
   507
	if (Read(r.iSize) != KErrNone) 
sl@0
   508
		{
sl@0
   509
		return KZipFileIOError;
sl@0
   510
		}
sl@0
   511
	else
sl@0
   512
	if (Read(r.iOffset) != KErrNone) 
sl@0
   513
		{
sl@0
   514
		return KZipFileIOError;
sl@0
   515
		}
sl@0
   516
	else
sl@0
   517
		{
sl@0
   518
		return KErrNone;
sl@0
   519
		}
sl@0
   520
}
sl@0
   521
sl@0
   522
TInt CZipFile::LoadMemberPointersL(void)
sl@0
   523
	{
sl@0
   524
	TCentralDirectoryHeader	header;
sl@0
   525
	TUint32					i;
sl@0
   526
	TUint32					memberPointerCount;
sl@0
   527
		
sl@0
   528
	if (Seek(iTrailer.iOffset) != KErrNone)
sl@0
   529
		{
sl@0
   530
		return KZipFileIOError;
sl@0
   531
		}
sl@0
   532
	memberPointerCount = iTrailer.iTotalEntryCount;
sl@0
   533
	iMemberPointers = new (ELeave) TMemberPointer[memberPointerCount];
sl@0
   534
	for (i = 0; i < memberPointerCount; i++)
sl@0
   535
		{
sl@0
   536
		iMemberPointers[i].iName = NULL;
sl@0
   537
		}
sl@0
   538
	CCnvCharacterSetConverter* converter = CCnvCharacterSetConverter::NewL();
sl@0
   539
	CleanupStack::PushL(converter);
sl@0
   540
	TInt converterState = CCnvCharacterSetConverter::KStateDefault;
sl@0
   541
	for (i = 0; i < memberPointerCount; i++)
sl@0
   542
		{
sl@0
   543
		if (ReadCentralDirectoryHeaderL(header, iMemberPointers[i], converter, converterState) != KErrNone)
sl@0
   544
			{
sl@0
   545
			return KZipFileError;
sl@0
   546
			}
sl@0
   547
		}
sl@0
   548
	CleanupStack::PopAndDestroy(converter);
sl@0
   549
	return KErrNone;
sl@0
   550
	}
sl@0
   551
	
sl@0
   552
LOCAL_C void ConvertFileNameToUnicodeL(
sl@0
   553
						TDes16& aUnicode,
sl@0
   554
						const TDesC8& aForeign,
sl@0
   555
						const TUint16& aMadeBy,
sl@0
   556
						CCnvCharacterSetConverter* aConverter,
sl@0
   557
						TInt aConverterState,
sl@0
   558
						RFs aFs)
sl@0
   559
// Have to decide whether filename encoding is CP850 or CP1252. According to tec support
sl@0
   560
// at WinZip, if 'madeby' is set to DOS(0) then the encoding is CP850 and if it's set to
sl@0
   561
// NTFS (11) then it's CP1252.  However, if the MS Compressed Folders program was used
sl@0
   562
// to zip, then madeby is always set to NTFS and the encoding is always CP850 - the exact
sl@0
   563
// opposite. Because of this confusion, I have written a very basic decision algorithm
sl@0
   564
// based on the premise that filenames are likely to use alphabet-style extended
sl@0
   565
// characters (rather than box edges or punctuation etc.)
sl@0
   566
	{
sl@0
   567
	TInt len = aForeign.Length();
sl@0
   568
	TInt ANSIpoints = 0;
sl@0
   569
	TInt OEMpoints = 0;
sl@0
   570
	for (TInt i=0; i<len; i++)
sl@0
   571
		{
sl@0
   572
		if (aForeign[i] >= 128 && aForeign[i] <= 165)
sl@0
   573
			OEMpoints ++;
sl@0
   574
		if (aForeign[i] >= 192 && aForeign[i] <= 255)
sl@0
   575
			ANSIpoints ++;
sl@0
   576
		}
sl@0
   577
	if (ANSIpoints == OEMpoints)
sl@0
   578
		{
sl@0
   579
		if (aMadeBy>>8) //only interested in the upper byte
sl@0
   580
			ANSIpoints ++;
sl@0
   581
		else
sl@0
   582
			OEMpoints ++;
sl@0
   583
		}
sl@0
   584
	TInt unconvertibleChars = 0;
sl@0
   585
sl@0
   586
	CCnvCharacterSetConverter::TAvailability availabilty = CCnvCharacterSetConverter::EAvailable;
sl@0
   587
	if (ANSIpoints > OEMpoints)
sl@0
   588
		{
sl@0
   589
		// It's probably ANSI (CP1252)
sl@0
   590
		availabilty = aConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierCodePage1252,aFs);
sl@0
   591
		aConverter->ConvertToUnicode(aUnicode, aForeign, aConverterState, unconvertibleChars);
sl@0
   592
		
sl@0
   593
		}
sl@0
   594
	if (OEMpoints > ANSIpoints || unconvertibleChars)
sl@0
   595
		{
sl@0
   596
		// It's definitely OEM (CP850)
sl@0
   597
 		availabilty = aConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierCP850,aFs);
sl@0
   598
		if(availabilty != CCnvCharacterSetConverter::EAvailable )
sl@0
   599
			{
sl@0
   600
			//if cp850 plugin is not available, use cp1252
sl@0
   601
			aConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierCodePage1252, aFs);
sl@0
   602
			}
sl@0
   603
		
sl@0
   604
		aConverter->ConvertToUnicode(aUnicode, aForeign, aConverterState);
sl@0
   605
		
sl@0
   606
		}
sl@0
   607
	}
sl@0
   608
sl@0
   609
TInt CZipFile::ReadCentralDirectoryHeaderL(
sl@0
   610
		 		   TCentralDirectoryHeader& aHeader, 
sl@0
   611
		 		   TMemberPointer&          aMemberPointer,
sl@0
   612
				   CCnvCharacterSetConverter* aConverter,
sl@0
   613
				   TInt aConverterState)
sl@0
   614
/*
sl@0
   615
As this function might be called many times and the request will 
sl@0
   616
eventually be translated to calls to server to read the data, 
sl@0
   617
so performance is the major issue. Try to minimize calls to server.
sl@0
   618
Read data in a chunk rather than member-by-member.
sl@0
   619
*/
sl@0
   620
	{
sl@0
   621
	TByte tmpHeader[KCentralDirectoryHeaderFixedLength];
sl@0
   622
 
sl@0
   623
	if (Read(tmpHeader,KCentralDirectoryHeaderFixedLength) != KErrNone)
sl@0
   624
		{
sl@0
   625
		return KZipFileIOError;
sl@0
   626
		}
sl@0
   627
sl@0
   628
	Mem::Copy(&aHeader.iSignature, &tmpHeader[0], 4);
sl@0
   629
sl@0
   630
	if (aHeader.iSignature != KCentralDirectoryHeaderSignature)
sl@0
   631
		{
sl@0
   632
		return KZipFileIOError;
sl@0
   633
		}
sl@0
   634
sl@0
   635
	Mem::Copy(&aHeader.iMadeBy, &tmpHeader[4], 2);
sl@0
   636
	Mem::Copy(&aHeader.iRequired, &tmpHeader[6], 2);
sl@0
   637
	Mem::Copy(&aHeader.iFlags, &tmpHeader[8], 2);
sl@0
   638
	Mem::Copy(&aHeader.iCompressionMethod, &tmpHeader[10], 2);
sl@0
   639
	Mem::Copy(&aHeader.iLastModifiedFileTime, &tmpHeader[12], 2);
sl@0
   640
	Mem::Copy(&aHeader.iLastModifiedFileDate, &tmpHeader[14], 2);
sl@0
   641
	Mem::Copy(&aHeader.iCRC32, &tmpHeader[16], 4);
sl@0
   642
	Mem::Copy(&aHeader.iCompressedSize, &tmpHeader[20], 4);
sl@0
   643
	Mem::Copy(&aHeader.iUncompressedSize, &tmpHeader[24], 4);
sl@0
   644
	Mem::Copy(&aHeader.iFileNameLength, &tmpHeader[28], 2);
sl@0
   645
	Mem::Copy(&aHeader.iExtraFieldLength, &tmpHeader[30], 2);
sl@0
   646
	Mem::Copy(&aHeader.iFileCommentLength, &tmpHeader[32], 2);
sl@0
   647
	Mem::Copy(&aHeader.iDiskNumberStart, &tmpHeader[34], 2);
sl@0
   648
	Mem::Copy(&aHeader.iInternalFileAttributes, &tmpHeader[36], 2);
sl@0
   649
	Mem::Copy(&aHeader.iExternalFileAttributes, &tmpHeader[38], 4);
sl@0
   650
	Mem::Copy(&aHeader.iLocalHeaderOffset, &tmpHeader[42], 4);
sl@0
   651
sl@0
   652
	aMemberPointer.iCRC32             = aHeader.iCRC32;
sl@0
   653
	aMemberPointer.iCompressedSize    = aHeader.iCompressedSize;
sl@0
   654
	aMemberPointer.iUncompressedSize  = aHeader.iUncompressedSize;
sl@0
   655
	aMemberPointer.iLocalHeaderOffset = aHeader.iLocalHeaderOffset;
sl@0
   656
    aMemberPointer.iName = new(ELeave) TFileName;
sl@0
   657
	
sl@0
   658
	TBuf8<KMaxFileName> input;
sl@0
   659
	if (iData->Read(input, aHeader.iFileNameLength) != KErrNone)
sl@0
   660
		{
sl@0
   661
		return KZipFileIOError;
sl@0
   662
		}
sl@0
   663
	ConvertFileNameToUnicodeL(*aMemberPointer.iName, input, aHeader.iMadeBy, aConverter, aConverterState, iFs);
sl@0
   664
	
sl@0
   665
    // Ignore the remaining fields
sl@0
   666
	TInt pos;
sl@0
   667
sl@0
   668
	pos = aHeader.iExtraFieldLength;	
sl@0
   669
	if (pos != 0)
sl@0
   670
		{
sl@0
   671
		// Don't pass aHeader.iExtraFieldLength in place of pos
sl@0
   672
		// as the below function will update the content of that variable.
sl@0
   673
		// In this case, the function is used to ignore the data
sl@0
   674
		// by just moving the current file pointer location.
sl@0
   675
		if (iData->Seek(ESeekCurrent, pos) != KErrNone) 
sl@0
   676
			{
sl@0
   677
			return KZipFileIOError;
sl@0
   678
			}
sl@0
   679
		}
sl@0
   680
sl@0
   681
	pos = aHeader.iFileCommentLength;
sl@0
   682
	if (pos != 0)
sl@0
   683
		{
sl@0
   684
		// Don't pass aHeader.iFileCommentLength in place of pos
sl@0
   685
		// as the below function will update the content of that variable.
sl@0
   686
		// In this case, the function is used to ignore the data
sl@0
   687
		// by just moving the current file pointer location.
sl@0
   688
		if (iData->Seek(ESeekCurrent, pos) != KErrNone) 
sl@0
   689
			{
sl@0
   690
			return KZipFileIOError;
sl@0
   691
			}
sl@0
   692
		}
sl@0
   693
sl@0
   694
  	return  KErrNone;
sl@0
   695
}
sl@0
   696
sl@0
   697
TInt CZipFile::ReadLocalHeader(TUint32 aOffset, TLocalHeader& aHeader)
sl@0
   698
/*
sl@0
   699
As this function might be called many times and the request will 
sl@0
   700
eventually be translated to calls to server to read the data, 
sl@0
   701
so performance is the major issue. Try to minimize calls to server.
sl@0
   702
Read data in a chunk rather than member-by-member.
sl@0
   703
*/
sl@0
   704
	{
sl@0
   705
	TByte tmpHeader[KLocalHeaderFixedLength];
sl@0
   706
sl@0
   707
	if (Seek(aOffset) != KErrNone)
sl@0
   708
		{
sl@0
   709
		return KZipFileIOError;
sl@0
   710
		}
sl@0
   711
	if (Read(tmpHeader,KLocalHeaderFixedLength) != KErrNone)
sl@0
   712
		{
sl@0
   713
		return KZipFileIOError;
sl@0
   714
		}
sl@0
   715
	Mem::Copy(&aHeader.iSignature, &tmpHeader[0], 4);
sl@0
   716
sl@0
   717
	if (aHeader.iSignature != KLocalHeaderSignature)
sl@0
   718
		{
sl@0
   719
		return KLocalHeaderSignatureInvalid;
sl@0
   720
		}
sl@0
   721
sl@0
   722
	Mem::Copy(&aHeader.iVersionNeeded, &tmpHeader[4], 2);
sl@0
   723
	Mem::Copy(&aHeader.iFlags, &tmpHeader[6], 2);
sl@0
   724
	Mem::Copy(&aHeader.iCompressionMethod, &tmpHeader[8], 2);
sl@0
   725
	Mem::Copy(&aHeader.iLastModifiedFileTime, &tmpHeader[10], 2);
sl@0
   726
	Mem::Copy(&aHeader.iLastModifiedFileDate, &tmpHeader[12], 2);
sl@0
   727
	Mem::Copy(&aHeader.iCRC32, &tmpHeader[14], 4);
sl@0
   728
	Mem::Copy(&aHeader.iCompressedSize, &tmpHeader[18], 4);
sl@0
   729
	Mem::Copy(&aHeader.iUncompressedSize, &tmpHeader[22], 4);
sl@0
   730
	Mem::Copy(&aHeader.iFileNameLength, &tmpHeader[26], 2);
sl@0
   731
	Mem::Copy(&aHeader.iExtraFieldLength, &tmpHeader[28], 2);
sl@0
   732
sl@0
   733
	return  KErrNone;
sl@0
   734
	}
sl@0
   735
	
sl@0
   736
const CZipFile::TMemberPointer* CZipFile::FindMemberPointer(const TDesC& aName, TBool aCaseInsensitive)
sl@0
   737
	{
sl@0
   738
	for (TUint32 i = 0; i < iTrailer.iTotalEntryCount; i++)
sl@0
   739
		{
sl@0
   740
		if (aCaseInsensitive && (!aName.CompareF(*iMemberPointers[i].iName)))
sl@0
   741
			{
sl@0
   742
			return iMemberPointers + i;
sl@0
   743
			}
sl@0
   744
		else if (aName == *iMemberPointers[i].iName)
sl@0
   745
			{
sl@0
   746
			return iMemberPointers + i;
sl@0
   747
			}
sl@0
   748
		}
sl@0
   749
	return NULL;
sl@0
   750
	}
sl@0
   751
sl@0
   752
RZipFileMemberReaderStream* CZipFile::MakeInputStreamL(
sl@0
   753
							TUint32 aDataOffset, 
sl@0
   754
							TUint32 aCompressedSize, 
sl@0
   755
							TUint32 aUncompressedSize, 
sl@0
   756
							TUint32 aCompressionMethod)
sl@0
   757
	{
sl@0
   758
	return RZipFileMemberReaderStream::NewL(
sl@0
   759
						    *this,
sl@0
   760
							aDataOffset,
sl@0
   761
							aCompressedSize,
sl@0
   762
							aUncompressedSize, 
sl@0
   763
							aCompressionMethod);
sl@0
   764
	}
sl@0
   765
sl@0
   766
CZipFileMember* CZipFile::MakeMemberL(TInt aMemberIndex)
sl@0
   767
	{
sl@0
   768
	TLocalHeader    header;
sl@0
   769
	TMemberPointer*	memberPointer;
sl@0
   770
	
sl@0
   771
	if (aMemberIndex >= iTrailer.iTotalEntryCount)
sl@0
   772
		{
sl@0
   773
		return NULL;
sl@0
   774
		}
sl@0
   775
	memberPointer = iMemberPointers + aMemberIndex;
sl@0
   776
	if (ReadLocalHeader(memberPointer->iLocalHeaderOffset, header) != KErrNone)
sl@0
   777
		{
sl@0
   778
		return NULL;
sl@0
   779
		}
sl@0
   780
	else
sl@0
   781
		{
sl@0
   782
		return MakeMemberL(*memberPointer, header);
sl@0
   783
		}
sl@0
   784
	}
sl@0
   785
	
sl@0
   786
CZipFileMember* CZipFile::MakeMemberL(
sl@0
   787
                              const TMemberPointer& aMemberPointer, 
sl@0
   788
			                  const TLocalHeader&   aHeader)
sl@0
   789
	{
sl@0
   790
	CZipFileMember* member;
sl@0
   791
	
sl@0
   792
	member = new (ELeave) CZipFileMember;
sl@0
   793
	CleanupStack::PushL(member);
sl@0
   794
	member->iCRC32 = aMemberPointer.iCRC32;
sl@0
   795
	member->iCompressedSize = aMemberPointer.iCompressedSize;
sl@0
   796
	member->iCompressionMethod = aHeader.iCompressionMethod;
sl@0
   797
	member->iName = new (ELeave) TFileName(*aMemberPointer.iName);
sl@0
   798
	TInt loop=0;
sl@0
   799
	while (loop < member->iName->Length())
sl@0
   800
		{
sl@0
   801
		if ((*member->iName)[loop] == '/')
sl@0
   802
			{
sl@0
   803
			(*member->iName)[loop] = '\\';
sl@0
   804
			}
sl@0
   805
		loop++;
sl@0
   806
		}
sl@0
   807
	member->iUncompressedSize = aMemberPointer.iUncompressedSize;
sl@0
   808
	member->iDataOffset = aMemberPointer.iLocalHeaderOffset + 
sl@0
   809
		                  KLocalHeaderFixedLength + 
sl@0
   810
						  aHeader.iFileNameLength + 
sl@0
   811
						  aHeader.iExtraFieldLength;
sl@0
   812
	CleanupStack::Pop();
sl@0
   813
	return member;
sl@0
   814
	}
sl@0
   815
sl@0
   816
void CZipFile::DeleteMemberPointers()
sl@0
   817
	{
sl@0
   818
	if (iMemberPointers)
sl@0
   819
		{
sl@0
   820
		for (TUint32 i = 0; i < iTrailer.iTotalEntryCount; i++)
sl@0
   821
			{
sl@0
   822
			delete iMemberPointers[i].iName;
sl@0
   823
			}
sl@0
   824
		delete[] iMemberPointers;
sl@0
   825
		iMemberPointers = 0;
sl@0
   826
		}
sl@0
   827
sl@0
   828
	delete iData;
sl@0
   829
	iData = NULL;
sl@0
   830
	return;
sl@0
   831
	}
sl@0
   832
sl@0
   833
void CZipFile::OpenFileL(const TDesC& aFileName)
sl@0
   834
	{
sl@0
   835
	// We need to look at the session path of the filesystem passed
sl@0
   836
	// in to derive the fullpath of the file to open
sl@0
   837
	HBufC* sessionPath = HBufC::NewLC(KMaxFileName);
sl@0
   838
	TPtr ptr(sessionPath->Des());
sl@0
   839
	User::LeaveIfError(iFs.SessionPath(ptr));
sl@0
   840
	TParse parse;
sl@0
   841
	User::LeaveIfError(parse.Set(aFileName, sessionPath, NULL));
sl@0
   842
	
sl@0
   843
	// Use the full name derived from the session path
sl@0
   844
	ContentAccess::CContent* content = 
sl@0
   845
		ContentAccess::CContent::NewL(parse.FullName());
sl@0
   846
	CleanupStack::PushL(content);
sl@0
   847
	iData = content->OpenContentL(ContentAccess::EPeek);
sl@0
   848
sl@0
   849
	// Parent content object no longer needed because we only need data
sl@0
   850
	CleanupStack::PopAndDestroy(content);
sl@0
   851
sl@0
   852
	// Seek to the end
sl@0
   853
	TInt length = 0;
sl@0
   854
	User::LeaveIfError(iData->Seek(ESeekEnd, length));
sl@0
   855
	iFileLength = length;
sl@0
   856
	CleanupStack::PopAndDestroy(sessionPath);
sl@0
   857
	}
sl@0
   858
sl@0
   859
TInt CZipFile::Read(TUint16& aUs)
sl@0
   860
	{
sl@0
   861
	TPckgBuf<TUint16> temp(aUs);
sl@0
   862
	
sl@0
   863
	if (iData->Read(temp) != KErrNone)
sl@0
   864
		{
sl@0
   865
		return KZipFileIOError;
sl@0
   866
		}
sl@0
   867
	
sl@0
   868
	aUs=temp();
sl@0
   869
	return KErrNone;
sl@0
   870
	}
sl@0
   871
	
sl@0
   872
TInt CZipFile::Read(TUint32& aUl)
sl@0
   873
	{
sl@0
   874
	TPckgBuf<TUint32> temp;
sl@0
   875
	
sl@0
   876
	if (iData->Read(temp) != KErrNone)
sl@0
   877
		{
sl@0
   878
		return KZipFileIOError;
sl@0
   879
		}
sl@0
   880
	aUl=temp();
sl@0
   881
	return KErrNone;
sl@0
   882
	}
sl@0
   883
sl@0
   884
TInt CZipFile::Read(TByte* aBytes, TUint32 aLength)
sl@0
   885
sl@0
   886
	{
sl@0
   887
	TPtr8 ptr(aBytes, aLength);
sl@0
   888
	if(iData->Read(ptr, aLength))
sl@0
   889
		{
sl@0
   890
		return KZipFileIOError;
sl@0
   891
		}
sl@0
   892
	else
sl@0
   893
		{
sl@0
   894
		return KErrNone;
sl@0
   895
		}
sl@0
   896
	}
sl@0
   897
	
sl@0
   898
TInt CZipFile::Seek(TInt aOffset)
sl@0
   899
	{
sl@0
   900
	if (iData->Seek(ESeekStart, aOffset) < 0) 
sl@0
   901
		{
sl@0
   902
		return KZipFileIOError;
sl@0
   903
		}
sl@0
   904
	else
sl@0
   905
		{
sl@0
   906
		return KErrNone;
sl@0
   907
		}
sl@0
   908
	}
sl@0
   909