1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 
3 /*
4  * Copyright (C) 2014 Red Hat
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  * 02111-1307, USA.
20  *
21  * Written by:
22  *     Jasper St. Pierre <jstpierre@mecheye.net>
23  */
24 
25 #include "config.h"
26 
27 #include "backends/native/dbus-utils.h"
28 
29 #include <glib.h>
30 
31 /* Stolen from tp_escape_as_identifier, from tp-glib,
32  * which follows the same escaping convention as systemd.
33  */
34 static inline gboolean
_esc_ident_bad(gchar c,gboolean is_first)35 _esc_ident_bad (gchar c, gboolean is_first)
36 {
37   return ((c < 'a' || c > 'z') &&
38           (c < 'A' || c > 'Z') &&
39           (c < '0' || c > '9' || is_first));
40 }
41 
42 static gchar *
escape_dbus_component(const gchar * name)43 escape_dbus_component (const gchar *name)
44 {
45   gboolean bad = FALSE;
46   size_t len = 0;
47   GString *op;
48   const gchar *ptr, *first_ok;
49 
50   g_return_val_if_fail (name != NULL, NULL);
51 
52   /* fast path for empty name */
53   if (name[0] == '\0')
54     return g_strdup ("_");
55 
56   for (ptr = name; *ptr; ptr++)
57     {
58       if (_esc_ident_bad (*ptr, ptr == name))
59         {
60           bad = TRUE;
61           len += 3;
62         }
63       else
64         len++;
65     }
66 
67   /* fast path if it's clean */
68   if (!bad)
69     return g_strdup (name);
70 
71   /* If strictly less than ptr, first_ok is the first uncopied safe character.
72    */
73   first_ok = name;
74   op = g_string_sized_new (len);
75   for (ptr = name; *ptr; ptr++)
76     {
77       if (_esc_ident_bad (*ptr, ptr == name))
78         {
79           /* copy preceding safe characters if any */
80           if (first_ok < ptr)
81             {
82               g_string_append_len (op, first_ok, ptr - first_ok);
83             }
84           /* escape the unsafe character */
85           g_string_append_printf (op, "_%02x", (unsigned char)(*ptr));
86           /* restart after it */
87           first_ok = ptr + 1;
88         }
89     }
90   /* copy trailing safe characters if any */
91   if (first_ok < ptr)
92     {
93       g_string_append_len (op, first_ok, ptr - first_ok);
94     }
95   return g_string_free (op, FALSE);
96 }
97 
98 char *
get_escaped_dbus_path(const char * prefix,const char * component)99 get_escaped_dbus_path (const char *prefix,
100                        const char *component)
101 {
102   char *escaped_component = escape_dbus_component (component);
103   char *path = g_strconcat (prefix, "/", escaped_component, NULL);
104 
105   g_free (escaped_component);
106   return path;
107 }
108