Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * (c) 1999-2003 Symbian Ltd
29 #include "randcliserv.h"
31 _LIT(KRandomServerImg,"z:\\system\\libs\\randsvr.exe"); // DLL/EXE name
32 _LIT(KRandomServerConnect, "Randsvr connect");
33 _LIT(KRandomServerGet, "Randsvr get");
35 const TUid KServerUid3={0x100066dc};
38 EXPORT_C void RAND_bytes(unsigned char* buf,int bytes)
40 TPtr8 ptr(buf,bytes,bytes);
46 EXPORT_C CRandom::CRandom(void)
50 EXPORT_C CSystemRandom* CSystemRandom::NewL(void)
52 CSystemRandom* self = new(ELeave)CSystemRandom();
56 EXPORT_C CSystemRandom* CSystemRandom::NewLC(void)
58 CSystemRandom* self = NewL();
59 CleanupStack::PushL(self);
63 void CSystemRandom::GenerateBytesL(TDes8& aDest)
65 TRandom::RandomL(aDest);
68 CSystemRandom::CSystemRandom(void)
72 EXPORT_C void TRandom::Random(TDes8& aDestination)
75 TRAPD(ret,rs.ConnectL());
78 User::Panic(KRandomServerConnect, ret);
80 TInt err=rs.GetRandom(aDestination);
83 User::Panic(KRandomServerGet, err);
88 EXPORT_C void TRandom::RandomL(TDes8& aDestination)
91 TRAPD(ret,rs.ConnectL());
92 User::LeaveIfError(ret);
93 CleanupClosePushL(rs);
95 TInt err=rs.GetRandom(aDestination);
96 User::LeaveIfError(err);
98 CleanupStack::PopAndDestroy(); // rs
101 EXPORT_C RRandomSession::RRandomSession(void)
105 static TInt StartServer()
106 // Borrowed from AndrewT's server startup code.
107 // Start the server process/thread which lives in an EPOCEXE object
111 const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
114 // EPOC and EKA2 is easy, we just create a new server process. Simultaneous
115 // launching of two such processes should be detected when the second one
116 // attempts to create the server object, failing with KErrAlreadyExists.
119 TInt r=server.Create(KRandomServerImg, KNullDesC, serverUid);
124 server.Rendezvous(stat);
125 if (stat!=KRequestPending)
126 server.Kill(0); // abort startup
128 server.Resume(); // logon OK - start the server
129 User::WaitForRequest(stat); // wait for start or death
130 // we can't use the 'exit reason' if the server panicked as this
131 // is the panic 'reason' and may be '0' which cannot be distinguished
133 r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
139 EXPORT_C void RRandomSession::ConnectL(void)
144 TInt r=CreateSession(KRandomServerName,TVersion(0,0,0),1);
145 if (r!=KErrNotFound && r!=KErrServerTerminated)
150 if (r!=KErrNone && r!=KErrAlreadyExists)
155 EXPORT_C TInt RRandomSession::GetRandom(TDes8& aDestination)
157 if (aDestination.Length()<KRandomBlockSize)
159 return SendReceive(CRandomSession::KRandomRequest,
160 TIpcArgs(&aDestination, aDestination.Length()));
166 TInt length=aDestination.Length();
167 for (i=0;(i+KRandomBlockSize)<length;i+=KRandomBlockSize)
169 TPtr8 buffer(&aDestination[i],KRandomBlockSize,KRandomBlockSize);
170 err=SendReceive(CRandomSession::KRandomRequest,
171 TIpcArgs(&buffer, KRandomBlockSize));
177 TPtr8 buffer(&aDestination[i],length%KRandomBlockSize,KRandomBlockSize);
178 err=SendReceive(CRandomSession::KRandomRequest,
179 TIpcArgs(&buffer, length-i));