00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "dbus-internals.h"
00026 #include "dbus-string.h"
00027
00028 #include <string.h>
00029
00030 #include <stdio.h>
00031 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
00032 #include "dbus-string-private.h"
00033 #include "dbus-marshal-basic.h"
00034
00035
00036
00037 #include "dbus-sysdeps.h"
00038
00077 static void
00078 fixup_alignment (DBusRealString *real)
00079 {
00080 unsigned char *aligned;
00081 unsigned char *real_block;
00082 unsigned int old_align_offset;
00083
00084
00085 _dbus_assert (real->len <= real->allocated - _DBUS_STRING_ALLOCATION_PADDING);
00086
00087 old_align_offset = real->align_offset;
00088 real_block = real->str - old_align_offset;
00089
00090 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
00091
00092 real->align_offset = aligned - real_block;
00093 real->str = aligned;
00094
00095 if (old_align_offset != real->align_offset)
00096 {
00097
00098 memmove (real_block + real->align_offset,
00099 real_block + old_align_offset,
00100 real->len + 1);
00101 }
00102
00103 _dbus_assert (real->align_offset < 8);
00104 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
00105 }
00106
00107 static void
00108 undo_alignment (DBusRealString *real)
00109 {
00110 if (real->align_offset != 0)
00111 {
00112 memmove (real->str - real->align_offset,
00113 real->str,
00114 real->len + 1);
00115
00116 real->str = real->str - real->align_offset;
00117 real->align_offset = 0;
00118 }
00119 }
00120
00130 dbus_bool_t
00131 _dbus_string_init_preallocated (DBusString *str,
00132 int allocate_size)
00133 {
00134 DBusRealString *real;
00135
00136 _dbus_assert (str != NULL);
00137
00138 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
00139
00140 real = (DBusRealString*) str;
00141
00142
00143
00144
00145
00146
00147
00148 real->str = dbus_malloc (_DBUS_STRING_ALLOCATION_PADDING + allocate_size);
00149 if (real->str == NULL)
00150 return FALSE;
00151
00152 real->allocated = _DBUS_STRING_ALLOCATION_PADDING + allocate_size;
00153 real->len = 0;
00154 real->str[real->len] = '\0';
00155
00156 real->max_length = _DBUS_STRING_MAX_MAX_LENGTH;
00157 real->constant = FALSE;
00158 real->locked = FALSE;
00159 real->invalid = FALSE;
00160 real->align_offset = 0;
00161
00162 fixup_alignment (real);
00163
00164 return TRUE;
00165 }
00166
00174 dbus_bool_t
00175 _dbus_string_init (DBusString *str)
00176 {
00177 return _dbus_string_init_preallocated (str, 0);
00178 }
00179
00180 #ifdef DBUS_BUILD_TESTS
00181
00182
00183
00184
00185
00186
00187 static void
00188 set_max_length (DBusString *str,
00189 int max_length)
00190 {
00191 DBusRealString *real;
00192
00193 real = (DBusRealString*) str;
00194
00195 real->max_length = max_length;
00196 }
00197 #endif
00198
00208 void
00209 _dbus_string_init_const (DBusString *str,
00210 const char *value)
00211 {
00212 _dbus_assert (value != NULL);
00213
00214 _dbus_string_init_const_len (str, value,
00215 strlen (value));
00216 }
00217
00228 void
00229 _dbus_string_init_const_len (DBusString *str,
00230 const char *value,
00231 int len)
00232 {
00233 DBusRealString *real;
00234
00235 _dbus_assert (str != NULL);
00236 _dbus_assert (len == 0 || value != NULL);
00237 _dbus_assert (len <= _DBUS_STRING_MAX_MAX_LENGTH);
00238 _dbus_assert (len >= 0);
00239
00240 real = (DBusRealString*) str;
00241
00242 real->str = (unsigned char*) value;
00243 real->len = len;
00244 real->allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
00245 real->max_length = real->len + 1;
00246 real->constant = TRUE;
00247 real->locked = TRUE;
00248 real->invalid = FALSE;
00249 real->align_offset = 0;
00250
00251
00252
00253
00254 }
00255
00261 void
00262 _dbus_string_free (DBusString *str)
00263 {
00264 DBusRealString *real = (DBusRealString*) str;
00265 DBUS_GENERIC_STRING_PREAMBLE (real);
00266
00267 if (real->constant)
00268 return;
00269 dbus_free (real->str - real->align_offset);
00270
00271 real->invalid = TRUE;
00272 }
00273
00274 static dbus_bool_t
00275 compact (DBusRealString *real,
00276 int max_waste)
00277 {
00278 unsigned char *new_str;
00279 int new_allocated;
00280 int waste;
00281
00282 waste = real->allocated - (real->len + _DBUS_STRING_ALLOCATION_PADDING);
00283
00284 if (waste <= max_waste)
00285 return TRUE;
00286
00287 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
00288
00289 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
00290 if (_DBUS_UNLIKELY (new_str == NULL))
00291 return FALSE;
00292
00293 real->str = new_str + real->align_offset;
00294 real->allocated = new_allocated;
00295 fixup_alignment (real);
00296
00297 return TRUE;
00298 }
00299
00300 #ifdef DBUS_BUILD_TESTS
00301
00302
00303
00313 void
00314 _dbus_string_lock (DBusString *str)
00315 {
00316 DBUS_LOCKED_STRING_PREAMBLE (str);
00317
00318 real->locked = TRUE;
00319
00320
00321
00322
00323 #define MAX_WASTE 48
00324 compact (real, MAX_WASTE);
00325 }
00326 #endif
00327
00328 static dbus_bool_t
00329 reallocate_for_length (DBusRealString *real,
00330 int new_length)
00331 {
00332 int new_allocated;
00333 unsigned char *new_str;
00334
00335
00336
00337
00338 if (real->allocated > (_DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
00339 new_allocated = _DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
00340 else
00341 new_allocated = real->allocated * 2;
00342
00343
00344
00345
00346
00347
00348
00349
00350 #ifdef DBUS_DISABLE_ASSERT
00351 #else
00352 #ifdef DBUS_BUILD_TESTS
00353 new_allocated = 0;
00354
00355
00356 #endif
00357 #endif
00358
00359
00360 new_allocated = MAX (new_allocated,
00361 new_length + _DBUS_STRING_ALLOCATION_PADDING);
00362
00363 _dbus_assert (new_allocated >= real->allocated);
00364 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
00365 if (_DBUS_UNLIKELY (new_str == NULL))
00366 return FALSE;
00367
00368 real->str = new_str + real->align_offset;
00369 real->allocated = new_allocated;
00370 fixup_alignment (real);
00371
00372 return TRUE;
00373 }
00374
00386 dbus_bool_t
00387 _dbus_string_compact (DBusString *str,
00388 int max_waste)
00389 {
00390 DBUS_STRING_PREAMBLE (str);
00391
00392 return compact (real, max_waste);
00393 }
00394
00395 static dbus_bool_t
00396 set_length (DBusRealString *real,
00397 int new_length)
00398 {
00399
00400
00401
00402 if (_DBUS_UNLIKELY (new_length > real->max_length))
00403 return FALSE;
00404 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
00405 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
00406 return FALSE;
00407 else
00408 {
00409 real->len = new_length;
00410 real->str[new_length] = '\0';
00411 return TRUE;
00412 }
00413 }
00414
00415 static dbus_bool_t
00416 open_gap (int len,
00417 DBusRealString *dest,
00418 int insert_at)
00419 {
00420 if (len == 0)
00421 return TRUE;
00422
00423 if (len > dest->max_length - dest->len)
00424 return FALSE;
00425
00426 if (!set_length (dest, dest->len + len))
00427 return FALSE;
00428
00429 memmove (dest->str + insert_at + len,
00430 dest->str + insert_at,
00431 dest->len - len - insert_at);
00432
00433 return TRUE;
00434 }
00435
00436 #ifndef _dbus_string_get_data
00437
00448 char*
00449 _dbus_string_get_data (DBusString *str)
00450 {
00451 DBUS_STRING_PREAMBLE (str);
00452
00453 return (char*) real->str;
00454 }
00455 #endif
00456
00457
00458 #ifndef _dbus_string_get_const_data
00459
00465 const char*
00466 _dbus_string_get_const_data (const DBusString *str)
00467 {
00468 DBUS_CONST_STRING_PREAMBLE (str);
00469
00470 return (const char*) real->str;
00471 }
00472 #endif
00473
00487 char*
00488 _dbus_string_get_data_len (DBusString *str,
00489 int start,
00490 int len)
00491 {
00492 DBUS_STRING_PREAMBLE (str);
00493 _dbus_assert (start >= 0);
00494 _dbus_assert (len >= 0);
00495 _dbus_assert (start <= real->len);
00496 _dbus_assert (len <= real->len - start);
00497
00498 return (char*) real->str + start;
00499 }
00500
00501
00502 #ifndef _dbus_string_get_const_data_len
00503
00511 const char*
00512 _dbus_string_get_const_data_len (const DBusString *str,
00513 int start,
00514 int len)
00515 {
00516 DBUS_CONST_STRING_PREAMBLE (str);
00517 _dbus_assert (start >= 0);
00518 _dbus_assert (len >= 0);
00519 _dbus_assert (start <= real->len);
00520 _dbus_assert (len <= real->len - start);
00521
00522 return (const char*) real->str + start;
00523 }
00524 #endif
00525
00526
00527 #ifndef _dbus_string_set_byte
00528
00535 void
00536 _dbus_string_set_byte (DBusString *str,
00537 int i,
00538 unsigned char byte)
00539 {
00540 DBUS_STRING_PREAMBLE (str);
00541 _dbus_assert (i < real->len);
00542 _dbus_assert (i >= 0);
00543
00544 real->str[i] = byte;
00545 }
00546 #endif
00547
00548
00549 #ifndef _dbus_string_get_byte
00550
00559 unsigned char
00560 _dbus_string_get_byte (const DBusString *str,
00561 int start)
00562 {
00563 DBUS_CONST_STRING_PREAMBLE (str);
00564 _dbus_assert (start <= real->len);
00565 _dbus_assert (start >= 0);
00566
00567 return real->str[start];
00568 }
00569 #endif
00570
00581 dbus_bool_t
00582 _dbus_string_insert_bytes (DBusString *str,
00583 int i,
00584 int n_bytes,
00585 unsigned char byte)
00586 {
00587 DBUS_STRING_PREAMBLE (str);
00588 _dbus_assert (i <= real->len);
00589 _dbus_assert (i >= 0);
00590 _dbus_assert (n_bytes >= 0);
00591
00592 if (n_bytes == 0)
00593 return TRUE;
00594
00595 if (!open_gap (n_bytes, real, i))
00596 return FALSE;
00597
00598 memset (real->str + i, byte, n_bytes);
00599
00600 return TRUE;
00601 }
00602
00611 dbus_bool_t
00612 _dbus_string_insert_byte (DBusString *str,
00613 int i,
00614 unsigned char byte)
00615 {
00616 DBUS_STRING_PREAMBLE (str);
00617 _dbus_assert (i <= real->len);
00618 _dbus_assert (i >= 0);
00619
00620 if (!open_gap (1, real, i))
00621 return FALSE;
00622
00623 real->str[i] = byte;
00624
00625 return TRUE;
00626 }
00627
00638 dbus_bool_t
00639 _dbus_string_steal_data (DBusString *str,
00640 char **data_return)
00641 {
00642 int old_max_length;
00643 DBUS_STRING_PREAMBLE (str);
00644 _dbus_assert (data_return != NULL);
00645
00646 undo_alignment (real);
00647
00648 *data_return = (char*) real->str;
00649
00650 old_max_length = real->max_length;
00651
00652
00653 if (!_dbus_string_init (str))
00654 {
00655
00656 real->str = (unsigned char*) *data_return;
00657 *data_return = NULL;
00658 fixup_alignment (real);
00659 return FALSE;
00660 }
00661
00662 real->max_length = old_max_length;
00663
00664 return TRUE;
00665 }
00666
00667 #ifdef DBUS_BUILD_TESTS
00668
00683 dbus_bool_t
00684 _dbus_string_steal_data_len (DBusString *str,
00685 char **data_return,
00686 int start,
00687 int len)
00688 {
00689 DBusString dest;
00690 DBUS_STRING_PREAMBLE (str);
00691 _dbus_assert (data_return != NULL);
00692 _dbus_assert (start >= 0);
00693 _dbus_assert (len >= 0);
00694 _dbus_assert (start <= real->len);
00695 _dbus_assert (len <= real->len - start);
00696
00697 if (!_dbus_string_init (&dest))
00698 return FALSE;
00699
00700 set_max_length (&dest, real->max_length);
00701
00702 if (!_dbus_string_move_len (str, start, len, &dest, 0))
00703 {
00704 _dbus_string_free (&dest);
00705 return FALSE;
00706 }
00707
00708 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
00709 if (!_dbus_string_steal_data (&dest, data_return))
00710 {
00711 _dbus_string_free (&dest);
00712 return FALSE;
00713 }
00714
00715 _dbus_string_free (&dest);
00716 return TRUE;
00717 }
00718 #endif
00719
00727 dbus_bool_t
00728 _dbus_string_copy_data (const DBusString *str,
00729 char **data_return)
00730 {
00731 DBUS_CONST_STRING_PREAMBLE (str);
00732 _dbus_assert (data_return != NULL);
00733
00734 *data_return = dbus_malloc (real->len + 1);
00735 if (*data_return == NULL)
00736 return FALSE;
00737
00738 memcpy (*data_return, real->str, real->len + 1);
00739
00740 return TRUE;
00741 }
00742
00752 void
00753 _dbus_string_copy_to_buffer (const DBusString *str,
00754 char *buffer,
00755 int avail_len)
00756 {
00757 DBUS_CONST_STRING_PREAMBLE (str);
00758
00759 _dbus_assert (avail_len >= 0);
00760 _dbus_assert (avail_len >= real->len);
00761
00762 memcpy (buffer, real->str, real->len);
00763 }
00764
00774 void
00775 _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
00776 char *buffer,
00777 int avail_len)
00778 {
00779 DBUS_CONST_STRING_PREAMBLE (str);
00780
00781 _dbus_assert (avail_len >= 0);
00782 _dbus_assert (avail_len > real->len);
00783
00784 memcpy (buffer, real->str, real->len+1);
00785 }
00786
00787 #ifdef DBUS_BUILD_TESTS
00788
00797 dbus_bool_t
00798 _dbus_string_copy_data_len (const DBusString *str,
00799 char **data_return,
00800 int start,
00801 int len)
00802 {
00803 DBusString dest;
00804
00805 DBUS_CONST_STRING_PREAMBLE (str);
00806 _dbus_assert (data_return != NULL);
00807 _dbus_assert (start >= 0);
00808 _dbus_assert (len >= 0);
00809 _dbus_assert (start <= real->len);
00810 _dbus_assert (len <= real->len - start);
00811
00812 if (!_dbus_string_init (&dest))
00813 return FALSE;
00814
00815 set_max_length (&dest, real->max_length);
00816
00817 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
00818 {
00819 _dbus_string_free (&dest);
00820 return FALSE;
00821 }
00822
00823 if (!_dbus_string_steal_data (&dest, data_return))
00824 {
00825 _dbus_string_free (&dest);
00826 return FALSE;
00827 }
00828
00829 _dbus_string_free (&dest);
00830 return TRUE;
00831 }
00832 #endif
00833
00834
00835 #ifndef _dbus_string_get_length
00836
00841 int
00842 _dbus_string_get_length (const DBusString *str)
00843 {
00844 DBUS_CONST_STRING_PREAMBLE (str);
00845
00846 return real->len;
00847 }
00848 #endif
00849
00862 dbus_bool_t
00863 _dbus_string_lengthen (DBusString *str,
00864 int additional_length)
00865 {
00866 DBUS_STRING_PREAMBLE (str);
00867 _dbus_assert (additional_length >= 0);
00868
00869 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
00870 return FALSE;
00871
00872 return set_length (real,
00873 real->len + additional_length);
00874 }
00875
00882 void
00883 _dbus_string_shorten (DBusString *str,
00884 int length_to_remove)
00885 {
00886 DBUS_STRING_PREAMBLE (str);
00887 _dbus_assert (length_to_remove >= 0);
00888 _dbus_assert (length_to_remove <= real->len);
00889
00890 set_length (real,
00891 real->len - length_to_remove);
00892 }
00893
00904 dbus_bool_t
00905 _dbus_string_set_length (DBusString *str,
00906 int length)
00907 {
00908 DBUS_STRING_PREAMBLE (str);
00909 _dbus_assert (length >= 0);
00910
00911 return set_length (real, length);
00912 }
00913
00914 static dbus_bool_t
00915 align_insert_point_then_open_gap (DBusString *str,
00916 int *insert_at_p,
00917 int alignment,
00918 int gap_size)
00919 {
00920 unsigned long new_len;
00921 unsigned long gap_pos;
00922 int insert_at;
00923 int delta;
00924 DBUS_STRING_PREAMBLE (str);
00925 _dbus_assert (alignment >= 1);
00926 _dbus_assert (alignment <= 8);
00927
00928 insert_at = *insert_at_p;
00929
00930 _dbus_assert (insert_at <= real->len);
00931
00932 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
00933 new_len = real->len + (gap_pos - insert_at) + gap_size;
00934
00935 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
00936 return FALSE;
00937
00938 delta = new_len - real->len;
00939 _dbus_assert (delta >= 0);
00940
00941 if (delta == 0)
00942 {
00943 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
00944 return TRUE;
00945 }
00946
00947 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
00948 real, insert_at)))
00949 return FALSE;
00950
00951
00952 if (gap_size < delta)
00953 {
00954 memset (&real->str[insert_at], '\0',
00955 gap_pos - insert_at);
00956 }
00957
00958 *insert_at_p = gap_pos;
00959
00960 return TRUE;
00961 }
00962
00963 static dbus_bool_t
00964 align_length_then_lengthen (DBusString *str,
00965 int alignment,
00966 int then_lengthen_by)
00967 {
00968 int insert_at;
00969
00970 insert_at = _dbus_string_get_length (str);
00971
00972 return align_insert_point_then_open_gap (str,
00973 &insert_at,
00974 alignment, then_lengthen_by);
00975 }
00976
00985 dbus_bool_t
00986 _dbus_string_align_length (DBusString *str,
00987 int alignment)
00988 {
00989 return align_length_then_lengthen (str, alignment, 0);
00990 }
00991
01001 dbus_bool_t
01002 _dbus_string_alloc_space (DBusString *str,
01003 int extra_bytes)
01004 {
01005 if (!_dbus_string_lengthen (str, extra_bytes))
01006 return FALSE;
01007 _dbus_string_shorten (str, extra_bytes);
01008
01009 return TRUE;
01010 }
01011
01012 static dbus_bool_t
01013 append (DBusRealString *real,
01014 const char *buffer,
01015 int buffer_len)
01016 {
01017 if (buffer_len == 0)
01018 return TRUE;
01019
01020 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
01021 return FALSE;
01022
01023 memcpy (real->str + (real->len - buffer_len),
01024 buffer,
01025 buffer_len);
01026
01027 return TRUE;
01028 }
01029
01037 dbus_bool_t
01038 _dbus_string_append (DBusString *str,
01039 const char *buffer)
01040 {
01041 unsigned long buffer_len;
01042
01043 DBUS_STRING_PREAMBLE (str);
01044 _dbus_assert (buffer != NULL);
01045
01046 buffer_len = strlen (buffer);
01047 if (buffer_len > (unsigned long) real->max_length)
01048 return FALSE;
01049
01050 return append (real, buffer, buffer_len);
01051 }
01052
01054 #define ASSIGN_2_OCTETS(p, octets) \
01055 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
01056
01058 #define ASSIGN_4_OCTETS(p, octets) \
01059 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
01060
01061 #ifdef DBUS_HAVE_INT64
01062
01063 #define ASSIGN_8_OCTETS(p, octets) \
01064 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
01065 #else
01066
01067 #define ASSIGN_8_OCTETS(p, octets) \
01068 do { \
01069 unsigned char *b; \
01070 \
01071 b = p; \
01072 \
01073 *b++ = octets[0]; \
01074 *b++ = octets[1]; \
01075 *b++ = octets[2]; \
01076 *b++ = octets[3]; \
01077 *b++ = octets[4]; \
01078 *b++ = octets[5]; \
01079 *b++ = octets[6]; \
01080 *b++ = octets[7]; \
01081 _dbus_assert (b == p + 8); \
01082 } while (0)
01083 #endif
01084
01085 #ifdef DBUS_BUILD_TESTS
01086
01094 dbus_bool_t
01095 _dbus_string_append_4_aligned (DBusString *str,
01096 const unsigned char octets[4])
01097 {
01098 DBUS_STRING_PREAMBLE (str);
01099
01100 if (!align_length_then_lengthen (str, 4, 4))
01101 return FALSE;
01102
01103 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
01104
01105 return TRUE;
01106 }
01107 #endif
01108
01109 #ifdef DBUS_BUILD_TESTS
01110
01118 dbus_bool_t
01119 _dbus_string_append_8_aligned (DBusString *str,
01120 const unsigned char octets[8])
01121 {
01122 DBUS_STRING_PREAMBLE (str);
01123
01124 if (!align_length_then_lengthen (str, 8, 8))
01125 return FALSE;
01126
01127 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
01128
01129 return TRUE;
01130 }
01131 #endif
01132
01142 dbus_bool_t
01143 _dbus_string_insert_2_aligned (DBusString *str,
01144 int insert_at,
01145 const unsigned char octets[4])
01146 {
01147 DBUS_STRING_PREAMBLE (str);
01148
01149 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
01150 return FALSE;
01151
01152 ASSIGN_2_OCTETS (real->str + insert_at, octets);
01153
01154 return TRUE;
01155 }
01156
01166 dbus_bool_t
01167 _dbus_string_insert_4_aligned (DBusString *str,
01168 int insert_at,
01169 const unsigned char octets[4])
01170 {
01171 DBUS_STRING_PREAMBLE (str);
01172
01173 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
01174 return FALSE;
01175
01176 ASSIGN_4_OCTETS (real->str + insert_at, octets);
01177
01178 return TRUE;
01179 }
01180
01190 dbus_bool_t
01191 _dbus_string_insert_8_aligned (DBusString *str,
01192 int insert_at,
01193 const unsigned char octets[8])
01194 {
01195 DBUS_STRING_PREAMBLE (str);
01196
01197 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
01198 return FALSE;
01199
01200 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
01201
01202 ASSIGN_8_OCTETS (real->str + insert_at, octets);
01203
01204 return TRUE;
01205 }
01206
01207
01218 dbus_bool_t
01219 _dbus_string_insert_alignment (DBusString *str,
01220 int *insert_at,
01221 int alignment)
01222 {
01223 DBUS_STRING_PREAMBLE (str);
01224
01225 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
01226 return FALSE;
01227
01228 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
01229
01230 return TRUE;
01231 }
01232
01242 dbus_bool_t
01243 _dbus_string_append_printf_valist (DBusString *str,
01244 const char *format,
01245 va_list args)
01246 {
01247 int len;
01248 va_list args_copy;
01249
01250 DBUS_STRING_PREAMBLE (str);
01251
01252 DBUS_VA_COPY (args_copy, args);
01253
01254
01255 len = _dbus_printf_string_upper_bound (format, args);
01256
01257 if (!_dbus_string_lengthen (str, len))
01258 {
01259
01260 va_end (args_copy);
01261 return FALSE;
01262 }
01263
01264 vsprintf ((char*) (real->str + (real->len - len)),
01265 format, args_copy);
01266
01267 va_end (args_copy);
01268
01269 return TRUE;
01270 }
01271
01280 dbus_bool_t
01281 _dbus_string_append_printf (DBusString *str,
01282 const char *format,
01283 ...)
01284 {
01285 va_list args;
01286 dbus_bool_t retval;
01287
01288 va_start (args, format);
01289 retval = _dbus_string_append_printf_valist (str, format, args);
01290 va_end (args);
01291
01292 return retval;
01293 }
01294
01303 dbus_bool_t
01304 _dbus_string_append_len (DBusString *str,
01305 const char *buffer,
01306 int len)
01307 {
01308 DBUS_STRING_PREAMBLE (str);
01309 _dbus_assert (buffer != NULL);
01310 _dbus_assert (len >= 0);
01311
01312 return append (real, buffer, len);
01313 }
01314
01323 dbus_bool_t
01324 _dbus_string_append_byte (DBusString *str,
01325 unsigned char byte)
01326 {
01327 DBUS_STRING_PREAMBLE (str);
01328
01329 if (!set_length (real, real->len + 1))
01330 return FALSE;
01331
01332 real->str[real->len-1] = byte;
01333
01334 return TRUE;
01335 }
01336
01337 #ifdef DBUS_BUILD_TESTS
01338
01345 dbus_bool_t
01346 _dbus_string_append_unichar (DBusString *str,
01347 dbus_unichar_t ch)
01348 {
01349 int len;
01350 int first;
01351 int i;
01352 unsigned char *out;
01353
01354 DBUS_STRING_PREAMBLE (str);
01355
01356
01357
01358 len = 0;
01359
01360 if (ch < 0x80)
01361 {
01362 first = 0;
01363 len = 1;
01364 }
01365 else if (ch < 0x800)
01366 {
01367 first = 0xc0;
01368 len = 2;
01369 }
01370 else if (ch < 0x10000)
01371 {
01372 first = 0xe0;
01373 len = 3;
01374 }
01375 else if (ch < 0x200000)
01376 {
01377 first = 0xf0;
01378 len = 4;
01379 }
01380 else if (ch < 0x4000000)
01381 {
01382 first = 0xf8;
01383 len = 5;
01384 }
01385 else
01386 {
01387 first = 0xfc;
01388 len = 6;
01389 }
01390
01391 if (len > (real->max_length - real->len))
01392 return FALSE;
01393
01394 if (!set_length (real, real->len + len))
01395 return FALSE;
01396
01397 out = real->str + (real->len - len);
01398
01399 for (i = len - 1; i > 0; --i)
01400 {
01401 out[i] = (ch & 0x3f) | 0x80;
01402 ch >>= 6;
01403 }
01404 out[0] = ch | first;
01405
01406 return TRUE;
01407 }
01408 #endif
01409
01410 static void
01411 delete (DBusRealString *real,
01412 int start,
01413 int len)
01414 {
01415 if (len == 0)
01416 return;
01417
01418 memmove (real->str + start, real->str + start + len, real->len - (start + len));
01419 real->len -= len;
01420 real->str[real->len] = '\0';
01421 }
01422
01432 void
01433 _dbus_string_delete (DBusString *str,
01434 int start,
01435 int len)
01436 {
01437 DBUS_STRING_PREAMBLE (str);
01438 _dbus_assert (start >= 0);
01439 _dbus_assert (len >= 0);
01440 _dbus_assert (start <= real->len);
01441 _dbus_assert (len <= real->len - start);
01442
01443 delete (real, start, len);
01444 }
01445
01446 static dbus_bool_t
01447 copy (DBusRealString *source,
01448 int start,
01449 int len,
01450 DBusRealString *dest,
01451 int insert_at)
01452 {
01453 if (len == 0)
01454 return TRUE;
01455
01456 if (!open_gap (len, dest, insert_at))
01457 return FALSE;
01458
01459 memmove (dest->str + insert_at,
01460 source->str + start,
01461 len);
01462
01463 return TRUE;
01464 }
01465
01475 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
01476 DBusRealString *real_source = (DBusRealString*) source; \
01477 DBusRealString *real_dest = (DBusRealString*) dest; \
01478 _dbus_assert ((source) != (dest)); \
01479 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
01480 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
01481 _dbus_assert (!real_dest->constant); \
01482 _dbus_assert (!real_dest->locked); \
01483 _dbus_assert ((start) >= 0); \
01484 _dbus_assert ((start) <= real_source->len); \
01485 _dbus_assert ((insert_at) >= 0); \
01486 _dbus_assert ((insert_at) <= real_dest->len)
01487
01498 dbus_bool_t
01499 _dbus_string_move (DBusString *source,
01500 int start,
01501 DBusString *dest,
01502 int insert_at)
01503 {
01504 DBusRealString *real_source = (DBusRealString*) source;
01505 _dbus_assert (start <= real_source->len);
01506
01507 return _dbus_string_move_len (source, start,
01508 real_source->len - start,
01509 dest, insert_at);
01510 }
01511
01522 dbus_bool_t
01523 _dbus_string_copy (const DBusString *source,
01524 int start,
01525 DBusString *dest,
01526 int insert_at)
01527 {
01528 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
01529
01530 return copy (real_source, start,
01531 real_source->len - start,
01532 real_dest,
01533 insert_at);
01534 }
01535
01550 dbus_bool_t
01551 _dbus_string_move_len (DBusString *source,
01552 int start,
01553 int len,
01554 DBusString *dest,
01555 int insert_at)
01556
01557 {
01558 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
01559 _dbus_assert (len >= 0);
01560 _dbus_assert ((start + len) <= real_source->len);
01561
01562
01563 if (len == 0)
01564 {
01565 return TRUE;
01566 }
01567 else if (start == 0 &&
01568 len == real_source->len &&
01569 real_dest->len == 0)
01570 {
01571
01572
01573
01574
01575
01576
01577 #define ASSIGN_DATA(a, b) do { \
01578 (a)->str = (b)->str; \
01579 (a)->len = (b)->len; \
01580 (a)->allocated = (b)->allocated; \
01581 (a)->align_offset = (b)->align_offset; \
01582 } while (0)
01583
01584 DBusRealString tmp;
01585
01586 ASSIGN_DATA (&tmp, real_source);
01587 ASSIGN_DATA (real_source, real_dest);
01588 ASSIGN_DATA (real_dest, &tmp);
01589
01590 return TRUE;
01591 }
01592 else
01593 {
01594 if (!copy (real_source, start, len,
01595 real_dest,
01596 insert_at))
01597 return FALSE;
01598
01599 delete (real_source, start,
01600 len);
01601
01602 return TRUE;
01603 }
01604 }
01605
01617 dbus_bool_t
01618 _dbus_string_copy_len (const DBusString *source,
01619 int start,
01620 int len,
01621 DBusString *dest,
01622 int insert_at)
01623 {
01624 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
01625 _dbus_assert (len >= 0);
01626 _dbus_assert (start <= real_source->len);
01627 _dbus_assert (len <= real_source->len - start);
01628
01629 return copy (real_source, start, len,
01630 real_dest,
01631 insert_at);
01632 }
01633
01655 dbus_bool_t
01656 _dbus_string_replace_len (const DBusString *source,
01657 int start,
01658 int len,
01659 DBusString *dest,
01660 int replace_at,
01661 int replace_len)
01662 {
01663 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
01664 _dbus_assert (len >= 0);
01665 _dbus_assert (start <= real_source->len);
01666 _dbus_assert (len <= real_source->len - start);
01667 _dbus_assert (replace_at >= 0);
01668 _dbus_assert (replace_at <= real_dest->len);
01669 _dbus_assert (replace_len <= real_dest->len - replace_at);
01670
01671 if (!copy (real_source, start, len,
01672 real_dest, replace_at))
01673 return FALSE;
01674
01675 delete (real_dest, replace_at + len, replace_len);
01676
01677 return TRUE;
01678 }
01679
01692 dbus_bool_t
01693 _dbus_string_split_on_byte (DBusString *source,
01694 unsigned char byte,
01695 DBusString *tail)
01696 {
01697 int byte_position;
01698 char byte_string[2] = "";
01699 int head_length;
01700 int tail_length;
01701
01702 byte_string[0] = (char) byte;
01703
01704 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
01705 return FALSE;
01706
01707 head_length = byte_position;
01708 tail_length = _dbus_string_get_length (source) - head_length - 1;
01709
01710 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
01711 tail, 0))
01712 return FALSE;
01713
01714
01715
01716 if (!_dbus_string_set_length (source, head_length))
01717 return FALSE;
01718
01719 return TRUE;
01720 }
01721
01722
01723
01724
01725
01731 #define UTF8_COMPUTE(Char, Mask, Len) \
01732 if (Char < 128) \
01733 { \
01734 Len = 1; \
01735 Mask = 0x7f; \
01736 } \
01737 else if ((Char & 0xe0) == 0xc0) \
01738 { \
01739 Len = 2; \
01740 Mask = 0x1f; \
01741 } \
01742 else if ((Char & 0xf0) == 0xe0) \
01743 { \
01744 Len = 3; \
01745 Mask = 0x0f; \
01746 } \
01747 else if ((Char & 0xf8) == 0xf0) \
01748 { \
01749 Len = 4; \
01750 Mask = 0x07; \
01751 } \
01752 else if ((Char & 0xfc) == 0xf8) \
01753 { \
01754 Len = 5; \
01755 Mask = 0x03; \
01756 } \
01757 else if ((Char & 0xfe) == 0xfc) \
01758 { \
01759 Len = 6; \
01760 Mask = 0x01; \
01761 } \
01762 else \
01763 { \
01764 Len = 0; \
01765 Mask = 0; \
01766 }
01767
01772 #define UTF8_LENGTH(Char) \
01773 ((Char) < 0x80 ? 1 : \
01774 ((Char) < 0x800 ? 2 : \
01775 ((Char) < 0x10000 ? 3 : \
01776 ((Char) < 0x200000 ? 4 : \
01777 ((Char) < 0x4000000 ? 5 : 6)))))
01778
01788 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
01789 (Result) = (Chars)[0] & (Mask); \
01790 for ((Count) = 1; (Count) < (Len); ++(Count)) \
01791 { \
01792 if (((Chars)[(Count)] & 0xc0) != 0x80) \
01793 { \
01794 (Result) = -1; \
01795 break; \
01796 } \
01797 (Result) <<= 6; \
01798 (Result) |= ((Chars)[(Count)] & 0x3f); \
01799 }
01800
01806 #define UNICODE_VALID(Char) \
01807 ((Char) < 0x110000 && \
01808 (((Char) & 0xFFFFF800) != 0xD800))
01809
01810 #ifdef DBUS_BUILD_TESTS
01811
01821 void
01822 _dbus_string_get_unichar (const DBusString *str,
01823 int start,
01824 dbus_unichar_t *ch_return,
01825 int *end_return)
01826 {
01827 int i, mask, len;
01828 dbus_unichar_t result;
01829 unsigned char c;
01830 unsigned char *p;
01831 DBUS_CONST_STRING_PREAMBLE (str);
01832 _dbus_assert (start >= 0);
01833 _dbus_assert (start <= real->len);
01834
01835 if (ch_return)
01836 *ch_return = 0;
01837 if (end_return)
01838 *end_return = real->len;
01839
01840 mask = 0;
01841 p = real->str + start;
01842 c = *p;
01843
01844 UTF8_COMPUTE (c, mask, len);
01845 if (len == 0)
01846 return;
01847 UTF8_GET (result, p, i, mask, len);
01848
01849 if (result == (dbus_unichar_t)-1)
01850 return;
01851
01852 if (ch_return)
01853 *ch_return = result;
01854 if (end_return)
01855 *end_return = start + len;
01856 }
01857 #endif
01858
01873 dbus_bool_t
01874 _dbus_string_find (const DBusString *str,
01875 int start,
01876 const char *substr,
01877 int *found)
01878 {
01879 return _dbus_string_find_to (str, start,
01880 ((const DBusRealString*)str)->len,
01881 substr, found);
01882 }
01883
01896 dbus_bool_t
01897 _dbus_string_find_eol (const DBusString *str,
01898 int start,
01899 int *found,
01900 int *found_len)
01901 {
01902 int i;
01903
01904 DBUS_CONST_STRING_PREAMBLE (str);
01905 _dbus_assert (start <= real->len);
01906 _dbus_assert (start >= 0);
01907
01908 i = start;
01909 while (i < real->len)
01910 {
01911 if (real->str[i] == '\r')
01912 {
01913 if ((i+1) < real->len && real->str[i+1] == '\n')
01914 {
01915 if (found)
01916 *found = i;
01917 if (found_len)
01918 *found_len = 2;
01919 return TRUE;
01920 }
01921 else
01922 {
01923 if (found)
01924 *found = i;
01925 if (found_len)
01926 *found_len = 1;
01927 return TRUE;
01928 }
01929 }
01930 else if (real->str[i] == '\n')
01931 {
01932 if (found)
01933 *found = i;
01934 if (found_len)
01935 *found_len = 1;
01936 return TRUE;
01937 }
01938 ++i;
01939 }
01940
01941 if (found)
01942 *found = real->len;
01943
01944 if (found_len)
01945 *found_len = 0;
01946
01947 return FALSE;
01948 }
01949
01966 dbus_bool_t
01967 _dbus_string_find_to (const DBusString *str,
01968 int start,
01969 int end,
01970 const char *substr,
01971 int *found)
01972 {
01973 int i;
01974 DBUS_CONST_STRING_PREAMBLE (str);
01975 _dbus_assert (substr != NULL);
01976 _dbus_assert (start <= real->len);
01977 _dbus_assert (start >= 0);
01978 _dbus_assert (substr != NULL);
01979 _dbus_assert (end <= real->len);
01980 _dbus_assert (start <= end);
01981
01982
01983 if (*substr == '\0')
01984 {
01985 if (found)
01986 *found = start;
01987 return TRUE;
01988 }
01989
01990 i = start;
01991 while (i < end)
01992 {
01993 if (real->str[i] == substr[0])
01994 {
01995 int j = i + 1;
01996
01997 while (j < end)
01998 {
01999 if (substr[j - i] == '\0')
02000 break;
02001 else if (real->str[j] != substr[j - i])
02002 break;
02003
02004 ++j;
02005 }
02006
02007 if (substr[j - i] == '\0')
02008 {
02009 if (found)
02010 *found = i;
02011 return TRUE;
02012 }
02013 }
02014
02015 ++i;
02016 }
02017
02018 if (found)
02019 *found = end;
02020
02021 return FALSE;
02022 }
02023
02034 dbus_bool_t
02035 _dbus_string_find_blank (const DBusString *str,
02036 int start,
02037 int *found)
02038 {
02039 int i;
02040 DBUS_CONST_STRING_PREAMBLE (str);
02041 _dbus_assert (start <= real->len);
02042 _dbus_assert (start >= 0);
02043
02044 i = start;
02045 while (i < real->len)
02046 {
02047 if (real->str[i] == ' ' ||
02048 real->str[i] == '\t')
02049 {
02050 if (found)
02051 *found = i;
02052 return TRUE;
02053 }
02054
02055 ++i;
02056 }
02057
02058 if (found)
02059 *found = real->len;
02060
02061 return FALSE;
02062 }
02063
02072 void
02073 _dbus_string_skip_blank (const DBusString *str,
02074 int start,
02075 int *end)
02076 {
02077 int i;
02078 DBUS_CONST_STRING_PREAMBLE (str);
02079 _dbus_assert (start <= real->len);
02080 _dbus_assert (start >= 0);
02081
02082 i = start;
02083 while (i < real->len)
02084 {
02085 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
02086 break;
02087
02088 ++i;
02089 }
02090
02091 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
02092
02093 if (end)
02094 *end = i;
02095 }
02096
02097
02106 void
02107 _dbus_string_skip_white (const DBusString *str,
02108 int start,
02109 int *end)
02110 {
02111 int i;
02112 DBUS_CONST_STRING_PREAMBLE (str);
02113 _dbus_assert (start <= real->len);
02114 _dbus_assert (start >= 0);
02115
02116 i = start;
02117 while (i < real->len)
02118 {
02119 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
02120 break;
02121
02122 ++i;
02123 }
02124
02125 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
02126
02127 if (end)
02128 *end = i;
02129 }
02130
02139 void
02140 _dbus_string_skip_white_reverse (const DBusString *str,
02141 int end,
02142 int *start)
02143 {
02144 int i;
02145 DBUS_CONST_STRING_PREAMBLE (str);
02146 _dbus_assert (end <= real->len);
02147 _dbus_assert (end >= 0);
02148
02149 i = end;
02150 while (i > 0)
02151 {
02152 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
02153 break;
02154 --i;
02155 }
02156
02157 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
02158
02159 if (start)
02160 *start = i;
02161 }
02162
02178 dbus_bool_t
02179 _dbus_string_pop_line (DBusString *source,
02180 DBusString *dest)
02181 {
02182 int eol, eol_len;
02183
02184 _dbus_string_set_length (dest, 0);
02185
02186 eol = 0;
02187 eol_len = 0;
02188 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
02189 {
02190 _dbus_assert (eol == _dbus_string_get_length (source));
02191 if (eol == 0)
02192 {
02193
02194 return FALSE;
02195 }
02196
02197 }
02198
02199
02200
02201
02202
02203 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
02204 return FALSE;
02205
02206
02207 if (!_dbus_string_set_length (dest, eol))
02208 {
02209 _dbus_assert_not_reached ("out of memory when shortening a string");
02210 return FALSE;
02211 }
02212
02213 return TRUE;
02214 }
02215
02216 #ifdef DBUS_BUILD_TESTS
02217
02223 void
02224 _dbus_string_delete_first_word (DBusString *str)
02225 {
02226 int i;
02227
02228 if (_dbus_string_find_blank (str, 0, &i))
02229 _dbus_string_skip_blank (str, i, &i);
02230
02231 _dbus_string_delete (str, 0, i);
02232 }
02233 #endif
02234
02235 #ifdef DBUS_BUILD_TESTS
02236
02241 void
02242 _dbus_string_delete_leading_blanks (DBusString *str)
02243 {
02244 int i;
02245
02246 _dbus_string_skip_blank (str, 0, &i);
02247
02248 if (i > 0)
02249 _dbus_string_delete (str, 0, i);
02250 }
02251 #endif
02252
02258 void
02259 _dbus_string_chop_white(DBusString *str)
02260 {
02261 int i;
02262
02263 _dbus_string_skip_white (str, 0, &i);
02264
02265 if (i > 0)
02266 _dbus_string_delete (str, 0, i);
02267
02268 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
02269
02270 _dbus_string_set_length (str, i);
02271 }
02272
02282 dbus_bool_t
02283 _dbus_string_equal (const DBusString *a,
02284 const DBusString *b)
02285 {
02286 const unsigned char *ap;
02287 const unsigned char *bp;
02288 const unsigned char *a_end;
02289 const DBusRealString *real_a = (const DBusRealString*) a;
02290 const DBusRealString *real_b = (const DBusRealString*) b;
02291 DBUS_GENERIC_STRING_PREAMBLE (real_a);
02292 DBUS_GENERIC_STRING_PREAMBLE (real_b);
02293
02294 if (real_a->len != real_b->len)
02295 return FALSE;
02296
02297 ap = real_a->str;
02298 bp = real_b->str;
02299 a_end = real_a->str + real_a->len;
02300 while (ap != a_end)
02301 {
02302 if (*ap != *bp)
02303 return FALSE;
02304
02305 ++ap;
02306 ++bp;
02307 }
02308
02309 return TRUE;
02310 }
02311
02312 #ifdef DBUS_BUILD_TESTS
02313
02326 dbus_bool_t
02327 _dbus_string_equal_len (const DBusString *a,
02328 const DBusString *b,
02329 int len)
02330 {
02331 const unsigned char *ap;
02332 const unsigned char *bp;
02333 const unsigned char *a_end;
02334 const DBusRealString *real_a = (const DBusRealString*) a;
02335 const DBusRealString *real_b = (const DBusRealString*) b;
02336 DBUS_GENERIC_STRING_PREAMBLE (real_a);
02337 DBUS_GENERIC_STRING_PREAMBLE (real_b);
02338
02339 if (real_a->len != real_b->len &&
02340 (real_a->len < len || real_b->len < len))
02341 return FALSE;
02342
02343 ap = real_a->str;
02344 bp = real_b->str;
02345 a_end = real_a->str + MIN (real_a->len, len);
02346 while (ap != a_end)
02347 {
02348 if (*ap != *bp)
02349 return FALSE;
02350
02351 ++ap;
02352 ++bp;
02353 }
02354
02355 return TRUE;
02356 }
02357 #endif
02358
02375 dbus_bool_t
02376 _dbus_string_equal_substring (const DBusString *a,
02377 int a_start,
02378 int a_len,
02379 const DBusString *b,
02380 int b_start)
02381 {
02382 const unsigned char *ap;
02383 const unsigned char *bp;
02384 const unsigned char *a_end;
02385 const DBusRealString *real_a = (const DBusRealString*) a;
02386 const DBusRealString *real_b = (const DBusRealString*) b;
02387 DBUS_GENERIC_STRING_PREAMBLE (real_a);
02388 DBUS_GENERIC_STRING_PREAMBLE (real_b);
02389 _dbus_assert (a_start >= 0);
02390 _dbus_assert (a_len >= 0);
02391 _dbus_assert (a_start <= real_a->len);
02392 _dbus_assert (a_len <= real_a->len - a_start);
02393 _dbus_assert (b_start >= 0);
02394 _dbus_assert (b_start <= real_b->len);
02395
02396 if (a_len > real_b->len - b_start)
02397 return FALSE;
02398
02399 ap = real_a->str + a_start;
02400 bp = real_b->str + b_start;
02401 a_end = ap + a_len;
02402 while (ap != a_end)
02403 {
02404 if (*ap != *bp)
02405 return FALSE;
02406
02407 ++ap;
02408 ++bp;
02409 }
02410
02411 _dbus_assert (bp <= (real_b->str + real_b->len));
02412
02413 return TRUE;
02414 }
02415
02423 dbus_bool_t
02424 _dbus_string_equal_c_str (const DBusString *a,
02425 const char *c_str)
02426 {
02427 const unsigned char *ap;
02428 const unsigned char *bp;
02429 const unsigned char *a_end;
02430 const DBusRealString *real_a = (const DBusRealString*) a;
02431 DBUS_GENERIC_STRING_PREAMBLE (real_a);
02432 _dbus_assert (c_str != NULL);
02433
02434 ap = real_a->str;
02435 bp = (const unsigned char*) c_str;
02436 a_end = real_a->str + real_a->len;
02437 while (ap != a_end && *bp)
02438 {
02439 if (*ap != *bp)
02440 return FALSE;
02441
02442 ++ap;
02443 ++bp;
02444 }
02445
02446 if (ap != a_end || *bp)
02447 return FALSE;
02448
02449 return TRUE;
02450 }
02451
02452 #ifdef DBUS_BUILD_TESTS
02453
02460 dbus_bool_t
02461 _dbus_string_starts_with_c_str (const DBusString *a,
02462 const char *c_str)
02463 {
02464 const unsigned char *ap;
02465 const unsigned char *bp;
02466 const unsigned char *a_end;
02467 const DBusRealString *real_a = (const DBusRealString*) a;
02468 DBUS_GENERIC_STRING_PREAMBLE (real_a);
02469 _dbus_assert (c_str != NULL);
02470
02471 ap = real_a->str;
02472 bp = (const unsigned char*) c_str;
02473 a_end = real_a->str + real_a->len;
02474 while (ap != a_end && *bp)
02475 {
02476 if (*ap != *bp)
02477 return FALSE;
02478
02479 ++ap;
02480 ++bp;
02481 }
02482
02483 if (*bp == '\0')
02484 return TRUE;
02485 else
02486 return FALSE;
02487 }
02488 #endif
02489
02498 dbus_bool_t
02499 _dbus_string_append_byte_as_hex (DBusString *str,
02500 int byte)
02501 {
02502 const char hexdigits[16] = {
02503 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
02504 'a', 'b', 'c', 'd', 'e', 'f'
02505 };
02506
02507 if (!_dbus_string_append_byte (str,
02508 hexdigits[(byte >> 4)]))
02509 return FALSE;
02510
02511 if (!_dbus_string_append_byte (str,
02512 hexdigits[(byte & 0x0f)]))
02513 {
02514 _dbus_string_set_length (str,
02515 _dbus_string_get_length (str) - 1);
02516 return FALSE;
02517 }
02518
02519 return TRUE;
02520 }
02521
02532 dbus_bool_t
02533 _dbus_string_hex_encode (const DBusString *source,
02534 int start,
02535 DBusString *dest,
02536 int insert_at)
02537 {
02538 DBusString result;
02539 const unsigned char *p;
02540 const unsigned char *end;
02541 dbus_bool_t retval;
02542
02543 _dbus_assert (start <= _dbus_string_get_length (source));
02544
02545 if (!_dbus_string_init (&result))
02546 return FALSE;
02547
02548 retval = FALSE;
02549
02550 p = (const unsigned char*) _dbus_string_get_const_data (source);
02551 end = p + _dbus_string_get_length (source);
02552 p += start;
02553
02554 while (p != end)
02555 {
02556 if (!_dbus_string_append_byte_as_hex (&result, *p))
02557 goto out;
02558
02559 ++p;
02560 }
02561
02562 if (!_dbus_string_move (&result, 0, dest, insert_at))
02563 goto out;
02564
02565 retval = TRUE;
02566
02567 out:
02568 _dbus_string_free (&result);
02569 return retval;
02570 }
02571
02582 dbus_bool_t
02583 _dbus_string_hex_decode (const DBusString *source,
02584 int start,
02585 int *end_return,
02586 DBusString *dest,
02587 int insert_at)
02588 {
02589 DBusString result;
02590 const unsigned char *p;
02591 const unsigned char *end;
02592 dbus_bool_t retval;
02593 dbus_bool_t high_bits;
02594
02595 _dbus_assert (start <= _dbus_string_get_length (source));
02596
02597 if (!_dbus_string_init (&result))
02598 return FALSE;
02599
02600 retval = FALSE;
02601
02602 high_bits = TRUE;
02603 p = (const unsigned char*) _dbus_string_get_const_data (source);
02604 end = p + _dbus_string_get_length (source);
02605 p += start;
02606
02607 while (p != end)
02608 {
02609 unsigned int val;
02610
02611 switch (*p)
02612 {
02613 case '0':
02614 val = 0;
02615 break;
02616 case '1':
02617 val = 1;
02618 break;
02619 case '2':
02620 val = 2;
02621 break;
02622 case '3':
02623 val = 3;
02624 break;
02625 case '4':
02626 val = 4;
02627 break;
02628 case '5':
02629 val = 5;
02630 break;
02631 case '6':
02632 val = 6;
02633 break;
02634 case '7':
02635 val = 7;
02636 break;
02637 case '8':
02638 val = 8;
02639 break;
02640 case '9':
02641 val = 9;
02642 break;
02643 case 'a':
02644 case 'A':
02645 val = 10;
02646 break;
02647 case 'b':
02648 case 'B':
02649 val = 11;
02650 break;
02651 case 'c':
02652 case 'C':
02653 val = 12;
02654 break;
02655 case 'd':
02656 case 'D':
02657 val = 13;
02658 break;
02659 case 'e':
02660 case 'E':
02661 val = 14;
02662 break;
02663 case 'f':
02664 case 'F':
02665 val = 15;
02666 break;
02667 default:
02668 goto done;
02669 }
02670
02671 if (high_bits)
02672 {
02673 if (!_dbus_string_append_byte (&result,
02674 val << 4))
02675 goto out;
02676 }
02677 else
02678 {
02679 int len;
02680 unsigned char b;
02681
02682 len = _dbus_string_get_length (&result);
02683
02684 b = _dbus_string_get_byte (&result, len - 1);
02685
02686 b |= val;
02687
02688 _dbus_string_set_byte (&result, len - 1, b);
02689 }
02690
02691 high_bits = !high_bits;
02692
02693 ++p;
02694 }
02695
02696 done:
02697 if (!_dbus_string_move (&result, 0, dest, insert_at))
02698 goto out;
02699
02700 if (end_return)
02701 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
02702
02703 retval = TRUE;
02704
02705 out:
02706 _dbus_string_free (&result);
02707 return retval;
02708 }
02709
02723 dbus_bool_t
02724 _dbus_string_validate_ascii (const DBusString *str,
02725 int start,
02726 int len)
02727 {
02728 const unsigned char *s;
02729 const unsigned char *end;
02730 DBUS_CONST_STRING_PREAMBLE (str);
02731 _dbus_assert (start >= 0);
02732 _dbus_assert (start <= real->len);
02733 _dbus_assert (len >= 0);
02734
02735 if (len > real->len - start)
02736 return FALSE;
02737
02738 s = real->str + start;
02739 end = s + len;
02740 while (s != end)
02741 {
02742 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
02743 return FALSE;
02744
02745 ++s;
02746 }
02747
02748 return TRUE;
02749 }
02750
02766 dbus_bool_t
02767 _dbus_string_validate_utf8 (const DBusString *str,
02768 int start,
02769 int len)
02770 {
02771 const unsigned char *p;
02772 const unsigned char *end;
02773 DBUS_CONST_STRING_PREAMBLE (str);
02774 _dbus_assert (start >= 0);
02775 _dbus_assert (start <= real->len);
02776 _dbus_assert (len >= 0);
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786 if (_DBUS_UNLIKELY (len > real->len - start))
02787 return FALSE;
02788
02789 p = real->str + start;
02790 end = p + len;
02791
02792 while (p < end)
02793 {
02794 int i, mask, char_len;
02795 dbus_unichar_t result;
02796
02797
02798 if (*p == '\0')
02799 break;
02800
02801
02802
02803
02804
02805
02806
02807 if (*p < 128)
02808 {
02809 ++p;
02810 continue;
02811 }
02812
02813 UTF8_COMPUTE (*p, mask, char_len);
02814
02815 if (_DBUS_UNLIKELY (char_len == 0))
02816 break;
02817
02818
02819 if (_DBUS_UNLIKELY ((end - p) < char_len))
02820 break;
02821
02822 UTF8_GET (result, p, i, mask, char_len);
02823
02824
02825 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len))
02826 break;
02827 #if 0
02828
02829 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1))
02830 break;
02831 #endif
02832
02833 if (_DBUS_UNLIKELY (!UNICODE_VALID (result)))
02834 break;
02835
02836
02837 _dbus_assert (result != (dbus_unichar_t)-1);
02838
02839 p += char_len;
02840 }
02841
02842
02843
02844
02845 if (_DBUS_UNLIKELY (p != end))
02846 return FALSE;
02847 else
02848 return TRUE;
02849 }
02850
02864 dbus_bool_t
02865 _dbus_string_validate_nul (const DBusString *str,
02866 int start,
02867 int len)
02868 {
02869 const unsigned char *s;
02870 const unsigned char *end;
02871 DBUS_CONST_STRING_PREAMBLE (str);
02872 _dbus_assert (start >= 0);
02873 _dbus_assert (len >= 0);
02874 _dbus_assert (start <= real->len);
02875
02876 if (len > real->len - start)
02877 return FALSE;
02878
02879 s = real->str + start;
02880 end = s + len;
02881 while (s != end)
02882 {
02883 if (_DBUS_UNLIKELY (*s != '\0'))
02884 return FALSE;
02885 ++s;
02886 }
02887
02888 return TRUE;
02889 }
02890
02896 void
02897 _dbus_string_zero (DBusString *str)
02898 {
02899 DBUS_STRING_PREAMBLE (str);
02900
02901 memset (real->str - real->align_offset, '\0', real->allocated);
02902 }
02905