1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * unparse.c -- convert a UUID to string
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 1996, 1997 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker *
6*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
8*6a54128fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
9*6a54128fSAndroid Build Coastguard Worker * are met:
10*6a54128fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
11*6a54128fSAndroid Build Coastguard Worker * notice, and the entire permission notice in its entirety,
12*6a54128fSAndroid Build Coastguard Worker * including the disclaimer of warranties.
13*6a54128fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
14*6a54128fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
15*6a54128fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
16*6a54128fSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote
17*6a54128fSAndroid Build Coastguard Worker * products derived from this software without specific prior
18*6a54128fSAndroid Build Coastguard Worker * written permission.
19*6a54128fSAndroid Build Coastguard Worker *
20*6a54128fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21*6a54128fSAndroid Build Coastguard Worker * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22*6a54128fSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23*6a54128fSAndroid Build Coastguard Worker * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24*6a54128fSAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*6a54128fSAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26*6a54128fSAndroid Build Coastguard Worker * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27*6a54128fSAndroid Build Coastguard Worker * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28*6a54128fSAndroid Build Coastguard Worker * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*6a54128fSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30*6a54128fSAndroid Build Coastguard Worker * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31*6a54128fSAndroid Build Coastguard Worker * DAMAGE.
32*6a54128fSAndroid Build Coastguard Worker * %End-Header%
33*6a54128fSAndroid Build Coastguard Worker */
34*6a54128fSAndroid Build Coastguard Worker
35*6a54128fSAndroid Build Coastguard Worker #include "config.h"
36*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
37*6a54128fSAndroid Build Coastguard Worker
38*6a54128fSAndroid Build Coastguard Worker #include "uuidP.h"
39*6a54128fSAndroid Build Coastguard Worker
40*6a54128fSAndroid Build Coastguard Worker static const char *fmt_lower =
41*6a54128fSAndroid Build Coastguard Worker "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
42*6a54128fSAndroid Build Coastguard Worker
43*6a54128fSAndroid Build Coastguard Worker static const char *fmt_upper =
44*6a54128fSAndroid Build Coastguard Worker "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
45*6a54128fSAndroid Build Coastguard Worker
46*6a54128fSAndroid Build Coastguard Worker #ifdef UUID_UNPARSE_DEFAULT_UPPER
47*6a54128fSAndroid Build Coastguard Worker #define FMT_DEFAULT fmt_upper
48*6a54128fSAndroid Build Coastguard Worker #else
49*6a54128fSAndroid Build Coastguard Worker #define FMT_DEFAULT fmt_lower
50*6a54128fSAndroid Build Coastguard Worker #endif
51*6a54128fSAndroid Build Coastguard Worker
uuid_unparse_x(const uuid_t uu,char * out,const char * fmt)52*6a54128fSAndroid Build Coastguard Worker static void uuid_unparse_x(const uuid_t uu, char *out, const char *fmt)
53*6a54128fSAndroid Build Coastguard Worker {
54*6a54128fSAndroid Build Coastguard Worker struct uuid uuid;
55*6a54128fSAndroid Build Coastguard Worker
56*6a54128fSAndroid Build Coastguard Worker uuid_unpack(uu, &uuid);
57*6a54128fSAndroid Build Coastguard Worker sprintf(out, fmt,
58*6a54128fSAndroid Build Coastguard Worker uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
59*6a54128fSAndroid Build Coastguard Worker uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
60*6a54128fSAndroid Build Coastguard Worker uuid.node[0], uuid.node[1], uuid.node[2],
61*6a54128fSAndroid Build Coastguard Worker uuid.node[3], uuid.node[4], uuid.node[5]);
62*6a54128fSAndroid Build Coastguard Worker }
63*6a54128fSAndroid Build Coastguard Worker
uuid_unparse_lower(const uuid_t uu,char * out)64*6a54128fSAndroid Build Coastguard Worker void uuid_unparse_lower(const uuid_t uu, char *out)
65*6a54128fSAndroid Build Coastguard Worker {
66*6a54128fSAndroid Build Coastguard Worker uuid_unparse_x(uu, out, fmt_lower);
67*6a54128fSAndroid Build Coastguard Worker }
68*6a54128fSAndroid Build Coastguard Worker
uuid_unparse_upper(const uuid_t uu,char * out)69*6a54128fSAndroid Build Coastguard Worker void uuid_unparse_upper(const uuid_t uu, char *out)
70*6a54128fSAndroid Build Coastguard Worker {
71*6a54128fSAndroid Build Coastguard Worker uuid_unparse_x(uu, out, fmt_upper);
72*6a54128fSAndroid Build Coastguard Worker }
73*6a54128fSAndroid Build Coastguard Worker
uuid_unparse(const uuid_t uu,char * out)74*6a54128fSAndroid Build Coastguard Worker void uuid_unparse(const uuid_t uu, char *out)
75*6a54128fSAndroid Build Coastguard Worker {
76*6a54128fSAndroid Build Coastguard Worker uuid_unparse_x(uu, out, FMT_DEFAULT);
77*6a54128fSAndroid Build Coastguard Worker }
78