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