"Fossies" - the Fresh Open Source Software Archive 
Member "apg-2.2.3/convert.c" (7 Aug 2003, 10170 Bytes) of package /linux/privat/old/apg-2.2.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 ** Copyright (c) 1999, 2000, 2001, 2002, 2003
3 ** Adel I. Mirzazhanov. All rights reserved
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 **
9 ** 1.Redistributions of source code must retain the above copyright notice,
10 ** this list of conditions and the following disclaimer.
11 ** 2.Redistributions in binary form must reproduce the above copyright
12 ** notice, this list of conditions and the following disclaimer in the
13 ** documentation and/or other materials provided with the distribution.
14 ** 3.The name of the author may not be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 ** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
33 #include <strings.h>
34 #endif
35 #ifndef APGBFM
36 # include "errs.h"
37 # include "randpass.h"
38 #endif
39
40 #include "convert.h"
41
42 /*
43 ** GLOBALS
44 */
45
46 /* small letters */
47 char let[26] =
48 {
49 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
50 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
51 'u', 'v', 'w', 'x', 'w', 'z'
52 };
53 /* capital letters */
54 char clet[26] =
55 {
56 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
57 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
58 'U', 'V', 'W', 'X', 'W', 'Z'
59 };
60
61 /*
62 ** FUNCTIONS
63 */
64
65 /*
66 ** decapitalize() - This routine replaces all capital letters
67 ** to small letters in the word:
68 ** INPUT:
69 ** char * - word.
70 ** OUTPUT:
71 ** none.
72 ** NOTES:
73 ** none.
74 */
75 void
76 decapitalize (char *word)
77 {
78 int i = 0; /* counter */
79 int j = 0; /* counter */
80 int str_len = strlen(word);
81 for(j = 0; j < str_len; j++)
82 for(i=0; i < 26; i++)
83 if(word[j] == clet[i])
84 word[j] = let[i];
85 }
86
87 #ifndef APGBFM
88 /*
89 ** capitalize() - This routine designed to modify sullable like this:
90 ** adel ----> Adel
91 ** dot ----> Dot
92 ** etc.
93 ** INPUT:
94 ** char * - syllable.
95 ** OUTPUT:
96 ** none.
97 ** NOTES:
98 ** none.
99 */
100 void
101 capitalize (char *syllable)
102 {
103 char tmp = 0x00;
104 int i = 0;
105 if ( randint(2) == TRUE)
106 {
107 (void)memcpy((void *)&tmp, (void *)syllable, sizeof(tmp));
108 for(i=0; i < 26; i++)
109 if ( let[i] == tmp )
110 if (is_restricted_symbol(clet[i]) != TRUE)
111 (void)memcpy ((void *)syllable, (void *)&clet[i], 1);
112 }
113 }
114
115 /*
116 ** numerize() - This routine designed to modify single-letter
117 ** syllable like this:
118 ** a ----> 1 or 2 or 3 etc.
119 ** u ----> 1 or 2 or 3 etc.
120 ** etc.
121 ** INPUT:
122 ** char * - single-letter syllable
123 ** OUTPUT:
124 ** none.
125 ** NOTES:
126 ** none.
127 */
128 void
129 numerize (char *syllable)
130 {
131 char *tmp;
132 if ( (tmp = (char *)calloc(1, 4)) == NULL)
133 err_sys_fatal("calloc");
134 if ( strlen (syllable) == 1 )
135 {
136 (void) gen_rand_symbol(tmp, S_NB);
137 (void)memcpy ((void *)syllable, (void *)tmp, 1);
138 }
139 free ((void *)tmp);
140 }
141 /*
142 ** specialize() - This routine designed to modify single-letter syllable
143 ** like this:
144 ** a ----> # or $ or % etc.
145 ** u ----> # or $ or % etc.
146 ** etc.
147 ** INPUT:
148 ** char * - single-letter syllable.
149 ** OUTPUT:
150 ** none.
151 ** NOTES:
152 ** none.
153 */
154 void
155 specialize (char *syllable)
156 {
157 char *tmp;
158 if ( (tmp = (char *)calloc(1, 4)) == NULL)
159 err_sys_fatal("calloc");
160 if ( strlen (syllable) == 1 )
161 {
162 (void) gen_rand_symbol(tmp, S_SS);
163 (void)memcpy ((void *)syllable, (void *)tmp, 1);
164 }
165 free ((void *)tmp);
166 }
167
168 /*
169 ** symb2name - convert symbol to it's name
170 ** INPUT:
171 ** char * - one symbol syllable
172 ** OUTPUT:
173 ** none.
174 ** NOTES:
175 ** none.
176 */
177 void
178 symb2name(char * syllable, char * h_syllable)
179 {
180 struct ssymb_names
181 {
182 char symbol;
183 char *name;
184 };
185 static struct ssymb_names ssn[42] =
186 {
187 {'1',"ONE"},
188 {'2',"TWO"},
189 {'3',"THREE"},
190 {'4',"FOUR"},
191 {'5',"FIVE"},
192 {'6',"SIX"},
193 {'7',"SEVEN"},
194 {'8',"EIGHT"},
195 {'9',"NINE"},
196 {'0',"ZERO"},
197 {33, "EXCLAMATION_POINT"},
198 {34, "QUOTATION_MARK"},
199 {35, "CROSSHATCH"},
200 {36, "DOLLAR_SIGN"},
201 {37, "PERCENT_SIGN"},
202 {38, "AMPERSAND"},
203 {39, "APOSTROPHE"},
204 {40, "LEFT_PARENTHESIS"},
205 {41, "RIGHT_PARENTHESIS"},
206 {42, "ASTERISK"},
207 {43, "PLUS_SIGN"},
208 {44, "COMMA"},
209 {45, "HYPHEN"},
210 {46, "PERIOD"},
211 {47, "SLASH"},
212 {58, "COLON"},
213 {59, "SEMICOLON"},
214 {60, "LESS_THAN"},
215 {61, "EQUAL_SIGN"},
216 {62, "GREATER_THAN"},
217 {63, "QUESTION_MARK"},
218 {64, "AT_SIGN"},
219 {91, "LEFT_BRACKET"},
220 {92, "BACKSLASH"},
221 {93, "RIGHT_BRACKET"},
222 {94, "CIRCUMFLEX"},
223 {95, "UNDERSCORE"},
224 {96, "GRAVE"},
225 {123, "LEFT_BRACE"},
226 {124, "VERTICAL_BAR"},
227 {125, "RIGHT_BRACE"},
228 {126, "TILDE"}
229 };
230 int i = 0;
231 int flag = FALSE;
232
233 if (strlen(syllable) == 1)
234 {
235 for (i = 0; i < 42; i++)
236 {
237 if(*syllable == ssn[i].symbol)
238 {
239 (void)memcpy((void*)h_syllable, (void*)ssn[i].name, strlen(ssn[i].name));
240 flag = TRUE;
241 }
242 }
243 if (flag != TRUE)
244 (void)memcpy((void*)h_syllable, (void*)syllable, strlen(syllable));
245 }
246 }
247
248 /*
249 ** spell_word - spell the word
250 ** INPUT:
251 ** char * - pointer to the word
252 ** char * - pointer to the spelled word
253 ** OUTPUT:
254 ** char * - pointer to the spelled word
255 ** NULL - something is wrong
256 ** NOTES:
257 ** You should free() memory pointed by spelled_word after each use of spell_word
258 */
259 char *
260 spell_word(char * word, char * spelled_word)
261 {
262 struct char_spell
263 {
264 char symbol;
265 char *name;
266 };
267 static struct char_spell cs[94] =
268 {
269 {'1',"ONE" },
270 {'2',"TWO" },
271 {'3',"THREE" },
272 {'4',"FOUR" },
273 {'5',"FIVE" },
274 {'6',"SIX" },
275 {'7',"SEVEN" },
276 {'8',"EIGHT" },
277 {'9',"NINE" },
278 {'0',"ZERO" },
279 {'A', "Alfa" },
280 {'B', "Bravo" },
281 {'C', "Charlie" },
282 {'D', "Delta" },
283 {'E', "Echo" },
284 {'F', "Foxtrot" },
285 {'G', "Golf" },
286 {'H', "Hotel" },
287 {'I', "India" },
288 {'J', "Juliett" },
289 {'K', "Kilo" },
290 {'L', "Lima" },
291 {'M', "Mike" },
292 {'N', "November" },
293 {'O', "Oscar" },
294 {'P', "Papa" },
295 {'Q', "Quebec" },
296 {'R', "Romeo" },
297 {'S', "Sierra" },
298 {'T', "Tango" },
299 {'U', "Uniform" },
300 {'V', "Victor" },
301 {'W', "Whiskey" },
302 {'X', "X_ray" },
303 {'Y', "Yankee" },
304 {'Z', "Zulu" },
305 {'a', "alfa" },
306 {'b', "bravo" },
307 {'c', "charlie" },
308 {'d', "delta" },
309 {'e', "echo" },
310 {'f', "foxtrot" },
311 {'g', "golf" },
312 {'h', "hotel" },
313 {'i', "india" },
314 {'j', "juliett" },
315 {'k', "kilo" },
316 {'l', "lima" },
317 {'m', "mike" },
318 {'n', "november" },
319 {'o', "oscar" },
320 {'p', "papa" },
321 {'q', "quebec" },
322 {'r', "romeo" },
323 {'s', "sierra" },
324 {'t', "tango" },
325 {'u', "uniform" },
326 {'v', "victor" },
327 {'w', "whiskey" },
328 {'x', "x_ray" },
329 {'y', "yankee" },
330 {'z', "zulu" },
331 {33, "EXCLAMATION_POINT"},
332 {34, "QUOTATION_MARK" },
333 {35, "CROSSHATCH" },
334 {36, "DOLLAR_SIGN" },
335 {37, "PERCENT_SIGN" },
336 {38, "AMPERSAND" },
337 {39, "APOSTROPHE" },
338 {40, "LEFT_PARENTHESIS" },
339 {41, "RIGHT_PARENTHESIS"},
340 {42, "ASTERISK" },
341 {43, "PLUS_SIGN" },
342 {44, "COMMA" },
343 {45, "HYPHEN" },
344 {46, "PERIOD" },
345 {47, "SLASH" },
346 {58, "COLON" },
347 {59, "SEMICOLON" },
348 {60, "LESS_THAN" },
349 {61, "EQUAL_SIGN" },
350 {62, "GREATER_THAN" },
351 {63, "QUESTION_MARK" },
352 {64, "AT_SIGN" },
353 {91, "LEFT_BRACKET" },
354 {92, "BACKSLASH" },
355 {93, "RIGHT_BRACKET" },
356 {94, "CIRCUMFLEX" },
357 {95, "UNDERSCORE" },
358 {96, "GRAVE" },
359 {123, "LEFT_BRACE" },
360 {124, "VERTICAL_BAR" },
361 {125, "RIGHT_BRACE" },
362 {126, "TILDE" }
363 };
364 int s_length = 0;
365 int i = 0;
366 int j = 0;
367 int word_len = strlen(word);
368 char * tmp_ptr;
369 char hyphen = '-';
370 char zero = 0x00;
371
372 /* Count the length of the spelled word */
373 for (i=0; i <= word_len; i++)
374 for (j=0; j < 94; j++)
375 if (word[i] == cs[j].symbol)
376 {
377 s_length = s_length + strlen(cs[j].name) + 1;
378 continue;
379 }
380
381 /* Allocate memory for spelled word */
382 if ( (spelled_word = (char *)calloc(1, (size_t)s_length)) == NULL)
383 return(NULL);
384
385 /* Construct spelled word */
386 tmp_ptr = spelled_word;
387
388 for (i=0; i < word_len; i++)
389 for (j=0; j < 94; j++)
390 if (word[i] == cs[j].symbol)
391 {
392 (void) memcpy((void *)tmp_ptr, (void *)cs[j].name, strlen(cs[j].name));
393 tmp_ptr = tmp_ptr + strlen(cs[j].name);
394 /* Place the hyphen after each symbol */
395 (void) memcpy((void *)(tmp_ptr), (void *)&hyphen, 1);
396 tmp_ptr = tmp_ptr + 1;
397 continue;
398 }
399
400 /* Remove hyphen at the end of the word */
401 tmp_ptr = tmp_ptr - 1;
402 (void) memcpy((void *)(tmp_ptr), (void *)&zero, 1);
403
404 return (spelled_word);
405 }
406
407 #endif /* APGBFM */