Coverage Report

Created: 2026-12-01 07:07

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
36.1k
{
32
36.1k
    unsigned char result[2];
33
36.1k
    unsigned int i;
34
36.1k
    debug_decl(sudo_hexchar, SUDO_DEBUG_UTIL);
35
36
65.5k
    for (i = 0; i < 2; i++) {
37
51.1k
  switch (s[i]) {
38
8.14k
  case '0':
39
8.14k
      result[i] = 0;
40
8.14k
      break;
41
991
  case '1':
42
991
      result[i] = 1;
43
991
      break;
44
1.68k
  case '2':
45
1.68k
      result[i] = 2;
46
1.68k
      break;
47
686
  case '3':
48
686
      result[i] = 3;
49
686
      break;
50
844
  case '4':
51
844
      result[i] = 4;
52
844
      break;
53
523
  case '5':
54
523
      result[i] = 5;
55
523
      break;
56
644
  case '6':
57
644
      result[i] = 6;
58
644
      break;
59
625
  case '7':
60
625
      result[i] = 7;
61
625
      break;
62
348
  case '8':
63
348
      result[i] = 8;
64
348
      break;
65
539
  case '9':
66
539
      result[i] = 9;
67
539
      break;
68
19
  case 'A':
69
11.0k
  case 'a':
70
11.0k
      result[i] = 10;
71
11.0k
      break;
72
0
  case 'B':
73
305
  case 'b':
74
305
      result[i] = 11;
75
305
      break;
76
254
  case 'C':
77
739
  case 'c':
78
739
      result[i] = 12;
79
739
      break;
80
19
  case 'D':
81
30
  case 'd':
82
30
      result[i] = 13;
83
30
      break;
84
237
  case 'E':
85
1.46k
  case 'e':
86
1.46k
      result[i] = 14;
87
1.46k
      break;
88
73
  case 'F':
89
697
  case 'f':
90
697
      result[i] = 15;
91
697
      break;
92
21.7k
  default:
93
      /* Invalid input. */
94
21.7k
      debug_return_int(-1);
95
51.1k
  }
96
51.1k
    }
97
14.4k
    debug_return_int((result[0] << 4) | result[1]);
98
14.4k
}