1106184Smarcel/*-
2139601Smarcel * Copyright (c) 2002,2005 Marcel Moolenaar
3106184Smarcel * Copyright (c) 2002 Hiten Mahesh Pandya
4106184Smarcel * All rights reserved.
5106184Smarcel *
6106184Smarcel * Redistribution and use in source and binary forms, with or without
7106184Smarcel * modification, are permitted provided that the following conditions
8106184Smarcel * are met:
9106184Smarcel * 1. Redistributions of source code must retain the above copyright
10106184Smarcel *    notice, this list of conditions and the following disclaimer.
11106184Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12106184Smarcel *    notice, this list of conditions and the following disclaimer in the
13106184Smarcel *    documentation and/or other materials provided with the distribution.
14106184Smarcel *
15106184Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16106184Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17106184Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18106184Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19106184Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20106184Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21106184Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22106184Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23106184Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24106184Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25106184Smarcel * SUCH DAMAGE.
26106184Smarcel *
27106184Smarcel * $FreeBSD$
28106184Smarcel */
29106184Smarcel
30106184Smarcel#include <string.h>
31106184Smarcel#include <uuid.h>
32106184Smarcel
33168377Sdelphij/* A macro used to improve the readability of uuid_compare(). */
34168377Sdelphij#define DIFF_RETURN(a, b, field)	do {			\
35168377Sdelphij	if ((a)->field != (b)->field)				\
36168377Sdelphij		return (((a)->field < (b)->field) ? -1 : 1);	\
37168377Sdelphij} while (0)
38168377Sdelphij
39106184Smarcel/*
40106184Smarcel * uuid_compare() - compare two UUIDs.
41106184Smarcel * See also:
42106184Smarcel *	http://www.opengroup.org/onlinepubs/009629399/uuid_compare.htm
43106184Smarcel *
44106184Smarcel * NOTE: Either UUID can be NULL, meaning a nil UUID. nil UUIDs are smaller
45106184Smarcel *	 than any non-nil UUID.
46106184Smarcel */
47106184Smarcelint32_t
48139601Smarceluuid_compare(const uuid_t *a, const uuid_t *b, uint32_t *status)
49106184Smarcel{
50160938Sdelphij	int	res;
51106184Smarcel
52106184Smarcel	if (status != NULL)
53106184Smarcel		*status = uuid_s_ok;
54106184Smarcel
55106184Smarcel	/* Deal with NULL or equal pointers. */
56106184Smarcel	if (a == b)
57106184Smarcel		return (0);
58106184Smarcel	if (a == NULL)
59106184Smarcel		return ((uuid_is_nil(b, NULL)) ? 0 : -1);
60106184Smarcel	if (b == NULL)
61106184Smarcel		return ((uuid_is_nil(a, NULL)) ? 0 : 1);
62106184Smarcel
63168377Sdelphij	/* We have to compare the hard way. */
64168377Sdelphij	DIFF_RETURN(a, b, time_low);
65168377Sdelphij	DIFF_RETURN(a, b, time_mid);
66168377Sdelphij	DIFF_RETURN(a, b, time_hi_and_version);
67168377Sdelphij	DIFF_RETURN(a, b, clock_seq_hi_and_reserved);
68168377Sdelphij	DIFF_RETURN(a, b, clock_seq_low);
69168377Sdelphij
70118668Smarcel	res = memcmp(a->node, b->node, sizeof(a->node));
71118668Smarcel	if (res)
72118668Smarcel		return ((res < 0) ? -1 : 1);
73118668Smarcel	return (0);
74106184Smarcel}
75168377Sdelphij
76168377Sdelphij#undef DIFF_RETURN
77