1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-string-util.c Would be in dbus-string.c, but not used in libdbus
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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
24 #include "dbus-internals.h"
25 #include "dbus-string.h"
26 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
27 #include "dbus-string-private.h"
30 * @addtogroup DBusString
35 * Copies the contents of a DBusString into a different
36 * buffer. The resulting buffer will be nul-terminated.
39 * @param buffer a C buffer to copy data to
40 * @param avail_len maximum length of C buffer
43 _dbus_string_copy_to_buffer (const DBusString *str,
48 DBUS_CONST_STRING_PREAMBLE (str);
50 _dbus_assert (avail_len >= 0);
52 copy_len = MIN (avail_len, real->len+1);
53 memcpy (buffer, real->str, copy_len);
54 if (avail_len > 0 && avail_len == copy_len)
55 buffer[avail_len-1] = '\0';
59 * Returns whether a string ends with the given suffix
61 * @todo memcmp might make this faster.
64 * @param c_str the C-style string
65 * @returns #TRUE if the string ends with the suffix
68 _dbus_string_ends_with_c_str (const DBusString *a,
71 const unsigned char *ap;
72 const unsigned char *bp;
73 const unsigned char *a_end;
74 unsigned long c_str_len;
75 const DBusRealString *real_a = (const DBusRealString*) a;
76 DBUS_GENERIC_STRING_PREAMBLE (real_a);
77 _dbus_assert (c_str != NULL);
79 c_str_len = strlen (c_str);
80 if (((unsigned long)real_a->len) < c_str_len)
83 ap = real_a->str + (real_a->len - c_str_len);
84 bp = (const unsigned char*) c_str;
85 a_end = real_a->str + real_a->len;
95 _dbus_assert (*ap == '\0');
96 _dbus_assert (*bp == '\0');
102 * Find the given byte scanning backward from the given start.
103 * Sets *found to -1 if the byte is not found.
105 * @param str the string
106 * @param start the place to start scanning (will not find the byte at this point)
107 * @param byte the byte to find
108 * @param found return location for where it was found
109 * @returns #TRUE if found
112 _dbus_string_find_byte_backward (const DBusString *str,
118 DBUS_CONST_STRING_PREAMBLE (str);
119 _dbus_assert (start <= real->len);
120 _dbus_assert (start >= 0);
121 _dbus_assert (found != NULL);
126 if (real->str[i] == byte)
139 * Skips whitespace from start, storing the first non-whitespace in *end.
140 * (whitespace is space, tab, newline, CR).
142 * @param str the string
143 * @param start where to start
144 * @param end where to store the first non-whitespace byte index
147 _dbus_string_skip_white (const DBusString *str,
152 DBUS_CONST_STRING_PREAMBLE (str);
153 _dbus_assert (start <= real->len);
154 _dbus_assert (start >= 0);
157 while (i < real->len)
159 if (!(real->str[i] == ' ' ||
160 real->str[i] == '\n' ||
161 real->str[i] == '\r' ||
162 real->str[i] == '\t'))
168 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
169 real->str[i] == '\t'));
177 #ifdef DBUS_BUILD_TESTS
178 #include "dbus-test.h"
182 test_max_len (DBusString *str,
187 if (!_dbus_string_set_length (str, max_len - 1))
188 _dbus_assert_not_reached ("setting len to one less than max should have worked");
191 if (!_dbus_string_set_length (str, max_len))
192 _dbus_assert_not_reached ("setting len to max len should have worked");
194 if (_dbus_string_set_length (str, max_len + 1))
195 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
197 if (!_dbus_string_set_length (str, 0))
198 _dbus_assert_not_reached ("setting len to zero should have worked");
202 test_hex_roundtrip (const unsigned char *data,
213 if (!_dbus_string_init (&orig))
214 _dbus_assert_not_reached ("could not init string");
216 if (!_dbus_string_init (&encoded))
217 _dbus_assert_not_reached ("could not init string");
219 if (!_dbus_string_init (&decoded))
220 _dbus_assert_not_reached ("could not init string");
222 if (!_dbus_string_append_len (&orig, data, len))
223 _dbus_assert_not_reached ("couldn't append orig data");
225 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
226 _dbus_assert_not_reached ("could not encode");
228 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
229 _dbus_assert_not_reached ("could not decode");
231 _dbus_assert (_dbus_string_get_length (&encoded) == end);
233 if (!_dbus_string_equal (&orig, &decoded))
237 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
238 _dbus_string_get_length (&orig),
239 _dbus_string_get_length (&encoded),
240 _dbus_string_get_length (&decoded));
241 printf ("Original: %s\n", data);
242 s = _dbus_string_get_const_data (&decoded);
243 printf ("Decoded: %s\n", s);
244 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
247 _dbus_string_free (&orig);
248 _dbus_string_free (&encoded);
249 _dbus_string_free (&decoded);
252 typedef void (* TestRoundtripFunc) (const unsigned char *data,
255 test_roundtrips (TestRoundtripFunc func)
257 (* func) ("Hello this is a string\n", -1);
258 (* func) ("Hello this is a string\n1", -1);
259 (* func) ("Hello this is a string\n12", -1);
260 (* func) ("Hello this is a string\n123", -1);
261 (* func) ("Hello this is a string\n1234", -1);
262 (* func) ("Hello this is a string\n12345", -1);
267 (* func) ("1234", 4);
268 (* func) ("12345", 5);
273 (* func) ("1234", 5);
274 (* func) ("12345", 6);
276 unsigned char buf[512];
280 while (i < _DBUS_N_ELEMENTS (buf))
286 while (i < _DBUS_N_ELEMENTS (buf))
294 #ifdef DBUS_BUILD_TESTS
295 /* The max length thing is sort of a historical artifact
296 * from a feature that turned out to be dumb; perhaps
297 * we should purge it entirely. The problem with
298 * the feature is that it looks like memory allocation
299 * failure, but is not a transient or resolvable failure.
302 set_max_length (DBusString *str,
305 DBusRealString *real;
307 real = (DBusRealString*) str;
309 real->max_length = max_length;
311 #endif /* DBUS_BUILD_TESTS */
314 * @ingroup DBusStringInternals
315 * Unit test for DBusString.
317 * @todo Need to write tests for _dbus_string_copy() and
318 * _dbus_string_move() moving to/from each of start/middle/end of a
319 * string. Also need tests for _dbus_string_move_len ()
321 * @returns #TRUE on success.
324 _dbus_string_test (void)
331 int lens[] = { 0, 1, 2, 3, 4, 5, 10, 16, 17, 18, 25, 31, 32, 33, 34, 35, 63, 64, 65, 66, 67, 68, 69, 70, 71, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136 };
336 while (i < _DBUS_N_ELEMENTS (lens))
338 if (!_dbus_string_init (&str))
339 _dbus_assert_not_reached ("failed to init string");
341 set_max_length (&str, lens[i]);
343 test_max_len (&str, lens[i]);
344 _dbus_string_free (&str);
349 /* Test shortening and setting length */
351 while (i < _DBUS_N_ELEMENTS (lens))
355 if (!_dbus_string_init (&str))
356 _dbus_assert_not_reached ("failed to init string");
358 set_max_length (&str, lens[i]);
360 if (!_dbus_string_set_length (&str, lens[i]))
361 _dbus_assert_not_reached ("failed to set string length");
366 _dbus_assert (_dbus_string_get_length (&str) == j);
369 _dbus_string_shorten (&str, 1);
370 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
375 _dbus_string_free (&str);
380 /* Test appending data */
381 if (!_dbus_string_init (&str))
382 _dbus_assert_not_reached ("failed to init string");
387 if (!_dbus_string_append (&str, "a"))
388 _dbus_assert_not_reached ("failed to append string to string\n");
390 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
392 if (!_dbus_string_append_byte (&str, 'b'))
393 _dbus_assert_not_reached ("failed to append byte to string\n");
395 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
400 _dbus_string_free (&str);
402 /* Check steal_data */
404 if (!_dbus_string_init (&str))
405 _dbus_assert_not_reached ("failed to init string");
407 if (!_dbus_string_append (&str, "Hello World"))
408 _dbus_assert_not_reached ("could not append to string");
410 i = _dbus_string_get_length (&str);
412 if (!_dbus_string_steal_data (&str, &s))
413 _dbus_assert_not_reached ("failed to steal data");
415 _dbus_assert (_dbus_string_get_length (&str) == 0);
416 _dbus_assert (((int)strlen (s)) == i);
422 if (!_dbus_string_append (&str, "Hello World"))
423 _dbus_assert_not_reached ("could not append to string");
425 i = _dbus_string_get_length (&str);
427 if (!_dbus_string_init (&other))
428 _dbus_assert_not_reached ("could not init string");
430 if (!_dbus_string_move (&str, 0, &other, 0))
431 _dbus_assert_not_reached ("could not move");
433 _dbus_assert (_dbus_string_get_length (&str) == 0);
434 _dbus_assert (_dbus_string_get_length (&other) == i);
436 if (!_dbus_string_append (&str, "Hello World"))
437 _dbus_assert_not_reached ("could not append to string");
439 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
440 _dbus_assert_not_reached ("could not move");
442 _dbus_assert (_dbus_string_get_length (&str) == 0);
443 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
445 if (!_dbus_string_append (&str, "Hello World"))
446 _dbus_assert_not_reached ("could not append to string");
448 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
449 _dbus_assert_not_reached ("could not move");
451 _dbus_assert (_dbus_string_get_length (&str) == 0);
452 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
454 _dbus_string_free (&other);
458 if (!_dbus_string_append (&str, "Hello World"))
459 _dbus_assert_not_reached ("could not append to string");
461 i = _dbus_string_get_length (&str);
463 if (!_dbus_string_init (&other))
464 _dbus_assert_not_reached ("could not init string");
466 if (!_dbus_string_copy (&str, 0, &other, 0))
467 _dbus_assert_not_reached ("could not copy");
469 _dbus_assert (_dbus_string_get_length (&str) == i);
470 _dbus_assert (_dbus_string_get_length (&other) == i);
472 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
473 _dbus_assert_not_reached ("could not copy");
475 _dbus_assert (_dbus_string_get_length (&str) == i);
476 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
477 _dbus_assert (_dbus_string_equal_c_str (&other,
478 "Hello WorldHello World"));
480 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
481 _dbus_assert_not_reached ("could not copy");
483 _dbus_assert (_dbus_string_get_length (&str) == i);
484 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
485 _dbus_assert (_dbus_string_equal_c_str (&other,
486 "Hello WorldHello WorldHello World"));
488 _dbus_string_free (&str);
489 _dbus_string_free (&other);
493 if (!_dbus_string_init (&str))
494 _dbus_assert_not_reached ("failed to init string");
496 if (!_dbus_string_append (&str, "Hello World"))
497 _dbus_assert_not_reached ("could not append to string");
499 i = _dbus_string_get_length (&str);
501 if (!_dbus_string_init (&other))
502 _dbus_assert_not_reached ("could not init string");
504 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
505 &other, 0, _dbus_string_get_length (&other)))
506 _dbus_assert_not_reached ("could not replace");
508 _dbus_assert (_dbus_string_get_length (&str) == i);
509 _dbus_assert (_dbus_string_get_length (&other) == i);
510 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
512 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
514 _dbus_assert_not_reached ("could not replace center space");
516 _dbus_assert (_dbus_string_get_length (&str) == i);
517 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
518 _dbus_assert (_dbus_string_equal_c_str (&other,
519 "HelloHello WorldWorld"));
522 if (!_dbus_string_replace_len (&str, 1, 1,
524 _dbus_string_get_length (&other) - 1,
526 _dbus_assert_not_reached ("could not replace end character");
528 _dbus_assert (_dbus_string_get_length (&str) == i);
529 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
530 _dbus_assert (_dbus_string_equal_c_str (&other,
531 "HelloHello WorldWorle"));
533 _dbus_string_free (&str);
534 _dbus_string_free (&other);
536 /* Check append/get unichar */
538 if (!_dbus_string_init (&str))
539 _dbus_assert_not_reached ("failed to init string");
542 if (!_dbus_string_append_unichar (&str, 0xfffc))
543 _dbus_assert_not_reached ("failed to append unichar");
545 _dbus_string_get_unichar (&str, 0, &ch, &i);
547 _dbus_assert (ch == 0xfffc);
548 _dbus_assert (i == _dbus_string_get_length (&str));
550 _dbus_string_free (&str);
552 /* Check insert/set/get byte */
554 if (!_dbus_string_init (&str))
555 _dbus_assert_not_reached ("failed to init string");
557 if (!_dbus_string_append (&str, "Hello"))
558 _dbus_assert_not_reached ("failed to append Hello");
560 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
561 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
562 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
563 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
564 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
566 _dbus_string_set_byte (&str, 1, 'q');
567 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
569 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
570 _dbus_assert_not_reached ("can't insert byte");
572 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
573 _dbus_assert_not_reached ("can't insert byte");
575 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
576 _dbus_assert_not_reached ("can't insert byte");
578 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
579 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
580 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
581 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
582 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
583 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
584 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
585 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
586 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
587 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
588 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
590 _dbus_string_free (&str);
592 /* Check append/parse int/double */
594 if (!_dbus_string_init (&str))
595 _dbus_assert_not_reached ("failed to init string");
597 if (!_dbus_string_append_int (&str, 27))
598 _dbus_assert_not_reached ("failed to append int");
600 i = _dbus_string_get_length (&str);
602 if (!_dbus_string_parse_int (&str, 0, &v, &end))
603 _dbus_assert_not_reached ("failed to parse int");
605 _dbus_assert (v == 27);
606 _dbus_assert (end == i);
608 _dbus_string_free (&str);
610 if (!_dbus_string_init (&str))
611 _dbus_assert_not_reached ("failed to init string");
613 if (!_dbus_string_append_double (&str, 50.3))
614 _dbus_assert_not_reached ("failed to append float");
616 i = _dbus_string_get_length (&str);
618 if (!_dbus_string_parse_double (&str, 0, &d, &end))
619 _dbus_assert_not_reached ("failed to parse float");
621 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
622 _dbus_assert (end == i);
624 _dbus_string_free (&str);
627 if (!_dbus_string_init (&str))
628 _dbus_assert_not_reached ("failed to init string");
630 if (!_dbus_string_append (&str, "Hello"))
631 _dbus_assert_not_reached ("couldn't append to string");
633 if (!_dbus_string_find (&str, 0, "He", &i))
634 _dbus_assert_not_reached ("didn't find 'He'");
635 _dbus_assert (i == 0);
637 if (!_dbus_string_find (&str, 0, "Hello", &i))
638 _dbus_assert_not_reached ("didn't find 'Hello'");
639 _dbus_assert (i == 0);
641 if (!_dbus_string_find (&str, 0, "ello", &i))
642 _dbus_assert_not_reached ("didn't find 'ello'");
643 _dbus_assert (i == 1);
645 if (!_dbus_string_find (&str, 0, "lo", &i))
646 _dbus_assert_not_reached ("didn't find 'lo'");
647 _dbus_assert (i == 3);
649 if (!_dbus_string_find (&str, 2, "lo", &i))
650 _dbus_assert_not_reached ("didn't find 'lo'");
651 _dbus_assert (i == 3);
653 if (_dbus_string_find (&str, 4, "lo", &i))
654 _dbus_assert_not_reached ("did find 'lo'");
656 if (!_dbus_string_find (&str, 0, "l", &i))
657 _dbus_assert_not_reached ("didn't find 'l'");
658 _dbus_assert (i == 2);
660 if (!_dbus_string_find (&str, 0, "H", &i))
661 _dbus_assert_not_reached ("didn't find 'H'");
662 _dbus_assert (i == 0);
664 if (!_dbus_string_find (&str, 0, "", &i))
665 _dbus_assert_not_reached ("didn't find ''");
666 _dbus_assert (i == 0);
668 if (_dbus_string_find (&str, 0, "Hello!", NULL))
669 _dbus_assert_not_reached ("Did find 'Hello!'");
671 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
672 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
674 if (_dbus_string_find (&str, 0, "ill", NULL))
675 _dbus_assert_not_reached ("Did find 'ill'");
677 if (_dbus_string_find (&str, 0, "q", NULL))
678 _dbus_assert_not_reached ("Did find 'q'");
680 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
681 _dbus_assert_not_reached ("Didn't find 'He'");
683 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
684 _dbus_assert_not_reached ("Did find 'Hello'");
686 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
687 _dbus_assert_not_reached ("Did not find 'H'");
688 _dbus_assert (i == 0);
690 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
691 _dbus_assert_not_reached ("Did not find 'o'");
692 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
694 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
695 _dbus_assert_not_reached ("Did find 'o'");
696 _dbus_assert (i == -1);
698 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
699 _dbus_assert_not_reached ("Did find 'e'");
700 _dbus_assert (i == -1);
702 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
703 _dbus_assert_not_reached ("Didn't find 'e'");
704 _dbus_assert (i == 1);
706 _dbus_string_free (&str);
709 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
710 if (!_dbus_string_init (&other))
711 _dbus_assert_not_reached ("could not init string");
713 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
714 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
716 _dbus_assert (end == 8);
718 _dbus_string_free (&other);
720 test_roundtrips (test_hex_roundtrip);
722 _dbus_string_free (&str);
727 #endif /* DBUS_BUILD_TESTS */