cryptutils

Cryptographic Software Utilities
git clone http://git.omkov.net/cryptutils
Log | Tree | Refs | README | LICENCE | Download

cryptutils/src/util/strtou.c (138 lines, 3.7 KiB) -rw-r--r-- file download

03b01d2 Jamozed 2022-03-06 17:10:01
0
// util/strtou.c
c01a723 Jamozed 2022-02-05 19:04:03
1
// String conversion source file from libutil
0737d9e Jamozed 2021-02-21 23:43:21
2
// Copyright (C) 2021, Jakob Wakeling
03b01d2 Jamozed 2022-03-06 17:10:01
3
// MIT Licence
0737d9e Jamozed 2021-02-21 23:43:21
4
c01a723 Jamozed 2022-02-05 19:04:03
5
#include "util.h"
c01a723 Jamozed 2022-02-05 19:04:03
6
0737d9e Jamozed 2021-02-21 23:43:21
7
#include <ctype.h>
0737d9e Jamozed 2021-02-21 23:43:21
8
#include <errno.h>
0737d9e Jamozed 2021-02-21 23:43:21
9
0737d9e Jamozed 2021-02-21 23:43:21
10
/* Convert a string to an unsigned 8-bit integer */
c01a723 Jamozed 2022-02-05 19:04:03
11
u8 strtou8(const char *nptr, char **endptr, register int base) {
c01a723 Jamozed 2022-02-05 19:04:03
12
	register const char *s = nptr; register u8 i = 0, c;
0737d9e Jamozed 2021-02-21 23:43:21
13
	
0737d9e Jamozed 2021-02-21 23:43:21
14
	for (; isspace(*s); ++s);
0737d9e Jamozed 2021-02-21 23:43:21
15
	
0737d9e Jamozed 2021-02-21 23:43:21
16
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
17
	
0737d9e Jamozed 2021-02-21 23:43:21
18
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
0737d9e Jamozed 2021-02-21 23:43:21
19
		s += 2; base = 16;
0737d9e Jamozed 2021-02-21 23:43:21
20
	}
0737d9e Jamozed 2021-02-21 23:43:21
21
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
0737d9e Jamozed 2021-02-21 23:43:21
22
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
23
	
0737d9e Jamozed 2021-02-21 23:43:21
24
	for (;; ++s) {
0737d9e Jamozed 2021-02-21 23:43:21
25
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
0737d9e Jamozed 2021-02-21 23:43:21
26
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
27
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
28
		else { break; }
0737d9e Jamozed 2021-02-21 23:43:21
29
		
0737d9e Jamozed 2021-02-21 23:43:21
30
		if (c >= base) { break; }
c01a723 Jamozed 2022-02-05 19:04:03
31
		if (i > (U8_MAX - c) / base) {
c01a723 Jamozed 2022-02-05 19:04:03
32
			errno = ERANGE; i = U8_MAX; goto end;
0737d9e Jamozed 2021-02-21 23:43:21
33
		}
0737d9e Jamozed 2021-02-21 23:43:21
34
		
0737d9e Jamozed 2021-02-21 23:43:21
35
		i = i * base + c;
0737d9e Jamozed 2021-02-21 23:43:21
36
	}
0737d9e Jamozed 2021-02-21 23:43:21
37
	
0737d9e Jamozed 2021-02-21 23:43:21
38
end:;
0737d9e Jamozed 2021-02-21 23:43:21
39
	if (endptr) { *endptr = (char *)s; } return i;
0737d9e Jamozed 2021-02-21 23:43:21
40
}
0737d9e Jamozed 2021-02-21 23:43:21
41
0737d9e Jamozed 2021-02-21 23:43:21
42
/* Convert a string to an unsigned 16-bit integer */
c01a723 Jamozed 2022-02-05 19:04:03
43
u16 strtou16(const char *nptr, char **endptr, register int base) {
c01a723 Jamozed 2022-02-05 19:04:03
44
	register const char *s = nptr; register u16 i = 0, c;
0737d9e Jamozed 2021-02-21 23:43:21
45
	
0737d9e Jamozed 2021-02-21 23:43:21
46
	for (; isspace(*s); ++s);
0737d9e Jamozed 2021-02-21 23:43:21
47
	
0737d9e Jamozed 2021-02-21 23:43:21
48
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
49
	
0737d9e Jamozed 2021-02-21 23:43:21
50
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
0737d9e Jamozed 2021-02-21 23:43:21
51
		s += 2; base = 16;
0737d9e Jamozed 2021-02-21 23:43:21
52
	}
0737d9e Jamozed 2021-02-21 23:43:21
53
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
0737d9e Jamozed 2021-02-21 23:43:21
54
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
55
	
0737d9e Jamozed 2021-02-21 23:43:21
56
	for (;; ++s) {
0737d9e Jamozed 2021-02-21 23:43:21
57
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
0737d9e Jamozed 2021-02-21 23:43:21
58
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
59
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
60
		else { break; }
0737d9e Jamozed 2021-02-21 23:43:21
61
		
0737d9e Jamozed 2021-02-21 23:43:21
62
		if (c >= base) { break; }
c01a723 Jamozed 2022-02-05 19:04:03
63
		if (i > (U16_MAX - c) / base) {
c01a723 Jamozed 2022-02-05 19:04:03
64
			errno = ERANGE; i = U16_MAX; goto end;
0737d9e Jamozed 2021-02-21 23:43:21
65
		}
0737d9e Jamozed 2021-02-21 23:43:21
66
		
0737d9e Jamozed 2021-02-21 23:43:21
67
		i = i * base + c;
0737d9e Jamozed 2021-02-21 23:43:21
68
	}
0737d9e Jamozed 2021-02-21 23:43:21
69
	
