2 * Copyright 2011 Calxeda, Inc.
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 #include <linux/ctype.h>
27 * This is what a UUID string looks like.
29 * x is a hexadecimal character. fields are separated by '-'s. When converting
30 * to a binary UUID, le means the field should be converted to little endian,
31 * and be means it should be converted to big endian.
34 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
38 int uuid_str_valid(const char *uuid)
45 for (i = 0, valid = 1; uuid[i] && valid; i++) {
47 case 8: case 13: case 18: case 23:
48 valid = (uuid[i] == '-');
51 valid = isxdigit(uuid[i]);
56 if (i != 36 || !valid)
62 void uuid_str_to_bin(const char *uuid, unsigned char *out)
71 tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16));
72 memcpy(out, &tmp32, 4);
74 tmp16 = cpu_to_le16(simple_strtoul(uuid + 9, NULL, 16));
75 memcpy(out + 4, &tmp16, 2);
77 tmp16 = cpu_to_le16(simple_strtoul(uuid + 14, NULL, 16));
78 memcpy(out + 6, &tmp16, 2);
80 tmp16 = cpu_to_be16(simple_strtoul(uuid + 19, NULL, 16));
81 memcpy(out + 8, &tmp16, 2);
83 tmp64 = cpu_to_be64(simple_strtoull(uuid + 24, NULL, 16));
84 memcpy(out + 10, (char *)&tmp64 + 2, 6);