[uuid] Add uuid_aton() to parse a UUID from a string

Add uuid_aton() to parse a UUID value from a string (analogous to
inet_aton(), inet6_aton(), sock_aton(), etc), treating it as a
32-digit hex string with optional hyphen separators.  The placement of
the separators is not checked: each byte within the hex string may be
separated by a hyphen, or not separated at all.

Add dedicated self-tests for UUID parsing and formatting (already
partially covered by the ":uuid" and ":guid" settings self-tests).

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2024-02-29 13:58:50 +00:00
parent 182ee90931
commit da7b266289
7 changed files with 203 additions and 2 deletions

View File

@@ -25,7 +25,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/base16.h>
#include <ipxe/uuid.h>
/** @file
@@ -53,3 +55,29 @@ const char * uuid_ntoa ( const union uuid *uuid ) {
uuid->canonical.e[4], uuid->canonical.e[5] );
return buf;
}
/**
* Parse UUID
*
* @v string UUID string
* @v uuid UUID to fill in
* @ret rc Return status code
*/
int uuid_aton ( const char *string, union uuid *uuid ) {
int len;
int rc;
/* Decode as hex string with optional '-' separator */
len = hex_decode ( ( '-' | HEX_DECODE_OPTIONAL ), string, uuid->raw,
sizeof ( *uuid ) );
if ( len < 0 ) {
rc = len;
return rc;
}
/* Check length */
if ( len != sizeof ( *uuid ) )
return -EINVAL;
return 0;
}