12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- /*
- * randutil.c
- *
- * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
- *
- * $Id: randutil.c 2000 2008-04-27 06:17:35Z aturner $
- */
- #include "config.h"
- #include <dnet.h>
- #include <err.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "randutil.h"
- static const char base64[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- void
- rand_strset(rand_t *r, void *buf, size_t len)
- {
- uint32_t u;
- char *p;
- int i;
- p = (char *)buf;
- i = (len + 3) / 4;
- u = rand_uint32(r);
- /* XXX - more Duff's device tomfoolery. */
- switch (len % 4) {
- case 0: do {
- u = rand_uint32(r);
- *p++ = base64[(u >> 18) & 0x3f];
- case 3:
- *p++ = base64[(u >> 12) & 0x3f];
- case 2:
- *p++ = base64[(u >> 6) & 0x3f];
- case 1:
- *p++ = base64[(u >> 0) & 0x3f];
- } while (--i > 0);
- }
- p[-1] = '\0';
- }
|