uuid-linux.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
0N/A/* $Id$ */
2751N/A/** @file
0N/A * InnoTek Portable Runtime - UUID, LINUX.
0N/A */
0N/A
0N/A/*
2362N/A * Copyright (C) 2006 InnoTek Systemberatung GmbH
0N/A *
2362N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License as published by the Free Software Foundation,
0N/A * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
0N/A * distribution. VirtualBox OSE is distributed in the hope that it will
0N/A * be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A *
0N/A * If you received this file as part of a commercial VirtualBox
0N/A * distribution, then only the terms of your commercial VirtualBox
0N/A * license agreement apply instead of the previous paragraph.
0N/A */
2362N/A
2362N/A
2362N/A/*******************************************************************************
0N/A* Header Files *
0N/A*******************************************************************************/
0N/A#include <iprt/uuid.h>
0N/A#include <iprt/assert.h>
0N/A#include <iprt/err.h>
2751N/A#include <iprt/string.h>
0N/A
0N/A#include <uuid/uuid.h>
0N/A#include <sys/types.h> /* for BYTE_ORDER */
0N/A
0N/A
0N/A/**
0N/A * Converts the byte order of the first 32 bit UUID component and next two 16
0N/A * bit components from BIG_ENDIAN to LITTLE_ENDIAN and vice versa.
0N/A *
0N/A * @returns iprt status code.
0N/A * @param pUuid Uuid to convert.
0N/A */
2751N/A
0N/Astatic void rtuuid_convert_byteorder(PRTUUID pUuid)
0N/A{
0N/A uint8_t *pu8 = &pUuid->au8[0];
0N/A uint8_t u8;
0N/A // 32 bit component
0N/A u8 = pu8[0];
0N/A pu8[0] = pu8[3];
0N/A pu8[3] = u8;
0N/A u8 = pu8[1];
0N/A pu8[1] = pu8[2];
0N/A pu8[2] = u8;
0N/A // two 16 bit components
0N/A u8 = pu8[4];
0N/A pu8[4] = pu8[5];
0N/A pu8[5] = u8;
0N/A u8 = pu8[6];
0N/A pu8[6] = pu8[7];
0N/A pu8[7] = u8;
0N/A}
0N/A
0N/A#if BYTE_ORDER == LITTLE_ENDIAN
0N/A#define RTUUID_CONVERT_BYTEORDER(pUuid) rtuuid_convert_byteorder(pUuid)
0N/A#else
0N/A#define RTUUID_CONVERT_BYTEORDER(pUuid) do {} while (0)
0N/A#endif
0N/A
0N/A/**
0N/A * Generates a new UUID value.
0N/A *
0N/A * @returns iprt status code.
0N/A * @param pUuid Where to store generated uuid.
0N/A */
0N/ARTR3DECL(int) RTUuidCreate(PRTUUID pUuid)
0N/A{
0N/A /* check params */
0N/A if (pUuid == NULL)
0N/A {
0N/A AssertMsgFailed(("pUuid=NULL\n"));
0N/A return VERR_INVALID_PARAMETER;
0N/A }
0N/A
2751N/A uuid_generate(&pUuid->au8[0]);
0N/A RTUUID_CONVERT_BYTEORDER(pUuid);
0N/A
0N/A return VINF_SUCCESS;
0N/A}
0N/A
0N/A/**
0N/A * Makes a null UUID value.
0N/A *
0N/A * @returns iprt status code.
0N/A * @param pUuid Where to store generated null uuid.
0N/A */
0N/ARTR3DECL(int) RTUuidClear(PRTUUID pUuid)
0N/A{
0N/A /* check params */
0N/A if (pUuid == NULL)
0N/A {
0N/A AssertMsgFailed(("pUuid=NULL\n"));
0N/A return VERR_INVALID_PARAMETER;
0N/A }
0N/A
0N/A uuid_clear(&pUuid->au8[0]);
0N/A
0N/A return VINF_SUCCESS;
0N/A}
0N/A
0N/A/**
0N/A * Checks if UUID is null.
0N/A *
0N/A * @returns true if UUID is null.
0N/A * @param pUuid uuid to check.
0N/A */
0N/ARTR3DECL(int) RTUuidIsNull(PCRTUUID pUuid)
0N/A{
0N/A /* check params */
0N/A if (pUuid == NULL)
0N/A {
0N/A AssertMsgFailed(("pUuid=NULL\n"));
0N/A return true;
0N/A }
0N/A
0N/A return uuid_is_null(&pUuid->au8[0]);
0N/A}
0N/A
0N/A/**
0N/A * Compares two UUID values.
0N/A *
0N/A * @returns 0 if eq, < 0 or > 0.
0N/A * @param pUuid1 First value to compare.
0N/A * @param pUuid2 Second value to compare.
0N/A */
0N/ARTR3DECL(int) RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
0N/A{
0N/A /* check params */
0N/A if ((pUuid1 == NULL) || (pUuid2 == NULL))
0N/A {
0N/A AssertMsgFailed(("Invalid parameters\n"));
0N/A return 1;
0N/A }
0N/A
0N/A return uuid_compare(pUuid1->aUuid, pUuid2->aUuid);
0N/A}
0N/A
0N/A/**
0N/A * Converts binary UUID to its string representation.
0N/A *
0N/A * @returns iprt status code.
0N/A * @param pUuid Uuid to convert.
0N/A * @param pszString Where to store result string.
0N/A * @param cchString pszString buffer length, must be >= RTUUID_STR_LENGTH.
0N/A */
0N/ARTR3DECL(int) RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
0N/A{
0N/A /* check params */
0N/A if ((pUuid == NULL) || (pszString == NULL) || (cchString < RTUUID_STR_LENGTH))
0N/A {
0N/A AssertMsgFailed(("Invalid parameters\n"));
0N/A return VERR_INVALID_PARAMETER;
0N/A }
0N/A
0N/A static RTUUID uuid;
0N/A memcpy(&uuid, pUuid, sizeof(RTUUID));
0N/A RTUUID_CONVERT_BYTEORDER(&uuid);
0N/A uuid_unparse(uuid.aUuid, pszString);
0N/A
0N/A return VINF_SUCCESS;
0N/A}
0N/A
0N/A/**
0N/A * Converts UUID from its string representation to binary format.
0N/A *
0N/A * @returns iprt status code.
0N/A * @param pUuid Where to store result Uuid.
0N/A * @param pszString String with UUID text data.
0N/A */
0N/ARTR3DECL(int) RTUuidFromStr(PRTUUID pUuid, const char *pszString)
0N/A{
0N/A /* check params */
0N/A if ((pUuid == NULL) || (pszString == NULL))
0N/A {
0N/A AssertMsgFailed(("Invalid parameters\n"));
0N/A return VERR_INVALID_PARAMETER;
0N/A }
0N/A
0N/A if (uuid_parse(pszString, &pUuid->au8[0]) == 0)
0N/A {
0N/A RTUUID_CONVERT_BYTEORDER(pUuid);
0N/A return VINF_SUCCESS;
0N/A }
0N/A
0N/A return VERR_INVALID_UUID_FORMAT;
0N/A}
0N/A