uuid.c 1.4 KB

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