/* $NetBSD: uuid_stream.c,v 1.3 2008/04/19 18:21:38 plunky Exp $ */12/*-3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2002 Marcel Moolenaar6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11*12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR19* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES20* 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, BUT23* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF27* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28*/2930#include <sys/endian.h>31#include <uuid.h>3233/*34* Encode/Decode UUID into octet-stream.35* http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt36*37* 0 1 2 338* 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 139* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+40* | time_low |41* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+42* | time_mid | time_hi_and_version |43* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+44* |clk_seq_hi_res | clk_seq_low | node (0-1) |45* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+46* | node (2-5) |47* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+48*49* NOTE: These routines are not part of the DCE RPC API. They are50* provided for convenience.51*/5253void54uuid_enc_le(void *buf, const uuid_t *uuid)55{56uint8_t *p = buf;57int i;5859le32enc(p, uuid->time_low);60le16enc(p + 4, uuid->time_mid);61le16enc(p + 6, uuid->time_hi_and_version);62p[8] = uuid->clock_seq_hi_and_reserved;63p[9] = uuid->clock_seq_low;64for (i = 0; i < _UUID_NODE_LEN; i++)65p[10 + i] = uuid->node[i];66}6768void69uuid_dec_le(const void *buf, uuid_t *uuid)70{71const uint8_t *p = buf;72int i;7374uuid->time_low = le32dec(p);75uuid->time_mid = le16dec(p + 4);76uuid->time_hi_and_version = le16dec(p + 6);77uuid->clock_seq_hi_and_reserved = p[8];78uuid->clock_seq_low = p[9];79for (i = 0; i < _UUID_NODE_LEN; i++)80uuid->node[i] = p[10 + i];81}8283void84uuid_enc_be(void *buf, const uuid_t *uuid)85{86uint8_t *p = buf;87int i;8889be32enc(p, uuid->time_low);90be16enc(p + 4, uuid->time_mid);91be16enc(p + 6, uuid->time_hi_and_version);92p[8] = uuid->clock_seq_hi_and_reserved;93p[9] = uuid->clock_seq_low;94for (i = 0; i < _UUID_NODE_LEN; i++)95p[10 + i] = uuid->node[i];96}9798void99uuid_dec_be(const void *buf, uuid_t *uuid)100{101const uint8_t *p = buf;102int i;103104uuid->time_low = be32dec(p);105uuid->time_mid = be16dec(p + 4);106uuid->time_hi_and_version = be16dec(p + 6);107uuid->clock_seq_hi_and_reserved = p[8];108uuid->clock_seq_low = p[9];109for (i = 0; i < _UUID_NODE_LEN; i++)110uuid->node[i] = p[10 + i];111}112113114