-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strutils.c
98 lines (82 loc) · 2.3 KB
/
strutils.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* String utils */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "strutils.h"
/* ------------------------------------------------------------------------ */
/* Decompose a URL into its components. */
void split_url(struct url_info *ui, char *url) {
char *p = url;
char *q;
memset(ui, 0, sizeof *ui);
q = strstr(p, "://");
ui->scheme = p;
*q = '\0';
p = q+3;
for (int i = 0; ui->scheme[i]; i++) {
ui->scheme[i] = tolower(ui->scheme[i]);
}
q = strchr(p, '/');
if (q) {
*q = '/';
ui->path = q;
q = strchr(q, '#');
if (q)
*q = '\0';
} else {
ui->path = "/";
}
ui->hostn = p;
q = strchr(p, ':');
if (q) {
*q = '\0';
ui->port = atoi(q+1);
}
if (ui->port == 0) {
if (strcmp(ui->scheme,"http") == 0) {
ui->port = 80;
}
if (strcmp(ui->scheme,"https") == 0) {
ui->port = 443;
}
}
}
/* ------------------------------------------------------------------------ */
// convert a hex string such as "A489B1" into an array like [0xA4, 0x89, 0xB1].
uint8_t nibble(char c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0; // Not a valid hexadecimal character
}
void hexToBytes(uint8_t *byteArray, const char *hexString) {
uint8_t oddLength = strlen(hexString) & 1;
uint8_t currentByte = 0;
uint8_t byteIndex = 0;
for (uint8_t charIndex = 0; charIndex < strlen(hexString); charIndex++) {
uint8_t oddCharIndex = charIndex & 1;
if (oddLength) {
if (oddCharIndex) {
currentByte = nibble(hexString[charIndex]) << 4;
} else {
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
} else {
if (!oddCharIndex) {
currentByte = nibble(hexString[charIndex]) << 4;
} else {
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
}
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */