1 /* $OpenBSD: kern_uuid.c,v 1.1 2014/07/13 15:32:28 miod Exp $ */ 2 /* $NetBSD: kern_uuid.c,v 1.18 2011/11/19 22:51:25 tls Exp $ */ 3 4 /* 5 * Copyright (c) 2002 Marcel Moolenaar 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_uuid.c,v 1.7 2004/01/12 30 13:34:11 rse Exp $ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/endian.h> 35 #include <sys/systm.h> 36 #include <sys/uuid.h> 37 38 /* 39 * For a description of UUIDs see RFC 4122. 40 */ 41 42 struct uuid_private { 43 struct { 44 uint32_t low; 45 uint16_t mid; 46 uint16_t hi; 47 } time; 48 uint16_t seq; 49 uint16_t node[UUID_NODE_LEN>>1]; 50 }; 51 52 #ifdef DEBUG 53 int 54 uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid) 55 { 56 const struct uuid_private *id; 57 int cnt; 58 59 id = (const struct uuid_private *)uuid; 60 cnt = snprintf(buf, sz, "%08x-%04x-%04x-%04x-%04x%04x%04x", 61 id->time.low, id->time.mid, id->time.hi, betoh16(id->seq), 62 betoh16(id->node[0]), betoh16(id->node[1]), betoh16(id->node[2])); 63 return (cnt); 64 } 65 66 int 67 uuid_printf(const struct uuid *uuid) 68 { 69 char buf[_UUID_BUF_LEN]; 70 71 (void) uuid_snprintf(buf, sizeof(buf), uuid); 72 printf("%s", buf); 73 return (0); 74 } 75 #endif 76 77 /* 78 * Encode/Decode UUID into octet-stream. 79 * 80 * 0 1 2 3 81 * 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 82 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 83 * | time_low | 84 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 85 * | time_mid | time_hi_and_version | 86 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 87 * |clk_seq_hi_res | clk_seq_low | node (0-1) | 88 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 89 * | node (2-5) | 90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 91 */ 92 93 void 94 uuid_enc_le(void *buf, const struct uuid *uuid) 95 { 96 uint8_t *p = buf; 97 int i; 98 99 p[0] = htole32(uuid->time_low); 100 p[4] = htole16(uuid->time_mid); 101 p[6] = htole16(uuid->time_hi_and_version); 102 p[8] = uuid->clock_seq_hi_and_reserved; 103 p[9] = uuid->clock_seq_low; 104 for (i = 0; i < UUID_NODE_LEN; i++) 105 p[10 + i] = uuid->node[i]; 106 } 107 108 void 109 uuid_dec_le(void const *buf, struct uuid *uuid) 110 { 111 const uint8_t *p = buf; 112 int i; 113 114 uuid->time_low = letoh32(*(uint32_t*)p); 115 uuid->time_mid = letoh16(*(uint16_t*)(p + 4)); 116 uuid->time_hi_and_version = letoh16(*(uint16_t*)(p + 6)); 117 uuid->clock_seq_hi_and_reserved = p[8]; 118 uuid->clock_seq_low = p[9]; 119 for (i = 0; i < UUID_NODE_LEN; i++) 120 uuid->node[i] = p[10 + i]; 121 } 122 123 void 124 uuid_enc_be(void *buf, const struct uuid *uuid) 125 { 126 uint8_t *p = buf; 127 int i; 128 129 p[0] = htobe32(uuid->time_low); 130 p[4] = htobe16(uuid->time_mid); 131 p[6] = htobe16(uuid->time_hi_and_version); 132 p[8] = uuid->clock_seq_hi_and_reserved; 133 p[9] = uuid->clock_seq_low; 134 for (i = 0; i < UUID_NODE_LEN; i++) 135 p[10 + i] = uuid->node[i]; 136 } 137 138 void 139 uuid_dec_be(void const *buf, struct uuid *uuid) 140 { 141 const uint8_t *p = buf; 142 int i; 143 144 uuid->time_low = betoh32(*(uint32_t*)p); 145 uuid->time_mid = betoh16(*(uint16_t*)(p + 4)); 146 uuid->time_hi_and_version = betoh16(*(uint16_t*)(p + 6)); 147 uuid->clock_seq_hi_and_reserved = p[8]; 148 uuid->clock_seq_low = p[9]; 149 for (i = 0; i < UUID_NODE_LEN; i++) 150 uuid->node[i] = p[10 + i]; 151 } 152