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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <stdio.h>
29#include <errno.h>
30#include <byteswap.h>
31#include <ipxe/base16.h>
32#include <ipxe/uuid.h>
33
34/** @file
35 *
36 * Universally unique IDs
37 *
38 */
39
40/**
41 * Convert UUID to printable string
42 *
43 * @v uuid UUID
44 * @ret string UUID in canonical form
45 */
46const char * uuid_ntoa ( const union uuid *uuid ) {
47 static char buf[37]; /* "00000000-0000-0000-0000-000000000000" */
48
49 sprintf ( buf, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
54 uuid->canonical.e[0], uuid->canonical.e[1],
55 uuid->canonical.e[2], uuid->canonical.e[3],
56 uuid->canonical.e[4], uuid->canonical.e[5] );
57 return buf;
58}
59
60/**
61 * Parse UUID
62 *
63 * @v string UUID string
64 * @v uuid UUID to fill in
65 * @ret rc Return status code
66 */
67int uuid_aton ( const char *string, union uuid *uuid ) {
68 int len;
69 int rc;
70
71 /* Decode as hex string with optional '-' separator */
72 len = hex_decode ( ( '-' | HEX_DECODE_OPTIONAL ), string, uuid->raw,
73 sizeof ( *uuid ) );
74 if ( len < 0 ) {
75 rc = len;
76 return rc;
77 }
78
79 /* Check length */
80 if ( len != sizeof ( *uuid ) )
81 return -EINVAL;
82
83 return 0;
84}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
int hex_decode(char separator, const char *encoded, void *data, size_t len)
Decode hexadecimal string (with optional byte separator character)
Definition base16.c:77
Base16 encoding.
#define HEX_DECODE_OPTIONAL
Treat separator as optional while decoding.
Definition base16.h:17
ring len
Length.
Definition dwmac.h:226
Error codes.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EINVAL
Invalid argument.
Definition errno.h:429
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define be32_to_cpu(value)
Definition byteswap.h:117
#define be16_to_cpu(value)
Definition byteswap.h:116
#define sprintf(buf, fmt,...)
Write a formatted string to a buffer.
Definition stdio.h:37
A universally unique ID.
Definition uuid.h:16
uint16_t d
2 hex digits, big-endian
Definition uuid.h:26
uint8_t raw[16]
Definition uuid.h:30
uint8_t e[6]
12 hex digits, big-endian
Definition uuid.h:28
uint32_t a
8 hex digits, big-endian
Definition uuid.h:20
struct uuid::@036242100033237070006335026303064005125164242336 canonical
Canonical form (00000000-0000-0000-0000-000000000000)
uint16_t b
2 hex digits, big-endian
Definition uuid.h:22
uint16_t c
2 hex digits, big-endian
Definition uuid.h:24
const char * uuid_ntoa(const union uuid *uuid)
Convert UUID to printable string.
Definition uuid.c:46
int uuid_aton(const char *string, union uuid *uuid)
Parse UUID.
Definition uuid.c:67
Universally unique IDs.