1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/store/pcstore/src/checkeduid.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,341 @@
1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +
1.20 +#include <assert.h>
1.21 +#include <memory.h>
1.22 +#include <pcstore/checkeduid.h>
1.23 +#include <pcstore/descriptors.h>
1.24 +#include <pcstore/storeexception.h>
1.25 +#include "pcstoreconst.h"
1.26 +
1.27 +namespace PCStore
1.28 +{
1.29 +const TUint crcTab[256] =
1.30 + {
1.31 + 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,0x8108,0x9129,0xa14a,
1.32 + 0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,
1.33 + 0x72f7,0x62d6,0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,0x2462,
1.34 + 0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,0xa56a,0xb54b,0x8528,0x9509,
1.35 + 0xe5ee,0xf5cf,0xc5ac,0xd58d,0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,
1.36 + 0x46b4,0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,0x48c4,0x58e5,
1.37 + 0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,
1.38 + 0x9969,0xa90a,0xb92b,0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
1.39 + 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,0x6ca6,0x7c87,0x4ce4,
1.40 + 0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,
1.41 + 0x8d68,0x9d49,0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,0xff9f,
1.42 + 0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,0x9188,0x81a9,0xb1ca,0xa1eb,
1.43 + 0xd10c,0xc12d,0xf14e,0xe16f,0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,
1.44 + 0x6067,0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,0x02b1,0x1290,
1.45 + 0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,
1.46 + 0xe54f,0xd52c,0xc50d,0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
1.47 + 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,0x26d3,0x36f2,0x0691,
1.48 + 0x16b0,0x6657,0x7676,0x4615,0x5634,0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,
1.49 + 0xb98a,0xa9ab,0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,0xcb7d,
1.50 + 0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,0x4a75,0x5a54,0x6a37,0x7a16,
1.51 + 0x0af1,0x1ad0,0x2ab3,0x3a92,0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,
1.52 + 0x8dc9,0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,0xef1f,0xff3e,
1.53 + 0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,
1.54 + 0x3eb2,0x0ed1,0x1ef0
1.55 + };
1.56 +
1.57 +/**
1.58 +Performs a CCITT CRC checksum on the specified data.
1.59 +
1.60 +On return from this function, the referenced 16 bit integer contains the checksum.
1.61 +
1.62 +@param aCrc A reference to a 16 bit integer to contain the checksum.
1.63 +@param aPtr A pointer to the start of the data to be checksum.
1.64 +@param aLength The length of the data to be checksum.
1.65 +*/
1.66 +void Crc(TUint16& aCrc,const void* aPtr,TInt aLength)
1.67 + {
1.68 + const TUint8* pB=static_cast<const TUint8*>(aPtr);
1.69 + const TUint8* pE=pB+aLength;
1.70 + TUint crc=aCrc;
1.71 + while (pB<pE)
1.72 + {
1.73 + crc=(crc<<8)^crcTab[((crc>>8)^*pB++)&0xff];
1.74 + }
1.75 + aCrc=(TUint16)crc;
1.76 + }
1.77 +
1.78 +
1.79 +/**
1.80 +Generate a checksum for the object of TCheckedUid.
1.81 +
1.82 +@param aPtr The pointer to the TCheckedUid.
1.83 +@return The resulted checksum for the TCheckedUid.
1.84 +*/
1.85 +TUint checkSum(const void *aPtr)
1.86 + {
1.87 + const TUint8* pB=static_cast<const TUint8*>(aPtr);
1.88 + const TUint8* pE=pB+(KMaxCheckedUid*sizeof(TUid));
1.89 + TUint8 buf[(KMaxCheckedUid*sizeof(TUid))>>1];
1.90 + TUint8* pT=(&buf[0]);
1.91 + while (pB<pE)
1.92 + {
1.93 + *pT++=(*pB);
1.94 + pB+=2;
1.95 + }
1.96 + TUint16 crc=0;
1.97 + Crc(crc,&buf[0],(KMaxCheckedUid*sizeof(TUid))>>1);
1.98 + return(crc);
1.99 + }
1.100 +
1.101 +/**
1.102 +Constructor that creates a zero-valued UID type.
1.103 +*/
1.104 +TUidType::TUidType()
1.105 + {
1.106 + memset(this,0,sizeof(TUidType));
1.107 + }
1.108 +
1.109 +
1.110 +/**
1.111 +Constructor that creates a UID type and sets the UID1 component
1.112 +to the specified value.
1.113 +
1.114 +The UID2 and UID3 components are set to KNullUid.
1.115 +
1.116 +@param aUid1 Value for UID1.
1.117 +*/
1.118 +TUidType::TUidType(TUid aUid1)
1.119 + {
1.120 + memset(this,0,sizeof(TUidType));
1.121 + iUid[0]=aUid1;
1.122 + }
1.123 +
1.124 +
1.125 +/**
1.126 +Constructor that creates a UID type and sets the UID1 and UID2 components
1.127 +to the specified values.
1.128 +
1.129 +The UID3 component is set to KNullUid.
1.130 +
1.131 +@param aUid1 Value for UID1.
1.132 +@param aUid2 Value for UID2.
1.133 +*/
1.134 +TUidType::TUidType(TUid aUid1, TUid aUid2)
1.135 + {
1.136 + iUid[0]=aUid1;
1.137 + iUid[1]=aUid2;
1.138 + iUid[2]=KNullUid;
1.139 + }
1.140 +
1.141 +/**
1.142 +Constructor that creates a UID type and sets all three UID components
1.143 +to the specified values.
1.144 +
1.145 +@param aUid1 Value for UID1.
1.146 +@param aUid2 Value for UID2.
1.147 +@param aUid3 Value for UID3.
1.148 +*/
1.149 +TUidType::TUidType(TUid aUid1, TUid aUid2, TUid aUid3)
1.150 + {
1.151 + iUid[0]=aUid1;
1.152 + iUid[1]=aUid2;
1.153 + iUid[2]=aUid3;
1.154 + }
1.155 +
1.156 +
1.157 +/**
1.158 +Compares this UID type for inequality with the specified UID type.
1.159 +
1.160 +@param aUidType The UID type to be compared.
1.161 +@return TRUE, if any component UID is not equal to the corresponding component
1.162 +UID in the specified UID type; FALSE otherwise.
1.163 +*/
1.164 +TBool TUidType::operator !=(const TUidType& aUidType) const
1.165 + {
1.166 + return(!(*this==aUidType));
1.167 + }
1.168 +
1.169 +/**
1.170 +Compares this UID type for equality with the specified UID type.
1.171 +
1.172 +@param aUidType The reference to the UID type to be compared.
1.173 +@return TRUE, if each component UID is equal to the corresponding component
1.174 +UID in the specified UID type; FALSE, otherwise.
1.175 +*/
1.176 +TBool TUidType::operator ==(const TUidType& aUidType) const
1.177 + {
1.178 + return(iUid[0]==aUidType.iUid[0] &&
1.179 + iUid[1]==aUidType.iUid[1] &&
1.180 + iUid[2]==aUidType.iUid[2]);
1.181 + }
1.182 +
1.183 +
1.184 +/**
1.185 +Gets the most derived UID.
1.186 +
1.187 +Taking the three UID components as a hierarchy with UID1 being the most general,
1.188 +UID2 being more specific than UID1 and UID3 being more specific than UID2,
1.189 +then the function returns:
1.190 +
1.191 +UID3, if UID3 is not KNullUid.
1.192 +UID2, if UID2 is not KNullUid.
1.193 +UID1, otherwise
1.194 +
1.195 +@return The most derived UID.
1.196 +*/
1.197 +TUid TUidType::MostDerived() const
1.198 + {
1.199 + if (iUid[2]!=KNullUid)
1.200 + {
1.201 + return(iUid[2]);
1.202 + }
1.203 + if (iUid[1]!=KNullUid)
1.204 + {
1.205 + return(iUid[1]);
1.206 + }
1.207 + return(iUid[0]);
1.208 + }
1.209 +
1.210 +/**
1.211 +Gets the UID component as identified by the specified index.
1.212 +
1.213 +@param aIndex Index value indicating which UID component to return.
1.214 + 0 specifies UID1,
1.215 + 1 specifies UID2,
1.216 + 2 specifies UID3.
1.217 +
1.218 +aIndex must be between 0 and 2.
1.219 +
1.220 +@return The reference to the required UID component.
1.221 +*/
1.222 +const TUid& TUidType::operator[](TInt aIndex) const
1.223 + {
1.224 + assert(aIndex >= 0 && aIndex <= 2);
1.225 + return(iUid[aIndex]);
1.226 + }
1.227 +
1.228 +/**
1.229 +Tests the object for a valid (non-KNullUid) UID type.
1.230 +
1.231 +@return TRUE, if at least one of the component UIDs is not KNullUid; FALSE,
1.232 +if all component UIDs are KNullUid.
1.233 +*/
1.234 +TBool TUidType::IsValid() const
1.235 + {
1.236 + return(MostDerived()!=KNullUid);
1.237 + }
1.238 +
1.239 +
1.240 +/**
1.241 +Default constructor.
1.242 +
1.243 +Initialises the object to binary zeroes.
1.244 +*/
1.245 +TCheckedUid::TCheckedUid()
1.246 + {
1.247 + memset(this,0, sizeof(TCheckedUid));
1.248 + }
1.249 +
1.250 +
1.251 +/**
1.252 +Constructor taking an existing Uid type.
1.253 +
1.254 +The constructor calculates a checksum.
1.255 +
1.256 +@param aUidType The reference to a Uid type to be packaged.
1.257 +*/
1.258 +TCheckedUid::TCheckedUid(const TUidType& aUidType)
1.259 + {
1.260 + Set(aUidType);
1.261 + }
1.262 +
1.263 +
1.264 +/**
1.265 +Constructor taking an existing TCheckedUid object encapsulated within
1.266 +a descriptor.
1.267 +
1.268 +The checksum is recalculated and must match the checksum value passed in the
1.269 +encapsulated TCheckedUid object, otherwise the content of this object is reset
1.270 +to binary zeroes.
1.271 +
1.272 +aDes should be an 8-bit descriptor which holds the contents of a TCheckedUid object.
1.273 +
1.274 +@param aDes The reference to an 8-bit descriptor containing an existing TCheckedUid
1.275 +object.
1.276 +*/
1.277 +TCheckedUid::TCheckedUid(const CDes8& aDes)
1.278 + {
1.279 + Set(aDes);
1.280 + }
1.281 +
1.282 +/**
1.283 +Sets the specified Uid type to be packaged, and recalculates the checksum.
1.284 +
1.285 +@param aUidType The reference to a Uid type to be set.
1.286 +*/
1.287 +void TCheckedUid::Set(const TUidType& aUidType)
1.288 + {
1.289 + iType = aUidType;
1.290 + iCheck = Check();
1.291 + }
1.292 +
1.293 +/**
1.294 +Sets an existing TCheckedUid object encapsulated within a descriptor.
1.295 +
1.296 +The checksum is recalculated and must match the checksum value passed in the
1.297 +encapsulated TCheckedUid object, otherwise the content of this object is reset
1.298 +to binary zeroes.
1.299 +
1.300 +aDes should be an 8-bit descriptor which holds the contents of a TCheckedUid object.
1.301 +
1.302 +@param aDes The reference to a descriptor containing an existing TCheckedUid object.
1.303 +*/
1.304 +void TCheckedUid::Set(const CDes8& aDes)
1.305 + {
1.306 + assert(aDes.Length() == sizeof(TCheckedUid));
1.307 + memcpy(this,aDes.Ptr(),sizeof(TCheckedUid));
1.308 + if (iCheck!=Check())
1.309 + {
1.310 + memset(this,0,sizeof(TCheckedUid));
1.311 + }
1.312 + }
1.313 +
1.314 +/**
1.315 +Gets a descriptor to represent this object's data.
1.316 +
1.317 +@return The descriptor for this object's data. The descriptor's length
1.318 +is the same as the length of a TCheckedUid object.
1.319 +*/
1.320 +CDes8 TCheckedUid::Des() const
1.321 + {
1.322 + return(CDes8 (reinterpret_cast<const TUint8*>(this),sizeof(TCheckedUid)));
1.323 + }
1.324 +
1.325 +/**
1.326 +Gets the Uid type contained in this object.
1.327 +
1.328 +@return The Uid type.
1.329 +*/
1.330 +const TUidType& TCheckedUid::UidType() const
1.331 + {
1.332 + return(iType);
1.333 + }
1.334 +
1.335 +/**
1.336 +Calculates the checksum of the UIDs.
1.337 +
1.338 +@return The checksum.
1.339 +*/
1.340 +TUint TCheckedUid::Check() const
1.341 + {
1.342 + return((checkSum((reinterpret_cast<TUint8*>(const_cast<TCheckedUid*>(this)))+1)<<16)|checkSum(this));
1.343 + }
1.344 +}