aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/otp-altdict.c
blob: accb46a07e20d2e17f074c53fe5f588f26e867ad (plain) (blame)
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
/*
 *  OTP Utilities
 *  Copyright (C) 2022 David M. Syzdek <david@syzdek.net>.
 *
 *  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 David M. Syzdek nor the
 *       names of its 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 DAVID M SYZDEK 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 src/otp-altdict.c
 */
#define _SRC_OTP_ALTDICT_C 1

///////////////
//           //
//  Headers  //
//           //
///////////////
#pragma mark - Headers

#ifdef HAVE_CONFIG_H
#   include <config.h>
#endif

#ifdef HAVE_BINDLE_PREFIX_H
#   include <bindle_prefix.h>
#else
#   include <bindle.h>
#endif

#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>

#include <otputil.h>


///////////////////
//               //
//  Definitions  //
//               //
///////////////////
#pragma mark - Definitions

#ifndef PROGRAM_NAME
#define PROGRAM_NAME "otp-altdict"
#endif
#ifndef PACKAGE_BUGREPORT
#define PACKAGE_BUGREPORT "david@syzdek.net"
#endif
#ifndef PACKAGE_COPYRIGHT
#define PACKAGE_COPYRIGHT ""
#endif
#ifndef PACKAGE_NAME
#define PACKAGE_NAME ""
#endif
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION ""
#endif


#define MY_BUFF_SIZE          88
#define MY_WORDBUFF_SIZE      128

#define MY_DFLT_WORD_MAXLEN   4

#define MY_CASE_LOWER         0x01
#define MY_CASE_UPPER         0x02
#define MY_CASE_CAPITALIZE    0x04
#define MY_CASE_CAMEL         0x08
#define MY_CASE_INVERT_CAMEL  0x10
#define MY_CASE_FIRST         0x20
#define MY_CASE_SECOND        0x40


/////////////////
//             //
//  Datatypes  //
//             //
/////////////////
#pragma mark - Datatypes

typedef struct _my_config my_config_t;
struct _my_config
{
   int               verbose;
   int               quiet;
   int               fd;
   int               allow_hex;
   int               allow_dups;
   int               word_case;
   int               ignore_warnings;
   int               pad_int;
   size_t            word_maxlen;
   size_t            buff_len;
   size_t            buff_off;
   const char *      file_out;
   const EVP_MD *    evp_md;
   struct stat       sb;
   char              buff[MY_BUFF_SIZE];
   char **           dict[2048];
};


/////////////////
//             //
//  Variables  //
//             //
/////////////////
#pragma mark - Variables

extern const char * otputil_dict_rfc1760[];


//////////////////
//              //
//  Prototypes  //
//              //
//////////////////
#pragma mark - Prototypes

extern int
main(
         int                           argc,
         char *                        argv[] );


static int
my_dict_save(
         my_config_t *                 cnf );


static int
my_dict_verify(
         my_config_t *                 cnf );


static int
my_dict_summary(
         my_config_t *                 cnf );


static void
my_error(
         const char *                  fmt,
         ... );


static void
my_info(
         my_config_t *                 cnf,
         const char *                  fmt,
         ... );


static int
my_buff_fill(
         my_config_t *                 cnf );


static int
my_buff_process(
         my_config_t *                 cnf );


static int
my_buff_process_word(
         my_config_t *                 cnf,
         const char *                  word );


static int
my_buff_process_word_hash(
         my_config_t *                 cnf,
         const char *                  word,
         int                           word_len );


static void
my_verbose(
         my_config_t *                 cnf,
         const char *                  fmt,
         ... );


static int
my_word_cmp_key(
         const void *                  a,
         const void *                  b );


static int
my_word_cmp_obj(
         const void *                  a,
         const void *                  b );


/////////////////
//             //
//  Functions  //
//             //
/////////////////
#pragma mark - Functions

