GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/uuid/uuid_stream.c Lines: 0 44 0.0 %
Date: 2017-11-13 Branches: 0 8 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: uuid_stream.c,v 1.3 2015/09/10 18:13:46 guenther Exp $	*/
2
/*	$NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky Exp $	*/
3
4
/*-
5
 * Copyright (c) 2002 Marcel Moolenaar
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
/*-
30
 * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
31
 * All rights reserved.
32
 *
33
 * Redistribution and use in source and binary forms, with or without
34
 * modification, are permitted provided that the following conditions
35
 * are met:
36
 * 1. Redistributions of source code must retain the above copyright
37
 *    notice, this list of conditions and the following disclaimer.
38
 * 2. Redistributions in binary form must reproduce the above copyright
39
 *    notice, this list of conditions and the following disclaimer in the
40
 *    documentation and/or other materials provided with the distribution.
41
 *
42
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52
 * SUCH DAMAGE.
53
 *
54
 * $FreeBSD: src/sys/sys/endian.h,v 1.7 2010/05/20 06:16:13 phk Exp $
55
 */
56
57
#include <sys/types.h>
58
#include <endian.h>
59
#include <uuid.h>
60
61
/*
62
 * Encode/Decode UUID into octet-stream.
63
 *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
64
 *
65
 * 0                   1                   2                   3
66
 *   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
67
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68
 *  |                          time_low                             |
69
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70
 *  |       time_mid                |         time_hi_and_version   |
71
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72
 *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
73
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74
 *  |                         node (2-5)                            |
75
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76
 *
77
 * NOTE: These routines are not part of the DCE RPC API. They are
78
 * provided for convenience.
79
 */
80
81
/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
82
83
static __inline uint16_t
84
be16dec(const void *pp)
85
{
86
	uint8_t const *p = (uint8_t const *)pp;
87
88
	return ((p[0] << 8) | p[1]);
89
}
90
91
static __inline uint32_t
92
be32dec(const void *pp)
93
{
94
	uint8_t const *p = (uint8_t const *)pp;
95
96
	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
97
}
98
99
static __inline uint16_t
100
le16dec(const void *pp)
101
{
102
	uint8_t const *p = (uint8_t const *)pp;
103
104
	return ((p[1] << 8) | p[0]);
105
}
106
107
static __inline uint32_t
108
le32dec(const void *pp)
109
{
110
	uint8_t const *p = (uint8_t const *)pp;
111
112
	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
113
}
114
115
static __inline void
116
be16enc(void *pp, uint16_t u)
117
{
118
	uint8_t *p = (uint8_t *)pp;
119
120
	p[0] = (u >> 8) & 0xff;
121
	p[1] = u & 0xff;
122
}
123
124
static __inline void
125
be32enc(void *pp, uint32_t u)
126
{
127
	uint8_t *p = (uint8_t *)pp;
128
129
	p[0] = (u >> 24) & 0xff;
130
	p[1] = (u >> 16) & 0xff;
131
	p[2] = (u >> 8) & 0xff;
132
	p[3] = u & 0xff;
133
}
134
135
static __inline void
136
le16enc(void *pp, uint16_t u)
137
{
138
	uint8_t *p = (uint8_t *)pp;
139
140
	p[0] = u & 0xff;
141
	p[1] = (u >> 8) & 0xff;
142
}
143
144
static __inline void
145
le32enc(void *pp, uint32_t u)
146
{
147
	uint8_t *p = (uint8_t *)pp;
148
149
	p[0] = u & 0xff;
150
	p[1] = (u >> 8) & 0xff;
151
	p[2] = (u >> 16) & 0xff;
152
	p[3] = (u >> 24) & 0xff;
153
}
154
155
void
156
uuid_enc_le(void *buf, const uuid_t *uuid)
157
{
158
	uint8_t *p = buf;
159
	int i;
160
161
	le32enc(p, uuid->time_low);
162
	le16enc(p + 4, uuid->time_mid);
163
	le16enc(p + 6, uuid->time_hi_and_version);
164
	p[8] = uuid->clock_seq_hi_and_reserved;
165
	p[9] = uuid->clock_seq_low;
166
	for (i = 0; i < _UUID_NODE_LEN; i++)
167
		p[10 + i] = uuid->node[i];
168
}
169
170
void
171
uuid_dec_le(const void *buf, uuid_t *uuid)
172
{
173
	const uint8_t *p = buf;
174
	int i;
175
176
	uuid->time_low = le32dec(p);
177
	uuid->time_mid = le16dec(p + 4);
178
	uuid->time_hi_and_version = le16dec(p + 6);
179
	uuid->clock_seq_hi_and_reserved = p[8];
180
	uuid->clock_seq_low = p[9];
181
	for (i = 0; i < _UUID_NODE_LEN; i++)
182
		uuid->node[i] = p[10 + i];
183
}
184
185
void
186
uuid_enc_be(void *buf, const uuid_t *uuid)
187
{
188
	uint8_t *p = buf;
189
	int i;
190
191
	be32enc(p, uuid->time_low);
192
	be16enc(p + 4, uuid->time_mid);
193
	be16enc(p + 6, uuid->time_hi_and_version);
194
	p[8] = uuid->clock_seq_hi_and_reserved;
195
	p[9] = uuid->clock_seq_low;
196
	for (i = 0; i < _UUID_NODE_LEN; i++)
197
		p[10 + i] = uuid->node[i];
198
}
199
200
void
201
uuid_dec_be(const void *buf, uuid_t *uuid)
202
{
203
	const uint8_t *p = buf;
204
	int i;
205
206
	uuid->time_low = be32dec(p);
207
	uuid->time_mid = be16dec(p + 4);
208
	uuid->time_hi_and_version = be16dec(p + 6);
209
	uuid->clock_seq_hi_and_reserved = p[8];
210
	uuid->clock_seq_low = p[9];
211
	for (i = 0; i < _UUID_NODE_LEN; i++)
212
		uuid->node[i] = p[10 + i];
213
}