mtpfws/mtpfw/datatypes/src/tmtptypeguid.cpp
changeset 0 d0791faffa3f
child 4 60a94a45d437
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @publishedPartner
       
    19 */
       
    20 
       
    21 #include <mtp/mtpdatatypeconstants.h>
       
    22 #include <mtp/tmtptypeguid.h>
       
    23 
       
    24 /**
       
    25 Default constructor.
       
    26 */
       
    27 EXPORT_C TMTPTypeGuid::TMTPTypeGuid()
       
    28     {
       
    29     iData.FillZ(iData.MaxLength());
       
    30     }
       
    31 
       
    32 EXPORT_C TMTPTypeGuid::TMTPTypeGuid(const TPtrC8& aData) : 
       
    33 TMTPTypeUint128::TMTPTypeUint128(aData)
       
    34     {
       
    35     }
       
    36 
       
    37   
       
    38 EXPORT_C TMTPTypeGuid::TMTPTypeGuid(const TUint64 aData1,const TUint64 aData2) 
       
    39     {  
       
    40     const TUint KBitsOfByte = 8;
       
    41     const TUint KBitsOfWORD = 16;
       
    42     TGUID guid;
       
    43     
       
    44     //int32
       
    45     guid.iUint32 = I64HIGH(aData1);
       
    46     
       
    47     //int16[]
       
    48     guid.iUint16[0] = ((TUint16)(I64LOW(aData1) >> KBitsOfWORD));
       
    49     guid.iUint16[1] = ((TUint16)(I64LOW(aData1))) ;
       
    50     //guid.iUint16[2] = ((TUint16)(I64HIGH(aData2)>> KBitsOfWORD));
       
    51     
       
    52     //byte[]
       
    53     for(TInt i = 0; i< KMTPGUIDUint8Num; i++)
       
    54         {
       
    55         guid.iByte[KMTPGUIDUint8Num - 1 - i] = ((TUint8)(aData2 >> (KBitsOfByte * i))) ;
       
    56         }
       
    57     
       
    58     iData.FillZ(iData.MaxLength());
       
    59     memcpy(&iData[0], &guid, KMTPTypeUINT128Size);
       
    60     }
       
    61 
       
    62 
       
    63 /**
       
    64 Sets the data type to the specified value.
       
    65 */  
       
    66 EXPORT_C void TMTPTypeGuid::Set(const TUint64 aData1,const TUint64 aData2)
       
    67 	{   
       
    68     const TUint KBitsOfByte = 8;
       
    69     const TUint KBitsOfWORD = 16;
       
    70     TGUID guid;
       
    71     
       
    72     //int32
       
    73     guid.iUint32 = I64HIGH(aData1);
       
    74     
       
    75     //int16[]
       
    76     guid.iUint16[0] = ((TUint16)(I64LOW(aData1) >> KBitsOfWORD));
       
    77     guid.iUint16[1] = ((TUint16)(I64LOW(aData1))) ;
       
    78     //guid.iUint16[2] = ((TUint16)(I64HIGH(aData2)>> KBitsOfWORD));
       
    79     
       
    80     //byte[]
       
    81     for(TInt i = 0; i< KMTPGUIDUint8Num; i++)
       
    82         {
       
    83         guid.iByte[KMTPGUIDUint8Num - 1 - i] = ((TUint8)(aData2 >> (KBitsOfByte * i))) ;
       
    84         }
       
    85     
       
    86     memcpy(&iData[0], &guid, KMTPTypeUINT128Size);
       
    87 	}
       
    88 
       
    89 EXPORT_C TInt TMTPTypeGuid::SetL(const TDesC& aData)
       
    90 	{
       
    91 	TInt ret = KErrNone;
       
    92 	TGUID guid;
       
    93 	
       
    94 	ret = IsGuidFormat(aData);
       
    95 	
       
    96 	if ( ret != KErrNone )
       
    97 		{
       
    98 		return ret;
       
    99 		}
       
   100 	
       
   101 	RBuf buf;
       
   102 	buf.CleanupClosePushL();
       
   103 	buf.Create(aData);		
       
   104 	TPtr8 guidPtr = buf.Collapse();			
       
   105 	TInt length = guidPtr.Length();
       
   106 	TInt offset = 0;
       
   107 	
       
   108 	TPtrC8 dataStr1(&guidPtr[offset], 8);
       
   109 	TLex8 t1(dataStr1);
       
   110 	offset += 9;
       
   111 	ret = t1.Val(guid.iUint32, EHex);
       
   112 	
       
   113 	TPtrC8 dataStr2(&guidPtr[offset], 4);
       
   114 	TLex8 t2(dataStr2);
       
   115 	offset += 5;
       
   116 	ret = t2.Val(guid.iUint16[0], EHex);
       
   117 	
       
   118 	TPtrC8 dataStr3(&guidPtr[offset], 4);
       
   119 	TLex8 t3(dataStr3);
       
   120 	offset += 5;
       
   121 	ret = t3.Val(guid.iUint16[1], EHex);
       
   122 	
       
   123 	TInt index = 0;
       
   124 	for (TInt i(offset); (i<23); i = i+2)
       
   125 		{
       
   126 		TPtrC8 dataStr4(&guidPtr[offset], 2);
       
   127 		TLex8 t4(dataStr4);
       
   128 		offset += 2;
       
   129 		ret = t4.Val(guid.iByte[index++], EHex);
       
   130 		}
       
   131 	offset++;
       
   132 	for (TInt i(offset); (i<length); i = i+2)
       
   133 		{
       
   134 		TPtrC8 dataStr5(&guidPtr[offset], 2);
       
   135 		TLex8 t5(dataStr5);
       
   136 		offset += 2;
       
   137 		ret = t5.Val(guid.iByte[index++], EHex);
       
   138 		}
       
   139 	
       
   140 	memcpy(&iData[0], &guid, KMTPTypeUINT128Size);
       
   141 	
       
   142 	CleanupStack::PopAndDestroy(&buf);
       
   143 	
       
   144 	return ret;
       
   145 	}
       
   146  
       
   147 TInt TMTPTypeGuid::IsGuidFormat(const TDesC& aData)
       
   148 	{
       
   149 	TInt ret = KErrNone;
       
   150 	
       
   151 	//verify GUID style data xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
       
   152 	RBuf buf;
       
   153 	buf.CleanupClosePushL();
       
   154 	buf.Create(aData);		
       
   155 	TPtr8 guidPtr = buf.Collapse();			
       
   156 	TInt length = guidPtr.Length();
       
   157 	
       
   158 	if ( length == 36 )
       
   159 		{
       
   160 		for ( TInt i=0;i<length;++i)
       
   161 			{
       
   162 			TChar c(guidPtr[i]);
       
   163 			if ( !c.IsHexDigit() )
       
   164 				{
       
   165 				if ( (guidPtr[i]=='-') && (i==8 || i==13 || i==18 || i==23) )
       
   166 					{}
       
   167 				else
       
   168 					{
       
   169 					ret = KErrArgument;
       
   170 					}
       
   171 				}
       
   172 			}
       
   173 		}
       
   174 	else
       
   175 		{
       
   176 		ret = KErrArgument;
       
   177 		}
       
   178 	CleanupStack::PopAndDestroy(&buf);
       
   179 	
       
   180 	return ret;
       
   181 	}
       
   182 
       
   183 
       
   184 
       
   185