Coverage Report

Created: 2025-02-26 06:46

/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
139k
{
37
139k
    unsigned char result[2];
38
139k
    unsigned int i;
39
139k
    debug_decl(sudo_hexchar, SUDO_DEBUG_UTIL);
40
41
393k
    for (i = 0; i < 2; i++) {
42
268k
  switch (s[i]) {
43
228k
  case '0':
44
228k
      result[i] = 0;
45
228k
      break;
46
9.40k
  case '1':
47
9.40k
      result[i] = 1;
48
9.40k
      break;
49
2.17k
  case '2':
50
2.17k
      result[i] = 2;
51
2.17k
      break;
52
18
  case '3':
53
18
      result[i] = 3;
54
18
      break;
55
531
  case '4':
56
531
      result[i] = 4;
57
531
      break;
58
4
  case '5':
59
4
      result[i] = 5;
60
4
      break;
61
10.1k
  case '6':
62
10.1k
      result[i] = 6;
63
10.1k
      break;
64
0
  case '7':
65
0
      result[i] = 7;
66
0
      break;
67
2
  case '8':
68
2
      result[i] = 8;
69
2
      break;
70
1
  case '9':
71
1
      result[i] = 9;
72
1
      break;
73
2
  case 'A':
74
192
  case 'a':
75
192
      result[i] = 10;
76
192
      break;
77
0
  case 'B':
78
25
  case 'b':
79
25
      result[i] = 11;
80
25
      break;
81
3
  case 'C':
82
27
  case 'c':
83
27
      result[i] = 12;
84
27
      break;
85
17
  case 'D':
86
20
  case 'd':
87
20
      result[i] = 13;
88
20
      break;
89
0
  case 'E':
90
19
  case 'e':
91
19
      result[i] = 14;
92
19
      break;
93
13
  case 'F':
94
3.65k
  case 'f':
95
3.65k
      result[i] = 15;
96
3.65k
      break;
97
14.3k
  default:
98
      /* Invalid input. */
99
14.3k
      debug_return_int(-1);
100
268k
  }
101
268k
    }
102
125k
    debug_return_int((result[0] << 4) | result[1]);
103
125k
}