D-Bus  1.6.8
dbus-syntax.c
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-syntax.c - utility functions for strings with special syntax
00003  *
00004  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
00005  * Copyright © 2011 Nokia Corporation
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include "dbus-syntax.h"
00027 
00028 #include "dbus-internals.h"
00029 #include "dbus-marshal-validate.h"
00030 #include "dbus-shared.h"
00031 
00053 dbus_bool_t
00054 dbus_validate_path (const char       *path,
00055                     DBusError        *error)
00056 {
00057   DBusString str;
00058   int len;
00059 
00060   _dbus_return_val_if_fail (path != NULL, FALSE);
00061 
00062   _dbus_string_init_const (&str, path);
00063   len = _dbus_string_get_length (&str);
00064 
00065   /* In general, it ought to be valid... */
00066   if (_DBUS_LIKELY (_dbus_validate_path (&str, 0, len)))
00067     return TRUE;
00068 
00069   /* slow path: string is invalid, find out why */
00070 
00071   if (!_dbus_string_validate_utf8 (&str, 0, len))
00072     {
00073       /* don't quote the actual string here, since a DBusError also needs to
00074        * be valid UTF-8 */
00075       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00076                       "Object path was not valid UTF-8");
00077       return FALSE;
00078     }
00079 
00080   /* FIXME: later, diagnose exactly how it was invalid */
00081   dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00082                   "Object path was not valid: '%s'", path);
00083   return FALSE;
00084 }
00085 
00100 dbus_bool_t
00101 dbus_validate_interface (const char       *name,
00102                          DBusError        *error)
00103 {
00104   DBusString str;
00105   int len;
00106 
00107   _dbus_return_val_if_fail (name != NULL, FALSE);
00108 
00109   _dbus_string_init_const (&str, name);
00110   len = _dbus_string_get_length (&str);
00111 
00112   /* In general, it ought to be valid... */
00113   if (_DBUS_LIKELY (_dbus_validate_interface (&str, 0, len)))
00114     return TRUE;
00115 
00116   /* slow path: string is invalid, find out why */
00117 
00118   if (!_dbus_string_validate_utf8 (&str, 0, len))
00119     {
00120       /* don't quote the actual string here, since a DBusError also needs to
00121        * be valid UTF-8 */
00122       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00123                       "Interface name was not valid UTF-8");
00124       return FALSE;
00125     }
00126 
00127   /* FIXME: later, diagnose exactly how it was invalid */
00128   dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00129                   "Interface name was not valid: '%s'", name);
00130   return FALSE;
00131 }
00132 
00147 dbus_bool_t
00148 dbus_validate_member (const char       *name,
00149                       DBusError        *error)
00150 {
00151   DBusString str;
00152   int len;
00153 
00154   _dbus_return_val_if_fail (name != NULL, FALSE);
00155 
00156   _dbus_string_init_const (&str, name);
00157   len = _dbus_string_get_length (&str);
00158 
00159   /* In general, it ought to be valid... */
00160   if (_DBUS_LIKELY (_dbus_validate_member (&str, 0, len)))
00161     return TRUE;
00162 
00163   /* slow path: string is invalid, find out why */
00164 
00165   if (!_dbus_string_validate_utf8 (&str, 0, len))
00166     {
00167       /* don't quote the actual string here, since a DBusError also needs to
00168        * be valid UTF-8 */
00169       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00170                       "Member name was not valid UTF-8");
00171       return FALSE;
00172     }
00173 
00174   /* FIXME: later, diagnose exactly how it was invalid */
00175   dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00176                   "Member name was not valid: '%s'", name);
00177   return FALSE;
00178 }
00179 
00194 dbus_bool_t
00195 dbus_validate_error_name (const char       *name,
00196                           DBusError        *error)
00197 {
00198   DBusString str;
00199   int len;
00200 
00201   _dbus_return_val_if_fail (name != NULL, FALSE);
00202 
00203   _dbus_string_init_const (&str, name);
00204   len = _dbus_string_get_length (&str);
00205 
00206   /* In general, it ought to be valid... */
00207   if (_DBUS_LIKELY (_dbus_validate_error_name (&str, 0, len)))
00208     return TRUE;
00209 
00210   /* slow path: string is invalid, find out why */
00211 
00212   if (!_dbus_string_validate_utf8 (&str, 0, len))
00213     {
00214       /* don't quote the actual string here, since a DBusError also needs to
00215        * be valid UTF-8 */
00216       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00217                       "Error name was not valid UTF-8");
00218       return FALSE;
00219     }
00220 
00221   /* FIXME: later, diagnose exactly how it was invalid */
00222   dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00223                   "Error name was not valid: '%s'", name);
00224   return FALSE;
00225 }
00226 
00241 dbus_bool_t
00242 dbus_validate_bus_name (const char       *name,
00243                         DBusError        *error)
00244 {
00245   DBusString str;
00246   int len;
00247 
00248   _dbus_return_val_if_fail (name != NULL, FALSE);
00249 
00250   _dbus_string_init_const (&str, name);
00251   len = _dbus_string_get_length (&str);
00252 
00253   /* In general, it ought to be valid... */
00254   if (_DBUS_LIKELY (_dbus_validate_bus_name (&str, 0, len)))
00255     return TRUE;
00256 
00257   /* slow path: string is invalid, find out why */
00258 
00259   if (!_dbus_string_validate_utf8 (&str, 0, len))
00260     {
00261       /* don't quote the actual string here, since a DBusError also needs to
00262        * be valid UTF-8 */
00263       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00264                       "Bus name was not valid UTF-8");
00265       return FALSE;
00266     }
00267 
00268   /* FIXME: later, diagnose exactly how it was invalid */
00269   dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00270                   "Bus name was not valid: '%s'", name);
00271   return FALSE;
00272 }
00273 
00288 dbus_bool_t
00289 dbus_validate_utf8 (const char       *alleged_utf8,
00290                     DBusError        *error)
00291 {
00292   DBusString str;
00293 
00294   _dbus_return_val_if_fail (alleged_utf8 != NULL, FALSE);
00295 
00296   _dbus_string_init_const (&str, alleged_utf8);
00297 
00298   if (_DBUS_LIKELY (_dbus_string_validate_utf8 (&str, 0,
00299                                                 _dbus_string_get_length (&str))))
00300     return TRUE;
00301 
00302   /* don't quote the actual string here, since a DBusError also needs to
00303    * be valid UTF-8 */
00304   dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
00305                   "String was not valid UTF-8");
00306   return FALSE;
00307 }
00308  /* end of group */