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