/*
 *
 * (c) Copyright 1989 OPEN SOFTWARE FOUNDATION, INC.
 * (c) Copyright 1989 HEWLETT-PACKARD COMPANY
 * (c) Copyright 1989 DIGITAL EQUIPMENT CORPORATION
 * To anyone who acknowledges that this file is provided "AS IS"
 * without any express or implied warranty:
 *                 permission to use, copy, modify, and distribute this
 * file for any purpose is hereby granted without fee, provided that
 * the above copyright notices and this notice appears in all source
 * code copies, and that none of the names of Open Software
 * Foundation, Inc., Hewlett-Packard Company, or Digital Equipment
 * Corporation be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  Neither Open Software Foundation, Inc., Hewlett-
 * Packard Company, nor Digital Equipment Corporation makes any
 * representations about the suitability of this software for any
 * purpose.
 *
 */

/*
 * Copyright (c) Likewise Software.  All rights Reserved.
 *
 * To anyone who acknowledges that this file is provided "AS IS"
 * without any express or implied warranty:
 *                 permission to use, copy, modify, and distribute this
 * file for any purpose is hereby granted without fee, provided that
 * the above copyright notices and this notice appears in all source
 * code copies, and that the name of Likewise Software, Inc.
 * not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  Likewise Software, Inc. does not make any
 * representations about the suitability of this software for any
 * purpose.
 */

#include "config.h"
#include <dce/dce.h>
#include <dce/rpcsts.h>
#include <dce/uuid.h>
#include <uuid/uuid.h>
#include <string.h>
#include <stdlib.h>

#define UUID_C_UUID_STRING_MAX          37

#define STATIC_ASSERT(name, expr) \
    int __static_assert##name[(expr) ? 1 : -1] ATTRIBUTE_UNUSED

#define CHECK_STRUCTURE(uuid) \
( \
    (((uuid)->clock_seq_hi_and_reserved & 0x80) == 0x00) || /* var #0 */ \
    (((uuid)->clock_seq_hi_and_reserved & 0xc0) == 0x80) || /* var #1 */ \
    (((uuid)->clock_seq_hi_and_reserved & 0xe0) == 0xc0) || /* var #2 */ \
    (((uuid)->clock_seq_hi_and_reserved & 0xe0) == 0xe0)    /* var #DAMNMICROSOFT */ \
)

#define bCHECK_STRUCTURE(uuid, status) \
{ \
    if (!CHECK_STRUCTURE (uuid)) \
    { \
        *(status) = uuid_s_bad_version; \
        return (FALSE); \
    } \
}

#define vCHECK_STRUCTURE(uuid, status) \
{ \
    if (!CHECK_STRUCTURE (uuid)) \
    { \
        *(status) = uuid_s_bad_version; \
        return; \
    } \
}

#define rCHECK_STRUCTURE(uuid, status, result) \
{ \
    if (!CHECK_STRUCTURE (uuid)) \
    { \
        *(status) = uuid_s_bad_version; \
        return (result); \
    } \
}

dce_uuid_t uuid_g_nil_uuid = { 0, 0, 0, 0, 0, {0} };
dce_uuid_t uuid_nil = { 0, 0, 0, 0, 0, {0} };

STATIC_ASSERT(uuid_types_compatible, sizeof(uuid_t) == sizeof(dce_uuid_t));

void dce_uuid_create
(
    dce_uuid_t              *uuid,
    unsigned32          *status
)
{
    uuid_t buffer;
    uuid_generate(buffer);
    memcpy(uuid, buffer, sizeof(buffer));
    *status = uuid_s_ok;
}


/*
 * U U I D _ C R E A T E _ N I L
 *
 * Create a 'nil' uuid
 */
void dce_uuid_create_nil
(
    dce_uuid_t              *uuid,
    unsigned32          *status
)
{
    uuid_t buffer;
    uuid_clear(buffer);
    memcpy(uuid, buffer, sizeof(buffer));
    *status = uuid_s_ok;
}


/*
 * U U I D _ T O _ S T R I N G
 *
 * Convert a UUID in (non-human) UUID format to STRING format
 */
