xref: /openbsd/lib/libc/uuid/uuid_stream.c (revision e6f98e3a)
1*e6f98e3aSguenther /*	$OpenBSD: uuid_stream.c,v 1.3 2015/09/10 18:13:46 guenther Exp $	*/
29a210c53Smiod /*	$NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky Exp $	*/
39a210c53Smiod 
4d6003a90Smiod /*-
59a210c53Smiod  * Copyright (c) 2002 Marcel Moolenaar
69a210c53Smiod  * All rights reserved.
79a210c53Smiod  *
89a210c53Smiod  * Redistribution and use in source and binary forms, with or without
99a210c53Smiod  * modification, are permitted provided that the following conditions
109a210c53Smiod  * are met:
119a210c53Smiod  *
129a210c53Smiod  * 1. Redistributions of source code must retain the above copyright
139a210c53Smiod  *    notice, this list of conditions and the following disclaimer.
149a210c53Smiod  * 2. Redistributions in binary form must reproduce the above copyright
159a210c53Smiod  *    notice, this list of conditions and the following disclaimer in the
169a210c53Smiod  *    documentation and/or other materials provided with the distribution.
179a210c53Smiod  *
189a210c53Smiod  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199a210c53Smiod  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209a210c53Smiod  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
219a210c53Smiod  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
229a210c53Smiod  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
239a210c53Smiod  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249a210c53Smiod  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259a210c53Smiod  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269a210c53Smiod  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
279a210c53Smiod  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289a210c53Smiod  */
29d6003a90Smiod /*-
30d6003a90Smiod  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
31d6003a90Smiod  * All rights reserved.
32d6003a90Smiod  *
33d6003a90Smiod  * Redistribution and use in source and binary forms, with or without
34d6003a90Smiod  * modification, are permitted provided that the following conditions
35d6003a90Smiod  * are met:
36d6003a90Smiod  * 1. Redistributions of source code must retain the above copyright
37d6003a90Smiod  *    notice, this list of conditions and the following disclaimer.
38d6003a90Smiod  * 2. Redistributions in binary form must reproduce the above copyright
39d6003a90Smiod  *    notice, this list of conditions and the following disclaimer in the
40d6003a90Smiod  *    documentation and/or other materials provided with the distribution.
41d6003a90Smiod  *
42d6003a90Smiod  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43d6003a90Smiod  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44d6003a90Smiod  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45d6003a90Smiod  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46d6003a90Smiod  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47d6003a90Smiod  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48d6003a90Smiod  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49d6003a90Smiod  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50d6003a90Smiod  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51d6003a90Smiod  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52d6003a90Smiod  * SUCH DAMAGE.
53d6003a90Smiod  *
54d6003a90Smiod  * $FreeBSD: src/sys/sys/endian.h,v 1.7 2010/05/20 06:16:13 phk Exp $
55d6003a90Smiod  */
569a210c53Smiod 
579a210c53Smiod #include <sys/types.h>
58d6003a90Smiod #include <endian.h>
599a210c53Smiod #include <uuid.h>
609a210c53Smiod 
619a210c53Smiod /*
629a210c53Smiod  * Encode/Decode UUID into octet-stream.
639a210c53Smiod  *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
649a210c53Smiod  *
659a210c53Smiod  * 0                   1                   2                   3
669a210c53Smiod  *   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
679a210c53Smiod  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689a210c53Smiod  *  |                          time_low                             |
699a210c53Smiod  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
709a210c53Smiod  *  |       time_mid                |         time_hi_and_version   |
719a210c53Smiod  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
729a210c53Smiod  *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
739a210c53Smiod  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
749a210c53Smiod  *  |                         node (2-5)                            |
759a210c53Smiod  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
769a210c53Smiod  *
77d6003a90Smiod  * NOTE: These routines are not part of the DCE RPC API. They are
789a210c53Smiod  * provided for convenience.
799a210c53Smiod  */
809a210c53Smiod 
81d6003a90Smiod /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
82d6003a90Smiod 
83d6003a90Smiod static __inline uint16_t
be16dec(const void * pp)84d6003a90Smiod be16dec(const void *pp)
85d6003a90Smiod {
86d6003a90Smiod 	uint8_t const *p = (uint8_t const *)pp;
87d6003a90Smiod 
88d6003a90Smiod 	return ((p[0] << 8) | p[1]);
89d6003a90Smiod }
90d6003a90Smiod 
91d6003a90Smiod static __inline uint32_t
be32dec(const void * pp)92d6003a90Smiod be32dec(const void *pp)
93d6003a90Smiod {
94d6003a90Smiod 	uint8_t const *p = (uint8_t const *)pp;
95d6003a90Smiod 
96d6003a90Smiod 	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
97d6003a90Smiod }
98d6003a90Smiod 
99d6003a90Smiod static __inline uint16_t
le16dec(const void * pp)100d6003a90Smiod le16dec(const void *pp)
101d6003a90Smiod {
102d6003a90Smiod 	uint8_t const *p = (uint8_t const *)pp;
103d6003a90Smiod 
104d6003a90Smiod 	return ((p[1] << 8) | p[0]);
105d6003a90Smiod }
106d6003a90Smiod 
107d6003a90Smiod static __inline uint32_t
le32dec(const void * pp)108d6003a90Smiod le32dec(const void *pp)
109d6003a90Smiod {
110d6003a90Smiod 	uint8_t const *p = (uint8_t const *)pp;
111d6003a90Smiod 
112d6003a90Smiod 	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
113d6003a90Smiod }
114d6003a90Smiod 
115d6003a90Smiod static __inline void
be16enc(void * pp,uint16_t u)116d6003a90Smiod be16enc(void *pp, uint16_t u)
117d6003a90Smiod {
118d6003a90Smiod 	uint8_t *p = (uint8_t *)pp;
119d6003a90Smiod 
120d6003a90Smiod 	p[0] = (u >> 8) & 0xff;
121d6003a90Smiod 	p[1] = u & 0xff;
122d6003a90Smiod }
123d6003a90Smiod 
124d6003a90Smiod static __inline void
be32enc(void * pp,uint32_t u)125d6003a90Smiod be32enc(void *pp, uint32_t u)
126d6003a90Smiod {
127d6003a90Smiod 	uint8_t *p = (uint8_t *)pp;
128d6003a90Smiod 
129d6003a90Smiod 	p[0] = (u >> 24) & 0xff;
130d6003a90Smiod 	p[1] = (u >> 16) & 0xff;
131d6003a90Smiod 	p[2] = (u >> 8) & 0xff;
132d6003a90Smiod 	p[3] = u & 0xff;
133d6003a90Smiod }
134d6003a90Smiod 
135d6003a90Smiod static __inline void
le16enc(void * pp,uint16_t u)136d6003a90Smiod le16enc(void *pp, uint16_t u)
137d6003a90Smiod {
138d6003a90Smiod 	uint8_t *p = (uint8_t *)pp;
139d6003a90Smiod 
140d6003a90Smiod 	p[0] = u & 0xff;
141d6003a90Smiod 	p[1] = (u >> 8) & 0xff;
142d6003a90Smiod }
143d6003a90Smiod 
144d6003a90Smiod static __inline void
le32enc(void * pp,uint32_t u)145d6003a90Smiod le32enc(void *pp, uint32_t u)
146d6003a90Smiod {
147d6003a90Smiod 	uint8_t *p = (uint8_t *)pp;
148d6003a90Smiod 
149d6003a90Smiod 	p[0] = u & 0xff;
150d6003a90Smiod 	p[1] = (u >> 8) & 0xff;
151d6003a90Smiod 	p[2] = (u >> 16) & 0xff;
152d6003a90Smiod 	p[3] = (u >> 24) & 0xff;
153d6003a90Smiod }
154d6003a90Smiod 
1559a210c53Smiod void
uuid_enc_le(void * buf,const uuid_t * uuid)156d6003a90Smiod uuid_enc_le(void *buf, const uuid_t *uuid)
1579a210c53Smiod {
1589a210c53Smiod 	uint8_t *p = buf;
1599a210c53Smiod 	int i;
1609a210c53Smiod 
161d6003a90Smiod 	le32enc(p, uuid->time_low);
162d6003a90Smiod 	le16enc(p + 4, uuid->time_mid);
163d6003a90Smiod 	le16enc(p + 6, uuid->time_hi_and_version);
1649a210c53Smiod 	p[8] = uuid->clock_seq_hi_and_reserved;
1659a210c53Smiod 	p[9] = uuid->clock_seq_low;
1669a210c53Smiod 	for (i = 0; i < _UUID_NODE_LEN; i++)
1679a210c53Smiod 		p[10 + i] = uuid->node[i];
1689a210c53Smiod }
1699a210c53Smiod 
1709a210c53Smiod void
uuid_dec_le(const void * buf,uuid_t * uuid)171d6003a90Smiod uuid_dec_le(const void *buf, uuid_t *uuid)
1729a210c53Smiod {
1739a210c53Smiod 	const uint8_t *p = buf;
1749a210c53Smiod 	int i;
1759a210c53Smiod 
176d6003a90Smiod 	uuid->time_low = le32dec(p);
177d6003a90Smiod 	uuid->time_mid = le16dec(p + 4);
178d6003a90Smiod 	uuid->time_hi_and_version = le16dec(p + 6);
1799a210c53Smiod 	uuid->clock_seq_hi_and_reserved = p[8];
1809a210c53Smiod 	uuid->clock_seq_low = p[9];
1819a210c53Smiod 	for (i = 0; i < _UUID_NODE_LEN; i++)
1829a210c53Smiod 		uuid->node[i] = p[10 + i];
1839a210c53Smiod }
1849a210c53Smiod 
1859a210c53Smiod void
uuid_enc_be(void * buf,const uuid_t * uuid)186d6003a90Smiod uuid_enc_be(void *buf, const uuid_t *uuid)
1879a210c53Smiod {
1889a210c53Smiod 	uint8_t *p = buf;
1899a210c53Smiod 	int i;
1909a210c53Smiod 
191d6003a90Smiod 	be32enc(p, uuid->time_low);
192d6003a90Smiod 	be16enc(p + 4, uuid->time_mid);
193d6003a90Smiod 	be16enc(p + 6, uuid->time_hi_and_version);
1949a210c53Smiod 	p[8] = uuid->clock_seq_hi_and_reserved;
1959a210c53Smiod 	p[9] = uuid->clock_seq_low;
1969a210c53Smiod 	for (i = 0; i < _UUID_NODE_LEN; i++)
1979a210c53Smiod 		p[10 + i] = uuid->node[i];
1989a210c53Smiod }
1999a210c53Smiod 
2009a210c53Smiod void
uuid_dec_be(const void * buf,uuid_t * uuid)201d6003a90Smiod uuid_dec_be(const void *buf, uuid_t *uuid)
2029a210c53Smiod {
2039a210c53Smiod 	const uint8_t *p = buf;
2049a210c53Smiod 	int i;
2059a210c53Smiod 
206d6003a90Smiod 	uuid->time_low = be32dec(p);
207d6003a90Smiod 	uuid->time_mid = be16dec(p + 4);
208d6003a90Smiod 	uuid->time_hi_and_version = be16dec(p + 6);
2099a210c53Smiod 	uuid->clock_seq_hi_and_reserved = p[8];
2109a210c53Smiod 	uuid->clock_seq_low = p[9];
2119a210c53Smiod 	for (i = 0; i < _UUID_NODE_LEN; i++)
2129a210c53Smiod 		uuid->node[i] = p[10 + i];
2139a210c53Smiod }
214