1181743Semax/*	$NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky Exp $	*/
2181743Semax
3181743Semax/*-
4181743Semax * Copyright (c) 2002 Marcel Moolenaar
5181743Semax * All rights reserved.
6181743Semax *
7181743Semax * Redistribution and use in source and binary forms, with or without
8181743Semax * modification, are permitted provided that the following conditions
9181743Semax * are met:
10181743Semax *
11181743Semax * 1. Redistributions of source code must retain the above copyright
12181743Semax *    notice, this list of conditions and the following disclaimer.
13181743Semax * 2. Redistributions in binary form must reproduce the above copyright
14181743Semax *    notice, this list of conditions and the following disclaimer in the
15181743Semax *    documentation and/or other materials provided with the distribution.
16181743Semax *
17181743Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18181743Semax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19181743Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20181743Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21181743Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22181743Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23181743Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24181743Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25181743Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26181743Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27181743Semax */
28181743Semax
29181743Semax#include <sys/cdefs.h>
30181743Semax__FBSDID("$FreeBSD$");
31181743Semax
32181743Semax#include <sys/endian.h>
33181743Semax#include <uuid.h>
34181743Semax
35181743Semax/*
36181743Semax * Encode/Decode UUID into octet-stream.
37181743Semax *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
38181743Semax *
39181743Semax * 0                   1                   2                   3
40181743Semax *   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
41181743Semax *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42181743Semax *  |                          time_low                             |
43181743Semax *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44181743Semax *  |       time_mid                |         time_hi_and_version   |
45181743Semax *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46181743Semax *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
47181743Semax *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48181743Semax *  |                         node (2-5)                            |
49181743Semax *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50181743Semax *
51181743Semax * NOTE: These routines are not part of the DCE RPC API. They are
52181743Semax * provided for convenience.
53181743Semax */
54181743Semax
55181743Semaxvoid
56181743Semaxuuid_enc_le(void *buf, const uuid_t *uuid)
57181743Semax{
58181743Semax	uint8_t *p = buf;
59181743Semax	int i;
60181743Semax
61181743Semax	le32enc(p, uuid->time_low);
62181743Semax	le16enc(p + 4, uuid->time_mid);
63181743Semax	le16enc(p + 6, uuid->time_hi_and_version);
64181743Semax	p[8] = uuid->clock_seq_hi_and_reserved;
65181743Semax	p[9] = uuid->clock_seq_low;
66181743Semax	for (i = 0; i < _UUID_NODE_LEN; i++)
67181743Semax		p[10 + i] = uuid->node[i];
68181743Semax}
69181743Semax
70181743Semaxvoid
71181743Semaxuuid_dec_le(const void *buf, uuid_t *uuid)
72181743Semax{
73181743Semax	const uint8_t *p = buf;
74181743Semax	int i;
75181743Semax
76181743Semax	uuid->time_low = le32dec(p);
77181743Semax	uuid->time_mid = le16dec(p + 4);
78181743Semax	uuid->time_hi_and_version = le16dec(p + 6);
79181743Semax	uuid->clock_seq_hi_and_reserved = p[8];
80181743Semax	uuid->clock_seq_low = p[9];
81181743Semax	for (i = 0; i < _UUID_NODE_LEN; i++)
82181743Semax		uuid->node[i] = p[10 + i];
83181743Semax}
84181743Semax
85181743Semaxvoid
86181743Semaxuuid_enc_be(void *buf, const uuid_t *uuid)
87181743Semax{
88181743Semax	uint8_t *p = buf;
89181743Semax	int i;
90181743Semax
91181743Semax	be32enc(p, uuid->time_low);
92181743Semax	be16enc(p + 4, uuid->time_mid);
93181743Semax	be16enc(p + 6, uuid->time_hi_and_version);
94181743Semax	p[8] = uuid->clock_seq_hi_and_reserved;
95181743Semax	p[9] = uuid->clock_seq_low;
96181743Semax	for (i = 0; i < _UUID_NODE_LEN; i++)
97181743Semax		p[10 + i] = uuid->node[i];
98181743Semax}
99181743Semax
100181743Semaxvoid
101181743Semaxuuid_dec_be(const void *buf, uuid_t *uuid)
102181743Semax{
103181743Semax	const uint8_t *p = buf;
104181743Semax	int i;
105181743Semax
106181743Semax	uuid->time_low = be32dec(p);
107181743Semax	uuid->time_mid = be16dec(p + 4);
108181743Semax	uuid->time_hi_and_version = be16dec(p + 6);
109181743Semax	uuid->clock_seq_hi_and_reserved = p[8];
110181743Semax	uuid->clock_seq_low = p[9];
111181743Semax	for (i = 0; i < _UUID_NODE_LEN; i++)
112181743Semax		uuid->node[i] = p[10 + i];
113181743Semax}
114