os/kernelhwsrv/kerneltest/e32test/misc/t_unzip.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/misc/t_unzip.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,171 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\misc\t_unzip.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <e32test.h>
    1.22 +#include <f32file.h>
    1.23 +#include "unzip.h"
    1.24 +
    1.25 +RTest test(_L("T_UNZIP"));
    1.26 +
    1.27 +RFile TheInputFile;
    1.28 +TUint8* OutPtr;
    1.29 +
    1.30 +GLDEF_C void AcceptUnzippedBlock(TZipInfo& /*aInfo*/, TUint8*& aOutPtr, TInt aError)
    1.31 +	{
    1.32 +	if (aError==KErrNone)
    1.33 +		OutPtr=aOutPtr;
    1.34 +	}
    1.35 +
    1.36 +GLDEF_C TInt ReadInputData(TUint8* aDest, TInt& aLength)
    1.37 +	{
    1.38 +	TPtr8 ptr(aDest,0,aLength);
    1.39 +	return TheInputFile.Read(ptr);
    1.40 +	}
    1.41 +
    1.42 +GLDEF_C TInt UnzipComplete(TZipInfo& /*a*/, TUint8* /*aOutPtr*/, TInt /*aError*/)
    1.43 +	{
    1.44 +	return KErrNone;
    1.45 +	}
    1.46 +
    1.47 +_LIT(KLitThreadName,"Unzip");
    1.48 +TInt Initialise(TZipInfo& a)
    1.49 +	{
    1.50 +	TInt r=InitInfo(a);
    1.51 +	if (r!=KErrNone)
    1.52 +		return r;
    1.53 +	a.iFileBufSize=4*a.iInBufSize;
    1.54 +	TAny* pFileBuf=MALLOC(a.iFileBufSize);
    1.55 +	if (!pFileBuf)
    1.56 +		return KErrNoMemory;
    1.57 +	a.iFileBuf=(TUint8*)pFileBuf;
    1.58 +	RThread t;
    1.59 +	r=t.Create(KLitThreadName,UnzipThread,0x2000,NULL,&a);
    1.60 +	if (r!=KErrNone)
    1.61 +		{
    1.62 +		FREE(pFileBuf);
    1.63 +		a.iFileBuf=NULL;
    1.64 +		return r;
    1.65 +		}
    1.66 +	t.SetPriority(EPriorityLess);
    1.67 +	t.Logon(a.iThreadStatus);
    1.68 +	t.Resume();
    1.69 +	a.iThreadHandle=t.Handle();
    1.70 +	return KErrNone;
    1.71 +	}
    1.72 +
    1.73 +void ProcessHeader(TZipInfo& a)
    1.74 +	{
    1.75 +	test.Printf(_L("Flags=%d\n"),a.iFlags);
    1.76 +	test.Printf(_L("Method=%d\n"),a.iMethod);
    1.77 +	test.Printf(_L("Crc=%d\n"),a.iCrc);
    1.78 +	test.Printf(_L("Compressed size=%d\n"),a.iCompressedSize);
    1.79 +	test.Printf(_L("Uncompressed size=%d\n"),a.iUncompressedSize);
    1.80 +	test.Printf(_L("File name %S\n"),&a.iName);
    1.81 +	test.Printf(_L("Data offset %d\n\n"),a.iDataOffset);
    1.82 +
    1.83 +	test.Next(_L("Allocate memory for unzipped file"));
    1.84 +	a.iOutBuf=(TUint8*)User::Alloc(a.iUncompressedSize);
    1.85 +	test(a.iOutBuf!=NULL);
    1.86 +	test.Next(_L("Begin unzipping"));
    1.87 +	a.iHeaderDone=2;
    1.88 +	TRequestStatus* pS=&a.iProcessedHeader;
    1.89 +	RThread t;
    1.90 +	t.SetHandle(a.iThreadHandle);
    1.91 +	t.RequestComplete(pS,0);
    1.92 +	}
    1.93 +
    1.94 +void Cleanup(TZipInfo& a)
    1.95 +	{
    1.96 +	delete a.iFileBuf;
    1.97 +	a.iFileBuf=NULL;
    1.98 +	delete a.iOutBuf;
    1.99 +	a.iOutBuf=NULL;
   1.100 +	RThread& t=*(RThread*)&a.iThreadHandle;
   1.101 +	t.Close();
   1.102 +	}
   1.103 +
   1.104 +GLDEF_C TInt E32Main()
   1.105 +	{
   1.106 +	test.Title();
   1.107 +	TFileName inputFileName;
   1.108 +	User::CommandLine(inputFileName);
   1.109 +	test.Start(_L("Connect to file server"));
   1.110 +	RFs fs;
   1.111 +	TInt r=fs.Connect();
   1.112 +	test(r==KErrNone);
   1.113 +	test.Printf(_L("Open file %S\n"),&inputFileName);
   1.114 +	r=TheInputFile.Open(fs,inputFileName,EFileRead);
   1.115 +	test(r==KErrNone);
   1.116 +	TZipInfo z;
   1.117 +	r=TheInputFile.Size(z.iRemain);
   1.118 +	test(r==KErrNone);
   1.119 +	test.Printf(_L("File size %d\n"),z.iRemain);
   1.120 +
   1.121 +	test.Next(_L("Initialise"));
   1.122 +	r=Initialise(z);
   1.123 +	test(r==KErrNone);
   1.124 +
   1.125 +	test.Next(_L("Read header"));
   1.126 +	TUint32 c=0;
   1.127 +	RThread t;
   1.128 +	t.SetHandle(z.iThreadHandle);
   1.129 +	while (z.iRemain && z.iThreadStatus==KRequestPending)
   1.130 +		{
   1.131 +		TRequestStatus dummy;
   1.132 +		TRequestStatus* pS=&dummy;
   1.133 +		r=ReadBlockToBuffer(z);
   1.134 +		test(r==KErrNone);
   1.135 +		t.RequestComplete(pS,0);		// same process
   1.136 +//		test.Printf(_L("."));
   1.137 +		while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending)
   1.138 +			DELAY(20000);
   1.139 +		if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending)
   1.140 +			{
   1.141 +			// after reading first block, process the header
   1.142 +			ProcessHeader(z);
   1.143 +			c=User::NTickCount();
   1.144 +			}
   1.145 +		}
   1.146 +
   1.147 +	test.Next(_L("\nWait for thread to exit"));
   1.148 +	User::WaitForRequest(z.iThreadStatus);
   1.149 +	if (z.iRemain || t.ExitReason()!=KErrNone)
   1.150 +		{
   1.151 +		test.Printf(_L("Error %d\n"),t.ExitReason());
   1.152 +		test(0);
   1.153 +		}
   1.154 +	TUint c2=User::NTickCount();
   1.155 +	test.Printf(_L("Took %dms\n"),c2-c);
   1.156 +	TheInputFile.Close();
   1.157 +	test.Getch();
   1.158 +
   1.159 +	TInt unc_size=OutPtr-z.iOutBuf;
   1.160 +	test.Printf(_L("Recovered size %d\n"),unc_size);
   1.161 +	test.Printf(_L("Writing to file\n"));
   1.162 +	RFile file;
   1.163 +	r=file.Replace(fs,z.iName,EFileWrite);
   1.164 +	test(r==KErrNone);
   1.165 +	TPtrC8 ptr(z.iOutBuf,unc_size);
   1.166 +	r=file.Write(ptr);
   1.167 +	file.Close();
   1.168 +
   1.169 +	fs.Close();
   1.170 +	Cleanup(z);
   1.171 +
   1.172 +	return KErrNone;
   1.173 +	}
   1.174 +