]>
sourceware.org Git - lvm2.git/blob - lib/uuid/uuid.c
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "lvm-wrappers.h"
26 static const char _c
[] =
27 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#";
29 static int _built_inverse
;
30 static char _inverse_c
[256];
32 int lvid_create(union lvid
*lvid
, struct id
*vgid
)
34 memcpy(lvid
->id
, vgid
, sizeof(*lvid
->id
));
35 return id_create(&lvid
->id
[1]);
38 void uuid_from_num(char *uuid
, uint32_t num
)
42 for (i
= ID_LEN
; i
; i
--) {
43 uuid
[i
- 1] = _c
[num
% (sizeof(_c
) - 1)];
44 num
/= sizeof(_c
) - 1;
48 int lvid_from_lvnum(union lvid
*lvid
, struct id
*vgid
, uint32_t lv_num
)
52 memcpy(lvid
->id
, vgid
, sizeof(*lvid
->id
));
54 for (i
= ID_LEN
; i
; i
--) {
55 lvid
->id
[1].uuid
[i
- 1] = _c
[lv_num
% (sizeof(_c
) - 1)];
56 lv_num
/= sizeof(_c
) - 1;
59 lvid
->s
[sizeof(lvid
->s
) - 1] = '\0';
64 int lvnum_from_lvid(union lvid
*lvid
)
69 for (i
= 0; i
< ID_LEN
; i
++) {
70 lv_num
*= sizeof(_c
) - 1;
71 if ((c
= strchr(_c
, lvid
->id
[1].uuid
[i
])))
72 lv_num
+= (int) (c
- _c
);
80 int lvid_in_restricted_range(union lvid
*lvid
)
84 for (i
= 0; i
< ID_LEN
- 3; i
++)
85 if (lvid
->id
[1].uuid
[i
] != '0')
88 for (i
= ID_LEN
- 3; i
< ID_LEN
; i
++)
89 if (!isdigit(lvid
->id
[1].uuid
[i
]))
96 int id_create(struct id
*id
)
99 size_t len
= sizeof(id
->uuid
);
101 memset(id
->uuid
, 0, len
);
102 if (!read_urandom(&id
->uuid
, len
)) {
107 * Skip out the last 2 chars in randomized creation for LVM1
108 * backwards compatibility.
110 for (i
= 0; i
< len
; i
++)
111 id
->uuid
[i
] = _c
[id
->uuid
[i
] % (sizeof(_c
) - 3)];
117 * The only validity check we have is that
118 * the uuid just contains characters from
119 * '_c'. A checksum would have been nice :(
121 static void _build_inverse(void)
129 memset(_inverse_c
, 0, sizeof(_inverse_c
));
131 for (ptr
= _c
; *ptr
; ptr
++)
132 _inverse_c
[(int) *ptr
] = (char) 0x1;
135 int id_valid(struct id
*id
)
141 for (i
= 0; i
< ID_LEN
; i
++)
142 if (!_inverse_c
[id
->uuid
[i
]]) {
143 log_error("UUID contains invalid character");
150 int id_equal(const struct id
*lhs
, const struct id
*rhs
)
152 return !memcmp(lhs
->uuid
, rhs
->uuid
, sizeof(lhs
->uuid
));
155 #define GROUPS (ID_LEN / 4)
157 int id_write_format(const struct id
*id
, char *buffer
, size_t size
)
161 static const unsigned group_size
[] = { 6, 4, 4, 4, 4, 4, 6 };
163 assert(ID_LEN
== 32);
165 /* split into groups separated by dashes */
166 if (size
< (32 + 6 + 1)) {
167 log_error("Couldn't write uuid, buffer too small.");
171 for (i
= 0, tot
= 0; i
< 7; i
++) {
172 memcpy(buffer
, id
->uuid
+ tot
, group_size
[i
]);
173 buffer
+= group_size
[i
];
174 tot
+= group_size
[i
];
182 int id_read_format(struct id
*id
, const char *buffer
)
186 /* just strip out any dashes */
189 if (*buffer
== '-') {
195 log_error("Too many characters to be uuid.");
199 id
->uuid
[out
++] = *buffer
++;
203 log_error("Couldn't read uuid: incorrect number of "
211 char *id_format_and_copy(struct dm_pool
*mem
, const struct id
*id
)
215 if (!(repstr
= dm_pool_alloc(mem
, 40))) {
216 log_error("dm_pool_alloc failed");
220 if (!id_write_format(id
, repstr
, 40))
This page took 0.047022 seconds and 5 git commands to generate.