uuid.c 2.9 KB

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