| 0 |      1 | // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|  |      2 | // All rights reserved.
 | 
|  |      3 | // This component and the accompanying materials are made available
 | 
|  |      4 | // under the terms of the License "Eclipse Public License v1.0"
 | 
|  |      5 | // which accompanies this distribution, and is available
 | 
|  |      6 | // at the URL "http://www.eclipse.org/legal/epl-v10.html".
 | 
|  |      7 | //
 | 
|  |      8 | // Initial Contributors:
 | 
|  |      9 | // Nokia Corporation - initial contribution.
 | 
|  |     10 | //
 | 
|  |     11 | // Contributors:
 | 
|  |     12 | //
 | 
|  |     13 | // Description:
 | 
|  |     14 | // e32test\misc\t_unzip.cpp
 | 
|  |     15 | // 
 | 
|  |     16 | //
 | 
|  |     17 | 
 | 
|  |     18 | #include <e32test.h>
 | 
|  |     19 | #include <f32file.h>
 | 
|  |     20 | #include "unzip.h"
 | 
|  |     21 | 
 | 
|  |     22 | RTest test(_L("T_UNZIP"));
 | 
|  |     23 | 
 | 
|  |     24 | RFile TheInputFile;
 | 
|  |     25 | TUint8* OutPtr;
 | 
|  |     26 | 
 | 
|  |     27 | GLDEF_C void AcceptUnzippedBlock(TZipInfo& /*aInfo*/, TUint8*& aOutPtr, TInt aError)
 | 
|  |     28 | 	{
 | 
|  |     29 | 	if (aError==KErrNone)
 | 
|  |     30 | 		OutPtr=aOutPtr;
 | 
|  |     31 | 	}
 | 
|  |     32 | 
 | 
|  |     33 | GLDEF_C TInt ReadInputData(TUint8* aDest, TInt& aLength)
 | 
|  |     34 | 	{
 | 
|  |     35 | 	TPtr8 ptr(aDest,0,aLength);
 | 
|  |     36 | 	return TheInputFile.Read(ptr);
 | 
|  |     37 | 	}
 | 
|  |     38 | 
 | 
|  |     39 | GLDEF_C TInt UnzipComplete(TZipInfo& /*a*/, TUint8* /*aOutPtr*/, TInt /*aError*/)
 | 
|  |     40 | 	{
 | 
|  |     41 | 	return KErrNone;
 | 
|  |     42 | 	}
 | 
|  |     43 | 
 | 
|  |     44 | _LIT(KLitThreadName,"Unzip");
 | 
|  |     45 | TInt Initialise(TZipInfo& a)
 | 
|  |     46 | 	{
 | 
|  |     47 | 	TInt r=InitInfo(a);
 | 
|  |     48 | 	if (r!=KErrNone)
 | 
|  |     49 | 		return r;
 | 
|  |     50 | 	a.iFileBufSize=4*a.iInBufSize;
 | 
|  |     51 | 	TAny* pFileBuf=MALLOC(a.iFileBufSize);
 | 
|  |     52 | 	if (!pFileBuf)
 | 
|  |     53 | 		return KErrNoMemory;
 | 
|  |     54 | 	a.iFileBuf=(TUint8*)pFileBuf;
 | 
|  |     55 | 	RThread t;
 | 
|  |     56 | 	r=t.Create(KLitThreadName,UnzipThread,0x2000,NULL,&a);
 | 
|  |     57 | 	if (r!=KErrNone)
 | 
|  |     58 | 		{
 | 
|  |     59 | 		FREE(pFileBuf);
 | 
|  |     60 | 		a.iFileBuf=NULL;
 | 
|  |     61 | 		return r;
 | 
|  |     62 | 		}
 | 
|  |     63 | 	t.SetPriority(EPriorityLess);
 | 
|  |     64 | 	t.Logon(a.iThreadStatus);
 | 
|  |     65 | 	t.Resume();
 | 
|  |     66 | 	a.iThreadHandle=t.Handle();
 | 
|  |     67 | 	return KErrNone;
 | 
|  |     68 | 	}
 | 
|  |     69 | 
 | 
|  |     70 | void ProcessHeader(TZipInfo& a)
 | 
|  |     71 | 	{
 | 
|  |     72 | 	test.Printf(_L("Flags=%d\n"),a.iFlags);
 | 
|  |     73 | 	test.Printf(_L("Method=%d\n"),a.iMethod);
 | 
|  |     74 | 	test.Printf(_L("Crc=%d\n"),a.iCrc);
 | 
|  |     75 | 	test.Printf(_L("Compressed size=%d\n"),a.iCompressedSize);
 | 
|  |     76 | 	test.Printf(_L("Uncompressed size=%d\n"),a.iUncompressedSize);
 | 
|  |     77 | 	test.Printf(_L("File name %S\n"),&a.iName);
 | 
|  |     78 | 	test.Printf(_L("Data offset %d\n\n"),a.iDataOffset);
 | 
|  |     79 | 
 | 
|  |     80 | 	test.Next(_L("Allocate memory for unzipped file"));
 | 
|  |     81 | 	a.iOutBuf=(TUint8*)User::Alloc(a.iUncompressedSize);
 | 
|  |     82 | 	test(a.iOutBuf!=NULL);
 | 
|  |     83 | 	test.Next(_L("Begin unzipping"));
 | 
|  |     84 | 	a.iHeaderDone=2;
 | 
|  |     85 | 	TRequestStatus* pS=&a.iProcessedHeader;
 | 
|  |     86 | 	RThread t;
 | 
|  |     87 | 	t.SetHandle(a.iThreadHandle);
 | 
|  |     88 | 	t.RequestComplete(pS,0);
 | 
|  |     89 | 	}
 | 
