Coverage Report

Created: 2025-08-26 06:57

/src/sudo/lib/util/hexchar.c
Line
Count
Source (jump to first uncovered line)
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
78.1k
{
37
78.1k
    unsigned char result[2];
38
78.1k
    unsigned int i;
39
78.1k
    debug_decl(sudo_hexchar, SUDO_DEBUG_UTIL);
40
41
218k
    for (i = 0; i < 2; i++) {
42
150k
  switch (s[i]) {
43
124k
  case '0':
44
124k
      result[i] = 0;
45
124k
      break;
46
5.26k
  case '1':
47
5.26k
      result[i] = 1;
48
5.26k
      break;
49
1.19k
  case '2':
50
1.19k
      result[i] = 2;
51
1.19k
      break;
52
274
  case '3':
53
274
      result[i] = 3;
54
274
      break;
55
126
  case '4':
56
126
      result[i] = 4;
57
126
      break;
58
3
  case '5':
59
3
      result[i] = 5;
60
3
      break;
61
5.59k
  case '6':
62
5.59k
      result[i] = 6;
63
5.59k
      break;
64
3
  case '7':
65
3
      result[i] = 7;
66
3
      break;
67
4
  case '8':
68
4
      result[i] = 8;
69
4
      break;
70
3
  case '9':
71
3
      result[i] = 9;
72
3
      break;
73
19
  case 'A':
74
19
  case 'a':
75
19
      result[i] = 10;
76
19
      break;
77
1
  case 'B':
78
1
  case 'b':
79
1
      result[i] = 11;
80
1
      break;
81
0
  case 'C':
82
1
  case 'c':
83
1
      result[i] = 12;
84
1
      break;
85
8
  case 'D':
86
45
  case 'd':
87
45
      result[i] = 13;
88
45
      break;
89
0
  case 'E':
90
1.20k
  case 'e':
91
1.20k
      result[i] = 14;
92
1.20k
      break;
93
0
  case 'F':
94
1.89k
  case 'f':
95
1.89k
      result[i] = 15;
96
1.89k
      break;
97
9.59k
  default:
98
      /* Invalid input. */
99
9.59k
      debug_return_int(-1);
100
150k
  }
101
150k
    }
102
68.5k
    debug_return_int((result[0] << 4) | result[1]);
103
68.5k
}