-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.c
85 lines (65 loc) · 1.69 KB
/
utils.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
//
// utils.c - Assorted utilities for networking plugin - some sourced from internet and possibly modified (public domain)
//
// Part of grblHAL
//
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "grbl/plugins.h"
#include "grbl/nuts_bolts.h"
#include "utils.h"
char *btoa (uint64_t bytes)
{
static char buf[16];
uint_fast8_t n = 0;
uint64_t size = bytes;
while(size > 1024) {
size >>= 10;
n++;
}
strcpy(buf, ftoa((float)bytes / (float)(1ULL << 10 * n), n ? 2 : 0));
if(n == 0) // remove trailing decimal point...
buf[strlen(buf) - 1] = '\0';
strcat(buf, n == 0 ? " B" : n == 1 ? " KB" : n == 2 ? " MB" : " GB");
return buf;
}
bool is_valid_port (uint16_t port)
{
return port > 0;
}
bool is_valid_hostname (const char *hostname)
{
bool ok = true;
char *s = (char *)hostname;
int c;
size_t len = 0;
while(ok) {
if((c = *s++) == '\0')
break;
len++;
ok = c == '-' || isdigit(c) || (isalpha(c) && c != ' ');
}
return ok && len >= HOSTNAME_LENGTH_MIN && len <= HOSTNAME_LENGTH_MAX;
}
bool is_valid_ssid (const char *ssid)
{
bool ok = true;
char *s = (char *)ssid;
int c;
size_t len = 0;
while(ok) {
if((c = *s++) == '\0')
break;
len++;
ok = isprint(c);
}
return ok && len >= SSID_LENGTH_MIN && len <= SSID_LENGTH_MAX;
}
bool is_valid_password (const char *password)
{
size_t len = strlen(password);
if(len == strlen(HIDDEN_PASSWORD) && memcmp(password, HIDDEN_PASSWORD, len) == 0)
len = PASSWORD_LENGTH_MAX + 1;
return len >= PASSWORD_LENGTH_MIN && len <= PASSWORD_LENGTH_MAX;
}