os/kernelhwsrv/brdbootldr/ubootldr/unzip.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1996-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 // ubootldr\unzip.cpp
    15 // 
    16 //
    17 
    18 #define FILE_ID	0x555A4950
    19 
    20 #include "bootldr.h"
    21 #include "unzip.h"
    22 #include "inflate.h"
    23 
    24 const TInt    INBUFSIZE           = 0x2000;
    25 const TUint32 RETRY_WARNING_COUNT = 100;	// if we get 100 retries, things must be really bad...
    26 
    27 TZipInfo* TheZipInfo;
    28 
    29 #define Z (*TheZipInfo)
    30 
    31 extern "C" {
    32 
    33 extern int inflate();
    34 
    35 TUint8 inbuf[INBUFSIZE];
    36 TUint8* volatile inptr;		/* index of next byte to be processed in inbuf */
    37 TUint8* volatile inbuf_end;	/* pointer to last valid input byte + 1 */
    38 TUint8* volatile outptr;	/* pointer to output data */
    39 
    40 TAny* malloc(TUint aSize)
    41 	{
    42 	return MALLOC((TInt)aSize);
    43 	}
    44 
    45 void free(TAny* aPtr)
    46 	{
    47 	FREE(aPtr);
    48 	}
    49 
    50 TUint8 fill_inbuf()
    51 	{
    52 	WAIT_FOR_ANY_REQUEST();	// wait for a block from the file
    53 	TUint w=Z.iFileBufW;
    54 	TInt avail=(TInt)w-(TInt)Z.iFileBufR;
    55 	TInt amount=(avail>(TInt)INBUFSIZE)?INBUFSIZE:avail;
    56 	TInt rix=(TInt)(Z.iFileBufR & (Z.iFileBufSize-1));
    57 	memcpy(inbuf,Z.iFileBuf+rix,amount);
    58 	Z.iFileBufR+=amount;
    59 	inptr=inbuf;
    60 	inbuf_end=inbuf+amount;
    61 	return *inptr++;
    62 	}
    63 
    64 void process_block(int error)
    65 	{
    66 	AcceptUnzippedBlock(Z, (TUint8*&)outptr, error);
    67 	}
    68 } // extern "C" {
    69 
    70 const TUint KZipSpan=0x30304b50u;
    71 TInt ParseZipHeader(TZipInfo& a)
    72 	{
    73 	TInt avail=inbuf_end-inptr;
    74 	if (avail<KZipLocalHeaderLen)
    75 		return KErrCorrupt;
    76 	a.iDataOffset=30;
    77 	TUint sig=*(TUint*)inptr;		// OK since at beginning of buffer
    78 	if (sig==KZipSpan)
    79 		{
    80 		PrintToScreen(_L("ZIP: Split archive\r\n"));
    81 		inptr+=4;
    82 		a.iDataOffset+=4;
    83 		}
    84 
    85 	sig=*(TUint*)inptr;		// OK since at beginning of buffer
    86 	inptr+=6;
    87 	if (sig!=KZipSignature)
    88 		return KErrCorrupt;
    89 	a.iFlags=*inptr++;
    90 	++inptr;
    91 	a.iMethod=*inptr++;
    92 	inptr+=5;
    93 	memcpy(&a.iCrc,inptr,12);		// crc, comp size, uncomp size
    94 	inptr+=12;
    95 	a.iFileNameLength=*inptr | (inptr[1]<<8);
    96 	inptr+=2;
    97 	a.iExtraLength=*inptr | (inptr[1]<<8);
    98 	inptr+=2;
    99 	if (a.iFlags & (CRPFLG|EXTFLG))
   100 		return KErrNotSupported;
   101 	if (a.iMethod!=STORED && a.iMethod!=DEFLATED)
   102 		return KErrNotSupported;
   103 	if (avail<KZipLocalHeaderLen+a.iFileNameLength+a.iExtraLength)
   104 		return KErrCorrupt;
   105 	a.iNameOffset=30;
   106 	a.iDataOffset+=a.iFileNameLength+a.iExtraLength;
   107 	TInt fnamelen=Min(a.iFileNameLength,a.iName.MaxLength());
   108 	TPtrC8 fileNamePtr(inptr,fnamelen);
   109 	a.iName.Copy(fileNamePtr);
   110 	return KErrNone;
   111 	}
   112 
   113 TInt UnzipThread(TAny* aInfo)
   114 	{
   115 	TheZipInfo=(TZipInfo*)aInfo;
   116 	Z.iProcessedHeader=KRequestPending;
   117 	inptr=inbuf;
   118 	inbuf_end=inbuf;
   119 	fill_inbuf();
   120 	inptr=inbuf;
   121 	TInt r=ParseZipHeader(Z);
   122 	if (r!=KErrNone)
   123 		return r;
   124 	inptr=inbuf+Z.iDataOffset;
   125 	Z.iHeaderDone=1;
   126 	WAIT_FOR_REQUEST(Z.iProcessedHeader);
   127 	outptr=Z.iOutBuf;
   128 	r=inflate();
   129 	r=UnzipComplete(Z, outptr, r);
   130 	return r;
   131 	}
   132 
   133 TInt InitInfo(TZipInfo& a)
   134 	{
   135 	a.iInBufSize=INBUFSIZE;
   136 	a.iFileBufR=0;
   137 	a.iFileBufW=0;
   138 	a.iFileBuf=NULL;
   139 	a.iProcessedHeader=KRequestPending;
   140 	a.iHeaderDone=0;
   141 	a.iOutBuf=NULL;
   142 	a.iThreadHandle=0;
   143 	a.iThreadStatus=KRequestPending;
   144 	return KErrNone;
   145 	}
   146 
   147 TInt ReadBlockToBuffer(TZipInfo& a, RFile &aBootFile)
   148 	{
   149 	TInt numAttempts = 0;
   150 
   151 	// If the buffer is full, wait for it to empty before continuing
   152 	// There is no point exiting after a number of retries because it will the fail later on
   153 	// So, loop forever if the buffer doesn't empty, but print a warning after the retry count has passed
   154 	while (a.iFileBufW-a.iFileBufR==a.iFileBufSize)
   155 		{
   156 		numAttempts++;
   157 		if (numAttempts % RETRY_WARNING_COUNT == 0)
   158 			{
   159 			PrintToScreen(_L("ZIP: ReadBlockToBuffer stuck waiting for buffer to empty (retry #%d)\r\n"), numAttempts);
   160 			}
   161 		DELAY(20000);		// buffer full so wait a bit
   162 		}
   163 
   164 	TInt req_len=Min(a.iRemain,INBUFSIZE);
   165 	TInt len=req_len;
   166 	TInt wix=(TInt)(a.iFileBufW & (a.iFileBufSize-1));
   167 	TInt r;
   168 	
   169 	r = ReadInputData(a.iFileBuf+wix,len);
   170 
   171 	if (len>req_len)
   172 		len=req_len;
   173 	if (r==KErrNone)
   174 		{
   175 		ImageReadProgress+=len;
   176 		a.iFileBufW+=len;
   177 		a.iRemain-=len;
   178 		}
   179 	return r;
   180 	}
   181