Coverage Report

Created: 2025-09-17 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/lib/util/hexchar.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2013-2015, 2023 Todd C. Miller <Todd.Miller@sudo.ws>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
/*
20
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22
 */
23
24
#include <config.h>
25
26
#include <sudo_compat.h>
27
#include <sudo_debug.h>
28
#include <sudo_util.h>
29
30
/*
31
 * Converts a two-byte hex string to decimal.
32
 * Returns a value 0-255 on success or -1 for invalid input.
33
 */
34
int
35
sudo_hexchar_v1(const char s[restrict static 2])
36
858
{
37
858
    unsigned char result[2];
38
858
    unsigned int i;
39
858
    debug_decl(sudo_hexchar, SUDO_DEBUG_UTIL);
40
41
1.45k
    for (i = 0; i < 2; i++) {
42
1.25k
  switch (s[i]) {
43
291
  case '0':
44
291
      result[i] = 0;
45
291
      break;
46
38
  case '1':
47
38
      result[i] = 1;
48
38
      break;
49
3
  case '2':
50
3
      result[i] = 2;
51
3
      break;
52
5
  case '3':
53
5
      result[i] = 3;
54
5
      break;
55
125
  case '4':
56
125
      result[i] = 4;
57
125
      break;
58
1
  case '5':
59
1
      result[i] = 5;
60
1
      break;
61
15
  case '6':
62
15
      result[i] = 6;
63
15
      break;
64
2
  case '7':
65
2
      result[i] = 7;
66
2
      break;
67
6
  case '8':
68
6
      result[i] = 8;
69
6
      break;
70
0
  case '9':
71
0
      result[i] = 9;
72
0
      break;
73
25
  case 'A':
74
28
  case 'a':
75
28
      result[i] = 10;
76
28
      break;
77
1
  case 'B':
78
5
  case 'b':
79
5
      result[i] = 11;
80
5
      break;
81
0
  case 'C':
82
2
  case 'c':
83
2
      result[i] = 12;
84
2
      break;
85
0
  case 'D':
86
0
  case 'd':
87
0
      result[i] = 13;
88
0
      break;
89
0
  case 'E':
90
10
  case 'e':
91
10
      result[i] = 14;
92
10
      break;
93
5
  case 'F':
94
68
  case 'f':
95
68
      result[i] = 15;
96
68
      break;
97
656
  default:
98
      /* Invalid input. */
99
656
      debug_return_int(-1);
100
1.25k
  }
101
1.25k
    }
102
202
    debug_return_int((result[0] << 4) | result[1]);
103
202
}