Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:20

0001 /*
0002  * unparse.c -- convert a UUID to string
0003  *
0004  * Copyright (C) 1996, 1997 Theodore Ts'o.
0005  *
0006  * %Begin-Header%
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, and the entire permission notice in its entirety,
0012  *    including the disclaimer of warranties.
0013  * 2. Redistributions in binary form must reproduce the above copyright
0014  *    notice, this list of conditions and the following disclaimer in the
0015  *    documentation and/or other materials provided with the distribution.
0016  * 3. The name of the author may not be used to endorse or promote
0017  *    products derived from this software without specific prior
0018  *    written permission.
0019  *
0020  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
0021  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0022  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
0023  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
0024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
0026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
0027  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
0030  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
0031  * DAMAGE.
0032  * %End-Header%
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #include <stdio.h>
0040 
0041 #include "uuidP.h"
0042 
0043 static const char *fmt_lower =
0044     "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
0045 
0046 static const char *fmt_upper =
0047     "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
0048 
0049 #ifdef UUID_UNPARSE_DEFAULT_UPPER
0050 #define FMT_DEFAULT fmt_upper
0051 #else
0052 #define FMT_DEFAULT fmt_lower
0053 #endif
0054 
0055 static void uuid_unparse_x(const uuid_t uu, char *out, const char *fmt)
0056 {
0057     struct uuid uuid;
0058 
0059     uuid_unpack(uu, &uuid);
0060     sprintf(out, fmt,
0061         uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
0062         uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
0063         uuid.node[0], uuid.node[1], uuid.node[2],
0064         uuid.node[3], uuid.node[4], uuid.node[5]);
0065 }
0066 
0067 void uuid_unparse_lower(const uuid_t uu, char *out)
0068 {
0069     uuid_unparse_x(uu, out, fmt_lower);
0070 }
0071 
0072 void uuid_unparse_upper(const uuid_t uu, char *out)
0073 {
0074     uuid_unparse_x(uu, out, fmt_upper);
0075 }
0076 
0077 void uuid_unparse(const uuid_t uu, char *out)
0078 {
0079     uuid_unparse_x(uu, out, FMT_DEFAULT);
0080 }