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
|
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <stdint.h> /* defines SIZE_MAX */
#include "../io/error.h"
#include "strings.h"
void ZoO_strings_initialize (struct ZoO_strings s [const restrict static 1])
{
s->words_count = 0;
s->words = (ZoO_char **) NULL;
s->word_sizes = (size_t *) NULL;
}
void ZoO_strings_finalize (struct ZoO_strings s [const restrict static 1])
{
if (s->words_count != 0)
{
ZoO_index i;
for (i = 0; i < s->words_count; ++i)
{
free((void *) s->words[i]);
}
s->words_count = 0;
free((void *) s->words);
free((void *) s->word_sizes);
s->words = (ZoO_char **) NULL;
s->word_sizes = (size_t *) NULL;
}
}
static int add_word
(
struct ZoO_strings s [const restrict static 1],
size_t const line_size,
const ZoO_char line [const restrict static line_size]
)
{
size_t * new_s_word_sizes;
ZoO_char * new_word, ** new_s_words;
if (s->words_count == ZoO_INDEX_MAX)
{
ZoO_S_WARNING("Data input sentence has too many words.");
return -1;
}
/* overflow-safe, as line_size < SIZE_MAX */
new_word = (ZoO_char *) calloc((line_size + 1), sizeof(ZoO_char));
if (new_word == (ZoO_char *) NULL)
{
ZoO_S_WARNING("Unable to allocate memory to extract new word.");
return -1;
}
memcpy((void *) new_word, (const void *) line, line_size);
new_word[line_size] = '\0';
new_s_words =
(ZoO_char **) realloc
(
(void *) s->words,
/* XXX: (sizeof() * _) assumed overflow-safe. */
/* (di->words_count + 1) overflow-safe */
(sizeof(ZoO_char *) * (s->words_count + 1))
);
if (new_s_words == (ZoO_char **) NULL)
{
ZoO_S_WARNING("Unable to reallocate memory to extract new word.");
free((void *) new_word);
return -1;
}
s->words = new_s_words;
new_s_word_sizes =
(size_t *) realloc
(
(void *) s->word_sizes,
/* XXX: (sizeof() * _) assumed overflow-safe. */
/* (di->words_count + 1) overflow-safe */
(sizeof(size_t) * (s->words_count + 1))
);
if (new_s_word_sizes == (size_t *) NULL)
{
ZoO_S_WARNING("Unable to reallocate memory to extract new word.");
free((void *) new_word);
return -1;
}
s->word_sizes = new_s_word_sizes;
s->words[s->words_count] = new_word;
s->word_sizes[s->words_count] = (line_size + 1);
s->words_count += 1;
return 0;
}
static int parse_word
(
struct ZoO_strings s [const restrict static 1],
ZoO_index const punctuations_count,
const ZoO_char punctuations [const restrict static punctuations_count],
size_t const line_size,
ZoO_char line [const static line_size]
)
{
ZoO_index j;
if (line_size == 0)
{
return 0;
}
for (j = 0; j < line_size; ++j)
{
switch (line[j])
{
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
line[j] = 'z' - ('Z' - line[j]);
break;
default:
break;
}
}
for (j = 0; j < punctuations_count; ++j)
{
/* overflow-safe: line_size > 1 */
if (line[line_size - 1] == punctuations[j])
{
if (line_size > 1)
{
if
(
/* overflow-safe: line_size > 1 */
(add_word(s, (line_size - 1), line) < 0)
/* overflow-safe: line_size > 1 */
/* prevents line[restrict] */
|| (add_word(s, 1, (line + (line_size - 1))) < 0)
)
{
return -1;
}
return 0;
}
}
}
return add_word(s, line_size, line);
}
int ZoO_strings_parse
(
struct ZoO_strings s [const restrict static 1],
size_t input_size,
ZoO_char input [const restrict],
ZoO_index const punctuations_count,
const ZoO_char punctuations [const restrict static punctuations_count]
)
{
size_t i, w_start;
ZoO_strings_finalize(s);
if (input == NULL)
{
return 0;
}
i = 0;
/* overflow-safe: input is '\0' terminated. */
while (input[i] == ' ')
{
++i;
}
w_start = i;
for (; i < input_size; ++i)
{
if (input[i] == ' ')
{
if
(
parse_word
(
s,
punctuations_count,
punctuations,
/* overflow-safe: w_start < i */
(i - w_start),
(input + w_start)
) < 0
)
{
ZoO_strings_finalize(s);
return -1;
}
++i;
/* safe, as input is terminated by '\0' */
while (input[i] == ' ')
{
++i;
}
w_start = i;
}
}
if
(
parse_word
(
s,
punctuations_count,
punctuations,
/* overflow-safe: w_start < i */
(i - w_start),
(input + w_start)
) < 0
)
{
ZoO_strings_finalize(s);
return -1;
}
return 0;
}
|