summaryrefslogtreecommitdiff
path: root/support/dktools/dk-rand.c
blob: 1eecf01f420e038ae8c0d525db8198f93ed741fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk-rand.ctr
*/

/*
Copyright (C) 2015-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
*/

/**	@file dk-rand.c The dk-rand module.
*/


#line 9 "dk-rand.ctr"

#include "dk4conf.h"
#include "dk4types.h"

#include <stdio.h>

#if DK4_ON_WINDOWS
/**	Allow use of the getenv() function.  */
#define	_CRT_SECURE_NO_WARNINGS	1
#endif

#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define	WINDOWS_H_INCLUDED 1
#endif
#ifndef WINCRYPT_H_INCLUDED
#include <wincrypt.h>
#define	WINCRYPT_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_UNISTD_H
#ifndef UNISTD_H_INCLUDED
#include <unistd.h>
#define	UNISTD_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define SYS_TYPES_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_SIGNAL_H
#ifndef SIGNAL_H_INCLUDED
#include <signal.h>
#define SIGNAL_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_PROCESS_H
#ifndef PROCESS_H_INCLUDED
#include <process.h>
#define	PROCESS_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_IO_H
#ifndef IO_H_INCLUDED
#include <io.h>
#define IO_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_FCNTL_H
#ifndef FCNTL_H_INCLUDED
#include <fcntl.h>
#define	FCNTL_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_OPENSSL_RAND_H

#ifndef OPENSSL_RAND_H_INCLUDED
#include <openssl/rand.h>
#define	OPENSSL_RAND_H_INCLUDED	1
#endif

#include "dk4const.h"
#include "dk4mem.h"
#include "dk4mema.h"
#include "dk4memrs.h"
#include "dk4strd.h"
#include "dk4fput.h"
#include "dk4vers.h"
#include "dk4maiddsz.h"
#include "dk4maasz.h"
#include "dk4mpl.h"
#include "dk4svbuf.h"





#line 95 "dk-rand.ctr"




/**	Buffer for random data.
*/
static	char		dk_rand_buf[16384];



#ifndef BUFSIZ
#define BUFSIZ	8192
#endif

/**	Use an explicitly specified buffer for stdout operations
	so we can securely reset the memory before exiting.
*/
static	char	stdout_buffer[BUFSIZ];


/**	Buffer to read seed file.
*/
static	char	seed_file_buffer[BUFSIZ];



/**	Help text to show for --help option.
*/
static const char * const	help_text[] = {
"",
"Generate binary random data",
"",
"dk-rand [-b bytes]",
"",
"-b bytes\tNumber of bytes to generate.",
"",
"--help\t\tShow this short help text.",
"--version\tShow version information.",
"--license\tShow license information.",
"",
"http://dktools.sourceforge.net",
"",
NULL


#line 139 "dk-rand.ctr"
};



