]> git.proxmox.com Git - mirror_ovs.git/blob - lib/uuid.h
Don't go beyond buffer length when printing descriptions
[mirror_ovs.git] / lib / uuid.h
1 /* Copyright (c) 2008, 2009, 2010 Nicira Networks
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef UUID_H
17 #define UUID_H 1
18
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include "util.h"
23
24 #define UUID_BIT 128 /* Number of bits in a UUID. */
25 #define UUID_OCTET (UUID_BIT / 8) /* Number of bytes in a UUID. */
26
27 /* A Universally Unique IDentifier (UUID) compliant with RFC 4122.
28 *
29 * Each of the parts is stored in host byte order, but the parts themselves are
30 * ordered from left to right. That is, (parts[0] >> 24) is the first 8 bits
31 * of the UUID when output in the standard form, and (parts[3] & 0xff) is the
32 * final 8 bits. */
33 struct uuid {
34 uint32_t parts[4];
35 };
36 BUILD_ASSERT_DECL(sizeof(struct uuid) == UUID_OCTET);
37
38 /* Formats a UUID as a string, in the conventional format.
39 *
40 * Example:
41 * struct uuid uuid = ...;
42 * printf("This UUID is "UUID_FMT"\n", UUID_ARGS(&uuid));
43 *
44 */
45 #define UUID_LEN 36
46 #define UUID_FMT "%08x-%04x-%04x-%04x-%04x%08x"
47 #define UUID_ARGS(UUID) \
48 ((unsigned int) ((UUID)->parts[0])), \
49 ((unsigned int) ((UUID)->parts[1] >> 16)), \
50 ((unsigned int) ((UUID)->parts[1] & 0xffff)), \
51 ((unsigned int) ((UUID)->parts[2] >> 16)), \
52 ((unsigned int) ((UUID)->parts[2] & 0xffff)), \
53 ((unsigned int) ((UUID)->parts[3]))
54
55 /* Returns a hash value for 'uuid'. This hash value is the same regardless of
56 * whether we are running on a 32-bit or 64-bit or big-endian or little-endian
57 * architecture. */
58 static inline size_t
59 uuid_hash(const struct uuid *uuid)
60 {
61 return uuid->parts[0];
62 }
63
64 /* Returns true if 'a == b', false otherwise. */
65 static inline bool
66 uuid_equals(const struct uuid *a, const struct uuid *b)
67 {
68 return (a->parts[0] == b->parts[0]
69 && a->parts[1] == b->parts[1]
70 && a->parts[2] == b->parts[2]
71 && a->parts[3] == b->parts[3]);
72 }
73
74 void uuid_init(void);
75 void uuid_generate(struct uuid *);
76 void uuid_zero(struct uuid *);
77 bool uuid_is_zero(const struct uuid *);
78 int uuid_compare_3way(const struct uuid *, const struct uuid *);
79 bool uuid_from_string(struct uuid *, const char *);
80 bool uuid_from_string_prefix(struct uuid *, const char *);
81
82 #endif /* vswitchd/cfgdb.h */