Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011 Calxeda, Inc. |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/ctype.h> |
| 24 | #include "common.h" |
| 25 | |
| 26 | /* |
| 27 | * This is what a UUID string looks like. |
| 28 | * |
| 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. |
| 32 | * |
| 33 | * 0 9 14 19 24 |
| 34 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 35 | * le le le be be |
| 36 | */ |
| 37 | |
| 38 | int uuid_str_valid(const char *uuid) |
| 39 | { |
| 40 | int i, valid; |
| 41 | |
| 42 | if (uuid == NULL) |
| 43 | return 0; |
| 44 | |
| 45 | for (i = 0, valid = 1; uuid[i] && valid; i++) { |
| 46 | switch (i) { |
| 47 | case 8: case 13: case 18: case 23: |
| 48 | valid = (uuid[i] == '-'); |
| 49 | break; |
| 50 | default: |
| 51 | valid = isxdigit(uuid[i]); |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (i != 36 || !valid) |
| 57 | return 0; |
| 58 | |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | void uuid_str_to_bin(const char *uuid, unsigned char *out) |
| 63 | { |
| 64 | uint16_t tmp16; |
| 65 | uint32_t tmp32; |
| 66 | uint64_t tmp64; |
| 67 | |
| 68 | if (!uuid || !out) |
| 69 | return; |
| 70 | |
| 71 | tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16)); |
| 72 | memcpy(out, &tmp32, 4); |
| 73 | |
| 74 | tmp16 = cpu_to_le16(simple_strtoul(uuid + 9, NULL, 16)); |
| 75 | memcpy(out + 4, &tmp16, 2); |
| 76 | |
| 77 | tmp16 = cpu_to_le16(simple_strtoul(uuid + 14, NULL, 16)); |
| 78 | memcpy(out + 6, &tmp16, 2); |
| 79 | |
| 80 | tmp16 = cpu_to_be16(simple_strtoul(uuid + 19, NULL, 16)); |
| 81 | memcpy(out + 8, &tmp16, 2); |
| 82 | |
| 83 | tmp64 = cpu_to_be64(simple_strtoull(uuid + 24, NULL, 16)); |
| 84 | memcpy(out + 10, (char *)&tmp64 + 2, 6); |
| 85 | } |