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 #ifdef DBUS_BUILD_TESTS
00275 /* Not using this feature at the moment,
00276  * so marked DBUS_BUILD_TESTS-only
00277  */
00287 void
00288 _dbus_string_lock (DBusString *str)
00289 {  
00290   DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
00291 
00292   real->locked = TRUE;
00293 
00294   /* Try to realloc to avoid excess memory usage, since
00295    * we know we won't change the string further
00296    */
00297 #define MAX_WASTE 48
00298   if (real->allocated - MAX_WASTE > real->len)
00299     {
00300       unsigned char *new_str;
00301       int new_allocated;
00302 
00303       new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
00304 
00305       new_str = dbus_realloc (real->str - real->align_offset,
00306                               new_allocated);
00307       if (new_str != NULL)
00308         {
00309           real->str = new_str + real->align_offset;
00310           real->allocated = new_allocated;
00311           fixup_alignment (real);
00312         }
00313     }
00314 }
00315 #endif /* DBUS_BUILD_TESTS */
00316 
00317 static dbus_bool_t
00318 reallocate_for_length (DBusRealString *real,
00319                        int             new_length)
00320 {
00321   int new_allocated;
00322   unsigned char *new_str;
00323 
00324   /* at least double our old allocation to avoid O(n), avoiding
00325    * overflow
00326    */
00327   if (real->allocated > (_DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
00328     new_allocated = _DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
00329   else
00330     new_allocated = real->allocated * 2;
00331 
00332   /* if you change the code just above here, run the tests without
00333    * the following assert-only hack before you commit
00334    */
00335   /* This is keyed off asserts in addition to tests so when you
00336    * disable asserts to profile, you don't get this destroyer
00337    * of profiles.
00338    */
00339 #ifdef DBUS_DISABLE_ASSERT
00340 #else
00341 #ifdef DBUS_BUILD_TESTS
00342   new_allocated = 0; /* ensure a realloc every time so that we go
00343                       * through all malloc failure codepaths
00344                       */
00345 #endif /* DBUS_BUILD_TESTS */
00346 #endif /* !DBUS_DISABLE_ASSERT */
00347 
00348   /* But be sure we always alloc at least space for the new length */
00349   new_allocated = MAX (new_allocated,
00350                        new_length + _DBUS_STRING_ALLOCATION_PADDING);
00351 
00352   _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
00353   new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
00354   if (_DBUS_UNLIKELY (new_str == NULL))
00355     return FALSE;
00356 
00357   real->str = new_str + real->align_offset;
00358   real->allocated = new_allocated;
00359   fixup_alignment (real);
00360 
00361   return TRUE;
00362 }
00363 
00364 static dbus_bool_t
00365 set_length (DBusRealString *real,
00366             int             new_length)
00367 {
00368   /* Note, we are setting the length not including nul termination */
00369 
00370   /* exceeding max length is the same as failure to allocate memory */
00371   if (_DBUS_UNLIKELY (new_length > real->max_length))
00372     return FALSE;
00373   else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
00374            _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
00375     return FALSE;
00376   else
00377     {
00378       real->len = new_length;
00379       real->str[new_length] = '\0';
00380       return TRUE;
00381     }
00382 }
00383 
00384 static dbus_bool_t
00385 open_gap (int             len,
00386           DBusRealString *dest,
00387           int             insert_at)
00388 {
00389   if (len == 0)
00390     return TRUE;
00391 
00392   if (len > dest->max_length - dest->len)
00393     return FALSE; /* detected overflow of dest->len + len below */
00394   
00395   if (!set_length (dest, dest->len + len))
00396     return FALSE;
00397 
00398   memmove (dest->str + insert_at + len, 
00399            dest->str + insert_at,
00400            dest->len - len - insert_at);
00401 
00402   return TRUE;
00403 }
00404 
00405 #ifndef _dbus_string_get_data
00406 
00417 char*
00418 _dbus_string_get_data (DBusString *str)
00419 {
00420   DBUS_STRING_PREAMBLE (str);
00421   
00422   return (char*) real->str;
00423 }
00424 #endif /* _dbus_string_get_data */
00425 
00426 /* only do the function if we don't have the macro */
00427 #ifndef _dbus_string_get_const_data
00428 
00434 const char*
00435 _dbus_string_get_const_data (const DBusString  *str)
00436 {
00437   DBUS_CONST_STRING_PREAMBLE (str);
00438   
00439   return (const char*) real->str;
00440 }
00441 #endif /* _dbus_string_get_const_data */
00442 
00456 char*
00457 _dbus_string_get_data_len (DBusString *str,
00458                            int         start,
00459                            int         len)
00460 {
00461   DBUS_STRING_PREAMBLE (str);
00462   _dbus_assert (start >= 0);
00463   _dbus_assert (len >= 0);
00464   _dbus_assert (start <= real->len);
00465   _dbus_assert (len <= real->len - start);
00466   
00467   return (char*) real->str + start;
00468 }
00469 
00470 /* only do the function if we don't have the macro */
00471 #ifndef _dbus_string_get_const_data_len
00472 
00480 const char*
00481 _dbus_string_get_const_data_len (const DBusString  *str,
00482                                  int                start,
00483                                  int                len)
00484 {
00485   DBUS_CONST_STRING_PREAMBLE (str);
00486   _dbus_assert (start >= 0);
00487   _dbus_assert (len >= 0);
00488   _dbus_assert (start <= real->len);
00489   _dbus_assert (len <= real->len - start);
00490   
00491   return (const char*) real->str + start;
00492 }
00493 #endif /* _dbus_string_get_const_data_len */
00494 
00495 /* only do the function if we don't have the macro */
00496 #ifndef _dbus_string_set_byte
00497 
00504 void
00505 _dbus_string_set_byte (DBusString    *str,
00506                        int            i,
00507                        unsigned char  byte)
00508 {
00509   DBUS_STRING_PREAMBLE (str);
00510   _dbus_assert (i < real->len);
00511   _dbus_assert (i >= 0);
00512   
00513   real->str[i] = byte;
00514 }
00515 #endif /* _dbus_string_set_byte */
00516 
00517 /* only have the function if we didn't create a macro */
00518 #ifndef _dbus_string_get_byte
00519 
00528 unsigned char
00529 _dbus_string_get_byte (const DBusString  *str,
00530                        int                start)
00531 {
00532   DBUS_CONST_STRING_PREAMBLE (str);
00533   _dbus_assert (start <= real->len);
00534   _dbus_assert (start >= 0);
00535   
00536   return real->str[start];
00537 }
00538 #endif /* _dbus_string_get_byte */
00539 
00550 dbus_bool_t
00551 _dbus_string_insert_bytes (DBusString   *str,
00552                            int           i,
00553                            int           n_bytes,
00554                            unsigned char byte)
00555 {
00556   DBUS_STRING_PREAMBLE (str);
00557   _dbus_assert (i <= real->len);
00558   _dbus_assert (i >= 0);
00559   _dbus_assert (n_bytes >= 0);
00560 
00561   if (n_bytes == 0)
00562     return TRUE;
00563   
00564   if (!open_gap (n_bytes, real, i))
00565     return FALSE;
00566   
00567   memset (real->str + i, byte, n_bytes);
00568 
00569   return TRUE;
00570 }
00571 
00580 dbus_bool_t
00581 _dbus_string_insert_byte (DBusString   *str,
00582                            int           i,
00583                            unsigned char byte)
00584 {
00585   DBUS_STRING_PREAMBLE (str);
00586   _dbus_assert (i <= real->len);
00587   _dbus_assert (i >= 0);
00588   
00589   if (!open_gap (1, real, i))
00590     return FALSE;
00591 
00592   real->str[i] = byte;
00593 
00594   return TRUE;
00595 }
00596 
00607 dbus_bool_t
00608 _dbus_string_steal_data (DBusString        *str,
00609                          char             **data_return)
00610 {
00611   int old_max_length;
00612   DBUS_STRING_PREAMBLE (str);
00613   _dbus_assert (data_return != NULL);
00614 
00615   undo_alignment (real);
00616   
00617   *data_return = (char*) real->str;
00618 
00619   old_max_length = real->max_length;
00620   
00621   /* reset the string */
00622   if (!_dbus_string_init (str))
00623     {
00624       /* hrm, put it back then */
00625       real->str = (unsigned char*) *data_return;
00626       *data_return = NULL;
00627       fixup_alignment (real);
00628       return FALSE;
00629     }
00630 
00631   real->max_length = old_max_length;
00632 
00633   return TRUE;
00634 }
00635 
00636 #ifdef DBUS_BUILD_TESTS
00637 
00652 dbus_bool_t
00653 _dbus_string_steal_data_len (DBusString        *str,
00654                              char             **data_return,
00655                              int                start,
00656                              int                len)
00657 {
00658   DBusString dest;
00659   DBUS_STRING_PREAMBLE (str);
00660   _dbus_assert (data_return != NULL);
00661   _dbus_assert (start >= 0);
00662   _dbus_assert (len >= 0);
00663   _dbus_assert (start <= real->len);
00664   _dbus_assert (len <= real->len - start);
00665 
00666   if (!_dbus_string_init (&dest))
00667     return FALSE;
00668 
00669   set_max_length (&dest, real->max_length);
00670   
00671   if (!_dbus_string_move_len (str, start, len, &dest, 0))
00672     {
00673       _dbus_string_free (&dest);
00674       return FALSE;
00675     }
00676 
00677   _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
00678   if (!_dbus_string_steal_data (&dest, data_return))
00679     {
00680       _dbus_string_free (&dest);
00681       return FALSE;
00682     }
00683 
00684   _dbus_string_free (&dest);
00685   return TRUE;
00686 }
00687 #endif /* DBUS_BUILD_TESTS */
00688 
00696 dbus_bool_t
00697 _dbus_string_copy_data (const DBusString  *str,
00698                         char             **data_return)
00699 {
00700   DBUS_CONST_STRING_PREAMBLE (str);
00701   _dbus_assert (data_return != NULL);
00702   
00703   *data_return = dbus_malloc (real->len + 1);
00704   if (*data_return == NULL)
00705     return FALSE;
00706 
00707   memcpy (*data_return, real->str, real->len + 1);
00708 
00709   return TRUE;
00710 }
00711 
00720 void
00721 _dbus_string_copy_to_buffer (const DBusString  *str,
00722                              char              *buffer,
00723                              int                avail_len)
00724 {
00725   int copy_len;
00726   DBUS_CONST_STRING_PREAMBLE (str);
00727 
00728   _dbus_assert (avail_len >= 0);
00729 
00730   copy_len = MIN (avail_len, real->len+1);
00731   memcpy (buffer, real->str, copy_len);
00732   if (avail_len > 0 && avail_len == copy_len)
00733     buffer[avail_len-1] = '\0';
00734 }
00735 
00736 #ifdef DBUS_BUILD_TESTS
00737 
00746 dbus_bool_t
00747 _dbus_string_copy_data_len (const DBusString  *str,
00748                             char             **data_return,
00749                             int                start,
00750                             int                len)
00751 {
00752   DBusString dest;
00753 
00754   DBUS_CONST_STRING_PREAMBLE (str);
00755   _dbus_assert (data_return != NULL);
00756   _dbus_assert (start >= 0);
00757   _dbus_assert (len >= 0);
00758   _dbus_assert (start <= real->len);
00759   _dbus_assert (len <= real->len - start);
00760 
00761   if (!_dbus_string_init (&dest))
00762     return FALSE;
00763 
00764   set_max_length (&dest, real->max_length);
00765 
00766   if (!_dbus_string_copy_len (str, start, len, &dest, 0))
00767     {
00768       _dbus_string_free (&dest);
00769       return FALSE;
00770     }
00771 
00772   if (!_dbus_string_steal_data (&dest, data_return))
00773     {
00774       _dbus_string_free (&dest);
00775       return FALSE;
00776     }
00777 
00778   _dbus_string_free (&dest);
00779   return TRUE;
00780 }
00781 #endif /* DBUS_BUILD_TESTS */
00782 
00783 /* Only have the function if we don't have the macro */
00784 #ifndef _dbus_string_get_length
00785 
00790 int
00791 _dbus_string_get_length (const DBusString  *str)
00792 {
00793   DBUS_CONST_STRING_PREAMBLE (str);
00794   
00795   return real->len;
00796 }
00797 #endif /* !_dbus_string_get_length */
00798 
00811 dbus_bool_t
00812 _dbus_string_lengthen (DBusString *str,
00813                        int         additional_length)
00814 {
00815   DBUS_STRING_PREAMBLE (str);  
00816   _dbus_assert (additional_length >= 0);
00817 
00818   if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
00819     return FALSE; /* would overflow */
00820   
00821   return set_length (real,
00822                      real->len + additional_length);
00823 }
00824 
00831 void
00832 _dbus_string_shorten (DBusString *str,
00833                       int         length_to_remove)
00834 {
00835   DBUS_STRING_PREAMBLE (str);
00836   _dbus_assert (length_to_remove >= 0);
00837   _dbus_assert (length_to_remove <= real->len);
00838 
00839   set_length (real,
00840               real->len - length_to_remove);
00841 }
00842 
00853 dbus_bool_t
00854 _dbus_string_set_length (DBusString *str,
00855                          int         length)
00856 {
00857   DBUS_STRING_PREAMBLE (str);
00858   _dbus_assert (length >= 0);
00859 
00860   return set_length (real, length);
00861 }
00862 
00863 static dbus_bool_t
00864 align_insert_point_then_open_gap (DBusString *str,
00865                                   int        *insert_at_p,
00866                                   int         alignment,
00867                                   int         gap_size)
00868 {
00869   unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
00870   unsigned long gap_pos;
00871   int insert_at;
00872   int delta;
00873   DBUS_STRING_PREAMBLE (str);
00874   _dbus_assert (alignment >= 1);
00875   _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
00876 
00877   insert_at = *insert_at_p;
00878 
00879   _dbus_assert (insert_at <= real->len);
00880   
00881   gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
00882   new_len = real->len + (gap_pos - insert_at) + gap_size;
00883   
00884   if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
00885     return FALSE;
00886   
00887   delta = new_len - real->len;
00888   _dbus_assert (delta >= 0);
00889 
00890   if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
00891     {
00892       _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
00893       return TRUE;
00894     }
00895 
00896   if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
00897                                  real, insert_at)))
00898     return FALSE;
00899 
00900   /* nul the padding if we had to add any padding */
00901   if (gap_size < delta)
00902     {
00903       memset (&real->str[insert_at], '\0',
00904               gap_pos - insert_at);
00905     }
00906 
00907   *insert_at_p = gap_pos;
00908   
00909   return TRUE;
00910 }
00911 
00912 static dbus_bool_t
00913 align_length_then_lengthen (DBusString *str,
00914                             int         alignment,
00915                             int         then_lengthen_by)
00916 {
00917   int insert_at;
00918 
00919   insert_at = _dbus_string_get_length (str);
00920   
00921   return align_insert_point_then_open_gap (str,
00922                                            &insert_at,
00923                                            alignment, then_lengthen_by);
00924 }
00925 
00934 dbus_bool_t
00935 _dbus_string_align_length (DBusString *str,
00936                            int         alignment)
00937 {
00938   return align_length_then_lengthen (str, alignment, 0);
00939 }
00940 
00950 dbus_bool_t
00951 _dbus_string_alloc_space (DBusString        *str,
00952                           int                extra_bytes)
00953 {
00954   if (!_dbus_string_lengthen (str, extra_bytes))
00955     return FALSE;
00956   _dbus_string_shorten (str, extra_bytes);
00957 
00958   return TRUE;
00959 }
00960 
00961 static dbus_bool_t
00962 append (DBusRealString *real,
00963         const char     *buffer,
00964         int             buffer_len)
00965 {
00966   if (buffer_len == 0)
00967     return TRUE;
00968 
00969   if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
00970     return FALSE;
00971 
00972   memcpy (real->str + (real->len - buffer_len),
00973           buffer,
00974           buffer_len);
00975 
00976   return TRUE;
00977 }
00978 
00986 dbus_bool_t
00987 _dbus_string_append (DBusString *str,
00988                      const char *buffer)
00989 {
00990   unsigned long buffer_len;
00991   
00992   DBUS_STRING_PREAMBLE (str);
00993   _dbus_assert (buffer != NULL);
00994   
00995   buffer_len = strlen (buffer);
00996   if (buffer_len > (unsigned long) real->max_length)
00997     return FALSE;
00998   
00999   return append (real, buffer, buffer_len);
01000 }
01001 
01003 #define ASSIGN_2_OCTETS(p, octets) \
01004   *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
01005 
01007 #define ASSIGN_4_OCTETS(p, octets) \
01008   *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
01009 
01010 #ifdef DBUS_HAVE_INT64
01011 
01012 #define ASSIGN_8_OCTETS(p, octets) \
01013   *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
01014 #else
01015 
01016 #define ASSIGN_8_OCTETS(p, octets)              \
01017 do {                                            \
01018   unsigned char *b;                             \
01019                                                 \
01020   b = p;                                        \
01021                                                 \
01022   *b++ = octets[0];                             \
01023   *b++ = octets[1];                             \
01024   *b++ = octets[2];                             \
01025   *b++ = octets[3];                             \
01026   *b++ = octets[4];                             \
01027   *b++ = octets[5];                             \
01028   *b++ = octets[6];                             \
01029   *b++ = octets[7];                             \
01030   _dbus_assert (b == p + 8);                    \
01031 } while (0)
01032 #endif /* DBUS_HAVE_INT64 */
01033 
01034 #ifdef DBUS_BUILD_TESTS
01035 
01043 dbus_bool_t
01044 _dbus_string_append_4_aligned (DBusString         *str,
01045                                const unsigned char octets[4])
01046 {
01047   DBUS_STRING_PREAMBLE (str);
01048   
01049   if (!align_length_then_lengthen (str, 4, 4))
01050     return FALSE;
01051 
01052   ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
01053 
01054   return TRUE;
01055 }
01056 #endif /* DBUS_BUILD_TESTS */
01057 
01058 #ifdef DBUS_BUILD_TESTS
01059 
01067 dbus_bool_t
01068 _dbus_string_append_8_aligned (DBusString         *str,
01069                                const unsigned char octets[8])
01070 {
01071   DBUS_STRING_PREAMBLE (str);
01072   
01073   if (!align_length_then_lengthen (str, 8, 8))
01074     return FALSE;
01075 
01076   ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
01077 
01078   return TRUE;
01079 }
01080 #endif /* DBUS_BUILD_TESTS */
01081 
01091 dbus_bool_t
01092 _dbus_string_insert_2_aligned (DBusString         *str,
01093                                int                 insert_at,
01094                                const unsigned char octets[4])
01095 {
01096   DBUS_STRING_PREAMBLE (str);
01097   
01098   if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
01099     return FALSE;
01100 
01101   ASSIGN_2_OCTETS (real->str + insert_at, octets);
01102 
01103   return TRUE;
01104 }
01105 
01115 dbus_bool_t
01116 _dbus_string_insert_4_aligned (DBusString         *str,
01117                                int                 insert_at,
01118                                const unsigned char octets[4])
01119 {
01120   DBUS_STRING_PREAMBLE (str);
01121   
01122   if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
01123     return FALSE;
01124 
01125   ASSIGN_4_OCTETS (real->str + insert_at, octets);
01126 
01127   return TRUE;
01128 }
01129 
01139 dbus_bool_t
01140 _dbus_string_insert_8_aligned (DBusString         *str,
01141                                int                 insert_at,
01142                                const unsigned char octets[8])
01143 {
01144   DBUS_STRING_PREAMBLE (str);
01145   
01146   if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
01147     return FALSE;
01148 
01149   _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
01150   
01151   ASSIGN_8_OCTETS (real->str + insert_at, octets);
01152 
01153   return TRUE;
01154 }
01155 
01156 
01167 dbus_bool_t
01168 _dbus_string_insert_alignment (DBusString        *str,
01169                                int               *insert_at,
01170                                int                alignment)
01171 {
01172   DBUS_STRING_PREAMBLE (str);
01173   
01174   if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
01175     return FALSE;
01176 
01177   _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
01178 
01179   return TRUE;
01180 }
01181 
01191 dbus_bool_t
01192 _dbus_string_append_printf_valist  (DBusString        *str,
01193                                     const char        *format,
01194                                     va_list            args)
01195 {
01196   int len;
01197   va_list args_copy;
01198 
01199   DBUS_STRING_PREAMBLE (str);
01200 
01201   DBUS_VA_COPY (args_copy, args);
01202 
01203   /* Measure the message length without terminating nul */
01204   len = _dbus_printf_string_upper_bound (format, args);
01205 
01206   if (!_dbus_string_lengthen (str, len))
01207     {
01208       /* don't leak the copy */
01209       va_end (args_copy);
01210       return FALSE;
01211     }
01212   
01213   vsprintf ((char*) (real->str + (real->len - len)),
01214             format, args_copy);
01215 
01216   va_end (args_copy);
01217 
01218   return TRUE;
01219 }
01220 
01229 dbus_bool_t
01230 _dbus_string_append_printf (DBusString        *str,
01231                             const char        *format,
01232                             ...)
01233 {
01234   va_list args;
01235   dbus_bool_t retval;
01236   
01237   va_start (args, format);
01238   retval = _dbus_string_append_printf_valist (str, format, args);
01239   va_end (args);
01240 
01241   return retval;
01242 }
01243 
01252 dbus_bool_t
01253 _dbus_string_append_len (DBusString *str,
01254                          const char *buffer,
01255                          int         len)
01256 {
01257   DBUS_STRING_PREAMBLE (str);
01258   _dbus_assert (buffer != NULL);
01259   _dbus_assert (len >= 0);
01260 
01261   return append (real, buffer, len);
01262 }
01263 
01272 dbus_bool_t
01273 _dbus_string_append_byte (DBusString    *str,
01274                           unsigned char  byte)
01275 {
01276   DBUS_STRING_PREAMBLE (str);
01277 
01278   if (!set_length (real, real->len + 1))
01279     return FALSE;
01280 
01281   real->str[real->len-1] = byte;
01282 
01283   return TRUE;
01284 }
01285 
01286 #ifdef DBUS_BUILD_TESTS
01287 
01294 dbus_bool_t
01295 _dbus_string_append_unichar (DBusString    *str,
01296                              dbus_unichar_t ch)
01297 {
01298   int len;
01299   int first;
01300   int i;
01301   unsigned char *out;
01302   
01303   DBUS_STRING_PREAMBLE (str);
01304 
01305   /* this code is from GLib but is pretty standard I think */
01306   
01307   len = 0;
01308   
01309   if (ch < 0x80)
01310     {
01311       first = 0;
01312       len = 1;
01313     }
01314   else if (ch < 0x800)
01315     {
01316       first = 0xc0;
01317       len = 2;
01318     }
01319   else if (ch < 0x10000)
01320     {
01321       first = 0xe0;
01322       len = 3;
01323     }
01324    else if (ch < 0x200000)
01325     {
01326       first = 0xf0;
01327       len = 4;
01328     }
01329   else if (ch < 0x4000000)
01330     {
01331       first = 0xf8;
01332       len = 5;
01333     }
01334   else
01335     {
01336       first = 0xfc;
01337       len = 6;
01338     }
01339 
01340   if (len > (real->max_length - real->len))
01341     return FALSE; /* real->len + len would overflow */
01342   
01343   if (!set_length (real, real->len + len))
01344     return FALSE;
01345 
01346   out = real->str + (real->len - len);
01347   
01348   for (i = len - 1; i > 0; --i)
01349     {
01350       out[i] = (ch & 0x3f) | 0x80;
01351       ch >>= 6;
01352     }
01353   out[0] = ch | first;
01354 
01355   return TRUE;
01356 }
01357 #endif /* DBUS_BUILD_TESTS */
01358 
01359 static void
01360 delete (DBusRealString *real,
01361         int             start,
01362         int             len)
01363 {
01364   if (len == 0)
01365     return;
01366   
01367   memmove (real->str + start, real->str + start + len, real->len - (start + len));
01368   real->len -= len;
01369   real->str[real->len] = '\0';
01370 }
01371 
01381 void
01382 _dbus_string_delete (DBusString       *str,
01383                      int               start,
01384                      int               len)
01385 {
01386   DBUS_STRING_PREAMBLE (str);
01387   _dbus_assert (start >= 0);
01388   _dbus_assert (len >= 0);
01389   _dbus_assert (start <= real->len);
01390   _dbus_assert (len <= real->len - start);
01391   
01392   delete (real, start, len);
01393 }
01394 
01395 static dbus_bool_t
01396 copy (DBusRealString *source,
01397       int             start,
01398       int             len,
01399       DBusRealString *dest,
01400       int             insert_at)
01401 {
01402   if (len == 0)
01403     return TRUE;
01404 
01405   if (!open_gap (len, dest, insert_at))
01406     return FALSE;
01407   
01408   memmove (dest->str + insert_at,
01409            source->str + start,
01410            len);
01411 
01412   return TRUE;
01413 }
01414 
01424 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at)       \
01425   DBusRealString *real_source = (DBusRealString*) source;               \
01426   DBusRealString *real_dest = (DBusRealString*) dest;                   \
01427   _dbus_assert ((source) != (dest));                                    \
01428   DBUS_GENERIC_STRING_PREAMBLE (real_source);                           \
01429   DBUS_GENERIC_STRING_PREAMBLE (real_dest);                             \
01430   _dbus_assert (!real_dest->constant);                                  \
01431   _dbus_assert (!real_dest->locked);                                    \
01432   _dbus_assert ((start) >= 0);                                          \
01433   _dbus_assert ((start) <= real_source->len);                           \
01434   _dbus_assert ((insert_at) >= 0);                                      \
01435   _dbus_assert ((insert_at) <= real_dest->len)
01436 
01447 dbus_bool_t
01448 _dbus_string_move (DBusString       *source,
01449                    int               start,
01450                    DBusString       *dest,
01451                    int               insert_at)
01452 {
01453   DBusRealString *real_source = (DBusRealString*) source;
01454   _dbus_assert (start <= real_source->len);
01455   
01456   return _dbus_string_move_len (source, start,
01457                                 real_source->len - start,
01458                                 dest, insert_at);
01459 }
01460 
01471 dbus_bool_t
01472 _dbus_string_copy (const DBusString *source,
01473                    int               start,
01474                    DBusString       *dest,
01475                    int               insert_at)
01476 {
01477   DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
01478 
01479   return copy (real_source, start,
01480                real_source->len - start,
01481                real_dest,
01482                insert_at);
01483 }
01484 
01499 dbus_bool_t
01500 _dbus_string_move_len (DBusString       *source,
01501                        int               start,
01502                        int               len,
01503                        DBusString       *dest,
01504                        int               insert_at)
01505 
01506 {
01507   DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
01508   _dbus_assert (len >= 0);
01509   _dbus_assert ((start + len) <= real_source->len);
01510 
01511 
01512   if (len == 0)
01513     {
01514       return TRUE;
01515     }
01516   else if (start == 0 &&
01517            len == real_source->len &&
01518            real_dest->len == 0)
01519     {
01520       /* Short-circuit moving an entire existing string to an empty string
01521        * by just swapping the buffers.
01522        */
01523       /* we assume ->constant doesn't matter as you can't have
01524        * a constant string involved in a move.
01525        */
01526 #define ASSIGN_DATA(a, b) do {                  \
01527         (a)->str = (b)->str;                    \
01528         (a)->len = (b)->len;                    \
01529         (a)->allocated = (b)->allocated;        \
01530         (a)->align_offset = (b)->align_offset;  \
01531       } while (0)
01532       
01533       DBusRealString tmp;
01534 
01535       ASSIGN_DATA (&tmp, real_source);
01536       ASSIGN_DATA (real_source, real_dest);
01537       ASSIGN_DATA (real_dest, &tmp);
01538 
01539       return TRUE;
01540     }
01541   else
01542     {
01543       if (!copy (real_source, start, len,
01544                  real_dest,
01545                  insert_at))
01546         return FALSE;
01547       
01548       delete (real_source, start,
01549               len);
01550       
01551       return TRUE;
01552     }
01553 }
01554 
01566 dbus_bool_t
01567 _dbus_string_copy_len (const DBusString *source,
01568                        int               start,
01569                        int               len,
01570                        DBusString       *dest,
01571                        int               insert_at)
01572 {
01573   DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
01574   _dbus_assert (len >= 0);
01575   _dbus_assert (start <= real_source->len);
01576   _dbus_assert (len <= real_source->len - start);
01577   
01578   return copy (real_source, start, len,
01579                real_dest,
01580                insert_at);
01581 }
01582 
01604 dbus_bool_t
01605 _dbus_string_replace_len (const DBusString *source,
01606                           int               start,
01607                           int               len,
01608                           DBusString       *dest,
01609                           int               replace_at,
01610                           int               replace_len)
01611 {
01612   DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
01613   _dbus_assert (len >= 0);
01614   _dbus_assert (start <= real_source->len);
01615   _dbus_assert (len <= real_source->len - start);
01616   _dbus_assert (replace_at >= 0);
01617   _dbus_assert (replace_at <= real_dest->len);
01618   _dbus_assert (replace_len <= real_dest->len - replace_at);
01619 
01620   if (!copy (real_source, start, len,
01621              real_dest, replace_at))
01622     return FALSE;
01623 
01624   delete (real_dest, replace_at + len, replace_len);
01625 
01626   return TRUE;
01627 }
01628 
01629 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
01630  * Pennington, and Tom Tromey are the authors and authorized relicense.
01631  */
01632 
01638 #define UTF8_COMPUTE(Char, Mask, Len)                                         \
01639   if (Char < 128)                                                             \
01640     {                                                                         \
01641       Len = 1;                                                                \
01642       Mask = 0x7f;                                                            \
01643     }                                                                         \
01644   else if ((Char & 0xe0) == 0xc0)                                             \
01645     {                                                                         \
01646       Len = 2;                                                                \
01647       Mask = 0x1f;                                                            \
01648     }                                                                         \
01649   else if ((Char & 0xf0) == 0xe0)                                             \
01650     {                                                                         \
01651       Len = 3;                                                                \
01652       Mask = 0x0f;                                                            \
01653     }                                                                         \
01654   else if ((Char & 0xf8) == 0xf0)                                             \
01655     {                                                                         \
01656       Len = 4;                                                                \
01657       Mask = 0x07;                                                            \
01658     }                                                                         \
01659   else if ((Char & 0xfc) == 0xf8)                                             \
01660     {                                                                         \
01661       Len = 5;                                                                \
01662       Mask = 0x03;                                                            \
01663     }                                                                         \
01664   else if ((Char & 0xfe) == 0xfc)                                             \
01665     {                                                                         \
01666       Len = 6;                                                                \
01667       Mask = 0x01;                                                            \
01668     }                                                                         \
01669   else                                                                        \
01670     {                                                                         \
01671       Len = 0;                                                               \
01672       Mask = 0;                                                               \
01673     }
01674 
01679 #define UTF8_LENGTH(Char)              \
01680   ((Char) < 0x80 ? 1 :                 \
01681    ((Char) < 0x800 ? 2 :               \
01682     ((Char) < 0x10000 ? 3 :            \
01683      ((Char) < 0x200000 ? 4 :          \
01684       ((Char) < 0x4000000 ? 5 : 6)))))
01685    
01695 #define UTF8_GET(Result, Chars, Count, Mask, Len)                             \
01696   (Result) = (Chars)[0] & (Mask);                                             \
01697   for ((Count) = 1; (Count) < (Len); ++(Count))                               \
01698     {                                                                         \
01699       if (((Chars)[(Count)] & 0xc0) != 0x80)                                  \
01700         {                                                                     \
01701           (Result) = -1;                                                      \
01702           break;                                                              \
01703         }                                                                     \
01704       (Result) <<= 6;                                                         \
01705       (Result) |= ((Chars)[(Count)] & 0x3f);                                  \
01706     }
01707 
01713 #define UNICODE_VALID(Char)                   \
01714     ((Char) < 0x110000 &&                     \
01715      (((Char) & 0xFFFFF800) != 0xD800) &&     \
01716      ((Char) < 0xFDD0 || (Char) > 0xFDEF) &&  \
01717      ((Char) & 0xFFFF) != 0xFFFF)
01718 
01719 #ifdef DBUS_BUILD_TESTS
01720 
01730 void
01731 _dbus_string_get_unichar (const DBusString *str,
01732                           int               start,
01733                           dbus_unichar_t   *ch_return,
01734                           int              *end_return)
01735 {
01736   int i, mask, len;
01737   dbus_unichar_t result;
01738   unsigned char c;
01739   unsigned char *p;
01740   DBUS_CONST_STRING_PREAMBLE (str);
01741   _dbus_assert (start >= 0);
01742   _dbus_assert (start <= real->len);
01743   
01744   if (ch_return)
01745     *ch_return = 0;
01746   if (end_return)
01747     *end_return = real->len;
01748   
01749   mask = 0;
01750   p = real->str + start;
01751   c = *p;
01752   
01753   UTF8_COMPUTE (c, mask, len);
01754   if (len == 0)
01755     return;
01756   UTF8_GET (result, p, i, mask, len);
01757 
01758   if (result == (dbus_unichar_t)-1)
01759     return;
01760 
01761   if (ch_return)
01762     *ch_return = result;
01763   if (end_return)
01764     *end_return = start + len;
01765 }
01766 #endif /* DBUS_BUILD_TESTS */
01767 
01782 dbus_bool_t
01783 _dbus_string_find (const DBusString *str,
01784                    int               start,
01785                    const char       *substr,
01786                    int              *found)
01787 {
01788   return _dbus_string_find_to (str, start,
01789                                ((const DBusRealString*)str)->len,
01790                                substr, found);
01791 }
01792 
01805 dbus_bool_t
01806 _dbus_string_find_eol (const DBusString *str,
01807                        int               start,
01808                        int              *found,
01809                        int              *found_len)
01810 {
01811   int i;
01812 
01813   DBUS_CONST_STRING_PREAMBLE (str);
01814   _dbus_assert (start <= real->len);
01815   _dbus_assert (start >= 0);
01816   
01817   i = start;
01818   while (i < real->len)
01819     {
01820       if (real->str[i] == '\r') 
01821         {
01822           if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
01823             {
01824               if (found) 
01825                 *found = i;
01826               if (found_len)
01827                 *found_len = 2;
01828               return TRUE;
01829             } 
01830           else /* only "\r" */
01831             {
01832               if (found) 
01833                 *found = i;
01834               if (found_len)
01835                 *found_len = 1;
01836               return TRUE;
01837             }
01838         } 
01839       else if (real->str[i] == '\n')  /* only "\n" */
01840         {
01841           if (found) 
01842             *found = i;
01843           if (found_len)
01844             *found_len = 1;
01845           return TRUE;
01846         }
01847       ++i;
01848     }
01849 
01850   if (found)
01851     *found = real->len;
01852 
01853   if (found_len)
01854     *found_len = 0;
01855   
01856   return FALSE;
01857 }
01858 
01875 dbus_bool_t
01876 _dbus_string_find_to (const DBusString *str,
01877                       int               start,
01878                       int               end,
01879                       const char       *substr,
01880                       int              *found)
01881 {
01882   int i;
01883   DBUS_CONST_STRING_PREAMBLE (str);
01884   _dbus_assert (substr != NULL);
01885   _dbus_assert (start <= real->len);
01886   _dbus_assert (start >= 0);
01887   _dbus_assert (substr != NULL);
01888   _dbus_assert (end <= real->len);
01889   _dbus_assert (start <= end);
01890 
01891   /* we always "find" an empty string */
01892   if (*substr == '\0')
01893     {
01894       if (found)
01895         *found = start;
01896       return TRUE;
01897     }
01898 
01899   i = start;
01900   while (i < end)
01901     {
01902       if (real->str[i] == substr[0])
01903         {
01904           int j = i + 1;
01905           
01906           while (j < end)
01907             {
01908               if (substr[j - i] == '\0')
01909                 break;
01910               else if (real->str[j] != substr[j - i])
01911                 break;
01912               
01913               ++j;
01914             }
01915 
01916           if (substr[j - i] == '\0')
01917             {
01918               if (found)
01919                 *found = i;
01920               return TRUE;
01921             }
01922         }
01923       
01924       ++i;
01925     }
01926 
01927   if (found)
01928     *found = end;
01929   
01930   return FALSE;  
01931 }
01932 
01943 dbus_bool_t
01944 _dbus_string_find_blank (const DBusString *str,
01945                          int               start,
01946                          int              *found)
01947 {
01948   int i;
01949   DBUS_CONST_STRING_PREAMBLE (str);
01950   _dbus_assert (start <= real->len);
01951   _dbus_assert (start >= 0);
01952   
01953   i = start;
01954   while (i < real->len)
01955     {
01956       if (real->str[i] == ' ' ||
01957           real->str[i] == '\t')
01958         {
01959           if (found)
01960             *found = i;
01961           return TRUE;
01962         }
01963       
01964       ++i;
01965     }
01966 
01967   if (found)
01968     *found = real->len;
01969   
01970   return FALSE;
01971 }
01972 
01981 void
01982 _dbus_string_skip_blank (const DBusString *str,
01983                          int               start,
01984                          int              *end)
01985 {
01986   int i;
01987   DBUS_CONST_STRING_PREAMBLE (str);
01988   _dbus_assert (start <= real->len);
01989   _dbus_assert (start >= 0);
01990   
01991   i = start;
01992   while (i < real->len)
01993     {
01994       if (!DBUS_IS_ASCII_BLANK (real->str[i]))
01995         break;
01996       
01997       ++i;
01998     }
01999 
02000   _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
02001   
02002   if (end)
02003     *end = i;
02004 }
02005 
02006 
02015 void
02016 _dbus_string_skip_white (const DBusString *str,
02017                          int               start,
02018                          int              *end)
02019 {
02020   int i;
02021   DBUS_CONST_STRING_PREAMBLE (str);
02022   _dbus_assert (start <= real->len);
02023   _dbus_assert (start >= 0);
02024   
02025   i = start;
02026   while (i < real->len)
02027     {
02028       if (!DBUS_IS_ASCII_WHITE (real->str[i]))
02029         break;
02030       
02031       ++i;
02032     }
02033 
02034   _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
02035   
02036   if (end)
02037     *end = i;
02038 }
02039 
02048 void
02049 _dbus_string_skip_white_reverse (const DBusString *str,
02050                                  int               end,
02051                                  int              *start)
02052 {
02053   int i;
02054   DBUS_CONST_STRING_PREAMBLE (str);
02055   _dbus_assert (end <= real->len);
02056   _dbus_assert (end >= 0);
02057   
02058   i = end;
02059   while (i > 0)
02060     {
02061       if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
02062         break;
02063       --i;
02064     }
02065 
02066   _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
02067   
02068   if (start)
02069     *start = i;
02070 }
02071 
02087 dbus_bool_t
02088 _dbus_string_pop_line (DBusString *source,
02089                        DBusString *dest)
02090 {
02091   int eol, eol_len;
02092   
02093   _dbus_string_set_length (dest, 0);
02094   
02095   eol = 0;
02096   eol_len = 0;
02097   if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
02098     {
02099       _dbus_assert (eol == _dbus_string_get_length (source));
02100       if (eol == 0)
02101         {
02102           /* If there's no newline and source has zero length, we're done */
02103           return FALSE;
02104         }
02105       /* otherwise, the last line of the file has no eol characters */
02106     }
02107 
02108   /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
02109    * since find_eol returned TRUE
02110    */
02111   
02112   if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
02113     return FALSE;
02114   
02115   /* remove line ending */
02116   if (!_dbus_string_set_length (dest, eol))
02117     {
02118       _dbus_assert_not_reached ("out of memory when shortening a string");
02119       return FALSE;
02120     }
02121 
02122   return TRUE;
02123 }
02124 
02125 #ifdef DBUS_BUILD_TESTS
02126 
02132 void
02133 _dbus_string_delete_first_word (DBusString *str)
02134 {
02135   int i;
02136   
02137   if (_dbus_string_find_blank (str, 0, &i))
02138     _dbus_string_skip_blank (str, i, &i);
02139 
02140   _dbus_string_delete (str, 0, i);
02141 }
02142 #endif
02143 
02144 #ifdef DBUS_BUILD_TESTS
02145 
02150 void
02151 _dbus_string_delete_leading_blanks (DBusString *str)
02152 {
02153   int i;
02154   
02155   _dbus_string_skip_blank (str, 0, &i);
02156 
02157   if (i > 0)
02158     _dbus_string_delete (str, 0, i);
02159 }
02160 #endif
02161 
02167 void
02168 _dbus_string_chop_white(DBusString *str)
02169 {
02170   int i;
02171   
02172   _dbus_string_skip_white (str, 0, &i);
02173 
02174   if (i > 0)
02175     _dbus_string_delete (str, 0, i);
02176   
02177   _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
02178 
02179   _dbus_string_set_length (str, i);
02180 }
02181 
02191 dbus_bool_t
02192 _dbus_string_equal (const DBusString *a,
02193                     const DBusString *b)
02194 {
02195   const unsigned char *ap;
02196   const unsigned char *bp;
02197   const unsigned char *a_end;
02198   const DBusRealString *real_a = (const DBusRealString*) a;
02199   const DBusRealString *real_b = (const DBusRealString*) b;
02200   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02201   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02202 
02203   if (real_a->len != real_b->len)
02204     return FALSE;
02205 
02206   ap = real_a->str;
02207   bp = real_b->str;
02208   a_end = real_a->str + real_a->len;
02209   while (ap != a_end)
02210     {
02211       if (*ap != *bp)
02212         return FALSE;
02213       
02214       ++ap;
02215       ++bp;
02216     }
02217 
02218   return TRUE;
02219 }
02220 
02221 #ifdef DBUS_BUILD_TESTS
02222 
02235 dbus_bool_t
02236 _dbus_string_equal_len (const DBusString *a,
02237                         const DBusString *b,
02238                         int               len)
02239 {
02240   const unsigned char *ap;
02241   const unsigned char *bp;
02242   const unsigned char *a_end;
02243   const DBusRealString *real_a = (const DBusRealString*) a;
02244   const DBusRealString *real_b = (const DBusRealString*) b;
02245   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02246   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02247 
02248   if (real_a->len != real_b->len &&
02249       (real_a->len < len || real_b->len < len))
02250     return FALSE;
02251 
02252   ap = real_a->str;
02253   bp = real_b->str;
02254   a_end = real_a->str + MIN (real_a->len, len);
02255   while (ap != a_end)
02256     {
02257       if (*ap != *bp)
02258         return FALSE;
02259       
02260       ++ap;
02261       ++bp;
02262     }
02263 
02264   return TRUE;
02265 }
02266 #endif /* DBUS_BUILD_TESTS */
02267 
02284 dbus_bool_t
02285 _dbus_string_equal_substring (const DBusString  *a,
02286                               int                a_start,
02287                               int                a_len,
02288                               const DBusString  *b,
02289                               int                b_start)
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   _dbus_assert (a_start >= 0);
02299   _dbus_assert (a_len >= 0);
02300   _dbus_assert (a_start <= real_a->len);
02301   _dbus_assert (a_len <= real_a->len - a_start);
02302   _dbus_assert (b_start >= 0);
02303   _dbus_assert (b_start <= real_b->len);
02304   
02305   if (a_len > real_b->len - b_start)
02306     return FALSE;
02307 
02308   ap = real_a->str + a_start;
02309   bp = real_b->str + b_start;
02310   a_end = ap + a_len;
02311   while (ap != a_end)
02312     {
02313       if (*ap != *bp)
02314         return FALSE;
02315       
02316       ++ap;
02317       ++bp;
02318     }
02319 
02320   _dbus_assert (bp <= (real_b->str + real_b->len));
02321   
02322   return TRUE;
02323 }
02324 
02332 dbus_bool_t
02333 _dbus_string_equal_c_str (const DBusString *a,
02334                           const char       *c_str)
02335 {
02336   const unsigned char *ap;
02337   const unsigned char *bp;
02338   const unsigned char *a_end;
02339   const DBusRealString *real_a = (const DBusRealString*) a;
02340   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02341   _dbus_assert (c_str != NULL);
02342   
02343   ap = real_a->str;
02344   bp = (const unsigned char*) c_str;
02345   a_end = real_a->str + real_a->len;
02346   while (ap != a_end && *bp)
02347     {
02348       if (*ap != *bp)
02349         return FALSE;
02350       
02351       ++ap;
02352       ++bp;
02353     }
02354 
02355   if (ap != a_end || *bp)
02356     return FALSE;
02357   
02358   return TRUE;
02359 }
02360 
02361 #ifdef DBUS_BUILD_TESTS
02362 
02369 dbus_bool_t
02370 _dbus_string_starts_with_c_str (const DBusString *a,
02371                                 const char       *c_str)
02372 {
02373   const unsigned char *ap;
02374   const unsigned char *bp;
02375   const unsigned char *a_end;
02376   const DBusRealString *real_a = (const DBusRealString*) a;
02377   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02378   _dbus_assert (c_str != NULL);
02379   
02380   ap = real_a->str;
02381   bp = (const unsigned char*) c_str;
02382   a_end = real_a->str + real_a->len;
02383   while (ap != a_end && *bp)
02384     {
02385       if (*ap != *bp)
02386         return FALSE;
02387       
02388       ++ap;
02389       ++bp;
02390     }
02391 
02392   if (*bp == '\0')
02393     return TRUE;
02394   else
02395     return FALSE;
02396 }
02397 #endif /* DBUS_BUILD_TESTS */
02398 
02407 dbus_bool_t
02408 _dbus_string_append_byte_as_hex (DBusString *str,
02409                                  int         byte)
02410 {
02411   const char hexdigits[16] = {
02412     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
02413     'a', 'b', 'c', 'd', 'e', 'f'
02414   };
02415 
02416   if (!_dbus_string_append_byte (str,
02417                                  hexdigits[(byte >> 4)]))
02418     return FALSE;
02419   
02420   if (!_dbus_string_append_byte (str,
02421                                  hexdigits[(byte & 0x0f)]))
02422     {
02423       _dbus_string_set_length (str,
02424                                _dbus_string_get_length (str) - 1);
02425       return FALSE;
02426     }
02427 
02428   return TRUE;
02429 }
02430 
02441 dbus_bool_t
02442 _dbus_string_hex_encode (const DBusString *source,
02443                          int               start,
02444                          DBusString       *dest,
02445                          int               insert_at)
02446 {
02447   DBusString result;
02448   const unsigned char *p;
02449   const unsigned char *end;
02450   dbus_bool_t retval;
02451   
02452   _dbus_assert (start <= _dbus_string_get_length (source));
02453 
02454   if (!_dbus_string_init (&result))
02455     return FALSE;
02456 
02457   retval = FALSE;
02458   
02459   p = (const unsigned char*) _dbus_string_get_const_data (source);
02460   end = p + _dbus_string_get_length (source);
02461   p += start;
02462   
02463   while (p != end)
02464     {
02465       if (!_dbus_string_append_byte_as_hex (&result, *p))
02466         goto out;
02467       
02468       ++p;
02469     }
02470 
02471   if (!_dbus_string_move (&result, 0, dest, insert_at))
02472     goto out;
02473 
02474   retval = TRUE;
02475 
02476  out:
02477   _dbus_string_free (&result);
02478   return retval;
02479 }
02480 
02491 dbus_bool_t
02492 _dbus_string_hex_decode (const DBusString *source,
02493                          int               start,
02494                          int              *end_return,
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   dbus_bool_t high_bits;
02503   
02504   _dbus_assert (start <= _dbus_string_get_length (source));
02505 
02506   if (!_dbus_string_init (&result))
02507     return FALSE;
02508 
02509   retval = FALSE;
02510 
02511   high_bits = TRUE;
02512   p = (const unsigned char*) _dbus_string_get_const_data (source);
02513   end = p + _dbus_string_get_length (source);
02514   p += start;
02515   
02516   while (p != end)
02517     {
02518       unsigned int val;
02519 
02520       switch (*p)
02521         {
02522         case '0':
02523           val = 0;
02524           break;
02525         case '1':
02526           val = 1;
02527           break;
02528         case '2':
02529           val = 2;
02530           break;
02531         case '3':
02532           val = 3;
02533           break;
02534         case '4':
02535           val = 4;
02536           break;
02537         case '5':
02538           val = 5;
02539           break;
02540         case '6':
02541           val = 6;
02542           break;
02543         case '7':
02544           val = 7;
02545           break;
02546         case '8':
02547           val = 8;
02548           break;
02549         case '9':
02550           val = 9;
02551           break;
02552         case 'a':
02553         case 'A':
02554           val = 10;
02555           break;
02556         case 'b':
02557         case 'B':
02558           val = 11;
02559           break;
02560         case 'c':
02561         case 'C':
02562           val = 12;
02563           break;
02564         case 'd':
02565         case 'D':
02566           val = 13;
02567           break;
02568         case 'e':
02569         case 'E':
02570           val = 14;
02571           break;
02572         case 'f':
02573         case 'F':
02574           val = 15;
02575           break;
02576         default:
02577           goto done;
02578         }
02579 
02580       if (high_bits)
02581         {
02582           if (!_dbus_string_append_byte (&result,
02583                                          val << 4))
02584             goto out;
02585         }
02586       else
02587         {
02588           int len;
02589           unsigned char b;
02590 
02591           len = _dbus_string_get_length (&result);
02592           
02593           b = _dbus_string_get_byte (&result, len - 1);
02594 
02595           b |= val;
02596 
02597           _dbus_string_set_byte (&result, len - 1, b);
02598         }
02599 
02600       high_bits = !high_bits;
02601 
02602       ++p;
02603     }
02604 
02605  done:
02606   if (!_dbus_string_move (&result, 0, dest, insert_at))
02607     goto out;
02608 
02609   if (end_return)
02610     *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
02611 
02612   retval = TRUE;
02613   
02614  out:
02615   _dbus_string_free (&result);  
02616   return retval;
02617 }
02618 
02632 dbus_bool_t
02633 _dbus_string_validate_ascii (const DBusString *str,
02634                              int               start,
02635                              int               len)
02636 {
02637   const unsigned char *s;
02638   const unsigned char *end;
02639   DBUS_CONST_STRING_PREAMBLE (str);
02640   _dbus_assert (start >= 0);
02641   _dbus_assert (start <= real->len);
02642   _dbus_assert (len >= 0);
02643   
02644   if (len > real->len - start)
02645     return FALSE;
02646   
02647   s = real->str + start;
02648   end = s + len;
02649   while (s != end)
02650     {
02651       if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
02652         return FALSE;
02653         
02654       ++s;
02655     }
02656   
02657   return TRUE;
02658 }
02659 
02675 dbus_bool_t
02676 _dbus_string_validate_utf8  (const DBusString *str,
02677                              int               start,
02678                              int               len)
02679 {
02680   const unsigned char *p;
02681   const unsigned char *end;
02682   DBUS_CONST_STRING_PREAMBLE (str);
02683   _dbus_assert (start >= 0);
02684   _dbus_assert (start <= real->len);
02685   _dbus_assert (len >= 0);
02686 
02687   /* we are doing _DBUS_UNLIKELY() here which might be
02688    * dubious in a generic library like GLib, but in D-Bus
02689    * we know we're validating messages and that it would
02690    * only be evil/broken apps that would have invalid
02691    * UTF-8. Also, this function seems to be a performance
02692    * bottleneck in profiles.
02693    */
02694   
02695   if (_DBUS_UNLIKELY (len > real->len - start))
02696     return FALSE;
02697   
02698   p = real->str + start;
02699   end = p + len;
02700   
02701   while (p < end)
02702     {
02703       int i, mask, char_len;
02704       dbus_unichar_t result;
02705 
02706       /* nul bytes considered invalid */
02707       if (*p == '\0')
02708         break;
02709       
02710       /* Special-case ASCII; this makes us go a lot faster in
02711        * D-Bus profiles where we are typically validating
02712        * function names and such. We have to know that
02713        * all following checks will pass for ASCII though,
02714        * comments follow ...
02715        */      
02716       if (*p < 128)
02717         {
02718           ++p;
02719           continue;
02720         }
02721       
02722       UTF8_COMPUTE (*p, mask, char_len);
02723 
02724       if (_DBUS_UNLIKELY (char_len == 0))  /* ASCII: char_len == 1 */
02725         break;
02726 
02727       /* check that the expected number of bytes exists in the remaining length */
02728       if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
02729         break;
02730         
02731       UTF8_GET (result, p, i, mask, char_len);
02732 
02733       /* Check for overlong UTF-8 */
02734       if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
02735         break;
02736 #if 0
02737       /* The UNICODE_VALID check below will catch this */
02738       if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
02739         break;
02740 #endif
02741 
02742       if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
02743         break;
02744 
02745       /* UNICODE_VALID should have caught it */
02746       _dbus_assert (result != (dbus_unichar_t)-1);
02747       
02748       p += char_len;
02749     }
02750 
02751   /* See that we covered the entire length if a length was
02752    * passed in
02753    */
02754   if (_DBUS_UNLIKELY (p != end))
02755     return FALSE;
02756   else
02757     return TRUE;
02758 }
02759 
02773 dbus_bool_t
02774 _dbus_string_validate_nul (const DBusString *str,
02775                            int               start,
02776                            int               len)
02777 {
02778   const unsigned char *s;
02779   const unsigned char *end;
02780   DBUS_CONST_STRING_PREAMBLE (str);
02781   _dbus_assert (start >= 0);
02782   _dbus_assert (len >= 0);
02783   _dbus_assert (start <= real->len);
02784   
02785   if (len > real->len - start)
02786     return FALSE;
02787   
02788   s = real->str + start;
02789   end = s + len;
02790   while (s != end)
02791     {
02792       if (_DBUS_UNLIKELY (*s != '\0'))
02793         return FALSE;
02794       ++s;
02795     }
02796   
02797   return TRUE;
02798 }
02799 
02805 void
02806 _dbus_string_zero (DBusString *str)
02807 {
02808   DBUS_STRING_PREAMBLE (str);
02809 
02810   memset (real->str - real->align_offset, '\0', real->allocated);
02811 }
02814 /* tests are in dbus-string-util.c */

Generated on Mon Dec 14 22:26:12 2009 for D-Bus by  doxygen 1.4.7