1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate */
5*7c478bd9Sstevel@tonic-gate
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate * This program is copyright Alec Muffett 1993. The author disclaims all
8*7c478bd9Sstevel@tonic-gate * responsibility or liability with respect to it's usage or its effect
9*7c478bd9Sstevel@tonic-gate * upon hardware or computer systems, and maintains copyright as set out
10*7c478bd9Sstevel@tonic-gate * in the "LICENCE" document which accompanies distributions of Crack v4.0
11*7c478bd9Sstevel@tonic-gate * and upwards.
12*7c478bd9Sstevel@tonic-gate */
13*7c478bd9Sstevel@tonic-gate
14*7c478bd9Sstevel@tonic-gate #include "packer.h"
15*7c478bd9Sstevel@tonic-gate
16*7c478bd9Sstevel@tonic-gate
17*7c478bd9Sstevel@tonic-gate #define RULE_NOOP ':'
18*7c478bd9Sstevel@tonic-gate #define RULE_PREPEND '^'
19*7c478bd9Sstevel@tonic-gate #define RULE_APPEND '$'
20*7c478bd9Sstevel@tonic-gate #define RULE_REVERSE 'r'
21*7c478bd9Sstevel@tonic-gate #define RULE_UPPERCASE 'u'
22*7c478bd9Sstevel@tonic-gate #define RULE_LOWERCASE 'l'
23*7c478bd9Sstevel@tonic-gate #define RULE_PLURALISE 'p'
24*7c478bd9Sstevel@tonic-gate #define RULE_CAPITALISE 'c'
25*7c478bd9Sstevel@tonic-gate #define RULE_DUPLICATE 'd'
26*7c478bd9Sstevel@tonic-gate #define RULE_REFLECT 'f'
27*7c478bd9Sstevel@tonic-gate #define RULE_SUBSTITUTE 's'
28*7c478bd9Sstevel@tonic-gate #define RULE_MATCH '/'
29*7c478bd9Sstevel@tonic-gate #define RULE_NOT '!'
30*7c478bd9Sstevel@tonic-gate #define RULE_LT '<'
31*7c478bd9Sstevel@tonic-gate #define RULE_GT '>'
32*7c478bd9Sstevel@tonic-gate #define RULE_EXTRACT 'x'
33*7c478bd9Sstevel@tonic-gate #define RULE_OVERSTRIKE 'o'
34*7c478bd9Sstevel@tonic-gate #define RULE_INSERT 'i'
35*7c478bd9Sstevel@tonic-gate #define RULE_EQUALS '='
36*7c478bd9Sstevel@tonic-gate #define RULE_PURGE '@'
37*7c478bd9Sstevel@tonic-gate #define RULE_CLASS '?' /* class rule? socialist ethic in cracker? */
38*7c478bd9Sstevel@tonic-gate #define RULE_DFIRST '['
39*7c478bd9Sstevel@tonic-gate #define RULE_DLAST ']'
40*7c478bd9Sstevel@tonic-gate #define RULE_MFIRST '('
41*7c478bd9Sstevel@tonic-gate #define RULE_MLAST ')'
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate int
Suffix(char * myword,char * suffix)44*7c478bd9Sstevel@tonic-gate Suffix(char *myword, char *suffix)
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate register int i;
47*7c478bd9Sstevel@tonic-gate register int j;
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate i = strlen(myword);
50*7c478bd9Sstevel@tonic-gate j = strlen(suffix);
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate if (i > j) {
53*7c478bd9Sstevel@tonic-gate return (STRCMP((myword + i - j), suffix));
54*7c478bd9Sstevel@tonic-gate } else {
55*7c478bd9Sstevel@tonic-gate return (-1);
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate }
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate char *
Reverse(register char * str)60*7c478bd9Sstevel@tonic-gate Reverse(register char *str) /* return a pointer to a reversal */
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate register int i;
63*7c478bd9Sstevel@tonic-gate register int j;
64*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate j = i = strlen(str);
67*7c478bd9Sstevel@tonic-gate while (*str) {
68*7c478bd9Sstevel@tonic-gate area[--i] = *str++;
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate area[j] = '\0';
71*7c478bd9Sstevel@tonic-gate return (area);
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate char *
Uppercase(register char * str)75*7c478bd9Sstevel@tonic-gate Uppercase(register char *str) /* return a pointer to an uppercase */
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate register char *ptr;
78*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate ptr = area;
81*7c478bd9Sstevel@tonic-gate while (*str) {
82*7c478bd9Sstevel@tonic-gate *(ptr++) = CRACK_TOUPPER(*str);
83*7c478bd9Sstevel@tonic-gate str++;
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate *ptr = '\0';
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate return (area);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate char *
Lowercase(register char * str)91*7c478bd9Sstevel@tonic-gate Lowercase(register char *str) /* return a pointer to an lowercase */
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate register char *ptr;
94*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate ptr = area;
97*7c478bd9Sstevel@tonic-gate while (*str) {
98*7c478bd9Sstevel@tonic-gate *(ptr++) = CRACK_TOLOWER(*str);
99*7c478bd9Sstevel@tonic-gate str++;
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate *ptr = '\0';
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate return (area);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate char *
Capitalise(register char * str)107*7c478bd9Sstevel@tonic-gate Capitalise(register char *str) /* return a pointer to an capitalised */
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate register char *ptr;
110*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate ptr = area;
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate while (*str) {
115*7c478bd9Sstevel@tonic-gate *(ptr++) = CRACK_TOLOWER(*str);
116*7c478bd9Sstevel@tonic-gate str++;
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate *ptr = '\0';
120*7c478bd9Sstevel@tonic-gate area[0] = CRACK_TOUPPER(area[0]);
121*7c478bd9Sstevel@tonic-gate return (area);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate char *
Pluralise(register char * string)125*7c478bd9Sstevel@tonic-gate Pluralise(register char *string) /* returns a pointer to a plural */
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate register int length;
128*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate length = strlen(string);
131*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, string, PATH_MAX);
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate if (!Suffix(string, "ch") ||
134*7c478bd9Sstevel@tonic-gate !Suffix(string, "ex") ||
135*7c478bd9Sstevel@tonic-gate !Suffix(string, "ix") ||
136*7c478bd9Sstevel@tonic-gate !Suffix(string, "sh") ||
137*7c478bd9Sstevel@tonic-gate !Suffix(string, "ss")) {
138*7c478bd9Sstevel@tonic-gate /* bench -> benches */
139*7c478bd9Sstevel@tonic-gate (void) strcat(area, "es");
140*7c478bd9Sstevel@tonic-gate } else if (length > 2 && string[length - 1] == 'y') {
141*7c478bd9Sstevel@tonic-gate if (strchr("aeiou", string[length - 2])) {
142*7c478bd9Sstevel@tonic-gate /* alloy -> alloys */
143*7c478bd9Sstevel@tonic-gate (void) strcat(area, "s");
144*7c478bd9Sstevel@tonic-gate } else {
145*7c478bd9Sstevel@tonic-gate /* gully -> gullies */
146*7c478bd9Sstevel@tonic-gate (void) strcpy(area + length - 1, "ies");
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate } else if (string[length - 1] == 's') {
149*7c478bd9Sstevel@tonic-gate /* bias -> biases */
150*7c478bd9Sstevel@tonic-gate (void) strcat(area, "es");
151*7c478bd9Sstevel@tonic-gate } else {
152*7c478bd9Sstevel@tonic-gate /* catchall */
153*7c478bd9Sstevel@tonic-gate (void) strcat(area, "s");
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate return (area);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate char *
Substitute(register char * string,register char old,register char new)160*7c478bd9Sstevel@tonic-gate Substitute(register char *string, register char old,
161*7c478bd9Sstevel@tonic-gate register char new) /* returns pointer to a swapped about copy */
162*7c478bd9Sstevel@tonic-gate {
163*7c478bd9Sstevel@tonic-gate register char *ptr;
164*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate ptr = area;
167*7c478bd9Sstevel@tonic-gate while (*string) {
168*7c478bd9Sstevel@tonic-gate *(ptr++) = (*string == old ? new : *string);
169*7c478bd9Sstevel@tonic-gate string++;
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate *ptr = '\0';
172*7c478bd9Sstevel@tonic-gate return (area);
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate
175*7c478bd9Sstevel@tonic-gate /* returns pointer to a purged copy */
176*7c478bd9Sstevel@tonic-gate char *
Purge(register char * string,register char target)177*7c478bd9Sstevel@tonic-gate Purge(register char *string, register char target)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate register char *ptr;
180*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
181*7c478bd9Sstevel@tonic-gate ptr = area;
182*7c478bd9Sstevel@tonic-gate while (*string) {
183*7c478bd9Sstevel@tonic-gate if (*string != target) {
184*7c478bd9Sstevel@tonic-gate *(ptr++) = *string;
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate string++;
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate *ptr = '\0';
189*7c478bd9Sstevel@tonic-gate return (area);
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate /* -------- CHARACTER CLASSES START HERE -------- */
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate /*
194*7c478bd9Sstevel@tonic-gate * this function takes two inputs, a class identifier and a character, and
195*7c478bd9Sstevel@tonic-gate * returns non-null if the given character is a member of the class, based
196*7c478bd9Sstevel@tonic-gate * upon restrictions set out below
197*7c478bd9Sstevel@tonic-gate */
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate int
MatchClass(register char class,register char input)200*7c478bd9Sstevel@tonic-gate MatchClass(register char class, register char input)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate register char c;
203*7c478bd9Sstevel@tonic-gate register int retval;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate retval = 0;
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate switch (class) {
208*7c478bd9Sstevel@tonic-gate /* ESCAPE */
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate case '?': /* ?? -> ? */
211*7c478bd9Sstevel@tonic-gate if (input == '?') {
212*7c478bd9Sstevel@tonic-gate retval = 1;
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate break;
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate /* ILLOGICAL GROUPINGS (ie: not in ctype.h) */
217*7c478bd9Sstevel@tonic-gate
218*7c478bd9Sstevel@tonic-gate case 'V':
219*7c478bd9Sstevel@tonic-gate case 'v': /* vowels */
220*7c478bd9Sstevel@tonic-gate c = CRACK_TOLOWER(input);
221*7c478bd9Sstevel@tonic-gate if (strchr("aeiou", c)) {
222*7c478bd9Sstevel@tonic-gate retval = 1;
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate break;
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate case 'C':
227*7c478bd9Sstevel@tonic-gate case 'c': /* consonants */
228*7c478bd9Sstevel@tonic-gate c = CRACK_TOLOWER(input);
229*7c478bd9Sstevel@tonic-gate if (strchr("bcdfghjklmnpqrstvwxyz", c)) {
230*7c478bd9Sstevel@tonic-gate retval = 1;
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate break;
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate case 'W':
235*7c478bd9Sstevel@tonic-gate case 'w': /* whitespace */
236*7c478bd9Sstevel@tonic-gate if (strchr("\t ", input)) {
237*7c478bd9Sstevel@tonic-gate retval = 1;
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate break;
240*7c478bd9Sstevel@tonic-gate
241*7c478bd9Sstevel@tonic-gate case 'P':
242*7c478bd9Sstevel@tonic-gate case 'p': /* punctuation */
243*7c478bd9Sstevel@tonic-gate if (strchr(".`,:;'!?\"", input)) {
244*7c478bd9Sstevel@tonic-gate retval = 1;
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate break;
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate case 'S':
249*7c478bd9Sstevel@tonic-gate case 's': /* symbols */
250*7c478bd9Sstevel@tonic-gate if (strchr("$%%^&*()-_+=|\\[]{}#@/~", input)) {
251*7c478bd9Sstevel@tonic-gate retval = 1;
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate break;
254*7c478bd9Sstevel@tonic-gate
255*7c478bd9Sstevel@tonic-gate /* LOGICAL GROUPINGS */
256*7c478bd9Sstevel@tonic-gate
257*7c478bd9Sstevel@tonic-gate case 'L':
258*7c478bd9Sstevel@tonic-gate case 'l': /* lowercase */
259*7c478bd9Sstevel@tonic-gate if (islower(input)) {
260*7c478bd9Sstevel@tonic-gate retval = 1;
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate break;
263*7c478bd9Sstevel@tonic-gate
264*7c478bd9Sstevel@tonic-gate case 'U':
265*7c478bd9Sstevel@tonic-gate case 'u': /* uppercase */
266*7c478bd9Sstevel@tonic-gate if (isupper(input)) {
267*7c478bd9Sstevel@tonic-gate retval = 1;
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate break;
270*7c478bd9Sstevel@tonic-gate
271*7c478bd9Sstevel@tonic-gate case 'A':
272*7c478bd9Sstevel@tonic-gate case 'a': /* alphabetic */
273*7c478bd9Sstevel@tonic-gate if (isalpha(input)) {
274*7c478bd9Sstevel@tonic-gate retval = 1;
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate break;
277*7c478bd9Sstevel@tonic-gate
278*7c478bd9Sstevel@tonic-gate case 'X':
279*7c478bd9Sstevel@tonic-gate case 'x': /* alphanumeric */
280*7c478bd9Sstevel@tonic-gate if (isalnum(input)) {
281*7c478bd9Sstevel@tonic-gate retval = 1;
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate break;
284*7c478bd9Sstevel@tonic-gate
285*7c478bd9Sstevel@tonic-gate case 'D':
286*7c478bd9Sstevel@tonic-gate case 'd': /* digits */
287*7c478bd9Sstevel@tonic-gate if (isdigit(input)) {
288*7c478bd9Sstevel@tonic-gate retval = 1;
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate break;
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate
293*7c478bd9Sstevel@tonic-gate if (isupper(class)) {
294*7c478bd9Sstevel@tonic-gate return (!retval);
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate return (retval);
297*7c478bd9Sstevel@tonic-gate }
298*7c478bd9Sstevel@tonic-gate
299*7c478bd9Sstevel@tonic-gate char *
PolyStrchr(register char * string,register char class)300*7c478bd9Sstevel@tonic-gate PolyStrchr(register char *string, register char class)
301*7c478bd9Sstevel@tonic-gate {
302*7c478bd9Sstevel@tonic-gate while (*string) {
303*7c478bd9Sstevel@tonic-gate if (MatchClass(class, *string)) {
304*7c478bd9Sstevel@tonic-gate return (string);
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate string++;
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate return ((char *)0);
309*7c478bd9Sstevel@tonic-gate }
310*7c478bd9Sstevel@tonic-gate
311*7c478bd9Sstevel@tonic-gate /* returns pointer to a swapped about copy */
312*7c478bd9Sstevel@tonic-gate char *
PolySubst(register char * string,register char class,register char new)313*7c478bd9Sstevel@tonic-gate PolySubst(register char *string, register char class, register char new)
314*7c478bd9Sstevel@tonic-gate {
315*7c478bd9Sstevel@tonic-gate register char *ptr;
316*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
317*7c478bd9Sstevel@tonic-gate
318*7c478bd9Sstevel@tonic-gate ptr = area;
319*7c478bd9Sstevel@tonic-gate while (*string) {
320*7c478bd9Sstevel@tonic-gate *(ptr++) = (MatchClass(class, *string) ? new : *string);
321*7c478bd9Sstevel@tonic-gate string++;
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate *ptr = '\0';
324*7c478bd9Sstevel@tonic-gate return (area);
325*7c478bd9Sstevel@tonic-gate }
326*7c478bd9Sstevel@tonic-gate
327*7c478bd9Sstevel@tonic-gate /* returns pointer to a purged copy */
328*7c478bd9Sstevel@tonic-gate char *
PolyPurge(register char * string,register char class)329*7c478bd9Sstevel@tonic-gate PolyPurge(register char *string, register char class)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate register char *ptr;
332*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
333*7c478bd9Sstevel@tonic-gate
334*7c478bd9Sstevel@tonic-gate ptr = area;
335*7c478bd9Sstevel@tonic-gate while (*string) {
336*7c478bd9Sstevel@tonic-gate if (!MatchClass(class, *string)) {
337*7c478bd9Sstevel@tonic-gate *(ptr++) = *string;
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate string++;
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate *ptr = '\0';
342*7c478bd9Sstevel@tonic-gate return (area);
343*7c478bd9Sstevel@tonic-gate }
344*7c478bd9Sstevel@tonic-gate /* -------- BACK TO NORMALITY -------- */
345*7c478bd9Sstevel@tonic-gate
346*7c478bd9Sstevel@tonic-gate int
Char2Int(char character)347*7c478bd9Sstevel@tonic-gate Char2Int(char character)
348*7c478bd9Sstevel@tonic-gate {
349*7c478bd9Sstevel@tonic-gate if (isdigit(character)) {
350*7c478bd9Sstevel@tonic-gate return (character - '0');
351*7c478bd9Sstevel@tonic-gate } else if (islower(character)) {
352*7c478bd9Sstevel@tonic-gate return (character - 'a' + 10);
353*7c478bd9Sstevel@tonic-gate } else if (isupper(character)) {
354*7c478bd9Sstevel@tonic-gate return (character - 'A' + 10);
355*7c478bd9Sstevel@tonic-gate }
356*7c478bd9Sstevel@tonic-gate return (-1);
357*7c478bd9Sstevel@tonic-gate }
358*7c478bd9Sstevel@tonic-gate
359*7c478bd9Sstevel@tonic-gate /* returns a pointer to a controlled Mangle */
360*7c478bd9Sstevel@tonic-gate char *
Mangle(char * input,char * control)361*7c478bd9Sstevel@tonic-gate Mangle(char *input, char *control)
362*7c478bd9Sstevel@tonic-gate {
363*7c478bd9Sstevel@tonic-gate int limit;
364*7c478bd9Sstevel@tonic-gate register char *ptr;
365*7c478bd9Sstevel@tonic-gate static char area[PATH_MAX];
366*7c478bd9Sstevel@tonic-gate char area2[PATH_MAX];
367*7c478bd9Sstevel@tonic-gate
368*7c478bd9Sstevel@tonic-gate area[0] = '\0';
369*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, input, PATH_MAX);
370*7c478bd9Sstevel@tonic-gate
371*7c478bd9Sstevel@tonic-gate for (ptr = control; *ptr; ptr++) {
372*7c478bd9Sstevel@tonic-gate switch (*ptr) {
373*7c478bd9Sstevel@tonic-gate case RULE_NOOP:
374*7c478bd9Sstevel@tonic-gate break;
375*7c478bd9Sstevel@tonic-gate case RULE_REVERSE:
376*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, Reverse(area), PATH_MAX);
377*7c478bd9Sstevel@tonic-gate break;
378*7c478bd9Sstevel@tonic-gate case RULE_UPPERCASE:
379*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, Uppercase(area), PATH_MAX);
380*7c478bd9Sstevel@tonic-gate break;
381*7c478bd9Sstevel@tonic-gate case RULE_LOWERCASE:
382*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, Lowercase(area), PATH_MAX);
383*7c478bd9Sstevel@tonic-gate break;
384*7c478bd9Sstevel@tonic-gate case RULE_CAPITALISE:
385*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, Capitalise(area),
386*7c478bd9Sstevel@tonic-gate PATH_MAX);
387*7c478bd9Sstevel@tonic-gate break;
388*7c478bd9Sstevel@tonic-gate case RULE_PLURALISE:
389*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, Pluralise(area), PATH_MAX);
390*7c478bd9Sstevel@tonic-gate break;
391*7c478bd9Sstevel@tonic-gate case RULE_REFLECT:
392*7c478bd9Sstevel@tonic-gate (void) strlcat(area, Reverse(area), PATH_MAX);
393*7c478bd9Sstevel@tonic-gate break;
394*7c478bd9Sstevel@tonic-gate case RULE_DUPLICATE:
395*7c478bd9Sstevel@tonic-gate (void) strlcpy(area2, area, PATH_MAX);
396*7c478bd9Sstevel@tonic-gate (void) strlcat(area, area2, PATH_MAX);
397*7c478bd9Sstevel@tonic-gate break;
398*7c478bd9Sstevel@tonic-gate case RULE_GT:
399*7c478bd9Sstevel@tonic-gate if (!ptr[1]) {
400*7c478bd9Sstevel@tonic-gate return ((char *)0);
401*7c478bd9Sstevel@tonic-gate } else {
402*7c478bd9Sstevel@tonic-gate limit = Char2Int(*(++ptr));
403*7c478bd9Sstevel@tonic-gate if (limit < 0) {
404*7c478bd9Sstevel@tonic-gate return ((char *)0);
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate if (strlen(area) <= limit) {
407*7c478bd9Sstevel@tonic-gate return ((char *)0);
408*7c478bd9Sstevel@tonic-gate }
409*7c478bd9Sstevel@tonic-gate }
410*7c478bd9Sstevel@tonic-gate break;
411*7c478bd9Sstevel@tonic-gate case RULE_LT:
412*7c478bd9Sstevel@tonic-gate if (!ptr[1]) {
413*7c478bd9Sstevel@tonic-gate return ((char *)0);
414*7c478bd9Sstevel@tonic-gate } else {
415*7c478bd9Sstevel@tonic-gate limit = Char2Int(*(++ptr));
416*7c478bd9Sstevel@tonic-gate if (limit < 0) {
417*7c478bd9Sstevel@tonic-gate return ((char *)0);
418*7c478bd9Sstevel@tonic-gate }
419*7c478bd9Sstevel@tonic-gate if (strlen(area) >= limit) {
420*7c478bd9Sstevel@tonic-gate return ((char *)0);
421*7c478bd9Sstevel@tonic-gate }
422*7c478bd9Sstevel@tonic-gate }
423*7c478bd9Sstevel@tonic-gate break;
424*7c478bd9Sstevel@tonic-gate case RULE_PREPEND:
425*7c478bd9Sstevel@tonic-gate if (!ptr[1]) {
426*7c478bd9Sstevel@tonic-gate return ((char *)0);
427*7c478bd9Sstevel@tonic-gate } else {
428*7c478bd9Sstevel@tonic-gate area2[0] = *(++ptr);
429*7c478bd9Sstevel@tonic-gate (void) strlcpy(area2 + 1, area,
430*7c478bd9Sstevel@tonic-gate PATH_MAX);
431*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, area2, PATH_MAX);
432*7c478bd9Sstevel@tonic-gate }
433*7c478bd9Sstevel@tonic-gate break;
434*7c478bd9Sstevel@tonic-gate case RULE_APPEND:
435*7c478bd9Sstevel@tonic-gate if (!ptr[1]) {
436*7c478bd9Sstevel@tonic-gate return ((char *)0);
437*7c478bd9Sstevel@tonic-gate } else {
438*7c478bd9Sstevel@tonic-gate register char *string;
439*7c478bd9Sstevel@tonic-gate
440*7c478bd9Sstevel@tonic-gate string = area;
441*7c478bd9Sstevel@tonic-gate while (*(string++));
442*7c478bd9Sstevel@tonic-gate string[-1] = *(++ptr);
443*7c478bd9Sstevel@tonic-gate *string = '\0';
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate break;
446*7c478bd9Sstevel@tonic-gate case RULE_EXTRACT:
447*7c478bd9Sstevel@tonic-gate if (!ptr[1] || !ptr[2]) {
448*7c478bd9Sstevel@tonic-gate return ((char *)0);
449*7c478bd9Sstevel@tonic-gate } else {
450*7c478bd9Sstevel@tonic-gate register int i;
451*7c478bd9Sstevel@tonic-gate int start;
452*7c478bd9Sstevel@tonic-gate int length;
453*7c478bd9Sstevel@tonic-gate
454*7c478bd9Sstevel@tonic-gate start = Char2Int(*(++ptr));
455*7c478bd9Sstevel@tonic-gate length = Char2Int(*(++ptr));
456*7c478bd9Sstevel@tonic-gate if (start < 0 || length < 0) {
457*7c478bd9Sstevel@tonic-gate return ((char *)0);
458*7c478bd9Sstevel@tonic-gate }
459*7c478bd9Sstevel@tonic-gate (void) strlcpy(area2, area, PATH_MAX);
460*7c478bd9Sstevel@tonic-gate for (i = 0; length-- &&
461*7c478bd9Sstevel@tonic-gate area2[start + i]; i++) {
462*7c478bd9Sstevel@tonic-gate area[i] = area2[start + i];
463*7c478bd9Sstevel@tonic-gate }
464*7c478bd9Sstevel@tonic-gate /* cant use strncpy()-no trailing NUL */
465*7c478bd9Sstevel@tonic-gate area[i] = '\0';
466*7c478bd9Sstevel@tonic-gate }
467*7c478bd9Sstevel@tonic-gate break;
468*7c478bd9Sstevel@tonic-gate case RULE_OVERSTRIKE:
469*7c478bd9Sstevel@tonic-gate if (!ptr[1] || !ptr[2]) {
470*7c478bd9Sstevel@tonic-gate return ((char *)0);
471*7c478bd9Sstevel@tonic-gate } else {
472*7c478bd9Sstevel@tonic-gate register int i;
473*7c478bd9Sstevel@tonic-gate
474*7c478bd9Sstevel@tonic-gate i = Char2Int(*(++ptr));
475*7c478bd9Sstevel@tonic-gate if (i < 0) {
476*7c478bd9Sstevel@tonic-gate return ((char *)0);
477*7c478bd9Sstevel@tonic-gate } else {
478*7c478bd9Sstevel@tonic-gate ++ptr;
479*7c478bd9Sstevel@tonic-gate if (area[i]) {
480*7c478bd9Sstevel@tonic-gate area[i] = *ptr;
481*7c478bd9Sstevel@tonic-gate }
482*7c478bd9Sstevel@tonic-gate }
483*7c478bd9Sstevel@tonic-gate }
484*7c478bd9Sstevel@tonic-gate break;
485*7c478bd9Sstevel@tonic-gate case RULE_INSERT:
486*7c478bd9Sstevel@tonic-gate if (!ptr[1] || !ptr[2]) {
487*7c478bd9Sstevel@tonic-gate return ((char *)0);
488*7c478bd9Sstevel@tonic-gate } else {
489*7c478bd9Sstevel@tonic-gate register int i;
490*7c478bd9Sstevel@tonic-gate register char *p1;
491*7c478bd9Sstevel@tonic-gate register char *p2;
492*7c478bd9Sstevel@tonic-gate
493*7c478bd9Sstevel@tonic-gate i = Char2Int(*(++ptr));
494*7c478bd9Sstevel@tonic-gate if (i < 0) {
495*7c478bd9Sstevel@tonic-gate return ((char *)0);
496*7c478bd9Sstevel@tonic-gate }
497*7c478bd9Sstevel@tonic-gate p1 = area;
498*7c478bd9Sstevel@tonic-gate p2 = area2;
499*7c478bd9Sstevel@tonic-gate while (i && *p1) {
500*7c478bd9Sstevel@tonic-gate i--;
501*7c478bd9Sstevel@tonic-gate *(p2++) = *(p1++);
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate *(p2++) = *(++ptr);
504*7c478bd9Sstevel@tonic-gate (void) strlcpy(p2, p1, PATH_MAX);
505*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, area2, PATH_MAX);
506*7c478bd9Sstevel@tonic-gate }
507*7c478bd9Sstevel@tonic-gate break;
508*7c478bd9Sstevel@tonic-gate /* THE FOLLOWING RULES REQUIRE CLASS MATCHING */
509*7c478bd9Sstevel@tonic-gate
510*7c478bd9Sstevel@tonic-gate case RULE_PURGE: /* @x or @?c */
511*7c478bd9Sstevel@tonic-gate if (!ptr[1] || (ptr[1] ==
512*7c478bd9Sstevel@tonic-gate RULE_CLASS && !ptr[2])) {
513*7c478bd9Sstevel@tonic-gate return ((char *)0);
514*7c478bd9Sstevel@tonic-gate } else if (ptr[1] != RULE_CLASS) {
515*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, Purge(area,
516*7c478bd9Sstevel@tonic-gate *(++ptr)), PATH_MAX);
517*7c478bd9Sstevel@tonic-gate } else {
518*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, PolyPurge(area,
519*7c478bd9Sstevel@tonic-gate ptr[2]), PATH_MAX);
520*7c478bd9Sstevel@tonic-gate ptr += 2;
521*7c478bd9Sstevel@tonic-gate }
522*7c478bd9Sstevel@tonic-gate break;
523*7c478bd9Sstevel@tonic-gate case RULE_SUBSTITUTE: /* sxy || s?cy */
524*7c478bd9Sstevel@tonic-gate if (!ptr[1] || !ptr[2] ||
525*7c478bd9Sstevel@tonic-gate (ptr[1] == RULE_CLASS && !ptr[3])) {
526*7c478bd9Sstevel@tonic-gate return ((char *)0);
527*7c478bd9Sstevel@tonic-gate } else if (ptr[1] != RULE_CLASS) {
528*7c478bd9Sstevel@tonic-gate ptr += 2;
529*7c478bd9Sstevel@tonic-gate } else {
530*7c478bd9Sstevel@tonic-gate (void) strlcpy(area, PolySubst(area,
531*7c478bd9Sstevel@tonic-gate ptr[2], ptr[3]), PATH_MAX);
532*7c478bd9Sstevel@tonic-gate ptr += 3;
533*7c478bd9Sstevel@tonic-gate }
534*7c478bd9Sstevel@tonic-gate break;
535*7c478bd9Sstevel@tonic-gate case RULE_MATCH: /* /x || /?c */
536*7c478bd9Sstevel@tonic-gate if (!ptr[1] ||
537*7c478bd9Sstevel@tonic-gate (ptr[1] == RULE_CLASS && !ptr[2])) {
538*7c478bd9Sstevel@tonic-gate return ((char *)0);
539*7c478bd9Sstevel@tonic-gate } else if (ptr[1] != RULE_CLASS) {
540*7c478bd9Sstevel@tonic-gate if (!strchr(area, *(++ptr))) {
541*7c478bd9Sstevel@tonic-gate return ((char *)0);
542*7c478bd9Sstevel@tonic-gate }
543*7c478bd9Sstevel@tonic-gate } else {
544*7c478bd9Sstevel@tonic-gate if (!PolyStrchr(area, ptr[2])) {
545*7c478bd9Sstevel@tonic-gate return ((char *)0);
546*7c478bd9Sstevel@tonic-gate }
547*7c478bd9Sstevel@tonic-gate ptr += 2;
548*7c478bd9Sstevel@tonic-gate }
549*7c478bd9Sstevel@tonic-gate break;
550*7c478bd9Sstevel@tonic-gate case RULE_NOT: /* !x || !?c */
551*7c478bd9Sstevel@tonic-gate if (!ptr[1] ||
552*7c478bd9Sstevel@tonic-gate (ptr[1] == RULE_CLASS && !ptr[2])) {
553*7c478bd9Sstevel@tonic-gate return ((char *)0);
554*7c478bd9Sstevel@tonic-gate } else if (ptr[1] != RULE_CLASS) {
555*7c478bd9Sstevel@tonic-gate if (strchr(area, *(++ptr))) {
556*7c478bd9Sstevel@tonic-gate return ((char *)0);
557*7c478bd9Sstevel@tonic-gate }
558*7c478bd9Sstevel@tonic-gate } else {
559*7c478bd9Sstevel@tonic-gate if (PolyStrchr(area, ptr[2])) {
560*7c478bd9Sstevel@tonic-gate return ((char *)0);
561*7c478bd9Sstevel@tonic-gate }
562*7c478bd9Sstevel@tonic-gate ptr += 2;
563*7c478bd9Sstevel@tonic-gate }
564*7c478bd9Sstevel@tonic-gate break;
565*7c478bd9Sstevel@tonic-gate /*
566*7c478bd9Sstevel@tonic-gate * alternative use for a boomerang, number 1: a standard throwing
567*7c478bd9Sstevel@tonic-gate * boomerang is an ideal thing to use to tuck the sheets under
568*7c478bd9Sstevel@tonic-gate * the mattress when making your bed. The streamlined shape of
569*7c478bd9Sstevel@tonic-gate * the boomerang allows it to slip easily 'twixt mattress and
570*7c478bd9Sstevel@tonic-gate * bedframe, and it's curve makes it very easy to hook sheets
571*7c478bd9Sstevel@tonic-gate * into the gap.
572*7c478bd9Sstevel@tonic-gate */
573*7c478bd9Sstevel@tonic-gate
574*7c478bd9Sstevel@tonic-gate case RULE_EQUALS: /* =nx || =n?c */
575*7c478bd9Sstevel@tonic-gate if (!ptr[1] || !ptr[2] ||
576*7c478bd9Sstevel@tonic-gate (ptr[2] == RULE_CLASS && !ptr[3])) {
577*7c478bd9Sstevel@tonic-gate return ((char *)0);
578*7c478bd9Sstevel@tonic-gate } else {
579*7c478bd9Sstevel@tonic-gate register int i;
580*7c478bd9Sstevel@tonic-gate
581*7c478bd9Sstevel@tonic-gate if ((i = Char2Int(ptr[1])) < 0) {
582*7c478bd9Sstevel@tonic-gate return ((char *)0);
583*7c478bd9Sstevel@tonic-gate }
584*7c478bd9Sstevel@tonic-gate if (ptr[2] != RULE_CLASS) {
585*7c478bd9Sstevel@tonic-gate ptr += 2;
586*7c478bd9Sstevel@tonic-gate if (area[i] != *ptr) {
587*7c478bd9Sstevel@tonic-gate return ((char *)0);
588*7c478bd9Sstevel@tonic-gate }
589*7c478bd9Sstevel@tonic-gate } else {
590*7c478bd9Sstevel@tonic-gate ptr += 3;
591*7c478bd9Sstevel@tonic-gate if (!MatchClass(*ptr,
592*7c478bd9Sstevel@tonic-gate area[i])) {
593*7c478bd9Sstevel@tonic-gate return ((char *)0);
594*7c478bd9Sstevel@tonic-gate }
595*7c478bd9Sstevel@tonic-gate }
596*7c478bd9Sstevel@tonic-gate }
597*7c478bd9Sstevel@tonic-gate break;
598*7c478bd9Sstevel@tonic-gate
599*7c478bd9Sstevel@tonic-gate case RULE_DFIRST:
600*7c478bd9Sstevel@tonic-gate if (area[0]) {
601*7c478bd9Sstevel@tonic-gate register int i;
602*7c478bd9Sstevel@tonic-gate
603*7c478bd9Sstevel@tonic-gate for (i = 1; area[i]; i++) {
604*7c478bd9Sstevel@tonic-gate area[i - 1] = area[i];
605*7c478bd9Sstevel@tonic-gate }
606*7c478bd9Sstevel@tonic-gate area[i - 1] = '\0';
607*7c478bd9Sstevel@tonic-gate }
608*7c478bd9Sstevel@tonic-gate break;
609*7c478bd9Sstevel@tonic-gate
610*7c478bd9Sstevel@tonic-gate case RULE_DLAST:
611*7c478bd9Sstevel@tonic-gate if (area[0]) {
612*7c478bd9Sstevel@tonic-gate register int i;
613*7c478bd9Sstevel@tonic-gate
614*7c478bd9Sstevel@tonic-gate for (i = 1; area[i]; i++);
615*7c478bd9Sstevel@tonic-gate area[i - 1] = '\0';
616*7c478bd9Sstevel@tonic-gate }
617*7c478bd9Sstevel@tonic-gate break;
618*7c478bd9Sstevel@tonic-gate
619*7c478bd9Sstevel@tonic-gate case RULE_MFIRST:
620*7c478bd9Sstevel@tonic-gate if (!ptr[1] ||
621*7c478bd9Sstevel@tonic-gate (ptr[1] == RULE_CLASS && !ptr[2])) {
622*7c478bd9Sstevel@tonic-gate return ((char *)0);
623*7c478bd9Sstevel@tonic-gate } else {
624*7c478bd9Sstevel@tonic-gate if (ptr[1] != RULE_CLASS) {
625*7c478bd9Sstevel@tonic-gate ptr++;
626*7c478bd9Sstevel@tonic-gate if (area[0] != *ptr) {
627*7c478bd9Sstevel@tonic-gate return ((char *)0);
628*7c478bd9Sstevel@tonic-gate }
629*7c478bd9Sstevel@tonic-gate } else {
630*7c478bd9Sstevel@tonic-gate ptr += 2;
631*7c478bd9Sstevel@tonic-gate if (!MatchClass(*ptr,
632*7c478bd9Sstevel@tonic-gate area[0])) {
633*7c478bd9Sstevel@tonic-gate return ((char *)0);
634*7c478bd9Sstevel@tonic-gate }
635*7c478bd9Sstevel@tonic-gate }
636*7c478bd9Sstevel@tonic-gate }
637*7c478bd9Sstevel@tonic-gate break;
638*7c478bd9Sstevel@tonic-gate case RULE_MLAST:
639*7c478bd9Sstevel@tonic-gate if (!ptr[1] ||
640*7c478bd9Sstevel@tonic-gate (ptr[1] == RULE_CLASS && !ptr[2])) {
641*7c478bd9Sstevel@tonic-gate return ((char *)0);
642*7c478bd9Sstevel@tonic-gate } else {
643*7c478bd9Sstevel@tonic-gate register int i;
644*7c478bd9Sstevel@tonic-gate
645*7c478bd9Sstevel@tonic-gate for (i = 0; area[i]; i++);
646*7c478bd9Sstevel@tonic-gate
647*7c478bd9Sstevel@tonic-gate if (i > 0) {
648*7c478bd9Sstevel@tonic-gate i--;
649*7c478bd9Sstevel@tonic-gate } else {
650*7c478bd9Sstevel@tonic-gate return ((char *)0);
651*7c478bd9Sstevel@tonic-gate }
652*7c478bd9Sstevel@tonic-gate if (ptr[1] != RULE_CLASS) {
653*7c478bd9Sstevel@tonic-gate ptr++;
654*7c478bd9Sstevel@tonic-gate if (area[i] != *ptr) {
655*7c478bd9Sstevel@tonic-gate return ((char *)0);
656*7c478bd9Sstevel@tonic-gate }
657*7c478bd9Sstevel@tonic-gate } else {
658*7c478bd9Sstevel@tonic-gate ptr += 2;
659*7c478bd9Sstevel@tonic-gate if (!MatchClass(*ptr,
660*7c478bd9Sstevel@tonic-gate area[i])) {
661*7c478bd9Sstevel@tonic-gate return ((char *)0);
662*7c478bd9Sstevel@tonic-gate }
663*7c478bd9Sstevel@tonic-gate }
664*7c478bd9Sstevel@tonic-gate }
665*7c478bd9Sstevel@tonic-gate break;
666*7c478bd9Sstevel@tonic-gate }
667*7c478bd9Sstevel@tonic-gate }
668*7c478bd9Sstevel@tonic-gate if (!area[0]) { /* have we deweted de poor widdle fing away? */
669*7c478bd9Sstevel@tonic-gate return ((char *)0);
670*7c478bd9Sstevel@tonic-gate }
671*7c478bd9Sstevel@tonic-gate return (area);
672*7c478bd9Sstevel@tonic-gate }
673*7c478bd9Sstevel@tonic-gate /*
674*7c478bd9Sstevel@tonic-gate * int
675*7c478bd9Sstevel@tonic-gate * PMatch(register char *control, register char *string)
676*7c478bd9Sstevel@tonic-gate * {
677*7c478bd9Sstevel@tonic-gate * while (*string && *control) {
678*7c478bd9Sstevel@tonic-gate * if (!MatchClass(*control, *string)) {
679*7c478bd9Sstevel@tonic-gate * return (0);
680*7c478bd9Sstevel@tonic-gate * }
681*7c478bd9Sstevel@tonic-gate *
682*7c478bd9Sstevel@tonic-gate * string++;
683*7c478bd9Sstevel@tonic-gate * control++;
684*7c478bd9Sstevel@tonic-gate * }
685*7c478bd9Sstevel@tonic-gate *
686*7c478bd9Sstevel@tonic-gate * if (*string || *control) {
687*7c478bd9Sstevel@tonic-gate * return (0);
688*7c478bd9Sstevel@tonic-gate * }
689*7c478bd9Sstevel@tonic-gate *
690*7c478bd9Sstevel@tonic-gate * return (1);
691*7c478bd9Sstevel@tonic-gate * }
692*7c478bd9Sstevel@tonic-gate */
693