1 1.4 riastrad /* $NetBSD: uuid.h,v 1.4 2021/12/19 11:38:04 riastradh Exp $ */ 2 1.2 riastrad 3 1.2 riastrad /*- 4 1.2 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 1.2 riastrad * All rights reserved. 6 1.2 riastrad * 7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.2 riastrad * by Taylor R. Campbell. 9 1.2 riastrad * 10 1.2 riastrad * Redistribution and use in source and binary forms, with or without 11 1.2 riastrad * modification, are permitted provided that the following conditions 12 1.2 riastrad * are met: 13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.2 riastrad * notice, this list of conditions and the following disclaimer. 15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.2 riastrad * documentation and/or other materials provided with the distribution. 18 1.2 riastrad * 19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.2 riastrad */ 31 1.2 riastrad 32 1.2 riastrad #ifndef _LINUX_UUID_H_ 33 1.2 riastrad #define _LINUX_UUID_H_ 34 1.2 riastrad 35 1.4 riastrad typedef struct { 36 1.4 riastrad unsigned char guid_bytes[16]; 37 1.4 riastrad } guid_t; 38 1.4 riastrad 39 1.4 riastrad #define GUID_INIT(x, y, z, v0, v1, v2, v3, v4, v5, v6, v7) ((guid_t) \ 40 1.4 riastrad { \ 41 1.4 riastrad .guid_bytes = { \ 42 1.4 riastrad [0] = (x) & 0xff, \ 43 1.4 riastrad [1] = ((x) >> 8) & 0xff, \ 44 1.4 riastrad [2] = ((x) >> 16) & 0xff, \ 45 1.4 riastrad [3] = ((x) >> 24) & 0xff, \ 46 1.4 riastrad [4] = (y) & 0xff, \ 47 1.4 riastrad [5] = ((y) >> 8) & 0xff, \ 48 1.4 riastrad [6] = (z) & 0xff, \ 49 1.4 riastrad [7] = ((z) >> 8) & 0xff, \ 50 1.4 riastrad [8] = (v0), \ 51 1.4 riastrad [9] = (v1), \ 52 1.4 riastrad [10] = (v2), \ 53 1.4 riastrad [11] = (v3), \ 54 1.4 riastrad [12] = (v4), \ 55 1.4 riastrad [13] = (v5), \ 56 1.4 riastrad [14] = (v6), \ 57 1.4 riastrad [15] = (v7), \ 58 1.4 riastrad } \ 59 1.4 riastrad }) 60 1.4 riastrad 61 1.2 riastrad #define UUID_STRING_LEN 36 62 1.2 riastrad 63 1.3 riastrad static inline int 64 1.3 riastrad uuid_is_valid(const char uuid[static 36]) 65 1.3 riastrad { 66 1.3 riastrad unsigned i; 67 1.3 riastrad 68 1.3 riastrad for (i = 0; i < 36; i++) { 69 1.3 riastrad switch (i) { 70 1.3 riastrad case 8: /* xxxxxxxx[-]xxxx-xxxx-xxxx-xxxxxxxxxxxx */ 71 1.3 riastrad case 12 + 1: /* xxxxxxxx-xxxx[-]xxxx-xxxx-xxxxxxxxxxxx */ 72 1.3 riastrad case 16 + 2: /* xxxxxxxx-xxxx-xxxx[-]xxxx-xxxxxxxxxxxx */ 73 1.3 riastrad case 20 + 3: /* xxxxxxxx-xxxx-xxxx-xxxx[-]xxxxxxxxxxxx */ 74 1.3 riastrad if (uuid[i] == '-') 75 1.3 riastrad continue; 76 1.3 riastrad return 0; 77 1.3 riastrad default: 78 1.3 riastrad if ('0' <= uuid[i] && uuid[i] <= '9') 79 1.3 riastrad continue; 80 1.3 riastrad if ('a' <= uuid[i] && uuid[i] <= 'f') 81 1.3 riastrad continue; 82 1.3 riastrad if ('A' <= uuid[i] && uuid[i] <= 'F') 83 1.3 riastrad continue; 84 1.3 riastrad return 0; 85 1.3 riastrad } 86 1.3 riastrad } 87 1.3 riastrad 88 1.3 riastrad return 1; 89 1.3 riastrad } 90 1.3 riastrad 91 1.2 riastrad #endif /* _LINUX_UUID_H_ */ 92