D-Bus  1.6.18
dbus-string.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-string.c String utility class (internal to D-Bus implementation)
3  *
4  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5  * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
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.
13  *
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.
18  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
26 #include "dbus-internals.h"
27 #include "dbus-string.h"
28 /* we allow a system header here, for speed/convenience */
29 #include <string.h>
30 /* for vsnprintf */
31 #include <stdio.h>
32 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
33 #include "dbus-string-private.h"
34 #include "dbus-marshal-basic.h" /* probably should be removed by moving the usage of DBUS_TYPE
35  * into the marshaling-related files
36  */
37 /* for DBUS_VA_COPY */
38 #include "dbus-sysdeps.h"
39 
78 static void
79 fixup_alignment (DBusRealString *real)
80 {
81  unsigned char *aligned;
82  unsigned char *real_block;
83  unsigned int old_align_offset;
84 
85  /* we have to have extra space in real->allocated for the align offset and nul byte */
86  _dbus_assert (real->len <= real->allocated - _DBUS_STRING_ALLOCATION_PADDING);
87 
88  old_align_offset = real->align_offset;
89  real_block = real->str - old_align_offset;
90 
91  aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
92 
93  real->align_offset = aligned - real_block;
94  real->str = aligned;
95 
96  if (old_align_offset != real->align_offset)
97  {
98  /* Here comes the suck */
99  memmove (real_block + real->align_offset,
100  real_block + old_align_offset,
101  real->len + 1);
102  }
103 
104  _dbus_assert (real->align_offset < 8);
105  _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
106 }
107 
108 static void
109 undo_alignment (DBusRealString *real)
110 {
111  if (real->align_offset != 0)
112  {
113  memmove (real->str - real->align_offset,
114  real->str,
115  real->len + 1);
116 
117  real->str = real->str - real->align_offset;
118  real->align_offset = 0;
119  }
120 }
121 
133  int allocate_size)
134 {
135  DBusRealString *real;
136 
137  _dbus_assert (str != NULL);
138 
139  _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
140 
141  real = (DBusRealString*) str;
142 
143  /* It's very important not to touch anything
144  * other than real->str if we're going to fail,
145  * since we also use this function to reset
146  * an existing string, e.g. in _dbus_string_steal_data()
147  */
148 
149  real->str = dbus_malloc (_DBUS_STRING_ALLOCATION_PADDING + allocate_size);
150  if (real->str == NULL)
151  return FALSE;
152 
153  real->allocated = _DBUS_STRING_ALLOCATION_PADDING + allocate_size;
154  real->len = 0;
155  real->str[real->len] = '\0';
156 
157  real->constant = FALSE;
158  real->locked = FALSE;
159  real->invalid = FALSE;
160  real->align_offset = 0;
161 
162  fixup_alignment (real);
163 
164  return TRUE;
165 }
166 
176 {
177  return _dbus_string_init_preallocated (str, 0);
178 }
179 
189 void
191  const char *value)
192 {
193  _dbus_assert (value != NULL);
194 
195  _dbus_string_init_const_len (str, value,
196  strlen (value));
197 }
198 
209 void
211  const char *value,
212  int len)
213 {
214  DBusRealString *real;
215 
216  _dbus_assert (str != NULL);
217  _dbus_assert (len == 0 || value != NULL);
219  _dbus_assert (len >= 0);
220 
221  real = (DBusRealString*) str;
222 
223  real->str = (unsigned char*) value;
224  real->len = len;
225  real->allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
226  real->constant = TRUE;
227  real->locked = TRUE;
228  real->invalid = FALSE;
229  real->align_offset = 0;
230 
231  /* We don't require const strings to be 8-byte aligned as the
232  * memory is coming from elsewhere.
233  */
234 }
235 
241 void
243 {
244  DBusRealString *real = (DBusRealString*) str;
246 
247  if (real->constant)
248  return;
249 
250  /* so it's safe if @p str returned by a failed
251  * _dbus_string_init call
252  * Bug: https://bugs.freedesktop.org/show_bug.cgi?id=65959
253  */
254  if (real->str == NULL)
255  return;
256 
257  dbus_free (real->str - real->align_offset);
258 
259  real->invalid = TRUE;
260 }
261 
262 static dbus_bool_t
263 compact (DBusRealString *real,
264  int max_waste)
265 {
266  unsigned char *new_str;
267  int new_allocated;
268  int waste;
269 
270  waste = real->allocated - (real->len + _DBUS_STRING_ALLOCATION_PADDING);
271 
272  if (waste <= max_waste)
273  return TRUE;
274 
275  new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
276 
277  new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
278  if (_DBUS_UNLIKELY (new_str == NULL))
279  return FALSE;
280 
281  real->str = new_str + real->align_offset;
282  real->allocated = new_allocated;
283  fixup_alignment (real);
284 
285  return TRUE;
286 }
287 
288 #ifdef DBUS_BUILD_TESTS
289 /* Not using this feature at the moment,
290  * so marked DBUS_BUILD_TESTS-only
291  */
301 void
302 _dbus_string_lock (DBusString *str)
303 {
304  DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
305 
306  real->locked = TRUE;
307 
308  /* Try to realloc to avoid excess memory usage, since
309  * we know we won't change the string further
310  */
311 #define MAX_WASTE 48
312  compact (real, MAX_WASTE);
313 }
314 #endif /* DBUS_BUILD_TESTS */
315 
316 static dbus_bool_t
317 reallocate_for_length (DBusRealString *real,
318  int new_length)
319 {
320  int new_allocated;
321  unsigned char *new_str;
322 
323  /* at least double our old allocation to avoid O(n), avoiding
324  * overflow
325  */
326  if (real->allocated > (_DBUS_STRING_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
327  new_allocated = _DBUS_STRING_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
328  else
329  new_allocated = real->allocated * 2;
330 
331  /* if you change the code just above here, run the tests without
332  * the following assert-only hack before you commit
333  */
334  /* This is keyed off asserts in addition to tests so when you
335  * disable asserts to profile, you don't get this destroyer
336  * of profiles.
337  */
338 #ifdef DBUS_DISABLE_ASSERT
339 #else
340 #ifdef DBUS_BUILD_TESTS
341  new_allocated = 0; /* ensure a realloc every time so that we go
342  * through all malloc failure codepaths
343  */
344 #endif /* DBUS_BUILD_TESTS */
345 #endif /* !DBUS_DISABLE_ASSERT */
346 
347  /* But be sure we always alloc at least space for the new length */
348  new_allocated = MAX (new_allocated,
349  new_length + _DBUS_STRING_ALLOCATION_PADDING);
350 
351  _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
352  new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
353  if (_DBUS_UNLIKELY (new_str == NULL))
354  return FALSE;
355 
356  real->str = new_str + real->align_offset;
357  real->allocated = new_allocated;
358  fixup_alignment (real);
359 
360  return TRUE;
361 }
362 
376  int max_waste)
377 {
378  DBUS_STRING_PREAMBLE (str);
379 
380  return compact (real, max_waste);
381 }
382 
383 static dbus_bool_t
384 set_length (DBusRealString *real,
385  int new_length)
386 {
387  /* Note, we are setting the length not including nul termination */
388 
389  /* exceeding max length is the same as failure to allocate memory */
390  if (_DBUS_UNLIKELY (new_length > _DBUS_STRING_MAX_LENGTH))
391  return FALSE;
392  else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
393  _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
394  return FALSE;
395  else
396  {
397  real->len = new_length;
398  real->str[new_length] = '\0';
399  return TRUE;
400  }
401 }
402 
403 static dbus_bool_t
404 open_gap (int len,
405  DBusRealString *dest,
406  int insert_at)
407 {
408  if (len == 0)
409  return TRUE;
410 
411  if (len > _DBUS_STRING_MAX_LENGTH - dest->len)
412  return FALSE; /* detected overflow of dest->len + len below */
413 
414  if (!set_length (dest, dest->len + len))
415  return FALSE;
416 
417  memmove (dest->str + insert_at + len,
418  dest->str + insert_at,
419  dest->len - len - insert_at);
420 
421  return TRUE;
422 }
423 
424 #ifndef _dbus_string_get_data
425 
436 char*
438 {
439  DBUS_STRING_PREAMBLE (str);
440 
441  return (char*) real->str;
442 }
443 #endif /* _dbus_string_get_data */
444 
445 /* only do the function if we don't have the macro */
446 #ifndef _dbus_string_get_const_data
447 
453 const char*
455 {
457 
458  return (const char*) real->str;
459 }
460 #endif /* _dbus_string_get_const_data */
461 
475 char*
477  int start,
478  int len)
479 {
480  DBUS_STRING_PREAMBLE (str);
481  _dbus_assert (start >= 0);
482  _dbus_assert (len >= 0);
483  _dbus_assert (start <= real->len);
484  _dbus_assert (len <= real->len - start);
485 
486  return (char*) real->str + start;
487 }
488 
489 /* only do the function if we don't have the macro */
490 #ifndef _dbus_string_get_const_data_len
491 
499 const char*
501  int start,
502  int len)
503 {
505  _dbus_assert (start >= 0);
506  _dbus_assert (len >= 0);
507  _dbus_assert (start <= real->len);
508  _dbus_assert (len <= real->len - start);
509 
510  return (const char*) real->str + start;
511 }
512 #endif /* _dbus_string_get_const_data_len */
513 
514 /* only do the function if we don't have the macro */
515 #ifndef _dbus_string_set_byte
516 
523 void
525  int i,
526  unsigned char byte)
527 {
528  DBUS_STRING_PREAMBLE (str);
529  _dbus_assert (i < real->len);
530  _dbus_assert (i >= 0);
531 
532  real->str[i] = byte;
533 }
534 #endif /* _dbus_string_set_byte */
535 
536 /* only have the function if we didn't create a macro */
537 #ifndef _dbus_string_get_byte
538 
547 unsigned char
549  int start)
550 {
552  _dbus_assert (start <= real->len);
553  _dbus_assert (start >= 0);
554 
555  return real->str[start];
556 }
557 #endif /* _dbus_string_get_byte */
558 
571  int i,
572  int n_bytes,
573  unsigned char byte)
574 {
575  DBUS_STRING_PREAMBLE (str);
576  _dbus_assert (i <= real->len);
577  _dbus_assert (i >= 0);
578  _dbus_assert (n_bytes >= 0);
579 
580  if (n_bytes == 0)
581  return TRUE;
582 
583  if (!open_gap (n_bytes, real, i))
584  return FALSE;
585 
586  memset (real->str + i, byte, n_bytes);
587 
588  return TRUE;
589 }
590 
601  int i,
602  unsigned char byte)
603 {
604  DBUS_STRING_PREAMBLE (str);
605  _dbus_assert (i <= real->len);
606  _dbus_assert (i >= 0);
607 
608  if (!open_gap (1, real, i))
609  return FALSE;
610 
611  real->str[i] = byte;
612 
613  return TRUE;
614 }
615 
628  char **data_return)
629 {
630  DBUS_STRING_PREAMBLE (str);
631  _dbus_assert (data_return != NULL);
632 
633  undo_alignment (real);
634 
635  *data_return = (char*) real->str;
636 
637  /* reset the string */
638  if (!_dbus_string_init (str))
639  {
640  /* hrm, put it back then */
641  real->str = (unsigned char*) *data_return;
642  *data_return = NULL;
643  fixup_alignment (real);
644  return FALSE;
645  }
646 
647  return TRUE;
648 }
649 
659  char **data_return)
660 {
662  _dbus_assert (data_return != NULL);
663 
664  *data_return = dbus_malloc (real->len + 1);
665  if (*data_return == NULL)
666  return FALSE;
667 
668  memcpy (*data_return, real->str, real->len + 1);
669 
670  return TRUE;
671 }
672 
682 void
684  char *buffer,
685  int avail_len)
686 {
688 
689  _dbus_assert (avail_len >= 0);
690  _dbus_assert (avail_len >= real->len);
691 
692  memcpy (buffer, real->str, real->len);
693 }
694 
704 void
706  char *buffer,
707  int avail_len)
708 {
710 
711  _dbus_assert (avail_len >= 0);
712  _dbus_assert (avail_len > real->len);
713 
714  memcpy (buffer, real->str, real->len+1);
715 }
716 
717 /* Only have the function if we don't have the macro */
718 #ifndef _dbus_string_get_length
719 
724 int
726 {
728 
729  return real->len;
730 }
731 #endif /* !_dbus_string_get_length */
732 
747  int additional_length)
748 {
749  DBUS_STRING_PREAMBLE (str);
750  _dbus_assert (additional_length >= 0);
751 
752  if (_DBUS_UNLIKELY (additional_length > _DBUS_STRING_MAX_LENGTH - real->len))
753  return FALSE; /* would overflow */
754 
755  return set_length (real,
756  real->len + additional_length);
757 }
758 
765 void
767  int length_to_remove)
768 {
769  DBUS_STRING_PREAMBLE (str);
770  _dbus_assert (length_to_remove >= 0);
771  _dbus_assert (length_to_remove <= real->len);
772 
773  set_length (real,
774  real->len - length_to_remove);
775 }
776 
789  int length)
790 {
791  DBUS_STRING_PREAMBLE (str);
792  _dbus_assert (length >= 0);
793 
794  return set_length (real, length);
795 }
796 
797 static dbus_bool_t
798 align_insert_point_then_open_gap (DBusString *str,
799  int *insert_at_p,
800  int alignment,
801  int gap_size)
802 {
803  unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
804  unsigned long gap_pos;
805  int insert_at;
806  int delta;
807  DBUS_STRING_PREAMBLE (str);
808  _dbus_assert (alignment >= 1);
809  _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
810 
811  insert_at = *insert_at_p;
812 
813  _dbus_assert (insert_at <= real->len);
814 
815  gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
816  new_len = real->len + (gap_pos - insert_at) + gap_size;
817 
818  if (_DBUS_UNLIKELY (new_len > (unsigned long) _DBUS_STRING_MAX_LENGTH))
819  return FALSE;
820 
821  delta = new_len - real->len;
822  _dbus_assert (delta >= 0);
823 
824  if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
825  {
826  _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
827  return TRUE;
828  }
829 
830  if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
831  real, insert_at)))
832  return FALSE;
833 
834  /* nul the padding if we had to add any padding */
835  if (gap_size < delta)
836  {
837  memset (&real->str[insert_at], '\0',
838  gap_pos - insert_at);
839  }
840 
841  *insert_at_p = gap_pos;
842 
843  return TRUE;
844 }
845 
846 static dbus_bool_t
847 align_length_then_lengthen (DBusString *str,
848  int alignment,
849  int then_lengthen_by)
850 {
851  int insert_at;
852 
853  insert_at = _dbus_string_get_length (str);
854 
855  return align_insert_point_then_open_gap (str,
856  &insert_at,
857  alignment, then_lengthen_by);
858 }
859 
870  int alignment)
871 {
872  return align_length_then_lengthen (str, alignment, 0);
873 }
874 
886  int extra_bytes)
887 {
888  if (!_dbus_string_lengthen (str, extra_bytes))
889  return FALSE;
890  _dbus_string_shorten (str, extra_bytes);
891 
892  return TRUE;
893 }
894 
895 static dbus_bool_t
896 append (DBusRealString *real,
897  const char *buffer,
898  int buffer_len)
899 {
900  if (buffer_len == 0)
901  return TRUE;
902 
903  if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
904  return FALSE;
905 
906  memcpy (real->str + (real->len - buffer_len),
907  buffer,
908  buffer_len);
909 
910  return TRUE;
911 }
912 
922  const char *buffer)
923 {
924  unsigned long buffer_len;
925 
926  DBUS_STRING_PREAMBLE (str);
927  _dbus_assert (buffer != NULL);
928 
929  buffer_len = strlen (buffer);
930  if (buffer_len > (unsigned long) _DBUS_STRING_MAX_LENGTH)
931  return FALSE;
932 
933  return append (real, buffer, buffer_len);
934 }
935 
937 #define ASSIGN_2_OCTETS(p, octets) \
938  *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
939 
941 #define ASSIGN_4_OCTETS(p, octets) \
942  *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
943 
944 #ifdef DBUS_HAVE_INT64
945 
946 #define ASSIGN_8_OCTETS(p, octets) \
947  *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
948 #else
949 
950 #define ASSIGN_8_OCTETS(p, octets) \
951 do { \
952  unsigned char *b; \
953  \
954  b = p; \
955  \
956  *b++ = octets[0]; \
957  *b++ = octets[1]; \
958  *b++ = octets[2]; \
959  *b++ = octets[3]; \
960  *b++ = octets[4]; \
961  *b++ = octets[5]; \
962  *b++ = octets[6]; \
963  *b++ = octets[7]; \
964  _dbus_assert (b == p + 8); \
965 } while (0)
966 #endif /* DBUS_HAVE_INT64 */
967 
979  int insert_at,
980  const unsigned char octets[4])
981 {
982  DBUS_STRING_PREAMBLE (str);
983 
984  if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
985  return FALSE;
986 
987  ASSIGN_2_OCTETS (real->str + insert_at, octets);
988 
989  return TRUE;
990 }
991 
1003  int insert_at,
1004  const unsigned char octets[4])
1005 {
1006  DBUS_STRING_PREAMBLE (str);
1007 
1008  if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1009  return FALSE;
1010 
1011  ASSIGN_4_OCTETS (real->str + insert_at, octets);
1012 
1013  return TRUE;
1014 }
1015 
1027  int insert_at,
1028  const unsigned char octets[8])
1029 {
1030  DBUS_STRING_PREAMBLE (str);
1031 
1032  if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1033  return FALSE;
1034 
1035  _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1036 
1037  ASSIGN_8_OCTETS (real->str + insert_at, octets);
1038 
1039  return TRUE;
1040 }
1041 
1042 
1055  int *insert_at,
1056  int alignment)
1057 {
1058  DBUS_STRING_PREAMBLE (str);
1059 
1060  if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1061  return FALSE;
1062 
1063  _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1064 
1065  return TRUE;
1066 }
1067 
1079  const char *format,
1080  va_list args)
1081 {
1082  int len;
1083  va_list args_copy;
1084 
1085  DBUS_STRING_PREAMBLE (str);
1086 
1087  DBUS_VA_COPY (args_copy, args);
1088 
1089  /* Measure the message length without terminating nul */
1090  len = _dbus_printf_string_upper_bound (format, args);
1091 
1092  if (len < 0)
1093  return FALSE;
1094 
1095  if (!_dbus_string_lengthen (str, len))
1096  {
1097  /* don't leak the copy */
1098  va_end (args_copy);
1099  return FALSE;
1100  }
1101 
1102  vsprintf ((char*) (real->str + (real->len - len)),
1103  format, args_copy);
1104 
1105  va_end (args_copy);
1106 
1107  return TRUE;
1108 }
1109 
1120  const char *format,
1121  ...)
1122 {
1123  va_list args;
1124  dbus_bool_t retval;
1125 
1126  va_start (args, format);
1127  retval = _dbus_string_append_printf_valist (str, format, args);
1128  va_end (args);
1129 
1130  return retval;
1131 }
1132 
1143  const char *buffer,
1144  int len)
1145 {
1146  DBUS_STRING_PREAMBLE (str);
1147  _dbus_assert (buffer != NULL);
1148  _dbus_assert (len >= 0);
1149 
1150  return append (real, buffer, len);
1151 }
1152 
1163  unsigned char byte)
1164 {
1165  DBUS_STRING_PREAMBLE (str);
1166 
1167  if (!set_length (real, real->len + 1))
1168  return FALSE;
1169 
1170  real->str[real->len-1] = byte;
1171 
1172  return TRUE;
1173 }
1174 
1175 static void
1176 delete (DBusRealString *real,
1177  int start,
1178  int len)
1179 {
1180  if (len == 0)
1181  return;
1182 
1183  memmove (real->str + start, real->str + start + len, real->len - (start + len));
1184  real->len -= len;
1185  real->str[real->len] = '\0';
1186 }
1187 
1197 void
1199  int start,
1200  int len)
1201 {
1202  DBUS_STRING_PREAMBLE (str);
1203  _dbus_assert (start >= 0);
1204  _dbus_assert (len >= 0);
1205  _dbus_assert (start <= real->len);
1206  _dbus_assert (len <= real->len - start);
1207 
1208  delete (real, start, len);
1209 }
1210 
1211 static dbus_bool_t
1212 copy (DBusRealString *source,
1213  int start,
1214  int len,
1215  DBusRealString *dest,
1216  int insert_at)
1217 {
1218  if (len == 0)
1219  return TRUE;
1220 
1221  if (!open_gap (len, dest, insert_at))
1222  return FALSE;
1223 
1224  memmove (dest->str + insert_at,
1225  source->str + start,
1226  len);
1227 
1228  return TRUE;
1229 }
1230 
1240 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1241  DBusRealString *real_source = (DBusRealString*) source; \
1242  DBusRealString *real_dest = (DBusRealString*) dest; \
1243  _dbus_assert ((source) != (dest)); \
1244  DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1245  DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1246  _dbus_assert (!real_dest->constant); \
1247  _dbus_assert (!real_dest->locked); \
1248  _dbus_assert ((start) >= 0); \
1249  _dbus_assert ((start) <= real_source->len); \
1250  _dbus_assert ((insert_at) >= 0); \
1251  _dbus_assert ((insert_at) <= real_dest->len)
1252 
1265  int start,
1266  DBusString *dest,
1267  int insert_at)
1268 {
1269  DBusRealString *real_source = (DBusRealString*) source;
1270  _dbus_assert (start <= real_source->len);
1271 
1272  return _dbus_string_move_len (source, start,
1273  real_source->len - start,
1274  dest, insert_at);
1275 }
1276 
1289  int start,
1290  DBusString *dest,
1291  int insert_at)
1292 {
1293  DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1294 
1295  return copy (real_source, start,
1296  real_source->len - start,
1297  real_dest,
1298  insert_at);
1299 }
1300 
1314  int start,
1315  int len,
1316  DBusString *dest,
1317  int insert_at)
1318 
1319 {
1320  DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1321  _dbus_assert (len >= 0);
1322  _dbus_assert ((start + len) <= real_source->len);
1323 
1324 
1325  if (len == 0)
1326  {
1327  return TRUE;
1328  }
1329  else if (start == 0 &&
1330  len == real_source->len &&
1331  real_dest->len == 0)
1332  {
1333  /* Short-circuit moving an entire existing string to an empty string
1334  * by just swapping the buffers.
1335  */
1336  /* we assume ->constant doesn't matter as you can't have
1337  * a constant string involved in a move.
1338  */
1339 #define ASSIGN_DATA(a, b) do { \
1340  (a)->str = (b)->str; \
1341  (a)->len = (b)->len; \
1342  (a)->allocated = (b)->allocated; \
1343  (a)->align_offset = (b)->align_offset; \
1344  } while (0)
1345 
1346  DBusRealString tmp;
1347 
1348  ASSIGN_DATA (&tmp, real_source);
1349  ASSIGN_DATA (real_source, real_dest);
1350  ASSIGN_DATA (real_dest, &tmp);
1351 
1352  return TRUE;
1353  }
1354  else
1355  {
1356  if (!copy (real_source, start, len,
1357  real_dest,
1358  insert_at))
1359  return FALSE;
1360 
1361  delete (real_source, start,
1362  len);
1363 
1364  return TRUE;
1365  }
1366 }
1367 
1381  int start,
1382  int len,
1383  DBusString *dest,
1384  int insert_at)
1385 {
1386  DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1387  _dbus_assert (len >= 0);
1388  _dbus_assert (start <= real_source->len);
1389  _dbus_assert (len <= real_source->len - start);
1390 
1391  return copy (real_source, start, len,
1392  real_dest,
1393  insert_at);
1394 }
1395 
1410  int start,
1411  int len,
1412  DBusString *dest,
1413  int replace_at,
1414  int replace_len)
1415 {
1416  DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1417  _dbus_assert (len >= 0);
1418  _dbus_assert (start <= real_source->len);
1419  _dbus_assert (len <= real_source->len - start);
1420  _dbus_assert (replace_at >= 0);
1421  _dbus_assert (replace_at <= real_dest->len);
1422  _dbus_assert (replace_len <= real_dest->len - replace_at);
1423 
1424  if (len == replace_len)
1425  {
1426  memmove (real_dest->str + replace_at,
1427  real_source->str + start, len);
1428  }
1429  else if (len < replace_len)
1430  {
1431  memmove (real_dest->str + replace_at,
1432  real_source->str + start, len);
1433  delete (real_dest, replace_at + len,
1434  replace_len - len);
1435  }
1436  else
1437  {
1438  int diff;
1439 
1440  _dbus_assert (len > replace_len);
1441 
1442  diff = len - replace_len;
1443 
1444  /* First of all we check if destination string can be enlarged as
1445  * required, then we overwrite previous bytes
1446  */
1447 
1448  if (!copy (real_source, start + replace_len, diff,
1449  real_dest, replace_at + replace_len))
1450  return FALSE;
1451 
1452  memmove (real_dest->str + replace_at,
1453  real_source->str + start, replace_len);
1454  }
1455 
1456  return TRUE;
1457 }
1458 
1473  unsigned char byte,
1474  DBusString *tail)
1475 {
1476  int byte_position;
1477  char byte_string[2] = "";
1478  int head_length;
1479  int tail_length;
1480 
1481  byte_string[0] = (char) byte;
1482 
1483  if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1484  return FALSE;
1485 
1486  head_length = byte_position;
1487  tail_length = _dbus_string_get_length (source) - head_length - 1;
1488 
1489  if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1490  tail, 0))
1491  return FALSE;
1492 
1493  /* remove the trailing delimiter byte from the head now.
1494  */
1495  if (!_dbus_string_set_length (source, head_length))
1496  return FALSE;
1497 
1498  return TRUE;
1499 }
1500 
1501 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1502  * Pennington, and Tom Tromey are the authors and authorized relicense.
1503  */
1504 
1510 #define UTF8_COMPUTE(Char, Mask, Len) \
1511  if (Char < 128) \
1512  { \
1513  Len = 1; \
1514  Mask = 0x7f; \
1515  } \
1516  else if ((Char & 0xe0) == 0xc0) \
1517  { \
1518  Len = 2; \
1519  Mask = 0x1f; \
1520  } \
1521  else if ((Char & 0xf0) == 0xe0) \
1522  { \
1523  Len = 3; \
1524  Mask = 0x0f; \
1525  } \
1526  else if ((Char & 0xf8) == 0xf0) \
1527  { \
1528  Len = 4; \
1529  Mask = 0x07; \
1530  } \
1531  else if ((Char & 0xfc) == 0xf8) \
1532  { \
1533  Len = 5; \
1534  Mask = 0x03; \
1535  } \
1536  else if ((Char & 0xfe) == 0xfc) \
1537  { \
1538  Len = 6; \
1539  Mask = 0x01; \
1540  } \
1541  else \
1542  { \
1543  Len = 0; \
1544  Mask = 0; \
1545  }
1546 
1551 #define UTF8_LENGTH(Char) \
1552  ((Char) < 0x80 ? 1 : \
1553  ((Char) < 0x800 ? 2 : \
1554  ((Char) < 0x10000 ? 3 : \
1555  ((Char) < 0x200000 ? 4 : \
1556  ((Char) < 0x4000000 ? 5 : 6)))))
1557 
1567 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1568  (Result) = (Chars)[0] & (Mask); \
1569  for ((Count) = 1; (Count) < (Len); ++(Count)) \
1570  { \
1571  if (((Chars)[(Count)] & 0xc0) != 0x80) \
1572  { \
1573  (Result) = -1; \
1574  break; \
1575  } \
1576  (Result) <<= 6; \
1577  (Result) |= ((Chars)[(Count)] & 0x3f); \
1578  }
1579 
1590 #define UNICODE_VALID(Char) \
1591  ((Char) < 0x110000 && \
1592  (((Char) & 0xFFFFF800) != 0xD800))
1593 
1610  int start,
1611  const char *substr,
1612  int *found)
1613 {
1614  return _dbus_string_find_to (str, start,
1615  ((const DBusRealString*)str)->len,
1616  substr, found);
1617 }
1618 
1633  int start,
1634  int *found,
1635  int *found_len)
1636 {
1637  int i;
1638 
1640  _dbus_assert (start <= real->len);
1641  _dbus_assert (start >= 0);
1642 
1643  i = start;
1644  while (i < real->len)
1645  {
1646  if (real->str[i] == '\r')
1647  {
1648  if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1649  {
1650  if (found)
1651  *found = i;
1652  if (found_len)
1653  *found_len = 2;
1654  return TRUE;
1655  }
1656  else /* only "\r" */
1657  {
1658  if (found)
1659  *found = i;
1660  if (found_len)
1661  *found_len = 1;
1662  return TRUE;
1663  }
1664  }
1665  else if (real->str[i] == '\n') /* only "\n" */
1666  {
1667  if (found)
1668  *found = i;
1669  if (found_len)
1670  *found_len = 1;
1671  return TRUE;
1672  }
1673  ++i;
1674  }
1675 
1676  if (found)
1677  *found = real->len;
1678 
1679  if (found_len)
1680  *found_len = 0;
1681 
1682  return FALSE;
1683 }
1684 
1703  int start,
1704  int end,
1705  const char *substr,
1706  int *found)
1707 {
1708  int i;
1710  _dbus_assert (substr != NULL);
1711  _dbus_assert (start <= real->len);
1712  _dbus_assert (start >= 0);
1713  _dbus_assert (substr != NULL);
1714  _dbus_assert (end <= real->len);
1715  _dbus_assert (start <= end);
1716 
1717  /* we always "find" an empty string */
1718  if (*substr == '\0')
1719  {
1720  if (found)
1721  *found = start;
1722  return TRUE;
1723  }
1724 
1725  i = start;
1726  while (i < end)
1727  {
1728  if (real->str[i] == substr[0])
1729  {
1730  int j = i + 1;
1731 
1732  while (j < end)
1733  {
1734  if (substr[j - i] == '\0')
1735  break;
1736  else if (real->str[j] != substr[j - i])
1737  break;
1738 
1739  ++j;
1740  }
1741 
1742  if (substr[j - i] == '\0')
1743  {
1744  if (found)
1745  *found = i;
1746  return TRUE;
1747  }
1748  }
1749 
1750  ++i;
1751  }
1752 
1753  if (found)
1754  *found = end;
1755 
1756  return FALSE;
1757 }
1758 
1771  int start,
1772  int *found)
1773 {
1774  int i;
1776  _dbus_assert (start <= real->len);
1777  _dbus_assert (start >= 0);
1778 
1779  i = start;
1780  while (i < real->len)
1781  {
1782  if (real->str[i] == ' ' ||
1783  real->str[i] == '\t')
1784  {
1785  if (found)
1786  *found = i;
1787  return TRUE;
1788  }
1789 
1790  ++i;
1791  }
1792 
1793  if (found)
1794  *found = real->len;
1795 
1796  return FALSE;
1797 }
1798 
1807 void
1809  int start,
1810  int *end)
1811 {
1812  int i;
1814  _dbus_assert (start <= real->len);
1815  _dbus_assert (start >= 0);
1816 
1817  i = start;
1818  while (i < real->len)
1819  {
1820  if (!DBUS_IS_ASCII_BLANK (real->str[i]))
1821  break;
1822 
1823  ++i;
1824  }
1825 
1826  _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
1827 
1828  if (end)
1829  *end = i;
1830 }
1831 
1832 
1841 void
1843  int start,
1844  int *end)
1845 {
1846  int i;
1848  _dbus_assert (start <= real->len);
1849  _dbus_assert (start >= 0);
1850 
1851  i = start;
1852  while (i < real->len)
1853  {
1854  if (!DBUS_IS_ASCII_WHITE (real->str[i]))
1855  break;
1856 
1857  ++i;
1858  }
1859 
1860  _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
1861 
1862  if (end)
1863  *end = i;
1864 }
1865 
1874 void
1876  int end,
1877  int *start)
1878 {
1879  int i;
1881  _dbus_assert (end <= real->len);
1882  _dbus_assert (end >= 0);
1883 
1884  i = end;
1885  while (i > 0)
1886  {
1887  if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
1888  break;
1889  --i;
1890  }
1891 
1892  _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
1893 
1894  if (start)
1895  *start = i;
1896 }
1897 
1915  DBusString *dest)
1916 {
1917  int eol, eol_len;
1918 
1919  _dbus_string_set_length (dest, 0);
1920 
1921  eol = 0;
1922  eol_len = 0;
1923  if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
1924  {
1925  _dbus_assert (eol == _dbus_string_get_length (source));
1926  if (eol == 0)
1927  {
1928  /* If there's no newline and source has zero length, we're done */
1929  return FALSE;
1930  }
1931  /* otherwise, the last line of the file has no eol characters */
1932  }
1933 
1934  /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
1935  * since find_eol returned TRUE
1936  */
1937 
1938  if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
1939  return FALSE;
1940 
1941  /* remove line ending */
1942  if (!_dbus_string_set_length (dest, eol))
1943  {
1944  _dbus_assert_not_reached ("out of memory when shortening a string");
1945  return FALSE;
1946  }
1947 
1948  return TRUE;
1949 }
1950 
1951 #ifdef DBUS_BUILD_TESTS
1952 
1958 void
1959 _dbus_string_delete_first_word (DBusString *str)
1960 {
1961  int i;
1962 
1963  if (_dbus_string_find_blank (str, 0, &i))
1964  _dbus_string_skip_blank (str, i, &i);
1965 
1966  _dbus_string_delete (str, 0, i);
1967 }
1968 #endif
1969 
1970 #ifdef DBUS_BUILD_TESTS
1971 
1976 void
1977 _dbus_string_delete_leading_blanks (DBusString *str)
1978 {
1979  int i;
1980 
1981  _dbus_string_skip_blank (str, 0, &i);
1982 
1983  if (i > 0)
1984  _dbus_string_delete (str, 0, i);
1985 }
1986 #endif
1987 
1993 void
1995 {
1996  int i;
1997 
1998  _dbus_string_skip_white (str, 0, &i);
1999 
2000  if (i > 0)
2001  _dbus_string_delete (str, 0, i);
2002 
2004 
2005  _dbus_string_set_length (str, i);
2006 }
2007 
2019  const DBusString *b)
2020 {
2021  const unsigned char *ap;
2022  const unsigned char *bp;
2023  const unsigned char *a_end;
2024  const DBusRealString *real_a = (const DBusRealString*) a;
2025  const DBusRealString *real_b = (const DBusRealString*) b;
2028 
2029  if (real_a->len != real_b->len)
2030  return FALSE;
2031 
2032  ap = real_a->str;
2033  bp = real_b->str;
2034  a_end = real_a->str + real_a->len;
2035  while (ap != a_end)
2036  {
2037  if (*ap != *bp)
2038  return FALSE;
2039 
2040  ++ap;
2041  ++bp;
2042  }
2043 
2044  return TRUE;
2045 }
2046 
2062  const DBusString *b,
2063  int len)
2064 {
2065  const unsigned char *ap;
2066  const unsigned char *bp;
2067  const unsigned char *a_end;
2068  const DBusRealString *real_a = (const DBusRealString*) a;
2069  const DBusRealString *real_b = (const DBusRealString*) b;
2072 
2073  if (real_a->len != real_b->len &&
2074  (real_a->len < len || real_b->len < len))
2075  return FALSE;
2076 
2077  ap = real_a->str;
2078  bp = real_b->str;
2079  a_end = real_a->str + MIN (real_a->len, len);
2080  while (ap != a_end)
2081  {
2082  if (*ap != *bp)
2083  return FALSE;
2084 
2085  ++ap;
2086  ++bp;
2087  }
2088 
2089  return TRUE;
2090 }
2091 
2110  int a_start,
2111  int a_len,
2112  const DBusString *b,
2113  int b_start)
2114 {
2115  const unsigned char *ap;
2116  const unsigned char *bp;
2117  const unsigned char *a_end;
2118  const DBusRealString *real_a = (const DBusRealString*) a;
2119  const DBusRealString *real_b = (const DBusRealString*) b;
2122  _dbus_assert (a_start >= 0);
2123  _dbus_assert (a_len >= 0);
2124  _dbus_assert (a_start <= real_a->len);
2125  _dbus_assert (a_len <= real_a->len - a_start);
2126  _dbus_assert (b_start >= 0);
2127  _dbus_assert (b_start <= real_b->len);
2128 
2129  if (a_len > real_b->len - b_start)
2130  return FALSE;
2131 
2132  ap = real_a->str + a_start;
2133  bp = real_b->str + b_start;
2134  a_end = ap + a_len;
2135  while (ap != a_end)
2136  {
2137  if (*ap != *bp)
2138  return FALSE;
2139 
2140  ++ap;
2141  ++bp;
2142  }
2143 
2144  _dbus_assert (bp <= (real_b->str + real_b->len));
2145 
2146  return TRUE;
2147 }
2148 
2158  const char *c_str)
2159 {
2160  const unsigned char *ap;
2161  const unsigned char *bp;
2162  const unsigned char *a_end;
2163  const DBusRealString *real_a = (const DBusRealString*) a;
2165  _dbus_assert (c_str != NULL);
2166 
2167  ap = real_a->str;
2168  bp = (const unsigned char*) c_str;
2169  a_end = real_a->str + real_a->len;
2170  while (ap != a_end && *bp)
2171  {
2172  if (*ap != *bp)
2173  return FALSE;
2174 
2175  ++ap;
2176  ++bp;
2177  }
2178 
2179  if (ap != a_end || *bp)
2180  return FALSE;
2181 
2182  return TRUE;
2183 }
2184 
2194  const char *c_str)
2195 {
2196  const unsigned char *ap;
2197  const unsigned char *bp;
2198  const unsigned char *a_end;
2199  const DBusRealString *real_a = (const DBusRealString*) a;
2201  _dbus_assert (c_str != NULL);
2202 
2203  ap = real_a->str;
2204  bp = (const unsigned char*) c_str;
2205  a_end = real_a->str + real_a->len;
2206  while (ap != a_end && *bp)
2207  {
2208  if (*ap != *bp)
2209  return FALSE;
2210 
2211  ++ap;
2212  ++bp;
2213  }
2214 
2215  if (*bp == '\0')
2216  return TRUE;
2217  else
2218  return FALSE;
2219 }
2220 
2231  unsigned char byte)
2232 {
2233  const char hexdigits[16] = {
2234  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2235  'a', 'b', 'c', 'd', 'e', 'f'
2236  };
2237 
2238  if (!_dbus_string_append_byte (str,
2239  hexdigits[(byte >> 4)]))
2240  return FALSE;
2241 
2242  if (!_dbus_string_append_byte (str,
2243  hexdigits[(byte & 0x0f)]))
2244  {
2246  _dbus_string_get_length (str) - 1);
2247  return FALSE;
2248  }
2249 
2250  return TRUE;
2251 }
2252 
2265  int start,
2266  DBusString *dest,
2267  int insert_at)
2268 {
2269  DBusString result;
2270  const unsigned char *p;
2271  const unsigned char *end;
2272  dbus_bool_t retval;
2273 
2274  _dbus_assert (start <= _dbus_string_get_length (source));
2275 
2276  if (!_dbus_string_init (&result))
2277  return FALSE;
2278 
2279  retval = FALSE;
2280 
2281  p = (const unsigned char*) _dbus_string_get_const_data (source);
2282  end = p + _dbus_string_get_length (source);
2283  p += start;
2284 
2285  while (p != end)
2286  {
2287  if (!_dbus_string_append_byte_as_hex (&result, *p))
2288  goto out;
2289 
2290  ++p;
2291  }
2292 
2293  if (!_dbus_string_move (&result, 0, dest, insert_at))
2294  goto out;
2295 
2296  retval = TRUE;
2297 
2298  out:
2299  _dbus_string_free (&result);
2300  return retval;
2301 }
2302 
2315  int start,
2316  int *end_return,
2317  DBusString *dest,
2318  int insert_at)
2319 {
2320  DBusString result;
2321  const unsigned char *p;
2322  const unsigned char *end;
2323  dbus_bool_t retval;
2324  dbus_bool_t high_bits;
2325 
2326  _dbus_assert (start <= _dbus_string_get_length (source));
2327 
2328  if (!_dbus_string_init (&result))
2329  return FALSE;
2330 
2331  retval = FALSE;
2332 
2333  high_bits = TRUE;
2334  p = (const unsigned char*) _dbus_string_get_const_data (source);
2335  end = p + _dbus_string_get_length (source);
2336  p += start;
2337 
2338  while (p != end)
2339  {
2340  unsigned int val;
2341 
2342  switch (*p)
2343  {
2344  case '0':
2345  val = 0;
2346  break;
2347  case '1':
2348  val = 1;
2349  break;
2350  case '2':
2351  val = 2;
2352  break;
2353  case '3':
2354  val = 3;
2355  break;
2356  case '4':
2357  val = 4;
2358  break;
2359  case '5':
2360  val = 5;
2361  break;
2362  case '6':
2363  val = 6;
2364  break;
2365  case '7':
2366  val = 7;
2367  break;
2368  case '8':
2369  val = 8;
2370  break;
2371  case '9':
2372  val = 9;
2373  break;
2374  case 'a':
2375  case 'A':
2376  val = 10;
2377  break;
2378  case 'b':
2379  case 'B':
2380  val = 11;
2381  break;
2382  case 'c':
2383  case 'C':
2384  val = 12;
2385  break;
2386  case 'd':
2387  case 'D':
2388  val = 13;
2389  break;
2390  case 'e':
2391  case 'E':
2392  val = 14;
2393  break;
2394  case 'f':
2395  case 'F':
2396  val = 15;
2397  break;
2398  default:
2399  goto done;
2400  }
2401 
2402  if (high_bits)
2403  {
2404  if (!_dbus_string_append_byte (&result,
2405  val << 4))
2406  goto out;
2407  }
2408  else
2409  {
2410  int len;
2411  unsigned char b;
2412 
2413  len = _dbus_string_get_length (&result);
2414 
2415  b = _dbus_string_get_byte (&result, len - 1);
2416 
2417  b |= val;
2418 
2419  _dbus_string_set_byte (&result, len - 1, b);
2420  }
2421 
2422  high_bits = !high_bits;
2423 
2424  ++p;
2425  }
2426 
2427  done:
2428  if (!_dbus_string_move (&result, 0, dest, insert_at))
2429  goto out;
2430 
2431  if (end_return)
2432  *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2433 
2434  retval = TRUE;
2435 
2436  out:
2437  _dbus_string_free (&result);
2438  return retval;
2439 }
2440 
2456  int start,
2457  int len)
2458 {
2459  const unsigned char *s;
2460  const unsigned char *end;
2462  _dbus_assert (start >= 0);
2463  _dbus_assert (start <= real->len);
2464  _dbus_assert (len >= 0);
2465 
2466  if (len > real->len - start)
2467  return FALSE;
2468 
2469  s = real->str + start;
2470  end = s + len;
2471  while (s != end)
2472  {
2473  if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2474  return FALSE;
2475 
2476  ++s;
2477  }
2478 
2479  return TRUE;
2480 }
2481 
2489 void
2491  int start,
2492  int len)
2493 {
2494  unsigned char *s;
2495  unsigned char *end;
2496  DBUS_STRING_PREAMBLE (str);
2497  _dbus_assert (start >= 0);
2498  _dbus_assert (start <= real->len);
2499  _dbus_assert (len >= 0);
2500  _dbus_assert (len <= real->len - start);
2501 
2502  s = real->str + start;
2503  end = s + len;
2504 
2505  while (s != end)
2506  {
2507  if (*s >= 'A' && *s <= 'Z')
2508  *s += 'a' - 'A';
2509  ++s;
2510  }
2511 }
2512 
2520 void
2522  int start,
2523  int len)
2524 {
2525  unsigned char *s;
2526  unsigned char *end;
2527  DBUS_STRING_PREAMBLE (str);
2528  _dbus_assert (start >= 0);
2529  _dbus_assert (start <= real->len);
2530  _dbus_assert (len >= 0);
2531  _dbus_assert (len <= real->len - start);
2532 
2533  s = real->str + start;
2534  end = s + len;
2535 
2536  while (s != end)
2537  {
2538  if (*s >= 'a' && *s <= 'z')
2539  *s += 'A' - 'a';
2540  ++s;
2541  }
2542 }
2543 
2561  int start,
2562  int len)
2563 {
2564  const unsigned char *p;
2565  const unsigned char *end;
2567  _dbus_assert (start >= 0);
2568  _dbus_assert (start <= real->len);
2569  _dbus_assert (len >= 0);
2570 
2571  /* we are doing _DBUS_UNLIKELY() here which might be
2572  * dubious in a generic library like GLib, but in D-Bus
2573  * we know we're validating messages and that it would
2574  * only be evil/broken apps that would have invalid
2575  * UTF-8. Also, this function seems to be a performance
2576  * bottleneck in profiles.
2577  */
2578 
2579  if (_DBUS_UNLIKELY (len > real->len - start))
2580  return FALSE;
2581 
2582  p = real->str + start;
2583  end = p + len;
2584 
2585  while (p < end)
2586  {
2587  int i, mask, char_len;
2588  dbus_unichar_t result;
2589 
2590  /* nul bytes considered invalid */
2591  if (*p == '\0')
2592  break;
2593 
2594  /* Special-case ASCII; this makes us go a lot faster in
2595  * D-Bus profiles where we are typically validating
2596  * function names and such. We have to know that
2597  * all following checks will pass for ASCII though,
2598  * comments follow ...
2599  */
2600  if (*p < 128)
2601  {
2602  ++p;
2603  continue;
2604  }
2605 
2606  UTF8_COMPUTE (*p, mask, char_len);
2607 
2608  if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2609  break;
2610 
2611  /* check that the expected number of bytes exists in the remaining length */
2612  if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2613  break;
2614 
2615  UTF8_GET (result, p, i, mask, char_len);
2616 
2617  /* Check for overlong UTF-8 */
2618  if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2619  break;
2620 #if 0
2621  /* The UNICODE_VALID check below will catch this */
2622  if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2623  break;
2624 #endif
2625 
2626  if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2627  break;
2628 
2629  /* UNICODE_VALID should have caught it */
2630  _dbus_assert (result != (dbus_unichar_t)-1);
2631 
2632  p += char_len;
2633  }
2634 
2635  /* See that we covered the entire length if a length was
2636  * passed in
2637  */
2638  if (_DBUS_UNLIKELY (p != end))
2639  return FALSE;
2640  else
2641  return TRUE;
2642 }
2643 
2659  int start,
2660  int len)
2661 {
2662  const unsigned char *s;
2663  const unsigned char *end;
2665  _dbus_assert (start >= 0);
2666  _dbus_assert (len >= 0);
2667  _dbus_assert (start <= real->len);
2668 
2669  if (len > real->len - start)
2670  return FALSE;
2671 
2672  s = real->str + start;
2673  end = s + len;
2674  while (s != end)
2675  {
2676  if (_DBUS_UNLIKELY (*s != '\0'))
2677  return FALSE;
2678  ++s;
2679  }
2680 
2681  return TRUE;
2682 }
2683 
2689 void
2691 {
2692  DBUS_STRING_PREAMBLE (str);
2693 
2694  memset (real->str - real->align_offset, '\0', real->allocated);
2695 }
2698 /* tests are in dbus-string-util.c */
#define DBUS_IS_ASCII_WHITE(c)
Checks for ASCII whitespace byte.
dbus_bool_t _dbus_string_insert_bytes(DBusString *str, int i, int n_bytes, unsigned char byte)
Inserts a number of bytes of a given value at the given position.
Definition: dbus-string.c:570
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:921
void _dbus_string_copy_to_buffer(const DBusString *str, char *buffer, int avail_len)
Copies the contents of a DBusString into a different buffer.
Definition: dbus-string.c:683
#define NULL
A null pointer, defined appropriately for C or C++.
#define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at)
Checks assertions for two strings we&#39;re copying a segment between, and declares real_source/real_dest...
Definition: dbus-string.c:1240
Internals of DBusString.
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2018
dbus_bool_t _dbus_string_find_eol(const DBusString *str, int start, int *found, int *found_len)
Finds end of line (&quot;\r\n&quot; or &quot;\n&quot;) in the string, returning TRUE and filling in the byte index where ...
Definition: dbus-string.c:1632
dbus_bool_t _dbus_string_lengthen(DBusString *str, int additional_length)
Makes a string longer by the given number of bytes.
Definition: dbus-string.c:746
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:600
#define ASSIGN_4_OCTETS(p, octets)
assign 4 bytes from one string to another
Definition: dbus-string.c:941
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
void _dbus_string_copy_to_buffer_with_nul(const DBusString *str, char *buffer, int avail_len)
Copies the contents of a DBusString into a different buffer.
Definition: dbus-string.c:705
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
Definition: dbus-string.c:2264
#define ASSIGN_8_OCTETS(p, octets)
assign 8 bytes from one string to another
Definition: dbus-string.c:950
dbus_bool_t _dbus_string_starts_with_c_str(const DBusString *a, const char *c_str)
Checks whether a string starts with the given C string.
Definition: dbus-string.c:2193
void _dbus_string_chop_white(DBusString *str)
Deletes leading and trailing whitespace.
Definition: dbus-string.c:1994
dbus_bool_t _dbus_string_insert_8_aligned(DBusString *str, int insert_at, const unsigned char octets[8])
Inserts 8 bytes aligned on an 8 byte boundary with any alignment padding initialized to 0...
Definition: dbus-string.c:1026
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void _dbus_string_tolower_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to lower case.
Definition: dbus-string.c:2490
unsigned int invalid
DBusString is invalid (e.g.
#define DBUS_CONST_STRING_PREAMBLE(str)
Checks assertions about a string that may be const or locked.
unsigned char _dbus_string_get_byte(const DBusString *str, int start)
Gets the byte at the given position.
Definition: dbus-string.c:548
char * _dbus_string_get_data_len(DBusString *str, int start, int len)
Gets a sub-portion of the raw character buffer from the string.
Definition: dbus-string.c:476
#define _DBUS_STRING_MAX_LENGTH
The maximum length of a DBusString.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_bool_t _dbus_string_append_printf_valist(DBusString *str, const char *format, va_list args)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1078
void _dbus_string_shorten(DBusString *str, int length_to_remove)
Makes a string shorter by the given number of bytes.
Definition: dbus-string.c:766
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:1288
#define UTF8_GET(Result, Chars, Count, Mask, Len)
Gets a UTF-8 value.
Definition: dbus-string.c:1567
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
Definition: dbus-string.c:1609
void _dbus_string_skip_white(const DBusString *str, int start, int *end)
Skips whitespace from start, storing the first non-whitespace in *end.
Definition: dbus-string.c:1842
#define ASSIGN_2_OCTETS(p, octets)
assign 2 bytes from one string to another
Definition: dbus-string.c:937
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
Definition: dbus-string.c:1264
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:460
#define UNICODE_VALID(Char)
Check whether a Unicode (5.2) char is in a valid range.
Definition: dbus-string.c:1590
dbus_bool_t _dbus_string_equal_c_str(const DBusString *a, const char *c_str)
Checks whether a string is equal to a C string.
Definition: dbus-string.c:2157
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
Definition: dbus-string.c:375
dbus_bool_t _dbus_string_copy_data(const DBusString *str, char **data_return)
Copies the data from the string into a char*.
Definition: dbus-string.c:658
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
char * _dbus_string_get_data(DBusString *str)
Gets the raw character buffer from the string.
Definition: dbus-string.c:437
#define DBUS_GENERIC_STRING_PREAMBLE(real)
Checks a bunch of assertions about a string object.
dbus_bool_t _dbus_string_replace_len(const DBusString *source, int start, int len, DBusString *dest, int replace_at, int replace_len)
Replaces a segment of dest string with a segment of source string.
Definition: dbus-string.c:1409
#define DBUS_STRING_PREAMBLE(str)
Checks assertions about a string object that needs to be modifiable - may not be locked or const...
void _dbus_string_skip_blank(const DBusString *str, int start, int *end)
Skips blanks from start, storing the first non-blank in *end (blank is space or tab).
Definition: dbus-string.c:1808
#define UTF8_LENGTH(Char)
computes length of a unicode character in UTF-8
Definition: dbus-string.c:1551
dbus_bool_t _dbus_string_pop_line(DBusString *source, DBusString *dest)
Assigns a newline-terminated or \r\n-terminated line from the front of the string to the given dest s...
Definition: dbus-string.c:1914
void _dbus_string_delete(DBusString *str, int start, int len)
Deletes a segment of a DBusString with length len starting at start.
Definition: dbus-string.c:1198
dbus_bool_t _dbus_string_init_preallocated(DBusString *str, int allocate_size)
Initializes a string that can be up to the given allocation size before it has to realloc...
Definition: dbus-string.c:132
int len
Length without nul.
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1119
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:725
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
Definition: dbus-string.c:2690
dbus_bool_t _dbus_string_validate_utf8(const DBusString *str, int start, int len)
Checks that the given range of the string is valid UTF-8.
Definition: dbus-string.c:2560
dbus_bool_t _dbus_string_align_length(DBusString *str, int alignment)
Align the length of a string to a specific alignment (typically 4 or 8) by appending nul bytes to the...
Definition: dbus-string.c:869
dbus_bool_t _dbus_string_equal_substring(const DBusString *a, int a_start, int a_len, const DBusString *b, int b_start)
Tests two sub-parts of two DBusString for equality.
Definition: dbus-string.c:2109
dbus_bool_t _dbus_string_equal_len(const DBusString *a, const DBusString *b, int len)
Tests two DBusString for equality up to the given length.
Definition: dbus-string.c:2061
unsigned int locked
DBusString has been locked and can&#39;t be changed.
dbus_bool_t _dbus_string_insert_byte(DBusString *str, int i, unsigned char byte)
Inserts a single byte at the given position.
Definition: dbus-string.c:600
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition: dbus-string.c:1162
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define TRUE
Expands to &quot;1&quot;.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
int allocated
Allocated size of data.
#define DBUS_IS_ASCII_BLANK(c)
Checks for ASCII blank byte.
dbus_bool_t _dbus_string_find_blank(const DBusString *str, int start, int *found)
Finds a blank (space or tab) in the string.
Definition: dbus-string.c:1770
void _dbus_string_set_byte(DBusString *str, int i, unsigned char byte)
Sets the value of the byte at the given position.
Definition: dbus-string.c:524
dbus_bool_t _dbus_string_alloc_space(DBusString *str, int extra_bytes)
Preallocate extra_bytes such that a future lengthening of the string by extra_bytes is guaranteed to ...
Definition: dbus-string.c:885
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
Definition: dbus-string.c:2314
unsigned int align_offset
str - align_offset is the actual malloc block
const char * _dbus_string_get_const_data_len(const DBusString *str, int start, int len)
const version of _dbus_string_get_data_len().
Definition: dbus-string.c:500
dbus_bool_t _dbus_string_append_byte_as_hex(DBusString *str, unsigned char byte)
Appends a two-character hex digit to a string, where the hex digit has the value of the given byte...
Definition: dbus-string.c:2230
dbus_bool_t _dbus_string_append_len(DBusString *str, const char *buffer, int len)
Appends block of bytes with the given length to a DBusString.
Definition: dbus-string.c:1142
dbus_bool_t _dbus_string_find_to(const DBusString *str, int start, int end, const char *substr, int *found)
Finds the given substring in the string, up to a certain position, returning TRUE and filling in the ...
Definition: dbus-string.c:1702
unsigned char * str
String data, plus nul termination.
dbus_bool_t _dbus_string_validate_nul(const DBusString *str, int start, int len)
Checks that the given range of the string is all nul bytes.
Definition: dbus-string.c:2658
#define FALSE
Expands to &quot;0&quot;.
dbus_bool_t _dbus_string_insert_4_aligned(DBusString *str, int insert_at, const unsigned char octets[4])
Inserts 4 bytes aligned on a 4 byte boundary with any alignment padding initialized to 0...
Definition: dbus-string.c:1002
void _dbus_string_skip_white_reverse(const DBusString *str, int end, int *start)
Skips whitespace from end, storing the start index of the trailing whitespace in *start.
Definition: dbus-string.c:1875
#define DBUS_LOCKED_STRING_PREAMBLE(str)
Checks assertions about a string object that may be locked but can&#39;t be const.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:788
dbus_bool_t _dbus_string_copy_len(const DBusString *source, int start, int len, DBusString *dest, int insert_at)
Like _dbus_string_copy(), but can copy a segment from the middle of the source string.
Definition: dbus-string.c:1380
int _dbus_printf_string_upper_bound(const char *format, va_list args)
Measure the length of the given format string and arguments, not including the terminating nul...
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:627
void _dbus_string_toupper_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to upper case.
Definition: dbus-string.c:2521
#define UTF8_COMPUTE(Char, Mask, Len)
computes length and mask of a unicode character
Definition: dbus-string.c:1510
dbus_bool_t _dbus_string_insert_2_aligned(DBusString *str, int insert_at, const unsigned char octets[4])
Inserts 2 bytes aligned on a 2 byte boundary with any alignment padding initialized to 0...
Definition: dbus-string.c:978
dbus_bool_t _dbus_string_insert_alignment(DBusString *str, int *insert_at, int alignment)
Inserts padding at *insert_at such to align it to the given boundary.
Definition: dbus-string.c:1054
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:454
dbus_bool_t _dbus_string_validate_ascii(const DBusString *str, int start, int len)
Checks that the given range of the string is valid ASCII with no nul bytes.
Definition: dbus-string.c:2455
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.
Definition: dbus-string.c:210
dbus_bool_t _dbus_string_move_len(DBusString *source, int start, int len, DBusString *dest, int insert_at)
Like _dbus_string_move(), but can move a segment from the middle of the source string.
Definition: dbus-string.c:1313
unsigned int constant
String data is not owned by DBusString.
dbus_bool_t _dbus_string_split_on_byte(DBusString *source, unsigned char byte, DBusString *tail)
Looks for the first occurance of a byte, deletes that byte, and moves everything after the byte to th...
Definition: dbus-string.c:1472