aboutsummaryrefslogtreecommitdiff
path: root/tools/tf_fuzz/utility/gibberish.cpp
blob: 45ce223090b583f5146673c3384d6875f5d74faa (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
/*
 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

/*
 * These functions produce random-gibberish quasi-words in a quasi-sentence.
 * Random character streams may be conceptually sufficient, but for testing
 * purposes, it's much easier for humans to remember and to distinguish semi-
 * pro-nounceable gibberish like "dokwab neltiegib..." than
 * "f7H%r^&B*5|j6@Mz>\#...".
 */

#include <string>

#include "gibberish.hpp"  // shouldn't need any other project headers


/**
 * \brief Returns a letter for random-gibberish quasi-words in a quasi-sentence.
 *
 * \return A letter character value.
 *
 */
char gibberish::letter(void)
{
    return 'a' + (rand() % ('z'-'a' + 1));
}


/**
 * \brief Returns a vowel for random-gibberish quasi-words in a quasi-sentence.
 *
 * \return A vowel character value.
 *
 */
char gibberish::vowel(void)
{
    char vowels[] = "aeiou";

    return vowels[rand() % 5];
}


/**
 * \brief Returns a consonant for random-gibberish quasi-words in a quasi-sentence.
 *
 * \return A consonant character value.
 *
 */
char gibberish::consonant(void)
{
    char candidate;

    do {
        candidate = letter();
    } while (   candidate == 'a' || candidate == 'e' || candidate == 'i'
             || candidate == 'o' || candidate == 'u');
    return candidate;
}


/**
 * \brief Appends a semi-pronounceable syllable onto a string, stopping before
 *        the end of the string, for random-gibberish quasi-words in a
 *        quasi-sentence.  Returns a pointer to the next open spot in the string.
 *
 * \param[in] string_ptr Pointer to where to put the word.
 *
 * \param[in] stop       Pointer to last character in quasi-sentence.
 *
 * \return    Pointer to first character after the word.
 *
 */
char *gibberish::syllable (char *string_ptr, char *stop)
{
    char *parser;  /* points into string while building it */

    parser = string_ptr;
    if ((rand() % 4) < 3) {
        if (parser < stop) *parser++ = consonant();
        if (parser < stop) *parser++ = vowel();
        if (parser < stop) *parser++ = letter();
    } else {
        if (parser < stop) *parser++ = vowel();
        if (((rand() % 4) < 1) && parser < stop) {
            *parser++ = vowel();
        }
        if (parser < stop) *parser++ = consonant();
    }
    return parser;
}


/**
 * \brief Appends a mostly-pronounceable quasi-word onto a quasi-sentence string,
 *        stopping before the end of the string.  Returns a pointer to the next
 *        open spot in the string.
 *
 * \param[in] initial_cap:  True if the first character should be capitalized.
 *
 * \param[in] string_ptr Pointer to where to put the word.
 *
 * \param[in] stop       Pointer to last character in quasi-sentence.
 *
 * \return    Pointer to first character after the word.
 *
 */
char *gibberish::word (bool initial_cap, char *string_ptr, char *stop)
{
    int syllable_count;
    char *parser;  /* points into string while building it */

    for (syllable_count = 0, parser = string_ptr;
            syllable_count < 4
         && (rand() % 4) >= syllable_count
         && parser < stop;
         syllable_count++) {
        parser = syllable (parser, stop);
    }
    if (initial_cap) {
        *string_ptr -= 'a' - 'A';  /* more or less assumes ASCII */
    }
    return parser;
}


/**
 * \brief Creates a mostly-pronounceable, random-gibberish quasi-sentence,
 *        stopping before the end of the string.
 *
 * \param[in] string_ptr Pointer to beginning of string for quasi-sentence.
 *
 * \param[in] stop       Pointer to last character in quasi-sentence.
 *
 */
void gibberish::sentence (char *string_ptr, char *stop)
{
    char *parser;  /* points into string while building it */
    char punctuation[] = ".?!";

    *stop = '\0';  /* null-terminate the string */
    --stop;
    parser = word (capitalize, string_ptr, stop);
    if (parser < stop) {
        *parser++ = ' ';
    }
    for (;  parser < stop; ) {
        parser = word (dont_capitalize, parser, stop);
        if (parser < stop) {
            *parser++ = ' ';
        }
    }
    parser--;  
    if (*parser == ' ') {
        *parser = vowel();  // just to not have a blank at the end
    }
    *stop = punctuation[rand() % 3];
}


/**
 * \brief Chooses a gibberish-sentence length.
 *
 */
int gibberish::pick_sentence_len (void)
{
    return min_literal_data_len + (rand() % literal_data_len_span);
}


/**
 * \brief Constructor for gibberish object.
 *
 */
gibberish::gibberish (void)
{
    // Nothing to set up.
}


/**
 * \brief Destructor for gibberish object.
 *
 */
gibberish::~gibberish (void)
{
    // Nothing to tear down.
}