/**	License information to show for --license option.
*/
static const char * const	license_text[] = {
"This software uses code from the following projects, either directly or as",
"a library:",
"",
"dktools\t\tDirk Krause's tools and libraries.",
"\t\tSee http://dktools.sourceforge.net/ for more information.",
#if DK4_HAVE_OPENSSL_RAND_H
"",
"OpenSSL\t\tCryptographic toolkit, used to build file checksums here.",
"\t\tSee http://www.openssl.org/ for more information.",
"",
"This product includes software developed by the OpenSSL Project for use in",
"the OpenSSL Toolkit (http://www.openssl.org/).",
"This product includes cryptographic software written by",
"Eric Young (eay@cryptsoft.com).",
"This product includes software written by Tim Hudson (tjh@cryptsoft.com).",
#endif
"",
"All the licenses below apply to the program.",
"Licenses for used libraries are shown as found on my Scientific Linux 6.x",
"computer in the /usr/share/doc directory on 2015-04-01. Check the project",
"homepages of the used libraries for additional information and/or updated",
"license terms.",
"",
"",
"DK tools and libraries license",
"==============================",
"Copyright (c) 2015-2016, Dirk Krause",
"All rights reserved.",
"",
"Redistribution and use in source and binary forms, with or without",
"modification, are permitted provided that the following conditions are met:",
"",
"* Redistributions of source code must retain the above copyright notice,",
"  this list of conditions and the following disclaimer.",
"* 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.",
"* Neither the name of the Dirk Krause nor the names of contributors may be",
"  used to endorse or promote products derived from this software without",
"  specific prior written permission.",
"",
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.",
#if DK4_HAVE_OPENSSL_RAND_H
"",
"",
"OpenSSL license",
"===============",
"",
"LICENSE ISSUES",
"--------------",
"",
"The OpenSSL toolkit stays under a dual license, i.e. both the conditions of",
"the OpenSSL License and the original SSLeay license apply to the toolkit.",
"See below for the actual license texts. Actually both licenses are BSD-style",
"Open Source licenses. In case of any license issues related to OpenSSL",
"please contact openssl-core@openssl.org.",
"",
"OpenSSL License",
"---------------",
"",
"Copyright (c) 1998-2011 The OpenSSL Project.  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.",
"",
"3. All advertising materials mentioning features or use of this",
"   software must display the following acknowledgment:",
"   \"This product includes software developed by the OpenSSL Project",
"   for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"",
"",
"4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to",
"   endorse or promote products derived from this software without",
"   prior written permission. For written permission, please contact",
"   openssl-core@openssl.org.",
"",
"5. Products derived from this software may not be called \"OpenSSL\"",
"   nor may \"OpenSSL\" appear in their names without prior written",
"   permission of the OpenSSL Project.",
"",
"6. Redistributions of any form whatsoever must retain the following",
"   acknowledgment:",
"   \"This product includes software developed by the OpenSSL Project",
"   for use in the OpenSSL Toolkit (http://www.openssl.org/)\"",
"",
"THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY",
"EXPRESSED 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 OpenSSL PROJECT OR",
"ITS 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.",
"",
"This product includes cryptographic software written by Eric Young",
"(eay@cryptsoft.com).  This product includes software written by Tim",
"Hudson (tjh@cryptsoft.com).",
"",
"",
"Original SSLeay License",
"-----------------------",
"",
"Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)",
"All rights reserved.",
"",
"This package is an SSL implementation written",
"by Eric Young (eay@cryptsoft.com).",
"The implementation was written so as to conform with Netscapes SSL.",
"",
"This library is free for commercial and non-commercial use as long as",
"the following conditions are aheared to.  The following conditions",
"apply to all code found in this distribution, be it the RC4, RSA,",
"lhash, DES, etc., code; not just the SSL code.  The SSL documentation",
"included with this distribution is covered by the same copyright terms",
"except that the holder is Tim Hudson (tjh@cryptsoft.com).",
"",
"Copyright remains Eric Young's, and as such any Copyright notices in",
"the code are not to be removed.",
"If this package is used in a product, Eric Young should be given attribution",
"as the author of the parts of the library used.",
"This can be in the form of a textual message at program startup or",
"in documentation (online or textual) provided with the package.",
"",
"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 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.",
"3. All advertising materials mentioning features or use of this software",
"   must display the following acknowledgement:",
"   \"This product includes cryptographic software written by",
"    Eric Young (eay@cryptsoft.com)\"",
"   The word 'cryptographic' can be left out if the rouines from the library",
"   being used are not cryptographic related :-).",
"4. If you include any Windows specific code (or a derivative thereof) from ",
"   the apps directory (application code) you must include an acknowledgement:",
"   \"This product includes software written by Tim Hudson (tjh@cryptsoft.com)\"",
"",
"THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``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 AUTHOR 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.",
"",
"The licence and distribution terms for any publically available version or",
"derivative of this code cannot be changed.  i.e. this code cannot simply be",
"copied and put under another distribution licence",
"[including the GNU Public Licence.]",
"",
#endif
"",
NULL


#line 331 "dk-rand.ctr"
};



/**	Version information.
*/
static	const char	version_text[] = { DKT_VERSION_C8 };



/**	Options used by the program.
*/
static	dk4_option_t	options[] = {

  /*	Configuration file.
  */
  { { dkT('b'),	dkT("bytes"),	DK4_OPT_ARG_SIZE },	{ NULL },	0 },

  /*	1	Help.
  */
  { { dkT('\0'),dkT("help"),	DK4_OPT_ARG_NONE},	{ NULL },	0},

  /*	2	Version.
  */
  { { dkT('\0'),dkT("version"),	DK4_OPT_ARG_NONE},	{ NULL },	0},

  /*	3	License
  */
  { { dkT('\0'),dkT("license"),	DK4_OPT_ARG_NONE},	{ NULL },	0}

};