int
main(
         int                           argc,
         char *                        argv[] )
{
   int               c;
   int               opt_index;
   int               rc;
   my_config_t       config;
   my_config_t *     cnf;
   char *            endptr;

   // getopt options
   static const char *  short_opt = "12Aa:CdHhiLl:Mo:qUVvW";
   static struct option long_opt[] =
   {
      {"help",             no_argument,       NULL, 'h' },
      {"quiet",            no_argument,       NULL, 'q' },
      {"silent",           no_argument,       NULL, 'q' },
      {"version",          no_argument,       NULL, 'V' },
      {"verbose",          no_argument,       NULL, 'v' },
      { NULL, 0, NULL, 0 }
   };

   OpenSSL_add_all_digests();

   memset(&config, 0, sizeof(config));

   opt_index            = 0;
   cnf                  = &config;
   cnf->evp_md          = EVP_sha1();
   cnf->word_maxlen     = MY_DFLT_WORD_MAXLEN;
   cnf->file_out        = NULL;

   while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
   {
      switch(c)
      {
         case -1:       /* no more arguments */
         case 0:        /* long options toggles */
         break;

         case '1':
         cnf->word_case |= MY_CASE_FIRST;
         break;

         case '2':
         cnf->word_case |= MY_CASE_SECOND;
         break;

         case 'A':
         cnf->word_case = 0xFFFFFF;
         break;

         case 'a':
         if ((cnf->evp_md = EVP_get_digestbyname(optarg)) == NULL)
         {
            fprintf(stderr, "%s: unknown digest `%s'\n", PROGRAM_NAME, optarg);
            fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
            return(1);
         };
         break;

         case 'C':
         cnf->word_case |= MY_CASE_CAPITALIZE;
         break;

         case 'd':
         cnf->allow_dups = 1;
         break;

         case 'H':
         cnf->allow_hex = 1;
         break;

         case 'h':
         printf("Usage: %s [OPTIONS] <wordlist>\n", PROGRAM_NAME);
         printf("OPTIONS:\n");
         printf("  -1                        capitalize first half of word (e.g. \"WOrd\"\n");
         printf("  -2                        capitalize second half of word (e.g. \"woRD\"\n");
         printf("  -A                        add word with all variations\n");
         printf("  -a algorithm              hash algorithm for alt dictionary (default: sha1)\n");
         printf("  -C                        add word as capitalized (e.g. \"Word\"\n");
         printf("  -d                        allow duplicates with S/KEY dictionary\n");
         printf("  -H                        use words which only contain hexadecimal characters\n");
         printf("  -h, --help                print this help and exit\n");
         printf("  -i                        ignore warnings\n");
         printf("  -L                        add word as lower case (e.g. \"word\"\n");
         printf("  -l length                 maximum word length (default: %i)\n", MY_DFLT_WORD_MAXLEN);
         printf("  -M                        add word as inverted camel case (e.g. \"wOrD\"\n");
         printf("  -o file                   output file\n");
         printf("  -q, --quiet, --silent     do not print messages\n");
         printf("  -U                        add word as upper case (e.g. \"WORD\"\n");
         printf("  -V, --version             print version number and exit\n");
         printf("  -v, --verbose             print verbose messages\n");
         printf("  -W                        add word as camel case (e.g. \"WoRd\"\n");
         printf("ALGORITHMS:\n");
         printf("  md4\n");
         printf("  md5\n");
         printf("  sha1\n");
         printf("  sha256\n");
         printf("  sha512\n");
         printf("\n");
         return(0);

         case 'i':
         cnf->ignore_warnings = 1;
         break;

         case 'L':
         cnf->word_case |= MY_CASE_LOWER;
         break;

         case 'l':
         cnf->word_maxlen = strtoull(optarg, &endptr, 0);
         if ( (optarg == endptr) || ((endptr[0])) )
         {
            fprintf(stderr, "%s: invalid value for `-l'\n", PROGRAM_NAME);
            fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
            return(1);
         };
         break;

         case 'M':
         cnf->word_case |= MY_CASE_INVERT_CAMEL;
         break;

         case 'o':
         cnf->file_out = optarg;
         break;

         case 'q':
         cnf->quiet = 1;
         break;

         case 'U':
         cnf->word_case |= MY_CASE_UPPER;
         break;

         case 'V':
         printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION);
         printf("Written by David M. Syzdek.\n");
         return(0);

         case 'v':
         cnf->verbose++;
         break;

         case 'W':
         cnf->word_case |= MY_CASE_CAMEL;
         break;

         case '?':
         fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
         return(1);

         default:
         fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
         fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
         return(1);
      };
   };
   if (optind == argc)
   {
      fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME);
      fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
      return(1);
   };
   if ((optind+1) < argc)
   {
      fprintf(stderr, "%s: unknown argument `%s'\n", PROGRAM_NAME, argv[optind+1]);
      fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
      return(1);
   };

   if (!(cnf->word_case))
      cnf->word_case = MY_CASE_LOWER;

   // open file and loop through word list
   my_info(cnf, "generating dictionary: processing word list\n");
   if (stat(argv[optind], &cnf->sb) == -1)
   {
      fprintf(stderr, "%s: stat(): %s\n", PROGRAM_NAME, strerror(errno));
      return(1);
   };
   if ((cnf->fd = open(argv[optind], O_RDONLY)) == -1)
   {
      my_error("stat(): %s\n", strerror(errno));
      return(1);
   };
   while((rc = my_buff_fill(cnf)) > 0)
      my_buff_process(cnf);
   close(cnf->fd);
   if (rc == -1)
      return(1);

   // verify dictionary
   if (my_dict_verify(cnf) == -1)
      return(1);

   // print possible words for each dictionary value
   if (my_dict_summary(cnf) == -1)
      return(1);

   // write dictionary
   if ((my_dict_save(cnf)))
      return(1);

   return(0);
}


