~ubuntu-branches/debian/sid/pwgen/sid

« back to all changes in this revision

Viewing changes to pw_phonemes.c

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Renardias
  • Date: 2001-12-06 17:38:58 UTC
  • Revision ID: james.westby@ubuntu.com-20011206173858-jf0wwxjtrm7e68hg
Tags: upstream-2.01
ImportĀ upstreamĀ versionĀ 2.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pw_phonemes.c --- generate secure passwords using phoneme rules
 
3
 *
 
4
 * Copyright (C) 2001 by Theodore Ts'o
 
5
 * 
 
6
 * This file may be distributed under the terms of the GNU Public
 
7
 * License.
 
8
 */
 
9
 
 
10
#include "pwgen.h"
 
11
 
 
12
struct pw_element elements[] = {
 
13
        { "a",  VOWEL },
 
14
        { "ae", VOWEL | DIPTHONG },
 
15
        { "ah", VOWEL | DIPTHONG },
 
16
        { "ai", VOWEL | DIPTHONG },
 
17
        { "b",  CONSONANT },
 
18
        { "c",  CONSONANT },
 
19
        { "ch", CONSONANT | DIPTHONG },
 
20
        { "d",  CONSONANT },
 
21
        { "e",  VOWEL },
 
22
        { "ee", VOWEL | DIPTHONG },
 
23
        { "ei", VOWEL | DIPTHONG },
 
24
        { "f",  CONSONANT },
 
25
        { "g",  CONSONANT },
 
26
        { "gh", CONSONANT | DIPTHONG | NOT_FIRST },
 
27
        { "h",  CONSONANT },
 
28
        { "i",  VOWEL },
 
29
        { "ie", VOWEL | DIPTHONG },
 
30
        { "j",  CONSONANT },
 
31
        { "k",  CONSONANT },
 
32
        { "l",  CONSONANT },
 
33
        { "m",  CONSONANT },
 
34
        { "n",  CONSONANT },
 
35
        { "ng", CONSONANT | DIPTHONG | NOT_FIRST },
 
36
        { "o",  VOWEL },
 
37
        { "oh", VOWEL | DIPTHONG },
 
38
        { "oo", VOWEL | DIPTHONG},
 
39
        { "p",  CONSONANT },
 
40
        { "ph", CONSONANT | DIPTHONG },
 
41
        { "qu", CONSONANT | DIPTHONG},
 
42
        { "r",  CONSONANT },
 
43
        { "s",  CONSONANT },
 
44
        { "sh", CONSONANT | DIPTHONG},
 
45
        { "t",  CONSONANT },
 
46
        { "th", CONSONANT | DIPTHONG},
 
47
        { "u",  VOWEL },
 
48
        { "v",  CONSONANT },
 
49
        { "w",  CONSONANT },
 
50
        { "x",  CONSONANT },
 
51
        { "y",  CONSONANT },
 
52
        { "z",  CONSONANT }
 
53
};
 
54
 
 
55
#define NUM_ELEMENTS (sizeof(elements) / sizeof (struct pw_element))
 
56
 
 
57
void pw_phonemes(char *buf, int size, int pw_flags)
 
58
{
 
59
        int     c, i, len, flags, feature_flags;
 
60
        int     prev, should_be, first;
 
61
        char    *str;
 
62
 
 
63
try_again:
 
64
        feature_flags = pw_flags;
 
65
        c = 0;
 
66
        prev = 0;
 
67
        should_be = 0;
 
68
        first = 1;
 
69
 
 
70
        should_be = pw_random_number(1) ? VOWEL : CONSONANT;
 
71
        
 
72
        while (c < size) {
 
73
                i = pw_random_number(NUM_ELEMENTS);
 
74
                str = elements[i].str;
 
75
                len = strlen(str);
 
76
                flags = elements[i].flags;
 
77
                /* Filter on the basic type of the next element */
 
78
                if ((flags & should_be) == 0)
 
79
                        continue;
 
80
                /* Handle the NOT_FIRST flag */
 
81
                if (first && (flags & NOT_FIRST))
 
82
                        continue;
 
83
                /* Don't allow VOWEL followed a Vowel/Dipthong pair */
 
84
                if ((prev & VOWEL) && (flags & VOWEL) &&
 
85
                    (flags & DIPTHONG))
 
86
                        continue;
 
87
                /* Don't allow us to overflow the buffer */
 
88
                if (len > size-c)
 
89
                        continue;
 
90
                /*
 
91
                 * OK, we found an element which matches our criteria,
 
92
                 * let's do it!
 
93
                 */
 
94
                strcpy(buf+c, str);
 
95
 
 
96
                /* Handle PW_ONE_CASE */
 
97
                if (feature_flags & PW_ONE_CASE) {
 
98
                        if ((first || flags & CONSONANT) &&
 
99
                            (pw_random_number(10) < 3)) {
 
100
                                buf[c] = toupper(buf[c]);
 
101
                                feature_flags &= ~PW_ONE_CASE;
 
102
                        }
 
103
                }
 
104
                
 
105
                c += len;
 
106
                
 
107
                /* Time to stop? */
 
108
                if (c >= size)
 
109
                        break;
 
110
                
 
111
                /*
 
112
                 * Handle PW_ONE_NUMBER
 
113
                 */
 
114
                if (feature_flags & PW_ONE_NUMBER) {
 
115
                        if (!first && (pw_random_number(10) < 3)) {
 
116
                                buf[c++] = pw_random_number(9)+'0';
 
117
                                buf[c] = 0;
 
118
                                feature_flags &= ~PW_ONE_NUMBER;
 
119
                                
 
120
                                first = 1;
 
121
                                prev = 0;
 
122
                                should_be = pw_random_number(1) ?
 
123
                                        VOWEL : CONSONANT;
 
124
                                continue;
 
125
                        }
 
126
                }
 
127
                                
 
128
                /*
 
129
                 * OK, figure out what the next element should be
 
130
                 */
 
131
                if (should_be == CONSONANT) {
 
132
                        should_be = VOWEL;
 
133
                } else { /* should_be == VOWEL */
 
134
                        if ((prev & VOWEL) ||
 
135
                            (flags & DIPTHONG) ||
 
136
                            (pw_random_number(10) > 3))
 
137
                                should_be = CONSONANT;
 
138
                        else
 
139
                                should_be = VOWEL;
 
140
                }
 
141
                prev = flags;
 
142
                first = 0;
 
143
        }
 
144
        if (feature_flags & (PW_ONE_CASE | PW_ONE_NUMBER))
 
145
                goto try_again;
 
146
}