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