// Reads up to N bytes where N is two bytes less than the buffer size. The
// Buffer is either NULL terminated at position N if the buffer is full, or
// the buffer is terminated with a space and a NULL character if the buffer is
// not full.
int
my_buff_fill(
         my_config_t *                 cnf )
{
   size_t      pos;
   ssize_t     len;

   // shift buffer
   for(pos = cnf->buff_off; (pos < cnf->buff_len); pos++)
      cnf->buff[pos - cnf->buff_off] = cnf->buff[pos];
   cnf->buff[pos]    =  '\0';
   cnf->buff_len     -= cnf->buff_off;
   cnf->buff_off     =  0;

   // add data to buffer
   if ((len = read(cnf->fd, &cnf->buff[cnf->buff_len], (sizeof(cnf->buff) - 2 - cnf->buff_len))) == -1)
   {
      my_error("read(): %s\n", strerror(errno));
      return(-1);
   };

   // clean up input
   for(pos = cnf->buff_len; (pos < (cnf->buff_len + len)); pos++)
   {
      // make input upper case
      cnf->buff[pos] = tolower(cnf->buff[pos]);
      // replace white space with space character (' ')
      cnf->buff[pos] = ((isspace(cnf->buff[pos]))) ? ' ' : cnf->buff[pos];
   };

   // adjust buffer length
   cnf->buff_len              += (size_t)len;
   cnf->buff[cnf->buff_len]   =  '\0';

   if (cnf->buff_len < ((sizeof(cnf->buff)-2)))
   {
      cnf->buff[cnf->buff_len+0] =  ' ';
      cnf->buff[cnf->buff_len+1] =  '\0';
   };

   return((int)len);
}


int
my_buff_process(
         my_config_t *                 cnf )
{
   size_t         pos;
   char *         word;

   // loop through buffer
   pos = 0;
   while(pos < cnf->buff_len)
   {
      // skip white space
      if ( ((isspace(cnf->buff[pos]))) || (cnf->buff[pos] == '\0') )
      {
         cnf->buff_off = pos;
         pos++;
         continue;
      };

      // process word
      if ((isalpha(cnf->buff[pos])))
      {
         word = &cnf->buff[pos];
         while( ((isalpha(cnf->buff[pos]))) && (pos < cnf->buff_len) )
            pos++;
         if (cnf->buff[pos] == ' ')
         {
            cnf->buff[pos] = '\0';
            if (my_buff_process_word(cnf, word) == -1)
               return(-1);
            cnf->buff_off = pos;
            pos++;
            continue;
         };
      };

      // skip garbage
      while( (cnf->buff[pos] != ' ') && (pos < cnf->buff_len) )
         pos++;
      if (cnf->buff[pos] == ' ')
         cnf->buff_off = pos;
   };

   return(0);
}


