uuid.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifdef _WIN32
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <windows.h>
  4. #endif // _WIN32
  5. #include "uuid.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. DEFINE_UUID(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  9. int _uuid_parse(const char *pszIn, uuid_t *uuid)
  10. {
  11. int i, nRet = 0;
  12. if(pszIn && uuid && strlen(pszIn) == _UUID_STRING_LEN)
  13. {
  14. int n[8];
  15. nRet = sscanf(pszIn,
  16. "%08x-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x",
  17. &uuid->Data1,
  18. &uuid->Data2,
  19. &uuid->Data3,
  20. &n[0], &n[1],
  21. &n[2], &n[3], &n[4], &n[5], &n[6], &n[7]);
  22. if(nRet == 11)
  23. {
  24. for(i = 0; i < 8; i++)
  25. {
  26. uuid->Data4[i] = (unsigned char)n[i];
  27. }
  28. }
  29. }
  30. return nRet == 11;
  31. }
  32. int _uuid_unparse(const uuid_t *uuid, char *pszOut, size_t nCChOut)
  33. {
  34. if(!uuid || !pszOut || nCChOut <= _UUID_STRING_LEN)
  35. return 0;
  36. return sprintf(pszOut, "%08x-%04hx-%04hx-%02hx%02hx-%02hx%02hx%02hx%02hx%02hx%02hx",
  37. uuid->Data1,
  38. uuid->Data2,
  39. uuid->Data3,
  40. uuid->Data4[0], uuid->Data4[1],
  41. uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
  42. }
  43. int _uuid_compare(const uuid_t *uuid1, const uuid_t *uuid2)
  44. {
  45. if(uuid1 && uuid2)
  46. return memcmp(uuid1, uuid2, sizeof(uuid_t));
  47. return 0;
  48. }
  49. void _uuid_copy(uuid_t *uuDest, const uuid_t *uuSrc)
  50. {
  51. if(uuDest && uuSrc)
  52. memcpy(uuDest, uuSrc, sizeof(uuid_t));
  53. }
  54. int _uuid_is_null(const uuid_t *uuid)
  55. {
  56. return !_uuid_compare(uuid, &UUID_NULL);
  57. }