/**	Constant texts used by the program, not localized.
*/
static	const char *	dk_rand_kw[] = {
/* 0 */
"dk-rand: ",

/* 1 */
"ERROR: Failed to write to standard output!\n",

/* 2 */
"ERROR: Failed to obtain random data!\n",

/* 3 */
"ERROR: Aborted by signal!\n",

/* 4 */
"ERROR: Failed to reset used buffers!\n",

/* 5 */
"ERROR: Failed to seed OpenSSL PRNG!\n",

/* 6 */
"ERROR: Failed to set up signal handlers!\n",

/* 7 */
"ERROR: Failed to restore signal handlers!\n",

/* 8 */
"ERROR: Numeric overflow in size calculation!\n",

NULL


#line 405 "dk-rand.ctr"
};



/**	Number of options in the options array.
*/
static	const size_t	szoptions =	sizeof(options)/sizeof(dk4_option_t);



/**	Exit status code.
*/
static	int		exval	= EXIT_FAILURE;



/**	Options: Help (1), version (2), license (4).
*/
static	int		hvl	= 0;



#ifdef SIGPIPE
/**	Indicator: SIGPIPE signal received.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t	sig_had_pipe	=	0;
#endif


/**	Indicator: SIGINT signal received.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t	sig_had_int	=	0;


/**	Indicator: SIGTERM signal received.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t	sig_had_term	=	0;


/**	Pass volatile pointer through to caller.
	The CERT C coding standard recommends to set signal indicator
	variables through pointers to avoid
	compilers from optimizing too hard (they might attempt
	to keep the value in a register although properly marked
	as volatile).
	@param	ptr	Address of volatile indicator variable.
	@return	Same pointer as ptr.
*/
static
DK4_VOLATILE
dk4_sig_atomic_t *
sig_pass_pointer(DK4_VOLATILE dk4_sig_atomic_t *ptr)
{
  return ptr;
}


#ifdef SIGPIPE
/**	Handler for SIGPIPE signal.
	@param	signo	Signal number (always SIGPIPE, ignored).
*/
static
void
sig_handler_pipe(int signo)
{
  *sig_pass_pointer(&sig_had_pipe) = 1;
}
#endif


/**	Handler for SIGINT signal.
	@param	signo	Signal number (always SIGINT, ignored).
*/
static
void
sig_handler_int(int signo)
{
  *sig_pass_pointer(&sig_had_int) = 1;
}


/**	Handler for SIGTERM signal.
	@param	signo	Signal number (always SIGTERM, ignored).
*/
static
void
sig_handler_term(int signo)
{
  *sig_pass_pointer(&sig_had_term) = 1;
}


/**	Read value from volatile atomic type.
	This function is necessary as some compilers mis-optimize
	direct access to volatile variables (at least if you believe
	one of the coding standards).
	@param	ap	Pointer to volatile atomic variable.
	@return	Contents of the variable.
*/
static
dk4_sig_atomic_t
sig_read_atomic(DK4_VOLATILE dk4_sig_atomic_t *ap)
{
  return (*ap);
}


/**	Check whether we can continue or must abort due to signal.
	@param	dopipe	Flag: Check for SIGPIPE too.
	@return	1 if we can continue, 0 otherwise.
*/
static
int
can_continue(int dopipe)
{
  int		 back	= 1;
  if (0 != sig_read_atomic(&sig_had_term)) { back = 0; }
  if (0 != sig_read_atomic(&sig_had_int))  { back = 0; }
#ifdef SIGPIPE
  if (0 != dopipe) {
    if (0 != sig_read_atomic(&sig_had_pipe)) { back = 0; }
  }
#endif
  return back;
}



/**	Write a log message to stderr.
	@param	i	Message index in dk_rand_kw.
*/
static
void
log_1(size_t i)
{
  fputs(dk_rand_kw[0], stderr);
  fputs(dk_rand_kw[i], stderr);
  fflush(stderr);
}



