GONEC_ARTERY_HttpServer/Src/httpd_base_func.c

404 lines
12 KiB
C

//
// Created by cfif on 12.12.22.
//
#include "string.h"
#include "inttypes.h"
//#include "stream.h"
#include "httpd_base_func.h"
#include "stdio.h"
#include "AsciiStringParsingUtils.h"
int extract_mac_ex_parameters(char *params, char *param_names[], char *param_vals[], int params_max) {
char *pair;
char *equals;
int loop;
/* If we have no parameters at all, return immediately. */
if (!params || (params[0] == '\0')) {
return (0);
}
/* Get a pointer to our first parameter */
pair = params;
/* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
* remainder (if any) */
for (loop = 0; (loop < params_max) && pair; loop++) {
/* Save the name of the parameter */
param_names[loop] = pair;
/* Remember the start of this name=value pair */
equals = pair;
/* Find the start of the next name=value pair and replace the delimiter
* with a 0 to terminate the previous pair string. */
pair = strchr(pair, ':');
if (pair) {
*pair = '\0';
pair++;
} else {
/* We didn't find a new parameter so find the end of the URI and
* replace the space with a '\0' */
pair = strchr(equals, ' ');
if (pair) {
*pair = '\0';
}
/* Revert to NULL so that we exit the loop as expected. */
pair = NULL;
}
/* Now find the '=' in the previous pair, replace it with '\0' and save
* the parameter value string. */
equals = strchr(equals, '.');
if (equals) {
*equals = '\0';
param_vals[loop] = equals + 1;
} else {
param_vals[loop] = NULL;
}
}
return loop;
}
int extract_ip_ex_parameters(char *params, char *param_names[], char *param_vals[], int params_max) {
char *pair;
char *equals;
int loop;
/* If we have no parameters at all, return immediately. */
if (!params || (params[0] == '\0')) {
return (0);
}
/* Get a pointer to our first parameter */
pair = params;
/* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
* remainder (if any) */
for (loop = 0; (loop < params_max) && pair; loop++) {
/* Save the name of the parameter */
param_names[loop] = pair;
/* Remember the start of this name=value pair */
equals = pair;
/* Find the start of the next name=value pair and replace the delimiter
* with a 0 to terminate the previous pair string. */
pair = strchr(pair, '.');
if (pair) {
*pair = '\0';
pair++;
} else {
/* We didn't find a new parameter so find the end of the URI and
* replace the space with a '\0' */
pair = strchr(equals, ' ');
if (pair) {
*pair = '\0';
}
/* Revert to NULL so that we exit the loop as expected. */
pair = NULL;
}
/* Now find the '=' in the previous pair, replace it with '\0' and save
* the parameter value string. */
equals = strchr(equals, '.');
if (equals) {
*equals = '\0';
param_vals[loop] = equals + 1;
} else {
param_vals[loop] = NULL;
}
}
return loop;
}
int extract_grp_ex_parameters(char *params, char *param_names[], char *param_vals[], int params_max) {
char *pair;
char *equals;
int loop;
/* If we have no parameters at all, return immediately. */
if (!params || (params[0] == '\0')) {
return (0);
}
/* Get a pointer to our first parameter */
pair = params;
/* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
* remainder (if any) */
for (loop = 0; (loop < params_max) && pair; loop++) {
/* Save the name of the parameter */
param_names[loop] = pair;
/* Remember the start of this name=value pair */
equals = pair;
/* Find the start of the next name=value pair and replace the delimiter
* with a 0 to terminate the previous pair string. */
pair = strchr(pair, ',');
if (pair) {
*pair = '\0';
pair++;
} else {
/* We didn't find a new parameter so find the end of the URI and
* replace the space with a '\0' */
pair = strchr(equals, ' ');
if (pair) {
*pair = '\0';
}
/* Revert to NULL so that we exit the loop as expected. */
pair = NULL;
}
/* Now find the '=' in the previous pair, replace it with '\0' and save
* the parameter value string. */
equals = strchr(equals, ',');
if (equals) {
*equals = '\0';
param_vals[loop] = equals + 1;
} else {
param_vals[loop] = NULL;
}
}
return loop;
}
int extract_path_ex_parameters(char *params, char *param_names[], char *param_vals[], int params_max) {
char *pair;
char *equals;
int loop;
/* If we have no parameters at all, return immediately. */
if (!params || (params[0] == '\0')) {
return (0);
}
/* Get a pointer to our first parameter */
pair = params;
/* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
* remainder (if any) */
for (loop = 0; (loop < params_max) && pair; loop++) {
/* Save the name of the parameter */
param_names[loop] = pair;
/* Remember the start of this name=value pair */
equals = pair;
/* Find the start of the next name=value pair and replace the delimiter
* with a 0 to terminate the previous pair string. */
pair = strchr(pair, '/');
if (pair) {
*pair = '\0';
pair++;
} else {
/* We didn't find a new parameter so find the end of the URI and
* replace the space with a '\0' */
pair = strchr(equals, ' ');
if (pair) {
*pair = '\0';
}
/* Revert to NULL so that we exit the loop as expected. */
pair = NULL;
}
/* Now find the '=' in the previous pair, replace it with '\0' and save
* the parameter value string. */
equals = strchr(equals, '/');
if (equals) {
*equals = '\0';
param_vals[loop] = equals + 1;
} else {
param_vals[loop] = NULL;
}
}
return loop;
}
int extract_uri_ex_parameters(char *params, char *param_names[], char *param_vals[], int params_max) {
char *pair;
char *equals;
int loop;
/* If we have no parameters at all, return immediately. */
if (!params || (params[0] == '\0')) {
return (0);
}
/* Get a pointer to our first parameter */
pair = params;
/* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
* remainder (if any) */
for (loop = 0; (loop < params_max) && pair; loop++) {
/* Save the name of the parameter */
param_names[loop] = pair;
/* Remember the start of this name=value pair */
equals = pair;
/* Find the start of the next name=value pair and replace the delimiter
* with a 0 to terminate the previous pair string. */
pair = strchr(pair, '&');
if (pair) {
*pair = '\0';
pair++;
} else {
/* We didn't find a new parameter so find the end of the URI and
* replace the space with a '\0' */
pair = strchr(equals, ' ');
if (pair) {
*pair = '\0';
}
/* Revert to NULL so that we exit the loop as expected. */
pair = NULL;
}
/* Now find the '=' in the previous pair, replace it with '\0' and save
* the parameter value string. */
equals = strchr(equals, '=');
if (equals) {
*equals = '\0';
param_vals[loop] = equals + 1;
} else {
param_vals[loop] = NULL;
}
}
return loop;
}
int extract_utf8_parameters(char *inBuf, char *outBuf, int maxlen) {
unsigned char *p;
uint8_t n;
int count = 0;
for (p = (unsigned char *) inBuf; *p; ++p) {
if (*p == '%') {
// if (debug_scanf(p + 1, "%x", &n)) {
// '
n = iAsciiStringParseHexByte(p + 1);
// if (scanf(p + 1, "%x", &n)) {
if (n != 0x27) {
outBuf[count] = n;
++count;
}
p += 2;
// }
} else {
outBuf[count] = *p;
++count;
if (count >= maxlen)
break;
}
}
outBuf[count] = '\0';
return count;
}
void StrToStr(char *str1, char *str2) {
while (*str1 == '0')
++str1;
if (strlen(str1) == 0) {
strcpy(str2, "0");
} else {
strcpy(str2, str1);
}
}
void string2hexString(char *input, int size, char *output) {
int loop;
int i;
i = 0;
loop = 0;
while (input[loop] != '\0') {
debug_sprintf((char *) (output + i), size, "%02X", input[loop]);
loop += 1;
i += 2;
}
output[i++] = '\0';
}
// mapping of ASCII characters to hex values
const uint8_t hashmap[] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // !"#$%&'
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ()*+,-./
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // @ABCDEFG
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PQRSTUVW
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // XYZ[\]^_
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // `abcdefg
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pqrstuvw
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // xyz{|}~.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
};
uint8_t HexToData(char *str, uint8_t *bytes) {
uint8_t pos;
uint8_t idx0;
uint8_t idx1;
for (pos = 0; ((pos < (strlen(str))) && (pos < strlen(str))); pos += 2) {
idx0 = (uint8_t) str[pos + 0];
idx1 = (uint8_t) str[pos + 1];
bytes[pos / 2] = (uint8_t) (hashmap[idx0] << 4) | hashmap[idx1];
}
bytes[pos / 2] = 0;
return strlen(str) / 2;
}