1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.5 2001/06/01 10:31:51 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $
6 * Purpose : zalloc, hash_string, safe_strerror, strcmpic,
7 * strncmpic, strsav, chomp, and MinGW32 strdup
9 * These are each too small to deserve their own file
10 * but don't really fit in any other file.
12 * Copyright : Written by and Copyright (C) 2001 the SourceForge
13 * IJBSWA team. http://ijbswa.sourceforge.net
15 * Based on the Internet Junkbuster originally written
16 * by and Copyright (C) 1997 Anonymous Coders and
17 * Junkbusters Corporation. http://www.junkbusters.com
19 * This program is free software; you can redistribute it
20 * and/or modify it under the terms of the GNU General
21 * Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at
23 * your option) any later version.
25 * This program is distributed in the hope that it will
26 * be useful, but WITHOUT ANY WARRANTY; without even the
27 * implied warranty of MERCHANTABILITY or FITNESS FOR A
28 * PARTICULAR PURPOSE. See the GNU General Public
29 * License for more details.
31 * The GNU General Public License should be included with
32 * this file. If not, you can view it at
33 * http://www.gnu.org/copyleft/gpl.html
34 * or write to the Free Software Foundation, Inc., 59
35 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * $Log: miscutil.c,v $
39 * Revision 1.5 2001/06/01 10:31:51 oes
40 * Added character class matching to trivimatch; renamed to simplematch
42 * Revision 1.4 2001/05/31 17:32:31 oes
44 * - Enhanced domain part globbing with infix and prefix asterisk
45 * matching and optional unanchored operation
47 * Revision 1.3 2001/05/29 23:10:09 oes
50 * - Introduced chomp()
51 * - Moved strsav() from showargs to miscutil
53 * Revision 1.2 2001/05/29 09:50:24 jongfoster
54 * Unified blocklist/imagelist/permissionslist.
55 * File format is still under discussion, but the internal changes
58 * Also modified interceptor behaviour:
59 * - We now intercept all URLs beginning with one of the following
60 * prefixes (and *only* these prefixes):
62 * * http://ijbswa.sf.net/config/
63 * * http://ijbswa.sourceforge.net/config/
64 * - New interceptors "home page" - go to http://i.j.b/ to see it.
65 * - Internal changes so that intercepted and fast redirect pages
66 * are not replaced with an image.
67 * - Interceptors now have the option to send a binary page direct
68 * to the client. (i.e. ijb-send-banner uses this)
69 * - Implemented show-url-info interceptor. (Which is why I needed
70 * the above interceptors changes - a typical URL is
71 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
72 * The previous mechanism would not have intercepted that, and
73 * if it had been intercepted then it then it would have replaced
76 * Revision 1.1.1.1 2001/05/15 13:59:00 oes
77 * Initial import of version 2.9.3 source tree
80 *********************************************************************/
94 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
96 /* Fix a problem with Solaris. There should be no effect on other
98 * Solaris's isspace() is a macro which uses it's argument directly
99 * as an array index. Therefore we need to make sure that high-bit
100 * characters generate +ve values, and ideally we also want to make
101 * the argument match the declared parameter type of "int".
103 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
104 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
106 /*********************************************************************
110 * Description : Malloc some memory and set it to '\0'.
111 * The way calloc() ought to be -acjc
114 * 1 : size = Size of memory chunk to return.
116 * Returns : Pointer to newly malloc'd memory chunk.
118 *********************************************************************/
119 void *zalloc(int size)
123 if ((ret = (void *)malloc(size)) != NULL)
125 memset(ret, 0, size);
132 /*********************************************************************
134 * Function : hash_string
136 * Description : Take a string and compute a (hopefuly) unique numeric
137 * integer value. This has several uses, but being able
138 * to "switch" a string the one of my favorites.
141 * 1 : s : string to be hashed.
143 * Returns : an unsigned long variable with the hashed value.
145 *********************************************************************/
146 unsigned long hash_string( const char* s )
148 unsigned long h = 0ul;
161 /*********************************************************************
165 * Description : For some reason (which is beyond me), gcc and WIN32
166 * don't like strdup. When a "free" is executed on a
167 * strdup'd ptr, it can at times freez up! So I just
168 * replaced it and problem was solved.
171 * 1 : s = string to duplicate
173 * Returns : Pointer to newly malloc'ed copy of the string.
175 *********************************************************************/
176 char *strdup( const char *s )
178 char * result = (char *)malloc( strlen(s)+1 );
188 #endif /* def __MINGW32__ */
192 /*********************************************************************
194 * Function : safe_strerror
196 * Description : Variant of the library routine strerror() which will
197 * work on systems without the library routine, and
198 * which should never return NULL.
201 * 1 : err = the `errno' of the last operation.
203 * Returns : An "English" string of the last `errno'. Allocated
204 * with strdup(), so caller frees. May be NULL if the
205 * system is out of memory.
207 *********************************************************************/
208 char *safe_strerror(int err)
216 #endif /* HAVE_STRERROR */
220 sprintf(buf, "(errno = %d)", err);
229 /*********************************************************************
231 * Function : strcmpic
233 * Description : Case insensitive string comparison
236 * 1 : s1 = string 1 to compare
237 * 2 : s2 = string 2 to compare
239 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
241 *********************************************************************/
242 int strcmpic(const char *s1, const char *s2)
246 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
252 return(ijb_tolower(*s1) - ijb_tolower(*s2));
257 /*********************************************************************
259 * Function : strncmpic
261 * Description : Case insensitive string comparison (upto n characters)
264 * 1 : s1 = string 1 to compare
265 * 2 : s2 = string 2 to compare
266 * 3 : n = maximum characters to compare
268 * Returns : 0 if s1==s2, Negative if s1<s2, Positive if s1>s2
270 *********************************************************************/
271 int strncmpic(const char *s1, const char *s2, size_t n)
273 if (n <= 0) return(0);
277 if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
286 return(ijb_tolower(*s1) - ijb_tolower(*s2));
291 /*********************************************************************
295 * Description : In-situ-eliminate all leading and trailing whitespace
299 * 1 : s : string to be chomped.
301 * Returns : chomped string
303 *********************************************************************/
304 char *chomp(char *string)
309 * strip trailing whitespace
311 p = string + strlen(string);
312 while (p > string && ijb_isspace(*(p-1)))
319 * find end of leading whitespace
322 while (*q && ijb_isspace(*q))
328 * if there was any, move the rest forwards
342 /*********************************************************************
346 * Description : Reallocate "old" and append text to it. This makes
347 * it easier to append to malloc'd strings.
350 * 1 : old = Old text that is to be extended. Will be
351 * free()d by this routine.
352 * 2 : text_to_append = Text to be appended to old.
354 * Returns : Pointer to newly malloc'ed appended string.
355 * If there is no text to append, return old. Caller
358 *********************************************************************/
359 char *strsav(char *old, const char *text_to_append)
361 int old_len, new_len;
364 if (( text_to_append == NULL) || (*text_to_append == '\0'))
371 old_len = strlen(old);
378 new_len = old_len + strlen(text_to_append) + 1;
382 if ((p = realloc(old, new_len)) == NULL)
384 log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
385 /* Never get here - LOG_LEVEL_FATAL causes program exit */
390 if ((p = (char *)malloc(new_len)) == NULL)
392 log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
393 /* Never get here - LOG_LEVEL_FATAL causes program exit */
397 strcpy(p + old_len, text_to_append);
403 /*********************************************************************
405 * Function : simplematch
407 * Description : String matching, with a (greedy) '*' wildcard that
408 * stands for zero or more arbitrary characters and
409 * character classes in [], which take both enumerations
413 * 1 : pattern = pattern for matching
414 * 2 : text = text to be matched
416 * Returns : 0 if match, else nonzero
418 *********************************************************************/
419 int simplematch(char *pattern, char *text)
428 unsigned char charmap[32];
434 /* EOF pattern but !EOF text? */
440 /* '*' in the pattern? */
444 /* The pattern ends afterwards? Speed up the return. */
450 /* Else, set wildcard mode and remember position after '*' */
455 /* Character range specification? */
458 memset(charmap, '\0', sizeof(charmap));
460 while (*++pat != ']')
466 else if (*pat == '-')
468 if ((*++pat == ']') || *pat == '\0')
472 for(i = lastchar; i <= *pat; i++)
474 charmap[i / 8] |= (1 << (i % 8));
479 charmap[*pat / 8] |= (1 << (*pat % 8));
483 } /* -END- if Character range specification */
486 /* Compare: Char match, or char range match*/
488 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
490 /* Sucess, go ahead */
495 /* In wildcard mode, just try again after failiure */
510 /* Cut off extra '*'s */
511 if(*pat == '*') pat++;
513 /* If this is the pattern's end, fine! */