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 
01680 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
01681  * Pennington, and Tom Tromey are the authors and authorized relicense.
01682  */
01683 
01689 #define UTF8_COMPUTE(Char, Mask, Len)                                         \
01690   if (Char < 128)                                                             \
01691     {                                                                         \
01692       Len = 1;                                                                \
01693       Mask = 0x7f;                                                            \
01694     }                                                                         \
01695   else if ((Char & 0xe0) == 0xc0)                                             \
01696     {                                                                         \
01697       Len = 2;                                                                \
01698       Mask = 0x1f;                                                            \
01699     }                                                                         \
01700   else if ((Char & 0xf0) == 0xe0)                                             \
01701     {                                                                         \
01702       Len = 3;                                                                \
01703       Mask = 0x0f;                                                            \
01704     }                                                                         \
01705   else if ((Char & 0xf8) == 0xf0)                                             \
01706     {                                                                         \
01707       Len = 4;                                                                \
01708       Mask = 0x07;                                                            \
01709     }                                                                         \
01710   else if ((Char & 0xfc) == 0xf8)                                             \
01711     {                                                                         \
01712       Len = 5;                                                                \
01713       Mask = 0x03;                                                            \
01714     }                                                                         \
01715   else if ((Char & 0xfe) == 0xfc)                                             \
01716     {                                                                         \
01717       Len = 6;                                                                \
01718       Mask = 0x01;                                                            \
01719     }                                                                         \
01720   else                                                                        \
01721     {                                                                         \
01722       Len = 0;                                                               \
01723       Mask = 0;                                                               \
01724     }
01725 
01730 #define UTF8_LENGTH(Char)              \
01731   ((Char) < 0x80 ? 1 :                 \
01732    ((Char) < 0x800 ? 2 :               \
01733     ((Char) < 0x10000 ? 3 :            \
01734      ((Char) < 0x200000 ? 4 :          \
01735       ((Char) < 0x4000000 ? 5 : 6)))))
01736    
01746 #define UTF8_GET(Result, Chars, Count, Mask, Len)                             \
01747   (Result) = (Chars)[0] & (Mask);                                             \
01748   for ((Count) = 1; (Count) < (Len); ++(Count))                               \
01749     {                                                                         \
01750       if (((Chars)[(Count)] & 0xc0) != 0x80)                                  \
01751         {                                                                     \
01752           (Result) = -1;                                                      \
01753           break;                                                              \
01754         }                                                                     \
01755       (Result) <<= 6;                                                         \
01756       (Result) |= ((Chars)[(Count)] & 0x3f);                                  \
01757     }
01758 
01764 #define UNICODE_VALID(Char)                   \
01765     ((Char) < 0x110000 &&                     \
01766      (((Char) & 0xFFFFF800) != 0xD800) &&     \
01767      ((Char) < 0xFDD0 || (Char) > 0xFDEF) &&  \
01768      ((Char) & 0xFFFF) != 0xFFFF)
01769 
01770 #ifdef DBUS_BUILD_TESTS
01771 
01781 void
01782 _dbus_string_get_unichar (const DBusString *str,
01783                           int               start,
01784                           dbus_unichar_t   *ch_return,
01785                           int              *end_return)
01786 {
01787   int i, mask, len;
01788   dbus_unichar_t result;
01789   unsigned char c;
01790   unsigned char *p;
01791   DBUS_CONST_STRING_PREAMBLE (str);
01792   _dbus_assert (start >= 0);
01793   _dbus_assert (start <= real->len);
01794   
01795   if (ch_return)
01796     *ch_return = 0;
01797   if (end_return)
01798     *end_return = real->len;
01799   
01800   mask = 0;
01801   p = real->str + start;
01802   c = *p;
01803   
01804   UTF8_COMPUTE (c, mask, len);
01805   if (len == 0)
01806     return;
01807   UTF8_GET (result, p, i, mask, len);
01808 
01809   if (result == (dbus_unichar_t)-1)
01810     return;
01811 
01812   if (ch_return)
01813     *ch_return = result;
01814   if (end_return)
01815     *end_return = start + len;
01816 }
01817 #endif /* DBUS_BUILD_TESTS */
01818 
01833 dbus_bool_t
01834 _dbus_string_find (const DBusString *str,
01835                    int               start,
01836                    const char       *substr,
01837                    int              *found)
01838 {
01839   return _dbus_string_find_to (str, start,
01840                                ((const DBusRealString*)str)->len,
01841                                substr, found);
01842 }
01843 
01856 dbus_bool_t
01857 _dbus_string_find_eol (const DBusString *str,
01858                        int               start,
01859                        int              *found,
01860                        int              *found_len)
01861 {
01862   int i;
01863 
01864   DBUS_CONST_STRING_PREAMBLE (str);
01865   _dbus_assert (start <= real->len);
01866   _dbus_assert (start >= 0);
01867   
01868   i = start;
01869   while (i < real->len)
01870     {
01871       if (real->str[i] == '\r') 
01872         {
01873           if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
01874             {
01875               if (found) 
01876                 *found = i;
01877               if (found_len)
01878                 *found_len = 2;
01879               return TRUE;
01880             } 
01881           else /* only "\r" */
01882             {
01883               if (found) 
01884                 *found = i;
01885               if (found_len)
01886                 *found_len = 1;
01887               return TRUE;
01888             }
01889         } 
01890       else if (real->str[i] == '\n')  /* only "\n" */
01891         {
01892           if (found) 
01893             *found = i;
01894           if (found_len)
01895             *found_len = 1;
01896           return TRUE;
01897         }
01898       ++i;
01899     }
01900 
01901   if (found)
01902     *found = real->len;
01903 
01904   if (found_len)
01905     *found_len = 0;
01906   
01907   return FALSE;
01908 }
01909 
01926 dbus_bool_t
01927 _dbus_string_find_to (const DBusString *str,
01928                       int               start,
01929                       int               end,
01930                       const char       *substr,
01931                       int              *found)
01932 {
01933   int i;
01934   DBUS_CONST_STRING_PREAMBLE (str);
01935   _dbus_assert (substr != NULL);
01936   _dbus_assert (start <= real->len);
01937   _dbus_assert (start >= 0);
01938   _dbus_assert (substr != NULL);
01939   _dbus_assert (end <= real->len);
01940   _dbus_assert (start <= end);
01941 
01942   /* we always "find" an empty string */
01943   if (*substr == '\0')
01944     {
01945       if (found)
01946         *found = start;
01947       return TRUE;
01948     }
01949 
01950   i = start;
01951   while (i < end)
01952     {
01953       if (real->str[i] == substr[0])
01954         {
01955           int j = i + 1;
01956           
01957           while (j < end)
01958             {
01959               if (substr[j - i] == '\0')
01960                 break;
01961               else if (real->str[j] != substr[j - i])
01962                 break;
01963               
01964               ++j;
01965             }
01966 
01967           if (substr[j - i] == '\0')
01968             {
01969               if (found)
01970                 *found = i;
01971               return TRUE;
01972             }
01973         }
01974       
01975       ++i;
01976     }
01977 
01978   if (found)
01979     *found = end;
01980   
01981   return FALSE;  
01982 }
01983 
01994 dbus_bool_t
01995 _dbus_string_find_blank (const DBusString *str,
01996                          int               start,
01997                          int              *found)
01998 {
01999   int i;
02000   DBUS_CONST_STRING_PREAMBLE (str);
02001   _dbus_assert (start <= real->len);
02002   _dbus_assert (start >= 0);
02003   
02004   i = start;
02005   while (i < real->len)
02006     {
02007       if (real->str[i] == ' ' ||
02008           real->str[i] == '\t')
02009         {
02010           if (found)
02011             *found = i;
02012           return TRUE;
02013         }
02014       
02015       ++i;
02016     }
02017 
02018   if (found)
02019     *found = real->len;
02020   
02021   return FALSE;
02022 }
02023 
02032 void
02033 _dbus_string_skip_blank (const DBusString *str,
02034                          int               start,
02035                          int              *end)
02036 {
02037   int i;
02038   DBUS_CONST_STRING_PREAMBLE (str);
02039   _dbus_assert (start <= real->len);
02040   _dbus_assert (start >= 0);
02041   
02042   i = start;
02043   while (i < real->len)
02044     {
02045       if (!DBUS_IS_ASCII_BLANK (real->str[i]))
02046         break;
02047       
02048       ++i;
02049     }
02050 
02051   _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
02052   
02053   if (end)
02054     *end = i;
02055 }
02056 
02057 
02066 void
02067 _dbus_string_skip_white (const DBusString *str,
02068                          int               start,
02069                          int              *end)
02070 {
02071   int i;
02072   DBUS_CONST_STRING_PREAMBLE (str);
02073   _dbus_assert (start <= real->len);
02074   _dbus_assert (start >= 0);
02075   
02076   i = start;
02077   while (i < real->len)
02078     {
02079       if (!DBUS_IS_ASCII_WHITE (real->str[i]))
02080         break;
02081       
02082       ++i;
02083     }
02084 
02085   _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
02086   
02087   if (end)
02088     *end = i;
02089 }
02090 
02099 void
02100 _dbus_string_skip_white_reverse (const DBusString *str,
02101                                  int               end,
02102                                  int              *start)
02103 {
02104   int i;
02105   DBUS_CONST_STRING_PREAMBLE (str);
02106   _dbus_assert (end <= real->len);
02107   _dbus_assert (end >= 0);
02108   
02109   i = end;
02110   while (i > 0)
02111     {
02112       if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
02113         break;
02114       --i;
02115     }
02116 
02117   _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
02118   
02119   if (start)
02120     *start = i;
02121 }
02122 
02138 dbus_bool_t
02139 _dbus_string_pop_line (DBusString *source,
02140                        DBusString *dest)
02141 {
02142   int eol, eol_len;
02143   
02144   _dbus_string_set_length (dest, 0);
02145   
02146   eol = 0;
02147   eol_len = 0;
02148   if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
02149     {
02150       _dbus_assert (eol == _dbus_string_get_length (source));
02151       if (eol == 0)
02152         {
02153           /* If there's no newline and source has zero length, we're done */
02154           return FALSE;
02155         }
02156       /* otherwise, the last line of the file has no eol characters */
02157     }
02158 
02159   /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
02160    * since find_eol returned TRUE
02161    */
02162   
02163   if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
02164     return FALSE;
02165   
02166   /* remove line ending */
02167   if (!_dbus_string_set_length (dest, eol))
02168     {
02169       _dbus_assert_not_reached ("out of memory when shortening a string");
02170       return FALSE;
02171     }
02172 
02173   return TRUE;
02174 }
02175 
02176 #ifdef DBUS_BUILD_TESTS
02177 
02183 void
02184 _dbus_string_delete_first_word (DBusString *str)
02185 {
02186   int i;
02187   
02188   if (_dbus_string_find_blank (str, 0, &i))
02189     _dbus_string_skip_blank (str, i, &i);
02190 
02191   _dbus_string_delete (str, 0, i);
02192 }
02193 #endif
02194 
02195 #ifdef DBUS_BUILD_TESTS
02196 
02201 void
02202 _dbus_string_delete_leading_blanks (DBusString *str)
02203 {
02204   int i;
02205   
02206   _dbus_string_skip_blank (str, 0, &i);
02207 
02208   if (i > 0)
02209     _dbus_string_delete (str, 0, i);
02210 }
02211 #endif
02212 
02218 void
02219 _dbus_string_chop_white(DBusString *str)
02220 {
02221   int i;
02222   
02223   _dbus_string_skip_white (str, 0, &i);
02224 
02225   if (i > 0)
02226     _dbus_string_delete (str, 0, i);
02227   
02228   _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
02229 
02230   _dbus_string_set_length (str, i);
02231 }
02232 
02242 dbus_bool_t
02243 _dbus_string_equal (const DBusString *a,
02244                     const DBusString *b)
02245 {
02246   const unsigned char *ap;
02247   const unsigned char *bp;
02248   const unsigned char *a_end;
02249   const DBusRealString *real_a = (const DBusRealString*) a;
02250   const DBusRealString *real_b = (const DBusRealString*) b;
02251   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02252   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02253 
02254   if (real_a->len != real_b->len)
02255     return FALSE;
02256 
02257   ap = real_a->str;
02258   bp = real_b->str;
02259   a_end = real_a->str + real_a->len;
02260   while (ap != a_end)
02261     {
02262       if (*ap != *bp)
02263         return FALSE;
02264       
02265       ++ap;
02266       ++bp;
02267     }
02268 
02269   return TRUE;
02270 }
02271 
02272 #ifdef DBUS_BUILD_TESTS
02273 
02286 dbus_bool_t
02287 _dbus_string_equal_len (const DBusString *a,
02288                         const DBusString *b,
02289                         int               len)
02290 {
02291   const unsigned char *ap;
02292   const unsigned char *bp;
02293   const unsigned char *a_end;
02294   const DBusRealString *real_a = (const DBusRealString*) a;
02295   const DBusRealString *real_b = (const DBusRealString*) b;
02296   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02297   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02298 
02299   if (real_a->len != real_b->len &&
02300       (real_a->len < len || real_b->len < len))
02301     return FALSE;
02302 
02303   ap = real_a->str;
02304   bp = real_b->str;
02305   a_end = real_a->str + MIN (real_a->len, len);
02306   while (ap != a_end)
02307     {
02308       if (*ap != *bp)
02309         return FALSE;
02310       
02311       ++ap;
02312       ++bp;
02313     }
02314 
02315   return TRUE;
02316 }
02317 #endif /* DBUS_BUILD_TESTS */
02318 
02335 dbus_bool_t
02336 _dbus_string_equal_substring (const DBusString  *a,
02337                               int                a_start,
02338                               int                a_len,
02339                               const DBusString  *b,
02340                               int                b_start)
02341 {
02342   const unsigned char *ap;
02343   const unsigned char *bp;
02344   const unsigned char *a_end;
02345   const DBusRealString *real_a = (const DBusRealString*) a;
02346   const DBusRealString *real_b = (const DBusRealString*) b;
02347   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02348   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02349   _dbus_assert (a_start >= 0);
02350   _dbus_assert (a_len >= 0);
02351   _dbus_assert (a_start <= real_a->len);
02352   _dbus_assert (a_len <= real_a->len - a_start);
02353   _dbus_assert (b_start >= 0);
02354   _dbus_assert (b_start <= real_b->len);
02355   
02356   if (a_len > real_b->len - b_start)
02357     return FALSE;
02358 
02359   ap = real_a->str + a_start;
02360   bp = real_b->str + b_start;
02361   a_end = ap + a_len;
02362   while (ap != a_end)
02363     {
02364       if (*ap != *bp)
02365         return FALSE;
02366       
02367       ++ap;
02368       ++bp;
02369     }
02370 
02371   _dbus_assert (bp <= (real_b->str + real_b->len));
02372   
02373   return TRUE;
02374 }
02375 
02383 dbus_bool_t
02384 _dbus_string_equal_c_str (const DBusString *a,
02385                           const char       *c_str)
02386 {
02387   const unsigned char *ap;
02388   const unsigned char *bp;
02389   const unsigned char *a_end;
02390   const DBusRealString *real_a = (const DBusRealString*) a;
02391   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02392   _dbus_assert (c_str != NULL);
02393   
02394   ap = real_a->str;
02395   bp = (const unsigned char*) c_str;
02396   a_end = real_a->str + real_a->len;
02397   while (ap != a_end && *bp)
02398     {
02399       if (*ap != *bp)
02400         return FALSE;
02401       
02402       ++ap;
02403       ++bp;
02404     }
02405 
02406   if (ap != a_end || *bp)
02407     return FALSE;
02408   
02409   return TRUE;
02410 }
02411 
02412 #ifdef DBUS_BUILD_TESTS
02413 
02420 dbus_bool_t
02421 _dbus_string_starts_with_c_str (const DBusString *a,
02422                                 const char       *c_str)
02423 {
02424   const unsigned char *ap;
02425   const unsigned char *bp;
02426   const unsigned char *a_end;
02427   const DBusRealString *real_a = (const DBusRealString*) a;
02428   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02429   _dbus_assert (c_str != NULL);
02430   
02431   ap = real_a->str;
02432   bp = (const unsigned char*) c_str;
02433   a_end = real_a->str + real_a->len;
02434   while (ap != a_end && *bp)
02435     {
02436       if (*ap != *bp)
02437         return FALSE;
02438       
02439       ++ap;
02440       ++bp;
02441     }
02442 
02443   if (*bp == '\0')
02444     return TRUE;
02445   else
02446     return FALSE;
02447 }
02448 #endif /* DBUS_BUILD_TESTS */
02449 
02458 dbus_bool_t
02459 _dbus_string_append_byte_as_hex (DBusString *str,
02460                                  int         byte)
02461 {
02462   const char hexdigits[16] = {
02463     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
02464     'a', 'b', 'c', 'd', 'e', 'f'
02465   };
02466 
02467   if (!_dbus_string_append_byte (str,
02468                                  hexdigits[(byte >> 4)]))
02469     return FALSE;
02470   
02471   if (!_dbus_string_append_byte (str,
02472                                  hexdigits[(byte & 0x0f)]))
02473     {
02474       _dbus_string_set_length (str,
02475                                _dbus_string_get_length (str) - 1);
02476       return FALSE;
02477     }
02478 
02479   return TRUE;
02480 }
02481 
02492 dbus_bool_t
02493 _dbus_string_hex_encode (const DBusString *source,
02494                          int               start,
02495                          DBusString       *dest,
02496                          int               insert_at)
02497 {
02498   DBusString result;
02499   const unsigned char *p;
02500   const unsigned char *end;
02501   dbus_bool_t retval;
02502   
02503   _dbus_assert (start <= _dbus_string_get_length (source));
02504 
02505   if (!_dbus_string_init (&result))
02506     return FALSE;
02507 
02508   retval = FALSE;
02509   
02510   p = (const unsigned char*) _dbus_string_get_const_data (source);
02511   end = p + _dbus_string_get_length (source);
02512   p += start;
02513   
02514   while (p != end)
02515     {
02516       if (!_dbus_string_append_byte_as_hex (&result, *p))
02517         goto out;
02518       
02519       ++p;
02520     }
02521 
02522   if (!_dbus_string_move (&result, 0, dest, insert_at))
02523     goto out;
02524 
02525   retval = TRUE;
02526 
02527  out:
02528   _dbus_string_free (&result);
02529   return retval;
02530 }
02531 
02542 dbus_bool_t
02543 _dbus_string_hex_decode (const DBusString *source,
02544                          int               start,
02545                          int              *end_return,
02546                          DBusString       *dest,
02547                          int               insert_at)
02548 {
02549   DBusString result;
02550   const unsigned char *p;
02551   const unsigned char *end;
02552   dbus_bool_t retval;
02553   dbus_bool_t high_bits;
02554   
02555   _dbus_assert (start <= _dbus_string_get_length (source));
02556 
02557   if (!_dbus_string_init (&result))
02558     return FALSE;
02559 
02560   retval = FALSE;
02561 
02562   high_bits = TRUE;
02563   p = (const unsigned char*) _dbus_string_get_const_data (source);
02564   end = p + _dbus_string_get_length (source);
02565   p += start;
02566   
02567   while (p != end)
02568     {
02569       unsigned int val;
02570 
02571       switch (*p)
02572         {
02573         case '0':
02574           val = 0;
02575           break;
02576         case '1':
02577           val = 1;
02578           break;
02579         case '2':
02580           val = 2;
02581           break;
02582         case '3':
02583           val = 3;
02584           break;
02585         case '4':
02586           val = 4;
02587           break;
02588         case '5':
02589           val = 5;
02590           break;
02591         case '6':
02592           val = 6;
02593           break;
02594         case '7':
02595           val = 7;
02596           break;
02597         case '8':
02598           val = 8;
02599           break;
02600         case '9':
02601           val = 9;
02602           break;
02603         case 'a':
02604         case 'A':
02605           val = 10;
02606           break;
02607         case 'b':
02608         case 'B':
02609           val = 11;
02610           break;
02611         case 'c':
02612         case 'C':
02613           val = 12;
02614           break;
02615         case 'd':
02616         case 'D':
02617           val = 13;
02618           break;
02619         case 'e':
02620         case 'E':
02621           val = 14;
02622           break;
02623         case 'f':
02624         case 'F':
02625           val = 15;
02626           break;
02627         default:
02628           goto done;
02629         }
02630 
02631       if (high_bits)
02632         {
02633           if (!_dbus_string_append_byte (&result,
02634                                          val << 4))
02635             goto out;
02636         }
02637       else
02638         {
02639           int len;
02640           unsigned char b;
02641 
02642           len = _dbus_string_get_length (&result);
02643           
02644           b = _dbus_string_get_byte (&result, len - 1);
02645 
02646           b |= val;
02647 
02648           _dbus_string_set_byte (&result, len - 1, b);
02649         }
02650 
02651       high_bits = !high_bits;
02652 
02653       ++p;
02654     }
02655 
02656  done:
02657   if (!_dbus_string_move (&result, 0, dest, insert_at))
02658     goto out;
02659 
02660   if (end_return)
02661     *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
02662 
02663   retval = TRUE;
02664   
02665  out:
02666   _dbus_string_free (&result);  
02667   return retval;
02668 }
02669 
02683 dbus_bool_t
02684 _dbus_string_validate_ascii (const DBusString *str,
02685                              int               start,
02686                              int               len)
02687 {
02688   const unsigned char *s;
02689   const unsigned char *end;
02690   DBUS_CONST_STRING_PREAMBLE (str);
02691   _dbus_assert (start >= 0);
02692   _dbus_assert (start <= real->len);
02693   _dbus_assert (len >= 0);
02694   
02695   if (len > real->len - start)
02696     return FALSE;
02697   
02698   s = real->str + start;
02699   end = s + len;
02700   while (s != end)
02701     {
02702       if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
02703         return FALSE;
02704         
02705       ++s;
02706     }
02707   
02708   return TRUE;
02709 }
02710 
02726 dbus_bool_t
02727 _dbus_string_validate_utf8  (const DBusString *str,
02728                              int               start,
02729                              int               len)
02730 {
02731   const unsigned char *p;
02732   const unsigned char *end;
02733   DBUS_CONST_STRING_PREAMBLE (str);
02734   _dbus_assert (start >= 0);
02735   _dbus_assert (start <= real->len);
02736   _dbus_assert (len >= 0);
02737 
02738   /* we are doing _DBUS_UNLIKELY() here which might be
02739    * dubious in a generic library like GLib, but in D-Bus
02740    * we know we're validating messages and that it would
02741    * only be evil/broken apps that would have invalid
02742    * UTF-8. Also, this function seems to be a performance
02743    * bottleneck in profiles.
02744    */
02745   
02746   if (_DBUS_UNLIKELY (len > real->len - start))
02747     return FALSE;
02748   
02749   p = real->str + start;
02750   end = p + len;
02751   
02752   while (p < end)
02753     {
02754       int i, mask, char_len;
02755       dbus_unichar_t result;
02756 
02757       /* nul bytes considered invalid */
02758       if (*p == '\0')
02759         break;
02760       
02761       /* Special-case ASCII; this makes us go a lot faster in
02762        * D-Bus profiles where we are typically validating
02763        * function names and such. We have to know that
02764        * all following checks will pass for ASCII though,
02765        * comments follow ...
02766        */      
02767       if (*p < 128)
02768         {
02769           ++p;
02770           continue;
02771         }
02772       
02773       UTF8_COMPUTE (*p, mask, char_len);
02774 
02775       if (_DBUS_UNLIKELY (char_len == 0))  /* ASCII: char_len == 1 */
02776         break;
02777 
02778       /* check that the expected number of bytes exists in the remaining length */
02779       if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
02780         break;
02781         
02782       UTF8_GET (result, p, i, mask, char_len);
02783 
02784       /* Check for overlong UTF-8 */
02785       if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
02786         break;
02787 #if 0
02788       /* The UNICODE_VALID check below will catch this */
02789       if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
02790         break;
02791 #endif
02792 
02793       if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
02794         break;
02795 
02796       /* UNICODE_VALID should have caught it */
02797       _dbus_assert (result != (dbus_unichar_t)-1);
02798       
02799       p += char_len;
02800     }
02801 
02802   /* See that we covered the entire length if a length was
02803    * passed in
02804    */
02805   if (_DBUS_UNLIKELY (p != end))
02806     return FALSE;
02807   else
02808     return TRUE;
02809 }
02810 
02824 dbus_bool_t
02825 _dbus_string_validate_nul (const DBusString *str,
02826                            int               start,
02827                            int               len)
02828 {
02829   const unsigned char *s;
02830   const unsigned char *end;
02831   DBUS_CONST_STRING_PREAMBLE (str);
02832   _dbus_assert (start >= 0);
02833   _dbus_assert (len >= 0);
02834   _dbus_assert (start <= real->len);
02835   
02836   if (len > real->len - start)
02837     return FALSE;
02838   
02839   s = real->str + start;
02840   end = s + len;
02841   while (s != end)
02842     {
02843       if (_DBUS_UNLIKELY (*s != '\0'))
02844         return FALSE;
02845       ++s;
02846     }
02847   
02848   return TRUE;
02849 }
02850 
02856 void
02857 _dbus_string_zero (DBusString *str)
02858 {
02859   DBUS_STRING_PREAMBLE (str);
02860 
02861   memset (real->str - real->align_offset, '\0', real->allocated);
02862 }
02865 /* tests are in dbus-string-util.c */

Generated on Sat Jun 14 22:43:04 2008 for D-Bus by  doxygen 1.4.6