| 1 | /* |
| 2 | * QEMU UUID functions |
| 3 | * |
| 4 | * Copyright 2016 Red Hat, Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Fam Zheng <famz@redhat.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #ifndef QEMU_UUID_H |
| 17 | #define QEMU_UUID_H |
| 18 | |
| 19 | |
| 20 | /* Version 4 UUID (pseudo random numbers), RFC4122 4.4. */ |
| 21 | |
| 22 | typedef struct { |
| 23 | union { |
| 24 | unsigned char data[16]; |
| 25 | struct { |
| 26 | /* Generated in BE endian, can be swapped with qemu_uuid_bswap. */ |
| 27 | uint32_t time_low; |
| 28 | uint16_t time_mid; |
| 29 | uint16_t time_high_and_version; |
| 30 | uint8_t clock_seq_and_reserved; |
| 31 | uint8_t clock_seq_low; |
| 32 | uint8_t node[6]; |
| 33 | } fields; |
| 34 | }; |
| 35 | } QemuUUID; |
| 36 | |
| 37 | /** |
| 38 | * UUID_LE - converts the fields of UUID to little-endian array, |
| 39 | * each of parameters is the filed of UUID. |
| 40 | * |
| 41 | * @time_low: The low field of the timestamp |
| 42 | * @time_mid: The middle field of the timestamp |
| 43 | * @time_hi_and_version: The high field of the timestamp |
| 44 | * multiplexed with the version number |
| 45 | * @clock_seq_hi_and_reserved: The high field of the clock |
| 46 | * sequence multiplexed with the variant |
| 47 | * @clock_seq_low: The low field of the clock sequence |
| 48 | * @node0: The spatially unique node0 identifier |
| 49 | * @node1: The spatially unique node1 identifier |
| 50 | * @node2: The spatially unique node2 identifier |
| 51 | * @node3: The spatially unique node3 identifier |
| 52 | * @node4: The spatially unique node4 identifier |
| 53 | * @node5: The spatially unique node5 identifier |
| 54 | */ |
| 55 | #define UUID_LE(time_low, time_mid, time_hi_and_version, \ |
| 56 | clock_seq_hi_and_reserved, clock_seq_low, node0, node1, node2, \ |
| 57 | node3, node4, node5) \ |
| 58 | { (time_low) & 0xff, ((time_low) >> 8) & 0xff, ((time_low) >> 16) & 0xff, \ |
| 59 | ((time_low) >> 24) & 0xff, (time_mid) & 0xff, ((time_mid) >> 8) & 0xff, \ |
| 60 | (time_hi_and_version) & 0xff, ((time_hi_and_version) >> 8) & 0xff, \ |
| 61 | (clock_seq_hi_and_reserved), (clock_seq_low), (node0), (node1), (node2),\ |
| 62 | (node3), (node4), (node5) } |
| 63 | |
| 64 | /* Normal (network byte order) UUID */ |
| 65 | #define UUID(time_low, time_mid, time_hi_and_version, \ |
| 66 | clock_seq_hi_and_reserved, clock_seq_low, node0, node1, node2, \ |
| 67 | node3, node4, node5) \ |
| 68 | { ((time_low) >> 24) & 0xff, ((time_low) >> 16) & 0xff, \ |
| 69 | ((time_low) >> 8) & 0xff, (time_low) & 0xff, \ |
| 70 | ((time_mid) >> 8) & 0xff, (time_mid) & 0xff, \ |
| 71 | ((time_hi_and_version) >> 8) & 0xff, (time_hi_and_version) & 0xff, \ |
| 72 | (clock_seq_hi_and_reserved), (clock_seq_low), \ |
| 73 | (node0), (node1), (node2), (node3), (node4), (node5) \ |
| 74 | } |
| 75 | |
| 76 | #define UUID_FMT "%02hhx%02hhx%02hhx%02hhx-" \ |
| 77 | "%02hhx%02hhx-%02hhx%02hhx-" \ |
| 78 | "%02hhx%02hhx-" \ |
| 79 | "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" |
| 80 | |
| 81 | #define UUID_NONE "00000000-0000-0000-0000-000000000000" |
| 82 | QEMU_BUILD_BUG_ON(sizeof(UUID_NONE) - 1 != 36); |
| 83 | |
| 84 | #define UUID_STR_LEN sizeof(UUID_NONE) |
| 85 | |
| 86 | void qemu_uuid_generate(QemuUUID *out); |
| 87 | |
| 88 | int qemu_uuid_is_null(const QemuUUID *uu); |
| 89 | |
| 90 | int qemu_uuid_is_equal(const QemuUUID *lhv, const QemuUUID *rhv); |
| 91 | |
| 92 | void qemu_uuid_unparse(const QemuUUID *uuid, char *out); |
| 93 | |
| 94 | char *qemu_uuid_unparse_strdup(const QemuUUID *uuid); |
| 95 | |
| 96 | int qemu_uuid_parse(const char *str, QemuUUID *uuid); |
| 97 | |
| 98 | QemuUUID qemu_uuid_bswap(QemuUUID uuid); |
| 99 | |
| 100 | uint32_t qemu_uuid_hash(const void *uuid); |
| 101 | |
| 102 | #endif |