vpnengine/vpnmanager/src/uuid.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Create a new UID for the policy.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <random.h>
       
    21 
       
    22 #include "uuid.h"
       
    23 
       
    24 void Uuid::MakeUuidL(TUuid& aUuid, TUuidVersion aVersion)
       
    25 /**
       
    26  * Generate a UUID of the specified version.
       
    27  * NOTE. As of this writing, only version 4
       
    28  * (ERandomBased) is supported
       
    29  **/
       
    30     {
       
    31     // We currently support only version 4
       
    32     if (aVersion != ERandomBased)
       
    33         {
       
    34         User::Leave(KErrNotSupported);
       
    35         }
       
    36     
       
    37     /* Fill UUID with random data */
       
    38     RandomizeL(aUuid);
       
    39 
       
    40     /* Brand UUID with version and variant */
       
    41     Brand(aUuid, aVersion);
       
    42     }
       
    43 
       
    44 void Uuid::RandomizeL(TUuid& aUuid)
       
    45 /**
       
    46  * Fill the UUID with pseudo-random data
       
    47  */
       
    48     {
       
    49     // Delegate the task to the Symbian Random Number Generator
       
    50     // (we trust that it is the best alternative available)
       
    51     
       
    52     TPckg<TUuid> pckgUuid(aUuid);
       
    53     TRandom::RandomL(pckgUuid);
       
    54     }
       
    55 
       
    56 void Uuid::Brand(TUuid& aUuid, TUuidVersion aVersion)
       
    57 /**
       
    58  * Set variant and version information to the UUID
       
    59  */
       
    60     {
       
    61     // We currently support only UUID version 4
       
    62     if (aVersion != ERandomBased)
       
    63         {
       
    64         return;
       
    65         }
       
    66 
       
    67     // First set the variant information to value:
       
    68     //     Msb0     Msb1    Msb2
       
    69     //     1        0       x
       
    70     // as specified in draft-mealling-uuid-urn-05.txt
       
    71     // (a variable number of the most significant bits
       
    72     // of the eight octet of the UUID, i.e. the
       
    73     // clock_seq_hi_and_reserved octet)
       
    74     aUuid.iClockSeqHiAndReserved &= 0x3F;     // Set Msb0 and Msb1 to 0, take the rest "as-is"
       
    75     aUuid.iClockSeqHiAndReserved |= (1 << 7); // Set Msb0 to 1
       
    76 
       
    77     // Then set the version number to indicate version 4
       
    78     //     Msb0     Msb1    Msb2    Msb3
       
    79     //     0        1       0       0
       
    80     // as specified in draft-mealling-uuid-urn-05.txt
       
    81     // (in the most significant four bits of the time
       
    82     // stamp)
       
    83     aUuid.iTimeHiAndVersion &= 0x0FFF;    // Set Msb0-Msb3 to 0, take the rest "as-is"
       
    84     aUuid.iTimeHiAndVersion |= (1 << 14); // Set Msb1 to 1
       
    85     }
       
    86 
       
    87 void Uuid::UuidToString(const TUuid& aUuid, TUuidString& aUuidString)
       
    88 /**
       
    89  * Create and return a string representation of the given UUID.
       
    90  * E.g. f81d4fae-7dec-11d0-a765-00a0c91e6bf6
       
    91  */
       
    92     {
       
    93     aUuidString.Format(KUuidFormatString,
       
    94                        static_cast<TUint>(aUuid.iTimeLow),
       
    95                        static_cast<TUint>(aUuid.iTimeMid),
       
    96                        static_cast<TUint>(aUuid.iTimeHiAndVersion),
       
    97                        static_cast<TUint>(aUuid.iClockSeqHiAndReserved),
       
    98                        static_cast<TUint>(aUuid.iClockSeqLow),
       
    99                        static_cast<TUint>(aUuid.iNode[0]),
       
   100                        static_cast<TUint>(aUuid.iNode[1]),
       
   101                        static_cast<TUint>(aUuid.iNode[2]),
       
   102                        static_cast<TUint>(aUuid.iNode[3]),
       
   103                        static_cast<TUint>(aUuid.iNode[4]),
       
   104                        static_cast<TUint>(aUuid.iNode[5]));
       
   105     }
       
   106 
       
   107 void Uuid::UuidToString(const TUuid& aUuid, TUuidString8& aUuidString)
       
   108 /**
       
   109  * Create and return a string representation of the given UUID.
       
   110  * E.g. f81d4fae-7dec-11d0-a765-00a0c91e6bf6
       
   111  */
       
   112     {
       
   113     aUuidString.Format(KUuidFormatString8,
       
   114                        static_cast<TUint>(aUuid.iTimeLow),
       
   115                        static_cast<TUint>(aUuid.iTimeMid),
       
   116                        static_cast<TUint>(aUuid.iTimeHiAndVersion),
       
   117                        static_cast<TUint>(aUuid.iClockSeqHiAndReserved),
       
   118                        static_cast<TUint>(aUuid.iClockSeqLow),
       
   119                        static_cast<TUint>(aUuid.iNode[0]),
       
   120                        static_cast<TUint>(aUuid.iNode[1]),
       
   121                        static_cast<TUint>(aUuid.iNode[2]),
       
   122                        static_cast<TUint>(aUuid.iNode[3]),
       
   123                        static_cast<TUint>(aUuid.iNode[4]),
       
   124                        static_cast<TUint>(aUuid.iNode[5]));
       
   125     }