165393a86SMarcel Moolenaar /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
41d1b40feSMarcel Moolenaar * Copyright (c) 2002,2005 Marcel Moolenaar
565393a86SMarcel Moolenaar * Copyright (c) 2002 Hiten Mahesh Pandya
665393a86SMarcel Moolenaar * All rights reserved.
765393a86SMarcel Moolenaar *
865393a86SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without
965393a86SMarcel Moolenaar * modification, are permitted provided that the following conditions
1065393a86SMarcel Moolenaar * are met:
1165393a86SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright
1265393a86SMarcel Moolenaar * notice, this list of conditions and the following disclaimer.
1365393a86SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright
1465393a86SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the
1565393a86SMarcel Moolenaar * documentation and/or other materials provided with the distribution.
1665393a86SMarcel Moolenaar *
1765393a86SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1865393a86SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1965393a86SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2065393a86SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2165393a86SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2265393a86SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2365393a86SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2465393a86SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2565393a86SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2665393a86SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2765393a86SMarcel Moolenaar * SUCH DAMAGE.
2865393a86SMarcel Moolenaar */
2965393a86SMarcel Moolenaar
3065393a86SMarcel Moolenaar #include <string.h>
3165393a86SMarcel Moolenaar #include <uuid.h>
3265393a86SMarcel Moolenaar
3377fbf8f2SXin LI /* A macro used to improve the readability of uuid_compare(). */
3477fbf8f2SXin LI #define DIFF_RETURN(a, b, field) do { \
3577fbf8f2SXin LI if ((a)->field != (b)->field) \
3677fbf8f2SXin LI return (((a)->field < (b)->field) ? -1 : 1); \
3777fbf8f2SXin LI } while (0)
3877fbf8f2SXin LI
3965393a86SMarcel Moolenaar /*
4065393a86SMarcel Moolenaar * uuid_compare() - compare two UUIDs.
4165393a86SMarcel Moolenaar * See also:
4265393a86SMarcel Moolenaar * http://www.opengroup.org/onlinepubs/009629399/uuid_compare.htm
4365393a86SMarcel Moolenaar *
4465393a86SMarcel Moolenaar * NOTE: Either UUID can be NULL, meaning a nil UUID. nil UUIDs are smaller
4565393a86SMarcel Moolenaar * than any non-nil UUID.
4665393a86SMarcel Moolenaar */
4765393a86SMarcel Moolenaar int32_t
uuid_compare(const uuid_t * a,const uuid_t * b,uint32_t * status)481d1b40feSMarcel Moolenaar uuid_compare(const uuid_t *a, const uuid_t *b, uint32_t *status)
4965393a86SMarcel Moolenaar {
5065393a86SMarcel Moolenaar int res;
5165393a86SMarcel Moolenaar
5265393a86SMarcel Moolenaar if (status != NULL)
5365393a86SMarcel Moolenaar *status = uuid_s_ok;
5465393a86SMarcel Moolenaar
5565393a86SMarcel Moolenaar /* Deal with NULL or equal pointers. */
5665393a86SMarcel Moolenaar if (a == b)
5765393a86SMarcel Moolenaar return (0);
5865393a86SMarcel Moolenaar if (a == NULL)
5965393a86SMarcel Moolenaar return ((uuid_is_nil(b, NULL)) ? 0 : -1);
6065393a86SMarcel Moolenaar if (b == NULL)
6165393a86SMarcel Moolenaar return ((uuid_is_nil(a, NULL)) ? 0 : 1);
6265393a86SMarcel Moolenaar
6377fbf8f2SXin LI /* We have to compare the hard way. */
6477fbf8f2SXin LI DIFF_RETURN(a, b, time_low);
6577fbf8f2SXin LI DIFF_RETURN(a, b, time_mid);
6677fbf8f2SXin LI DIFF_RETURN(a, b, time_hi_and_version);
6777fbf8f2SXin LI DIFF_RETURN(a, b, clock_seq_hi_and_reserved);
6877fbf8f2SXin LI DIFF_RETURN(a, b, clock_seq_low);
6977fbf8f2SXin LI
703031a431SMarcel Moolenaar res = memcmp(a->node, b->node, sizeof(a->node));
713031a431SMarcel Moolenaar if (res)
723031a431SMarcel Moolenaar return ((res < 0) ? -1 : 1);
733031a431SMarcel Moolenaar return (0);
7465393a86SMarcel Moolenaar }
7577fbf8f2SXin LI
7677fbf8f2SXin LI #undef DIFF_RETURN
77