LCOV - code coverage report
Current view: top level - basic - bus-label.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 33 35 94.3 %
Date: 2015-07-29 18:47:03 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
       2             : 
       3             : /***
       4             :   This file is part of systemd.
       5             : 
       6             :   Copyright 2013 Lennart Poettering
       7             : 
       8             :   systemd is free software; you can redistribute it and/or modify it
       9             :   under the terms of the GNU Lesser General Public License as published by
      10             :   the Free Software Foundation; either version 2.1 of the License, or
      11             :   (at your option) any later version.
      12             : 
      13             :   systemd is distributed in the hope that it will be useful, but
      14             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      16             :   Lesser General Public License for more details.
      17             : 
      18             :   You should have received a copy of the GNU Lesser General Public License
      19             :   along with systemd; If not, see <http://www.gnu.org/licenses/>.
      20             : ***/
      21             : 
      22             : #include <stdlib.h>
      23             : 
      24             : #include "util.h"
      25             : #include "macro.h"
      26             : 
      27             : #include "bus-label.h"
      28             : 
      29          87 : char *bus_label_escape(const char *s) {
      30             :         char *r, *t;
      31             :         const char *f;
      32             : 
      33          87 :         assert_return(s, NULL);
      34             : 
      35             :         /* Escapes all chars that D-Bus' object path cannot deal
      36             :          * with. Can be reversed with bus_path_unescape(). We special
      37             :          * case the empty string. */
      38             : 
      39          88 :         if (*s == 0)
      40           2 :                 return strdup("_");
      41             : 
      42          86 :         r = new(char, strlen(s)*3 + 1);
      43          86 :         if (!r)
      44           0 :                 return NULL;
      45             : 
      46        1262 :         for (f = s, t = r; *f; f++) {
      47             : 
      48             :                 /* Escape everything that is not a-zA-Z0-9. We also
      49             :                  * escape 0-9 if it's the first character */
      50             : 
      51        2350 :                 if (!(*f >= 'A' && *f <= 'Z') &&
      52        2186 :                     !(*f >= 'a' && *f <= 'z') &&
      53         162 :                     !(f > s && *f >= '0' && *f <= '9')) {
      54         158 :                         *(t++) = '_';
      55         158 :                         *(t++) = hexchar(*f >> 4);
      56         160 :                         *(t++) = hexchar(*f);
      57             :                 } else
      58        1018 :                         *(t++) = *f;
      59             :         }
      60             : 
      61          86 :         *t = 0;
      62             : 
      63          86 :         return r;
      64             : }
      65             : 
      66          95 : char *bus_label_unescape_n(const char *f, size_t l) {
      67             :         char *r, *t;
      68             :         size_t i;
      69             : 
      70          95 :         assert_return(f, NULL);
      71             : 
      72             :         /* Special case for the empty string */
      73          95 :         if (l == 1 && *f == '_')
      74           5 :                 return strdup("");
      75             : 
      76          90 :         r = new(char, l + 1);
      77          92 :         if (!r)
      78           0 :                 return NULL;
      79             : 
      80        1577 :         for (i = 0, t = r; i < l; ++i) {
      81        1486 :                 if (f[i] == '_') {
      82             :                         int a, b;
      83             : 
      84         333 :                         if (l - i < 3 ||
      85         331 :                             (a = unhexchar(f[i + 1])) < 0 ||
      86         165 :                             (b = unhexchar(f[i + 2])) < 0) {
      87             :                                 /* Invalid escape code, let's take it literal then */
      88           1 :                                 *(t++) = '_';
      89             :                         } else {
      90         165 :                                 *(t++) = (char) ((a << 4) | b);
      91         165 :                                 i += 2;
      92             :                         }
      93             :                 } else
      94        1319 :                         *(t++) = f[i];
      95             :         }
      96             : 
      97          91 :         *t = 0;
      98             : 
      99          91 :         return r;
     100             : }

Generated by: LCOV version 1.11