1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message-builder.c Build messages from text files for testing (internal to D-BUS implementation)
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifdef DBUS_BUILD_TESTS
27 #include "dbus-message-builder.h"
28 #include "dbus-hash.h"
29 #include "dbus-internals.h"
30 #include "dbus-marshal.h"
33 * @defgroup DBusMessageBuilder code for loading test message data
34 * @ingroup DBusInternals
35 * @brief code for loading up test data for unit tests
37 * The code in here is used for unit testing, it loads
38 * up message data from a description in a file.
48 DBusString name; /**< Name of the length */
49 int start; /**< Calculate length since here */
50 int length; /**< length to write */
51 int offset; /**< where to write it into the data */
52 int endian; /**< endianness to write with */
56 free_saved_length (void *data)
58 SavedLength *sl = data;
61 return; /* all hash free functions have to accept NULL */
63 _dbus_string_free (&sl->name);
68 ensure_saved_length (DBusHashTable *hash,
69 const DBusString *name)
74 s = _dbus_string_get_const_data (name);
76 sl = _dbus_hash_table_lookup_string (hash, s);
80 sl = dbus_new0 (SavedLength, 1);
82 if (!_dbus_string_init (&sl->name))
88 if (!_dbus_string_copy (name, 0, &sl->name, 0))
91 s = _dbus_string_get_const_data (&sl->name);
93 if (!_dbus_hash_table_insert_string (hash, (char*)s, sl))
104 free_saved_length (sl);
109 save_start (DBusHashTable *hash,
110 const DBusString *name,
115 sl = ensure_saved_length (hash, name);
119 else if (sl->start >= 0)
121 _dbus_warn ("Same START_LENGTH given twice\n");
131 save_length (DBusHashTable *hash,
132 const DBusString *name,
137 sl = ensure_saved_length (hash, name);
141 else if (sl->length >= 0)
143 _dbus_warn ("Same END_LENGTH given twice\n");
153 save_offset (DBusHashTable *hash,
154 const DBusString *name,
160 sl = ensure_saved_length (hash, name);
164 else if (sl->offset >= 0)
166 _dbus_warn ("Same LENGTH given twice\n");
178 /** Saves the segment to delete in order to unalign the next item */
179 #define SAVE_FOR_UNALIGN(str, boundary) \
180 int align_pad_start = _dbus_string_get_length (str); \
181 int align_pad_end = _DBUS_ALIGN_VALUE (align_pad_start, (boundary))
183 /** Deletes the alignment padding */
184 #define PERFORM_UNALIGN(str) \
187 _dbus_string_delete ((str), align_pad_start, \
188 align_pad_end - align_pad_start); \
194 append_quoted_string (DBusString *dest,
195 const DBusString *quoted,
199 dbus_bool_t in_quotes = FALSE;
202 /* FIXME: We might want to add escaping in case we want to put '
203 * characters in our strings.
207 while (i < _dbus_string_get_length (quoted))
211 b = _dbus_string_get_byte (quoted, i);
219 if (!_dbus_string_append_byte (dest, b))
227 else if (b == ' ' || b == '\n' || b == '\t')
228 break; /* end on whitespace if not quoted */
231 if (!_dbus_string_append_byte (dest, b))
242 if (!_dbus_string_append_byte (dest, '\0'))
248 append_saved_length (DBusString *dest,
249 DBusHashTable *length_hash,
250 const DBusString *name,
254 if (!save_offset (length_hash, name,
257 _dbus_warn ("failed to save offset to LENGTH\n");
261 if (!_dbus_marshal_uint32 (dest, endian,
264 _dbus_warn ("failed to append a length\n");
272 message_type_from_string (const DBusString *str,
277 s = _dbus_string_get_const_data_len (str, start,
278 _dbus_string_get_length (str) - start);
280 if (strncmp (s, "method_call", strlen ("method_call")) == 0)
281 return DBUS_MESSAGE_TYPE_METHOD_CALL;
282 else if (strncmp (s, "method_return", strlen ("method_return")) == 0)
283 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
284 else if (strncmp (s, "signal", strlen ("signal")) == 0)
285 return DBUS_MESSAGE_TYPE_SIGNAL;
286 else if (strncmp (s, "error", strlen ("error")) == 0)
287 return DBUS_MESSAGE_TYPE_ERROR;
288 else if (strncmp (s, "invalid", strlen ("invalid")) == 0)
289 return DBUS_MESSAGE_TYPE_INVALID;
295 append_string_field (DBusString *dest,
297 const char *field_name,
303 if (!_dbus_string_align_length (dest, 4))
305 _dbus_warn ("could not align field name\n");
309 if (!_dbus_string_append (dest, field_name))
311 _dbus_warn ("couldn't append field name\n");
315 if (!_dbus_string_append_byte (dest, type))
317 _dbus_warn ("could not append typecode byte\n");
321 len = strlen (value);
323 if (!_dbus_marshal_uint32 (dest, endian, len))
325 _dbus_warn ("couldn't append string length\n");
329 if (!_dbus_string_append (dest, value))
331 _dbus_warn ("couldn't append field value\n");
335 if (!_dbus_string_append_byte (dest, 0))
337 _dbus_warn ("couldn't append string nul term\n");
345 * Reads the given filename, which should be in "message description
346 * language" (look at some examples), and builds up the message data
347 * from it. The message data may be invalid, or valid.
349 * The parser isn't very strict, it's just a hack for test programs.
351 * The file format is:
353 * VALID_HEADER <type> normal header; byte order, type, padding, header len, body len, serial
354 * REQUIRED_FIELDS add required fields with placeholder values
355 * BIG_ENDIAN switch to big endian
356 * LITTLE_ENDIAN switch to little endian
357 * OPPOSITE_ENDIAN switch to opposite endian
358 * ALIGN <N> aligns to the given value
359 * UNALIGN skips alignment for the next marshal
360 * BYTE <N> inserts the given integer in [0,255] or char in 'a' format
361 * START_LENGTH <name> marks the start of a length to measure
362 * END_LENGTH <name> records the length since START_LENGTH under the given name
363 * (or if no START_LENGTH, absolute length)
364 * LENGTH <name> inserts the saved length of the same name
365 * CHOP <N> chops last N bytes off the data
366 * FIELD_NAME <abcd> inserts 4-byte field name
367 * TYPE <typename> inserts a typecode byte
370 * Following commands insert aligned data unless
371 * preceded by "UNALIGN":
373 * INT32 <N> marshals an INT32
374 * UINT32 <N> marshals a UINT32
375 * INT64 <N> marshals an INT64
376 * UINT64 <N> marshals a UINT64
377 * DOUBLE <N> marshals a double
378 * STRING 'Foo' marshals a string
379 * OBJECT_PATH '/foo/bar' marshals an object path
380 * BYTE_ARRAY { 'a', 3, 4, 5, 6} marshals a BYTE array
381 * BOOLEAN_ARRAY { false, true, false} marshals a BOOLEAN array
382 * INT32_ARRAY { 3, 4, 5, 6} marshals an INT32 array
383 * UINT32_ARRAY { 3, 4, 5, 6} marshals an UINT32 array
384 * DOUBLE_ARRAY { 1.0, 2.0, 3.0, 4.0} marshals a DOUBLE array
385 * STRING_ARRAY { "foo", "bar", "gazonk"} marshals a STRING array
388 * @todo add support for array types INT32_ARRAY { 3, 4, 5, 6 }
391 * @param dest the string to append the message data to
392 * @param filename the filename to load
393 * @returns #TRUE on success
396 _dbus_message_data_load (DBusString *dest,
397 const DBusString *filename)
405 DBusHashTable *length_hash;
412 if (!_dbus_string_init (&file))
415 if (!_dbus_string_init (&line))
417 _dbus_string_free (&file);
421 _dbus_verbose ("Loading %s\n", _dbus_string_get_const_data (filename));
423 dbus_error_init (&error);
424 if (!_dbus_file_get_contents (&file, filename, &error))
426 _dbus_warn ("Getting contents of %s failed: %s\n",
427 _dbus_string_get_const_data (filename), error.message);
428 dbus_error_free (&error);
432 length_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
435 if (length_hash == NULL)
438 endian = DBUS_COMPILER_BYTE_ORDER;
442 while (_dbus_string_pop_line (&file, &line))
444 dbus_bool_t just_set_unalign;
446 just_set_unalign = FALSE;
449 _dbus_string_delete_leading_blanks (&line);
451 if (_dbus_string_get_length (&line) == 0)
456 else if (_dbus_string_starts_with_c_str (&line,
459 /* Ignore this comment */
462 else if (_dbus_string_starts_with_c_str (&line,
469 if (_dbus_string_get_length (&line) < (int) strlen ("VALID_HEADER "))
471 _dbus_warn ("no args to VALID_HEADER\n");
475 if (!_dbus_string_append_byte (dest, endian))
477 _dbus_warn ("could not append endianness\n");
481 message_type = message_type_from_string (&line,
482 strlen ("VALID_HEADER "));
483 if (message_type < 0)
485 _dbus_warn ("VALID_HEADER not followed by space then known message type\n");
489 if (!_dbus_string_append_byte (dest, message_type))
491 _dbus_warn ("could not append message type\n");
498 if (!_dbus_string_append_byte (dest, '\0'))
500 _dbus_warn ("could not append nul pad\n");
506 _dbus_string_init_const (&name, "Header");
507 if (!append_saved_length (dest, length_hash,
508 &name, _dbus_string_get_length (dest),
512 _dbus_string_init_const (&name, "Body");
513 if (!append_saved_length (dest, length_hash,
514 &name, _dbus_string_get_length (dest),
519 if (!_dbus_marshal_int32 (dest, endian, 1))
521 _dbus_warn ("couldn't append client serial\n");
525 else if (_dbus_string_starts_with_c_str (&line,
528 if (!append_string_field (dest, endian,
529 DBUS_HEADER_FIELD_INTERFACE,
531 "org.freedesktop.BlahBlahInterface"))
533 if (!append_string_field (dest, endian,
534 DBUS_HEADER_FIELD_MEMBER,
538 if (!append_string_field (dest, endian,
539 DBUS_HEADER_FIELD_PATH,
540 DBUS_TYPE_OBJECT_PATH,
544 else if (_dbus_string_starts_with_c_str (&line,
547 endian = DBUS_BIG_ENDIAN;
549 else if (_dbus_string_starts_with_c_str (&line,
552 endian = DBUS_LITTLE_ENDIAN;
554 else if (_dbus_string_starts_with_c_str (&line,
557 if (endian == DBUS_BIG_ENDIAN)
558 endian = DBUS_LITTLE_ENDIAN;
560 endian = DBUS_BIG_ENDIAN;
562 else if (_dbus_string_starts_with_c_str (&line,
569 _dbus_string_delete_first_word (&line);
571 if (!_dbus_string_parse_int (&line, 0, &val, &end))
573 _dbus_warn ("Failed to parse integer\n");
579 _dbus_warn ("Aligning to %ld boundary is crack\n",
584 orig_len = _dbus_string_get_length (dest);
586 if (!_dbus_string_align_length (dest, val))
589 if (_dbus_string_parse_int (&line, end, &val, NULL))
591 /* If there's an optional second int argument,
592 * fill in align padding with that value
594 if (val < 0 || val > 255)
596 _dbus_warn ("can't fill align padding with %ld, must be a byte value\n", val);
601 while (end < _dbus_string_get_length (dest))
603 _dbus_string_set_byte (dest, end, val);
608 else if (_dbus_string_starts_with_c_str (&line, "UNALIGN"))
611 just_set_unalign = TRUE;
613 else if (_dbus_string_starts_with_c_str (&line, "CHOP"))
617 /* FIXME if you CHOP the offset for a LENGTH
618 * command, we segfault.
621 _dbus_string_delete_first_word (&line);
623 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
625 _dbus_warn ("Failed to parse integer to chop\n");
629 if (val > _dbus_string_get_length (dest))
631 _dbus_warn ("Trying to chop %ld bytes but we only have %d\n",
633 _dbus_string_get_length (dest));
637 _dbus_string_shorten (dest, val);
639 else if (_dbus_string_starts_with_c_str (&line,
642 _dbus_string_delete_first_word (&line);
644 if (!save_start (length_hash, &line,
645 _dbus_string_get_length (dest)))
647 _dbus_warn ("failed to save length start\n");
651 else if (_dbus_string_starts_with_c_str (&line,
654 _dbus_string_delete_first_word (&line);
656 if (!save_length (length_hash, &line,
657 _dbus_string_get_length (dest)))
659 _dbus_warn ("failed to save length end\n");
663 else if (_dbus_string_starts_with_c_str (&line,
666 SAVE_FOR_UNALIGN (dest, 4);
668 _dbus_string_delete_first_word (&line);
670 if (!append_saved_length (dest, length_hash,
672 unalign ? align_pad_start : align_pad_end,
675 _dbus_warn ("failed to add LENGTH\n");
679 PERFORM_UNALIGN (dest);
681 else if (_dbus_string_starts_with_c_str (&line,
684 _dbus_string_delete_first_word (&line);
686 if (_dbus_string_get_length (&line) != 4)
688 _dbus_warn ("Field name must be four characters not \"%s\"\n",
689 _dbus_string_get_const_data (&line));
696 _dbus_string_align_length (dest, 4);
698 if (!_dbus_string_copy (&line, 0, dest,
699 _dbus_string_get_length (dest)))
702 else if (_dbus_string_starts_with_c_str (&line,
707 _dbus_string_delete_first_word (&line);
709 if (_dbus_string_starts_with_c_str (&line, "INVALID"))
710 code = DBUS_TYPE_INVALID;
711 else if (_dbus_string_starts_with_c_str (&line, "NIL"))
712 code = DBUS_TYPE_NIL;
713 else if (_dbus_string_starts_with_c_str (&line, "BYTE"))
714 code = DBUS_TYPE_BYTE;
715 else if (_dbus_string_starts_with_c_str (&line, "BOOLEAN"))
716 code = DBUS_TYPE_BOOLEAN;
717 else if (_dbus_string_starts_with_c_str (&line, "INT32"))
718 code = DBUS_TYPE_INT32;
719 else if (_dbus_string_starts_with_c_str (&line, "UINT32"))
720 code = DBUS_TYPE_UINT32;
721 else if (_dbus_string_starts_with_c_str (&line, "DOUBLE"))
722 code = DBUS_TYPE_DOUBLE;
723 else if (_dbus_string_starts_with_c_str (&line, "STRING"))
724 code = DBUS_TYPE_STRING;
725 else if (_dbus_string_starts_with_c_str (&line, "OBJECT_PATH"))
726 code = DBUS_TYPE_OBJECT_PATH;
727 else if (_dbus_string_starts_with_c_str (&line, "NAMED"))
728 code = DBUS_TYPE_NAMED;
729 else if (_dbus_string_starts_with_c_str (&line, "ARRAY"))
730 code = DBUS_TYPE_ARRAY;
731 else if (_dbus_string_starts_with_c_str (&line, "DICT"))
732 code = DBUS_TYPE_DICT;
735 _dbus_warn ("%s is not a valid type name\n", _dbus_string_get_const_data (&line));
739 if (!_dbus_string_append_byte (dest, code))
741 _dbus_warn ("could not append typecode byte\n");
745 else if (_dbus_string_starts_with_c_str (&line,
748 SAVE_FOR_UNALIGN (dest, 4);
749 int i, len, allocated;
750 unsigned char *values;
755 values = dbus_new (unsigned char, allocated);
758 _dbus_warn ("could not allocate memory for BYTE_ARRAY\n");
764 _dbus_string_delete_first_word (&line);
765 _dbus_string_skip_blank (&line, 0, &i);
766 b = _dbus_string_get_byte (&line, i++);
771 while (i < _dbus_string_get_length (&line))
773 _dbus_string_skip_blank (&line, i, &i);
775 if (_dbus_string_get_byte (&line, i) == '\'' &&
776 _dbus_string_get_length (&line) >= i + 4 &&
777 _dbus_string_get_byte (&line, i + 1) == '\\' &&
778 _dbus_string_get_byte (&line, i + 2) == '\'' &&
779 _dbus_string_get_byte (&line, i + 3) == '\'')
784 else if (_dbus_string_get_byte (&line, i) == '\'' &&
785 _dbus_string_get_length (&line) >= i + 3 &&
786 _dbus_string_get_byte (&line, i + 2) == '\'')
788 val = _dbus_string_get_byte (&line, i + 1);
793 if (!_dbus_string_parse_int (&line, i, &val, &i))
795 _dbus_warn ("Failed to parse integer for BYTE_ARRAY\n");
799 if (val < 0 || val > 255)
801 _dbus_warn ("A byte must be in range 0-255 not %ld\n",
808 if (len == allocated)
811 values = dbus_realloc (values, allocated * sizeof (unsigned char));
814 _dbus_warn ("could not allocate memory for BYTE_ARRAY\n");
819 _dbus_string_skip_blank (&line, i, &i);
821 b = _dbus_string_get_byte (&line, i++);
829 if (!_dbus_marshal_int32 (dest, endian, len) ||
830 !_dbus_string_append_len (dest, values, len))
832 _dbus_warn ("failed to append BYTE_ARRAY\n");
837 PERFORM_UNALIGN (dest);
839 else if (_dbus_string_starts_with_c_str (&line,
842 SAVE_FOR_UNALIGN (dest, 4);
843 int i, len, allocated;
844 unsigned char *values;
845 unsigned char b, val;
848 values = dbus_new (unsigned char, allocated);
851 _dbus_warn ("could not allocate memory for BOOLEAN_ARRAY\n");
857 _dbus_string_delete_first_word (&line);
858 _dbus_string_skip_blank (&line, 0, &i);
859 b = _dbus_string_get_byte (&line, i++);
864 while (i < _dbus_string_get_length (&line))
866 _dbus_string_skip_blank (&line, i, &i);
868 if (_dbus_string_find_to (&line, i, i + 5,
874 else if (_dbus_string_find_to (&line, i, i + 4,
882 _dbus_warn ("could not parse BOOLEAN_ARRAY\n");
887 if (len == allocated)
890 values = dbus_realloc (values, allocated * sizeof (unsigned char));
893 _dbus_warn ("could not allocate memory for BOOLEAN_ARRAY\n");
898 _dbus_string_skip_blank (&line, i, &i);
900 b = _dbus_string_get_byte (&line, i++);
908 if (!_dbus_marshal_int32 (dest, endian, len) ||
909 !_dbus_string_append_len (dest, values, len))
911 _dbus_warn ("failed to append BOOLEAN_ARRAY\n");
916 PERFORM_UNALIGN (dest);
918 else if (_dbus_string_starts_with_c_str (&line,
921 SAVE_FOR_UNALIGN (dest, 4);
922 int i, len, allocated;
923 dbus_int32_t *values;
928 values = dbus_new (dbus_int32_t, allocated);
931 _dbus_warn ("could not allocate memory for INT32_ARRAY\n");
937 _dbus_string_delete_first_word (&line);
938 _dbus_string_skip_blank (&line, 0, &i);
939 b = _dbus_string_get_byte (&line, i++);
944 while (i < _dbus_string_get_length (&line))
946 _dbus_string_skip_blank (&line, i, &i);
948 if (!_dbus_string_parse_int (&line, i, &val, &i))
950 _dbus_warn ("could not parse integer for INT32_ARRAY\n");
955 if (len == allocated)
958 values = dbus_realloc (values, allocated * sizeof (dbus_int32_t));
961 _dbus_warn ("could not allocate memory for INT32_ARRAY\n");
966 _dbus_string_skip_blank (&line, i, &i);
968 b = _dbus_string_get_byte (&line, i++);
976 if (!_dbus_marshal_int32_array (dest, endian, values, len))
978 _dbus_warn ("failed to append INT32_ARRAY\n");
983 PERFORM_UNALIGN (dest);
985 else if (_dbus_string_starts_with_c_str (&line,
988 SAVE_FOR_UNALIGN (dest, 4);
989 int i, len, allocated;
990 dbus_uint32_t *values;
995 values = dbus_new (dbus_uint32_t, allocated);
998 _dbus_warn ("could not allocate memory for UINT32_ARRAY\n");
1004 _dbus_string_delete_first_word (&line);
1005 _dbus_string_skip_blank (&line, 0, &i);
1006 b = _dbus_string_get_byte (&line, i++);
1011 while (i < _dbus_string_get_length (&line))
1013 _dbus_string_skip_blank (&line, i, &i);
1015 if (!_dbus_string_parse_int (&line, i, &val, &i))
1017 _dbus_warn ("could not parse integer for UINT32_ARRAY\n");
1021 values[len++] = val;
1022 if (len == allocated)
1025 values = dbus_realloc (values, allocated * sizeof (dbus_uint32_t));
1028 _dbus_warn ("could not allocate memory for UINT32_ARRAY\n");
1033 _dbus_string_skip_blank (&line, i, &i);
1035 b = _dbus_string_get_byte (&line, i++);
1043 if (!_dbus_marshal_uint32_array (dest, endian, values, len))
1045 _dbus_warn ("failed to append UINT32_ARRAY\n");
1050 PERFORM_UNALIGN (dest);
1052 else if (_dbus_string_starts_with_c_str (&line,
1055 SAVE_FOR_UNALIGN (dest, 8);
1056 int i, len, allocated;
1062 values = dbus_new (double, allocated);
1065 _dbus_warn ("could not allocate memory for DOUBLE_ARRAY\n");
1071 _dbus_string_delete_first_word (&line);
1072 _dbus_string_skip_blank (&line, 0, &i);
1073 b = _dbus_string_get_byte (&line, i++);
1078 while (i < _dbus_string_get_length (&line))
1080 _dbus_string_skip_blank (&line, i, &i);
1082 if (!_dbus_string_parse_double (&line, i, &val, &i))
1084 _dbus_warn ("could not parse double for DOUBLE_ARRAY\n");
1088 values[len++] = val;
1089 if (len == allocated)
1092 values = dbus_realloc (values, allocated * sizeof (double));
1095 _dbus_warn ("could not allocate memory for DOUBLE_ARRAY\n");
1100 _dbus_string_skip_blank (&line, i, &i);
1102 b = _dbus_string_get_byte (&line, i++);
1110 if (!_dbus_marshal_double_array (dest, endian, values, len))
1112 _dbus_warn ("failed to append DOUBLE_ARRAY\n");
1117 PERFORM_UNALIGN (dest);
1119 else if (_dbus_string_starts_with_c_str (&line,
1122 SAVE_FOR_UNALIGN (dest, 4);
1123 int i, len, allocated;
1130 values = dbus_new (char *, allocated);
1133 _dbus_warn ("could not allocate memory for STRING_ARRAY\n");
1139 _dbus_string_delete_first_word (&line);
1140 _dbus_string_skip_blank (&line, 0, &i);
1141 b = _dbus_string_get_byte (&line, i++);
1146 _dbus_string_init (&val_str);
1147 while (i < _dbus_string_get_length (&line))
1149 _dbus_string_skip_blank (&line, i, &i);
1151 if (!append_quoted_string (&val_str, &line, i, &i))
1153 _dbus_warn ("could not parse quoted string for STRING_ARRAY\n");
1158 if (!_dbus_string_steal_data (&val_str, &val))
1160 _dbus_warn ("could not allocate memory for STRING_ARRAY string\n");
1164 values[len++] = val;
1165 if (len == allocated)
1168 values = dbus_realloc (values, allocated * sizeof (char *));
1171 _dbus_warn ("could not allocate memory for STRING_ARRAY\n");
1176 _dbus_string_skip_blank (&line, i, &i);
1178 b = _dbus_string_get_byte (&line, i++);
1184 _dbus_warn ("missing comma when parsing STRING_ARRAY\n");
1188 _dbus_string_free (&val_str);
1190 if (!_dbus_marshal_string_array (dest, endian, (const char **)values, len))
1192 _dbus_warn ("failed to append STRING_ARRAY\n");
1197 dbus_free_string_array (values);
1199 PERFORM_UNALIGN (dest);
1201 else if (_dbus_string_starts_with_c_str (&line, "BYTE"))
1203 unsigned char the_byte;
1205 _dbus_string_delete_first_word (&line);
1207 if (_dbus_string_equal_c_str (&line, "'\\''"))
1209 else if (_dbus_string_get_byte (&line, 0) == '\'' &&
1210 _dbus_string_get_length (&line) >= 3 &&
1211 _dbus_string_get_byte (&line, 2) == '\'')
1212 the_byte = _dbus_string_get_byte (&line, 1);
1216 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
1218 _dbus_warn ("Failed to parse integer for BYTE\n");
1224 _dbus_warn ("A byte must be in range 0-255 not %ld\n",
1228 the_byte = (unsigned char) val;
1231 _dbus_string_append_byte (dest, the_byte);
1233 else if (_dbus_string_starts_with_c_str (&line,
1238 _dbus_string_delete_first_word (&line);
1240 if (_dbus_string_starts_with_c_str (&line, "true"))
1242 else if (_dbus_string_starts_with_c_str (&line, "false"))
1246 _dbus_warn ("could not parse BOOLEAN\n");
1249 if (!_dbus_string_append_byte (dest, val))
1251 _dbus_warn ("failed to append BOOLEAN\n");
1256 else if (_dbus_string_starts_with_c_str (&line,
1259 SAVE_FOR_UNALIGN (dest, 4);
1262 _dbus_string_delete_first_word (&line);
1264 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
1266 _dbus_warn ("could not parse integer for INT32\n");
1270 if (!_dbus_marshal_int32 (dest, endian,
1273 _dbus_warn ("failed to append INT32\n");
1277 PERFORM_UNALIGN (dest);
1279 else if (_dbus_string_starts_with_c_str (&line,
1282 SAVE_FOR_UNALIGN (dest, 4);
1285 _dbus_string_delete_first_word (&line);
1287 if (!_dbus_string_parse_uint (&line, 0, &val, NULL))
1290 if (!_dbus_marshal_uint32 (dest, endian,
1293 _dbus_warn ("failed to append UINT32\n");
1297 PERFORM_UNALIGN (dest);
1299 else if (_dbus_string_starts_with_c_str (&line,
1302 SAVE_FOR_UNALIGN (dest, 8);
1305 _dbus_string_delete_first_word (&line);
1307 if (!_dbus_string_parse_double (&line, 0, &val, NULL))
1310 if (!_dbus_marshal_double (dest, endian,
1313 _dbus_warn ("failed to append DOUBLE\n");
1317 PERFORM_UNALIGN (dest);
1319 else if (_dbus_string_starts_with_c_str (&line,
1322 SAVE_FOR_UNALIGN (dest, 4);
1326 _dbus_string_delete_first_word (&line);
1328 size_offset = _dbus_string_get_length (dest);
1329 size_offset = _DBUS_ALIGN_VALUE (size_offset, 4);
1330 if (!_dbus_marshal_uint32 (dest, endian, 0))
1332 _dbus_warn ("Failed to append string size\n");
1336 old_len = _dbus_string_get_length (dest);
1337 if (!append_quoted_string (dest, &line, 0, NULL))
1339 _dbus_warn ("Failed to append quoted string\n");
1343 _dbus_marshal_set_uint32 (dest, endian, size_offset,
1344 /* subtract 1 for nul */
1345 _dbus_string_get_length (dest) - old_len - 1);
1347 PERFORM_UNALIGN (dest);
1349 else if (_dbus_string_starts_with_c_str (&line,
1352 SAVE_FOR_UNALIGN (dest, 4);
1356 _dbus_string_delete_first_word (&line);
1358 size_offset = _dbus_string_get_length (dest);
1359 size_offset = _DBUS_ALIGN_VALUE (size_offset, 4);
1360 if (!_dbus_marshal_uint32 (dest, endian, 0))
1362 _dbus_warn ("Failed to append string size\n");
1366 old_len = _dbus_string_get_length (dest);
1367 if (!append_quoted_string (dest, &line, 0, NULL))
1369 _dbus_warn ("Failed to append quoted string\n");
1373 _dbus_marshal_set_uint32 (dest, endian, size_offset,
1374 /* subtract 1 for nul */
1375 _dbus_string_get_length (dest) - old_len - 1);
1377 PERFORM_UNALIGN (dest);
1382 if (!just_set_unalign && unalign)
1384 _dbus_warn ("UNALIGN prior to something that isn't aligned\n");
1388 goto next_iteration; /* skip parse_failed */
1392 _dbus_warn ("couldn't process line %d \"%s\"\n",
1393 line_no, _dbus_string_get_const_data (&line));
1398 _dbus_hash_iter_init (length_hash, &iter);
1399 while (_dbus_hash_iter_next (&iter))
1401 SavedLength *sl = _dbus_hash_iter_get_value (&iter);
1404 s = _dbus_string_get_const_data (&sl->name);
1408 _dbus_warn ("Used LENGTH %s but never did END_LENGTH\n",
1412 else if (sl->offset < 0)
1414 _dbus_warn ("Did END_LENGTH %s but never used LENGTH\n",
1423 _dbus_verbose ("Filling in length %s endian = %d offset = %d start = %d length = %d\n",
1424 s, sl->endian, sl->offset, sl->start, sl->length);
1425 _dbus_marshal_set_int32 (dest,
1428 sl->length - sl->start);
1431 _dbus_hash_iter_remove_entry (&iter);
1436 _dbus_verbose_bytes_of_string (dest, 0, _dbus_string_get_length (dest));
1439 if (length_hash != NULL)
1440 _dbus_hash_table_unref (length_hash);
1442 _dbus_string_free (&file);
1443 _dbus_string_free (&line);
1448 #endif /* DBUS_BUILD_TESTS */