-/* -*- 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
#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
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);
/* 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;
}
_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))
/** @} */ /* end of exported functions */
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
#include "dbus-test.h"
#include <stdio.h>
_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))
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;
}
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;
}
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;
}
retval = FALSE;
- if (!_dbus_string_init (&line, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&line))
_dbus_assert_not_reached ("no memory");
next_iteration:
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>") ||
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))
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;
}
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))
{
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
}
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;
}
return TRUE;
}
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */