networksecurity/tlsprovider/Test/tlstest2/testpadding.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2006-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 "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 //
       
    15 
       
    16 /**
       
    17  @file testpadding.cpp
       
    18  @internalTechnology
       
    19 */
       
    20 #include "testpadding.h"
       
    21 
       
    22 #include <securityerr.h>
       
    23 
       
    24 CTlsTestPadding* CTlsTestPadding::NewLC(TInt aBlockBytes, TInt aPaddingMod)
       
    25 	{
       
    26 	return new (ELeave) CTlsTestPadding(aBlockBytes, aPaddingMod);
       
    27 	}
       
    28 	
       
    29 void CTlsTestPadding::DoPadL(const TDesC8& aInput,TDes8& aOutput)
       
    30 	{
       
    31 	TInt paddingBytes=BlockSize()-(aInput.Length()%BlockSize());
       
    32 	aOutput.Append(aInput);
       
    33 	aOutput.SetLength(aOutput.Length()+paddingBytes);
       
    34 	for (TInt i=1;i<=paddingBytes;i++)
       
    35 		{
       
    36 		// apply the padding modifier to every byte in the padding
       
    37 		aOutput[aOutput.Length()-i]=(TUint8)(paddingBytes+iPaddingMod-1);
       
    38 		}
       
    39 	}
       
    40 	
       
    41 void CTlsTestPadding::UnPadL(const TDesC8& aInput,TDes8& aOutput)
       
    42 	{
       
    43 	TInt paddingLen = aInput[aInput.Length()-1] + 1;
       
    44 
       
    45 	if (paddingLen > aInput.Length())
       
    46 		{
       
    47 		User::Leave(KErrInvalidPadding);
       
    48 		}
       
    49 	
       
    50 	// check the whole length of the padding is the same byte
       
    51 	TPtrC8 padding = aInput.Right(paddingLen);
       
    52 	for (TInt i = 0; i < paddingLen; ++i)
       
    53 		{
       
    54 		if (padding[i] != paddingLen)
       
    55 			{
       
    56 			User::Leave(KErrInvalidPadding);
       
    57 			}
       
    58 		}	
       
    59 
       
    60 	TInt outlen = aInput.Length() - paddingLen;
       
    61 	aOutput.Append(aInput.Left(outlen));
       
    62 	}
       
    63 	
       
    64 TInt CTlsTestPadding::MinPaddingLength(void) const
       
    65 	{
       
    66 	return 1;
       
    67 	}
       
    68 
       
    69 TInt CTlsTestPadding::MaxPaddedLength(TInt aInputBytes) const
       
    70 	{
       
    71 	TUint padBytes = BlockSize() - (aInputBytes % BlockSize());
       
    72 	return padBytes + aInputBytes;
       
    73 	}
       
    74 	
       
    75 CTlsTestPadding::CTlsTestPadding(TInt aBlockBytes, TInt aPaddingMod)
       
    76 	: CPadding(aBlockBytes), iPaddingMod(aPaddingMod)
       
    77 	{
       
    78 	}