uuid.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <common/debug.h>
  10. #include <common/uuid.h>
  11. /* Return the hex nibble value of a char */
  12. static int8_t hex_val(char hex)
  13. {
  14. int8_t val = 0;
  15. if ((hex >= '0') && (hex <= '9')) {
  16. val = (int8_t)(hex - '0');
  17. } else if ((hex >= 'a') && (hex <= 'f')) {
  18. val = (int8_t)(hex - 'a' + 0xa);
  19. } else if ((hex >= 'A') && (hex <= 'F')) {
  20. val = (int8_t)(hex - 'A' + 0xa);
  21. } else {
  22. val = -1;
  23. }
  24. return val;
  25. }
  26. /*
  27. * Read hex_src_len hex characters from hex_src, convert to bytes and
  28. * store in buffer pointed to by dest
  29. */
  30. static int read_hex(uint8_t *dest, char *hex_src, unsigned int hex_src_len)
  31. {
  32. int8_t nibble;
  33. uint8_t byte;
  34. /*
  35. * The string length must be a multiple of 2 to represent an
  36. * exact number of bytes.
  37. */
  38. assert((hex_src_len % 2U) == 0U);
  39. for (unsigned int i = 0U; i < (hex_src_len / 2U); i++) {
  40. nibble = 0;
  41. byte = 0U;
  42. nibble = hex_val(hex_src[2U * i]);
  43. if (nibble < 0) {
  44. return -1;
  45. }
  46. byte = (uint8_t)nibble;
  47. byte <<= 4U;
  48. nibble = hex_val(hex_src[(2U * i) + 1U]);
  49. if (nibble < 0) {
  50. return -1;
  51. }
  52. byte |= (uint8_t)nibble;
  53. *dest = byte;
  54. dest++;
  55. }
  56. return 0;
  57. }
  58. /* Parse UUIDs of the form aabbccdd-eeff-4099-8877-665544332211 */
  59. int read_uuid(uint8_t *dest, char *uuid)
  60. {
  61. int err;
  62. uint8_t *dest_start = dest;
  63. /* Check that we have enough characters */
  64. if (strnlen(uuid, UUID_STRING_LENGTH) != UUID_STRING_LENGTH) {
  65. WARN("UUID string is too short\n");
  66. return -EINVAL;
  67. }
  68. /* aabbccdd */
  69. err = read_hex(dest, uuid, 8);
  70. uuid += 8;
  71. dest += 4;
  72. /* Check for '-' */
  73. err |= ((*uuid == '-') ? 0 : -1);
  74. uuid++;
  75. /* eeff */
  76. err |= read_hex(dest, uuid, 4);
  77. uuid += 4;
  78. dest += 2;
  79. /* Check for '-' */
  80. err |= ((*uuid == '-') ? 0 : -1);
  81. uuid++;
  82. /* 4099 */
  83. err |= read_hex(dest, uuid, 4);
  84. uuid += 4;
  85. dest += 2;
  86. /* Check for '-' */
  87. err |= ((*uuid == '-') ? 0 : -1);
  88. uuid++;
  89. /* 8877 */
  90. err |= read_hex(dest, uuid, 4);
  91. uuid += 4;
  92. dest += 2;
  93. /* Check for '-' */
  94. err |= ((*uuid == '-') ? 0 : -1);
  95. uuid++;
  96. /* 665544332211 */
  97. err |= read_hex(dest, uuid, 12);
  98. uuid += 12;
  99. dest += 6;
  100. if (err < 0) {
  101. WARN("Error parsing UUID\n");
  102. /* Clear the buffer on error */
  103. memset((void *)dest_start, '\0', UUID_BYTES_LENGTH * sizeof(uint8_t));
  104. return -EINVAL;
  105. }
  106. return 0;
  107. }
  108. /*
  109. * Helper function to check if 2 UUIDs match.
  110. */
  111. bool uuid_match(uint32_t *uuid1, uint32_t *uuid2)
  112. {
  113. return !memcmp(uuid1, uuid2, sizeof(uint32_t) * 4);
  114. }
  115. /*
  116. * Helper function to copy from one UUID struct to another.
  117. */
  118. void copy_uuid(uint32_t *to_uuid, uint32_t *from_uuid)
  119. {
  120. to_uuid[0] = from_uuid[0];
  121. to_uuid[1] = from_uuid[1];
  122. to_uuid[2] = from_uuid[2];
  123. to_uuid[3] = from_uuid[3];
  124. }
  125. bool is_null_uuid(uint32_t *uuid)
  126. {
  127. return (uuid[0] == 0 && uuid[1] == 0 &&
  128. uuid[2] == 0 && uuid[3] == 0);
  129. }