housekeeping 2

This commit is contained in:
viandoxdev 2022-08-31 18:59:06 +02:00
parent e11c8b5c48
commit 2bd21ee138
No known key found for this signature in database
GPG Key ID: AF1410C5BC10AA25
8 changed files with 202 additions and 196 deletions

View File

@ -6,12 +6,14 @@
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <math.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

1
hid.c
View File

@ -4,6 +4,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <pthread.h>
#include <stdbool.h>

2
hid.h
View File

@ -3,7 +3,7 @@
#define HID_H_
#include "net.h"
#include <linux/input.h>
#include <linux/input-event-codes.h>
#include <stdint.h>
typedef uint64_t uniq_t;

107
json.c
View File

@ -4,21 +4,16 @@
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static JSONError jerrno = NoError;
static size_t jerr_index = 0;
const char * json_strerr() {
return JSONErrorMessage[jerrno];
}
const char *json_strerr() { return JSONErrorMessage[jerrno]; }
size_t json_err_loc() {
return jerr_index;
}
size_t json_err_loc() { return jerr_index; }
// Shorthand to set jerno and return -1;
static inline int set_jerrno(JSONError err) {
@ -279,14 +274,16 @@ static int json_parse_boolean(const char **buf, const char *buf_end, uint8_t **r
header->len = 8;
if (**buf == 't') {
if(*buf + 4 > buf_end) return set_jerrno(SrcOverflow);
if (*buf + 4 > buf_end)
return set_jerrno(SrcOverflow);
if (strncmp(*buf, "true", 4) != 0) {
return set_jerrno(BadKeyword);
}
*buf += 4;
*value = 1;
} else if (**buf == 'f') {
if(*buf + 5 > buf_end) return set_jerrno(SrcOverflow);
if (*buf + 5 > buf_end)
return set_jerrno(SrcOverflow);
if (strncmp(*buf, "false", 5) != 0) {
return set_jerrno(BadKeyword);
}
@ -312,7 +309,8 @@ static int json_parse_null(const char **buf, const char *buf_end, uint8_t **rest
header->type = (uint32_t)Null;
header->len = 0;
if(*buf + 4 > buf_end) return set_jerrno(SrcOverflow);
if (*buf + 4 > buf_end)
return set_jerrno(SrcOverflow);
if (strncmp(*buf, "null", 4) != 0) {
return set_jerrno(BadKeyword);
}
@ -336,16 +334,21 @@ static int json_parse_array(const char **buf, const char *buf_end, uint8_t **res
(*buf)++; // Skip [
// skip initial whitespace
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
if(*buf == buf_end) return set_jerrno(SrcOverflow);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == ']') {
header->len = 0;
return 0;
}
while (1) {
if (json_parse_value(buf, buf_end, dst, dst_end) != 0) return -1;
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
if (*buf == buf_end) return set_jerrno(SrcOverflow);
if (json_parse_value(buf, buf_end, dst, dst_end) != 0)
return -1;
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == ',') {
(*buf)++;
} else if (**buf == ']') {
@ -375,8 +378,10 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
(*buf)++; // Skip {
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
if(*buf == buf_end) return set_jerrno(SrcOverflow);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == '}') {
header->len = 0;
return 0;
@ -384,23 +389,31 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
while (1) {
// Skip whitespace before key
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
// Parse key
if (json_parse_string(buf, buf_end, dst, dst_end) != 0) return -1;
if (json_parse_string(buf, buf_end, dst, dst_end) != 0)
return -1;
// Skip whitespace after key
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
// There should be at least one char
if(*buf == buf_end) return set_jerrno(SrcOverflow);
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
// There should be a colon
if(**buf != ':') return set_jerrno(ObjectBadChar);
if (**buf != ':')
return set_jerrno(ObjectBadChar);
// Skip colon
(*buf)++;
// Parse value (takes char of whitespace)
if (json_parse_value(buf, buf_end, dst, dst_end) != 0) return -1;
if (json_parse_value(buf, buf_end, dst, dst_end) != 0)
return -1;
// Skip whitespace after value
for(; *buf < buf_end && is_whitespace(**buf); (*buf)++);
for (; *buf < buf_end && is_whitespace(**buf); (*buf)++)
;
// There should be at least one char (} or ,)
if (*buf == buf_end) return set_jerrno(SrcOverflow);
if (*buf == buf_end)
return set_jerrno(SrcOverflow);
if (**buf == ',') {
(*buf)++;
} else if (**buf == '}') {
@ -418,7 +431,8 @@ static int json_parse_object(const char **buf, const char *buf_end, uint8_t **re
static int json_parse_value(const char **buf, const char *buf_end, uint8_t **restrict dst,
const uint8_t *dst_end) {
for (; *buf < buf_end; (*buf)++) {
if(is_whitespace(**buf)) continue;
if (is_whitespace(**buf))
continue;
switch (**buf) {
case '"':
@ -475,8 +489,7 @@ void json_print_value(uint8_t ** buf) {
printf("%lf", *(double *)*buf);
*buf += sizeof(double);
break;
case Boolean:
{
case Boolean: {
uint64_t value = *(uint64_t *)*buf;
if (value == 1) {
printf("true");
@ -486,13 +499,11 @@ void json_print_value(uint8_t ** buf) {
printf("(boolean) garbage");
}
*buf += 8;
}
break;
} break;
case Null:
printf("null");
break;
case Array:
{
case Array: {
uint8_t *end = *buf + header->len;
printf("[");
while (1) {
@ -504,10 +515,8 @@ void json_print_value(uint8_t ** buf) {
break;
}
}
}
break;
case Object:
{
} break;
case Object: {
uint8_t *end = *buf + header->len;
printf("{");
while (1) {
@ -521,8 +530,7 @@ void json_print_value(uint8_t ** buf) {
break;
}
}
}
break;
} break;
}
}
@ -542,14 +550,12 @@ static void json_adapt_set(uint8_t * buf, JSONAdapter * adapters, size_t adapter
if (strcmp(path, adapters[i].path) == 0 && header->type == adapters[i].type) {
void *p = ptr + adapters[i].offset;
switch (header->type) {
case String:
{
case String: {
char *v = malloc(header->len + 1);
strncpy(v, (char *)(buf + sizeof(JSONHeader)), header->len);
v[header->len] = '\0';
*(char **)p = v;
}
break;
} break;
case Number:
*(double *)p = *(double *)(buf + sizeof(JSONHeader));
break;
@ -561,7 +567,8 @@ static void json_adapt_set(uint8_t * buf, JSONAdapter * adapters, size_t adapter
}
}
static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapter_count, void * ptr, char * full_path, char * path) {
static void json_adapt_priv(uint8_t **buf, JSONAdapter *adapters, size_t adapter_count, void *ptr,
char *full_path, char *path) {
JSONHeader *header = (JSONHeader *)*buf;
switch (header->type) {
@ -580,18 +587,15 @@ static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapt
case Null:
*buf += sizeof(JSONHeader);
break;
case Array:
{
case Array: {
*buf += sizeof(JSONHeader);
uint8_t *end = *buf + header->len;
for (size_t index = 0; *buf < end; index++) {
int len = sprintf(path, ".%lu", index);
json_adapt_priv(buf, adapters, adapter_count, ptr, full_path, path + len);
}
}
break;
case Object:
{
} break;
case Object: {
*buf += sizeof(JSONHeader);
uint8_t *end = *buf + header->len;
while (*buf < end) {
@ -603,8 +607,7 @@ static void json_adapt_priv(uint8_t ** buf, JSONAdapter * adapters, size_t adapt
json_adapt_priv(buf, adapters, adapter_count, ptr, full_path, path + len);
}
}
break;
} break;
}
}

5
json.h
View File

@ -1,10 +1,9 @@
// vi:ft=c
#ifndef JSON_H_
#define JSON_H_
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
#include <stddef.h>
#include <stdint.h>
typedef struct __attribute__((packed, aligned(8))) {
uint32_t type;
@ -50,7 +49,7 @@ static const char * JSONErrorMessage[JERRORNO_MAX + 1] = {
"Illegal escape in string",
"Unexpected character in number",
"Unexpected character in object",
"?"
"?",
};
#endif

2
net.h
View File

@ -1,7 +1,7 @@
// vi:ft=c
#ifndef NET_H_
#define NET_H_
#include <linux/input.h>
#include <linux/input-event-codes.h>
#include <stdint.h>
#include <stdlib.h>

View File

@ -2,6 +2,7 @@
#include "net.h"
#include "util.h"
#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <netinet/in.h>
#include <netinet/tcp.h>