1 /* $NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2002 Marcel Moolenaar 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 #include <sys/endian.h> 33 #include <uuid.h> 34 35 /* 36 * Encode/Decode UUID into octet-stream. 37 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt 38 * 39 * 0 1 2 3 40 * 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 41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 42 * | time_low | 43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 44 * | time_mid | time_hi_and_version | 45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 46 * |clk_seq_hi_res | clk_seq_low | node (0-1) | 47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 48 * | node (2-5) | 49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 50 * 51 * NOTE: These routines are not part of the DCE RPC API. They are 52 * provided for convenience. 53 */ 54 55 void 56 uuid_enc_le(void *buf, const uuid_t *uuid) 57 { 58 uint8_t *p = buf; 59 int i; 60 61 le32enc(p, uuid->time_low); 62 le16enc(p + 4, uuid->time_mid); 63 le16enc(p + 6, uuid->time_hi_and_version); 64 p[8] = uuid->clock_seq_hi_and_reserved; 65 p[9] = uuid->clock_seq_low; 66 for (i = 0; i < _UUID_NODE_LEN; i++) 67 p[10 + i] = uuid->node[i]; 68 } 69 70 void 71 uuid_dec_le(const void *buf, uuid_t *uuid) 72 { 73 const uint8_t *p = buf; 74 int i; 75 76 uuid->time_low = le32dec(p); 77 uuid->time_mid = le16dec(p + 4); 78 uuid->time_hi_and_version = le16dec(p + 6); 79 uuid->clock_seq_hi_and_reserved = p[8]; 80 uuid->clock_seq_low = p[9]; 81 for (i = 0; i < _UUID_NODE_LEN; i++) 82 uuid->node[i] = p[10 + i]; 83 } 84 85 void 86 uuid_enc_be(void *buf, const uuid_t *uuid) 87 { 88 uint8_t *p = buf; 89 int i; 90 91 be32enc(p, uuid->time_low); 92 be16enc(p + 4, uuid->time_mid); 93 be16enc(p + 6, uuid->time_hi_and_version); 94 p[8] = uuid->clock_seq_hi_and_reserved; 95 p[9] = uuid->clock_seq_low; 96 for (i = 0; i < _UUID_NODE_LEN; i++) 97 p[10 + i] = uuid->node[i]; 98 } 99 100 void 101 uuid_dec_be(const void *buf, uuid_t *uuid) 102 { 103 const uint8_t *p = buf; 104 int i; 105 106 uuid->time_low = be32dec(p); 107 uuid->time_mid = be16dec(p + 4); 108 uuid->time_hi_and_version = be16dec(p + 6); 109 uuid->clock_seq_hi_and_reserved = p[8]; 110 uuid->clock_seq_low = p[9]; 111 for (i = 0; i < _UUID_NODE_LEN; i++) 112 uuid->node[i] = p[10 + i]; 113 } 114