c45317d605c174f0454d4815f53873b19b6ee691
[platform/upstream/nettle.git] / tools / nettle-lfib-stream.c
1 /* lfib-stream.c
2
3    Generates a pseudorandom stream, using the Knuth lfib
4    (non-cryptographic) pseudorandom generator.
5
6    Copyright (C) 2003 Niels Möller
7
8    This file is part of GNU Nettle.
9
10    GNU Nettle is free software: you can redistribute it and/or
11    modify it under the terms of either:
12
13      * the GNU Lesser General Public License as published by the Free
14        Software Foundation; either version 3 of the License, or (at your
15        option) any later version.
16
17    or
18
19      * the GNU General Public License as published by the Free
20        Software Foundation; either version 2 of the License, or (at your
21        option) any later version.
22
23    or both in parallel, as here.
24
25    GNU Nettle is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28    General Public License for more details.
29
30    You should have received copies of the GNU General Public License and
31    the GNU Lesser General Public License along with this program.  If
32    not, see http://www.gnu.org/licenses/.
33 */
34
35 #if HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include "knuth-lfib.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <time.h>
46
47 #define BUFSIZE 500
48
49 static void
50 usage(void)
51 {
52   fprintf(stderr, "Usage: lfib-stream [SEED]\n");
53 }
54
55 int
56 main(int argc, char **argv)
57 {
58   struct knuth_lfib_ctx ctx;
59   uint32_t seed;
60
61   if (argc == 1)
62     seed = time(NULL);
63
64   else if (argc == 2)
65     {
66       seed = atoi(argv[1]);
67       if (!seed)
68         {
69           usage();
70           return EXIT_FAILURE;
71         }
72     }
73   else
74     {
75       usage();
76       return EXIT_FAILURE;
77     }
78
79   knuth_lfib_init(&ctx, seed);
80
81   for (;;)
82     {
83       char buffer[BUFSIZE];
84       knuth_lfib_random(&ctx, BUFSIZE, buffer);
85
86       if (fwrite(buffer, 1, BUFSIZE, stdout) < BUFSIZE
87           || fflush(stdout) < 0)
88         return EXIT_FAILURE;
89     }
90
91   /* Not reached. This program is usually terminated by SIGPIPE */
92 }