diff options
author | Vladimir Dronnikov <dronnikov@gmail.com> | 2009-10-15 09:24:25 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-10-15 09:24:25 +0200 |
commit | db67a20595be279e7db9f5f8e27bd94534efb8d4 (patch) | |
tree | 0263d0ac18ff70781d9a3074f12f5abcd2e18dfc /libbb | |
parent | 7eabffafa5b6faaaa87ff8ba0efbf637aaa364e8 (diff) | |
download | busybox-w32-db67a20595be279e7db9f5f8e27bd94534efb8d4.tar.gz busybox-w32-db67a20595be279e7db9f5f8e27bd94534efb8d4.tar.bz2 busybox-w32-db67a20595be279e7db9f5f8e27bd94534efb8d4.zip |
move generate_uuid from mkswap to libbb
Signed-off-by: Vladimir Dronnikov <dronnikov@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xfuncs.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index aa0812914..e47b01dc1 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -209,3 +209,66 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) | |||
209 | { | 209 | { |
210 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); | 210 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); |
211 | } | 211 | } |
212 | |||
213 | void FAST_FUNC generate_uuid(uint8_t *buf) | ||
214 | { | ||
215 | /* http://www.ietf.org/rfc/rfc4122.txt | ||
216 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
217 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
218 | * | time_low | | ||
219 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
220 | * | time_mid | time_hi_and_version | | ||
221 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
222 | * |clk_seq_and_variant | node (0-1) | | ||
223 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
224 | * | node (2-5) | | ||
225 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
226 | * IOW, uuid has this layout: | ||
227 | * uint32_t time_low (big endian) | ||
228 | * uint16_t time_mid (big endian) | ||
229 | * uint16_t time_hi_and_version (big endian) | ||
230 | * version is a 4-bit field: | ||
231 | * 1 Time-based | ||
232 | * 2 DCE Security, with embedded POSIX UIDs | ||
233 | * 3 Name-based (MD5) | ||
234 | * 4 Randomly generated | ||
235 | * 5 Name-based (SHA-1) | ||
236 | * uint16_t clk_seq_and_variant (big endian) | ||
237 | * variant is a 3-bit field: | ||
238 | * 0xx Reserved, NCS backward compatibility | ||
239 | * 10x The variant specified in rfc4122 | ||
240 | * 110 Reserved, Microsoft backward compatibility | ||
241 | * 111 Reserved for future definition | ||
242 | * uint8_t node[6] | ||
243 | * | ||
244 | * For version 4, these bits are set/cleared: | ||
245 | * time_hi_and_version & 0x0fff | 0x4000 | ||
246 | * clk_seq_and_variant & 0x3fff | 0x8000 | ||
247 | */ | ||
248 | pid_t pid; | ||
249 | int i; | ||
250 | |||
251 | i = open("/dev/urandom", O_RDONLY); | ||
252 | if (i >= 0) { | ||
253 | read(i, buf, 16); | ||
254 | close(i); | ||
255 | } | ||
256 | /* Paranoia. /dev/urandom may be missing. | ||
257 | * rand() is guaranteed to generate at least [0, 2^15) range, | ||
258 | * but lowest bits in some libc are not so "random". */ | ||
259 | srand(monotonic_us()); | ||
260 | pid = getpid(); | ||
261 | while (1) { | ||
262 | for (i = 0; i < 16; i++) | ||
263 | buf[i] ^= rand() >> 5; | ||
264 | if (pid == 0) | ||
265 | break; | ||
266 | srand(pid); | ||
267 | pid = 0; | ||
268 | } | ||
269 | |||
270 | /* version = 4 */ | ||
271 | buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; | ||
272 | /* variant = 10x */ | ||
273 | buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; | ||
274 | } | ||