eulaurarien/accel.c
Geoffrey Frogeye 66ac52c5db
Workflow: JSON parser acceleration
Sadly is even worse because of the ctypes-induced conversions.
2019-12-09 10:42:37 +01:00

102 lines
3.3 KiB
C

#include <stdlib.h>
#include <stdio.h>
char ip4_flat(char* value, wchar_t* flat)
{
unsigned char value_index = 0;
unsigned char octet_index = 0;
unsigned char octet_value = 0;
char flat_index;
unsigned char value_chara;
do {
value_chara = value[value_index];
if (value_chara >= '0' && value_chara <= '9') {
octet_value *= 10;
octet_value += value_chara - '0';
} else if (value_chara == '.') {
for (flat_index = (octet_index+1)*8-1; flat_index >= octet_index*8; flat_index--) {
flat[flat_index] = '0' + (octet_value & 1);
octet_value >>= 1;
}
octet_index++;
octet_value = 0;
} else if (value_chara == '\0') {
if (octet_index != 3) {
return 1;
}
for (flat_index = 31; flat_index >= 24; flat_index--) {
flat[flat_index] = '0' + (octet_value & 1);
octet_value >>= 1;
}
return 0;
} else {
return 1;
}
value_index++;
} while (1); // This ugly thing save one comparison
return 1;
}
#define MAX_OUTPUT 255
char feed_dns_parse_json(char* line, char* name, char* value)
{
unsigned short line_index = 0;
unsigned char quote_index = 0;
unsigned char output_index = 0;
char line_chara;
char* current_output = NULL;
char type = 0; // 0: error, 1: cname, 2: a, 3: aaaa
do {
line_chara = line[line_index];
if (line_chara == '"') {
quote_index += 1;
switch (quote_index) {
case 7: // Start of name
current_output = name;
break;
case 8: // End of name
name[output_index] = '\0';
current_output = NULL;
break;
case 11: // Start of type
line_chara = line[++line_index];
if (line_chara == 'c') { // Must be CNAME
type = 1;
break;
} else if (line_chara == 'a') { // A or AAAA
line_chara = line[++line_index];
if (line[line_chara+2] == '"') { // Is A
type = 2;
quote_index++;
break;
} else if (line[line_chara+1] == 'a') { // Must be AAAA
type = 3;
break;
}
}
return 0;
case 15: // Start of value
current_output = value;
break;
case 16: // End of value
value[output_index] = '\0';
return type;
}
output_index = 0;
} else if (line_chara == '\0') {
return 0;
} else {
if (current_output != 0) {
if (output_index >= MAX_OUTPUT) {
return 0;
}
current_output[output_index] = line_chara;
output_index++;
}
}
line_index++;
} while (1); // This ugly thing save one comparison
return 0;
}