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