/**	Seed pseudo random number generator.
	@return	1 on success, 0 on error.
*/
static
int
seed_prng(void)
{
  char		 buffer[64];
  char		*envptr;
#if DK4_ON_WINDOWS
  HCRYPTPROV	 hcp;
  BOOL		 res;
#else
  FILE		*fipo;
  size_t	 rdb;
#endif
  int		 cc;
  int		 rounds = 0;
  int		 back = 0;
  int		 svbres = 0;
  

#line 574 "dk-rand.ctr"

  /*	Let OpenSSL library functions try to initialize.
  */
  if (1 == RAND_status()) {
    back = 1;
    goto finished;
  }

  /*	Attempt EGD socket if specified in environment.
  */
#if	DK4_HAVE_RAND_EGD && DK4_HAVE_STRUCT_SOCKADDR_UN
  envptr = getenv("EGDSOCKET");
  if (NULL != envptr) {
    if (0 < RAND_egd(envptr)) {
      if (1 == RAND_status()) {
        back = 1;
	goto finished;
      }
    }
  }
#endif

  /*	Use platform specific mechanisms if available.
  */
#if DK4_ON_WINDOWS
  res = CryptAcquireContext(
    &hcp, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT
  );
  if (res) {
    cc = 1;
    do {
      if (0 != can_continue(0)) {
        res = CryptGenRandom(hcp, (DWORD)sizeof(buffer), buffer);
        if (res) {
          RAND_seed(buffer, sizeof(buffer));
	  if (1 == RAND_status()) {
	    cc = 0;
	    back = 1;
	  } else {
	    rounds++;
	  }
        } else {
          cc = -1;
	  /* ERROR: Failed to obtain random bytes from crypto context */
        }
      } else {
        cc = -1;
      }
    } while ((1 == cc) && (128 > rounds));
    CryptReleaseContext(hcp, (DWORD)0UL);
    (void)dk4mem_reset_secure(buffer, sizeof(buffer), NULL);
    if (0 == back) {
      if (0 != can_continue(0)) {
        RAND_screen();
	if (1 == RAND_status()) {
	  back = 1;
	}
      }
    }
  } else {
    /* ERROR: Failed to acquire crypto context */
  }
#else
  fipo = fopen("/dev/random", "rb");
  if (NULL == fipo) {
    fipo = fopen("/dev/urandom", "rb");
  }
  if (NULL != fipo) {
    dk4mem_reset(seed_file_buffer, sizeof(seed_file_buffer), NULL);
    svbres = dk4setvbuf(
      fipo, DK4_FILE_FULL_BUFFERING,
      seed_file_buffer, sizeof(seed_file_buffer), NULL
    );
    if (0 != svbres) {
      cc = 1;
      do {
        if (0 != can_continue(0)) {
          rdb = fread(buffer, 1, sizeof(buffer), fipo);
	  if (0 < rdb) {
	    RAND_seed(buffer, rdb);
	    if (1 == RAND_status()) {
	      cc = 0;
	      back = 1;
	    } else {
	      rounds++;
	    }
	  } else {
	    cc = -1;
	  }
        } else {
          cc = -1;
        }
      } while ((1 == cc) && (128 > rounds));
    } else {
      /* ERROR: Failed to set file buffer */
    }
    fclose(fipo);
    (void)dk4mem_reset_secure(seed_file_buffer, sizeof(seed_file_buffer), NULL);
    (void)dk4mem_reset_secure(buffer, sizeof(buffer), NULL);
  } else {
    /* ERROR: Failed to open both /dev/random and /dev/urandom */
  }
#endif

  finished:
  

#line 680 "dk-rand.ctr"
  return back;
}



