2003-02-11 20:32:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003 Sean Chittenden <seanc@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
2003-02-11 20:32:18 +01:00
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2008-08-08 04:46:47 +02:00
|
|
|
#include <errno.h>
|
2020-02-15 20:13:37 +01:00
|
|
|
#include <stdbool.h>
|
2003-02-15 11:26:10 +01:00
|
|
|
#include <stdlib.h>
|
2019-12-13 05:37:39 +01:00
|
|
|
#include <stdint.h>
|
2003-02-15 11:26:10 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2003-02-11 20:32:18 +01:00
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
#include "randomize_fd.h"
|
|
|
|
|
|
|
|
static struct rand_node *rand_root;
|
|
|
|
static struct rand_node *rand_tail;
|
2003-02-11 20:32:18 +01:00
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
static struct rand_node *
|
|
|
|
rand_node_allocate(void)
|
2003-02-11 20:32:18 +01:00
|
|
|
{
|
|
|
|
struct rand_node *n;
|
|
|
|
|
2008-08-12 01:24:42 +02:00
|
|
|
n = (struct rand_node *)malloc(sizeof(struct rand_node));
|
2003-02-11 20:32:18 +01:00
|
|
|
if (n == NULL)
|
2008-08-12 01:24:42 +02:00
|
|
|
err(1, "malloc");
|
2003-02-11 20:32:18 +01:00
|
|
|
|
2008-08-12 01:24:42 +02:00
|
|
|
n->len = 0;
|
|
|
|
n->cp = NULL;
|
|
|
|
n->next = NULL;
|
2003-02-11 20:32:18 +01:00
|
|
|
return(n);
|
|
|
|
}
|
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
static void
|
|
|
|
rand_node_free(struct rand_node *n)
|
2003-02-11 20:32:18 +01:00
|
|
|
{
|
|
|
|
if (n != NULL) {
|
|
|
|
if (n->cp != NULL)
|
|
|
|
free(n->cp);
|
|
|
|
|
|
|
|
free(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
static void
|
|
|
|
rand_node_free_rec(struct rand_node *n)
|
2003-02-11 20:32:18 +01:00
|
|
|
{
|
|
|
|
if (n != NULL) {
|
|
|
|
if (n->next != NULL)
|
|
|
|
rand_node_free_rec(n->next);
|
|
|
|
|
|
|
|
rand_node_free(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
static void
|
|
|
|
rand_node_append(struct rand_node *n)
|
2003-02-11 20:32:18 +01:00
|
|
|
{
|
2003-02-15 11:26:10 +01:00
|
|
|
if (rand_root == NULL)
|
2003-02-11 20:32:18 +01:00
|
|
|
rand_root = rand_tail = n;
|
2003-02-15 11:26:10 +01:00
|
|
|
else {
|
2003-02-11 20:32:18 +01:00
|
|
|
rand_tail->next = n;
|
|
|
|
rand_tail = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
int
|
|
|
|
randomize_fd(int fd, int type, int unique, double denom)
|
2003-02-11 20:32:18 +01:00
|
|
|
{
|
2006-04-14 19:32:27 +02:00
|
|
|
u_char *buf;
|
2008-08-08 04:46:47 +02:00
|
|
|
u_int slen;
|
|
|
|
u_long i, j, numnode, selected;
|
2003-02-11 20:32:18 +01:00
|
|
|
struct rand_node *n, *prev;
|
2003-02-15 11:26:10 +01:00
|
|
|
int bufleft, eof, fndstr, ret;
|
2008-08-08 04:46:47 +02:00
|
|
|
size_t bufc, buflen;
|
2003-02-15 11:26:10 +01:00
|
|
|
ssize_t len;
|
2003-02-11 20:32:18 +01:00
|
|
|
|
|
|
|
rand_root = rand_tail = NULL;
|
2003-02-15 11:26:10 +01:00
|
|
|
bufc = i = 0;
|
2010-06-14 15:03:25 +02:00
|
|
|
bufleft = eof = fndstr = numnode = 0;
|
2003-02-11 20:32:18 +01:00
|
|
|
|
|
|
|
if (type == RANDOM_TYPE_UNSET)
|
|
|
|
type = RANDOM_TYPE_LINES;
|
|
|
|
|
|
|
|
buflen = sizeof(u_char) * MAXBSIZE;
|
|
|
|
buf = (u_char *)malloc(buflen);
|
|
|
|
if (buf == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
|
|
|
|
while (!eof) {
|
|
|
|
/* Check to see if we have bits in the buffer */
|
|
|
|
if (bufleft == 0) {
|
|
|
|
len = read(fd, buf, buflen);
|
|
|
|
if (len == -1)
|
|
|
|
err(1, "read");
|
2003-02-15 11:26:10 +01:00
|
|
|
else if (len == 0) {
|
2003-02-11 20:32:18 +01:00
|
|
|
eof++;
|
2003-02-15 11:26:10 +01:00
|
|
|
break;
|
|
|
|
} else if ((size_t)len < buflen)
|
|
|
|
buflen = (size_t)len;
|
2003-02-11 20:32:18 +01:00
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
bufleft = (int)len;
|
2003-02-11 20:32:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for a newline */
|
2003-02-15 11:26:10 +01:00
|
|
|
for (i = bufc; i <= buflen && bufleft >= 0; i++, bufleft--) {
|
2003-02-11 20:32:18 +01:00
|
|
|
if (i == buflen) {
|
|
|
|
if (fndstr) {
|
|
|
|
if (!eof) {
|
|
|
|
memmove(buf, &buf[bufc], i - bufc);
|
2003-02-15 11:26:10 +01:00
|
|
|
i -= bufc;
|
2003-02-11 20:32:18 +01:00
|
|
|
bufc = 0;
|
|
|
|
len = read(fd, &buf[i], buflen - i);
|
|
|
|
if (len == -1)
|
|
|
|
err(1, "read");
|
|
|
|
else if (len == 0) {
|
|
|
|
eof++;
|
|
|
|
break;
|
2003-02-15 11:26:10 +01:00
|
|
|
} else if (len < (ssize_t)(buflen - i))
|
|
|
|
buflen = i + (size_t)len;
|
2003-02-11 20:32:18 +01:00
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
bufleft = (int)len;
|
2003-02-11 20:32:18 +01:00
|
|
|
fndstr = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2006-04-14 19:32:27 +02:00
|
|
|
buflen *= 2;
|
|
|
|
buf = (u_char *)realloc(buf, buflen);
|
|
|
|
if (buf == NULL)
|
2003-02-11 20:32:18 +01:00
|
|
|
err(1, "realloc");
|
|
|
|
|
|
|
|
if (!eof) {
|
2006-04-14 19:32:27 +02:00
|
|
|
len = read(fd, &buf[i], buflen - i);
|
2003-02-11 20:32:18 +01:00
|
|
|
if (len == -1)
|
|
|
|
err(1, "read");
|
|
|
|
else if (len == 0) {
|
|
|
|
eof++;
|
|
|
|
break;
|
2003-02-15 11:26:10 +01:00
|
|
|
} else if (len < (ssize_t)(buflen - i))
|
2006-04-14 19:32:27 +02:00
|
|
|
buflen = i + (size_t)len;
|
2003-02-11 20:32:18 +01:00
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
bufleft = (int)len;
|
2003-02-11 20:32:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') ||
|
2006-04-14 19:32:27 +02:00
|
|
|
(type == RANDOM_TYPE_WORDS && isspace(buf[i])) ||
|
2003-02-11 20:32:18 +01:00
|
|
|
(eof && i == buflen - 1)) {
|
2016-05-12 00:04:28 +02:00
|
|
|
make_token:
|
2019-12-13 05:37:39 +01:00
|
|
|
if (numnode == UINT32_MAX - 1) {
|
2008-08-08 04:46:47 +02:00
|
|
|
errno = EFBIG;
|
2008-08-10 13:31:56 +02:00
|
|
|
err(1, "too many delimiters");
|
2008-08-08 04:46:47 +02:00
|
|
|
}
|
|
|
|
numnode++;
|
2003-02-11 20:32:18 +01:00
|
|
|
n = rand_node_allocate();
|
2003-02-15 11:26:10 +01:00
|
|
|
if (-1 != (int)i) {
|
|
|
|
slen = i - (u_long)bufc;
|
|
|
|
n->len = slen + 2;
|
|
|
|
n->cp = (u_char *)malloc(slen + 2);
|
|
|
|
if (n->cp == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
|
|
|
|
memmove(n->cp, &buf[bufc], slen);
|
|
|
|
n->cp[slen] = buf[i];
|
|
|
|
n->cp[slen + 1] = '\0';
|
|
|
|
bufc = i + 1;
|
|
|
|
}
|
2003-02-11 20:32:18 +01:00
|
|
|
rand_node_append(n);
|
2003-02-15 11:26:10 +01:00
|
|
|
fndstr = 1;
|
2003-02-11 20:32:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 11:26:10 +01:00
|
|
|
/* Necessary evil to compensate for files that don't end with a newline */
|
|
|
|
if (bufc != i) {
|
|
|
|
i--;
|
|
|
|
goto make_token;
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:14:05 +02:00
|
|
|
(void)close(fd);
|
|
|
|
|
2012-10-22 05:06:59 +02:00
|
|
|
free(buf);
|
|
|
|
|
2003-02-11 20:32:18 +01:00
|
|
|
for (i = numnode; i > 0; i--) {
|
2020-01-16 22:38:44 +01:00
|
|
|
selected = arc4random_uniform(numnode);
|
2003-02-11 20:32:18 +01:00
|
|
|
|
|
|
|
for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
|
|
|
|
if (j == selected) {
|
2003-02-15 11:26:10 +01:00
|
|
|
if (n->cp == NULL)
|
|
|
|
break;
|
|
|
|
|
2020-02-15 20:13:37 +01:00
|
|
|
if (random_uniform_denom(denom)) {
|
2008-08-12 01:24:42 +02:00
|
|
|
ret = printf("%.*s",
|
|
|
|
(int)n->len - 1, n->cp);
|
2008-08-08 04:46:47 +02:00
|
|
|
if (ret < 0)
|
|
|
|
err(1, "printf");
|
|
|
|
}
|
2003-02-11 20:32:18 +01:00
|
|
|
if (unique) {
|
|
|
|
if (n == rand_root)
|
|
|
|
rand_root = n->next;
|
|
|
|
if (n == rand_tail)
|
|
|
|
rand_tail = prev;
|
|
|
|
|
|
|
|
prev->next = n->next;
|
|
|
|
rand_node_free(n);
|
|
|
|
numnode--;
|
|
|
|
}
|
2008-08-08 04:46:47 +02:00
|
|
|
break;
|
2003-02-11 20:32:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if (!unique)
|
|
|
|
rand_node_free_rec(rand_root);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|