uuid.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "qemu/osdep.h"
  16. #include "qemu/uuid.h"
  17. #include "qemu/bswap.h"
  18. void qemu_uuid_generate(QemuUUID *uuid)
  19. {
  20. int i;
  21. uint32_t tmp[4];
  22. QEMU_BUILD_BUG_ON(sizeof(QemuUUID) != 16);
  23. for (i = 0; i < 4; ++i) {
  24. tmp[i] = g_random_int();
  25. }
  26. memcpy(uuid, tmp, sizeof(tmp));
  27. /* Set the two most significant bits (bits 6 and 7) of the
  28. clock_seq_hi_and_reserved to zero and one, respectively. */
  29. uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80;
  30. /* Set the four most significant bits (bits 12 through 15) of the
  31. time_hi_and_version field to the 4-bit version number.
  32. */
  33. uuid->data[6] = (uuid->data[6] & 0xf) | 0x40;
  34. }
  35. int qemu_uuid_is_null(const QemuUUID *uu)
  36. {
  37. static QemuUUID null_uuid;
  38. return qemu_uuid_is_equal(uu, &null_uuid);
  39. }
  40. int qemu_uuid_is_equal(const QemuUUID *lhv, const QemuUUID *rhv)
  41. {
  42. return memcmp(lhv, rhv, sizeof(QemuUUID)) == 0;
  43. }
  44. void qemu_uuid_unparse(const QemuUUID *uuid, char *out)
  45. {
  46. const unsigned char *uu = &uuid->data[0];
  47. snprintf(out, UUID_STR_LEN, UUID_FMT,
  48. uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
  49. uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
  50. }
  51. char *qemu_uuid_unparse_strdup(const QemuUUID *uuid)
  52. {
  53. const unsigned char *uu = &uuid->data[0];
  54. return g_strdup_printf(UUID_FMT,
  55. uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6],
  56. uu[7], uu[8], uu[9], uu[10], uu[11], uu[12],
  57. uu[13], uu[14], uu[15]);
  58. }
  59. static bool qemu_uuid_is_valid(const char *str)
  60. {
  61. int i;
  62. for (i = 0; i < strlen(str); i++) {
  63. const char c = str[i];
  64. if (i == 8 || i == 13 || i == 18 || i == 23) {
  65. if (str[i] != '-') {
  66. return false;
  67. }
  68. } else {
  69. if ((c >= '0' && c <= '9') ||
  70. (c >= 'A' && c <= 'F') ||
  71. (c >= 'a' && c <= 'f')) {
  72. continue;
  73. }
  74. return false;
  75. }
  76. }
  77. return i == 36;
  78. }
  79. int qemu_uuid_parse(const char *str, QemuUUID *uuid)
  80. {
  81. unsigned char *uu = &uuid->data[0];
  82. int ret;
  83. if (!qemu_uuid_is_valid(str)) {
  84. return -1;
  85. }
  86. ret = sscanf(str, UUID_FMT, &uu[0], &uu[1], &uu[2], &uu[3],
  87. &uu[4], &uu[5], &uu[6], &uu[7], &uu[8], &uu[9],
  88. &uu[10], &uu[11], &uu[12], &uu[13], &uu[14],
  89. &uu[15]);
  90. if (ret != 16) {
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. /* Swap from UUID format endian (BE) to the opposite or vice versa.
  96. */
  97. QemuUUID qemu_uuid_bswap(QemuUUID uuid)
  98. {
  99. bswap32s(&uuid.fields.time_low);
  100. bswap16s(&uuid.fields.time_mid);
  101. bswap16s(&uuid.fields.time_high_and_version);
  102. return uuid;
  103. }
  104. /* djb2 hash algorithm */
  105. uint32_t qemu_uuid_hash(const void *uuid)
  106. {
  107. QemuUUID *qid = (QemuUUID *) uuid;
  108. uint32_t h = 5381;
  109. int i;
  110. for (i = 0; i < ARRAY_SIZE(qid->data); i++) {
  111. h = (h << 5) + h + qid->data[i];
  112. }
  113. return h;
  114. }