/**	Create random data, write to standard output.
*/
static
void
create_random_data(int pbso)
{
  size_t	sz	= 512;		/* Amount of data to produce */
  size_t	by;			/* Bytes to process */
  size_t	wrb;			/* Bytes written */
#if DK4_ON_WINDOWS
  int		oldmode	= _O_TEXT;	/* Previous stdout mode */
#endif
  int		cc	= 1;		/* Flag: Can continue */

#if DK4_ON_WINDOWS
    oldmode = _setmode(_fileno(stdout), _O_BINARY);
#endif
  if (0 != seed_prng()) {
    if (0 != options[0].found) {
      if (0 < options[0].val.s) {
        sz = options[0].val.s;
      }
    }
    if (0 == sz) { sz = 512; }
    exval = EXIT_SUCCESS;
    do {
      if (0 != can_continue(1)) {
	by = sizeof(dk_rand_buf);
	if (by > sz) { by = sz; }
	if (0 == sz) {
	  cc = 0;
	} else {
	  if ((dk4_um_t)(INT_MAX) >= (dk4_um_t)by) {
	    if (0 < RAND_bytes((unsigned char *)dk_rand_buf, (int)by)) {
	      if (0 != can_continue(1)) {
	        wrb = fwrite(dk_rand_buf, 1, by, stdout);
	        if (wrb == by) {
	          if (by >= sz) {
	            sz = 0;
		    cc = 0;
	          } else {
	            sz -= by;
	          }
	        } else {
	          /* ERROR: Failed to write bytes to standard output */
		  log_1(1);
	          exval = EXIT_FAILURE;
	          cc = 0;
	        } 
	      } else {
	        cc = -1;
	      }
	    } else {
	      /* ERROR: Failed to obtain random data */
	      log_1(2);
	      exval = EXIT_FAILURE;
	      cc = 0;
	    }
	  } else {
	    /* ERROR: Numeric overflow */
	    cc = 0;
	    exval = EXIT_FAILURE;
	    log_1(8);
	  }
	}
      } else {
        cc = -1;
      }
    } while (1 == cc);
    if (-1 == cc) {
      /* ERROR: Aborted by signal */
      log_1(3);
      exval = EXIT_FAILURE;
    }

    /*	Securely reset buffers.
    */
    RAND_cleanup();
    fflush(stdout);
    if (0 == pbso) {
      /* ERROR: Failed to reset buffer */
      log_1(4);
      exval = EXIT_FAILURE;
    } else {
      if (0 == dk4mem_reset_secure(stdout_buffer,sizeof(stdout_buffer),NULL)) {
        /* ERROR: Failed to reset buffer */
	log_1(4);
        exval = EXIT_FAILURE;
      }
    }
    if (0 == dk4mem_reset_secure(dk_rand_buf, sizeof(dk_rand_buf), NULL)) {
      /* ERROR: Failed to reset buffer */
      log_1(4);
      exval = EXIT_FAILURE;
    }
  } else {
    /* ERROR: Failed to seed PRNG */
    log_1(5);
  }
#if DK4_ON_WINDOWS
    _setmode(_fileno(stdout), oldmode);
#endif
}



/**	Show a text paragraph on standard output.
	@param	txt	Text to show.
*/
static
void
show_text(const char * const *txt)
{
  if (NULL != txt) {
    while (NULL != *txt) {
      fputs(*(txt++), stdout);
      fputc('\n', stdout);
    }
    fflush(stdout);
  }
}



/**	Handle --help, --version, --license options.
*/
static
void
run_hvl(void)
{
  if (0 != (1 & hvl)) {
    show_text(help_text);
  }
  if (0 != (2 & hvl)) {
    fputs(version_text, stdout);
    fputc('\n', stdout);
    fflush(stdout);
  }
  if (0 != (4 & hvl)) {
    show_text(license_text);
  }
  exval = EXIT_SUCCESS;
}



#endif



