uuid_time.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * uuid_time.c --- Interpret the time field from a uuid. This program
  4. * violates the UUID abstraction barrier by reaching into the guts
  5. * of a UUID and interpreting it.
  6. *
  7. * Copyright (C) 1998, 1999 Theodore Ts'o.
  8. *
  9. * %Begin-Header%
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, and the entire permission notice in its entirety,
  15. * including the disclaimer of warranties.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. The name of the author may not be used to endorse or promote
  20. * products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  26. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  29. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  33. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  34. * DAMAGE.
  35. * %End-Header%
  36. */
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <stdlib.h>
  40. #include <sys/types.h>
  41. #include <time.h>
  42. #include "uuidP.h"
  43. time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
  44. {
  45. struct uuid uuid;
  46. uint32_t high;
  47. struct timeval tv;
  48. unsigned long long clock_reg;
  49. uuid_unpack(uu, &uuid);
  50. high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
  51. clock_reg = uuid.time_low | ((unsigned long long) high << 32);
  52. clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
  53. tv.tv_sec = clock_reg / 10000000;
  54. tv.tv_usec = (clock_reg % 10000000) / 10;
  55. if (ret_tv)
  56. *ret_tv = tv;
  57. return tv.tv_sec;
  58. }
  59. int uuid_type(const uuid_t uu)
  60. {
  61. struct uuid uuid;
  62. uuid_unpack(uu, &uuid);
  63. return ((uuid.time_hi_and_version >> 12) & 0xF);
  64. }
  65. int uuid_variant(const uuid_t uu)
  66. {
  67. struct uuid uuid;
  68. int var;
  69. uuid_unpack(uu, &uuid);
  70. var = uuid.clock_seq;
  71. if ((var & 0x8000) == 0)
  72. return UUID_VARIANT_NCS;
  73. if ((var & 0x4000) == 0)
  74. return UUID_VARIANT_DCE;
  75. if ((var & 0x2000) == 0)
  76. return UUID_VARIANT_MICROSOFT;
  77. return UUID_VARIANT_OTHER;
  78. }
  79. #ifdef DEBUG
  80. static const char *variant_string(int variant)
  81. {
  82. switch (variant) {
  83. case UUID_VARIANT_NCS:
  84. return "NCS";
  85. case UUID_VARIANT_DCE:
  86. return "DCE";
  87. case UUID_VARIANT_MICROSOFT:
  88. return "Microsoft";
  89. default:
  90. return "Other";
  91. }
  92. }
  93. int
  94. main(int argc, char **argv)
  95. {
  96. uuid_t buf;
  97. time_t time_reg;
  98. struct timeval tv;
  99. int type, variant;
  100. if (argc != 2) {
  101. fprintf(stderr, "Usage: %s uuid\n", argv[0]);
  102. exit(1);
  103. }
  104. if (uuid_parse(argv[1], buf)) {
  105. fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
  106. exit(1);
  107. }
  108. variant = uuid_variant(buf);
  109. type = uuid_type(buf);
  110. time_reg = uuid_time(buf, &tv);
  111. printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
  112. if (variant != UUID_VARIANT_DCE) {
  113. printf("Warning: This program only knows how to interpret "
  114. "DCE UUIDs.\n\tThe rest of the output is likely "
  115. "to be incorrect!!\n");
  116. }
  117. printf("UUID type is %d", type);
  118. switch (type) {
  119. case 1:
  120. printf(" (time based)\n");
  121. break;
  122. case 2:
  123. printf(" (DCE)\n");
  124. break;
  125. case 3:
  126. printf(" (name-based)\n");
  127. break;
  128. case 4:
  129. printf(" (random)\n");
  130. break;
  131. default:
  132. puts("");
  133. }
  134. if (type != 1) {
  135. printf("Warning: not a time-based UUID, so UUID time "
  136. "decoding will likely not work!\n");
  137. }
  138. printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
  139. ctime(&time_reg));
  140. return 0;
  141. }
  142. #endif