iPXE
uuid.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <byteswap.h>
30 #include <ipxe/base16.h>
31 #include <ipxe/uuid.h>
32 
33 /** @file
34  *
35  * Universally unique IDs
36  *
37  */
38 
39 /**
40  * Convert UUID to printable string
41  *
42  * @v uuid UUID
43  * @ret string UUID in canonical form
44  */
45 const char * uuid_ntoa ( const union uuid *uuid ) {
46  static char buf[37]; /* "00000000-0000-0000-0000-000000000000" */
47 
48  sprintf ( buf, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
53  uuid->canonical.e[0], uuid->canonical.e[1],
54  uuid->canonical.e[2], uuid->canonical.e[3],
55  uuid->canonical.e[4], uuid->canonical.e[5] );
56  return buf;
57 }
58 
59 /**
60  * Parse UUID
61  *
62  * @v string UUID string
63  * @v uuid UUID to fill in
64  * @ret rc Return status code
65  */
66 int uuid_aton ( const char *string, union uuid *uuid ) {
67  int len;
68  int rc;
69 
70  /* Decode as hex string with optional '-' separator */
71  len = hex_decode ( ( '-' | HEX_DECODE_OPTIONAL ), string, uuid->raw,
72  sizeof ( *uuid ) );
73  if ( len < 0 ) {
74  rc = len;
75  return rc;
76  }
77 
78  /* Check length */
79  if ( len != sizeof ( *uuid ) )
80  return -EINVAL;
81 
82  return 0;
83 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
uint32_t a
8 hex digits, big-endian
Definition: uuid.h:19
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint16_t c
2 hex digits, big-endian
Definition: uuid.h:23
Error codes.
A universally unique ID.
Definition: uuid.h:15
#define sprintf(buf, fmt,...)
Write a formatted string to a buffer.
Definition: stdio.h:36
Universally unique IDs.
uint16_t d
2 hex digits, big-endian
Definition: uuid.h:25
int uuid_aton(const char *string, union uuid *uuid)
Parse UUID.
Definition: uuid.c:66
#define be32_to_cpu(value)
Definition: byteswap.h:116
#define HEX_DECODE_OPTIONAL
Treat separator as optional while decoding.
Definition: base16.h:16
#define be16_to_cpu(value)
Definition: byteswap.h:115
int hex_decode(char separator, const char *encoded, void *data, size_t len)
Decode hexadecimal string (with optional byte separator character)
Definition: base16.c:76
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint16_t b
2 hex digits, big-endian
Definition: uuid.h:21
uint8_t e[6]
12 hex digits, big-endian
Definition: uuid.h:27
uint8_t raw[16]
Definition: uuid.h:29
const char * uuid_ntoa(const union uuid *uuid)
Convert UUID to printable string.
Definition: uuid.c:45
uint32_t len
Length.
Definition: ena.h:14
struct uuid::@594 canonical
Canonical form (00000000-0000-0000-0000-000000000000)
Base16 encoding.