1/*	$NetBSD: uuid_stream.c,v 1.2 2006/02/09 22:03:15 dogcow Exp $	*/
2
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
29#include <sys/cdefs.h>
30#if defined(LIBC_SCCS) && !defined(lint)
31__RCSID("$NetBSD: uuid_stream.c,v 1.2 2006/02/09 22:03:15 dogcow Exp $");
32#endif
33
34#include "namespace.h"
35
36#include <machine/endian.h>
37#include <uuid.h>
38
39/*
40 * Encode/Decode UUID into octet-stream.
41 *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
42 *
43 * 0                   1                   2                   3
44 *   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
45 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 *  |                          time_low                             |
47 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 *  |       time_mid                |         time_hi_and_version   |
49 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
51 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 *  |                         node (2-5)                            |
53 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 *
55 * NOTE: These routines are not part of the DCE RPC API.  There are
56 * provided for convenience.
57 */
58
59void
60uuid_enc_le(void *buf, const uuid_t *uuid)
61{
62	uint8_t *p = buf;
63	int i;
64
65	le32enc(p, uuid->time_low);
66	le16enc(p + 4, uuid->time_mid);
67	le16enc(p + 6, uuid->time_hi_and_version);
68	p[8] = uuid->clock_seq_hi_and_reserved;
69	p[9] = uuid->clock_seq_low;
70	for (i = 0; i < _UUID_NODE_LEN; i++)
71		p[10 + i] = uuid->node[i];
72}
73
74void
75uuid_dec_le(const void *buf, uuid_t *uuid)
76{
77	const uint8_t *p = buf;
78	int i;
79
80	uuid->time_low = le32dec(p);
81	uuid->time_mid = le16dec(p + 4);
82	uuid->time_hi_and_version = le16dec(p + 6);
83	uuid->clock_seq_hi_and_reserved = p[8];
84	uuid->clock_seq_low = p[9];
85	for (i = 0; i < _UUID_NODE_LEN; i++)
86		uuid->node[i] = p[10 + i];
87}
88
89void
90uuid_enc_be(void *buf, const uuid_t *uuid)
91{
92	uint8_t *p = buf;
93	int i;
94
95	be32enc(p, uuid->time_low);
96	be16enc(p + 4, uuid->time_mid);
97	be16enc(p + 6, uuid->time_hi_and_version);
98	p[8] = uuid->clock_seq_hi_and_reserved;
99	p[9] = uuid->clock_seq_low;
100	for (i = 0; i < _UUID_NODE_LEN; i++)
101		p[10 + i] = uuid->node[i];
102}
103
104void
105uuid_dec_be(const void *buf, uuid_t *uuid)
106{
107	const uint8_t *p = buf;
108	int i;
109
110	uuid->time_low = be32dec(p);
111	uuid->time_mid = be16dec(p + 4);
112	uuid->time_hi_and_version = be16dec(p + 6);
113	uuid->clock_seq_hi_and_reserved = p[8];
114	uuid->clock_seq_low = p[9];
115	for (i = 0; i < _UUID_NODE_LEN; i++)
116		uuid->node[i] = p[10 + i];
117}
118