int
my_buff_process_word(
         my_config_t *                 cnf,
         const char *                  word )
{
   size_t            pos;
   int               hexonly;
   int               x;
   int               len;
   char *            str;

   // checks syntax of word
   hexonly = 1;
   for(pos = 0; ((word[pos])); pos++)
   {
      // checks word length
      if (pos >= cnf->word_maxlen)
      {
         my_verbose(cnf, "word '%s': word too long\n", word);
         return(0);
      };

      // checks for valid characters
      if (!(isalpha(word[pos])))
      {
         my_verbose(cnf, "word '%s': contains non-alpha characters\n", word);
         return(0);
      };

      // checks for non-hex characters
      if (!(isxdigit(word[pos])))
         hexonly = 0;
   };
   if ( ((hexonly)) && (!(cnf->allow_hex)) )
   {
      my_verbose(cnf, "word '%s': contains only hexadecimal characters\n", word);
      return(0);
   };

   // search for S/KEY dictionary word
   if (!(cnf->allow_dups))
   {
      if (bindle_bindex(word, otputil_dict_rfc1760, 2048, sizeof(char *), 0, NULL, my_word_cmp_key) != -1)
      {
         my_verbose(cnf, "word '%s': is contained in S/KEY dictionary\n", word);
         return(0);
      };
   };

   my_verbose(cnf, "word '%s': processing\n", word);

   // copy word
   if ((str = bindle_strdup(word)) == NULL)
   {
      my_error("out of virtual memory\n");
      return(-1);
   };
   len = (int)strlen(str);

   // generate hash for lower case word
   if ((cnf->word_case & MY_CASE_LOWER))
   {
      for(x = 0; ((str[x])); x++)
         str[x] = tolower(str[x]);
      if (my_buff_process_word_hash(cnf, str, len) == -1)
      {
         free(str);
         return(-1);
      };
   };

   // generates hash for first letter capital
   if ((cnf->word_case & MY_CASE_CAPITALIZE))
   {
      for(x = 1; ((str[x])); x++)
         str[x] = tolower(str[x]);
      str[0] = toupper(str[0]);
      if (my_buff_process_word_hash(cnf, str, len) == -1)
      {
         free(str);
         return(-1);
      };
   };

   // generates hash for upper case word
   if ((cnf->word_case & MY_CASE_UPPER))
   {
      for(x = 0; ((str[x])); x++)
         str[x] = toupper(str[x]);
      if (my_buff_process_word_hash(cnf, str, len) == -1)
      {
         free(str);
         return(-1);
      };
   };

   // generates hash for camel case word
   if ((cnf->word_case & MY_CASE_CAMEL))
   {
      for(x = 0; ((str[x])); x++)
         str[x] = ((x & 0x01)) ? tolower(str[x]) : toupper(str[x]);
      if (my_buff_process_word_hash(cnf, str, len) == -1)
      {
         free(str);
         return(-1);
      };
   };

   // generates hash for inverted camel case word
   if ((cnf->word_case & MY_CASE_INVERT_CAMEL))
   {
      for(x = 0; ((str[x])); x++)
         str[x] = ((x & 0x01)) ? toupper(str[x]) : tolower(str[x]);
      if (my_buff_process_word_hash(cnf, str, len) == -1)
      {
         free(str);
         return(-1);
      };
   };

   // generates hash for first half of word capitalized
   if ((cnf->word_case & MY_CASE_FIRST))
   {
      for(x = 0; ((str[x])); x++)
         str[x] = ((len/2) > x) ? toupper(str[x]) : tolower(str[x]);
      if (my_buff_process_word_hash(cnf, str, len) == -1)
      {
         free(str);
         return(-1);
      };
   };

   // generates hash for second half of word capitalized
   if ((cnf->word_case & MY_CASE_FIRST))
   {
      for(x = 0; ((str[x])); x++)
         str[x] = ((len/2) < x) ? toupper(str[x]) : tolower(str[x]);
      if (my_buff_process_word_hash(cnf, str, len) == -1)
      {
         free(str);
         return(-1);
      };
   };

   free(str);

   return(0);
}


int
my_buff_process_word_hash(
         my_config_t *                 cnf,
         const char *                  word,
         int                           word_len )
{
   unsigned char     md[EVP_MAX_MD_SIZE];
   unsigned          md_len;
   int               val;
   int               rc;

   md_len = sizeof(md);
   if (!(EVP_Digest(word, word_len, md, &md_len, cnf->evp_md, NULL)))
   {
      my_error("unable to geneate hash for '%s'\n", word);
      return(-1);
   };

   // generate value
   val  = 0;
   val |= (md[md_len-1] & 0xff) << 0;
   val |= (md[md_len-2] & 0x07) << 8;

   // add word to dictionary
   if ((rc = bindle_strsadd(&cnf->dict[val], word)) != BNDL_SUCCESS)
   {
      my_error("bindle_strsadd(): %s\n", strerror(rc));
      return(-1);
   };

   return(0);
}


int
my_dict_save(
         my_config_t *                 cnf )
{
   int            x;
   int            count;
   FILE *         fs;
   char           word[MY_WORDBUFF_SIZE];
   char           algo[MY_WORDBUFF_SIZE];
   int            line;
   size_t         wrap;

   my_info(cnf, "writing dictionary to file ...\n");

#ifdef HAVE_EVP_MD_GET0_NAME
   snprintf(algo, sizeof(algo), "%s", EVP_MD_get0_name(cnf->evp_md));
#else
   snprintf(algo, sizeof(algo), "hash");
#endif
   for(x = 0; ((algo[x])); x++)
      algo[x] = tolower(algo[x]);

   fs = stdout;
   if ((cnf->file_out))
   {
      if ((fs = fopen(cnf->file_out, "w")) == NULL)
      {
         my_error("fopen(): %s\n", strerror(errno));
         return(1);
      };
   };

   wrap = 59 / (cnf->word_maxlen+5);
   line = 0;

   fprintf(fs, "const char * otputil_rfc2289_dict_%s[] =\n", algo);
   fprintf(fs, "{");
   for(x = 0; (x < 2048); x++)
   {
      if ((x % wrap) == 0)
      {
         if (x == 0)
            fprintf(fs, "\n   ");
         else
         {
            count = (int)(line * wrap);
            fprintf(fs, "// vals: %i - %i\n   ", count, (count+(int)wrap-1));
            line++;
         };
      };
      if ((cnf->dict[x]))
         snprintf(word, sizeof(word), "\"%s\",", cnf->dict[x][0]);
      if (!(cnf->dict[x]))
         snprintf(word, sizeof(word), "NULL,");
      fprintf(fs, "%-*s", (int)(cnf->word_maxlen+5), word);
   };
   if ((x % wrap) == 0)
      fprintf(fs, "\n    ");
   fprintf(fs, "NULL\n");
   fprintf(fs, "};\n");

   if ((cnf->file_out))
      fclose(fs);

   return(0);
}