0737d9e Jamozed 2021-02-21 23:43:21
70
end:;
0737d9e Jamozed 2021-02-21 23:43:21
71
	if (endptr) { *endptr = (char *)s; } return i;
0737d9e Jamozed 2021-02-21 23:43:21
72
}
0737d9e Jamozed 2021-02-21 23:43:21
73
0737d9e Jamozed 2021-02-21 23:43:21
74
/* Convert a string to an unsigned 32-bit integer */
c01a723 Jamozed 2022-02-05 19:04:03
75
u32 strtou32(const char *nptr, char **endptr, register int base) {
c01a723 Jamozed 2022-02-05 19:04:03
76
	register const char *s = nptr; register u32 i = 0, c;
0737d9e Jamozed 2021-02-21 23:43:21
77
	
0737d9e Jamozed 2021-02-21 23:43:21
78
	for (; isspace(*s); ++s);
0737d9e Jamozed 2021-02-21 23:43:21
79
	
0737d9e Jamozed 2021-02-21 23:43:21
80
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
81
	
0737d9e Jamozed 2021-02-21 23:43:21
82
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
0737d9e Jamozed 2021-02-21 23:43:21
83
		s += 2; base = 16;
0737d9e Jamozed 2021-02-21 23:43:21
84
	}
0737d9e Jamozed 2021-02-21 23:43:21
85
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
0737d9e Jamozed 2021-02-21 23:43:21
86
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
87
	
0737d9e Jamozed 2021-02-21 23:43:21
88
	for (;; ++s) {
0737d9e Jamozed 2021-02-21 23:43:21
89
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
0737d9e Jamozed 2021-02-21 23:43:21
90
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
91
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
92
		else { break; }
0737d9e Jamozed 2021-02-21 23:43:21
93
		
0737d9e Jamozed 2021-02-21 23:43:21
94
		if (c >= base) { break; }
c01a723 Jamozed 2022-02-05 19:04:03
95
		if (i > (U32_MAX - c) / base) {
c01a723 Jamozed 2022-02-05 19:04:03
96
			errno = ERANGE; i = U32_MAX; goto end;
0737d9e Jamozed 2021-02-21 23:43:21
97
		}
0737d9e Jamozed 2021-02-21 23:43:21
98
		
0737d9e Jamozed 2021-02-21 23:43:21
99
		i = i * base + c;
0737d9e Jamozed 2021-02-21 23:43:21
100
	}
0737d9e Jamozed 2021-02-21 23:43:21
101
	
0737d9e Jamozed 2021-02-21 23:43:21
102
end:;
0737d9e Jamozed 2021-02-21 23:43:21
103
	if (endptr) { *endptr = (char *)s; } return i;
0737d9e Jamozed 2021-02-21 23:43:21
104
}
0737d9e Jamozed 2021-02-21 23:43:21
105
0737d9e Jamozed 2021-02-21 23:43:21
106
/* Convert a string to an unsigned 64-bit integer */
c01a723 Jamozed 2022-02-05 19:04:03
107
u64 strtou64(const char *nptr, char **endptr, register int base) {
c01a723 Jamozed 2022-02-05 19:04:03
108
	register const char *s = nptr; register u64 i = 0, c;
0737d9e Jamozed 2021-02-21 23:43:21
109
	
0737d9e Jamozed 2021-02-21 23:43:21
110
	for (; isspace(*s); ++s);
0737d9e Jamozed 2021-02-21 23:43:21
111
	
0737d9e Jamozed 2021-02-21 23:43:21
112
	if (*s == '+') { ++s; } else if (*s == '-') { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
113
	
0737d9e Jamozed 2021-02-21 23:43:21
114
	if ((!base || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
0737d9e Jamozed 2021-02-21 23:43:21
115
		s += 2; base = 16;
0737d9e Jamozed 2021-02-21 23:43:21
116
	}
0737d9e Jamozed 2021-02-21 23:43:21
117
	else if (base == 0) { base = s[0] == '0' ? 8 : 10; }
0737d9e Jamozed 2021-02-21 23:43:21
118
	else if (base < 2 || base > 36) { errno = EINVAL; goto end; }
0737d9e Jamozed 2021-02-21 23:43:21
119
	
0737d9e Jamozed 2021-02-21 23:43:21
120
	for (;; ++s) {
0737d9e Jamozed 2021-02-21 23:43:21
121
		if (*s >= '0' && *s <= '9') { c = *s - '0'; }
0737d9e Jamozed 2021-02-21 23:43:21
122
		else if (*s >= 'A' && *s <= 'Z') { c = *s - ('A' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
123
		else if (*s >= 'a' && *s <= 'z') { c = *s - ('a' - 10); }
0737d9e Jamozed 2021-02-21 23:43:21
124
		else { break; }
0737d9e Jamozed 2021-02-21 23:43:21
125
		
0737d9e Jamozed 2021-02-21 23:43:21
126
		if (c >= base) { break; }
c01a723 Jamozed 2022-02-05 19:04:03
127
		if (i > (U64_MAX - c) / base) {
c01a723 Jamozed 2022-02-05 19:04:03
128
			errno = ERANGE; i = U64_MAX; goto end;
0737d9e Jamozed 2021-02-21 23:43:21
129
		}
0737d9e Jamozed 2021-02-21 23:43:21
130
		
0737d9e Jamozed 2021-02-21 23:43:21
131
		i = i * base + c;
0737d9e Jamozed 2021-02-21 23:43:21
132
	}
0737d9e Jamozed 2021-02-21 23:43:21
133
	
0737d9e Jamozed 2021-02-21 23:43:21
134
end:;
0737d9e Jamozed 2021-02-21 23:43:21
135
	if (endptr) { *endptr = (char *)s; } return i;
0737d9e Jamozed 2021-02-21 23:43:21
136
}
137