uuid.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2022 Pierre-Anthony Lemieux <pal@palemieux.com>
  3. * Zane van Iperen <zane@zanevaniperen.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * UUID parsing and serialization utilities.
  24. * The library treats the UUID as an opaque sequence of 16 unsigned bytes,
  25. * i.e. ignoring the internal layout of the UUID, which depends on the type
  26. * of the UUID.
  27. *
  28. * @author Pierre-Anthony Lemieux <pal@palemieux.com>
  29. * @author Zane van Iperen <zane@zanevaniperen.com>
  30. */
  31. #ifndef AVUTIL_UUID_H
  32. #define AVUTIL_UUID_H
  33. #include <stdint.h>
  34. #include <string.h>
  35. #define AV_PRI_UUID \
  36. "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-" \
  37. "%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
  38. #define AV_PRI_URN_UUID \
  39. "urn:uuid:%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-" \
  40. "%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
  41. /* AV_UUID_ARG() is used together with AV_PRI_UUID() or AV_PRI_URN_UUID
  42. * to print UUIDs, e.g.
  43. * av_log(NULL, AV_LOG_DEBUG, "UUID: " AV_PRI_UUID, AV_UUID_ARG(uuid));
  44. */
  45. #define AV_UUID_ARG(x) \
  46. (x)[ 0], (x)[ 1], (x)[ 2], (x)[ 3], \
  47. (x)[ 4], (x)[ 5], (x)[ 6], (x)[ 7], \
  48. (x)[ 8], (x)[ 9], (x)[10], (x)[11], \
  49. (x)[12], (x)[13], (x)[14], (x)[15]
  50. #define AV_UUID_LEN 16
  51. /* Binary representation of a UUID */
  52. typedef uint8_t AVUUID[AV_UUID_LEN];
  53. /**
  54. * Parses a string representation of a UUID formatted according to IETF RFC 4122
  55. * into an AVUUID. The parsing is case-insensitive. The string must be 37
  56. * characters long, including the terminating NUL character.
  57. *
  58. * Example string representation: "2fceebd0-7017-433d-bafb-d073a7116696"
  59. *
  60. * @param[in] in String representation of a UUID,
  61. * e.g. 2fceebd0-7017-433d-bafb-d073a7116696
  62. * @param[out] uu AVUUID
  63. * @return A non-zero value in case of an error.
  64. */
  65. int av_uuid_parse(const char *in, AVUUID uu);
  66. /**
  67. * Parses a URN representation of a UUID, as specified at IETF RFC 4122,
  68. * into an AVUUID. The parsing is case-insensitive. The string must be 46
  69. * characters long, including the terminating NUL character.
  70. *
  71. * Example string representation: "urn:uuid:2fceebd0-7017-433d-bafb-d073a7116696"
  72. *
  73. * @param[in] in URN UUID
  74. * @param[out] uu AVUUID
  75. * @return A non-zero value in case of an error.
  76. */
  77. int av_uuid_urn_parse(const char *in, AVUUID uu);
  78. /**
  79. * Parses a string representation of a UUID formatted according to IETF RFC 4122
  80. * into an AVUUID. The parsing is case-insensitive.
  81. *
  82. * @param[in] in_start Pointer to the first character of the string representation
  83. * @param[in] in_end Pointer to the character after the last character of the
  84. * string representation. That memory location is never
  85. * accessed. It is an error if `in_end - in_start != 36`.
  86. * @param[out] uu AVUUID
  87. * @return A non-zero value in case of an error.
  88. */
  89. int av_uuid_parse_range(const char *in_start, const char *in_end, AVUUID uu);
  90. /**
  91. * Serializes a AVUUID into a string representation according to IETF RFC 4122.
  92. * The string is lowercase and always 37 characters long, including the
  93. * terminating NUL character.
  94. *
  95. * @param[in] uu AVUUID
  96. * @param[out] out Pointer to an array of no less than 37 characters.
  97. * @return A non-zero value in case of an error.
  98. */
  99. void av_uuid_unparse(const AVUUID uu, char *out);
  100. /**
  101. * Compares two UUIDs for equality.
  102. *
  103. * @param[in] uu1 AVUUID
  104. * @param[in] uu2 AVUUID
  105. * @return Nonzero if uu1 and uu2 are identical, 0 otherwise
  106. */
  107. static inline int av_uuid_equal(const AVUUID uu1, const AVUUID uu2)
  108. {
  109. return memcmp(uu1, uu2, AV_UUID_LEN) == 0;
  110. }
  111. /**
  112. * Copies the bytes of src into dest.
  113. *
  114. * @param[out] dest AVUUID
  115. * @param[in] src AVUUID
  116. */
  117. static inline void av_uuid_copy(AVUUID dest, const AVUUID src)
  118. {
  119. memcpy(dest, src, AV_UUID_LEN);
  120. }
  121. /**
  122. * Sets a UUID to the nil UUID, i.e. a UUID with have all
  123. * its 128 bits set to zero.
  124. *
  125. * @param[in,out] uu UUID to be set to the nil UUID
  126. */
  127. static inline void av_uuid_nil(AVUUID uu)
  128. {
  129. memset(uu, 0, AV_UUID_LEN);
  130. }
  131. #endif /* AVUTIL_UUID_H */