8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 20:43:03 +01:00
firebird-mirror/src/jrd/os/posix/guid.cpp

63 lines
2.0 KiB
C++
Raw Normal View History

/*
* PROGRAM: JRD Access Method
* MODULE: guid.cpp
2003-09-10 17:09:18 +02:00
* DESCRIPTION: Portable GUID (posix)
*
* 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.
*
* 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.
*
* The Original Code was created by Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
* $Id: guid.cpp,v 1.6 2004-06-30 01:41:58 skidder Exp $
*
*/
#include "../jrd/os/guid.h"
#include "firebird.h"
#include "fb_exception.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
void GenerateGuid(FB_GUID* guid) {
// do not use /dev/random because it may return lesser data than we need.
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
Firebird::system_call_failed::raise("open");
if (read(fd, guid, sizeof(FB_GUID)) != sizeof(FB_GUID))
Firebird::system_call_failed::raise("read");
close(fd);
}
void GuidToString(char* buffer, const FB_GUID* guid) {
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]);
}
void StringToGuid(FB_GUID* guid, const char* buffer) {
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]);
}