xref: /freebsd/usr.bin/random/randomize_fd.c (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
1 /*
2  * Copyright (C) 2003 Sean Chittenden <seanc@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include "randomize_fd.h"
43 
44 static struct rand_node *rand_root;
45 static struct rand_node *rand_tail;
46 
47 static struct rand_node *
48 rand_node_allocate(void)
49 {
50 	struct rand_node *n;
51 
52 	n = (struct rand_node *)malloc(sizeof(struct rand_node));
53 	if (n == NULL)
54 		err(1, "malloc");
55 
56 	n->len = 0;
57 	n->cp = NULL;
58 	n->next = NULL;
59 	return(n);
60 }
61 
62 static void
63 rand_node_free(struct rand_node *n)
64 {
65 	if (n != NULL) {
66 		if (n->cp != NULL)
67 			free(n->cp);
68 
69 		free(n);
70 	}
71 }
72 
73 static void
74 rand_node_free_rec(struct rand_node *n)
75 {
76 	if (n != NULL) {
77 		if (n->next != NULL)
78 			rand_node_free_rec(n->next);
79 
80 		rand_node_free(n);
81 	}
82 }
83 
84 static void
85 rand_node_append(struct rand_node *n)
86 {
87 	if (rand_root == NULL)
88 		rand_root = rand_tail = n;
89 	else {
90 		rand_tail->next = n;
91 		rand_tail = n;
92 	}
93 }
94 
95 int
96 randomize_fd(int fd, int type, int unique, double denom)
97 {
98 	u_char *buf;
99 	u_int slen;
100 	u_long i, j, numnode, selected;
101 	struct rand_node *n, *prev;
102 	int bufleft, eof, fndstr, ret;
103 	size_t bufc, buflen;
104 	ssize_t len;
105 
106 	rand_root = rand_tail = NULL;
107 	bufc = i = 0;
108 	bufleft = eof = fndstr = numnode = 0;
109 
110 	if (type == RANDOM_TYPE_UNSET)
111 		type = RANDOM_TYPE_LINES;
112 
113 	buflen = sizeof(u_char) * MAXBSIZE;
114 	buf = (u_char *)malloc(buflen);
115 	if (buf == NULL)
116 		err(1, "malloc");
117 
118 	while (!eof) {
119 		/* Check to see if we have bits in the buffer */
120 		if (bufleft == 0) {
121 			len = read(fd, buf, buflen);
122 			if (len == -1)
123 				err(1, "read");
124 			else if (len == 0) {
125 				eof++;
126 				break;
127 			} else if ((size_t)len < buflen)
128 				buflen = (size_t)len;
129 
130 			bufleft = (int)len;
131 		}
132 
133 		/* Look for a newline */
134 		for (i = bufc; i <= buflen && bufleft >= 0; i++, bufleft--) {
135 			if (i == buflen) {
136 				if (fndstr) {
137 					if (!eof) {
138 						memmove(buf, &buf[bufc], i - bufc);
139 						i -= bufc;
140 						bufc = 0;
141 						len = read(fd, &buf[i], buflen - i);
142 						if (len == -1)
143 							err(1, "read");
144 						else if (len == 0) {
145 							eof++;
146 							break;
147 						} else if (len < (ssize_t)(buflen - i))
148 							buflen = i + (size_t)len;
149 
150 						bufleft = (int)len;
151 						fndstr = 0;
152 					}
153 				} else {
154 					buflen *= 2;
155 					buf = (u_char *)realloc(buf, buflen);
156 					if (buf == NULL)
157 						err(1, "realloc");
158 
159 					if (!eof) {
160 						len = read(fd, &buf[i], buflen - i);
161 						if (len == -1)
162 							err(1, "read");
163 						else if (len == 0) {
164 							eof++;
165 							break;
166 						} else if (len < (ssize_t)(buflen - i))
167 							buflen = i + (size_t)len;
168 
169 						bufleft = (int)len;
170 					}
171 
172 				}
173 			}
174 
175 			if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') ||
176 			    (type == RANDOM_TYPE_WORDS && isspace(buf[i])) ||
177 			    (eof && i == buflen - 1)) {
178 make_token:
179 				if (numnode == UINT32_MAX - 1) {
180 					errno = EFBIG;
181 					err(1, "too many delimiters");
182 				}
183 				numnode++;
184 				n = rand_node_allocate();
185 				if (-1 != (int)i) {
186 					slen = i - (u_long)bufc;
187 					n->len = slen + 2;
188 					n->cp = (u_char *)malloc(slen + 2);
189 					if (n->cp == NULL)
190 						err(1, "malloc");
191 
192 					memmove(n->cp, &buf[bufc], slen);
193 					n->cp[slen] = buf[i];
194 					n->cp[slen + 1] = '\0';
195 					bufc = i + 1;
196 				}
197 				rand_node_append(n);
198 				fndstr = 1;
199 			}
200 		}
201 	}
202 
203 	/* Necessary evil to compensate for files that don't end with a newline */
204 	if (bufc != i) {
205 		i--;
206 		goto make_token;
207 	}
208 
209 	(void)close(fd);
210 
211 	free(buf);
212 
213 	for (i = numnode; i > 0; i--) {
214 		selected = arc4random_uniform(numnode);
215 
216 		for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
217 			if (j == selected) {
218 				if (n->cp == NULL)
219 					break;
220 
221 				if (arc4random_uniform(denom) == 0) {
222 					ret = printf("%.*s",
223 						(int)n->len - 1, n->cp);
224 					if (ret < 0)
225 						err(1, "printf");
226 				}
227 				if (unique) {
228 					if (n == rand_root)
229 						rand_root = n->next;
230 					if (n == rand_tail)
231 						rand_tail = prev;
232 
233 					prev->next = n->next;
234 					rand_node_free(n);
235 					numnode--;
236 				}
237 				break;
238 			}
239 		}
240 	}
241 
242 	fflush(stdout);
243 
244 	if (!unique)
245 		rand_node_free_rec(rand_root);
246 
247 	return(0);
248 }
249