Dumping more static memory buffers, dynamic here we come

This commit is contained in:
Pentium44 2021-04-17 02:47:20 -07:00
parent 7f88238255
commit 51884ea852
8 changed files with 100 additions and 110 deletions

View File

@ -49,8 +49,6 @@
#define MAX_DATA_BUFSIZE (MAX_VAR_DATA_LEN + 1)
#define MAXVARS 1488
#define MAX_PIPE_CMDS 32
#define MAX_CONCAT_BUF 131072
#define MAX_READFILE_LEN 2097152
// for networking functions
#define MAX_ADDRESS_LEN 256
#define MAX_ADDRESS_BUF (MAX_ADDRESS_LEN + 1)

View File

@ -5,18 +5,20 @@
View README file supplied with this software for more details
*/
#include "x3mem.h"
// Inset backquote buffers
#define MAX_BACKQUOTE_LEN 6144
#define MAX_BQ_BUFSIZE (MAX_BACKQUOTE_LEN + 1)
#define MAX_BQ_FUNCTIONS 16
#define MAX_BQ_FUNCTIONS 128
struct s_bq
{
char function [MAX_BQ_BUFSIZE];
char *function;
};
typedef struct s_bq BQ;
UX3_EXT QLIST QM_BQ [ONE]; // Dynamic-memory QLIST
void s_set_bq(int index, char *function);
int get_bq_count();
char *parse_bq(char *string);

View File

@ -7,7 +7,7 @@
#include "x3mem.h"
UX3_EXT QLIST QM_RETURN [ONE]; // Dynamic-memory QLIST
UX3_EXT QLIST QM_SS [ONE]; // Dynamic-memory QLIST
char *process_line(char *line);

View File

@ -5,6 +5,8 @@
View README file supplied with this software for more details
*/
#include "x3mem.h"
#ifndef MY_GLOBAL
#define MY_GLOBAL extern
#endif /* MY_GLOBAL */
@ -12,12 +14,14 @@
struct s_variables
{
char var_name [MAX_VAR_NAME_LEN];
char var_data [MAX_VAR_DATA_LEN];
char *var_data;
};
typedef struct s_variables VARS;
MY_GLOBAL VARS svars [MAXVARS];
UX3_EXT QLIST QM_VARIABLES [ONE]; // Dynamic-memory QLIST
void set_var(int index, char *varname, char *vardata);
int get_var_count();
char *get_var_data(char *varname);

View File

