2 * Copyright (C) 2003 Sean Chittenden <seanc@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/param.h>
43 #include "randomize_fd.h"
45 static struct rand_node *rand_root;
46 static struct rand_node *rand_tail;
48 static struct rand_node *
49 rand_node_allocate(void)
53 n = (struct rand_node *)malloc(sizeof(struct rand_node));
64 rand_node_free(struct rand_node *n)
75 rand_node_free_rec(struct rand_node *n)
79 rand_node_free_rec(n->next);
86 rand_node_append(struct rand_node *n)
88 if (rand_root == NULL)
89 rand_root = rand_tail = n;
97 randomize_fd(int fd, int type, int unique, double denom)
101 u_long i, j, numnode, selected;
102 struct rand_node *n, *prev;
103 int bufleft, eof, fndstr, ret;
107 rand_root = rand_tail = NULL;
109 bufleft = eof = fndstr = numnode = 0;
111 if (type == RANDOM_TYPE_UNSET)
112 type = RANDOM_TYPE_LINES;
114 buflen = sizeof(u_char) * MAXBSIZE;
115 buf = (u_char *)malloc(buflen);
120 /* Check to see if we have bits in the buffer */
122 len = read(fd, buf, buflen);
128 } else if ((size_t)len < buflen)
129 buflen = (size_t)len;
134 /* Look for a newline */
135 for (i = bufc; i <= buflen && bufleft >= 0; i++, bufleft--) {
139 memmove(buf, &buf[bufc], i - bufc);
142 len = read(fd, &buf[i], buflen - i);
148 } else if (len < (ssize_t)(buflen - i))
149 buflen = i + (size_t)len;
156 buf = (u_char *)realloc(buf, buflen);
161 len = read(fd, &buf[i], buflen - i);
167 } else if (len < (ssize_t)(buflen - i))
168 buflen = i + (size_t)len;
176 if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') ||
177 (type == RANDOM_TYPE_WORDS && isspace(buf[i])) ||
178 (eof && i == buflen - 1)) {
180 if (numnode == UINT32_MAX - 1) {
182 err(1, "too many delimiters");
185 n = rand_node_allocate();
187 slen = i - (u_long)bufc;
189 n->cp = (u_char *)malloc(slen + 2);
193 memmove(n->cp, &buf[bufc], slen);
194 n->cp[slen] = buf[i];
195 n->cp[slen + 1] = '\0';
204 /* Necessary evil to compensate for files that don't end with a newline */
214 for (i = numnode; i > 0; i--) {
215 selected = arc4random_uniform(numnode);
217 for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
222 if (random_uniform_denom(denom)) {
224 (int)n->len - 1, n->cp);
234 prev->next = n->next;
246 rand_node_free_rec(rand_root);