File: randutil.c

package info (click to toggle)
fragroute 1.2-7.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 692 kB
  • ctags: 520
  • sloc: ansic: 4,331; sh: 2,968; makefile: 74
file content (49 lines) | stat: -rw-r--r-- 850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * randutil.c
 *
 * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
 *
 * $Id: randutil.c,v 1.1 2002/04/07 22:55:20 dugsong Exp $
 */

#include "config.h"

#include <dumbnet.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';
}