@ -16,6 +16,7 @@ BQ s_bqf [MAX_BQ_FUNCTIONS];
void s_inset(int index, char *function)
{
s_bqf[index].function = qmalloc(QM_BQ, (strlen(function) + 1));
strcpy(s_bqf[index].function, function);
}
@ -24,7 +25,7 @@ int get_bq_count()
int yy;
for(yy = 0; yy < MAXVARS; yy++)
{
if(strlen(s_bqf[yy].function) > 0)
if(s_bqf[yy].function != NULL)
{
continue;
}
@ -38,15 +39,17 @@ int get_bq_count()
char *parse_bq(char *string)
{
int str_char, buf_len, var_len;
static char finished [MAX_STRING_BUFSIZE];
char varbuffer [MAX_BQ_BUFSIZE];
int str_char, var_len;
char *varbuffer, *finished;
/* String pointers */
char *input_pointer;
char *output_pointer;
char *variable_pointer;
varbuffer = qmalloc(QM_BQ, strlen(string));
finished = qmalloc(QM_BQ, (strlen(string) + 2));
*finished = NULLBYTE;
for (input_pointer = string, output_pointer = finished;
@ -81,10 +84,8 @@ char *parse_bq(char *string)
syn_error("ss:error:backquoted function must return string!");
var_len = strlen(variable_pointer);
buf_len = strlen(finished) + var_len;
if(buf_len > MAX_STRING_LEN)
syn_error("ss:error:string buffer overflow!");
qrealloc(finished, (var_len));
strcat (finished, variable_pointer);
output_pointer += var_len;

View File

@ -21,22 +21,12 @@
char *process_line(char *line)
{
char *tok_srch, *retbuf;
char *tok_srch, *retbuf, *filebuf, *compbuf, *loopbuf;
//printf("%s", &line[strlen(line)-2]);
static char concatbuf[MAX_CONCAT_BUF+1];
static char filebuf[MAX_READFILE_LEN+1];
static char compbuf[MAX_STRING_BUFSIZE];
static char loopbuf[MAX_STRING_BUFSIZE];
static char countbuf[MAX_STRING_BUFSIZE];
static char dirpathbuf[MAX_STRING_BUFSIZE];
// Make sure static buffers are empty before initialize
// Write with zeros
bzero(filebuf, MAX_READFILE_LEN);
bzero(concatbuf, MAX_CONCAT_BUF);
tok_srch = strtok(line, "=\" ;");
/* reuse function */
@ -75,14 +65,8 @@ char *process_line(char *line)
{
int loop_count, ii;
char *proc_return;
char *countbuf;
if(loopbuf != NULL || countbuf != NULL)
{
bzero(loopbuf, MAX_STRING_LEN);
bzero(countbuf, MAX_STRING_LEN);
}
*loopbuf = '\0';
// We have an if statement
tok_srch = strtok(NULL, ";");
@ -92,6 +76,17 @@ char *process_line(char *line)
return NULL;
}
// Check if there's anything after the first quote
if(strcmp(tok_srch, "\n") == 0 ||
strcmp(tok_srch, " \n") == 0 ||
strcmp(tok_srch, " ") == 0)
{
x_warn("ss:warn:loop syntax error, missing argument?");
return NULL;
}
countbuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1));
*countbuf = '\0';
strcat(countbuf, parse_vars(tok_srch));
loop_count = atoi(countbuf);
@ -102,14 +97,26 @@ char *process_line(char *line)
return NULL;
}
// Check if there's anything after the first quote
if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
{
x_warn("ss:warn:if syntax error, missing first argument?");
return NULL;
}
loopbuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1));
*loopbuf = '\0';
strcat(loopbuf, parse_vars(tok_srch));
for(ii = 0; ii < loop_count; ii++)
{
proc_return = process_line(parse_vars(loopbuf));
if(proc_return != NULL)
{
printf("%s\n", proc_return);
fflush(stdout);
}
}
return NULL;
}
@ -118,11 +125,6 @@ char *process_line(char *line)
// IF statement
else if(strncmp("comp:", tok_srch, 5) == 0)
{
if(compbuf != NULL)
{
bzero(compbuf, MAX_STRING_LEN);
}
// We have an if statement
tok_srch = strtok(NULL, "\"");
if(tok_srch == NULL)
@ -145,15 +147,8 @@ char *process_line(char *line)
return NULL;
}
if(strlen(tok_srch) < MAX_STRING_LEN)
{
compbuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1));
strcpy(compbuf, parse_vars(tok_srch));
}
else
{
x_warn("ss:warn:if, return exceeds compare buffer");
return NULL;
}
tok_srch = strtok(NULL, "\"");
if(tok_srch == NULL)
@ -176,7 +171,8 @@ char *process_line(char *line)
return NULL;
}
if(atoi(compbuf) == atoi(parse_vars(tok_srch)))
if(atoi(compbuf) == atoi(parse_vars(tok_srch)) &&
strncmp(compbuf, parse_vars(tok_srch), strlen(compbuf)) == 0)
{
return "true";
}
@ -335,7 +331,7 @@ char *process_line(char *line)
dirstr_size += (strlen(ent->d_name)+3);
}
retbuf = qmalloc(QM_RETURN, dirstr_size);
retbuf = qmalloc(QM_SS, dirstr_size);
sprintf(retbuf, "%s\n", "ss:showdir:");
@ -392,7 +388,7 @@ char *process_line(char *line)
sprintf(origsize, "%ld", fsize(in));
uint32_t deco_return = ss_decompress(filename, filedecout);
retbuf = qmalloc(QM_RETURN, (sizeof(deco_return) + strlen(filename) + strlen(origsize) + 40));
retbuf = qmalloc(QM_SS, (sizeof(deco_return) + strlen(filename) + strlen(origsize) + 40));
sprintf(retbuf, "ss: %s: decompressed: %s -> %u", filename, origsize, deco_return);
fclose(in);
@ -542,7 +538,7 @@ char *process_line(char *line)
uint32_t deco_return = ss_decompress(file_comp, filedecout);
if(atoi(comp_size) < (int)comp_return && comp_return != 0 && deco_return != 0 && i != 1)
{
retbuf = qmalloc(QM_RETURN, sizeof(deco_return) + sizeof(comp_return) + strlen(file_comp) + 40);
retbuf = qmalloc(QM_SS, sizeof(deco_return) + sizeof(comp_return) + strlen(file_comp) + 40);
sprintf(retbuf, "ss: %s: compressed: %u -> %u", file_comp, deco_return, comp_return);
break;
}
@ -1091,20 +1087,12 @@ char *process_line(char *line)
file_line = parse_vars(tok_srch);
file_md5_val = md5_file(file_line);
catfile = ss_concat(file_md5_val, file_line);
if(strlen(catfile) < MAX_CONCAT_BUF)
{
retbuf = qmalloc(QM_RETURN, strlen(catfile)+1);
retbuf = qmalloc(QM_SS, strlen(catfile)+1);
strcpy(retbuf, catfile);
free(catfile);
return retbuf;
}
else
{
free(catfile);
x_warn("ss:warn:md5 returned NULL!");
return NULL;
}
}
/* system execute function */
else if(strncmp("exec", tok_srch, 4) == 0 || strncmp("~", tok_srch, 1) == 0)
@ -1296,19 +1284,19 @@ char *process_line(char *line)
tok_srch = strtok(NULL, "\"");
if(tok_srch == NULL)
{
x_warn("ss:warn:write syntax error, missing quote?");
x_warn("ss:warn:read syntax error, missing quote?");
return NULL;
}
if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
{
x_warn("ss:warn:write syntax error, missing quote?");
x_warn("ss:warn:read syntax error, missing quote?");
return NULL;
}
if(strtok(NULL, "\"") == NULL)
{
x_warn("ss:warn:write syntax error, missing quote?");
x_warn("ss:warn:read syntax error, missing quote?");
return NULL;
}
@ -1320,18 +1308,18 @@ char *process_line(char *line)
/* open file */
read_file = fopen(filename, "r");
/* Check if file was opened successfully */
if(read_file == NULL)
{
x_warn("ss:warn:read, failed to open '%s'", filename);
return NULL;
}
fseek(read_file, 0L, SEEK_END);
read_size = ftell(read_file);
rewind(read_file);
dynfile = qmalloc(QM_RETURN, read_size);
/* Check if file was opened successfully */
if(read_file == NULL)
{
x_warn("ss:warn:write, failed open");
return NULL;
}
dynfile = qmalloc(QM_SS, read_size);
char *nullbyte = "\0";
strcpy(dynfile, nullbyte);
@ -1423,7 +1411,7 @@ char *process_line(char *line)
read_size = ftell(read_file);
rewind(read_file);
dynfile = qmalloc(QM_RETURN, read_size);
dynfile = qmalloc(QM_SS, read_size);
strcpy(dynfile, "\0");
@ -1434,22 +1422,11 @@ char *process_line(char *line)
fclose(read_file);
if((read_size + strlen(parse_vars(tok_srch))) <= MAX_READFILE_LEN)
{
char *filebuf = qmalloc(QM_RETURN, (read_size + strlen(parse_vars(tok_srch))));
filebuf = qmalloc(QM_SS, (read_size + strlen(parse_vars(tok_srch))));
read_file = fopen(filename, "w");
sprintf(filebuf, "%s%s", dynfile, parse_vars(tok_srch));
fprintf(read_file, "%s", filebuf);
fclose(read_file);
return NULL;
}
else
{
fclose(read_file);
x_warn("ss:warn:cat, file too large (2MB max)");
return NULL;
}
return NULL;
} /* cat function */

