1 /* $OpenBSD: uuid_compare.c,v 1.3 2015/09/10 18:13:46 guenther Exp $ */ 2 /* $NetBSD: uuid_compare.c,v 1.2 2008/04/23 07:52:32 plunky Exp $ */ 3 4 /*- 5 * Copyright (c) 2002,2005 Marcel Moolenaar 6 * Copyright (c) 2002 Hiten Mahesh Pandya 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD: src/lib/libc/uuid/uuid_compare.c,v 1.6 2007/04/05 02:07:33 delphij Exp $ 31 */ 32 33 #include <string.h> 34 #include <uuid.h> 35 36 /* A macro used to improve the readability of uuid_compare(). */ 37 #define DIFF_RETURN(a, b, field) do { \ 38 if ((a)->field != (b)->field) \ 39 return (((a)->field < (b)->field) ? -1 : 1); \ 40 } while (0) 41 42 /* 43 * uuid_compare() - compare two UUIDs. 44 * See also: 45 * http://www.opengroup.org/onlinepubs/009629399/uuid_compare.htm 46 * 47 * NOTE: Either UUID can be NULL, meaning a nil UUID. nil UUIDs are smaller 48 * than any non-nil UUID. 49 */ 50 int32_t 51 uuid_compare(const uuid_t *a, const uuid_t *b, uint32_t *status) 52 { 53 int res; 54 55 if (status != NULL) 56 *status = uuid_s_ok; 57 58 /* Deal with NULL or equal pointers. */ 59 if (a == b) 60 return (0); 61 if (a == NULL) 62 return ((uuid_is_nil(b, NULL)) ? 0 : -1); 63 if (b == NULL) 64 return ((uuid_is_nil(a, NULL)) ? 0 : 1); 65 66 /* We have to compare the hard way. */ 67 DIFF_RETURN(a, b, time_low); 68 DIFF_RETURN(a, b, time_mid); 69 DIFF_RETURN(a, b, time_hi_and_version); 70 DIFF_RETURN(a, b, clock_seq_hi_and_reserved); 71 DIFF_RETURN(a, b, clock_seq_low); 72 73 res = memcmp(a->node, b->node, sizeof(a->node)); 74 if (res) 75 return ((res < 0) ? -1 : 1); 76 return (0); 77 } 78 79 #undef DIFF_RETURN 80