sl@0: /* sl@0: * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "fsmarshaller.h" sl@0: #include "fsdatatypes.h" sl@0: #include "NullStream.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // Generic externalization stuff /////////////////////////////////////////////// sl@0: sl@0: /** sl@0: * Determine how many bytes are taked up by the externalized representation of sl@0: * an object. sl@0: */ sl@0: template sl@0: TInt Size(const T& aObject) sl@0: { sl@0: RNullWriteStream stream; sl@0: TRAPD(err, stream << aObject); sl@0: if(err != KErrNone) sl@0: { sl@0: _LIT(KPanicCategory, "InValid Size"); sl@0: User::Panic(KPanicCategory,ESerialisationPanic); sl@0: } sl@0: stream.Close(); sl@0: return stream.BytesWritten(); sl@0: } sl@0: sl@0: /** sl@0: * Externalize an object to a buffer. Leaves if an error occurs. sl@0: */ sl@0: template sl@0: void WriteL(const T& aIn, TDes8& aOut) sl@0: { sl@0: RDesWriteStream stream(aOut); sl@0: stream << aIn; sl@0: stream.Close(); sl@0: } sl@0: sl@0: /** sl@0: * Externalize an object to a buffer. In debug mode, it will panics if an error sl@0: * occurs - eg buffer is too short. In release mode, errors are ignored. sl@0: */ sl@0: template sl@0: void Write(const T& aIn, TDes8& aOut) sl@0: { sl@0: TRAPD(err, WriteL(aIn, aOut)); sl@0: if(err != KErrNone) sl@0: { sl@0: _LIT(KPanicCategory, "Writing Error"); sl@0: User::Panic(KPanicCategory,ESerialisationPanic); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * Implement Externalization selector for RPointerArrays to use a function. sl@0: */ sl@0: template sl@0: EXTERNALIZE_FUNCTION(RPointerArray) sl@0: sl@0: /** sl@0: * Function to externalize RPointerArrays. sl@0: * sl@0: * Although this is generic, it's currently only instantiated for CKeyInfo. sl@0: */ sl@0: template sl@0: void ExternalizeL(const RPointerArray& aIn, RWriteStream& aOut) sl@0: { sl@0: TInt count = aIn.Count(); sl@0: aOut.WriteInt32L(count); sl@0: for (TInt i = 0 ; i < count ; ++i) sl@0: { sl@0: T* object = aIn[i]; sl@0: if (object == NULL) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: aOut << *object; sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * Implement Externalization selector for RArrays to use a function. sl@0: */ sl@0: template sl@0: EXTERNALIZE_FUNCTION(RArray) sl@0: sl@0: /** sl@0: * Function to externalize RArrays. sl@0: */ sl@0: template sl@0: void ExternalizeL(const RArray& aIn, RWriteStream& aOut) sl@0: { sl@0: TInt count = aIn.Count(); sl@0: aOut.WriteInt32L(count); sl@0: for (TInt i = 0 ; i < count ; ++i) sl@0: { sl@0: aOut << aIn[i]; sl@0: } sl@0: } sl@0: sl@0: // No-so-generic internalization stuff ///////////////////////////////////////// sl@0: sl@0: /** sl@0: * Internalize an object from a descriptor. sl@0: */ sl@0: template sl@0: void ReadL(const TDesC8& aIn, T& aOut) sl@0: { sl@0: RDesReadStream stream(aIn); sl@0: stream >> aOut; sl@0: stream.Close(); sl@0: } sl@0: sl@0: /** sl@0: * Implement Internalization selector for RArrays to use a function. sl@0: */ sl@0: template sl@0: inline Internalize::Function Internalization(const RArray*) sl@0: { sl@0: return Internalize::Function(); sl@0: } sl@0: sl@0: /** sl@0: * Function to internalize an RArray. sl@0: */ sl@0: template sl@0: void InternalizeL(RArray& aOut, RReadStream& aIn) sl@0: { sl@0: ASSERT(aOut.Count() == 0); sl@0: TInt count = aIn.ReadInt32L(); sl@0: for (TInt i = 0 ; i < count ; ++i) sl@0: { sl@0: T t; sl@0: aIn >> t; sl@0: User::LeaveIfError(aOut.Append(t)); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * Internalize an object from a stream. Creates an instance by calling the sl@0: * class' NewL(RReadStream&) method. sl@0: */ sl@0: template sl@0: inline void CreateL(RReadStream& aIn, T*& aOut) sl@0: { sl@0: aOut = T::NewL(aIn); sl@0: } sl@0: sl@0: /** sl@0: * Internalize an object from a descriptor. sl@0: */ sl@0: template sl@0: void CreateL(const TDesC8& aIn, T& aOut) sl@0: { sl@0: RDesReadStream stream(aIn); sl@0: CreateL(stream, aOut); sl@0: stream.Close(); sl@0: } sl@0: sl@0: /** sl@0: * Internalize an object from a descriptor leaving the result on the cleanup sl@0: * stack. This is generic, but is only instantiated for RInteger. sl@0: */ sl@0: template sl@0: void CreateLC(const TDesC8& aIn, T& aOut) sl@0: { sl@0: RDesReadStream stream(aIn); sl@0: CreateLC(stream, aOut); sl@0: stream.Close(); sl@0: } sl@0: sl@0: /** sl@0: * Internalize a cryptotokens object from a descriptor. sl@0: */ sl@0: template sl@0: void CreateL(const TDesC8& aIn, MCTToken& aToken, T& aOut) sl@0: { sl@0: RDesReadStream stream(aIn); sl@0: CreateL(stream, aToken, aOut); sl@0: stream.Close(); sl@0: } sl@0: sl@0: /** sl@0: * Internalize a cryptotoken object from a stream. Creates an instance by sl@0: * calling the class' NewL(RReadStream&, MCTToken&) method. sl@0: */ sl@0: template sl@0: inline void CreateL(RReadStream& aIn, MCTToken& aToken, T*& aOut) sl@0: { sl@0: aOut = T::NewL(aIn, aToken); sl@0: } sl@0: sl@0: /** sl@0: * Internalize an array of of key info objects from a stream. sl@0: */ sl@0: void CreateL(RReadStream& aIn, MCTToken& aToken, MKeyInfoArray& aOut) sl@0: { sl@0: TInt count = aIn.ReadInt32L(); sl@0: for (TInt i = 0 ; i < count ; ++i) sl@0: { sl@0: CCTKeyInfo* t; sl@0: CreateL(aIn, aToken, t); sl@0: CleanupReleasePushL(*t); sl@0: User::LeaveIfError(aOut.Append(t)); sl@0: CleanupStack::Pop(); sl@0: } sl@0: } sl@0: sl@0: // can we combine this with the one above? sl@0: sl@0: /** sl@0: * Internalize an RMPointerArray of cryptotoken objects from a stream. sl@0: */ sl@0: template sl@0: void CreateL(RReadStream& aIn, MCTToken& aToken, RMPointerArray& aOut) sl@0: { sl@0: TInt count = aIn.ReadInt32L(); sl@0: for (TInt i = 0 ; i < count ; ++i) sl@0: { sl@0: T* t; sl@0: CreateL(aIn, aToken, t); sl@0: CleanupReleasePushL(*t); sl@0: User::LeaveIfError(aOut.Append(t)); sl@0: CleanupStack::Pop(); sl@0: } sl@0: } sl@0: sl@0: // Serialization for RIntegers ///////////////////////////////////////////////// sl@0: sl@0: // This is exported as it is useful itself in the server sl@0: sl@0: /** sl@0: * Implement a CreateL function for internalizing HBufC8s. sl@0: */ sl@0: inline void CreateL(RReadStream& aIn, HBufC8*& aOut) sl@0: { sl@0: aOut = HBufC8::NewL(aIn, KMaxIntegerSize); sl@0: } sl@0: sl@0: /** sl@0: * Implement a CreateLC function for internalizing RIntegers. sl@0: */ sl@0: EXPORT_C void CreateLC(RReadStream& aIn, RInteger& aOut) sl@0: { sl@0: HBufC8* rBuf; sl@0: CreateL(aIn, rBuf); sl@0: CleanupStack::PushL(rBuf); sl@0: TPtr8 rPtr(rBuf->Des()); sl@0: aOut = RInteger::NewL(rPtr); sl@0: CleanupStack::PopAndDestroy(rBuf); sl@0: CleanupStack::PushL(aOut); sl@0: } sl@0: sl@0: /** sl@0: * Externalize an RInteger. sl@0: */ sl@0: EXPORT_C void ExternalizeL(const TInteger& aIn, RWriteStream& aOut) sl@0: { sl@0: HBufC8* sBuf = aIn.BufferLC(); sl@0: aOut << *sBuf; sl@0: CleanupStack::PopAndDestroy(sBuf); sl@0: } sl@0: sl@0: // Externalization for signature objects /////////////////////////////////////// sl@0: sl@0: /** sl@0: * Implement Externalization selector for CDSASignature to use a function. sl@0: */ sl@0: EXTERNALIZE_FUNCTION(CDSASignature) sl@0: sl@0: /** sl@0: * Externalise a DSA signature object. sl@0: */ sl@0: void ExternalizeL(const CDSASignature& aIn, RWriteStream& aOut) sl@0: { sl@0: aOut << aIn.R(); sl@0: aOut << aIn.S(); sl@0: } sl@0: sl@0: /** sl@0: * Implement Externalization selector for CRSASignature to use a function. sl@0: */ sl@0: EXTERNALIZE_FUNCTION(CRSASignature) sl@0: sl@0: /** sl@0: * Externalize an RSA signature object. sl@0: */ sl@0: void ExternalizeL(const CRSASignature& aIn, RWriteStream& aOut) sl@0: { sl@0: aOut << aIn.S(); sl@0: } sl@0: sl@0: // Internalization for signature objects /////////////////////////////////////// sl@0: sl@0: /** sl@0: * Specialise CreateL for CRSASignature. sl@0: */ sl@0: template <> sl@0: void CreateL(RReadStream& aIn, CRSASignature*& aOut) sl@0: { sl@0: RInteger r; sl@0: CreateLC(aIn, r); sl@0: aOut = CRSASignature::NewL(r); sl@0: CleanupStack::Pop(); // r sl@0: } sl@0: sl@0: /** sl@0: * Specialise CreateL for CDSASignature. sl@0: */ sl@0: template <> sl@0: void CreateL(RReadStream& aIn, CDSASignature*& aOut) sl@0: { sl@0: RInteger r; sl@0: CreateLC(aIn, r); sl@0: RInteger s; sl@0: CreateLC(aIn, s); sl@0: aOut = CDSASignature::NewL(r, s); sl@0: CleanupStack::Pop(2); // s, r sl@0: } sl@0: sl@0: // Serialization for DH ojects ///////////////////////////////////////////////// sl@0: sl@0: /** sl@0: * Implement Externalization selector for CDHParams to use a function. sl@0: */ sl@0: EXTERNALIZE_FUNCTION(CDHParams) sl@0: sl@0: /** sl@0: * Externalise a TDHParams object. sl@0: */ sl@0: void ExternalizeL(const CDHParams& aIn, RWriteStream& aOut) sl@0: { sl@0: aOut << aIn.N(); sl@0: aOut << aIn.G(); sl@0: } sl@0: sl@0: /** sl@0: * Specialise CreateL for CDHParams. sl@0: */ sl@0: template <> sl@0: void CreateL(RReadStream& aIn, CDHParams*& aOut) sl@0: { sl@0: RInteger n; sl@0: CreateLC(aIn, n); sl@0: RInteger g; sl@0: CreateLC(aIn, g); sl@0: aOut = CDHParams::NewL(n, g); sl@0: CleanupStack::PopAndDestroy(2, &n); // g, n sl@0: } sl@0: sl@0: /** sl@0: * Implement Externalization selector for CDHPublicKey to use a function. sl@0: */ sl@0: EXTERNALIZE_FUNCTION(CDHPublicKey) sl@0: sl@0: /** sl@0: * Externalise a CDHPublicKey object. sl@0: */ sl@0: void ExternalizeL(const CDHPublicKey& aIn, RWriteStream& aOut) sl@0: { sl@0: aOut << aIn.N(); sl@0: aOut << aIn.G(); sl@0: aOut << aIn.X(); sl@0: } sl@0: sl@0: /** sl@0: * Specialise CreateL for CDHPublicKey. sl@0: */ sl@0: template <> sl@0: void CreateL(RReadStream& aIn, CDHPublicKey*& aOut) sl@0: { sl@0: RInteger n; sl@0: CreateLC(aIn, n); sl@0: RInteger g; sl@0: CreateLC(aIn, g); sl@0: RInteger X; sl@0: CreateLC(aIn, X); sl@0: aOut = CDHPublicKey::NewL(n, g, X); sl@0: CleanupStack::Pop(3); // X, g, n sl@0: } sl@0: sl@0: // Common ////////////////////////////////////////////////////////////////////// sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, RArray& aOut) sl@0: { sl@0: ::ReadL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const CKeyInfoBase& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::Write(const CKeyInfoBase& aIn, TDes8& aOut) sl@0: { sl@0: ::Write(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const RArray& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::Write(const RArray& aIn, TDes8& aOut) sl@0: { sl@0: ::Write(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const MCertInfo& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::Write(const MCertInfo& aIn, TDes8& aOut) sl@0: { sl@0: ::Write(aIn, aOut); sl@0: } sl@0: sl@0: // Client ////////////////////////////////////////////////////////////////////// sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, MCTToken& aToken, MKeyInfoArray& aOut) sl@0: { sl@0: ::CreateL(aIn, aToken, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, MCTToken& aToken, CCTKeyInfo*& aOut) sl@0: { sl@0: ::CreateL(aIn, aToken, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CDSASignature*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CRSASignature*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const CDHParams& aIn) sl@0: { sl@0: return Size(aIn.N()) + Size(aIn.G()); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::WriteL(const CDHParams& aIn, TDes8& aOut) sl@0: { sl@0: ::WriteL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const CDHPublicKey& aIn) sl@0: { sl@0: return Size(aIn.N()) + Size(aIn.G()) + Size(aIn.X()); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::WriteL(const CDHPublicKey& aIn, TDes8& aOut) sl@0: { sl@0: ::WriteL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, RInteger& aInteger) sl@0: { sl@0: ::CreateLC(aIn, aInteger); sl@0: CleanupStack::Pop(); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const CPBEncryptParms& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::Write(const CPBEncryptParms& aIn, TDes8& aOut) sl@0: { sl@0: ::Write(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, MCTToken& aToken, RMPointerArray& aOut) sl@0: { sl@0: ::CreateL(aIn, aToken, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, MCTToken& aToken, CCTCertInfo*& aOut) sl@0: { sl@0: ::CreateL(aIn, aToken, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, RArray& aOut) sl@0: { sl@0: ::ReadL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const CCertAttributeFilter& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::WriteL(const CCertAttributeFilter& aIn, TDes8& aOut) sl@0: { sl@0: ::WriteL(aIn, aOut); sl@0: } sl@0: sl@0: // Server ////////////////////////////////////////////////////////////////////// sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const RPointerArray& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CPBEncryptParms*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::Write(const RPointerArray& aIn, TDes8& aOut) sl@0: { sl@0: ::Write(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CKeyInfo*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: } sl@0: sl@0: /** sl@0: * Determine the size of an externalized big integer. This assumes that the sl@0: * size of the buffer returned by TInteger::BufferLC() is the same as the size sl@0: * of the integer as returned by TInteger::ByteCount(). This is done to avoid sl@0: * allocating the buffer just so we can tell how big it is. sl@0: */ sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const TInteger& aIn) sl@0: { sl@0: // This is an overestimate as the length is encoded with a TCardinality sl@0: return sizeof(TInt32) + aIn.ByteCount(); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const CDSASignature& aIn) sl@0: { sl@0: return Size(aIn.R()) + Size(aIn.S()); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::WriteL(const CDSASignature& aIn, TDes8& aOut) sl@0: { sl@0: ::WriteL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const CRSASignature& aIn) sl@0: { sl@0: return Size(aIn.S()); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::WriteL(const CRSASignature& aIn, TDes8& aOut) sl@0: { sl@0: ::WriteL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CDHParams*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CDHPublicKey*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::WriteL(const TInteger& aIn, TDes8& aOut) sl@0: { sl@0: ::WriteL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CCertInfo*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const RPointerArray& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::Write(const RPointerArray& aIn, TDes8& aOut) sl@0: { sl@0: ::Write(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C TInt TokenDataMarshaller::Size(const RArray& aIn) sl@0: { sl@0: return ::Size(aIn); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::Write(const RArray& aIn, TDes8& aOut) sl@0: { sl@0: ::Write(aIn, aOut); sl@0: } sl@0: sl@0: EXPORT_C void TokenDataMarshaller::ReadL(const TDesC8& aIn, CCertAttributeFilter*& aOut) sl@0: { sl@0: ::CreateL(aIn, aOut); sl@0: }