string uuid() { string result; #ifdef _WIN32 #ifndef _UNICODE UUID uid; auto status = UuidCreate(&uid); ASSERT(status == RPC_S_OK, "Failed to create UUID"); char* str; auto res = UuidToStringA(&uid, reinterpret_cast<RPC_CSTR*>(&str)); ASSERT(res == RPC_S_OK, "Failed to create string from UUID"); result = reinterpret_cast<const char*>(str); RpcStringFreeA(reinterpret_cast<RPC_CSTR*>(&str)); #else UUID uid; auto status = UuidCreate(&uid); ASSERT(status == RPC_S_OK, "Failed to create UUID"); wchar_t* str; auto res = UuidToStringW(&uid, reinterpret_cast<RPC_WSTR*>(&str)); ASSERT(res == RPC_S_OK, "Failed to create string from UUID"); wstring wideRes = reinterpret_cast<const wchar_t*>(str); result = engine::toUtf8String(wideRes); RpcStringFreeW(reinterpret_cast<RPC_WSTR*>(&str)); #endif #endif return result; }
std::wstring Directory::CreateDirectoryWithUniqueName (const std::wstring & strFolderPathRoot) { UUID uuid; RPC_WSTR str_uuid; UuidCreate (&uuid); UuidToString (&uuid, &str_uuid); std::wstring pcTemplate = strFolderPathRoot + FILE_SEPARATOR_STR; pcTemplate += (TCHAR *) str_uuid; RpcStringFree (&str_uuid); int attemps = 10; while (!CreateDirectory(pcTemplate)) { UuidCreate (&uuid); UuidToString (&uuid, &str_uuid); pcTemplate = strFolderPathRoot + FILE_SEPARATOR_STR; pcTemplate += (TCHAR *) str_uuid; RpcStringFree (&str_uuid); attemps--; if (0 == attemps) { pcTemplate = _T(""); } } return pcTemplate; }
static void test_UuidCreate(void) { UUID guid; BYTE version; UuidCreate(&guid); version = (guid.Data3 & 0xf000) >> 12; ok(version == 4 || broken(version == 1), "unexpected version %d\n", version); if (version == 4) { static UUID v4and = { 0, 0, 0x4000, { 0x80,0,0,0,0,0,0,0 } }; static UUID v4or = { 0xffffffff, 0xffff, 0x4fff, { 0xbf,0xff,0xff,0xff,0xff,0xff,0xff,0xff } }; UUID and, or; RPC_STATUS rslt; int i; char buf[39]; and = guid; or = guid; /* Generate a bunch of UUIDs and mask them. By the end, we expect * every randomly generated bit to have been zero at least once, * resulting in no bits set in the and mask except those which are not * randomly generated: the version number and the topmost bits of the * Data4 field (treated as big-endian.) Similarly, we expect only * the bits which are not randomly set to be cleared in the or mask. */ for (i = 0; i < 1000; i++) { LPBYTE src, dst; UuidCreate(&guid); for (src = (LPBYTE)&guid, dst = (LPBYTE)∧ src - (LPBYTE)&guid < sizeof(guid); src++, dst++) *dst &= *src; for (src = (LPBYTE)&guid, dst = (LPBYTE)∨ src - (LPBYTE)&guid < sizeof(guid); src++, dst++) *dst |= *src; } ok(UuidEqual(&and, &v4and, &rslt), "unexpected bits set in V4 UUID: %s\n", printGuid(buf, sizeof(buf), &and)); ok(UuidEqual(&or, &v4or, &rslt), "unexpected bits set in V4 UUID: %s\n", printGuid(buf, sizeof(buf), &or)); } else { /* Older versions of Windows generate V1 UUIDs. For these, there are * many stable bits, including at least the MAC address if one is * present. Just check that Data4[0]'s most significant bits are * set as expected. */ ok((guid.Data4[0] & 0xc0) == 0x80, "unexpected value in Data4[0]: %02x\n", guid.Data4[0] & 0xc0); } }
void random_c::generate_bytes(void *destination, size_t num_bytes) { UUID uuid; if (!m_seeded) { srand(GetTickCount()); m_seeded = true; } if (!m_tried_uuidcreate) { // Find out whether UuidCreate returns different // data in Data4 on each call by comparing up to five // results. If not use srand() and rand(). UUID first_uuid; int i; m_use_uuidcreate = true; RPC_STATUS status = UuidCreate(&first_uuid); if ((RPC_S_OK == status) || (RPC_S_UUID_LOCAL_ONLY == status)) { for (i = 0; i < 5; ++i) { status = UuidCreate(&uuid); if (((RPC_S_OK != status) && (RPC_S_UUID_LOCAL_ONLY != status)) || !memcmp(first_uuid.Data4, uuid.Data4, sizeof(uuid.Data4))) { m_use_uuidcreate = false; break; } } } else m_use_uuidcreate = false; m_tried_uuidcreate = true; } size_t num_written = 0; while (num_written < num_bytes) { if (m_use_uuidcreate) { RPC_STATUS status = UuidCreate(&uuid); if ((RPC_S_OK != status) && (RPC_S_UUID_LOCAL_ONLY != status)) { m_use_uuidcreate = false; continue; } int num_left = num_bytes - num_written; if (num_left > 8) num_left = 8; memcpy((unsigned char *)destination + num_written, &uuid.Data4, num_left); num_written += num_left; } else for (; num_written < num_bytes; ++num_written) ((unsigned char *)destination)[num_written] = (unsigned char)(256.0 * rand() / (RAND_MAX + 1.0)); } }
TEST(Guid2StrTest, RandomGUID) { GUID guid = { 0 }; UuidCreate(&guid); Guid2Str *o = new Guid2Str(guid); EXPECT_TRUE(o->Str() != NULL); delete o; }
static void _uuuid_create(struct uuuid_t** uuuid, int* status, int nil) { struct uuuid_t* u; RPC_STATUS st; u = uuuid_new(); if (!u) { *status = UUUID_ERR; return; } if (nil) st = UuidCreateNil(&u->uuid); else st = UuidCreate(&u->uuid); if (st != RPC_S_OK) { uuuid_free(u); *status = UUUID_ERR; return; } *uuuid = u; *status = UUUID_OK; }
void MSVC8_Filter::write_filter_name_vs100(OutputWriter &output, int indent, const std::string &parent) const { std::string new_filter; if (parent.length()) { new_filter = parent + "\\" + name; } else { new_filter = name; } output.write_line(indent, " <Filter Include=\"" + new_filter + "\">"); // Create a new GUID: unsigned char *projectGUID = 0; GUID guid; UuidCreate(&guid); UuidToStringA(&guid, &projectGUID); _strupr((char *) projectGUID); std::string returnGUID = std::string("{") + ((char *) projectGUID) + std::string("}"); RpcStringFreeA(&projectGUID); output.write_line(indent, " <UniqueIdentifier>" + returnGUID + "</UniqueIdentifier>"); output.write_line(indent, " </Filter>"); std::vector<MSVC8_FileItem *>::size_type index; for (index = 0; index < files.size(); index++) { files[index]->write_filter_name_vs100(output, indent, new_filter); } }
static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch) { UUID uuid; RPC_STATUS status = UuidCreate(&uuid); HRESULT result; if (RPC_S_OK != status && RPC_S_UUID_LOCAL_ONLY != status && RPC_S_UUID_NO_ADDRESS != status) { giterr_set(GITERR_NET, "Unable to generate name for temp file"); return -1; } if (buffer_len_cch < UUID_LENGTH_CCH + 1) { giterr_set(GITERR_NET, "Buffer too small for name of temp file"); return -1; } result = StringCbPrintfW( buffer, buffer_len_cch, L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x", uuid.Data1, uuid.Data2, uuid.Data3, uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3], uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]); if (FAILED(result)) { giterr_set(GITERR_OS, "Unable to generate name for temp file"); return -1; } return 0; }
zuuid_t * zuuid_new (void) { zuuid_t *self = (zuuid_t *) zmalloc (sizeof (zuuid_t)); if (self) { #if defined (HAVE_LIBUUID) # if defined (__WINDOWS__) UUID uuid; assert (sizeof (uuid) == ZUUID_LEN); UuidCreate (&uuid); zuuid_set (self, (byte *) &uuid); # else uuid_t uuid; assert (sizeof (uuid) == ZUUID_LEN); uuid_generate (uuid); zuuid_set (self, (byte *) uuid); # endif #else // No UUID system calls, so generate a random string byte uuid [ZUUID_LEN]; int fd = open ("/dev/urandom", O_RDONLY); if (fd != -1) { ssize_t bytes_read = read (fd, uuid, ZUUID_LEN); assert (bytes_read == ZUUID_LEN); close (fd); } zuuid_set (self, uuid); #endif } return self; }
static ULONG V2vConnectorProcessInternalTx(V2V_CONNECTOR_STATE *vcs) { unsigned available; volatile UCHAR *msg; ULONG error; V2V_FRAME_HEADER *header; V2V_POST_INTERNAL *vpi; RPC_STATUS rpcstat; size_t msize; printf("V2VAPP-CONNECTOR sending internal message #%d\n", vcs->txCounter + 1); available = v2v_nc2_producer_bytes_available(vcs->channel); printf("V2VAPP-CONNECTOR channel indicates minimum bytes available: 0x%x\n", available); if (vcs->vac->xferSize == 0) { printf("V2VAPP-CONNECTOR transer size 0, send nothing\n"); return ERROR_SUCCESS; } if (vcs->vac->fastrx && v2v_nc2_remote_requested_fast_wakeup(vcs->channel)) msize = MIN(vcs->vac->xferSize, vcs->vac->xferMaxFastRx); else msize = vcs->vac->xferSize; if (!v2v_nc2_prep_message(vcs->channel, msize, V2V_MESSAGE_TYPE_INTERNAL, 0, &msg)) { error = GetLastError(); if (error == ERROR_RETRY) { /* No room right now, return and try again later */ printf("V2VAPP-CONNECTOR not enough buffer space to send message #%d; retry\n", vcs->txCounter + 1); return ERROR_RETRY; } printf("V2VAPP-CONNECTOR transmit internal data failure; abort processing - error: 0x%x\n", error); return error; /* failure */ } vcs->txCounter++; /* next message */ header = (V2V_FRAME_HEADER*)msg; header->id = (USHORT)vcs->txCounter; header->type = V2V_MESSAGE_TYPE_INTERNAL; header->cs = 0; header->length = vcs->vac->xferSize; vpi = (V2V_POST_INTERNAL*)msg; rpcstat = UuidCreate(&vpi->guid); if (rpcstat != RPC_S_OK) { printf("V2VAPP-CONNECTOR UuidCreate() failed - error: 0x%x; using NULL GUID\n", rpcstat); memset((void*)(msg + sizeof(V2V_FRAME_HEADER)), 0, sizeof(GUID)); } /* Fill it up with some data and send it */ memset((void*)(msg + sizeof(V2V_POST_INTERNAL)), 'X', (vcs->vac->xferSize - sizeof(V2V_POST_INTERNAL))); header->cs = V2vChecksum((const UCHAR*)msg, vcs->vac->xferSize); v2v_nc2_send_messages(vcs->channel); /* Keep the send loop going by setting the event. If there is no more room, the prep message call will return ERROR_RETRY and just land us back in the wait. */ SetEvent(v2v_get_send_event(vcs->channel)); return ERROR_SUCCESS; }
static void initialize_display_settings( HWND desktop ) { static const WCHAR display_device_guid_propW[] = { '_','_','w','i','n','e','_','d','i','s','p','l','a','y','_', 'd','e','v','i','c','e','_','g','u','i','d',0 }; GUID guid; RPC_CSTR guid_str; ATOM guid_atom; DEVMODEW dmW; UuidCreate( &guid ); UuidToStringA( &guid, &guid_str ); WINE_TRACE( "display guid %s\n", guid_str ); guid_atom = GlobalAddAtomA( (LPCSTR)guid_str ); SetPropW( desktop, display_device_guid_propW, ULongToHandle(guid_atom) ); RpcStringFreeA( &guid_str ); /* Store current display mode in the registry */ if (EnumDisplaySettingsExW( NULL, ENUM_CURRENT_SETTINGS, &dmW, 0 )) { WINE_TRACE( "Current display mode %ux%u %u bpp %u Hz\n", dmW.dmPelsWidth, dmW.dmPelsHeight, dmW.dmBitsPerPel, dmW.dmDisplayFrequency ); ChangeDisplaySettingsExW( NULL, &dmW, 0, CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY, NULL ); } }
static void get_uuid(char *str) { #ifdef HAVE_WINDOWS_H UUID guid; UuidCreate(&guid); sprintf(str, "%08lX-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); #elif defined(HAVE_CFUUIDCREATE) CFUUIDRef myUUID; CFStringRef myUUIDString; char strBuffer[100]; myUUID = CFUUIDCreate(kCFAllocatorDefault); myUUIDString = CFUUIDCreateString(kCFAllocatorDefault, myUUID);/* This is the safest way to obtain a C string from a CFString.*/ CFStringGetCString(myUUIDString, str, SMPD_MAX_DBS_NAME_LEN, kCFStringEncodingASCII); CFRelease(myUUIDString); #elif defined(HAVE_UUID_GENERATE) uuid_t guid; uuid_generate(guid); uuid_unparse(guid, str); #else sprintf(str, "%X%X%X%X", rand(), rand(), rand(), rand()); #endif }
/** * CreateGuid generates a global unique identifier. It calls the * Win32 API function for doing this. */ OpcUa_Guid* OPCUA_DLLCALL OpcUa_P_Guid_Create(OpcUa_Guid* Guid) { #ifndef _GUID_CREATE_NOT_AVAILABLE if(UuidCreate((UUID*)Guid) != RPC_S_OK) { /* Error */ Guid = OpcUa_Null; return OpcUa_Null; } /* Good */ return Guid; #else unsigned int *data = (unsigned int*)Guid; int chunks = 16 / sizeof(unsigned int); static const int intbits = sizeof(int)*8; static int randbits = 0; if (!randbits) { OpcUa_DateTime now; int max = RAND_MAX; do { ++randbits; } while ((max=max>>1)); now = OpcUa_P_DateTime_UtcNow(); srand(now.dwLowDateTime^now.dwHighDateTime); rand(); /* Skip first */ }
double BytesRandomD() { BYTE Buffer[0x464]; SHA_CTX Context; int idx; idx = 0; memcpy(Buffer, RandomSeed, SHA_DIGEST_LENGTH); idx += sizeof(RandomSeed); GlobalMemoryStatus((LPMEMORYSTATUS)&Buffer[idx]); idx += sizeof(MEMORYSTATUS); UuidCreate((UUID *)&Buffer[idx]); idx += sizeof(UUID); GetCursorPos((LPPOINT)&Buffer[idx]); idx += sizeof(POINT); *(DWORD *)(Buffer + idx) = GetTickCount(); *(DWORD *)(Buffer + idx + 4) = GetMessageTime(); *(DWORD *)(Buffer + idx + 8) = GetCurrentThreadId(); *(DWORD *)(Buffer + idx + 12) = GetCurrentProcessId(); idx += 16; QueryPerformanceCounter((LARGE_INTEGER *)&Buffer[idx]); SHA1_Init(&Context); SHA1_Update(&Context, Buffer, 0x464); SHA1_Update(&Context, "additional salt...", 0x13); SHA1_Final(RandomSeed, &Context); return BytesSHA1d(Buffer, 0x464); }
static PyObject* uuidcreate(PyObject* obj, PyObject*args) { UUID result; wchar_t *cresult; PyObject *oresult; /* May return ok, local only, and no address. For local only, the documentation says we still get a uuid. For RPC_S_UUID_NO_ADDRESS, it's not clear whether we can use the result. */ if (UuidCreate(&result) == RPC_S_UUID_NO_ADDRESS) { PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result"); return NULL; } if (UuidToStringW(&result, &cresult) == RPC_S_OUT_OF_MEMORY) { PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen"); return NULL; } oresult = PyUnicode_FromWideChar(cresult, wcslen(cresult)); RpcStringFreeW(&cresult); return oresult; }
ClusterMapLink::ClusterMapLink(GUID dest, GUID src) { UuidCreate(&UUID_inst); UuidCopy(&SourceClusterMapUUID,&src); UuidCopy(&DestinationClusterMapUUID,&dest); }
static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch) { UUID uuid; RPC_STATUS status = UuidCreate(&uuid); int result; if (RPC_S_OK != status && RPC_S_UUID_LOCAL_ONLY != status && RPC_S_UUID_NO_ADDRESS != status) { giterr_set(GITERR_NET, "Unable to generate name for temp file"); return -1; } if (buffer_len_cch < UUID_LENGTH_CCH + 1) { giterr_set(GITERR_NET, "Buffer too small for name of temp file"); return -1; } #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API) result = swprintf_s(buffer, buffer_len_cch, #else result = wsprintfW(buffer, #endif L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x", uuid.Data1, uuid.Data2, uuid.Data3, uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3], uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]); if (result < UUID_LENGTH_CCH) { giterr_set(GITERR_OS, "Unable to generate name for temp file"); return -1; } return 0; }
/** * Generate a UUID * * @param out the output character array * @return CPL_OK on success or an error code */ cpl_return_t cpl_platform_generate_uuid(cpl_uuid_t* out) { #if defined(__unix__) || defined(__APPLE__) // Static assertion int __a[sizeof(uuid_t) == sizeof(cpl_id_t) ? 1 : -1]; (void) __a; uuid_t uuid; uuid_generate_random(uuid); memcpy(out, uuid, sizeof(cpl_id_t)); #elif defined(_WINDOWS) // Static assertion int __a[sizeof(UUID) == sizeof(cpl_id_t) ? 1 : -1]; (void) __a; UUID uuid; UuidCreate(&uuid); memcpy(out, &uuid, sizeof(cpl_id_t)); #else #error "Not implemented for this platform" #endif return CPL_OK; }
/* 010509 Carl Corcoran */ HRESULT CCString::FromNewGuid() { GUID guid; UuidCreate(&guid); this->FromGuid(&guid); return S_OK; }
char* pn_i_genuuid(void) { unsigned char *generated; UUID uuid; UuidCreate(&uuid); UuidToString(&uuid, &generated); char* r = pn_strdup((const char*)generated); RpcStringFree(&generated); return r; }
zmq::uuid_t::uuid_t () { RPC_STATUS ret = UuidCreate (&uuid); zmq_assert (ret == RPC_S_OK); ret = UuidToStringA (&uuid, &string_buf); zmq_assert (ret == RPC_S_OK); create_blob (); }
void uuid_generate_random(uuid_t out){ UUID uuid; UuidCreateNil(&uuid); UuidCreate(&uuid); uuid.Data1 = htonl(uuid.Data1); uuid.Data2 = htons(uuid.Data2); uuid.Data3 = htons(uuid.Data3); memcpy(out, &uuid, UUID_T_LENGTH); }
uuid operator()() { UUID u_; UuidCreate(&u_); char *p = reinterpret_cast<char*>(&u_); uuid u; u.assign(p, p + sizeof(UUID)); return u; }
// create a new UUID void Uuid::Create() { UUID uuid; // can't fail UuidCreate(&uuid); Set(uuid); }
directconnection::directconnection(const char* CallID, const char* Wlid) { memset(this, 0, sizeof(directconnection)); wlid = mir_strdup(Wlid); callId = mir_strdup(CallID); mNonce = (UUID*)mir_alloc(sizeof(UUID)); UuidCreate(mNonce); ts = time(NULL); }
BOOL UuidCreateToString(TCHAR ** StringUuid) { UUID Uuid; RPC_STATUS Status = UuidCreate(&Uuid); if (Status == RPC_S_OK) { Status = UuidToString(&Uuid, StringUuid); } return Status == RPC_S_OK; }
SWITCH_DECLARE(void) switch_uuid_get(switch_uuid_t *uuid) { switch_mutex_lock(runtime.uuid_mutex); #ifndef WIN32 apr_uuid_get((apr_uuid_t *) uuid); #else UuidCreate((UUID *) uuid); #endif switch_mutex_unlock(runtime.uuid_mutex); }
void gen_uuid_inplace (char *buf) { unsigned char *str = NULL; UUID uuid; UuidCreate(&uuid); UuidToString(&uuid, &str); memcpy(buf, str, 37); RpcStringFree(&str); }
std::string FMUCodeGen::generateGUID() { UUID uuid; UuidCreate ( &uuid ); unsigned char * str; UuidToStringA ( &uuid, &str ); std::string guid( ( char* ) str ); guid = "{"+guid+"}"; RpcStringFreeA ( &str ); return guid; }
void CSQLiteAddressbookPlugin::addAddressbook(const wxString& name, const wxString& path) { UUID id; wxASSERT(!addressBookNameExists(name) && !addressBookPathExists(path)); UuidCreate(&id); CSQLiteAddressbook *pBook = new CSQLiteAddressbook(id, name, path); m_Addressbooks.push_back(pBook); m_pApplication->registerModule(pBook); saveAddressbookInfo(); }