LCOV - code coverage report
Current view: top level - basic - device-nodes.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 26 32 81.2 %
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 2008-2011 Kay Sievers
       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 <stdio.h>
      23             : 
      24             : #include "device-nodes.h"
      25             : #include "utf8.h"
      26             : 
      27         129 : int whitelisted_char_for_devnode(char c, const char *white) {
      28             : 
      29         129 :         if ((c >= '0' && c <= '9') ||
      30          71 :             (c >= 'A' && c <= 'Z') ||
      31          86 :             (c >= 'a' && c <= 'z') ||
      32          22 :             strchr("#+-.:=@_", c) != NULL ||
      33           0 :             (white != NULL && strchr(white, c) != NULL))
      34         122 :                 return 1;
      35             : 
      36           7 :         return 0;
      37             : }
      38             : 
      39          10 : int encode_devnode_name(const char *str, char *str_enc, size_t len) {
      40             :         size_t i, j;
      41             : 
      42          10 :         if (str == NULL || str_enc == NULL)
      43           0 :                 return -EINVAL;
      44             : 
      45         144 :         for (i = 0, j = 0; str[i] != '\0'; i++) {
      46             :                 int seqlen;
      47             : 
      48         134 :                 seqlen = utf8_encoded_valid_unichar(&str[i]);
      49         134 :                 if (seqlen > 1) {
      50             : 
      51           4 :                         if (len-j < (size_t)seqlen)
      52           0 :                                 return -EINVAL;
      53             : 
      54           4 :                         memcpy(&str_enc[j], &str[i], seqlen);
      55           4 :                         j += seqlen;
      56           4 :                         i += (seqlen-1);
      57             : 
      58         130 :                 } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i], NULL)) {
      59             : 
      60           8 :                         if (len-j < 4)
      61           0 :                                 return -EINVAL;
      62             : 
      63           8 :                         sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
      64           8 :                         j += 4;
      65             : 
      66             :                 } else {
      67         122 :                         if (len-j < 1)
      68           0 :                                 return -EINVAL;
      69             : 
      70         122 :                         str_enc[j] = str[i];
      71         122 :                         j++;
      72             :                 }
      73             :         }
      74             : 
      75          10 :         if (len-j < 1)
      76           0 :                 return -EINVAL;
      77             : 
      78          10 :         str_enc[j] = '\0';
      79          10 :         return 0;
      80             : }

Generated by: LCOV version 1.11