Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/hexchar.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2013-2015, 2023 Todd C. Miller <[email protected]>
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
{
32
unsigned char result[2];
33
unsigned int i;
34
debug_decl(sudo_hexchar, SUDO_DEBUG_UTIL);
35
36
for (i = 0; i < 2; i++) {
37
switch (s[i]) {
38
case '0':
39
result[i] = 0;
40
break;
41
case '1':
42
result[i] = 1;
43
break;
44
case '2':
45
result[i] = 2;
46
break;
47
case '3':
48
result[i] = 3;
49
break;
50
case '4':
51
result[i] = 4;
52
break;
53
case '5':
54
result[i] = 5;
55
break;
56
case '6':
57
result[i] = 6;
58
break;
59
case '7':
60
result[i] = 7;
61
break;
62
case '8':
63
result[i] = 8;
64
break;
65
case '9':
66
result[i] = 9;
67
break;
68
case 'A':
69
case 'a':
70
result[i] = 10;
71
break;
72
case 'B':
73
case 'b':
74
result[i] = 11;
75
break;
76
case 'C':
77
case 'c':
78
result[i] = 12;
79
break;
80
case 'D':
81
case 'd':
82
result[i] = 13;
83
break;
84
case 'E':
85
case 'e':
86
result[i] = 14;
87
break;
88
case 'F':
89
case 'f':
90
result[i] = 15;
91
break;
92
default:
93
/* Invalid input. */
94
debug_return_int(-1);
95
}
96
}
97
debug_return_int((result[0] << 4) | result[1]);
98
}
99
100