1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/contentmgmt/contentaccessfwfordrm/source/cafutils/cafmimeheader.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,243 @@
1.4 +/*
1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#ifndef REMOVE_CAF1
1.23 +
1.24 +#include <caf/caftypes.h>
1.25 +#include <s32strm.h>
1.26 +#include <caf/cafmimeheader.h>
1.27 +#include "mimefieldanddata.h"
1.28 +
1.29 +using namespace ContentAccess;
1.30 +
1.31 +EXPORT_C CCafMimeHeader* CCafMimeHeader::NewL(const TDesC8 &aContentType)
1.32 + {
1.33 + CCafMimeHeader *self = new (ELeave) CCafMimeHeader();
1.34 + CleanupStack::PushL(self);
1.35 + self->ConstructL(aContentType);
1.36 + CleanupStack::Pop(self);
1.37 + return self;
1.38 + }
1.39 +
1.40 +CCafMimeHeader::CCafMimeHeader()
1.41 + {
1.42 + }
1.43 +
1.44 +void CCafMimeHeader::ConstructL(const TDesC8 &aContentType)
1.45 + {
1.46 + SetStandardMimeDataL(EContentType, aContentType);
1.47 + }
1.48 +
1.49 +EXPORT_C CCafMimeHeader::~CCafMimeHeader()
1.50 + {
1.51 + TInt i=0;
1.52 + iNonStandardMimeHeaders.ResetAndDestroy();
1.53 + iNonStandardMimeHeaders.Close();
1.54 +
1.55 + for(i = 0; i < EMimeMax; i++)
1.56 + {
1.57 + delete iStandardMimeHeader[i];
1.58 + }
1.59 + }
1.60 +
1.61 +EXPORT_C void CCafMimeHeader::SetStandardMimeDataL(const TMimeFields &aIndex, const TDesC8& aData)
1.62 + {
1.63 + HBufC8 *data = aData.AllocLC();
1.64 +
1.65 + // make sure mime types are lower case
1.66 + if(aIndex == EContentType)
1.67 + {
1.68 + data->Des().LowerCase();
1.69 + }
1.70 +
1.71 + // replace whatever value was there before
1.72 + delete iStandardMimeHeader[aIndex];
1.73 + iStandardMimeHeader[aIndex] = data;
1.74 +
1.75 + CleanupStack::Pop(data);
1.76 + }
1.77 +
1.78 +EXPORT_C TPtrC8 CCafMimeHeader::StandardMimeData(const TMimeFields &aIndex) const
1.79 + {
1.80 + // If the field value has not been set, return a null string
1.81 + if(iStandardMimeHeader[aIndex] == NULL)
1.82 + {
1.83 + return KNullDesC8();
1.84 + }
1.85 + return TPtrC8(iStandardMimeHeader[aIndex]->Des());
1.86 + }
1.87 +
1.88 +
1.89 +EXPORT_C TInt CCafMimeHeader::NonStandardMimeCount() const
1.90 + {
1.91 + return iNonStandardMimeHeaders.Count();
1.92 + }
1.93 +
1.94 +EXPORT_C void CCafMimeHeader::AddNonStandardMimeL(const TDesC8& aFieldName, const TDesC8& aData)
1.95 + {
1.96 + CMimeFieldAndData *mimeData = CMimeFieldAndData::NewL(aFieldName, aData);
1.97 + CleanupStack::PushL(mimeData);
1.98 + iNonStandardMimeHeaders.Append(mimeData);
1.99 + CleanupStack::Pop(mimeData);
1.100 + }
1.101 +
1.102 +EXPORT_C TPtrC8 CCafMimeHeader::NonStandardMimeField(TInt aIndex) const
1.103 + {
1.104 + return iNonStandardMimeHeaders[aIndex]->FieldName();
1.105 + }
1.106 +
1.107 +EXPORT_C TPtrC8 CCafMimeHeader::NonStandardMimeData(TInt aIndex) const
1.108 + {
1.109 + return iNonStandardMimeHeaders[aIndex]->Data();
1.110 + }
1.111 +
1.112 +EXPORT_C void CCafMimeHeader::ExternalizeL(RWriteStream& aStream) const
1.113 + {
1.114 + TInt i=0;
1.115 +
1.116 + // Write the contents of this class out to a stream
1.117 +
1.118 + // Write the number of standard mime headers
1.119 + aStream.WriteInt32L(EMimeMax);
1.120 +
1.121 + // Write out the standard mime data, each one preceeded by it's length
1.122 + for(i = 0; i < EMimeMax; i++)
1.123 + {
1.124 + TInt length=0;
1.125 +
1.126 + // Length is zero if the mime field has not been set,
1.127 + if(iStandardMimeHeader[i] == NULL)
1.128 + {
1.129 + length = 0;
1.130 + }
1.131 + else
1.132 + {
1.133 + length = iStandardMimeHeader[i]->Length();
1.134 + }
1.135 +
1.136 + // write the field length
1.137 + aStream.WriteInt16L(length);
1.138 + if(length > 0)
1.139 + {
1.140 + // write the field value if length > 0
1.141 + aStream.WriteL(*iStandardMimeHeader[i]);
1.142 + }
1.143 + }
1.144 +
1.145 + aStream.WriteInt32L(iNonStandardMimeHeaders.Count());
1.146 +
1.147 + // Write out non standard mime headers and data, each Des preceeded by it's length
1.148 + for(i = 0; i < iNonStandardMimeHeaders.Count(); i++)
1.149 + {
1.150 + aStream.WriteInt16L(iNonStandardMimeHeaders[i]->FieldName().Length());
1.151 + if(iNonStandardMimeHeaders[i]->FieldName().Length() > 0)
1.152 + {
1.153 + aStream.WriteL(iNonStandardMimeHeaders[i]->FieldName());
1.154 + }
1.155 + aStream.WriteInt16L(iNonStandardMimeHeaders[i]->Data().Length());
1.156 + if(iNonStandardMimeHeaders[i]->Data().Length() > 0)
1.157 + {
1.158 + aStream.WriteL(iNonStandardMimeHeaders[i]->Data());
1.159 + }
1.160 + }
1.161 + }
1.162 +
1.163 +EXPORT_C void CCafMimeHeader::InternalizeL(RReadStream& aStream)
1.164 + {
1.165 + TInt i=0;
1.166 + TInt fieldCount=0;
1.167 + TInt16 length=0;
1.168 + HBufC8 *field=NULL;
1.169 + HBufC8 *data=NULL;
1.170 +
1.171 +
1.172 + // clear any existing headers
1.173 + iNonStandardMimeHeaders.ResetAndDestroy();
1.174 + for(i = 0; i < EMimeMax; i++)
1.175 + {
1.176 + // delete the existing data for the mime field
1.177 + delete iStandardMimeHeader[i];
1.178 + iStandardMimeHeader[i] = NULL;
1.179 + }
1.180 +
1.181 +
1.182 + fieldCount= aStream.ReadInt32L();
1.183 + if(fieldCount != EMimeMax)
1.184 + {
1.185 + // something is seriously wrong, should always be EMimeMax standard headers
1.186 + User::Leave(KErrGeneral);
1.187 + }
1.188 +
1.189 + for(i = 0; i < EMimeMax; i++)
1.190 + {
1.191 + // read the length from the stream
1.192 + length = aStream.ReadInt16L();
1.193 + if(length > 0)
1.194 + {
1.195 + // read in non zero fields, others will remain NULL
1.196 + data = HBufC8::NewLC(length);
1.197 + TPtr8 dataPtr = data->Des();
1.198 + aStream.ReadL(dataPtr,length);
1.199 + SetStandardMimeDataL(static_cast <TMimeFields>(i), dataPtr);
1.200 + CleanupStack::PopAndDestroy(data);
1.201 + }
1.202 + }
1.203 +
1.204 + fieldCount= aStream.ReadInt32L();
1.205 +
1.206 + for(i = 0; i < fieldCount; i++)
1.207 + {
1.208 + // Read mime field name
1.209 + length = aStream.ReadInt16L();
1.210 + if(length < 0)
1.211 + {
1.212 + User::Leave(KErrCorrupt);
1.213 + }
1.214 +
1.215 + field = HBufC8::NewLC(length);
1.216 + TPtr8 fieldPtr = field->Des();
1.217 + if(length > 0)
1.218 + {
1.219 + aStream.ReadL(fieldPtr,length);
1.220 + }
1.221 +
1.222 + // Read data
1.223 + length = aStream.ReadInt16L();
1.224 + if(length < 0)
1.225 + {
1.226 + User::Leave(KErrCorrupt);
1.227 + }
1.228 +
1.229 + data = HBufC8::NewLC(length);
1.230 + TPtr8 dataPtr = data->Des();
1.231 + if(length > 0)
1.232 + {
1.233 + aStream.ReadL(dataPtr,length);
1.234 + }
1.235 +
1.236 + // Add a new mime field and data to our array
1.237 + AddNonStandardMimeL(fieldPtr, dataPtr);
1.238 +
1.239 + // free memory allocated
1.240 + CleanupStack::PopAndDestroy(data);
1.241 + CleanupStack::PopAndDestroy(field);
1.242 + }
1.243 + }
1.244 +
1.245 +
1.246 +#endif // #ifndef REMOVE_CAF1