blob: c48bf3836262d507581c028f9538a5e0ae93c222 [file] [log] [blame]
Jason Hobbse11938e2011-08-23 11:06:56 +00001/*
2 * Copyright 2011 Calxeda, Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbse11938e2011-08-23 11:06:56 +00005 */
6
7#include <linux/ctype.h>
8#include "common.h"
9
10/*
11 * This is what a UUID string looks like.
12 *
13 * x is a hexadecimal character. fields are separated by '-'s. When converting
14 * to a binary UUID, le means the field should be converted to little endian,
15 * and be means it should be converted to big endian.
16 *
17 * 0 9 14 19 24
18 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
19 * le le le be be
20 */
21
22int uuid_str_valid(const char *uuid)
23{
24 int i, valid;
25
26 if (uuid == NULL)
27 return 0;
28
29 for (i = 0, valid = 1; uuid[i] && valid; i++) {
30 switch (i) {
31 case 8: case 13: case 18: case 23:
32 valid = (uuid[i] == '-');
33 break;
34 default:
35 valid = isxdigit(uuid[i]);
36 break;
37 }
38 }
39
40 if (i != 36 || !valid)
41 return 0;
42
43 return 1;
44}
45
46void uuid_str_to_bin(const char *uuid, unsigned char *out)
47{
48 uint16_t tmp16;
49 uint32_t tmp32;
50 uint64_t tmp64;
51
52 if (!uuid || !out)
53 return;
54
55 tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16));
56 memcpy(out, &tmp32, 4);
57
58 tmp16 = cpu_to_le16(simple_strtoul(uuid + 9, NULL, 16));
59 memcpy(out + 4, &tmp16, 2);
60
61 tmp16 = cpu_to_le16(simple_strtoul(uuid + 14, NULL, 16));
62 memcpy(out + 6, &tmp16, 2);
63
64 tmp16 = cpu_to_be16(simple_strtoul(uuid + 19, NULL, 16));
65 memcpy(out + 8, &tmp16, 2);
66
67 tmp64 = cpu_to_be64(simple_strtoull(uuid + 24, NULL, 16));
68 memcpy(out + 10, (char *)&tmp64 + 2, 6);
69}