1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-address.c Server address parser.
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2004,2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "dbus-address.h"
27 #include "dbus-internals.h"
28 #include "dbus-list.h"
29 #include "dbus-string.h"
30 #include "dbus-protocol.h"
33 * @defgroup DBusAddressInternals Address parsing
34 * @ingroup DBusInternals
35 * @brief Implementation of parsing addresses of D-BUS servers.
41 * Internals of DBusAddressEntry
43 struct DBusAddressEntry
45 DBusString method; /**< The address type (unix, tcp, etc.) */
47 DBusList *keys; /**< List of keys */
48 DBusList *values; /**< List of values */
51 /** @} */ /* End of internals */
54 dbus_address_entry_free (DBusAddressEntry *entry)
58 _dbus_string_free (&entry->method);
60 link = _dbus_list_get_first_link (&entry->keys);
63 _dbus_string_free (link->data);
64 dbus_free (link->data);
66 link = _dbus_list_get_next_link (&entry->keys, link);
68 _dbus_list_clear (&entry->keys);
70 link = _dbus_list_get_first_link (&entry->values);
73 _dbus_string_free (link->data);
74 dbus_free (link->data);
76 link = _dbus_list_get_next_link (&entry->values, link);
78 _dbus_list_clear (&entry->values);
84 * @defgroup DBusAddress Address parsing
86 * @brief Parsing addresses of D-BUS servers.
92 * Frees a #NULL-terminated array of address entries.
94 * @param entries the array.
97 dbus_address_entries_free (DBusAddressEntry **entries)
101 for (i = 0; entries[i] != NULL; i++)
102 dbus_address_entry_free (entries[i]);
106 static DBusAddressEntry *
109 DBusAddressEntry *entry;
111 entry = dbus_new0 (DBusAddressEntry, 1);
116 if (!_dbus_string_init (&entry->method))
126 * Returns the method string of an address entry.
128 * @param entry the entry.
129 * @returns a string describing the method. This string
133 dbus_address_entry_get_method (DBusAddressEntry *entry)
135 return _dbus_string_get_const_data (&entry->method);
139 * Returns a value from a key of an entry.
141 * @param entry the entry.
142 * @param key the key.
143 * @returns the key value. This string must not be freed.
146 dbus_address_entry_get_value (DBusAddressEntry *entry,
149 DBusList *values, *keys;
151 keys = _dbus_list_get_first_link (&entry->keys);
152 values = _dbus_list_get_first_link (&entry->values);
156 _dbus_assert (values != NULL);
158 if (_dbus_string_equal_c_str (keys->data, key))
159 return _dbus_string_get_const_data (values->data);
161 keys = _dbus_list_get_next_link (&entry->keys, keys);
162 values = _dbus_list_get_next_link (&entry->values, values);
168 #define _DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE(b) \
169 (((b) >= 'a' && (b) <= 'z') || \
170 ((b) >= 'A' && (b) <= 'Z') || \
171 ((b) >= '0' && (b) <= '9') || \
179 * Appends an escaped version of one string to another string,
180 * using the D-BUS address escaping mechanism
182 * @param escaped the string to append to
183 * @param unescaped the string to escape
184 * @returns #FALSE if no memory
187 _dbus_address_append_escaped (DBusString *escaped,
188 const DBusString *unescaped)
197 orig_len = _dbus_string_get_length (escaped);
198 p = _dbus_string_get_const_data (unescaped);
199 end = p + _dbus_string_get_length (unescaped);
202 if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
204 if (!_dbus_string_append_byte (escaped, *p))
209 if (!_dbus_string_append_byte (escaped, '%'))
211 if (!_dbus_string_append_byte_as_hex (escaped, *p))
222 _dbus_string_set_length (escaped, orig_len);
227 append_unescaped_value (DBusString *unescaped,
228 const DBusString *escaped,
239 p = _dbus_string_get_const_data (escaped) + escaped_start;
240 end = p + escaped_len;
243 if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
245 if (!_dbus_string_append_byte (unescaped, *p))
250 /* Efficiency is king */
259 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
260 "In D-BUS address, percent character was not followed by two hex digits");
269 _dbus_string_init_const (&hex, buf);
271 if (!_dbus_string_hex_decode (&hex, 0, &hex_end,
273 _dbus_string_get_length (unescaped)))
278 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
279 "In D-BUS address, percent character was followed by characters other than hex digits");
285 /* Error, should have been escaped */
286 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
287 "In D-BUS address, character '%c' should have been escaped\n",
298 if (!ret && error && !dbus_error_is_set (error))
299 _DBUS_SET_OOM (error);
301 _dbus_assert (ret || error == NULL || dbus_error_is_set (error));
307 * Parses an address string of the form:
309 * method:key=value,key=value;method:key=value
311 * @todo document address format in the specification
313 * @param address the address.
314 * @param entry return location to an array of entries.
315 * @param array_len return location for array length.
316 * @param error address where an error can be returned.
317 * @returns #TRUE on success, #FALSE otherwise.
320 dbus_parse_address (const char *address,
321 DBusAddressEntry ***entry,
326 int pos, end_pos, len, i;
327 DBusList *entries, *link;
328 DBusAddressEntry **entry_array;
330 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
332 _dbus_string_init_const (&str, address);
336 len = _dbus_string_get_length (&str);
340 DBusAddressEntry *entry;
344 entry = create_entry ();
347 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
352 /* Append the entry */
353 if (!_dbus_list_append (&entries, entry))
355 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
356 dbus_address_entry_free (entry);
360 /* Look for a semi-colon */
361 if (!_dbus_string_find (&str, pos, ";", &end_pos))
364 /* Look for the colon : */
365 if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
367 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon");
371 if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
373 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
379 while (pos < end_pos)
381 int comma_pos, equals_pos;
383 if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
386 if (!_dbus_string_find_to (&str, pos, comma_pos, "=", &equals_pos) ||
387 equals_pos == pos || equals_pos + 1 == comma_pos)
389 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
390 "'=' character not found or has no value following it");
398 key = dbus_new0 (DBusString, 1);
402 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
406 value = dbus_new0 (DBusString, 1);
409 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
414 if (!_dbus_string_init (key))
416 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
423 if (!_dbus_string_init (value))
425 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
426 _dbus_string_free (key);
433 if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
435 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
436 _dbus_string_free (key);
437 _dbus_string_free (value);
444 if (!append_unescaped_value (value, &str, equals_pos + 1,
445 comma_pos - equals_pos - 1, error))
447 _dbus_assert (error == NULL || dbus_error_is_set (error));
448 _dbus_string_free (key);
449 _dbus_string_free (value);
456 if (!_dbus_list_append (&entry->keys, key))
458 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
459 _dbus_string_free (key);
460 _dbus_string_free (value);
467 if (!_dbus_list_append (&entry->values, value))
469 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
470 _dbus_string_free (value);
483 *array_len = _dbus_list_get_length (&entries);
485 entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
489 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
494 entry_array [*array_len] = NULL;
496 link = _dbus_list_get_first_link (&entries);
500 entry_array[i] = link->data;
502 link = _dbus_list_get_next_link (&entries, link);
505 _dbus_list_clear (&entries);
506 *entry = entry_array;
512 link = _dbus_list_get_first_link (&entries);
515 dbus_address_entry_free (link->data);
516 link = _dbus_list_get_next_link (&entries, link);
519 _dbus_list_clear (&entries);
526 * Escapes the given string as a value in a key=value pair
527 * for a D-BUS address.
529 * @param value the unescaped value
530 * @returns newly-allocated escaped value or #NULL if no memory
533 dbus_address_escape_value (const char *value)
536 DBusString unescaped;
541 _dbus_string_init_const (&unescaped, value);
543 if (!_dbus_string_init (&escaped))
546 if (!_dbus_address_append_escaped (&escaped, &unescaped))
549 if (!_dbus_string_steal_data (&escaped, &ret))
553 _dbus_string_free (&escaped);
558 * Unescapes the given string as a value in a key=value pair
559 * for a D-BUS address.
561 * @param value the escaped value
562 * @param error error to set if the unescaping fails
563 * @returns newly-allocated unescaped value or #NULL if no memory
566 dbus_address_unescape_value (const char *value,
569 DBusString unescaped;
575 _dbus_string_init_const (&escaped, value);
577 if (!_dbus_string_init (&unescaped))
580 if (!append_unescaped_value (&unescaped, &escaped,
581 0, _dbus_string_get_length (&escaped),
585 if (!_dbus_string_steal_data (&unescaped, &ret))
589 if (ret == NULL && error && !dbus_error_is_set (error))
590 _DBUS_SET_OOM (error);
592 _dbus_assert (ret != NULL || error == NULL || dbus_error_is_set (error));
594 _dbus_string_free (&unescaped);
598 /** @} */ /* End of public API */
600 #ifdef DBUS_BUILD_TESTS
601 #include "dbus-test.h"
607 const char *unescaped;
610 static const EscapeTest escape_tests[] = {
611 { "abcde", "abcde" },
616 { "abc%24", "abc$" },
617 { "%24abc", "$abc" },
618 { "abc%24abc", "abc$abc" },
630 static const char* invalid_escaped_values[] = {
645 _dbus_address_test (void)
647 DBusAddressEntry **entries;
652 dbus_error_init (&error);
655 while (i < _DBUS_N_ELEMENTS (escape_tests))
657 const EscapeTest *test = &escape_tests[i];
661 escaped = dbus_address_escape_value (test->unescaped);
663 _dbus_assert_not_reached ("oom");
665 if (strcmp (escaped, test->escaped) != 0)
667 _dbus_warn ("Escaped '%s' as '%s' should have been '%s'\n",
668 test->unescaped, escaped, test->escaped);
673 unescaped = dbus_address_unescape_value (test->escaped, &error);
674 if (unescaped == NULL)
676 _dbus_warn ("Failed to unescape '%s': %s\n",
677 test->escaped, error.message);
678 dbus_error_free (&error);
682 if (strcmp (unescaped, test->unescaped) != 0)
684 _dbus_warn ("Unescaped '%s' as '%s' should have been '%s'\n",
685 test->escaped, unescaped, test->unescaped);
688 dbus_free (unescaped);
694 while (i < _DBUS_N_ELEMENTS (invalid_escaped_values))
698 unescaped = dbus_address_unescape_value (invalid_escaped_values[i],
700 if (unescaped != NULL)
702 _dbus_warn ("Should not have successfully unescaped '%s' to '%s'\n",
703 invalid_escaped_values[i], unescaped);
704 dbus_free (unescaped);
708 _dbus_assert (dbus_error_is_set (&error));
709 dbus_error_free (&error);
714 if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
715 &entries, &len, &error))
716 _dbus_assert_not_reached ("could not parse address");
717 _dbus_assert (len == 2);
718 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0);
719 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "name"), "test") == 0);
720 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "sliff"), "sloff") == 0);
722 dbus_address_entries_free (entries);
724 /* Different possible errors */
725 if (dbus_parse_address ("foo", &entries, &len, &error))
726 _dbus_assert_not_reached ("Parsed incorrect address.");
728 dbus_error_free (&error);
730 if (dbus_parse_address ("foo:bar", &entries, &len, &error))
731 _dbus_assert_not_reached ("Parsed incorrect address.");
733 dbus_error_free (&error);
735 if (dbus_parse_address ("foo:bar,baz", &entries, &len, &error))
736 _dbus_assert_not_reached ("Parsed incorrect address.");
738 dbus_error_free (&error);
740 if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &error))
741 _dbus_assert_not_reached ("Parsed incorrect address.");
743 dbus_error_free (&error);
745 if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &error))
746 _dbus_assert_not_reached ("Parsed incorrect address.");
748 dbus_error_free (&error);
750 if (dbus_parse_address ("foo:=foo", &entries, &len, &error))
751 _dbus_assert_not_reached ("Parsed incorrect address.");
753 dbus_error_free (&error);
755 if (dbus_parse_address ("foo:foo=", &entries, &len, &error))
756 _dbus_assert_not_reached ("Parsed incorrect address.");
758 dbus_error_free (&error);
760 if (dbus_parse_address ("foo:foo,bar=baz", &entries, &len, &error))
761 _dbus_assert_not_reached ("Parsed incorrect address.");
763 dbus_error_free (&error);