- Moved strsav() from showargs to miscutil
[privoxy.git] / miscutil.c
1 const char miscutil_rcs[] = "$Id: miscutil.c,v 1.2 2001/05/29 09:50:24 jongfoster 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.2  2001/05/29 09:50:24  jongfoster
40  *    Unified blocklist/imagelist/permissionslist.
41  *    File format is still under discussion, but the internal changes
42  *    are (mostly) done.
43  *
44  *    Also modified interceptor behaviour:
45  *    - We now intercept all URLs beginning with one of the following
46  *      prefixes (and *only* these prefixes):
47  *        * http://i.j.b/
48  *        * http://ijbswa.sf.net/config/
49  *        * http://ijbswa.sourceforge.net/config/
50  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
51  *    - Internal changes so that intercepted and fast redirect pages
52  *      are not replaced with an image.
53  *    - Interceptors now have the option to send a binary page direct
54  *      to the client. (i.e. ijb-send-banner uses this)
55  *    - Implemented show-url-info interceptor.  (Which is why I needed
56  *      the above interceptors changes - a typical URL is
57  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
58  *      The previous mechanism would not have intercepted that, and
59  *      if it had been intercepted then it then it would have replaced
60  *      it with an image.)
61  *
62  *    Revision 1.1.1.1  2001/05/15 13:59:00  oes
63  *    Initial import of version 2.9.3 source tree
64  *
65  *
66  *********************************************************************/
67 \f
68
69 #include "config.h"
70
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <malloc.h>
75 #include <ctype.h>
76
77 #include "miscutil.h"
78 #include "errlog.h"
79
80 const char miscutil_h_rcs[] = MISCUTIL_H_VERSION;
81
82 /* Fix a problem with Solaris.  There should be no effect on other
83  * platforms.
84  * Solaris's isspace() is a macro which uses it's argument directly
85  * as an array index.  Therefore we need to make sure that high-bit
86  * characters generate +ve values, and ideally we also want to make
87  * the argument match the declared parameter type of "int".
88  */
89 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
90 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))   
91
92 /*********************************************************************
93  *
94  * Function    :  zalloc
95  *
96  * Description :  Malloc some memory and set it to '\0'.
97  *                The way calloc() ought to be -acjc
98  *
99  * Parameters  :
100  *          1  :  size = Size of memory chunk to return.
101  *
102  * Returns     :  Pointer to newly malloc'd memory chunk.
103  *
104  *********************************************************************/
105 void *zalloc(int size)
106 {
107    void * ret;
108
109    if ((ret = (void *)malloc(size)) != NULL)
110    {
111       memset(ret, 0, size);
112    }
113
114    return(ret);
115 }
116
117
118 /*********************************************************************
119  *
120  * Function    :  hash_string
121  *
122  * Description :  Take a string and compute a (hopefuly) unique numeric
123  *                integer value.  This has several uses, but being able
124  *                to "switch" a string the one of my favorites.
125  *
126  * Parameters  :
127  *          1  :  s : string to be hashed.
128  *
129  * Returns     :  an unsigned long variable with the hashed value.
130  *
131  *********************************************************************/
132 unsigned long hash_string( const char* s )
133 {
134    unsigned long h = 0ul; 
135
136    for ( ; *s; ++s )
137    {
138       h = 5 * h + *s;
139    }
140
141    return (h);
142
143 }
144
145
146 #ifdef __MINGW32__
147 /*********************************************************************
148  *
149  * Function    :  strdup
150  *
151  * Description :  For some reason (which is beyond me), gcc and WIN32
152  *                don't like strdup.  When a "free" is executed on a
153  *                strdup'd ptr, it can at times freez up!  So I just
154  *                replaced it and problem was solved.
155  *
156  * Parameters  :
157  *          1  :  s = string to duplicate
158  *
159  * Returns     :  Pointer to newly malloc'ed copy of the string.
160  *
161  *********************************************************************/
162 char *strdup( const char *s )
163 {
164    char * result = (char *)malloc( strlen(s)+1 );
165
166    if (result != NULL)
167    {
168       strcpy( result, s );
169    }
170
171    return( result );
172 }
173
174 #endif /* def __MINGW32__ */
175
176
177
178 /*********************************************************************
179  *
180  * Function    :  safe_strerror
181  *
182  * Description :  Variant of the library routine strerror() which will
183  *                work on systems without the library routine, and
184  *                which should never return NULL.
185  *
186  * Parameters  :
187  *          1  :  err = the `errno' of the last operation.
188  *
189  * Returns     :  An "English" string of the last `errno'.  Allocated
190  *                with strdup(), so caller frees.  May be NULL if the
191  *                system is out of memory.
192  *
193  *********************************************************************/
194 char *safe_strerror(int err)
195 {
196    char *s = NULL;
197    char buf[BUFSIZ];
198
199
200 #ifndef NOSTRERROR
201    s = strerror(err);
202 #endif /* NOSTRERROR */
203
204    if (s == NULL)
205    {
206       sprintf(buf, "(errno = %d)", err);
207       s = buf;
208    }
209
210    return(strdup(s));
211
212 }
213
214
215 /*********************************************************************
216  *
217  * Function    :  strcmpic
218  *
219  * Description :  Case insensitive string comparison
220  *
221  * Parameters  :
222  *          1  :  s1 = string 1 to compare
223  *          2  :  s2 = string 2 to compare
224  *
225  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
226  *
227  *********************************************************************/
228 int strcmpic(const char *s1, const char *s2)
229 {
230    while (*s1 && *s2)
231    {
232       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
233       {
234          break;
235       }
236       s1++, s2++;
237    }
238    return(ijb_tolower(*s1) - ijb_tolower(*s2));
239
240 }
241
242
243 /*********************************************************************
244  *
245  * Function    :  strncmpic
246  *
247  * Description :  Case insensitive string comparison (upto n characters)
248  *
249  * Parameters  :
250  *          1  :  s1 = string 1 to compare
251  *          2  :  s2 = string 2 to compare
252  *          3  :  n = maximum characters to compare
253  *
254  * Returns     :  0 if s1==s2, Negative if s1<s2, Positive if s1>s2
255  *
256  *********************************************************************/
257 int strncmpic(const char *s1, const char *s2, size_t n)
258 {
259    if (n <= 0) return(0);
260
261    while (*s1 && *s2)
262    {
263       if ( ( *s1 != *s2 ) && ( ijb_tolower(*s1) != ijb_tolower(*s2) ) )
264       {
265          break;
266       }
267
268       if (--n <= 0) break;
269
270       s1++, s2++;
271    }
272    return(ijb_tolower(*s1) - ijb_tolower(*s2));
273
274 }
275
276
277 /*********************************************************************
278  *
279  * Function    :  chomp
280  *
281  * Description :  In-situ-eliminate all leading and trailing whitespace
282  *                from a string.
283  *
284  * Parameters  :
285  *          1  :  s : string to be chomped.
286  *
287  * Returns     :  chomped string
288  *
289  *********************************************************************/
290 char *chomp(char *string)
291 {
292    char *p, *q, *r;
293
294    /* 
295     * strip trailing whitespace
296     */
297    p = string + strlen(string);
298    while (p > string && ijb_isspace(*(p-1)))
299    {
300       p--;
301    }
302    *p = '\0';
303
304    /* 
305     * find end of leading whitespace 
306     */
307    q = r = string;
308    while (*q && ijb_isspace(*q))
309    {
310       q++;
311    }
312
313    /*
314     * if there was any, move the rest forwards
315     */
316    if (q != string)
317    {
318       while (q <= p)
319       {
320          *r++ = *q++;
321       }
322    }
323
324    return(string);
325
326 }
327
328 /*********************************************************************
329  *
330  * Function    :  strsav
331  *
332  * Description :  Reallocate "old" and append text to it.  This makes
333  *                it easier to append to malloc'd strings.
334  *
335  * Parameters  :
336  *          1  :  old = Old text that is to be extended.  Will be
337  *                free()d by this routine.
338  *          2  :  text_to_append = Text to be appended to old.
339  *
340  * Returns     :  Pointer to newly malloc'ed appended string.
341  *                If there is no text to append, return old.  Caller
342  *                must free().
343  *
344  *********************************************************************/
345 char *strsav(char *old, const char *text_to_append)
346 {
347    int old_len, new_len;
348    char *p;
349
350    if (( text_to_append == NULL) || (*text_to_append == '\0'))
351    {
352       return(old);
353    }
354
355    if (NULL != old)
356    {
357       old_len = strlen(old);
358    }
359    else
360    {
361       old_len = 0;
362    }
363
364    new_len = old_len + strlen(text_to_append) + 1;
365
366    if (old)
367    {
368       if ((p = realloc(old, new_len)) == NULL)
369       {
370          log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes failed!", new_len);
371          /* Never get here - LOG_LEVEL_FATAL causes program exit */
372       }
373    }
374    else
375    {
376       if ((p = (char *)malloc(new_len)) == NULL)
377       {
378          log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes failed!", new_len);
379          /* Never get here - LOG_LEVEL_FATAL causes program exit */
380       }
381    }
382
383    strcpy(p + old_len, text_to_append);
384    return(p);
385
386 }
387
388 /*
389   Local Variables:
390   tab-width: 3
391   end:
392 */