GUI changes to rename "permissions file" to "actions file".
[privoxy.git] / miscutil.c
1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.3 2001/05/29 23:10:09 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/miscutil.c,v $
5  *
6  * Purpose     :  zalloc, hash_string, safe_strerror, strcmpic,
7  *                strncmpic, strsav, chomp, and MinGW32 strdup
8  *                functions. 
9  *                These are each too small to deserve their own file
10  *                but don't really fit in any other file.
11  *
12  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
13  *                IJBSWA team.  http://ijbswa.sourceforge.net
14  *
15  *                Based on the Internet Junkbuster originally written
16  *                by and Copyright (C) 1997 Anonymous Coders and 
17  *                Junkbusters Corporation.  http://www.junkbusters.com
18  *
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.
24  *
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.
30  *
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.
36  *
37  * Revisions   :
38  *    $Log: miscutil.c,v $
39  *    Revision 1.3  2001/05/29 23:10:09  oes
40  *
41  *
42  *     - Introduced chomp()
43  *     - Moved strsav() from showargs to miscutil
44  *
45  *    Revision 1.2  2001/05/29 09:50:24  jongfoster
46  *    Unified blocklist/imagelist/permissionslist.
47  *    File format is still under discussion, but the internal changes
48  *    are (mostly) done.
49  *
50  *    Also modified interceptor behaviour:
51  *    - We now intercept all URLs beginning with one of the following
52  *      prefixes (and *only* these prefixes):
53  *        * http://i.j.b/
54  *        * http://ijbswa.sf.net/config/
55  *        * http://ijbswa.sourceforge.net/config/
56  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
57  *    - Internal changes so that intercepted and fast redirect pages
58  *      are not replaced with an image.
59  *    - Interceptors now have the option to send a binary page direct
60  *      to the client. (i.e. ijb-send-banner uses this)
61  *    - Implemented show-url-info interceptor.  (Which is why I needed
62  *      the above interceptors changes - a typical URL is
63  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
64  *      The previous mechanism would not have intercepted that, and
65  *      if it had been intercepted then it then it would have replaced
66  *      it with an image.)
67  *
68  *    Revision 1.1.1.1  2001/05/15 13:59:00  oes
69  *    Initial import of version 2.9.3 source tree
70  *
71  *
72  *********************************************************************/
73 \f
74
75 #include "config.h"
76
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <malloc.h>
81 #include <ctype.h>
82
83 #include "miscutil.h"
84 #include "errlog.h"
85
86 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
87
88 /* Fix a problem with Solaris.  There should be no effect on other
89  * platforms.
90  * Solaris's isspace() is a macro which uses it's argument directly
91  * as an array index.  Therefore we need to make sure that high-bit
92  * characters generate +ve values, and ideally we also want to make
93  * the argument match the declared parameter type of "int".
94  */
95 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
96 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))   
97
98 /*********************************************************************
99  *
100  * Function    :  zalloc
101  *
102  * Description :  Malloc some memory and set it to '\0'.
103  *                The way calloc() ought to be -acjc
104  *
105  * Parameters  :
106  *          1  :  size = Size of memory chunk to return.
107  *
108  * Returns     :  Pointer to newly malloc'd memory chunk.
109  *
110  *********************************************************************/
111 void *zalloc(int size)
112 {
113    void * ret;
114
115    if ((ret = (void *)malloc(size)) != NULL)
116    {
117       memset(ret, 0, size);
118    }
119
120    return(ret);
121 }
122
123
124 /*********************************************************************
125  *
126  * Function    :  hash_string
127  *
128  * Description :  Take a string and compute a (hopefuly) unique numeric
129  *                integer value.  This has several uses, but being able
130  *                to "switch" a string the one of my favorites.
131  *
132  * Parameters  :
133  *          1  :  s : string to be hashed.
134  *
135  * Returns     :  an unsigned long variable with the hashed value.
136  *
137  *********************************************************************/
138 unsigned long hash_string( const char* s )
139 {
140    unsigned long h = 0ul; 
141
142    for ( ; *s; ++s )
143    {
144       h = 5 * h + *s;
145    }
146
147    return (h);
148
149 }
150
151
152 #ifdef __MINGW32__
153 /*********************************************************************
154  *
155  * Function    :  strdup
156  *
157  * Description :  For some reason (which is beyond me), gcc and WIN32
158  *                don't like strdup.  When a "free" is executed on a
159  *                strdup'd ptr, it can at times freez up!  So I just
160  *                replaced it and problem was solved.
161  *
162  * Parameters  :
163  *          1  :  s = string to duplicate
164  *
165  * Returns     :  Pointer to newly malloc'ed copy of the string.
166  *
167  *********************************************************************/
168 char *strdup( const char *s )
169 {
170    char * result = (char *)malloc( strlen(s)+1 );
171
172    if (result != NULL)
173    {
174       strcpy( result, s );
175    }
176
177    return( result );
178 }
179
180 #endif /* def __MINGW32__ */
181
182
183
184 /*********************************************************************
185  *
186  * Function    :  safe_strerror
187  *
188  * Description :  Variant of the library routine strerror() which will
189  *                work on systems without the library routine, and
190  *                which should never return NULL.
191  *
192  * Parameters  :
193  *          1  :  err = the `errno' of the last operation.
194  *
195  * Returns     :  An "English" string of the last `errno'.  Allocated
196  *                with strdup(), so caller frees.  May be NULL if the
197  *                system is out of memory.
198  *
199  *********************************************************************/
200 char *safe_strerror(int err)
201 {
202    char *s = NULL;
203    char buf[BUFSIZ];
204
205
206 #ifndef NOSTRERROR
207    s = strerror(err);
208 #endif /* NOSTRERROR */
209
210    if (s == NULL)
211    {
212       sprintf(buf, "(errno = %d)", err);
213       s = buf;
214    }
215
216    return(strdup(s));
217
218 }
219
220
221 /*********************************************************************
222  *
223  * Function    :  strcmpic
224  *
225  * Description :  Case insensitive string comparison
226  *
227  * Parameters  :
228  *          1  :  s1 = string 1 to compare
229  *          2  :  s2 = string 2 to compare
230  *
231  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
232  *
233  *********************************************************************/
234 int strcmpic(const char *s1, const char *s2)
235 {
236    while (*s1 && *s2)
237    {
238       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
239       {
240          break;
241       }
242       s1++, s2++;
243    }
244    return(ijb_tolower(*s1) - ijb_tolower(*s2));
245
246 }
247
248
249 /*********************************************************************
250  *
251  * Function    :  strncmpic
252  *
253  * Description :  Case insensitive string comparison (upto n characters)
254  *
255  * Parameters  :
256  *          1  :  s1 = string 1 to compare
257  *          2  :  s2 = string 2 to compare
258  *          3  :  n = maximum characters to compare
259  *
260  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
261  *
262  *********************************************************************/
263 int strncmpic(const char *s1, const char *s2, size_t n)
264 {
265    if (n <= 0) return(0);
266
267    while (*s1 && *s2)
268    {
269       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
270       {
271          break;
272       }
273
274       if (--n <= 0) break;
275
276       s1++, s2++;
277    }
278    return(ijb_tolower(*s1) - ijb_tolower(*s2));
279
280 }
281
282
283 /*********************************************************************
284  *
285  * Function    :  chomp
286  *
287  * Description :  In-situ-eliminate all leading and trailing whitespace
288  *                from a string.
289  *
290  * Parameters  :
291  *          1  :  s : string to be chomped.
292  *
293  * Returns     :  chomped string
294  *
295  *********************************************************************/
296 char *chomp(char *string)
297 {
298    char *p, *q, *r;
299
300    /* 
301     * strip trailing whitespace
302     */
303    p = string + strlen(string);
304    while (p > string && ijb_isspace(*(p-1)))
305    {
306       p--;
307    }
308    *p = '\0';
309
310    /* 
311     * find end of leading whitespace 
312     */
313    q = r = string;
314    while (*q && ijb_isspace(*q))
315    {
316       q++;
317    }
318
319    /*
320     * if there was any, move the rest forwards
321     */
322    if (q != string)
323    {
324       while (q <= p)
325       {
326          *r++ = *q++;
327       }
328    }
329
330    return(string);
331
332 }
333
334 /*********************************************************************
335  *
336  * Function    :  strsav
337  *
338  * Description :  Reallocate "old" and append text to it.  This makes
339  *                it easier to append to malloc'd strings.
340  *
341  * Parameters  :
342  *          1  :  old = Old text that is to be extended.  Will be
343  *                free()d by this routine.
344  *          2  :  text_to_append = Text to be appended to old.
345  *
346  * Returns     :  Pointer to newly malloc'ed appended string.
347  *                If there is no text to append, return old.  Caller
348  *                must free().
349  *
350  *********************************************************************/
351 char *strsav(char *old, const char *text_to_append)
352 {
353    int old_len, new_len;
354    char *p;
355
356    if (( text_to_append == NULL) || (*text_to_append == '\0'))
357    {
358       return(old);
359    }
360
361    if (NULL != old)
362    {
363       old_len = strlen(old);
364    }
365    else
366    {
367       old_len = 0;
368    }
369
370    new_len = old_len + strlen(text_to_append) + 1;
371
372    if (old)
373    {
374       if ((p = realloc(old, new_len)) == NULL)
375       {
376          log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
377          /* Never get here - LOG_LEVEL_FATAL causes program exit */
378       }
379    }
380    else
381    {
382       if ((p = (char *)malloc(new_len)) == NULL)
383       {
384          log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
385          /* Never get here - LOG_LEVEL_FATAL causes program exit */
386       }
387    }
388
389    strcpy(p + old_len, text_to_append);
390    return(p);
391
392 }
393
394
395 /*********************************************************************
396  *
397  * Function    :  trivimatch
398  *
399  * Description :  Trivial string matching, with only one metacharacter,
400  *                namely '*', which stands for zero or more arbitrary
401  *                characters.
402  *
403  *                Note: The * is greedy, i.e. it will try to match as
404  *                      much text es possible.
405  *
406  * Parameters  :
407  *          1  :  pattern = pattern for matching
408  *          2  :  text    = text to be matched
409  *
410  * Returns     :  0 if match, else nonzero
411  *
412  *********************************************************************/
413 int trivimatch(char *pattern, char *text)
414 {
415    char *fallback; 
416    char *pat = pattern;
417    char *txt = text;
418    int wildcard = 0;
419   
420    while (*txt)
421    {
422       /* EOF pattern but !EOF text? */
423       if (*pat == '\0')
424       {
425          return 1;
426       }
427
428       /* '*' in the pattern?  */
429       if (*pat == '*') 
430       {
431
432          /* The pattern ends afterwards? Speed up the return. */
433          if (*++pat == '\0')
434          {
435             return 0;
436          }
437
438          /* Else, set wildcard mode and remember position after '*' */
439          wildcard = 1;
440          fallback = pat;
441       }
442
443       /* Compare: */
444       if (*pat != *txt)
445       {
446          /* In wildcard mode, just try again */
447          if(wildcard)
448          {
449             /* Without wildcard mode, this is fatal! */
450             pat = fallback;
451          }
452
453          /* Bad luck otherwise */
454          else
455          {
456             return 1;
457          }
458       }
459       /* We had a match, advance */
460       else
461       {
462          pat++;
463       }
464       txt++;
465   }
466
467   if(*pat == '*')  pat++;
468
469   /* Hey, we've made it all the way through! */
470   return(*pat);
471
472 }
473
474 /*
475   Local Variables:
476   tab-width: 3
477   end:
478 */