2003-08-06 18:30:49 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: JRD Access Method
|
2003-12-05 11:35:47 +01:00
|
|
|
* MODULE: guid.cpp
|
2003-08-06 18:30:49 +02:00
|
|
|
* DESCRIPTION: Portable GUID (win32)
|
|
|
|
*
|
2004-06-30 03:45:18 +02:00
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
2003-08-06 18:30:49 +02:00
|
|
|
*
|
2004-06-30 03:45:18 +02:00
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
2003-08-06 18:30:49 +02:00
|
|
|
*
|
2004-06-30 03:45:18 +02:00
|
|
|
* The Original Code was created by Nickolay Samofatov
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
2003-08-06 18:30:49 +02:00
|
|
|
*
|
2004-06-30 03:45:18 +02:00
|
|
|
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*
|
|
|
|
*
|
2003-08-06 18:30:49 +02:00
|
|
|
*/
|
|
|
|
|
2004-11-16 09:34:35 +01:00
|
|
|
// minimum win32 version: win95 / winnt4
|
2004-11-14 19:03:11 +01:00
|
|
|
#define _WIN32_WINNT 0x0400
|
|
|
|
|
2003-08-06 20:06:22 +02:00
|
|
|
#include <windows.h>
|
2004-12-02 13:15:33 +01:00
|
|
|
#include <wincrypt.h>
|
2003-08-06 20:06:22 +02:00
|
|
|
#include <objbase.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2006-07-27 14:19:30 +02:00
|
|
|
#include "firebird.h"
|
2003-08-06 18:30:49 +02:00
|
|
|
#include "../jrd/os/guid.h"
|
|
|
|
#include "fb_exception.h"
|
|
|
|
|
2004-11-14 19:03:11 +01:00
|
|
|
void GenerateRandomBytes(void* buffer, size_t size)
|
|
|
|
{
|
|
|
|
HCRYPTPROV hProv;
|
|
|
|
|
|
|
|
// Acquire crypto-provider context handle.
|
2006-12-20 15:43:23 +01:00
|
|
|
if (! CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
|
2004-11-14 19:03:11 +01:00
|
|
|
{
|
|
|
|
if (GetLastError() == NTE_BAD_KEYSET)
|
|
|
|
{
|
|
|
|
// A common cause of this error is that the key container does not exist.
|
|
|
|
// To create a key container, call CryptAcquireContext
|
|
|
|
// using the CRYPT_NEWKEYSET flag.
|
2006-12-20 15:43:23 +01:00
|
|
|
if (! CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
|
2004-11-14 19:03:11 +01:00
|
|
|
{
|
|
|
|
Firebird::system_call_failed::raise("CryptAcquireContext");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Firebird::system_call_failed::raise("CryptAcquireContext");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-16 09:34:35 +01:00
|
|
|
if (! CryptGenRandom(hProv, size, static_cast<UCHAR*>(buffer)))
|
2004-11-14 19:03:11 +01:00
|
|
|
{
|
|
|
|
Firebird::system_call_failed::raise("CryptGenRandom");
|
|
|
|
}
|
|
|
|
CryptReleaseContext(hProv, 0);
|
|
|
|
}
|
|
|
|
|
2003-12-05 11:35:47 +01:00
|
|
|
void GenerateGuid(FB_GUID* guid) {
|
2004-03-07 08:58:55 +01:00
|
|
|
const HRESULT error = CoCreateGuid((GUID*)guid);
|
2004-03-01 04:35:23 +01:00
|
|
|
if (!SUCCEEDED(error))
|
|
|
|
Firebird::system_call_failed::raise("CoCreateGuid", error);
|
2003-08-06 18:30:49 +02:00
|
|
|
}
|
|
|
|
|
2003-12-05 11:35:47 +01:00
|
|
|
void GuidToString(char* buffer, const FB_GUID* guid) {
|
2003-08-06 20:06:22 +02:00
|
|
|
sprintf(buffer, "{%04hX%04hX-%04hX-%04hX-%04hX-%04hX%04hX%04hX}",
|
|
|
|
guid->data[0], guid->data[1], guid->data[2], guid->data[3],
|
|
|
|
guid->data[4], guid->data[5], guid->data[6], guid->data[7]);
|
2003-08-06 18:30:49 +02:00
|
|
|
}
|
|
|
|
|
2003-12-05 11:35:47 +01:00
|
|
|
void StringToGuid(FB_GUID* guid, const char* buffer) {
|
2003-08-06 20:06:22 +02:00
|
|
|
sscanf(buffer, "{%04hX%04hX-%04hX-%04hX-%04hX-%04hX%04hX%04hX}",
|
|
|
|
&guid->data[0], &guid->data[1], &guid->data[2], &guid->data[3],
|
|
|
|
&guid->data[4], &guid->data[5], &guid->data[6], &guid->data[7]);
|
2003-08-06 18:30:49 +02:00
|
|
|
}
|
2003-12-05 11:35:47 +01:00
|
|
|
|