Revert "Merge branch 'upstream' into tizen"
[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  */
7  
8 /* nettle, low-level cryptographics library
9  *
10  * Copyright (C) 2003 Niels Möller
11  *  
12  * The nettle library is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or (at your
15  * option) any later version.
16  * 
17  * The nettle library is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20  * License for more details.
21  * 
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with the nettle library; see the file COPYING.LIB.  If not, write to
24  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25  * MA 02111-1301, USA.
26  */
27
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include "knuth-lfib.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <time.h>
39
40 #define BUFSIZE 500
41
42 static void
43 usage(void)
44 {
45   fprintf(stderr, "Usage: lfib-stream [SEED]\n");
46 }
47
48 int
49 main(int argc, char **argv)
50 {
51   struct knuth_lfib_ctx ctx;
52   uint32_t seed;
53
54   if (argc == 1)
55     seed = time(NULL);
56
57   else if (argc == 2)
58     {
59       seed = atoi(argv[1]);
60       if (!seed)
61         {
62           usage();
63           return EXIT_FAILURE;
64         }
65     }
66   else
67     {
68       usage();
69       return EXIT_FAILURE;
70     }
71
72   knuth_lfib_init(&ctx, seed);
73
74   for (;;)
75     {
76       char buffer[BUFSIZE];
77       knuth_lfib_random(&ctx, BUFSIZE, buffer);
78
79       if (fwrite(buffer, 1, BUFSIZE, stdout) < BUFSIZE
80           || fflush(stdout) < 0)
81         return EXIT_FAILURE;
82     }
83
84   /* Not reached. This program is usually terminated by SIGPIPE */
85 }