[daemon-fix] Fixed sending daemon match rules for kdbus broadcasts
[platform/upstream/dbus.git] / dbus / dbus-sha.c
index 2f73e36..febfba2 100644 (file)
@@ -1,10 +1,10 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-sha.c SHA-1 implementation
  *
  * Copyright (C) 2003 Red Hat Inc.
  * Copyright (C) 1995 A. M. Kuchling
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
 #include "dbus-internals.h"
-#include "dbus-marshal.h"
 #include "dbus-sha.h"
+#include "dbus-marshal-basic.h" /* for byteswap routines */
 #include <string.h>
 
 /* The following comments have the history of where this code
@@ -157,7 +158,7 @@ effort (for example the reengineering of a great many Capstone chips).
 #define subRound(a, b, c, d, e, f, k, data) \
    ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )
 
-#endif /* DOXYGEN_SHOULD_SKIP_THIS */
+#endif /* !DOXYGEN_SHOULD_SKIP_THIS */
 
 /* Perform the SHA transformation.  Note that this code, like MD5, seems to
    break some optimizing compilers due to the complexity of the expressions
@@ -432,9 +433,9 @@ _dbus_sha_update (DBusSHAContext   *context,
                   const DBusString *data)
 {
   unsigned int inputLen;
-  unsigned char *input;
+  const unsigned char *input;
 
-  _dbus_string_get_const_data (data, (const char**) &input);
+  input = (const unsigned char*) _dbus_string_get_const_data (data);
   inputLen = _dbus_string_get_length (data);
 
   sha_append (context, input, inputLen);
@@ -465,7 +466,7 @@ _dbus_sha_final (DBusSHAContext   *context,
   /* some kind of security paranoia, though it seems pointless
    * to me given the nonzeroed stuff flying around
    */
-  memset ((void*)context, '\0', sizeof (DBusSHAContext));
+  _DBUS_ZERO(*context);
 
   return TRUE;
 }
