LCOV - code coverage report
Current view: top level - basic - device-nodes.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 26 32 81.2 %
Date: 2019-08-22 15:41:25 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <stdio.h>
       5             : #include <string.h>
       6             : 
       7             : #include "device-nodes.h"
       8             : #include "utf8.h"
       9             : 
      10         129 : int whitelisted_char_for_devnode(char c, const char *white) {
      11             : 
      12         129 :         if ((c >= '0' && c <= '9') ||
      13          86 :             (c >= 'A' && c <= 'Z') ||
      14          71 :             (c >= 'a' && c <= 'z') ||
      15          15 :             strchr("#+-.:=@_", c) != NULL ||
      16           0 :             (white != NULL && strchr(white, c) != NULL))
      17         122 :                 return 1;
      18             : 
      19           7 :         return 0;
      20             : }
      21             : 
      22          10 : int encode_devnode_name(const char *str, char *str_enc, size_t len) {
      23             :         size_t i, j;
      24             : 
      25          10 :         if (!str || !str_enc)
      26           0 :                 return -EINVAL;
      27             : 
      28         144 :         for (i = 0, j = 0; str[i] != '\0'; i++) {
      29             :                 int seqlen;
      30             : 
      31         134 :                 seqlen = utf8_encoded_valid_unichar(str + i, (size_t) -1);
      32         134 :                 if (seqlen > 1) {
      33             : 
      34           4 :                         if (len-j < (size_t)seqlen)
      35           0 :                                 return -EINVAL;
      36             : 
      37           4 :                         memcpy(&str_enc[j], &str[i], seqlen);
      38           4 :                         j += seqlen;
      39           4 :                         i += (seqlen-1);
      40             : 
      41         130 :                 } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i], NULL)) {
      42             : 
      43           8 :                         if (len-j < 4)
      44           0 :                                 return -EINVAL;
      45             : 
      46           8 :                         sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
      47           8 :                         j += 4;
      48             : 
      49             :                 } else {
      50         122 :                         if (len-j < 1)
      51           0 :                                 return -EINVAL;
      52             : 
      53         122 :                         str_enc[j] = str[i];
      54         122 :                         j++;
      55             :                 }
      56             :         }
      57             : 
      58          10 :         if (len-j < 1)
      59           0 :                 return -EINVAL;
      60             : 
      61          10 :         str_enc[j] = '\0';
      62          10 :         return 0;
      63             : }

Generated by: LCOV version 1.14