View File

@ -11,11 +11,11 @@
#include "inc/deps.h"
#include "inc/eprintf.h"
#include "inc/x3mem.h"
#include "inc/util.h"
#include "inc/lexer.h"
#include "inc/pipe.h"
#include "inc/tar.h"
#include "inc/inset.h"
#include "inc/vars.h"
int main(int argc, char **argv)
@ -99,11 +99,17 @@ int main(int argc, char **argv)
fflush(stdout);
}
// Free used mallocs within QM_RETURN
qflush(QM_RETURN);
// Free used mallocs within QM_SS & QM_BQ
qflush(QM_SS);
qflush(QM_BQ);
} /* end of while */
} /* file null */
/* Free variables after:
a) scripts parsed or
b) user session is over */
qflush(QM_VARIABLES);
if (argc != 1 ) fclose(script);
exit(EXIT_SUCCESS);
}

View File

@ -12,6 +12,9 @@
void set_var(int index, char *varname, char *vardata)
{
strcpy(svars[index].var_name, varname);
/* Set pointer in struct to a block of memory
THEN write to the memory buffer. */
svars[index].var_data = qmalloc(QM_VARIABLES, (strlen(vardata) + 1));
strcpy(svars[index].var_data, vardata);
}
@ -50,15 +53,18 @@ int get_var_count()
char *parse_vars(char *string)
{
int str_char, buf_len, var_len;
static char finished [MAX_STRING_BUFSIZE];
char varbuffer [MAX_VAR_NAME_BUFSIZE];
int str_char, var_len;
char *varbuffer, *finished;
varbuffer = qmalloc(QM_VARIABLES, (strlen(string) + 1));
/* String pointers */
char *input_pointer;
char *output_pointer;
char *variable_pointer;
finished = qmalloc(QM_VARIABLES, (strlen(string) + 1));
*finished = NULLBYTE;
for (input_pointer = string, output_pointer = finished;
@ -74,33 +80,29 @@ char *parse_vars(char *string)
str_char = *input_pointer;
if ((str_char != NULLBYTE) && (str_char != TOKEN))
syn_error("ss:error:buffer out of sync");
x_error("ss:error:buffer out of sync");
str_char = variable_pointer [-1];
if ((str_char != NULLBYTE) && (str_char != TOKEN))
syn_error("ss:error:buffer out of sync");
x_error("ss:error:buffer out of sync");
variable_pointer[-1] = NULLBYTE;
if(*varbuffer == NULLBYTE)
{
syn_warn("ss:warning:variable syntax error!");
x_warn("ss:warning:variable syntax error!");
return NULL;
}
variable_pointer = get_var_data(varbuffer);
printf("%s\n", variable_pointer);
if(variable_pointer == NULL)
{
syn_error("ss:error:variable data not found, abort!");
x_error("ss:error:variable data not found, abort!");
}
var_len = strlen(variable_pointer);
buf_len = strlen(finished) + var_len;
if(buf_len > MAX_STRING_LEN)
{
syn_warn("ss:error:variable data too long, abort!");
}
qrealloc(finished, (var_len + 1));
strcat (finished, variable_pointer);
output_pointer += var_len;