1 /* $NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2002 Marcel Moolenaar
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/endian.h>
38 * Encode/Decode UUID into octet-stream.
39 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
42 * 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
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | time_mid | time_hi_and_version |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * |clk_seq_hi_res | clk_seq_low | node (0-1) |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * NOTE: These routines are not part of the DCE RPC API. They are
54 * provided for convenience.
58 uuid_enc_le(void *buf, const uuid_t *uuid)
63 le32enc(p, uuid->time_low);
64 le16enc(p + 4, uuid->time_mid);
65 le16enc(p + 6, uuid->time_hi_and_version);
66 p[8] = uuid->clock_seq_hi_and_reserved;
67 p[9] = uuid->clock_seq_low;
68 for (i = 0; i < _UUID_NODE_LEN; i++)
69 p[10 + i] = uuid->node[i];
73 uuid_dec_le(const void *buf, uuid_t *uuid)
75 const uint8_t *p = buf;
78 uuid->time_low = le32dec(p);
79 uuid->time_mid = le16dec(p + 4);
80 uuid->time_hi_and_version = le16dec(p + 6);
81 uuid->clock_seq_hi_and_reserved = p[8];
82 uuid->clock_seq_low = p[9];
83 for (i = 0; i < _UUID_NODE_LEN; i++)
84 uuid->node[i] = p[10 + i];
88 uuid_enc_be(void *buf, const uuid_t *uuid)
93 be32enc(p, uuid->time_low);
94 be16enc(p + 4, uuid->time_mid);
95 be16enc(p + 6, uuid->time_hi_and_version);
96 p[8] = uuid->clock_seq_hi_and_reserved;
97 p[9] = uuid->clock_seq_low;
98 for (i = 0; i < _UUID_NODE_LEN; i++)
99 p[10 + i] = uuid->node[i];
103 uuid_dec_be(const void *buf, uuid_t *uuid)
105 const uint8_t *p = buf;
108 uuid->time_low = be32dec(p);
109 uuid->time_mid = be16dec(p + 4);
110 uuid->time_hi_and_version = be16dec(p + 6);
111 uuid->clock_seq_hi_and_reserved = p[8];
112 uuid->clock_seq_low = p[9];
113 for (i = 0; i < _UUID_NODE_LEN; i++)
114 uuid->node[i] = p[10 + i];