installationservices/swi/source/sisfile/sisstring.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Definition of the Swi::Sis::CString
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file sisstring.cpp
       
    22 */
       
    23 
       
    24 #include <e32def.h>
       
    25 #include "sisstring.h"
       
    26 #include "sisfieldtypes.h"
       
    27 #include "sisdataprovider.h"
       
    28 #include "sisinstallerrors.h"
       
    29 
       
    30 using namespace Swi::Sis;
       
    31 
       
    32 EXPORT_C /*static*/ CString* CString::NewLC(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    33 	{
       
    34 	CString* self = new(ELeave) CString();
       
    35 	CleanupStack::PushL(self);
       
    36 	self->ConstructL(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 EXPORT_C /*static*/ CString* CString::NewL(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    41 	{
       
    42 	CString* self = NewLC(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
    43 	CleanupStack::Pop(self);
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 EXPORT_C CString* CString::NewL(TPtrProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    48 	{
       
    49 	CString* self = CString::NewLC(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
    50 	CleanupStack::Pop(self);
       
    51 	return self;	
       
    52 	}
       
    53 
       
    54 EXPORT_C CString* CString::NewLC(TPtrProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    55 	{
       
    56 	CString* self = new (ELeave) CString;
       
    57 	CleanupStack::PushL(self);
       
    58 	self->ConstructL(aDataProvider, aBytesRead, aTypeReadBehaviour);
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 CString::CString()
       
    63 	{
       
    64 	}
       
    65 
       
    66 EXPORT_C CString::~CString()
       
    67 	{
       
    68 	delete iData;
       
    69 	}
       
    70 
       
    71 void CString::ConstructL(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
    72 	{
       
    73 	TInt64 fieldOffset = aBytesRead;
       
    74 	
       
    75 	CField::ConstructL(aDataProvider, EFieldTypeString, aBytesRead, aTypeReadBehaviour);
       
    76 
       
    77 	// If string has an Odd Length, something is wrong.
       
    78 	if (I64LOW(Length()) & 0x1)
       
    79 		{
       
    80 		User::Leave(KErrSISInvalidStringLength);
       
    81 		}
       
    82 	
       
    83 	if (I64HIGH(Length()))
       
    84 		{
       
    85 		// Length is either -ve or to big for a string
       
    86 		User::Leave(KErrSISStringInvalidLength);
       
    87 		}
       
    88 	TInt32 stringLength=I64LOW(Length()) / 2;
       
    89 	iData= HBufC::NewL(stringLength);
       
    90 	
       
    91 	TPtr8 p(const_cast<TUint8*>(reinterpret_cast<const TUint8*>(iData->Des().Ptr())) , stringLength*2);
       
    92 	p.SetLength(stringLength*2);
       
    93 	CField::CheckedReadL(aDataProvider, p, stringLength*2, aBytesRead);
       
    94 	iData->Des().SetLength(stringLength);
       
    95 	iDataPtr.Set(*iData);
       
    96 	CField::EnsureAlignedL(aDataProvider, aBytesRead - fieldOffset, aBytesRead, aTypeReadBehaviour);
       
    97 	}
       
    98 	
       
    99 	
       
   100 void CString::ConstructL(TPtrProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour)
       
   101 	{
       
   102 	TInt64 fieldOffset = aBytesRead;
       
   103 	CField::ConstructL(aDataProvider, EFieldTypeString, aBytesRead, aTypeReadBehaviour);
       
   104 
       
   105 	// If string has an Odd Length, something is wrong.
       
   106 	if (I64LOW(Length()) & 0x1)
       
   107 		{
       
   108 		User::Leave(KErrSISInvalidStringLength);
       
   109 		}
       
   110 	
       
   111 	if (I64HIGH(Length()))
       
   112 		{
       
   113 		// Length is either -ve or to big for a string
       
   114 		User::Leave(KErrSISStringInvalidLength);
       
   115 		}
       
   116 	TInt32 stringLength=I64LOW(Length());
       
   117 	
       
   118 	// get a pointer to the underlying data.
       
   119 	const TUint8* ptr = aDataProvider.ReadL(stringLength).Ptr();
       
   120 	aBytesRead += stringLength;
       
   121 	
       
   122 	// cast it to a TUint16 and construct the pointer from it.
       
   123 	stringLength /= 2;
       
   124 	const TUint16* uPtr = reinterpret_cast<const TUint16*>(ptr);
       
   125 	iDataPtr.Set(uPtr, stringLength);
       
   126 	
       
   127 	CField::EnsureAlignedL(aDataProvider, aBytesRead - fieldOffset, aBytesRead, aTypeReadBehaviour);
       
   128 	}