]> git.ipfire.org Git - thirdparty/mdadm.git/blob - uuid.c
Document PPL in man md
[thirdparty/mdadm.git] / uuid.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include <string.h>
26
27 const int uuid_zero[4] = { 0, 0, 0, 0 };
28
29 int same_uuid(int a[4], int b[4], int swapuuid)
30 {
31 if (swapuuid) {
32 /* parse uuids are hostendian.
33 * uuid's from some superblocks are big-ending
34 * if there is a difference, we need to swap..
35 */
36 unsigned char *ac = (unsigned char *)a;
37 unsigned char *bc = (unsigned char *)b;
38 int i;
39 for (i = 0; i < 16; i += 4) {
40 if (ac[i+0] != bc[i+3] ||
41 ac[i+1] != bc[i+2] ||
42 ac[i+2] != bc[i+1] ||
43 ac[i+3] != bc[i+0])
44 return 0;
45 }
46 return 1;
47 } else {
48 if (a[0]==b[0] &&
49 a[1]==b[1] &&
50 a[2]==b[2] &&
51 a[3]==b[3])
52 return 1;
53 return 0;
54 }
55 }
56
57 void copy_uuid(void *a, int b[4], int swapuuid)
58 {
59 if (swapuuid) {
60 /* parse uuids are hostendian.
61 * uuid's from some superblocks are big-ending
62 * if there is a difference, we need to swap..
63 */
64 unsigned char *ac = (unsigned char *)a;
65 unsigned char *bc = (unsigned char *)b;
66 int i;
67 for (i = 0; i < 16; i += 4) {
68 ac[i+0] = bc[i+3];
69 ac[i+1] = bc[i+2];
70 ac[i+2] = bc[i+1];
71 ac[i+3] = bc[i+0];
72 }
73 } else
74 memcpy(a, b, 16);
75 }
76
77 /*
78 * Parse a 128 bit uuid in 4 integers
79 * format is 32 hexx nibbles with options :.<space> separator
80 * If not exactly 32 hex digits are found, return 0
81 * else return 1
82 */
83 int parse_uuid(char *str, int uuid[4])
84 {
85 int hit = 0; /* number of Hex digIT */
86 int i;
87 char c;
88 for (i = 0; i < 4; i++)
89 uuid[i] = 0;
90
91 while ((c = *str++) != 0) {
92 int n;
93 if (c >= '0' && c <= '9')
94 n = c-'0';
95 else if (c >= 'a' && c <= 'f')
96 n = 10 + c - 'a';
97 else if (c >= 'A' && c <= 'F')
98 n = 10 + c - 'A';
99 else if (strchr(":. -", c))
100 continue;
101 else return 0;
102
103 if (hit<32) {
104 uuid[hit/8] <<= 4;
105 uuid[hit/8] += n;
106 }
107 hit++;
108 }
109 if (hit == 32)
110 return 1;
111 return 0;
112 }