Rebuild with utf-8 encoding
[privoxy.git] / miscutil.c
index a002abb..f04344c 100644 (file)
@@ -1,4 +1,4 @@
-const char miscutil_rcs[] = "$Id: miscutil.c,v 1.74 2012/03/09 16:23:50 fabiankeil Exp $";
+const char miscutil_rcs[] = "$Id: miscutil.c,v 1.77 2012/07/23 12:41:59 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/miscutil.c,v $
@@ -8,7 +8,7 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.74 2012/03/09 16:23:50 fabianke
  *                to deserve their own file but don't really fit in
  *                any other file.
  *
- * Copyright   :  Written by and Copyright (C) 2001-2011 the
+ * Copyright   :  Written by and Copyright (C) 2001-2012 the
  *                Privoxy team. http://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
@@ -129,6 +129,42 @@ char *strdup_or_die(const char *str)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  malloc_or_die
+ *
+ * Description :  malloc wrapper that either succeeds or causes
+ *                program termination.
+ *
+ *                Useful in situations were the buffer size is "small"
+ *                and malloc() failures couldn't be handled better
+ *                anyway. In case of debug builds, failures trigger
+ *                an assert().
+ *
+ * Parameters  :
+ *          1  :  buffer_size = Size of the space to allocate
+ *
+ * Returns     :  Pointer to newly malloc'd memory
+ *
+ *********************************************************************/
+void *malloc_or_die(size_t buffer_size)
+{
+   char *new_buf;
+
+   new_buf = malloc(buffer_size);
+
+   if (new_buf == NULL)
+   {
+      assert(new_buf != NULL);
+      log_error(LOG_LEVEL_FATAL, "Out of memory in malloc_or_die().");
+      exit(1);
+   }
+
+   return(new_buf);
+
+}
+
+
 #if defined(unix)
 /*********************************************************************
  *
@@ -213,7 +249,7 @@ int strcmpic(const char *s1, const char *s2)
 
    while (*s1 && *s2)
    {
-      if (( *s1 != *s2) && ( privoxy_tolower(*s1) != privoxy_tolower(*s2) ) )
+      if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
       {
          break;
       }
@@ -246,7 +282,7 @@ int strncmpic(const char *s1, const char *s2, size_t n)
 
    while (*s1 && *s2)
    {
-      if (( *s1 != *s2) && ( privoxy_tolower(*s1) != privoxy_tolower(*s2) ) )
+      if ((*s1 != *s2) && (privoxy_tolower(*s1) != privoxy_tolower(*s2)))
       {
          break;
       }
@@ -488,6 +524,31 @@ char *string_toupper(const char *string)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  string_move
+ *
+ * Description :  memmove wrapper to move the last part of a string
+ *                towards the beginning, overwriting the part in
+ *                the middle. strlcpy() can't be used here as the
+ *                strings overlap.
+ *
+ * Parameters  :
+ *          1  :  dst = Destination to overwrite
+ *          2  :  src = Source to move.
+ *
+ * Returns     :  N/A
+ *
+ *********************************************************************/
+void string_move(char *dst, char *src)
+{
+   assert(dst < src);
+
+   /* +1 to copy the terminating nul as well. */
+   memmove(dst, src, strlen(src)+1);
+}
+
+
 /*********************************************************************
  *
  * Function    :  bindup
@@ -586,7 +647,7 @@ char * make_path(const char * dir, const char * file)
 #else /* ifndef _WIN32 || __OS2__ */
       || (*file == '/') /* Absolute path (U*ix) */
 #endif /* ifndef _WIN32 || __OS2__  */
-     )
+      )
    {
       return strdup(file);
    }
@@ -1374,7 +1435,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
           tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
 #endif
         } else if (alternate_form) {
-          if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X'))
+          if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') )
             { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; }
          /* alternate form should have no effect for p conversion, but ... */
 #ifdef HPUX_COMPATIBLE
@@ -1385,7 +1446,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
          /* Actually it uses 0x prefix even for a zero value. */
                    && arg_sign != 0
 #endif
-                 ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
+                  ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; }
 #endif
         }
         zero_padding_insertion_ind = str_arg_l;
@@ -1397,7 +1458,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
           * converting a zero value with a precision of zero is a null string.
           * Actually HP returns all zeroes, and Linux returns "(nil)". */
 #endif
-       ) {
+        ) {
          /* converted to null string */
          /* When zero value is formatted with an explicit precision 0,
             the resulting formatted string is empty (d, i, u, o, x, X, p).   */
@@ -1437,7 +1498,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
           if (zero_padding_insertion_ind+1 < str_arg_l &&
               tmp[zero_padding_insertion_ind]   == '0' &&
              (tmp[zero_padding_insertion_ind+1] == 'x' ||
-              tmp[zero_padding_insertion_ind+1] == 'X')) {
+              tmp[zero_padding_insertion_ind+1] == 'X') ) {
             zero_padding_insertion_ind += 2;
           }
         }
@@ -1452,7 +1513,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
               && !(zero_padding_insertion_ind < str_arg_l
                    && tmp[zero_padding_insertion_ind] == '0')
 #endif
-         ) {        /* assure leading zero for alternate-form octal numbers */
+          ) {        /* assure leading zero for alternate-form octal numbers */
             if (!precision_specified || precision < num_of_digits+1) {
              /* precision is increased to force the first character to be zero,
                 except if a zero value is formatted with an explicit precision