@@ -489,7 +490,7 @@ _dbus_sha_compute (const DBusString *data,
 
   _dbus_sha_update (&context, data);
 
-  if (!_dbus_string_init (&digest, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&digest))
     return FALSE;
 
   if (!_dbus_sha_final (&context, &digest))
@@ -510,7 +511,7 @@ _dbus_sha_compute (const DBusString *data,
 
 /** @} */ /* end of exported functions */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-test.h"
 #include <stdio.h>
 
@@ -526,7 +527,7 @@ check_sha_binary (const unsigned char *input,
   _dbus_string_init_const_len (&input_str, input, input_len);
   _dbus_string_init_const (&expected_str, expected);
 
-  if (!_dbus_string_init (&results, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&results))
     _dbus_assert_not_reached ("no memory for SHA-1 results");
 
   if (!_dbus_sha_compute (&input_str, &results))
@@ -534,10 +535,9 @@ check_sha_binary (const unsigned char *input,
 
   if (!_dbus_string_equal (&expected_str, &results))
     {
-      const char *s;
-      _dbus_string_get_const_data (&results, &s);
       _dbus_warn ("Expected hash %s got %s for SHA-1 sum\n",
-                  expected, s);
+                  expected,
+                  _dbus_string_get_const_data (&results));
       _dbus_string_free (&results);
       return FALSE;
     }
@@ -569,10 +569,8 @@ decode_compact_string (const DBusString *line,
 
   if (!_dbus_string_parse_int (line, offset, &val, &next))
     {
-      const char *s;
-      _dbus_string_get_const_data (line, &s);
       fprintf (stderr, "could not parse length at start of compact string: %s\n",
-               s);
+               _dbus_string_get_const_data (line));
       return FALSE;
     }
 
@@ -581,10 +579,8 @@ decode_compact_string (const DBusString *line,
   offset = next;
   if (!_dbus_string_parse_int (line, offset, &val, &next))
     {
-      const char *s;
-      _dbus_string_get_const_data (line, &s);
       fprintf (stderr, "could not parse start bit 'b' in compact string: %s\n",
-               s);
+               _dbus_string_get_const_data (line));
       return FALSE;
     }
   
@@ -668,7 +664,7 @@ get_next_expected_result (DBusString *results,
 
   retval = FALSE;
   
-  if (!_dbus_string_init (&line, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&line))
     _dbus_assert_not_reached ("no memory");
   
  next_iteration:
@@ -682,8 +678,6 @@ get_next_expected_result (DBusString *results,
         goto next_iteration;
       else if (_dbus_string_starts_with_c_str (&line, "H>"))
         {
-          const char *s;          
-          _dbus_string_get_const_data (&line, &s);
           /* don't print */
         }
       else if (_dbus_string_starts_with_c_str (&line, "D>") ||
@@ -752,22 +746,23 @@ process_test_data (const char *test_data_dir)
   int line_no;
   dbus_bool_t retval;
   int success_count;
-  
+  DBusError error = DBUS_ERROR_INIT;
+
   retval = FALSE;
   
-  if (!_dbus_string_init (&tests_file, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&tests_file))
     _dbus_assert_not_reached ("no memory");
 
-  if (!_dbus_string_init (&results_file, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&results_file))
     _dbus_assert_not_reached ("no memory");
 
-  if (!_dbus_string_init (&tests, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&tests))
     _dbus_assert_not_reached ("no memory");
 
-  if (!_dbus_string_init (&results, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&results))
     _dbus_assert_not_reached ("no memory");
 
-  if (!_dbus_string_init (&line, _DBUS_INT_MAX))
+  if (!_dbus_string_init (&line))
     _dbus_assert_not_reached ("no memory");
   
   if (!_dbus_string_append (&tests_file, test_data_dir))
@@ -784,21 +779,20 @@ process_test_data (const char *test_data_dir)
   if (!_dbus_concat_dir_and_file (&results_file, &tmp))
     _dbus_assert_not_reached ("no memory");
 
-  if (_dbus_file_get_contents (&tests, &tests_file) != DBUS_RESULT_SUCCESS)
+  if (!_dbus_file_get_contents (&tests, &tests_file, &error))
     {
-      const char *s;
-      _dbus_string_get_const_data (&tests_file, &s);
-      fprintf (stderr, "could not load test data file %s\n",
-               s);
+      fprintf (stderr, "could not load test data file %s: %s\n",
+               _dbus_string_get_const_data (&tests_file),
+               error.message);
+      dbus_error_free (&error);
       goto out;
     }
 
-  if (_dbus_file_get_contents (&results, &results_file) != DBUS_RESULT_SUCCESS)
+  if (!_dbus_file_get_contents (&results, &results_file, &error))
     {
-      const char *s;
-      _dbus_string_get_const_data (&results_file, &s);
-      fprintf (stderr, "could not load results data file %s\n",
-               s);
+      fprintf (stderr, "could not load results data file %s: %s\n",
+               _dbus_string_get_const_data (&results_file), error.message);
+      dbus_error_free (&error);
       goto out;
     }
 
@@ -817,9 +811,7 @@ process_test_data (const char *test_data_dir)
         goto next_iteration;
       else if (_dbus_string_starts_with_c_str (&line, "H>"))
         {
-          const char *s;          
-          _dbus_string_get_const_data (&line, &s);
-          printf ("SHA-1: %s\n", s);
+          printf ("SHA-1: %s\n", _dbus_string_get_const_data (&line));
 
           if (_dbus_string_find (&line, 0, "Type 3", NULL))
             {
@@ -846,16 +838,16 @@ process_test_data (const char *test_data_dir)
 
           success = FALSE;
           
-          if (!_dbus_string_init (&next_line, _DBUS_INT_MAX))
+          if (!_dbus_string_init (&next_line))
             _dbus_assert_not_reached ("no memory");
 
-          if (!_dbus_string_init (&expected, _DBUS_INT_MAX))
+          if (!_dbus_string_init (&expected))
             _dbus_assert_not_reached ("no memory");
           
-          if (!_dbus_string_init (&test, _DBUS_INT_MAX))
+          if (!_dbus_string_init (&test))
             _dbus_assert_not_reached ("no memory");
 
-          if (!_dbus_string_init (&result, _DBUS_INT_MAX))
+          if (!_dbus_string_init (&result))
             _dbus_assert_not_reached ("no memory");
 
           /* the "compact strings" are "^"-terminated not
@@ -888,22 +880,15 @@ process_test_data (const char *test_data_dir)
             }
           
           if (!_dbus_string_equal (&result, &expected))
-            {
-              const char *s1;
-              const char *s2;
-
-              _dbus_string_get_const_data (&result, &s1);
-              _dbus_string_get_const_data (&expected, &s2);
-              
+            {              
               fprintf (stderr, " for line %d got hash %s expected %s\n",
-                       line_no, s1, s2);
+                       line_no,
+                       _dbus_string_get_const_data (&result),
+                       _dbus_string_get_const_data (&expected));
               goto failure;
             }
           else
             {
-              const char *s;
-              _dbus_string_get_const_data (&result, &s);
-              /* printf (" Got expected: %s\n", s); */
               success_count += 1;
             }
 
@@ -981,4 +966,4 @@ _dbus_sha_test (const char *test_data_dir)
   return TRUE;
 }
 
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */