3
0

uuid.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * uuid.c -- utility routines for manipulating UUID's.
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "../ext2fs/ext2_types.h"
  8. #include "e2p.h"
  9. struct uuid {
  10. __u32 time_low;
  11. __u16 time_mid;
  12. __u16 time_hi_and_version;
  13. __u16 clock_seq;
  14. __u8 node[6];
  15. };
  16. /* Returns 1 if the uuid is the NULL uuid */
  17. int e2p_is_null_uuid(void *uu)
  18. {
  19. __u8 *cp;
  20. int i;
  21. for (i=0, cp = uu; i < 16; i++)
  22. if (*cp)
  23. return 0;
  24. return 1;
  25. }
  26. static void e2p_unpack_uuid(void *in, struct uuid *uu)
  27. {
  28. __u8 *ptr = in;
  29. __u32 tmp;
  30. tmp = *ptr++;
  31. tmp = (tmp << 8) | *ptr++;
  32. tmp = (tmp << 8) | *ptr++;
  33. tmp = (tmp << 8) | *ptr++;
  34. uu->time_low = tmp;
  35. tmp = *ptr++;
  36. tmp = (tmp << 8) | *ptr++;
  37. uu->time_mid = tmp;
  38. tmp = *ptr++;
  39. tmp = (tmp << 8) | *ptr++;
  40. uu->time_hi_and_version = tmp;
  41. tmp = *ptr++;
  42. tmp = (tmp << 8) | *ptr++;
  43. uu->clock_seq = tmp;
  44. memcpy(uu->node, ptr, 6);
  45. }
  46. void e2p_uuid_to_str(void *uu, char *out)
  47. {
  48. struct uuid uuid;
  49. e2p_unpack_uuid(uu, &uuid);
  50. sprintf(out,
  51. "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  52. uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
  53. uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
  54. uuid.node[0], uuid.node[1], uuid.node[2],
  55. uuid.node[3], uuid.node[4], uuid.node[5]);
  56. }
  57. const char *e2p_uuid2str(void *uu)
  58. {
  59. static char buf[80];
  60. if (e2p_is_null_uuid(uu))
  61. return "<none>";
  62. e2p_uuid_to_str(uu, buf);
  63. return buf;
  64. }