summaryrefslogtreecommitdiff
path: root/utils.c
blob: fa970874552145a4169a3252457967e0ed2d76cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdint.h>

#include "types.h"

uint64_t str_to_unsigned(struct string str, uint8_t *err) {
	if (str.len == 0) {
		*err = 1;
		return 0;
	}

	uint64_t val = 0;
	while (str.len > 0) {
		switch(str.data[0]) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			val *= 10;
			val += str.data[0] - 0x30;
			break;
		default:
			*err = 1;
			return 0;
		}

		str.data++;
		str.len--;
	}

	*err = 0;
	return val;
}