1cea25275SFam Zheng /* 2cea25275SFam Zheng * QEMU UUID functions 3cea25275SFam Zheng * 4cea25275SFam Zheng * Copyright 2016 Red Hat, Inc. 5cea25275SFam Zheng * 6cea25275SFam Zheng * Authors: 7cea25275SFam Zheng * Fam Zheng <famz@redhat.com> 8cea25275SFam Zheng * 9cea25275SFam Zheng * This program is free software; you can redistribute it and/or modify it 10cea25275SFam Zheng * under the terms of the GNU General Public License as published by the Free 11cea25275SFam Zheng * Software Foundation; either version 2 of the License, or (at your option) 12cea25275SFam Zheng * any later version. 13cea25275SFam Zheng * 14cea25275SFam Zheng */ 15cea25275SFam Zheng 16cea25275SFam Zheng #include "qemu/osdep.h" 17cea25275SFam Zheng #include "qemu-common.h" 18cea25275SFam Zheng #include "qemu/uuid.h" 19cea25275SFam Zheng #include "qemu/bswap.h" 20cea25275SFam Zheng 21cea25275SFam Zheng void qemu_uuid_generate(QemuUUID *uuid) 22cea25275SFam Zheng { 23cea25275SFam Zheng int i; 24cea25275SFam Zheng uint32_t tmp[4]; 25cea25275SFam Zheng 26cea25275SFam Zheng QEMU_BUILD_BUG_ON(sizeof(QemuUUID) != 16); 27cea25275SFam Zheng 28cea25275SFam Zheng for (i = 0; i < 4; ++i) { 29cea25275SFam Zheng tmp[i] = g_random_int(); 30cea25275SFam Zheng } 31cea25275SFam Zheng memcpy(uuid, tmp, sizeof(tmp)); 32cea25275SFam Zheng /* Set the two most significant bits (bits 6 and 7) of the 33cea25275SFam Zheng clock_seq_hi_and_reserved to zero and one, respectively. */ 34cea25275SFam Zheng uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80; 35cea25275SFam Zheng /* Set the four most significant bits (bits 12 through 15) of the 36cea25275SFam Zheng time_hi_and_version field to the 4-bit version number. 37cea25275SFam Zheng */ 38cea25275SFam Zheng uuid->data[6] = (uuid->data[6] & 0xf) | 0x40; 39cea25275SFam Zheng } 40cea25275SFam Zheng 41cea25275SFam Zheng int qemu_uuid_is_null(const QemuUUID *uu) 42cea25275SFam Zheng { 433630be75SFam Zheng static QemuUUID null_uuid; 44cea25275SFam Zheng return memcmp(uu, &null_uuid, sizeof(QemuUUID)) == 0; 45cea25275SFam Zheng } 46cea25275SFam Zheng 47cea25275SFam Zheng void qemu_uuid_unparse(const QemuUUID *uuid, char *out) 48cea25275SFam Zheng { 49cea25275SFam Zheng const unsigned char *uu = &uuid->data[0]; 50cea25275SFam Zheng snprintf(out, UUID_FMT_LEN + 1, UUID_FMT, 51cea25275SFam Zheng uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], 52cea25275SFam Zheng uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]); 53cea25275SFam Zheng } 54cea25275SFam Zheng 55cea25275SFam Zheng char *qemu_uuid_unparse_strdup(const QemuUUID *uuid) 56cea25275SFam Zheng { 57cea25275SFam Zheng const unsigned char *uu = &uuid->data[0]; 58cea25275SFam Zheng return g_strdup_printf(UUID_FMT, 59cea25275SFam Zheng uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], 60cea25275SFam Zheng uu[7], uu[8], uu[9], uu[10], uu[11], uu[12], 61cea25275SFam Zheng uu[13], uu[14], uu[15]); 62cea25275SFam Zheng } 63cea25275SFam Zheng 64*0d6ae947SFam Zheng static bool qemu_uuid_is_valid(const char *str) 65*0d6ae947SFam Zheng { 66*0d6ae947SFam Zheng int i; 67*0d6ae947SFam Zheng 68*0d6ae947SFam Zheng for (i = 0; i < strlen(str); i++) { 69*0d6ae947SFam Zheng const char c = str[i]; 70*0d6ae947SFam Zheng if (i == 8 || i == 13 || i == 18 || i == 23) { 71*0d6ae947SFam Zheng if (str[i] != '-') { 72*0d6ae947SFam Zheng return false; 73*0d6ae947SFam Zheng } 74*0d6ae947SFam Zheng } else { 75*0d6ae947SFam Zheng if ((c >= '0' && c <= '9') || 76*0d6ae947SFam Zheng (c >= 'A' && c <= 'F') || 77*0d6ae947SFam Zheng (c >= 'a' && c <= 'f')) { 78*0d6ae947SFam Zheng continue; 79*0d6ae947SFam Zheng } 80*0d6ae947SFam Zheng return false; 81*0d6ae947SFam Zheng } 82*0d6ae947SFam Zheng } 83*0d6ae947SFam Zheng return i == 36; 84*0d6ae947SFam Zheng } 85*0d6ae947SFam Zheng 869c5ce8dbSFam Zheng int qemu_uuid_parse(const char *str, QemuUUID *uuid) 87cea25275SFam Zheng { 889c5ce8dbSFam Zheng unsigned char *uu = &uuid->data[0]; 89cea25275SFam Zheng int ret; 90cea25275SFam Zheng 91*0d6ae947SFam Zheng if (!qemu_uuid_is_valid(str)) { 92cea25275SFam Zheng return -1; 93cea25275SFam Zheng } 94cea25275SFam Zheng 959c5ce8dbSFam Zheng ret = sscanf(str, UUID_FMT, &uu[0], &uu[1], &uu[2], &uu[3], 969c5ce8dbSFam Zheng &uu[4], &uu[5], &uu[6], &uu[7], &uu[8], &uu[9], 979c5ce8dbSFam Zheng &uu[10], &uu[11], &uu[12], &uu[13], &uu[14], 989c5ce8dbSFam Zheng &uu[15]); 99cea25275SFam Zheng 100cea25275SFam Zheng if (ret != 16) { 101cea25275SFam Zheng return -1; 102cea25275SFam Zheng } 103cea25275SFam Zheng return 0; 104cea25275SFam Zheng } 105cea25275SFam Zheng 106cea25275SFam Zheng /* Swap from UUID format endian (BE) to the opposite or vice versa. 107cea25275SFam Zheng */ 108cea25275SFam Zheng void qemu_uuid_bswap(QemuUUID *uuid) 109cea25275SFam Zheng { 110cea25275SFam Zheng assert(QEMU_PTR_IS_ALIGNED(uuid, sizeof(uint32_t))); 111cea25275SFam Zheng bswap32s(&uuid->fields.time_low); 112cea25275SFam Zheng bswap16s(&uuid->fields.time_mid); 113cea25275SFam Zheng bswap16s(&uuid->fields.time_high_and_version); 114cea25275SFam Zheng } 115