kern_uuid.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* $OpenBSD: kern_uuid.c,v 1.2 2014/08/31 20:15:54 miod Exp $ */
  2. /* $NetBSD: kern_uuid.c,v 1.18 2011/11/19 22:51:25 tls Exp $ */
  3. /*-
  4. * Copyright (c) 2002 Marcel Moolenaar
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_uuid.c,v 1.7 2004/01/12
  29. 13:34:11 rse Exp $
  30. */
  31. /*-
  32. * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
  33. * All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  45. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  48. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54. * SUCH DAMAGE.
  55. *
  56. * $FreeBSD: src/sys/sys/endian.h,v 1.7 2010/05/20 06:16:13 phk Exp $
  57. */
  58. #include <sys/param.h>
  59. #include <sys/endian.h>
  60. #include <sys/systm.h>
  61. #include <sys/uuid.h>
  62. /*
  63. * For a description of UUIDs see RFC 4122.
  64. */
  65. struct uuid_private {
  66. struct {
  67. uint32_t low;
  68. uint16_t mid;
  69. uint16_t hi;
  70. } time;
  71. uint16_t seq;
  72. uint16_t node[_UUID_NODE_LEN>>1];
  73. };
  74. #ifdef DEBUG
  75. int
  76. uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
  77. {
  78. const struct uuid_private *id;
  79. int cnt;
  80. id = (const struct uuid_private *)uuid;
  81. cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x",
  82. id->time.low, id->time.mid, id->time.hi, betoh16(id->seq),
  83. betoh16(id->node[0]), betoh16(id->node[1]), betoh16(id->node[2]));
  84. return (cnt);
  85. }
  86. int
  87. uuid_printf(const struct uuid *uuid)
  88. {
  89. char buf[_UUID_BUF_LEN];
  90. (void) uuid_snprintf(buf, sizeof(buf), uuid);
  91. printf("%s", buf);
  92. return (0);
  93. }
  94. #endif
  95. /*
  96. * Encode/Decode UUID into octet-stream.
  97. * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
  98. *
  99. * 0 1 2 3
  100. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  101. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  102. * | time_low |
  103. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  104. * | time_mid | time_hi_and_version |
  105. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  106. * |clk_seq_hi_res | clk_seq_low | node (0-1) |
  107. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  108. * | node (2-5) |
  109. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  110. */
  111. /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
  112. static __inline uint16_t
  113. be16dec(const void *pp)
  114. {
  115. uint8_t const *p = (uint8_t const *)pp;
  116. return ((p[0] << 8) | p[1]);
  117. }
  118. static __inline uint32_t
  119. be32dec(const void *pp)
  120. {
  121. uint8_t const *p = (uint8_t const *)pp;
  122. return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
  123. }
  124. static __inline uint16_t
  125. le16dec(const void *pp)
  126. {
  127. uint8_t const *p = (uint8_t const *)pp;
  128. return ((p[1] << 8) | p[0]);
  129. }
  130. static __inline uint32_t
  131. le32dec(const void *pp)
  132. {
  133. uint8_t const *p = (uint8_t const *)pp;
  134. return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
  135. }
  136. static __inline void
  137. be16enc(void *pp, uint16_t u)
  138. {
  139. uint8_t *p = (uint8_t *)pp;
  140. p[0] = (u >> 8) & 0xff;
  141. p[1] = u & 0xff;
  142. }
  143. static __inline void
  144. be32enc(void *pp, uint32_t u)
  145. {
  146. uint8_t *p = (uint8_t *)pp;
  147. p[0] = (u >> 24) & 0xff;
  148. p[1] = (u >> 16) & 0xff;
  149. p[2] = (u >> 8) & 0xff;
  150. p[3] = u & 0xff;
  151. }
  152. static __inline void
  153. le16enc(void *pp, uint16_t u)
  154. {
  155. uint8_t *p = (uint8_t *)pp;
  156. p[0] = u & 0xff;
  157. p[1] = (u >> 8) & 0xff;
  158. }
  159. static __inline void
  160. le32enc(void *pp, uint32_t u)
  161. {
  162. uint8_t *p = (uint8_t *)pp;
  163. p[0] = u & 0xff;
  164. p[1] = (u >> 8) & 0xff;
  165. p[2] = (u >> 16) & 0xff;
  166. p[3] = (u >> 24) & 0xff;
  167. }
  168. void
  169. uuid_enc_le(void *buf, const struct uuid *uuid)
  170. {
  171. uint8_t *p = buf;
  172. int i;
  173. le32enc(p, uuid->time_low);
  174. le16enc(p + 4, uuid->time_mid);
  175. le16enc(p + 6, uuid->time_hi_and_version);
  176. p[8] = uuid->clock_seq_hi_and_reserved;
  177. p[9] = uuid->clock_seq_low;
  178. for (i = 0; i < _UUID_NODE_LEN; i++)
  179. p[10 + i] = uuid->node[i];
  180. }
  181. void
  182. uuid_dec_le(const void *buf, struct uuid *uuid)
  183. {
  184. const uint8_t *p = buf;
  185. int i;
  186. uuid->time_low = le32dec(p);
  187. uuid->time_mid = le16dec(p + 4);
  188. uuid->time_hi_and_version = le16dec(p + 6);
  189. uuid->clock_seq_hi_and_reserved = p[8];
  190. uuid->clock_seq_low = p[9];
  191. for (i = 0; i < _UUID_NODE_LEN; i++)
  192. uuid->node[i] = p[10 + i];
  193. }
  194. void
  195. uuid_enc_be(void *buf, const struct uuid *uuid)
  196. {
  197. uint8_t *p = buf;
  198. int i;
  199. be32enc(p, uuid->time_low);
  200. be16enc(p + 4, uuid->time_mid);
  201. be16enc(p + 6, uuid->time_hi_and_version);
  202. p[8] = uuid->clock_seq_hi_and_reserved;
  203. p[9] = uuid->clock_seq_low;
  204. for (i = 0; i < _UUID_NODE_LEN; i++)
  205. p[10 + i] = uuid->node[i];
  206. }
  207. void
  208. uuid_dec_be(const void *buf, struct uuid *uuid)
  209. {
  210. const uint8_t *p = buf;
  211. int i;
  212. uuid->time_low = be32dec(p);
  213. uuid->time_mid = be16dec(p + 4);
  214. uuid->time_hi_and_version = be16dec(p + 6);
  215. uuid->clock_seq_hi_and_reserved = p[8];
  216. uuid->clock_seq_low = p[9];
  217. for (i = 0; i < _UUID_NODE_LEN; i++)
  218. uuid->node[i] = p[10 + i];
  219. }