/** * Fills n_bytes of the given buffer with random bytes. * * @param buffer an allocated buffer * @param n_bytes the number of bytes in buffer to write to */ void _dbus_generate_random_bytes_buffer (char *buffer, int n_bytes) { DBusString str; if (!_dbus_string_init (&str)) { _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes); return; } if (!_dbus_generate_random_bytes (&str, n_bytes)) { _dbus_string_free (&str); _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes); return; } _dbus_string_copy_to_buffer (&str, buffer, n_bytes); _dbus_string_free (&str); }
/** * Fills n_bytes of the given buffer with random bytes. * * @param buffer an allocated buffer * @param n_bytes the number of bytes in buffer to write to * @param error location to store reason for failure * @returns #TRUE on success */ dbus_bool_t _dbus_generate_random_bytes_buffer (char *buffer, int n_bytes, DBusError *error) { DBusString str; if (!_dbus_string_init (&str)) { _DBUS_SET_OOM (error); return FALSE; } if (!_dbus_generate_random_bytes (&str, n_bytes, error)) { _dbus_string_free (&str); return FALSE; } _dbus_string_copy_to_buffer (&str, buffer, n_bytes); _dbus_string_free (&str); return TRUE; }
static dbus_bool_t _dbus_read_uuid_file_without_creating (const DBusString *filename, DBusGUID *uuid, DBusError *error) { DBusString contents; DBusString decoded; int end; if (!_dbus_string_init (&contents)) { _DBUS_SET_OOM (error); return FALSE; } if (!_dbus_string_init (&decoded)) { _dbus_string_free (&contents); _DBUS_SET_OOM (error); return FALSE; } if (!_dbus_file_get_contents (&contents, filename, error)) goto error; _dbus_string_chop_white (&contents); if (_dbus_string_get_length (&contents) != DBUS_UUID_LENGTH_HEX) { dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT, "UUID file '%s' should contain a hex string of length %d, not length %d, with no other text", _dbus_string_get_const_data (filename), DBUS_UUID_LENGTH_HEX, _dbus_string_get_length (&contents)); goto error; } if (!_dbus_string_hex_decode (&contents, 0, &end, &decoded, 0)) { _DBUS_SET_OOM (error); goto error; } if (end == 0) { dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT, "UUID file '%s' contains invalid hex data", _dbus_string_get_const_data (filename)); goto error; } if (_dbus_string_get_length (&decoded) != DBUS_UUID_LENGTH_BYTES) { dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT, "UUID file '%s' contains %d bytes of hex-encoded data instead of %d", _dbus_string_get_const_data (filename), _dbus_string_get_length (&decoded), DBUS_UUID_LENGTH_BYTES); goto error; } _dbus_string_copy_to_buffer (&decoded, uuid->as_bytes, DBUS_UUID_LENGTH_BYTES); _dbus_string_free (&decoded); _dbus_string_free (&contents); _DBUS_ASSERT_ERROR_IS_CLEAR (error); return TRUE; error: _DBUS_ASSERT_ERROR_IS_SET (error); _dbus_string_free (&contents); _dbus_string_free (&decoded); return FALSE; }