int
my_dict_summary(
         my_config_t *                 cnf )
{
   int      x;
   int      y;

   if ((cnf->quiet))
      return(0);

#ifdef HAVE_EVP_MD_GET0_NAME
   my_info(cnf, "OTP Alt Dictionary for %s with words of 1-%i letters\n", EVP_MD_get0_name(cnf->evp_md), (int)cnf->word_maxlen);
#else
   my_info(cnf, "OTP Alt Dictionary for words of 1-%i letters\n", (int)cnf->word_maxlen);
#endif
   for(x = 0; (x < 2048); x++)
   {
      // skip output if value does not have words
      if (!(cnf->dict[x]))
      {
         my_info(cnf, "%-4i%s", x, (((x & 0x07) == 0x07) ? "\n\n\n" : "\n") );
         continue;
      };

      // display words for specific value
      my_info(cnf, "%-4i  %-*s", x, cnf->word_maxlen, cnf->dict[x][0]);
      for(y = 1; ((cnf->dict[x][y])); y++)
      {
         if ( (((y-1)*(cnf->word_maxlen+1))%70) > (((y-0)*(cnf->word_maxlen+1))%70) )
            my_info(cnf, "\n     ");
         my_info(cnf, " %-*s", cnf->word_maxlen, cnf->dict[x][y]);
      };
      my_info(cnf, (((x & 0x07) == 0x07) ? "\n\n\n" : "\n") );
   };

   return(0);
}


int
my_dict_verify(
         my_config_t *                 cnf )
{
   int               x;
   int               missing;
   size_t            count;

   missing = 0;

   my_info(cnf, "generating dictionary: checking for missing dictionary values\n");

   // verify and sort dictionary
   for(x = 0; (x < 2048); x++)
   {
      if (!(cnf->dict[x]))
      {
         my_info(cnf, "missing dictionary word for value '%i'\n", x);
         missing++;
         continue;
      };
      for(count = 0; ((cnf->dict[x][count])); count++);
      qsort(cnf->dict[x], count, sizeof(char *), &my_word_cmp_obj);
   };

   my_info(cnf, "%i values are missing dictionary words\n", missing);

   if ( ((missing)) && (!(cnf->ignore_warnings)) )
      return(-1);
   return(0);
}


void
my_error(
         const char *                  fmt,
         ... )
{
   va_list args;
   fprintf(stderr, "%s: ", PROGRAM_NAME);
   va_start(args, fmt);
   vfprintf(stderr, fmt, args);
   va_end(args);
   return;
}


void
my_info(
         my_config_t *                 cnf,
         const char *                  fmt,
         ... )
{
   va_list args;
   if ((cnf->quiet))
      return;
   va_start(args, fmt);
   vprintf(fmt, args);
   va_end(args);
   return;
}


void
my_verbose(
         my_config_t *                 cnf,
         const char *                  fmt,
         ... )
{
   va_list args;
   if (!(cnf->verbose))
      return;
   va_start(args, fmt);
   vprintf(fmt, args);
   va_end(args);
   return;
}


int
my_word_cmp_key(
         const void *                  a,
         const void *                  b )
{
   const char * const * obj;
   const char *         key;
   key = a;
   obj = b;
   return(strcasecmp(key, *obj));
}


int
my_word_cmp_obj(
         const void *                  a,
         const void *                  b )
{
   const char * const * ap;
   const char * const * bp;
   size_t               alen;
   size_t               blen;
   ap = a;
   bp = b;
   if ( (alen = strlen(*ap)) < (blen = strlen(*bp)) )
      return(-1);
   if (alen > blen)
      return(1);
   return(strcasecmp(*ap, *bp));
}

/* end of source file */