dbus-string.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-string.c String utility class (internal to D-BUS implementation)
00003  * 
00004  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include "dbus-internals.h"
00025 #include "dbus-string.h"
00026 /* we allow a system header here, for speed/convenience */
00027 #include <string.h>
00028 /* for vsnprintf */
00029 #include <stdio.h>
00030 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
00031 #include "dbus-string-private.h"
00032 #include "dbus-marshal-basic.h" /* probably should be removed by moving the usage of DBUS_TYPE
00033                                  * into the marshaling-related files
00034                                  */
00035 /* for DBUS_VA_COPY */
00036 #include "dbus-sysdeps.h"
00037 
00076 static void
00077 fixup_alignment (DBusRealString *real)
00078 {
00079   unsigned char *aligned;
00080   unsigned char *real_block;
00081   unsigned int old_align_offset;
00082 
00083   /* we have to have extra space in real->allocated for the align offset and nul byte */
00084   _dbus_assert (real->len <= real->allocated - _DBUS_STRING_ALLOCATION_PADDING);
00085   
00086   old_align_offset = real->align_offset;
00087   real_block = real->str - old_align_offset;
00088   
00089   aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
00090 
00091   real->align_offset = aligned - real_block;
00092   real->str = aligned;
00093   
00094   if (old_align_offset != real->align_offset)
00095     {
00096       /* Here comes the suck */
00097       memmove (real_block + real->align_offset,
00098                real_block + old_align_offset,
00099                real->len + 1);
00100     }
00101 
00102   _dbus_assert (real->align_offset < 8);
00103   _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
00104 }
00105 
00106 static void
00107 undo_alignment (DBusRealString *real)
00108 {
00109   if (real->align_offset != 0)
00110     {
00111       memmove (real->str - real->align_offset,
00112                real->str,
00113                real->len + 1);
00114 
00115       real->str = real->str - real->align_offset;
00116       real->align_offset = 0;
00117     }
00118 }
00119 
00129 dbus_bool_t
00130 _dbus_string_init_preallocated (DBusString *str,
00131                                 int         allocate_size)
00132 {
00133   DBusRealString *real;
00134   
00135   _dbus_assert (str != NULL);
00136 
00137   _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
00138   
00139   real = (DBusRealString*) str;
00140 
00141   /* It's very important not to touch anything
00142    * other than real->str if we're going to fail,
00143    * since we also use this function to reset
00144    * an existing string, e.g. in _dbus_string_steal_data()
00145    */
00146   
00147   real->str = dbus_malloc (_DBUS_STRING_ALLOCATION_PADDING + allocate_size);
00148   if (real->str == NULL)
00149     return FALSE;  
00150   
00151   real->allocated = _DBUS_STRING_ALLOCATION_PADDING + allocate_size;
00152   real->len = 0;
00153   real->str[real->len] = '\0';
00154   
00155   real->max_length = _DBUS_STRING_MAX_MAX_LENGTH;
00156   real->constant = FALSE;
00157   real->locked = FALSE;
00158   real->invalid = FALSE;
00159   real->align_offset = 0;
00160   
00161   fixup_alignment (real);
00162   
00163   return TRUE;
00164 }
00165 
00173 dbus_bool_t
00174 _dbus_string_init (DBusString *str)
00175 {
00176   return _dbus_string_init_preallocated (str, 0);
00177 }
00178 
00179 #ifdef DBUS_BUILD_TESTS
00180 /* The max length thing is sort of a historical artifact
00181  * from a feature that turned out to be dumb; perhaps
00182  * we should purge it entirely. The problem with
00183  * the feature is that it looks like memory allocation
00184  * failure, but is not a transient or resolvable failure.
00185  */
00186 static void
00187 set_max_length (DBusString *str,
00188                 int         max_length)
00189 {
00190   DBusRealString *real;
00191   
00192   real = (DBusRealString*) str;
00193 
00194   real->max_length = max_length;
00195 }
00196 #endif /* DBUS_BUILD_TESTS */
00197 
00207 void
00208 _dbus_string_init_const (DBusString *str,
00209                          const char *value)
00210 {
00211   _dbus_assert (value != NULL);
00212   
00213   _dbus_string_init_const_len (str, value,
00214                                strlen (value));
00215 }
00216 
00227 void
00228 _dbus_string_init_const_len (DBusString *str,
00229                              const char *value,
00230                              int         len)
00231 {
00232   DBusRealString *real;
00233   
00234   _dbus_assert (str != NULL);
00235   _dbus_assert (value != NULL);
00236   _dbus_assert (len <= _DBUS_STRING_MAX_MAX_LENGTH);
00237   _dbus_assert (len >= 0);
00238   
00239   real = (DBusRealString*) str;
00240   
00241   real->str = (unsigned char*) value;
00242   real->len = len;
00243   real->allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
00244   real->max_length = real->len + 1;
00245   real->constant = TRUE;
00246   real->locked = TRUE;
00247   real->invalid = FALSE;
00248   real->align_offset = 0;
00249 
00250   /* We don't require const strings to be 8-byte aligned as the
00251    * memory is coming from elsewhere.
00252    */
00253 }
00254 
00260 void
00261 _dbus_string_free (DBusString *str)
00262 {
00263   DBusRealString *real = (DBusRealString*) str;
00264   DBUS_GENERIC_STRING_PREAMBLE (real);
00265   
00266   if (real->constant)
00267     return;
00268   dbus_free (real->str - real->align_offset);
00269 
00270   real->invalid = TRUE;
00271 }
00272 
00273 #ifdef DBUS_BUILD_TESTS
00274 /* Not using this feature at the moment,
00275  * so marked DBUS_BUILD_TESTS-only
00276  */
00286 void
00287 _dbus_string_lock (DBusString *str)
00288 {  
00289   DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
00290 
00291   real->locked = TRUE;
00292 
00293   /* Try to realloc to avoid excess memory usage, since
00294    * we know we won't change the string further
00295    */
00296 #define MAX_WASTE 48
00297   if (real->allocated - MAX_WASTE > real->len)
00298     {
00299       unsigned char *new_str;
00300       int new_allocated;
00301 
00302       new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
00303 
00304       new_str = dbus_realloc (real->str - real->align_offset,
00305                               new_allocated);
00306       if (new_str != NULL)
00307         {
00308           real->str = new_str + real->align_offset;
00309           real->allocated = new_allocated;
00310           fixup_alignment (real);
00311         }
00312     }
00313 }
00314 #endif /* DBUS_BUILD_TESTS */
00315 
00316 static dbus_bool_t
00317 reallocate_for_length (DBusRealString *real,
00318                        int             new_length)
00319 {
00320   int new_allocated;
00321   unsigned char *new_str;
00322 
00323   /* at least double our old allocation to avoid O(n), avoiding
00324    * overflow
00325    */
00326   if (real->allocated > (_DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
00327     new_allocated = _DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
00328   else
00329     new_allocated = real->allocated * 2;
00330 
00331   /* if you change the code just above here, run the tests without
00332    * the following assert-only hack before you commit
00333    */
00334   /* This is keyed off asserts in addition to tests so when you
00335    * disable asserts to profile, you don't get this destroyer
00336    * of profiles.
00337    */
00338 #ifdef DBUS_DISABLE_ASSERT
00339 #else
00340 #ifdef DBUS_BUILD_TESTS
00341   new_allocated = 0; /* ensure a realloc every time so that we go
00342                       * through all malloc failure codepaths
00343                       */
00344 #endif /* DBUS_BUILD_TESTS */
00345 #endif /* !DBUS_DISABLE_ASSERT */
00346 
00347   /* But be sure we always alloc at least space for the new length */
00348   new_allocated = MAX (new_allocated,
00349                        new_length + _DBUS_STRING_ALLOCATION_PADDING);
00350 
00351   _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
00352   new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
00353   if (_DBUS_UNLIKELY (new_str == NULL))
00354     return FALSE;
00355 
00356   real->str = new_str + real->align_offset;
00357   real->allocated = new_allocated;
00358   fixup_alignment (real);
00359 
00360   return TRUE;
00361 }
00362 
00363 static dbus_bool_t
00364 set_length (DBusRealString *real,
00365             int             new_length)
00366 {
00367   /* Note, we are setting the length not including nul termination */
00368 
00369   /* exceeding max length is the same as failure to allocate memory */
00370   if (_DBUS_UNLIKELY (new_length > real->max_length))
00371     return FALSE;
00372   else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
00373            _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
00374     return FALSE;
00375   else
00376     {
00377       real->len = new_length;
00378       real->str[new_length] = '\0';
00379       return TRUE;
00380     }
00381 }
00382 
00383 static dbus_bool_t
00384 open_gap (int             len,
00385           DBusRealString *dest,
00386           int             insert_at)
00387 {
00388   if (len == 0)
00389     return TRUE;
00390 
00391   if (len > dest->max_length - dest->len)
00392     return FALSE; /* detected overflow of dest->len + len below */
00393   
00394   if (!set_length (dest, dest->len + len))
00395     return FALSE;
00396 
00397   memmove (dest->str + insert_at + len, 
00398            dest->str + insert_at,
00399            dest->len - len - insert_at);
00400 
00401   return TRUE;
00402 }
00403 
00404 #ifndef _dbus_string_get_data
00405 
00416 char*
00417 _dbus_string_get_data (DBusString *str)
00418 {
00419   DBUS_STRING_PREAMBLE (str);
00420   
00421   return (char*) real->str;
00422 }
00423 #endif /* _dbus_string_get_data */
00424 
00425 /* only do the function if we don't have the macro */
00426 #ifndef _dbus_string_get_const_data
00427 
00433 const char*
00434 _dbus_string_get_const_data (const DBusString  *str)
00435 {
00436   DBUS_CONST_STRING_PREAMBLE (str);
00437   
00438   return (const char*) real->str;
00439 }
00440 #endif /* _dbus_string_get_const_data */
00441 
00455 char*
00456 _dbus_string_get_data_len (DBusString *str,
00457                            int         start,
00458                            int         len)
00459 {
00460   DBUS_STRING_PREAMBLE (str);
00461   _dbus_assert (start >= 0);
00462   _dbus_assert (len >= 0);
00463   _dbus_assert (start <= real->len);
00464   _dbus_assert (len <= real->len - start);
00465   
00466   return (char*) real->str + start;
00467 }
00468 
00469 /* only do the function if we don't have the macro */
00470 #ifndef _dbus_string_get_const_data_len
00471 
00479 const char*
00480 _dbus_string_get_const_data_len (const DBusString  *str,
00481                                  int                start,
00482                                  int                len)
00483 {
00484   DBUS_CONST_STRING_PREAMBLE (str);
00485   _dbus_assert (start >= 0);
00486   _dbus_assert (len >= 0);
00487   _dbus_assert (start <= real->len);
00488   _dbus_assert (len <= real->len - start);
00489   
00490   return (const char*) real->str + start;
00491 }
00492 #endif /* _dbus_string_get_const_data_len */
00493 
00494 /* only do the function if we don't have the macro */
00495 #ifndef _dbus_string_set_byte
00496 
00503 void
00504 _dbus_string_set_byte (DBusString    *str,
00505                        int            i,
00506                        unsigned char  byte)
00507 {
00508   DBUS_STRING_PREAMBLE (str);
00509   _dbus_assert (i < real->len);
00510   _dbus_assert (i >= 0);
00511   
00512   real->str[i] = byte;
00513 }
00514 #endif /* _dbus_string_set_byte */
00515 
00516 /* only have the function if we didn't create a macro */
00517 #ifndef _dbus_string_get_byte
00518 
00527 unsigned char
00528 _dbus_string_get_byte (const DBusString  *str,
00529                        int                start)
00530 {
00531   DBUS_CONST_STRING_PREAMBLE (str);
00532   _dbus_assert (start <= real->len);
00533   _dbus_assert (start >= 0);
00534   
00535   return real->str[start];
00536 }
00537 #endif /* _dbus_string_get_byte */
00538 
00549 dbus_bool_t
00550 _dbus_string_insert_bytes (DBusString   *str,
00551                            int           i,
00552                            int           n_bytes,
00553                            unsigned char byte)
00554 {
00555   DBUS_STRING_PREAMBLE (str);
00556   _dbus_assert (i <= real->len);
00557   _dbus_assert (i >= 0);
00558   _dbus_assert (n_bytes >= 0);
00559 
00560   if (n_bytes == 0)
00561     return TRUE;
00562   
00563   if (!open_gap (n_bytes, real, i))
00564     return FALSE;
00565   
00566   memset (real->str + i, byte, n_bytes);
00567 
00568   return TRUE;
00569 }
00570 
00579 dbus_bool_t
00580 _dbus_string_insert_byte (DBusString   *str,
00581                            int           i,
00582                            unsigned char byte)
00583 {
00584   DBUS_STRING_PREAMBLE (str);
00585   _dbus_assert (i <= real->len);
00586   _dbus_assert (i >= 0);
00587   
00588   if (!open_gap (1, real, i))
00589     return FALSE;
00590 
00591   real->str[i] = byte;
00592 
00593   return TRUE;
00594 }
00595 
00606 dbus_bool_t
00607 _dbus_string_steal_data (DBusString        *str,
00608                          char             **data_return)
00609 {
00610   int old_max_length;
00611   DBUS_STRING_PREAMBLE (str);
00612   _dbus_assert (data_return != NULL);
00613 
00614   undo_alignment (real);
00615   
00616   *data_return = (char*) real->str;
00617 
00618   old_max_length = real->max_length;
00619   
00620   /* reset the string */
00621   if (!_dbus_string_init (str))
00622     {
00623       /* hrm, put it back then */
00624       real->str = (unsigned char*) *data_return;
00625       *data_return = NULL;
00626       fixup_alignment (real);
00627       return FALSE;
00628     }
00629 
00630   real->max_length = old_max_length;
00631 
00632   return TRUE;
00633 }
00634 
00635 #ifdef DBUS_BUILD_TESTS
00636 
00651 dbus_bool_t
00652 _dbus_string_steal_data_len (DBusString        *str,
00653                              char             **data_return,
00654                              int                start,
00655                              int                len)
00656 {
00657   DBusString dest;
00658   DBUS_STRING_PREAMBLE (str);
00659   _dbus_assert (data_return != NULL);
00660   _dbus_assert (start >= 0);
00661   _dbus_assert (len >= 0);
00662   _dbus_assert (start <= real->len);
00663   _dbus_assert (len <= real->len - start);
00664 
00665   if (!_dbus_string_init (&dest))
00666     return FALSE;
00667 
00668   set_max_length (&dest, real->max_length);
00669   
00670   if (!_dbus_string_move_len (str, start, len, &dest, 0))
00671     {
00672       _dbus_string_free (&dest);
00673       return FALSE;
00674     }
00675 
00676   _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
00677   if (!_dbus_string_steal_data (&dest, data_return))
00678     {
00679       _dbus_string_free (&dest);
00680       return FALSE;
00681     }
00682 
00683   _dbus_string_free (&dest);
00684   return TRUE;
00685 }
00686 #endif /* DBUS_BUILD_TESTS */
00687 
00695 dbus_bool_t
00696 _dbus_string_copy_data (const DBusString  *str,
00697                         char             **data_return)
00698 {
00699   DBUS_CONST_STRING_PREAMBLE (str);
00700   _dbus_assert (data_return != NULL);
00701   
00702   *data_return = dbus_malloc (real->len + 1);
00703   if (*data_return == NULL)
00704     return FALSE;
00705 
00706   memcpy (*data_return, real->str, real->len + 1);
00707 
00708   return TRUE;
00709 }
00710 
00719 void
00720 _dbus_string_copy_to_buffer (const DBusString  *str,
00721                              char              *buffer,
00722                              int                avail_len)
00723 {
00724   int copy_len;
00725   DBUS_CONST_STRING_PREAMBLE (str);
00726 
00727   _dbus_assert (avail_len >= 0);
00728 
00729   copy_len = MIN (avail_len, real->len+1);
00730   memcpy (buffer, real->str, copy_len);
00731   if (avail_len > 0 && avail_len == copy_len)
00732     buffer[avail_len-1] = '\0';
00733 }
00734 
00735 #ifdef DBUS_BUILD_TESTS
00736 
00745 dbus_bool_t
00746 _dbus_string_copy_data_len (const DBusString  *str,
00747                             char             **data_return,
00748                             int                start,
00749                             int                len)
00750 {
00751   DBusString dest;
00752 
00753   DBUS_CONST_STRING_PREAMBLE (str);
00754   _dbus_assert (data_return != NULL);
00755   _dbus_assert (start >= 0);
00756   _dbus_assert (len >= 0);
00757   _dbus_assert (start <= real->len);
00758   _dbus_assert (len <= real->len - start);
00759 
00760   if (!_dbus_string_init (&dest))
00761     return FALSE;
00762 
00763   set_max_length (&dest, real->max_length);
00764 
00765   if (!_dbus_string_copy_len (str, start, len, &dest, 0))
00766     {
00767       _dbus_string_free (&dest);
00768       return FALSE;
00769     }
00770 
00771   if (!_dbus_string_steal_data (&dest, data_return))
00772     {
00773       _dbus_string_free (&dest);
00774       return FALSE;
00775     }
00776 
00777   _dbus_string_free (&dest);
00778   return TRUE;
00779 }
00780 #endif /* DBUS_BUILD_TESTS */
00781 
00782 /* Only have the function if we don't have the macro */
00783 #ifndef _dbus_string_get_length
00784 
00789 int
00790 _dbus_string_get_length (const DBusString  *str)
00791 {
00792   DBUS_CONST_STRING_PREAMBLE (str);
00793   
00794   return real->len;
00795 }
00796 #endif /* !_dbus_string_get_length */
00797 
00810 dbus_bool_t
00811 _dbus_string_lengthen (DBusString *str,
00812                        int         additional_length)
00813 {
00814   DBUS_STRING_PREAMBLE (str);  
00815   _dbus_assert (additional_length >= 0);
00816 
00817   if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
00818     return FALSE; /* would overflow */
00819   
00820   return set_length (real,
00821                      real->len + additional_length);
00822 }
00823 
00830 void
00831 _dbus_string_shorten (DBusString *str,
00832                       int         length_to_remove)
00833 {
00834   DBUS_STRING_PREAMBLE (str);
00835   _dbus_assert (length_to_remove >= 0);
00836   _dbus_assert (length_to_remove <= real->len);
00837 
00838   set_length (real,
00839               real->len - length_to_remove);
00840 }
00841 
00852 dbus_bool_t
00853 _dbus_string_set_length (DBusString *str,
00854                          int         length)
00855 {
00856   DBUS_STRING_PREAMBLE (str);
00857   _dbus_assert (length >= 0);
00858 
00859   return set_length (real, length);
00860 }
00861 
00862 static dbus_bool_t
00863 align_insert_point_then_open_gap (DBusString *str,
00864                                   int        *insert_at_p,
00865                                   int         alignment,
00866                                   int         gap_size)
00867 {
00868   unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
00869   unsigned long gap_pos;
00870   int insert_at;
00871   int delta;
00872   DBUS_STRING_PREAMBLE (str);
00873   _dbus_assert (alignment >= 1);
00874   _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
00875 
00876   insert_at = *insert_at_p;
00877 
00878   _dbus_assert (insert_at <= real->len);
00879   
00880   gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
00881   new_len = real->len + (gap_pos - insert_at) + gap_size;
00882   
00883   if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
00884     return FALSE;
00885   
00886   delta = new_len - real->len;
00887   _dbus_assert (delta >= 0);
00888 
00889   if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
00890     {
00891       _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
00892       return TRUE;
00893     }
00894 
00895   if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
00896                                  real, insert_at)))
00897     return FALSE;
00898 
00899   /* nul the padding if we had to add any padding */
00900   if (gap_size < delta)
00901     {
00902       memset (&real->str[insert_at], '\0',
00903               gap_pos - insert_at);
00904     }
00905 
00906   *insert_at_p = gap_pos;
00907   
00908   return TRUE;
00909 }
00910 
00911 static dbus_bool_t
00912 align_length_then_lengthen (DBusString *str,
00913                             int         alignment,
00914                             int         then_lengthen_by)
00915 {
00916   int insert_at;
00917 
00918   insert_at = _dbus_string_get_length (str);
00919   
00920   return align_insert_point_then_open_gap (str,
00921                                            &insert_at,
00922                                            alignment, then_lengthen_by);
00923 }
00924 
00933 dbus_bool_t
00934 _dbus_string_align_length (DBusString *str,
00935                            int         alignment)
00936 {
00937   return align_length_then_lengthen (str, alignment, 0);
00938 }
00939 
00949 dbus_bool_t
00950 _dbus_string_alloc_space (DBusString        *str,
00951                           int                extra_bytes)
00952 {
00953   if (!_dbus_string_lengthen (str, extra_bytes))
00954     return FALSE;
00955   _dbus_string_shorten (str, extra_bytes);
00956 
00957   return TRUE;
00958 }
00959 
00960 static dbus_bool_t
00961 append (DBusRealString *real,
00962         const char     *buffer,
00963         int             buffer_len)
00964 {
00965   if (buffer_len == 0)
00966     return TRUE;
00967 
00968   if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
00969     return FALSE;
00970 
00971   memcpy (real->str + (real->len - buffer_len),
00972           buffer,
00973           buffer_len);
00974 
00975   return TRUE;
00976 }
00977 
00985 dbus_bool_t
00986 _dbus_string_append (DBusString *str,
00987                      const char *buffer)
00988 {
00989   unsigned long buffer_len;
00990   
00991   DBUS_STRING_PREAMBLE (str);
00992   _dbus_assert (buffer != NULL);
00993   
00994   buffer_len = strlen (buffer);
00995   if (buffer_len > (unsigned long) real->max_length)
00996     return FALSE;
00997   
00998   return append (real, buffer, buffer_len);
00999 }
01000 
01002 #define ASSIGN_2_OCTETS(p, octets) \
01003   *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
01004 
01006 #define ASSIGN_4_OCTETS(p, octets) \
01007   *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
01008 
01009 #ifdef DBUS_HAVE_INT64
01010 
01011 #define ASSIGN_8_OCTETS(p, octets) \
01012   *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
01013 #else
01014 
01015 #define ASSIGN_8_OCTETS(p, octets)              \
01016 do {                                            \
01017   unsigned char *b;                             \
01018                                                 \
01019   b = p;                                        \
01020                                                 \
01021   *b++ = octets[0];                             \
01022   *b++ = octets[1];                             \
01023   *b++ = octets[2];                             \
01024   *b++ = octets[3];                             \
01025   *b++ = octets[4];                             \
01026   *b++ = octets[5];                             \
01027   *b++ = octets[6];                             \
01028   *b++ = octets[7];                             \
01029   _dbus_assert (b == p + 8);                    \
01030 } while (0)
01031 #endif /* DBUS_HAVE_INT64 */
01032 
01033 #ifdef DBUS_BUILD_TESTS
01034 
01042 dbus_bool_t
01043 _dbus_string_append_4_aligned (DBusString         *str,
01044                                const unsigned char octets[4])
01045 {
01046   DBUS_STRING_PREAMBLE (str);
01047   
01048   if (!align_length_then_lengthen (str, 4, 4))
01049     return FALSE;
01050 
01051   ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
01052 
01053   return TRUE;
01054 }
01055 #endif /* DBUS_BUILD_TESTS */
01056 
01057 #ifdef DBUS_BUILD_TESTS
01058 
01066 dbus_bool_t
01067 _dbus_string_append_8_aligned (DBusString         *str,
01068                                const unsigned char octets[8])
01069 {
01070   DBUS_STRING_PREAMBLE (str);
01071   
01072   if (!align_length_then_lengthen (str, 8, 8))
01073     return FALSE;
01074 
01075   ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
01076 
01077   return TRUE;
01078 }
01079 #endif /* DBUS_BUILD_TESTS */
01080 
01090 dbus_bool_t
01091 _dbus_string_insert_2_aligned (DBusString         *str,
01092                                int                 insert_at,
01093                                const unsigned char octets[4])
01094 {
01095   DBUS_STRING_PREAMBLE (str);
01096   
01097   if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
01098     return FALSE;
01099 
01100   ASSIGN_2_OCTETS (real->str + insert_at, octets);
01101 
01102   return TRUE;
01103 }
01104 
01114 dbus_bool_t
01115 _dbus_string_insert_4_aligned (DBusString         *str,
01116                                int                 insert_at,
01117                                const unsigned char octets[4])
01118 {
01119   DBUS_STRING_PREAMBLE (str);
01120   
01121   if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
01122     return FALSE;
01123 
01124   ASSIGN_4_OCTETS (real->str + insert_at, octets);
01125 
01126   return TRUE;
01127 }
01128 
01138 dbus_bool_t
01139 _dbus_string_insert_8_aligned (DBusString         *str,
01140                                int                 insert_at,
01141                                const unsigned char octets[8])
01142 {
01143   DBUS_STRING_PREAMBLE (str);
01144   
01145   if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
01146     return FALSE;
01147 
01148   _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
01149   
01150   ASSIGN_8_OCTETS (real->str + insert_at, octets);
01151 
01152   return TRUE;
01153 }
01154 
01155 
01166 dbus_bool_t
01167 _dbus_string_insert_alignment (DBusString        *str,
01168                                int               *insert_at,
01169                                int                alignment)
01170 {
01171   DBUS_STRING_PREAMBLE (str);
01172   
01173   if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
01174     return FALSE;
01175 
01176   _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
01177 
01178   return TRUE;
01179 }
01180 
01190 dbus_bool_t
01191 _dbus_string_append_printf_valist  (DBusString        *str,
01192                                     const char        *format,
01193                                     va_list            args)
01194 {
01195   int len;
01196   char c;
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 = vsnprintf (&c, 1, 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 
01809 dbus_bool_t
01810 _dbus_string_find_to (const DBusString *str,
01811                       int               start,
01812                       int               end,
01813                       const char       *substr,
01814                       int              *found)
01815 {
01816   int i;
01817   DBUS_CONST_STRING_PREAMBLE (str);
01818   _dbus_assert (substr != NULL);
01819   _dbus_assert (start <= real->len);
01820   _dbus_assert (start >= 0);
01821   _dbus_assert (substr != NULL);
01822   _dbus_assert (end <= real->len);
01823   _dbus_assert (start <= end);
01824 
01825   /* we always "find" an empty string */
01826   if (*substr == '\0')
01827     {
01828       if (found)
01829         *found = start;
01830       return TRUE;
01831     }
01832 
01833   i = start;
01834   while (i < end)
01835     {
01836       if (real->str[i] == substr[0])
01837         {
01838           int j = i + 1;
01839           
01840           while (j < end)
01841             {
01842               if (substr[j - i] == '\0')
01843                 break;
01844               else if (real->str[j] != substr[j - i])
01845                 break;
01846               
01847               ++j;
01848             }
01849 
01850           if (substr[j - i] == '\0')
01851             {
01852               if (found)
01853                 *found = i;
01854               return TRUE;
01855             }
01856         }
01857       
01858       ++i;
01859     }
01860 
01861   if (found)
01862     *found = end;
01863   
01864   return FALSE;  
01865 }
01866 
01877 dbus_bool_t
01878 _dbus_string_find_blank (const DBusString *str,
01879                          int               start,
01880                          int              *found)
01881 {
01882   int i;
01883   DBUS_CONST_STRING_PREAMBLE (str);
01884   _dbus_assert (start <= real->len);
01885   _dbus_assert (start >= 0);
01886   
01887   i = start;
01888   while (i < real->len)
01889     {
01890       if (real->str[i] == ' ' ||
01891           real->str[i] == '\t')
01892         {
01893           if (found)
01894             *found = i;
01895           return TRUE;
01896         }
01897       
01898       ++i;
01899     }
01900 
01901   if (found)
01902     *found = real->len;
01903   
01904   return FALSE;
01905 }
01906 
01915 void
01916 _dbus_string_skip_blank (const DBusString *str,
01917                          int               start,
01918                          int              *end)
01919 {
01920   int i;
01921   DBUS_CONST_STRING_PREAMBLE (str);
01922   _dbus_assert (start <= real->len);
01923   _dbus_assert (start >= 0);
01924   
01925   i = start;
01926   while (i < real->len)
01927     {
01928       if (!(real->str[i] == ' ' ||
01929             real->str[i] == '\t'))
01930         break;
01931       
01932       ++i;
01933     }
01934 
01935   _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
01936                                     real->str[i] == '\t'));
01937   
01938   if (end)
01939     *end = i;
01940 }
01941 
01957 dbus_bool_t
01958 _dbus_string_pop_line (DBusString *source,
01959                        DBusString *dest)
01960 {
01961   int eol;
01962   dbus_bool_t have_newline;
01963   
01964   _dbus_string_set_length (dest, 0);
01965   
01966   eol = 0;
01967   if (_dbus_string_find (source, 0, "\n", &eol))
01968     {
01969       have_newline = TRUE;
01970       eol += 1; /* include newline */
01971     }
01972   else
01973     {
01974       eol = _dbus_string_get_length (source);
01975       have_newline = FALSE;
01976     }
01977 
01978   if (eol == 0)
01979     return FALSE; /* eof */
01980   
01981   if (!_dbus_string_move_len (source, 0, eol,
01982                               dest, 0))
01983     {
01984       return FALSE;
01985     }
01986 
01987   /* dump the newline and the \r if we have one */
01988   if (have_newline)
01989     {
01990       dbus_bool_t have_cr;
01991       
01992       _dbus_assert (_dbus_string_get_length (dest) > 0);
01993 
01994       if (_dbus_string_get_length (dest) > 1 &&
01995           _dbus_string_get_byte (dest,
01996                                  _dbus_string_get_length (dest) - 2) == '\r')
01997         have_cr = TRUE;
01998       else
01999         have_cr = FALSE;
02000         
02001       _dbus_string_set_length (dest,
02002                                _dbus_string_get_length (dest) -
02003                                (have_cr ? 2 : 1));
02004     }
02005   
02006   return TRUE;
02007 }
02008 
02009 #ifdef DBUS_BUILD_TESTS
02010 
02016 void
02017 _dbus_string_delete_first_word (DBusString *str)
02018 {
02019   int i;
02020   
02021   if (_dbus_string_find_blank (str, 0, &i))
02022     _dbus_string_skip_blank (str, i, &i);
02023 
02024   _dbus_string_delete (str, 0, i);
02025 }
02026 #endif
02027 
02028 #ifdef DBUS_BUILD_TESTS
02029 
02034 void
02035 _dbus_string_delete_leading_blanks (DBusString *str)
02036 {
02037   int i;
02038   
02039   _dbus_string_skip_blank (str, 0, &i);
02040 
02041   if (i > 0)
02042     _dbus_string_delete (str, 0, i);
02043 }
02044 #endif
02045 
02055 dbus_bool_t
02056 _dbus_string_equal (const DBusString *a,
02057                     const DBusString *b)
02058 {
02059   const unsigned char *ap;
02060   const unsigned char *bp;
02061   const unsigned char *a_end;
02062   const DBusRealString *real_a = (const DBusRealString*) a;
02063   const DBusRealString *real_b = (const DBusRealString*) b;
02064   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02065   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02066 
02067   if (real_a->len != real_b->len)
02068     return FALSE;
02069 
02070   ap = real_a->str;
02071   bp = real_b->str;
02072   a_end = real_a->str + real_a->len;
02073   while (ap != a_end)
02074     {
02075       if (*ap != *bp)
02076         return FALSE;
02077       
02078       ++ap;
02079       ++bp;
02080     }
02081 
02082   return TRUE;
02083 }
02084 
02085 #ifdef DBUS_BUILD_TESTS
02086 
02099 dbus_bool_t
02100 _dbus_string_equal_len (const DBusString *a,
02101                         const DBusString *b,
02102                         int               len)
02103 {
02104   const unsigned char *ap;
02105   const unsigned char *bp;
02106   const unsigned char *a_end;
02107   const DBusRealString *real_a = (const DBusRealString*) a;
02108   const DBusRealString *real_b = (const DBusRealString*) b;
02109   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02110   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02111 
02112   if (real_a->len != real_b->len &&
02113       (real_a->len < len || real_b->len < len))
02114     return FALSE;
02115 
02116   ap = real_a->str;
02117   bp = real_b->str;
02118   a_end = real_a->str + MIN (real_a->len, len);
02119   while (ap != a_end)
02120     {
02121       if (*ap != *bp)
02122         return FALSE;
02123       
02124       ++ap;
02125       ++bp;
02126     }
02127 
02128   return TRUE;
02129 }
02130 #endif /* DBUS_BUILD_TESTS */
02131 
02148 dbus_bool_t
02149 _dbus_string_equal_substring (const DBusString  *a,
02150                               int                a_start,
02151                               int                a_len,
02152                               const DBusString  *b,
02153                               int                b_start)
02154 {
02155   const unsigned char *ap;
02156   const unsigned char *bp;
02157   const unsigned char *a_end;
02158   const DBusRealString *real_a = (const DBusRealString*) a;
02159   const DBusRealString *real_b = (const DBusRealString*) b;
02160   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02161   DBUS_GENERIC_STRING_PREAMBLE (real_b);
02162   _dbus_assert (a_start >= 0);
02163   _dbus_assert (a_len >= 0);
02164   _dbus_assert (a_start <= real_a->len);
02165   _dbus_assert (a_len <= real_a->len - a_start);
02166   _dbus_assert (b_start >= 0);
02167   _dbus_assert (b_start <= real_b->len);
02168   
02169   if (a_len > real_b->len - b_start)
02170     return FALSE;
02171 
02172   ap = real_a->str + a_start;
02173   bp = real_b->str + b_start;
02174   a_end = ap + a_len;
02175   while (ap != a_end)
02176     {
02177       if (*ap != *bp)
02178         return FALSE;
02179       
02180       ++ap;
02181       ++bp;
02182     }
02183 
02184   _dbus_assert (bp <= (real_b->str + real_b->len));
02185   
02186   return TRUE;
02187 }
02188 
02196 dbus_bool_t
02197 _dbus_string_equal_c_str (const DBusString *a,
02198                           const char       *c_str)
02199 {
02200   const unsigned char *ap;
02201   const unsigned char *bp;
02202   const unsigned char *a_end;
02203   const DBusRealString *real_a = (const DBusRealString*) a;
02204   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02205   _dbus_assert (c_str != NULL);
02206   
02207   ap = real_a->str;
02208   bp = (const unsigned char*) c_str;
02209   a_end = real_a->str + real_a->len;
02210   while (ap != a_end && *bp)
02211     {
02212       if (*ap != *bp)
02213         return FALSE;
02214       
02215       ++ap;
02216       ++bp;
02217     }
02218 
02219   if (ap != a_end || *bp)
02220     return FALSE;
02221   
02222   return TRUE;
02223 }
02224 
02225 #ifdef DBUS_BUILD_TESTS
02226 
02233 dbus_bool_t
02234 _dbus_string_starts_with_c_str (const DBusString *a,
02235                                 const char       *c_str)
02236 {
02237   const unsigned char *ap;
02238   const unsigned char *bp;
02239   const unsigned char *a_end;
02240   const DBusRealString *real_a = (const DBusRealString*) a;
02241   DBUS_GENERIC_STRING_PREAMBLE (real_a);
02242   _dbus_assert (c_str != NULL);
02243   
02244   ap = real_a->str;
02245   bp = (const unsigned char*) c_str;
02246   a_end = real_a->str + real_a->len;
02247   while (ap != a_end && *bp)
02248     {
02249       if (*ap != *bp)
02250         return FALSE;
02251       
02252       ++ap;
02253       ++bp;
02254     }
02255 
02256   if (*bp == '\0')
02257     return TRUE;
02258   else
02259     return FALSE;
02260 }
02261 #endif /* DBUS_BUILD_TESTS */
02262 
02271 dbus_bool_t
02272 _dbus_string_append_byte_as_hex (DBusString *str,
02273                                  int         byte)
02274 {
02275   const char hexdigits[16] = {
02276     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
02277     'a', 'b', 'c', 'd', 'e', 'f'
02278   };
02279 
02280   if (!_dbus_string_append_byte (str,
02281                                  hexdigits[(byte >> 4)]))
02282     return FALSE;
02283   
02284   if (!_dbus_string_append_byte (str,
02285                                  hexdigits[(byte & 0x0f)]))
02286     {
02287       _dbus_string_set_length (str,
02288                                _dbus_string_get_length (str) - 1);
02289       return FALSE;
02290     }
02291 
02292   return TRUE;
02293 }
02294 
02305 dbus_bool_t
02306 _dbus_string_hex_encode (const DBusString *source,
02307                          int               start,
02308                          DBusString       *dest,
02309                          int               insert_at)
02310 {
02311   DBusString result;
02312   const unsigned char *p;
02313   const unsigned char *end;
02314   dbus_bool_t retval;
02315   
02316   _dbus_assert (start <= _dbus_string_get_length (source));
02317 
02318   if (!_dbus_string_init (&result))
02319     return FALSE;
02320 
02321   retval = FALSE;
02322   
02323   p = (const unsigned char*) _dbus_string_get_const_data (source);
02324   end = p + _dbus_string_get_length (source);
02325   p += start;
02326   
02327   while (p != end)
02328     {
02329       if (!_dbus_string_append_byte_as_hex (&result, *p))
02330         goto out;
02331       
02332       ++p;
02333     }
02334 
02335   if (!_dbus_string_move (&result, 0, dest, insert_at))
02336     goto out;
02337 
02338   retval = TRUE;
02339 
02340  out:
02341   _dbus_string_free (&result);
02342   return retval;
02343 }
02344 
02355 dbus_bool_t
02356 _dbus_string_hex_decode (const DBusString *source,
02357                          int               start,
02358                          int              *end_return,
02359                          DBusString       *dest,
02360                          int               insert_at)
02361 {
02362   DBusString result;
02363   const unsigned char *p;
02364   const unsigned char *end;
02365   dbus_bool_t retval;
02366   dbus_bool_t high_bits;
02367   
02368   _dbus_assert (start <= _dbus_string_get_length (source));
02369 
02370   if (!_dbus_string_init (&result))
02371     return FALSE;
02372 
02373   retval = FALSE;
02374 
02375   high_bits = TRUE;
02376   p = (const unsigned char*) _dbus_string_get_const_data (source);
02377   end = p + _dbus_string_get_length (source);
02378   p += start;
02379   
02380   while (p != end)
02381     {
02382       unsigned int val;
02383 
02384       switch (*p)
02385         {
02386         case '0':
02387           val = 0;
02388           break;
02389         case '1':
02390           val = 1;
02391           break;
02392         case '2':
02393           val = 2;
02394           break;
02395         case '3':
02396           val = 3;
02397           break;
02398         case '4':
02399           val = 4;
02400           break;
02401         case '5':
02402           val = 5;
02403           break;
02404         case '6':
02405           val = 6;
02406           break;
02407         case '7':
02408           val = 7;
02409           break;
02410         case '8':
02411           val = 8;
02412           break;
02413         case '9':
02414           val = 9;
02415           break;
02416         case 'a':
02417         case 'A':
02418           val = 10;
02419           break;
02420         case 'b':
02421         case 'B':
02422           val = 11;
02423           break;
02424         case 'c':
02425         case 'C':
02426           val = 12;
02427           break;
02428         case 'd':
02429         case 'D':
02430           val = 13;
02431           break;
02432         case 'e':
02433         case 'E':
02434           val = 14;
02435           break;
02436         case 'f':
02437         case 'F':
02438           val = 15;
02439           break;
02440         default:
02441           goto done;
02442         }
02443 
02444       if (high_bits)
02445         {
02446           if (!_dbus_string_append_byte (&result,
02447                                          val << 4))
02448             goto out;
02449         }
02450       else
02451         {
02452           int len;
02453           unsigned char b;
02454 
02455           len = _dbus_string_get_length (&result);
02456           
02457           b = _dbus_string_get_byte (&result, len - 1);
02458 
02459           b |= val;
02460 
02461           _dbus_string_set_byte (&result, len - 1, b);
02462         }
02463 
02464       high_bits = !high_bits;
02465 
02466       ++p;
02467     }
02468 
02469  done:
02470   if (!_dbus_string_move (&result, 0, dest, insert_at))
02471     goto out;
02472 
02473   if (end_return)
02474     *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
02475 
02476   retval = TRUE;
02477   
02478  out:
02479   _dbus_string_free (&result);  
02480   return retval;
02481 }
02482 
02496 dbus_bool_t
02497 _dbus_string_validate_ascii (const DBusString *str,
02498                              int               start,
02499                              int               len)
02500 {
02501   const unsigned char *s;
02502   const unsigned char *end;
02503   DBUS_CONST_STRING_PREAMBLE (str);
02504   _dbus_assert (start >= 0);
02505   _dbus_assert (start <= real->len);
02506   _dbus_assert (len >= 0);
02507   
02508   if (len > real->len - start)
02509     return FALSE;
02510   
02511   s = real->str + start;
02512   end = s + len;
02513   while (s != end)
02514     {
02515       if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
02516         return FALSE;
02517         
02518       ++s;
02519     }
02520   
02521   return TRUE;
02522 }
02523 
02539 dbus_bool_t
02540 _dbus_string_validate_utf8  (const DBusString *str,
02541                              int               start,
02542                              int               len)
02543 {
02544   const unsigned char *p;
02545   const unsigned char *end;
02546   DBUS_CONST_STRING_PREAMBLE (str);
02547   _dbus_assert (start >= 0);
02548   _dbus_assert (start <= real->len);
02549   _dbus_assert (len >= 0);
02550 
02551   /* we are doing _DBUS_UNLIKELY() here which might be
02552    * dubious in a generic library like GLib, but in D-BUS
02553    * we know we're validating messages and that it would
02554    * only be evil/broken apps that would have invalid
02555    * UTF-8. Also, this function seems to be a performance
02556    * bottleneck in profiles.
02557    */
02558   
02559   if (_DBUS_UNLIKELY (len > real->len - start))
02560     return FALSE;
02561   
02562   p = real->str + start;
02563   end = p + len;
02564   
02565   while (p < end)
02566     {
02567       int i, mask, char_len;
02568       dbus_unichar_t result;
02569 
02570       /* nul bytes considered invalid */
02571       if (*p == '\0')
02572         break;
02573       
02574       /* Special-case ASCII; this makes us go a lot faster in
02575        * D-BUS profiles where we are typically validating
02576        * function names and such. We have to know that
02577        * all following checks will pass for ASCII though,
02578        * comments follow ...
02579        */      
02580       if (*p < 128)
02581         {
02582           ++p;
02583           continue;
02584         }
02585       
02586       UTF8_COMPUTE (*p, mask, char_len);
02587 
02588       if (_DBUS_UNLIKELY (char_len == 0))  /* ASCII: char_len == 1 */
02589         break;
02590 
02591       /* check that the expected number of bytes exists in the remaining length */
02592       if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
02593         break;
02594         
02595       UTF8_GET (result, p, i, mask, char_len);
02596 
02597       /* Check for overlong UTF-8 */
02598       if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
02599         break;
02600 #if 0
02601       /* The UNICODE_VALID check below will catch this */
02602       if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
02603         break;
02604 #endif
02605 
02606       if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
02607         break;
02608 
02609       /* UNICODE_VALID should have caught it */
02610       _dbus_assert (result != (dbus_unichar_t)-1);
02611       
02612       p += char_len;
02613     }
02614 
02615   /* See that we covered the entire length if a length was
02616    * passed in
02617    */
02618   if (_DBUS_UNLIKELY (p != end))
02619     return FALSE;
02620   else
02621     return TRUE;
02622 }
02623 
02637 dbus_bool_t
02638 _dbus_string_validate_nul (const DBusString *str,
02639                            int               start,
02640                            int               len)
02641 {
02642   const unsigned char *s;
02643   const unsigned char *end;
02644   DBUS_CONST_STRING_PREAMBLE (str);
02645   _dbus_assert (start >= 0);
02646   _dbus_assert (len >= 0);
02647   _dbus_assert (start <= real->len);
02648   
02649   if (len > real->len - start)
02650     return FALSE;
02651   
02652   s = real->str + start;
02653   end = s + len;
02654   while (s != end)
02655     {
02656       if (_DBUS_UNLIKELY (*s != '\0'))
02657         return FALSE;
02658       ++s;
02659     }
02660   
02661   return TRUE;
02662 }
02663 
02669 void
02670 _dbus_string_zero (DBusString *str)
02671 {
02672   DBUS_STRING_PREAMBLE (str);
02673 
02674   memset (real->str - real->align_offset, '\0', real->allocated);
02675 }
02678 /* tests are in dbus-string-util.c */

Generated on Tue Jul 7 15:14:00 2009 for D-BUS by  doxygen 1.4.6