1 /* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "UUID.h" 33 34 #include "NotImplemented.h" 35 36 #if PLATFORM(QT) 37 #include <QUuid> 38 #endif 39 40 #if OS(WINDOWS) 41 #include <objbase.h> 42 #elif OS(DARWIN) && USE(CF) 43 #include <CoreFoundation/CoreFoundation.h> 44 #elif OS(LINUX) && !PLATFORM(CHROMIUM) 45 #include <stdio.h> 46 #elif (OS(LINUX) && PLATFORM(CHROMIUM)) || (OS(DARWIN) && !USE(CF)) 47 #include <wtf/HexNumber.h> 48 #include <wtf/RandomNumber.h> 49 #include <wtf/text/StringBuilder.h> 50 #endif 51 52 namespace WebCore { 53 54 static const char uuidVersionRequired = '4'; 55 static const int uuidVersionIdentifierIndex = 14; 56 57 String createCanonicalUUIDString() 58 { 59 #if PLATFORM(QT) && !defined(QT_NO_QUUID_STRING) 60 QUuid uuid = QUuid::createUuid(); 61 String canonicalUuidStr = uuid.toString().mid(1, 36).toLower(); // remove opening and closing bracket and make it lower. 62 ASSERT(canonicalUuidStr[uuidVersionIdentifierIndex] == uuidVersionRequired); 63 return canonicalUuidStr; 64 #elif OS(WINDOWS) 65 GUID uuid = { 0 }; 66 HRESULT hr = CoCreateGuid(&uuid); 67 if (FAILED(hr)) 68 return String(); 69 wchar_t uuidStr[40]; 70 int num = StringFromGUID2(uuid, reinterpret_cast<LPOLESTR>(uuidStr), WTF_ARRAY_LENGTH(uuidStr)); 71 ASSERT(num == 39); 72 String canonicalUuidStr = String(uuidStr + 1, num - 3).lower(); // remove opening and closing bracket and make it lower. 73 ASSERT(canonicalUuidStr[uuidVersionIdentifierIndex] == uuidVersionRequired); 74 return canonicalUuidStr; 75 #elif OS(DARWIN) && USE(CF) 76 CFUUIDRef uuid = CFUUIDCreate(0); 77 CFStringRef uuidStrRef = CFUUIDCreateString(0, uuid); 78 String uuidStr(uuidStrRef); 79 CFRelease(uuidStrRef); 80 CFRelease(uuid); 81 String canonicalUuidStr = uuidStr.lower(); // make it lower. 82 ASSERT(canonicalUuidStr[uuidVersionIdentifierIndex] == uuidVersionRequired); 83 return canonicalUuidStr; 84 #elif OS(LINUX) && !PLATFORM(CHROMIUM) 85 // This does not work for the linux system that turns on sandbox. 86 FILE* fptr = fopen("/proc/sys/kernel/random/uuid", "r"); 87 if (!fptr) 88 return String(); 89 char uuidStr[37]; 90 char* result = fgets(uuidStr, sizeof(uuidStr), fptr); 91 fclose(fptr); 92 if (!result) 93 return String(); 94 String canonicalUuidStr = String(uuidStr).lower(); // make it lower. 95 ASSERT(canonicalUuidStr[uuidVersionIdentifierIndex] == uuidVersionRequired); 96 return canonicalUuidStr; 97 #elif (OS(LINUX) && PLATFORM(CHROMIUM)) || (OS(DARWIN) && !USE(CF)) 98 unsigned randomData[4]; 99 for (size_t i = 0; i < WTF_ARRAY_LENGTH(randomData); ++i) 100 randomData[i] = static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)); 101 102 // Format as Version 4 UUID. 103 StringBuilder builder; 104 appendUnsignedAsHexFixedSize(randomData[0], builder, 8, Lowercase); 105 builder.append("-"); 106 appendUnsignedAsHexFixedSize(randomData[1] >> 16, builder, 4, Lowercase); 107 builder.append("-4"); 108 appendUnsignedAsHexFixedSize(randomData[1] & 0x00000fff, builder, 3, Lowercase); 109 builder.append("-"); 110 appendUnsignedAsHexFixedSize((randomData[2] >> 30) | 0x8, builder, 1, Lowercase); 111 appendUnsignedAsHexFixedSize((randomData[2] >> 16) & 0x00000fff, builder, 3, Lowercase); 112 builder.append("-"); 113 appendUnsignedAsHexFixedSize(randomData[2] & 0x0000ffff, builder, 4, Lowercase); 114 appendUnsignedAsHexFixedSize(randomData[3], builder, 8, Lowercase); 115 builder.append("\n"); 116 return builder.toString(); 117 #else 118 notImplemented(); 119 return String(); 120 #endif 121 } 122 123 } 124