dbus-string.c

00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-string.c String utility class (internal to D-Bus implementation)
00003  * 
00004  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
00005  * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 
00025 #include "dbus-internals.h"
00026 #include "dbus-string.h"
00027 /* we allow a system header here, for speed/convenience */
00028 #include <string.h>
00029 /* for vsnprintf */
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" /* probably should be removed by moving the usage of DBUS_TYPE
00034                                  * into the marshaling-related files
00035                                  */
00036 /* for DBUS_VA_COPY */
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   /* we have to have extra space in real->allocated for the align offset and nul byte */
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       /* Here comes the suck */
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   /* It's very important not to touch anything
00143    * other than real->str if we're going to fail,
00144    * since we also use this function to reset
00145    * an existing string, e.g. in _dbus_string_steal_data()
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 /* The max length thing is sort of a historical artifact
00182  * from a feature that turned out to be dumb; perhaps
00183  * we should purge it entirely. The problem with
00184  * the feature is that it looks like memory allocation
00185  * failure, but is not a transient or resolvable failure.
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 /* DBUS_BUILD_TESTS */
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; /* a lie, just to avoid special-case assertions... */
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   /* We don't require const strings to be 8-byte aligned as the
00252    * memory is coming from elsewhere.
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 /* Not using this feature at the moment,
00302  * so marked DBUS_BUILD_TESTS-only
00303  */
00313 void
00314 _dbus_string_lock (DBusString *str)
00315 {  
00316   DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
00317 
00318   real->locked = TRUE;
00319 
00320   /* Try to realloc to avoid excess memory usage, since
00321    * we know we won't change the string further
00322    */
00323 #define MAX_WASTE 48
00324   compact (real, MAX_WASTE);
00325 }
00326 #endif /* DBUS_BUILD_TESTS */
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   /* at least double our old allocation to avoid O(n), avoiding
00336    * overflow
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   /* if you change the code just above here, run the tests without
00344    * the following assert-only hack before you commit
00345    */
00346   /* This is keyed off asserts in addition to tests so when you
00347    * disable asserts to profile, you don't get this destroyer
00348    * of profiles.
00349    */
00350 #ifdef DBUS_DISABLE_ASSERT
00351 #else
00352 #ifdef DBUS_BUILD_TESTS
00353   new_allocated = 0; /* ensure a realloc every time so that we go
00354                       * through all malloc failure codepaths
00355                       */
00356 #endif /* DBUS_BUILD_TESTS */
00357 #endif /* !DBUS_DISABLE_ASSERT */
00358 
00359   /* But be sure we always alloc at least space for the new length */
00360   new_allocated = MAX (new_allocated,
00361                        new_length + _DBUS_STRING_ALLOCATION_PADDING);
00362 
00363   _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
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   /* Note, we are setting the length not including nul termination */
00400 
00401   /* exceeding max length is the same as failure to allocate memory */
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; /* detected overflow of dest->len + len below */
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 /* _dbus_string_get_data */
00456 
00457 /* only do the function if we don't have the macro */
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 /* _dbus_string_get_const_data */
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 /* only do the function if we don't have the macro */
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 /* _dbus_string_get_const_data_len */
00525 
00526 /* only do the function if we don't have the macro */
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 /* _dbus_string_set_byte */
00547 
00548 /* only have the function if we didn't create a macro */
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 /* _dbus_string_get_byte */
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   /* reset the string */
00653   if (!_dbus_string_init (str))
00654     {
00655       /* hrm, put it back then */
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 /* DBUS_BUILD_TESTS */
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 /* DBUS_BUILD_TESTS */
00833 
00834 /* Only have the function if we don't have the macro */
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 /* !_dbus_string_get_length */
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; /* would overflow */
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; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
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); /* it has to be a bug if > 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) /* only happens if gap_size == 0 and insert_at is aligned already */
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   /* nul the padding if we had to add any padding */
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 /* DBUS_HAVE_INT64 */
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 /* DBUS_BUILD_TESTS */
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 /* DBUS_BUILD_TESTS */
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   /* Measure the message length without terminating nul */
01255   len = _dbus_printf_string_upper_bound (format, args);
01256 
01257   if (!_dbus_string_lengthen (str, len))
01258     {
01259       /* don't leak the copy */
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   /* this code is from GLib but is pretty standard I think */
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; /* real->len + len would overflow */
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 /* DBUS_BUILD_TESTS */
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       /* Short-circuit moving an entire existing string to an empty string
01572        * by just swapping the buffers.
01573        */
01574       /* we assume ->constant doesn't matter as you can't have
01575        * a constant string involved in a move.
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   /* remove the trailing delimiter byte from the head now.
01715    */
01716   if (!_dbus_string_set_length (source, head_length))
01717     return FALSE;
01718 
01719   return TRUE;
01720 }
01721 
01722 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
01723  * Pennington, and Tom Tromey are the authors and authorized relicense.
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      ((Char) < 0xFDD0 || (Char) > 0xFDEF) &&  \
01810      ((Char) & 0xFFFF) != 0xFFFF)
01811 
01812 #ifdef DBUS_BUILD_TESTS
01813 
01823 void
01824 _dbus_string_get_unichar (const DBusString *str,
01825                           int               start,
01826                           dbus_unichar_t   *ch_return,
01827                           int              *end_return)
01828 {
01829   int i, mask, len;
01830   dbus_unichar_t result;
01831   unsigned char c;
01832   unsigned char *p;
01833   DBUS_CONST_STRING_PREAMBLE (str);
01834   _dbus_assert (start >= 0);
01835   _dbus_assert (start <= real->len);
01836   
01837   if (ch_return)
01838     *ch_return = 0;
01839   if (end_return)
01840     *end_return = real->len;
01841   
01842   mask = 0;
01843   p = real->str + start;
01844   c = *p;
01845   
01846   UTF8_COMPUTE (c, mask, len);
01847   if (len == 0)
01848     return;
01849   UTF8_GET (result, p, i, mask, len);
01850 
01851   if (result == (dbus_unichar_t)-1)
01852     return;
01853 
01854   if (ch_return)
01855     *ch_return = result;
01856   if (end_return)
01857     *end_return = start + len;
01858 }
01859 #endif /* DBUS_BUILD_TESTS */
01860 
01875 dbus_bool_t
01876 _dbus_string_find (const DBusString *str,
01877                    int               start,
01878                    const char       *substr,
01879                    int              *found)
01880 {
01881   return _dbus_string_find_to (str, start,
01882                                ((const DBusRealString*)str)->len,
01883                                substr, found);
01884 }
01885 
01898 dbus_bool_t
01899 _dbus_string_find_eol (const DBusString *str,
01900                        int               start,
01901                        int              *found,
01902                        int              *found_len)
01903 {
01904   int i;
01905 
01906   DBUS_CONST_STRING_PREAMBLE (str);
01907   _dbus_assert (start <= real->len);
01908   _dbus_assert (start >= 0);
01909   
01910   i = start;
01911   while (i < real->len)
01912     {
01913       if (real->str[i] == '\r') 
01914         {
01915           if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
01916             {
01917               if (found) 
01918                 *found = i;
01919               if (found_len)
01920                 *found_len = 2;
01921               return TRUE;
01922             } 
01923           else /* only "\r" */
01924             {
01925               if (found) 
01926                 *found = i;
01927               if (found_len)
01928                 *found_len = 1;
01929               return TRUE;
01930             }
01931         } 
01932       else if (real->str[i] == '\n')  /* only "\n" */
01933         {
01934           if (found) 
01935             *found = i;
01936           if (found_len)
01937             *found_len = 1;
01938           return TRUE;
01939         }
01940       ++i;
01941     }
01942 
01943   if (found)
01944     *found = real->len;
01945 
01946   if (found_len)
01947     *found_len = 0;
01948   
01949   return FALSE;
01950 }
01951 
01968 dbus_bool_t
01969 _dbus_string_find_to (const DBusString *str,
01970                       int               start,
01971                       int               end,
01972                       const char       *substr,
01973                       int              *found)
01974 {
01975   int i;
01976   DBUS_CONST_STRING_PREAMBLE (str);
01977   _dbus_assert (substr != NULL);
01978   _dbus_assert (start <= real->len);
01979   _dbus_assert (start >= 0);
01980   _dbus_assert (substr != NULL);
01981   _dbus_assert (end <= real->len);
01982   _dbus_assert (start <= end);
01983 
01984   /* we always "find" an empty string */
01985   if (*substr == '\0')
01986     {
01987       if (found)
01988         *found = start;
01989       return TRUE;
01990     }
01991 
01992   i = start;
01993   while (i < end)
01994     {
01995       if (real->str[i] == substr[0])
01996         {
01997           int j = i + 1;
01998           
01999           while (j < end)
02000             {
02001               if (substr[j - i] == '\0')
02002                 break;
02003               else if (real->str[j] != substr[j - i])
02004                 break;
02005               
02006               ++j;
02007             }
02008 
02009           if (substr[j - i] == '\0')
02010             {
02011               if (found)
02012                 *found = i;
02013               return TRUE;
02014             }
02015         }
02016       
02017       ++i;
02018     }
02019 
02020   if (found)
02021     *found = end;
02022   
02023   return FALSE;  
02024 }
02025 
02036 dbus_bool_t
02037 _dbus_string_find_blank (const DBusString *str,
02038                          int               start,
02039                          int              *found)
02040 {
02041   int i;
02042   DBUS_CONST_STRING_PREAMBLE (str);
02043   _dbus_assert (start <= real->len);
02044   _dbus_assert (start >= 0);
02045   
02046   i = start;
02047   while (i < real->len)
02048     {
02049       if (real->str[i] == ' ' ||
02050           real->str[i] == '\t')
02051         {
02052           if (found)
02053             *found = i;
02054           return TRUE;
02055         }
02056       
02057       ++i;
02058     }
02059 
02060   if (found)
02061     *found = real->len;
02062   
02063   return FALSE;
02064 }
02065 
02074 void
02075 _dbus_string_skip_blank (const DBusString *str,
02076                          int               start,
02077                          int              *end)
02078 {
02079   int i;
02080   DBUS_CONST_STRING_PREAMBLE (str);
02081   _dbus_assert (start <= real->len);
02082   _dbus_assert (start >= 0);
02083   
02084   i = start;
02085   while (i < real->len)
02086     {
02087       if (!DBUS_IS_ASCII_BLANK (real->str[i]))
02088         break;
02089       
02090       ++i;
02091     }
02092 
02093   _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
02094   
02095   if (end)
02096     *end = i;
02097 }
02098 
02099 
02108 void
02109 _dbus_string_skip_white (const DBusString *str,
02110                          int               start,
02111                          int              *end)
02112 {
02113   int i;
02114   DBUS_CONST_STRING_PREAMBLE (str);
02115   _dbus_assert (start <= real->len);
02116   _dbus_assert (start >= 0);
02117   
02118   i = start;
02119   while (i < real->len)
02120     {
02121       if (!DBUS_IS_ASCII_WHITE (real->str[i]))
02122         break;
02123       
02124       ++i;
02125     }
02126 
02127   _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
02128   
02129   if (end)
02130     *end = i;
02131 }
02132 
02141 void
02142 _dbus_string_skip_white_reverse (const DBusString *str,
02143                                  int               end,
02144                                  int              *start)
02145 {
02146   int i;
02147   DBUS_CONST_STRING_PREAMBLE (str);
02148   _dbus_assert (end <= real->len);
02149   _dbus_assert (end >= 0);
02150   
02151   i = end;
02152   while (i > 0)
02153     {
02154       if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
02155         break;
02156       --i;
02157     }
02158 
02159   _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
02160   
02161   if (start)
02162     *start = i;
02163 }
02164 
02180 dbus_bool_t
02181 _dbus_string_pop_line (DBusString *source,
02182                        DBusString *dest)
02183 {
02184   int eol, eol_len;
02185   
02186   _dbus_string_set_length (dest, 0);
02187   
02188   eol = 0;
02189   eol_len = 0;
02190   if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
02191     {
02192       _dbus_assert (eol == _dbus_string_get_length (source));
02193       if (eol == 0)
02194         {
02195           /* If there's no newline and source has zero length, we're done */
02196           return FALSE;
02197         }
02198       /* otherwise, the last line of the file has no eol characters */
02199     }
02200 
02201   /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
02202    * since find_eol returned TRUE
02203    */
02204   
02205   if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
02206     return FALSE;
02207   
02208   /* remove line ending */
02209   if (!_dbus_string_set_length (dest, eol))
02210     {
02211       _dbus_assert_not_reached ("out of memory when shortening a string");
02212       return FALSE;
02213     }
02214 
02215   return TRUE;
02216 }
02217 
02218 #ifdef DBUS_BUILD_TESTS
02219 
02225 void
02226 _dbus_string_delete_first_word (DBusString *str)
02227 {
02228   int i;
02229   
02230   if (_dbus_string_find_blank (str, 0, &i))
02231     _dbus_string_skip_blank (str, i, &i);
02232 
02233   _dbus_string_delete (str, 0, i);
02234 }
02235 #endif
02236 
02237 #ifdef DBUS_BUILD_TESTS
02238 
02243 void
02244 _dbus_string_delete_leading_blanks (DBusString *str)
02245 {
02246   int i;
02247   
02248   _dbus_string_skip_blank (str, 0, &i);
02249 
02250   if (i > 0)
02251     _dbus_string_delete (str, 0, i);
02252 }
02253 #endif
02254 
02260 void
02261 _dbus_string_chop_white(DBusString *str)
02262 {
02263   int i;
02264   
02265   _dbus_string_skip_white (str, 0, &i);
02266 
02267   if (i > 0)
02268     _dbus_string_delete (str, 0, i);
02269   
02270   _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
02271 
02272   _dbus_string_set_length (str, i);
02273 }
02274 
02284 dbus_bool_t
02285 _dbus_string_equal (const DBusString *a,
02286                     const DBusString *b)
02287 {
02288   const unsigned char *ap;
02289   const unsigned char *bp;
02290   const unsigned char *a_end;
02291   const DBusRealString *real_a = (const DBusRealString*) a;
02292   const DBusRealString *real_b = (const DBusRealString*) b;
02293   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02294   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02295 
02296   if (real_a->len != real_b->len)
02297     return FALSE;
02298 
02299   ap = real_a->str;
02300   bp = real_b->str;
02301   a_end = real_a->str + real_a->len;
02302   while (ap != a_end)
02303     {
02304       if (*ap != *bp)
02305         return FALSE;
02306       
02307       ++ap;
02308       ++bp;
02309     }
02310 
02311   return TRUE;
02312 }
02313 
02314 #ifdef DBUS_BUILD_TESTS
02315 
02328 dbus_bool_t
02329 _dbus_string_equal_len (const DBusString *a,
02330                         const DBusString *b,
02331                         int               len)
02332 {
02333   const unsigned char *ap;
02334   const unsigned char *bp;
02335   const unsigned char *a_end;
02336   const DBusRealString *real_a = (const DBusRealString*) a;
02337   const DBusRealString *real_b = (const DBusRealString*) b;
02338   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02339   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02340 
02341   if (real_a->len != real_b->len &&
02342       (real_a->len < len || real_b->len < len))
02343     return FALSE;
02344 
02345   ap = real_a->str;
02346   bp = real_b->str;
02347   a_end = real_a->str + MIN (real_a->len, len);
02348   while (ap != a_end)
02349     {
02350       if (*ap != *bp)
02351         return FALSE;
02352       
02353       ++ap;
02354       ++bp;
02355     }
02356 
02357   return TRUE;
02358 }
02359 #endif /* DBUS_BUILD_TESTS */
02360 
02377 dbus_bool_t
02378 _dbus_string_equal_substring (const DBusString  *a,
02379                               int                a_start,
02380                               int                a_len,
02381                               const DBusString  *b,
02382                               int                b_start)
02383 {
02384   const unsigned char *ap;
02385   const unsigned char *bp;
02386   const unsigned char *a_end;
02387   const DBusRealString *real_a = (const DBusRealString*) a;
02388   const DBusRealString *real_b = (const DBusRealString*) b;
02389   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02390   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02391   _dbus_assert (a_start >= 0);
02392   _dbus_assert (a_len >= 0);
02393   _dbus_assert (a_start <= real_a->len);
02394   _dbus_assert (a_len <= real_a->len - a_start);
02395   _dbus_assert (b_start >= 0);
02396   _dbus_assert (b_start <= real_b->len);
02397   
02398   if (a_len > real_b->len - b_start)
02399     return FALSE;
02400 
02401   ap = real_a->str + a_start;
02402   bp = real_b->str + b_start;
02403   a_end = ap + a_len;
02404   while (ap != a_end)
02405     {
02406       if (*ap != *bp)
02407         return FALSE;
02408       
02409       ++ap;
02410       ++bp;
02411     }
02412 
02413   _dbus_assert (bp <= (real_b->str + real_b->len));
02414   
02415   return TRUE;
02416 }
02417 
02425 dbus_bool_t
02426 _dbus_string_equal_c_str (const DBusString *a,
02427                           const char       *c_str)
02428 {
02429   const unsigned char *ap;
02430   const unsigned char *bp;
02431   const unsigned char *a_end;
02432   const DBusRealString *real_a = (const DBusRealString*) a;
02433   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02434   _dbus_assert (c_str != NULL);
02435   
02436   ap = real_a->str;
02437   bp = (const unsigned char*) c_str;
02438   a_end = real_a->str + real_a->len;
02439   while (ap != a_end && *bp)
02440     {
02441       if (*ap != *bp)
02442         return FALSE;
02443       
02444       ++ap;
02445       ++bp;
02446     }
02447 
02448   if (ap != a_end || *bp)
02449     return FALSE;
02450   
02451   return TRUE;
02452 }
02453 
02454 #ifdef DBUS_BUILD_TESTS
02455 
02462 dbus_bool_t
02463 _dbus_string_starts_with_c_str (const DBusString *a,
02464                                 const char       *c_str)
02465 {
02466   const unsigned char *ap;
02467   const unsigned char *bp;
02468   const unsigned char *a_end;
02469   const DBusRealString *real_a = (const DBusRealString*) a;
02470   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02471   _dbus_assert (c_str != NULL);
02472   
02473   ap = real_a->str;
02474   bp = (const unsigned char*) c_str;
02475   a_end = real_a->str + real_a->len;
02476   while (ap != a_end && *bp)
02477     {
02478       if (*ap != *bp)
02479         return FALSE;
02480       
02481       ++ap;
02482       ++bp;
02483     }
02484 
02485   if (*bp == '\0')
02486     return TRUE;
02487   else
02488     return FALSE;
02489 }
02490 #endif /* DBUS_BUILD_TESTS */
02491 
02500 dbus_bool_t
02501 _dbus_string_append_byte_as_hex (DBusString *str,
02502                                  int         byte)
02503 {
02504   const char hexdigits[16] = {
02505     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
02506     'a', 'b', 'c', 'd', 'e', 'f'
02507   };
02508 
02509   if (!_dbus_string_append_byte (str,
02510                                  hexdigits[(byte >> 4)]))
02511     return FALSE;
02512   
02513   if (!_dbus_string_append_byte (str,
02514                                  hexdigits[(byte & 0x0f)]))
02515     {
02516       _dbus_string_set_length (str,
02517                                _dbus_string_get_length (str) - 1);
02518       return FALSE;
02519     }
02520 
02521   return TRUE;
02522 }
02523 
02534 dbus_bool_t
02535 _dbus_string_hex_encode (const DBusString *source,
02536                          int               start,
02537                          DBusString       *dest,
02538                          int               insert_at)
02539 {
02540   DBusString result;
02541   const unsigned char *p;
02542   const unsigned char *end;
02543   dbus_bool_t retval;
02544   
02545   _dbus_assert (start <= _dbus_string_get_length (source));
02546 
02547   if (!_dbus_string_init (&result))
02548     return FALSE;
02549 
02550   retval = FALSE;
02551   
02552   p = (const unsigned char*) _dbus_string_get_const_data (source);
02553   end = p + _dbus_string_get_length (source);
02554   p += start;
02555   
02556   while (p != end)
02557     {
02558       if (!_dbus_string_append_byte_as_hex (&result, *p))
02559         goto out;
02560       
02561       ++p;
02562     }
02563 
02564   if (!_dbus_string_move (&result, 0, dest, insert_at))
02565     goto out;
02566 
02567   retval = TRUE;
02568 
02569  out:
02570   _dbus_string_free (&result);
02571   return retval;
02572 }
02573 
02584 dbus_bool_t
02585 _dbus_string_hex_decode (const DBusString *source,
02586                          int               start,
02587                          int              *end_return,
02588                          DBusString       *dest,
02589                          int               insert_at)
02590 {
02591   DBusString result;
02592   const unsigned char *p;
02593   const unsigned char *end;
02594   dbus_bool_t retval;
02595   dbus_bool_t high_bits;
02596   
02597   _dbus_assert (start <= _dbus_string_get_length (source));
02598 
02599   if (!_dbus_string_init (&result))
02600     return FALSE;
02601 
02602   retval = FALSE;
02603 
02604   high_bits = TRUE;
02605   p = (const unsigned char*) _dbus_string_get_const_data (source);
02606   end = p + _dbus_string_get_length (source);
02607   p += start;
02608   
02609   while (p != end)
02610     {
02611       unsigned int val;
02612 
02613       switch (*p)
02614         {
02615         case '0':
02616           val = 0;
02617           break;
02618         case '1':
02619           val = 1;
02620           break;
02621         case '2':
02622           val = 2;
02623           break;
02624         case '3':
02625           val = 3;
02626           break;
02627         case '4':
02628           val = 4;
02629           break;
02630         case '5':
02631           val = 5;
02632           break;
02633         case '6':
02634           val = 6;
02635           break;
02636         case '7':
02637           val = 7;
02638           break;
02639         case '8':
02640           val = 8;
02641           break;
02642         case '9':
02643           val = 9;
02644           break;
02645         case 'a':
02646         case 'A':
02647           val = 10;
02648           break;
02649         case 'b':
02650         case 'B':
02651           val = 11;
02652           break;
02653         case 'c':
02654         case 'C':
02655           val = 12;
02656           break;
02657         case 'd':
02658         case 'D':
02659           val = 13;
02660           break;
02661         case 'e':
02662         case 'E':
02663           val = 14;
02664           break;
02665         case 'f':
02666         case 'F':
02667           val = 15;
02668           break;
02669         default:
02670           goto done;
02671         }
02672 
02673       if (high_bits)
02674         {
02675           if (!_dbus_string_append_byte (&result,
02676                                          val << 4))
02677             goto out;
02678         }
02679       else
02680         {
02681           int len;
02682           unsigned char b;
02683 
02684           len = _dbus_string_get_length (&result);
02685           
02686           b = _dbus_string_get_byte (&result, len - 1);
02687 
02688           b |= val;
02689 
02690           _dbus_string_set_byte (&result, len - 1, b);
02691         }
02692 
02693       high_bits = !high_bits;
02694 
02695       ++p;
02696     }
02697 
02698  done:
02699   if (!_dbus_string_move (&result, 0, dest, insert_at))
02700     goto out;
02701 
02702   if (end_return)
02703     *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
02704 
02705   retval = TRUE;
02706   
02707  out:
02708   _dbus_string_free (&result);  
02709   return retval;
02710 }
02711 
02725 dbus_bool_t
02726 _dbus_string_validate_ascii (const DBusString *str,
02727                              int               start,
02728                              int               len)
02729 {
02730   const unsigned char *s;
02731   const unsigned char *end;
02732   DBUS_CONST_STRING_PREAMBLE (str);
02733   _dbus_assert (start >= 0);
02734   _dbus_assert (start <= real->len);
02735   _dbus_assert (len >= 0);
02736   
02737   if (len > real->len - start)
02738     return FALSE;
02739   
02740   s = real->str + start;
02741   end = s + len;
02742   while (s != end)
02743     {
02744       if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
02745         return FALSE;
02746         
02747       ++s;
02748     }
02749   
02750   return TRUE;
02751 }
02752 
02768 dbus_bool_t
02769 _dbus_string_validate_utf8  (const DBusString *str,
02770                              int               start,
02771                              int               len)
02772 {
02773   const unsigned char *p;
02774   const unsigned char *end;
02775   DBUS_CONST_STRING_PREAMBLE (str);
02776   _dbus_assert (start >= 0);
02777   _dbus_assert (start <= real->len);
02778   _dbus_assert (len >= 0);
02779 
02780   /* we are doing _DBUS_UNLIKELY() here which might be
02781    * dubious in a generic library like GLib, but in D-Bus
02782    * we know we're validating messages and that it would
02783    * only be evil/broken apps that would have invalid
02784    * UTF-8. Also, this function seems to be a performance
02785    * bottleneck in profiles.
02786    */
02787   
02788   if (_DBUS_UNLIKELY (len > real->len - start))
02789     return FALSE;
02790   
02791   p = real->str + start;
02792   end = p + len;
02793   
02794   while (p < end)
02795     {
02796       int i, mask, char_len;
02797       dbus_unichar_t result;
02798 
02799       /* nul bytes considered invalid */
02800       if (*p == '\0')
02801         break;
02802       
02803       /* Special-case ASCII; this makes us go a lot faster in
02804        * D-Bus profiles where we are typically validating
02805        * function names and such. We have to know that
02806        * all following checks will pass for ASCII though,
02807        * comments follow ...
02808        */      
02809       if (*p < 128)
02810         {
02811           ++p;
02812           continue;
02813         }
02814       
02815       UTF8_COMPUTE (*p, mask, char_len);
02816 
02817       if (_DBUS_UNLIKELY (char_len == 0))  /* ASCII: char_len == 1 */
02818         break;
02819 
02820       /* check that the expected number of bytes exists in the remaining length */
02821       if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
02822         break;
02823         
02824       UTF8_GET (result, p, i, mask, char_len);
02825 
02826       /* Check for overlong UTF-8 */
02827       if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
02828         break;
02829 #if 0
02830       /* The UNICODE_VALID check below will catch this */
02831       if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
02832         break;
02833 #endif
02834 
02835       if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
02836         break;
02837 
02838       /* UNICODE_VALID should have caught it */
02839       _dbus_assert (result != (dbus_unichar_t)-1);
02840       
02841       p += char_len;
02842     }
02843 
02844   /* See that we covered the entire length if a length was
02845    * passed in
02846    */
02847   if (_DBUS_UNLIKELY (p != end))
02848     return FALSE;
02849   else
02850     return TRUE;
02851 }
02852 
02866 dbus_bool_t
02867 _dbus_string_validate_nul (const DBusString *str,
02868                            int               start,
02869                            int               len)
02870 {
02871   const unsigned char *s;
02872   const unsigned char *end;
02873   DBUS_CONST_STRING_PREAMBLE (str);
02874   _dbus_assert (start >= 0);
02875   _dbus_assert (len >= 0);
02876   _dbus_assert (start <= real->len);
02877   
02878   if (len > real->len - start)
02879     return FALSE;
02880   
02881   s = real->str + start;
02882   end = s + len;
02883   while (s != end)
02884     {
02885       if (_DBUS_UNLIKELY (*s != '\0'))
02886         return FALSE;
02887       ++s;
02888     }
02889   
02890   return TRUE;
02891 }
02892 
02898 void
02899 _dbus_string_zero (DBusString *str)
02900 {
02901   DBUS_STRING_PREAMBLE (str);
02902 
02903   memset (real->str - real->align_offset, '\0', real->allocated);
02904 }
02907 /* tests are in dbus-string-util.c */

Generated on Tue Feb 24 16:40:40 2009 for D-Bus by  doxygen 1.5.1