|  |     90 | 
 | 
|  |     91 | void Cleanup(TZipInfo& a)
 | 
|  |     92 | 	{
 | 
|  |     93 | 	delete a.iFileBuf;
 | 
|  |     94 | 	a.iFileBuf=NULL;
 | 
|  |     95 | 	delete a.iOutBuf;
 | 
|  |     96 | 	a.iOutBuf=NULL;
 | 
|  |     97 | 	RThread& t=*(RThread*)&a.iThreadHandle;
 | 
|  |     98 | 	t.Close();
 | 
|  |     99 | 	}
 | 
|  |    100 | 
 | 
|  |    101 | GLDEF_C TInt E32Main()
 | 
|  |    102 | 	{
 | 
|  |    103 | 	test.Title();
 | 
|  |    104 | 	TFileName inputFileName;
 | 
|  |    105 | 	User::CommandLine(inputFileName);
 | 
|  |    106 | 	test.Start(_L("Connect to file server"));
 | 
|  |    107 | 	RFs fs;
 | 
|  |    108 | 	TInt r=fs.Connect();
 | 
|  |    109 | 	test(r==KErrNone);
 | 
|  |    110 | 	test.Printf(_L("Open file %S\n"),&inputFileName);
 | 
|  |    111 | 	r=TheInputFile.Open(fs,inputFileName,EFileRead);
 | 
|  |    112 | 	test(r==KErrNone);
 | 
|  |    113 | 	TZipInfo z;
 | 
|  |    114 | 	r=TheInputFile.Size(z.iRemain);
 | 
|  |    115 | 	test(r==KErrNone);
 | 
|  |    116 | 	test.Printf(_L("File size %d\n"),z.iRemain);
 | 
|  |    117 | 
 | 
|  |    118 | 	test.Next(_L("Initialise"));
 | 
|  |    119 | 	r=Initialise(z);
 | 
|  |    120 | 	test(r==KErrNone);
 | 
|  |    121 | 
 | 
|  |    122 | 	test.Next(_L("Read header"));
 | 
|  |    123 | 	TUint32 c=0;
 | 
|  |    124 | 	RThread t;
 | 
|  |    125 | 	t.SetHandle(z.iThreadHandle);
 | 
|  |    126 | 	while (z.iRemain && z.iThreadStatus==KRequestPending)
 | 
|  |    127 | 		{
 | 
|  |    128 | 		TRequestStatus dummy;
 | 
|  |    129 | 		TRequestStatus* pS=&dummy;
 | 
|  |    130 | 		r=ReadBlockToBuffer(z);
 | 
|  |    131 | 		test(r==KErrNone);
 | 
|  |    132 | 		t.RequestComplete(pS,0);		// same process
 | 
|  |    133 | //		test.Printf(_L("."));
 | 
|  |    134 | 		while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending)
 | 
|  |    135 | 			DELAY(20000);
 | 
|  |    136 | 		if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending)
 | 
|  |    137 | 			{
 | 
|  |    138 | 			// after reading first block, process the header
 | 
|  |    139 | 			ProcessHeader(z);
 | 
|  |    140 | 			c=User::NTickCount();
 | 
|  |    141 | 			}
 | 
|  |    142 | 		}
 | 
|  |    143 | 
 | 
|  |    144 | 	test.Next(_L("\nWait for thread to exit"));
 | 
|  |    145 | 	User::WaitForRequest(z.iThreadStatus);
 | 
|  |    146 | 	if (z.iRemain || t.ExitReason()!=KErrNone)
 | 
|  |    147 | 		{
 | 
|  |    148 | 		test.Printf(_L("Error %d\n"),t.ExitReason());
 | 
|  |    149 | 		test(0);
 | 
|  |    150 | 		}
 | 
|  |    151 | 	TUint c2=User::NTickCount();
 | 
|  |    152 | 	test.Printf(_L("Took %dms\n"),c2-c);
 | 
|  |    153 | 	TheInputFile.Close();
 | 
|  |    154 | 	test.Getch();
 | 
|  |    155 | 
 | 
|  |    156 | 	TInt unc_size=OutPtr-z.iOutBuf;
 | 
|  |    157 | 	test.Printf(_L("Recovered size %d\n"),unc_size);
 | 
|  |    158 | 	test.Printf(_L("Writing to file\n"));
 | 
|  |    159 | 	RFile file;
 | 
|  |    160 | 	r=file.Replace(fs,z.iName,EFileWrite);
 | 
|  |    161 | 	test(r==KErrNone);
 | 
|  |    162 | 	TPtrC8 ptr(z.iOutBuf,unc_size);
 | 
|  |    163 | 	r=file.Write(ptr);
 | 
|  |    164 | 	file.Close();
 | 
|  |    165 | 
 | 
|  |    166 | 	fs.Close();
 | 
|  |    167 | 	Cleanup(z);
 | 
|  |    168 | 
 | 
|  |    169 | 	return KErrNone;
 | 
|  |    170 | 	}
 | 
|  |    171 | 
 |