Coverage Report

Created: 2026-01-16 06:47

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
#include <config.h>
20
21
#include <sudo_compat.h>
22
#include <sudo_debug.h>
23
#include <sudo_util.h>
24
25
/*
26
 * Converts a two-byte hex string to decimal.
27
 * Returns a value 0-255 on success or -1 for invalid input.
28
 */
29
int
30
sudo_hexchar_v1(const char s[restrict static 2])
31
2.22k
{
32
2.22k
    unsigned char result[2];
33
2.22k
    unsigned int i;
34
2.22k
    debug_decl(sudo_hexchar, SUDO_DEBUG_UTIL);
35
36
4.26k
    for (i = 0; i < 2; i++) {
37
3.62k
  switch (s[i]) {
38
1.42k
  case '0':
39
1.42k
      result[i] = 0;
40
1.42k
      break;
41
191
  case '1':
42
191
      result[i] = 1;
43
191
      break;
44
4
  case '2':
45
4
      result[i] = 2;
46
4
      break;
47
1
  case '3':
48
1
      result[i] = 3;
49
1
      break;
50
126
  case '4':
51
126
      result[i] = 4;
52
126
      break;
53
36
  case '5':
54
36
      result[i] = 5;
55
36
      break;
56
22
  case '6':
57
22
      result[i] = 6;
58
22
      break;
59
4
  case '7':
60
4
      result[i] = 7;
61
4
      break;
62
2
  case '8':
63
2
      result[i] = 8;
64
2
      break;
65
3
  case '9':
66
3
      result[i] = 9;
67
3
      break;
68
74
  case 'A':
69
75
  case 'a':
70
75
      result[i] = 10;
71
75
      break;
72
0
  case 'B':
73
0
  case 'b':
74
0
      result[i] = 11;
75
0
      break;
76
0
  case 'C':
77
1
  case 'c':
78
1
      result[i] = 12;
79
1
      break;
80
2
  case 'D':
81
2
  case 'd':
82
2
      result[i] = 13;
83
2
      break;
84
126
  case 'E':
85
140
  case 'e':
86
140
      result[i] = 14;
87
140
      break;
88
5
  case 'F':
89
14
  case 'f':
90
14
      result[i] = 15;
91
14
      break;
92
1.58k
  default:
93
      /* Invalid input. */
94
1.58k
      debug_return_int(-1);
95
3.62k
  }
96
3.62k
    }
97
638
    debug_return_int((result[0] << 4) | result[1]);
98
638
}