void dce_uuid_to_string
(
    dce_uuid_p_t            uuid,
    unsigned_char_p_t   *uuid_string,
    unsigned32          *status
)
{
    uuid_t buffer;

    if (uuid_string == NULL)
    {
        *status = uuid_s_ok;
        return;
    }

    vCHECK_STRUCTURE (uuid, status);

    *uuid_string = (unsigned_char_p_t)malloc(UUID_C_UUID_STRING_MAX);
    if (!*uuid_string)
    {
        *status = uuid_s_no_memory;
        return;
    }
    memcpy(buffer, uuid, sizeof(buffer));
    uuid_unparse(buffer, (char*) *uuid_string);
    *status = uuid_s_ok;
}


/*
 * U U I D _  F R O M _ S T R I N G
 *
 * Convert a UUID in STRING format to a UUID in UUID format
 */
void dce_uuid_from_string
(
    unsigned_char_p_t   uuid_string,
    dce_uuid_t              *uuid,
    unsigned32          *status
)
{
    uuid_t buffer;
    if (uuid_parse((char*) uuid_string, buffer) == 0)
    {
        *status = uuid_s_ok;
        memcpy(uuid, buffer, sizeof(buffer));
    }
    else
    {
        *status = uuid_s_invalid_string_uuid;
    }
}


/*
 * U U I D _ E Q U A L
 *
 * Compare two UUIDs
 */
boolean32 dce_uuid_equal
(
    dce_uuid_p_t            uuid1,
    dce_uuid_p_t            uuid2,
    unsigned32          *status
)
{
    uuid_t buffer1, buffer2;

    bCHECK_STRUCTURE (uuid1, status);
    bCHECK_STRUCTURE (uuid2, status);

    memcpy(buffer1, uuid1, sizeof(buffer1));
    memcpy(buffer2, uuid2, sizeof(buffer2));
    *status = uuid_s_ok;
    return uuid_compare(buffer1, buffer2) == 0;
}


/*
 * U U I D _ I S _ N I L
 *
 * Return boolean true if uuid is 'nil'
 */
boolean32 dce_uuid_is_nil
(
    dce_uuid_p_t            uuid,
    unsigned32          *status
)
{
    uuid_t buffer;

    bCHECK_STRUCTURE (uuid, status);

    memcpy(buffer, uuid, sizeof(buffer));
    *status = uuid_s_ok;
    return uuid_is_null(buffer);
}


/*
 * U U I D _ C O M P A R E
 *
 * Compare two UUIDs "lexically"
 */
signed32 dce_uuid_compare
(
    dce_uuid_p_t            uuid1,
    dce_uuid_p_t            uuid2,
    unsigned32          *status
)
{
    uuid_t buffer1, buffer2;

    bCHECK_STRUCTURE (uuid1, status);
    bCHECK_STRUCTURE (uuid2, status);

    memcpy(buffer1, uuid1, sizeof(buffer1));
    memcpy(buffer2, uuid2, sizeof(buffer2));
    *status = uuid_s_ok;
    return uuid_compare(buffer1, buffer2);
}


/*
 * U U I D _ H A S H
 *
 * Create a hash value for a UUID
 */
unsigned16 dce_uuid_hash
(
    dce_uuid_p_t            uuid,
    unsigned32          *status
)
{
    short               c0, c1;
    short               x, y;
    byte_p_t            next_uuid;

    rCHECK_STRUCTURE (uuid, status, 0);

    /*
     * initialize counters
     */
    c0 = c1 = 0;
    next_uuid = (byte_p_t) uuid;

    /*
     * For speed lets unroll the following loop:
     *
     *   for (i = 0; i < UUID_K_LENGTH; i++)
     *   {
     *       c0 = c0 + *next_uuid++;
     *       c1 = c1 + c0;
     *   }
     */
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;

    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;

    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;

    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;
    c0 = c0 + *next_uuid++;
    c1 = c1 + c0;

    /*
     *  Calculate the value for "First octet" of the hash
     */
    x = -c1 % 255;
    if (x < 0)
    {
        x = x + 255;
    }

    /*
     *  Calculate the value for "second octet" of the hash
     */
    y = (c1 - c0) % 255;
    if (y < 0)
    {
        y = y + 255;
    }

    /*
     * return the pieces put together
     */
    *status = uuid_s_ok;

    return ((y * 256) + x);
}
