nrelease - fix/improve livecd
[dragonfly.git] / lib / libc / uuid / uuid_stream.c
1 /*      $NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky 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  * $FreeBSD: src/lib/libc/uuid/uuid_stream.c,v 1.1 2008/08/14 22:23:16 emax Exp $
29  */
30
31 #include <sys/endian.h>
32 #include <uuid.h>
33
34 /*
35  * Encode/Decode UUID into octet-stream.
36  *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
37  *
38  * 0                   1                   2                   3
39  *   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
40  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  *  |                          time_low                             |
42  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43  *  |       time_mid                |         time_hi_and_version   |
44  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45  *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
46  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47  *  |                         node (2-5)                            |
48  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49  *
50  * NOTE: These routines are not part of the DCE RPC API. They are
51  * provided for convenience.
52  */
53
54 void
55 uuid_enc_le(void *buf, const uuid_t *uuid)
56 {
57         uint8_t *p = buf;
58         int i;
59
60         le32enc(p, uuid->time_low);
61         le16enc(p + 4, uuid->time_mid);
62         le16enc(p + 6, uuid->time_hi_and_version);
63         p[8] = uuid->clock_seq_hi_and_reserved;
64         p[9] = uuid->clock_seq_low;
65         for (i = 0; i < _UUID_NODE_LEN; i++)
66                 p[10 + i] = uuid->node[i];
67 }
68
69 void
70 uuid_dec_le(const void *buf, uuid_t *uuid)
71 {
72         const uint8_t *p = buf;
73         int i;
74
75         uuid->time_low = le32dec(p);
76         uuid->time_mid = le16dec(p + 4);
77         uuid->time_hi_and_version = le16dec(p + 6);
78         uuid->clock_seq_hi_and_reserved = p[8];
79         uuid->clock_seq_low = p[9];
80         for (i = 0; i < _UUID_NODE_LEN; i++)
81                 uuid->node[i] = p[10 + i];
82 }
83
84 void
85 uuid_enc_be(void *buf, const uuid_t *uuid)
86 {
87         uint8_t *p = buf;
88         int i;
89
90         be32enc(p, uuid->time_low);
91         be16enc(p + 4, uuid->time_mid);
92         be16enc(p + 6, uuid->time_hi_and_version);
93         p[8] = uuid->clock_seq_hi_and_reserved;
94         p[9] = uuid->clock_seq_low;
95         for (i = 0; i < _UUID_NODE_LEN; i++)
96                 p[10 + i] = uuid->node[i];
97 }
98
99 void
100 uuid_dec_be(const void *buf, uuid_t *uuid)
101 {
102         const uint8_t *p = buf;
103         int i;
104
105         uuid->time_low = be32dec(p);
106         uuid->time_mid = be16dec(p + 4);
107         uuid->time_hi_and_version = be16dec(p + 6);
108         uuid->clock_seq_hi_and_reserved = p[8];
109         uuid->clock_seq_low = p[9];
110         for (i = 0; i < _UUID_NODE_LEN; i++)
111                 uuid->node[i] = p[10 + i];
112 }