/**	Main function.
	@param	argc	Number of command line arguments.
	@param	argv	Command line arguments array.
	@return	0 on success, all other values indicate errors.
*/
#if DK4_CHAR_SIZE > 1
int wmain(int argc, wchar_t *argv[])
#else
int main(int argc, char *argv[])
#endif
{
#if DK4_HAVE_OPENSSL_RAND_H

#if DK4_HAVE_SIGACTION
#ifdef SIGPIPE
  struct sigaction	opipe;		/* Old disposition for SIGPIPE */
  struct sigaction	npipe;		/* New disposition for SIGPIPE */
#endif
  struct sigaction	oint;		/* Old disposition for SIGINT */
  struct sigaction	nint;		/* New disposition for SIGINT */
  struct sigaction	oterm;		/* Old disposition for SIGTERM */
  struct sigaction	nterm;		/* New disposition for SIGTERM */
#else
#ifdef SIGPIPE
  dk4_sig_handler_t	*opipe	= NULL;	/* Old disposition for SIGPIPE */
#endif
  dk4_sig_handler_t	*oterm	= NULL;	/* Old disposition for SIGTERM */
  dk4_sig_handler_t	*oint	= NULL;	/* Old disposition for SIGINT */
#endif
#if DK4_HAVE_SIGACTION
#ifdef SIGPIPE
  int			set_pipe = 0;	/* Flag: Disposition SIGPIPE changed */
#endif
  int			set_int	 = 0;	/* Flag: Disposition SIGINT changed */
  int			set_term = 0;	/* Flag: Disposition SIGTERM changed */
  int			sig_i_f	 = 0;	/* Flag: Handler installation failed */
  int			sig_r_f	 = 0;	/* Flag: Handler restoration failed */
#endif
  int			pbso	 = 0;	/* Flag: Private buffer for stdout */
  int		 res;

  /*	Use private buffer for standard output.
  */
  pbso = dk4setvbuf(
    stdout, DK4_FILE_LINE_BUFFERING, stdout_buffer, sizeof(stdout_buffer), NULL
  );

  /*	Process command line arguments.
  */
  res = dk4opt_process_argv(
    options, szoptions, argc, argv, NULL, NULL, 1, 1, NULL
  );
  if (0 == res) {
    goto finished;
  }

  /*	Set up signal handlers.
  */
#if DK4_HAVE_SIGACTION
#ifdef SIGPIPE
  DK4_MEMRES(&npipe, sizeof(npipe));
  npipe.sa_handler = sig_handler_pipe;
  npipe.sa_flags = 0;
  if (0 != sigemptyset(&npipe.sa_mask))		{ sig_i_f = 1; goto finished; }
  if (0 != sigaddset(&npipe.sa_mask, SIGPIPE))	{ sig_i_f = 1; goto finished; }
  if (0 != sigaction(SIGPIPE, &npipe, &opipe))	{ sig_i_f = 1; goto finished; }
  set_pipe = 1;
#endif
  DK4_MEMRES(&nterm, sizeof(nterm));
  nterm.sa_handler = sig_handler_term;
  nterm.sa_flags = 0;
  if (0 != sigemptyset(&nterm.sa_mask))		{ sig_i_f = 1; goto finished; }
  if (0 != sigaddset(&nterm.sa_mask, SIGTERM))	{ sig_i_f = 1; goto finished; }
  if (0 != sigaction(SIGTERM, &nterm, &oterm))	{ sig_i_f = 1; goto finished; }
  set_term = 1;
  DK4_MEMRES(&nint, sizeof(nint));
  nint.sa_handler = sig_handler_int;
  nint.sa_flags = 0;
  if (0 != sigemptyset(&nint.sa_mask))		{ sig_i_f = 1; goto finished; }
  if (0 != sigaddset(&nint.sa_mask, SIGINT))	{ sig_i_f = 1; goto finished; }
  if (0 != sigaction(SIGINT, &nint, &oint))	{ sig_i_f = 1; goto finished; }
  set_int = 1;
#else
#if DK4_HAVE_SIGSET
#ifdef SIGPIPE
  opipe = sigset(SIGPIPE, sig_handler_pipe);
#endif
  oint  = sigset(SIGINT,  sig_handler_int);
  oterm = sigset(SIGTERM, sig_handler_term);
#else
#ifdef SIGPIPE
  opipe = signal(SIGPIPE, sig_handler_pipe);
#endif
  oint  = signal(SIGINT,  sig_handler_int);
  oterm = signal(SIGTERM, sig_handler_term);
#endif
#endif

  /*	Check help, version, license.
  */
  if (0 != options[1].found) { hvl |= 1; }
  if (0 != options[2].found) { hvl |= 2; }
  if (0 != options[3].found) { hvl |= 4; }
  if (0 != hvl) {
    run_hvl();
    goto finished;
  }

  /*	Create random data.
  */
  create_random_data(pbso);

  /*	Clean up at end of program.
  */
  finished:

  /*	Restore signal handlers
  */
#if DK4_HAVE_SIGACTION
  if (0 != sig_i_f) {
    /* ERROR: Failed to set up signal handlers */
    log_1(6);
  }
  if (0 != set_int) {
    if (0 != sigaction(SIGINT, &oint, NULL))	{ sig_r_f = 1; }
  }
  if (0 != set_term) {
    if (0 != sigaction(SIGTERM, &oterm, NULL))	{ sig_r_f = 1; }
  }
#ifdef SIGPIPE
  if (0 != set_pipe) {
    if (0 != sigaction(SIGPIPE, &opipe, NULL))	{ sig_r_f = 1; }
  }
#endif
  if (0 != sig_r_f) {
    /* ERROR: Failed to restore signal handlers */
    log_1(7);
  }
#else
#if DK4_HAVE_SIGSET
  if (NULL != oterm)	{ sigset(SIGTERM, oterm); }
  if (NULL != oint )	{ sigset(SIGINT,  oint ); }
#ifdef SIGPIPE
  if (NULL != opipe)	{ sigset(SIGPIPE, opipe); }
#endif
#else
  if (NULL != oterm)	{ signal(SIGTERM, oterm); }
  if (NULL != oint )	{ signal(SIGINT,  oint ); }
#ifdef SIGPIPE
  if (NULL != opipe)	{ signal(SIGPIPE, opipe); }
#endif
#endif
#endif

#else
  fputs(
    "dk-rand: ERROR: OpenSSL support was not available during build!\n",
    stderr
  );
  fflush(stderr);
#endif
  exit(exval); return exval;
}