Tizen 2.0 Release
[external/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., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #include "knuth-lfib.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <time.h>
35
36 #define BUFSIZE 500
37
38 static void
39 usage(void)
40 {
41   fprintf(stderr, "Usage: lfib-stream [SEED]\n");
42 }
43
44 int
45 main(int argc, char **argv)
46 {
47   struct knuth_lfib_ctx ctx;
48   uint32_t seed;
49
50   if (argc == 1)
51     seed = time(NULL);
52
53   else if (argc == 2)
54     {
55       seed = atoi(argv[1]);
56       if (!seed)
57         {
58           usage();
59           return EXIT_FAILURE;
60         }
61     }
62   else
63     {
64       usage();
65       return EXIT_FAILURE;
66     }
67
68   knuth_lfib_init(&ctx, seed);
69
70   for (;;)
71     {
72       char buffer[BUFSIZE];
73       knuth_lfib_random(&ctx, BUFSIZE, buffer);
74
75       if (fwrite(buffer, 1, BUFSIZE, stdout) < BUFSIZE
76           || fflush(stdout) < 0)
77         return EXIT_FAILURE;
78     }
79
80   /* Not reached. This program is usually terminated by SIGPIPE */
81 }