uuid.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Universally Unique IDentifier (UUID)
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "uuid.h"
  11. int uuid_str2bin(const char *str, u8 *bin)
  12. {
  13. const char *pos;
  14. u8 *opos;
  15. pos = str;
  16. opos = bin;
  17. if (hexstr2bin(pos, opos, 4))
  18. return -1;
  19. pos += 8;
  20. opos += 4;
  21. if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
  22. return -1;
  23. pos += 4;
  24. opos += 2;
  25. if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
  26. return -1;
  27. pos += 4;
  28. opos += 2;
  29. if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
  30. return -1;
  31. pos += 4;
  32. opos += 2;
  33. if (*pos++ != '-' || hexstr2bin(pos, opos, 6))
  34. return -1;
  35. return 0;
  36. }
  37. int uuid_bin2str(const u8 *bin, char *str, size_t max_len)
  38. {
  39. int len;
  40. len = os_snprintf(str, max_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
  41. "%02x%02x-%02x%02x%02x%02x%02x%02x",
  42. bin[0], bin[1], bin[2], bin[3],
  43. bin[4], bin[5], bin[6], bin[7],
  44. bin[8], bin[9], bin[10], bin[11],
  45. bin[12], bin[13], bin[14], bin[15]);
  46. if (os_snprintf_error(max_len, len))
  47. return -1;
  48. return 0;
  49. }
  50. int is_nil_uuid(const u8 *uuid)
  51. {
  52. int i;
  53. for (i = 0; i < UUID_LEN; i++)
  54. if (uuid[i])
  55. return 0;
  56. return 1;
  57. }