cppcheck/htdocs/archive/addfile.c

71 lines
2.0 KiB
C
Raw Normal View History

2013-07-06 17:24:23 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-07-07 10:27:45 +02:00
#include <ctype.h>
#include "webarchive.h"
2013-07-06 17:24:23 +02:00
2013-07-07 10:27:45 +02:00
const char *validate(const char *data)
{
int i;
if (strncmp(data,"name=",5) != 0)
return "invalid query string: must start with 'name='";
i = 5;
while (isalnum(data[i]))
i++;
if (i == 5)
return "invalid query string: no name";
if (i > 35)
return "invalid query string: max name size is 32";
if (data[i] == '\0')
return NULL;
if (data[i] != '&')
return "invalid query string: only alphanumeric characters are allowed in the name";
if (strncmp(data+i,"&data=",6)!=0)
return "invalid query string";
i += 6;
// TODO: check XML data..
2013-07-07 11:07:23 +02:00
2013-07-07 10:27:45 +02:00
return NULL;
}
2013-07-06 17:24:23 +02:00
int main()
{
const char *query_string = getenv("QUERY_STRING");
if (query_string == NULL) {
generatepage("Internal error: empty/invalid data");
2013-07-06 17:24:23 +02:00
} else if (strlen(query_string) > 1024) {
generatepage("Internal error: data size limit exceeded (1024)");
2013-07-07 10:27:45 +02:00
} else if (NULL != validate(query_string)) {
generatepage(validate(query_string));
2013-07-06 17:24:23 +02:00
} else {
char data[4096] = {0};
unencode(query_string, data);
2013-07-07 10:27:45 +02:00
if (NULL != validate(data)) {
generatepage(validate(data));
2013-07-07 10:27:45 +02:00
} else {
char *olddata[MAX_RECORDS] = {0};
olddata[0] = data;
if (!readdata(&olddata[1], MAX_RECORDS-1)) {
generatepage("Failed to add file (access denied). Try again.");
return EXIT_SUCCESS;
}
2013-07-07 10:27:45 +02:00
sortdata(olddata, MAX_RECORDS);
2013-07-06 17:24:23 +02:00
2013-07-07 10:27:45 +02:00
FILE *f = fopen("data.txt", "wt");
if (f == NULL) {
generatepage("Failed to add file (access denied). Try again.");
return EXIT_SUCCESS;
}
2013-07-07 10:27:45 +02:00
for (int i = 0; i < MAX_RECORDS && olddata[i]; i++)
fprintf(f, "%s\n", olddata[i]);
fclose(f);
generatepage("saved.");
2013-07-07 10:27:45 +02:00
}
2013-07-06 17:24:23 +02:00
}
return EXIT_SUCCESS;
}