D-Bus  1.12.2
dbus-marshal-byteswap-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-byteswap-util.c Would be in dbus-marshal-byteswap.c but tests/bus only
3  *
4  * Copyright (C) 2005 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 
26 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
27 #include "dbus-marshal-byteswap.h"
28 #include "dbus-test.h"
29 #include <stdio.h>
30 
31 static void
32 do_byteswap_test (int byte_order)
33 {
34  int sequence;
35  DBusString signature;
36  DBusString body;
37  int opposite_order;
38 
39  if (!_dbus_string_init (&signature) || !_dbus_string_init (&body))
41 
42  opposite_order = byte_order == DBUS_LITTLE_ENDIAN ? DBUS_BIG_ENDIAN : DBUS_LITTLE_ENDIAN;
43 
44  sequence = 0;
45  while (dbus_internal_do_not_use_generate_bodies (sequence,
46  byte_order,
47  &signature, &body))
48  {
49  DBusString copy;
50  DBusTypeReader body_reader;
51  DBusTypeReader copy_reader;
52 
53  if (!_dbus_string_init (&copy))
55 
56  if (!_dbus_string_copy (&body, 0, &copy, 0))
58 
59  _dbus_marshal_byteswap (&signature, 0,
60  byte_order,
61  opposite_order,
62  &copy, 0);
63 
64  _dbus_type_reader_init (&body_reader, byte_order, &signature, 0,
65  &body, 0);
66  _dbus_type_reader_init (&copy_reader, opposite_order, &signature, 0,
67  &copy, 0);
68 
69  if (!_dbus_type_reader_equal_values (&body_reader, &copy_reader))
70  {
71  _dbus_verbose_bytes_of_string (&signature, 0,
72  _dbus_string_get_length (&signature));
74  _dbus_string_get_length (&body));
76  _dbus_string_get_length (&copy));
77 
78  _dbus_warn ("Byte-swapped data did not have same values as original data");
79  _dbus_assert_not_reached ("test failed");
80  }
81 
82  _dbus_string_free (&copy);
83 
84  _dbus_string_set_length (&signature, 0);
85  _dbus_string_set_length (&body, 0);
86  ++sequence;
87  }
88 
89  _dbus_string_free (&signature);
90  _dbus_string_free (&body);
91 
92  printf (" %d blocks swapped from order '%c' to '%c'\n",
93  sequence, byte_order, opposite_order);
94 }
95 
97 _dbus_marshal_byteswap_test (void)
98 {
99  do_byteswap_test (DBUS_LITTLE_ENDIAN);
100  do_byteswap_test (DBUS_BIG_ENDIAN);
101 
102  return TRUE;
103 }
104 
105 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1283
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define DBUS_BIG_ENDIAN
Code marking MSB-first byte order in the wire protocol.
Definition: dbus-protocol.h:54
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
The type reader is an iterator for reading values from a block of values.
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
void _dbus_type_reader_init(DBusTypeReader *reader, int byte_order, const DBusString *type_str, int type_pos, const DBusString *value_str, int value_pos)
Initializes a type reader.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:802
DBUS_PRIVATE_EXPORT void _dbus_verbose_bytes_of_string(const DBusString *str, int start, int len)
Dump the given part of the string to verbose log.
void _dbus_marshal_byteswap(const DBusString *signature, int signature_start, int old_byte_order, int new_byte_order, DBusString *value_str, int value_pos)
Byteswaps the marshaled data in the given value_str.
#define DBUS_LITTLE_ENDIAN
Code marking LSB-first byte order in the wire protocol.
Definition: dbus-protocol.h:53