SDL_guid.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // convert the guid to a printable string
  20. void SDL_GUIDToString(SDL_GUID guid, char *pszGUID, int cbGUID)
  21. {
  22. static const char k_rgchHexToASCII[] = "0123456789abcdef";
  23. int i;
  24. if ((!pszGUID) || (cbGUID <= 0)) {
  25. return;
  26. }
  27. for (i = 0; i < sizeof(guid.data) && i < (cbGUID - 1) / 2; i++) {
  28. // each input byte writes 2 ascii chars, and might write a null byte.
  29. // If we don't have room for next input byte, stop
  30. unsigned char c = guid.data[i];
  31. *pszGUID++ = k_rgchHexToASCII[c >> 4];
  32. *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
  33. }
  34. *pszGUID = '\0';
  35. }
  36. /*-----------------------------------------------------------------------------
  37. * Purpose: Returns the 4 bit nibble for a hex character
  38. * Input : c -
  39. * Output : unsigned char
  40. *-----------------------------------------------------------------------------*/
  41. static unsigned char nibble(unsigned char c)
  42. {
  43. if ((c >= '0') && (c <= '9')) {
  44. return c - '0';
  45. }
  46. if ((c >= 'A') && (c <= 'F')) {
  47. return c - 'A' + 0x0a;
  48. }
  49. if ((c >= 'a') && (c <= 'f')) {
  50. return c - 'a' + 0x0a;
  51. }
  52. // received an invalid character, and no real way to return an error
  53. // AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c);
  54. return 0;
  55. }
  56. // convert the string version of a guid to the struct
  57. SDL_GUID SDL_StringToGUID(const char *pchGUID)
  58. {
  59. SDL_GUID guid;
  60. int maxoutputbytes = sizeof(guid);
  61. size_t len = SDL_strlen(pchGUID);
  62. Uint8 *p;
  63. size_t i;
  64. // Make sure it's even
  65. len = (len) & ~0x1;
  66. SDL_memset(&guid, 0x00, sizeof(guid));
  67. p = (Uint8 *)&guid;
  68. for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i += 2, p++) {
  69. *p = (nibble((unsigned char)pchGUID[i]) << 4) | nibble((unsigned char)pchGUID[i + 1]);
  70. }
  71. return guid;
  72. }