From d1ec81771c18749ef9a1f7dafc45d5e3296cd0df Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sat, 13 Jul 2013 09:16:15 +0200 Subject: [PATCH] Fixed 3191 (GUI: Does not compile against Qt 5) --- gui/gui.pro | 6 ++ gui/main.cpp | 4 ++ htdocs/archive/edit.c | 110 +++++++++++++++----------------- htdocs/archive/report.c | 89 -------------------------- htdocs/archive/setfiledata.c | 118 +++++++++++++++-------------------- htdocs/archive/webarchive.h | 83 ++++++++++++++++++++++++ 6 files changed, 194 insertions(+), 216 deletions(-) delete mode 100644 htdocs/archive/report.c diff --git a/gui/gui.pro b/gui/gui.pro index 8f02a04d6..44f850a96 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -5,6 +5,12 @@ DEPENDPATH += . \ ../lib INCLUDEPATH += . \ ../lib + +# In Qt 5 widgets are in separate module +greaterThan(QT_MAJOR_VERSION, 4) { + QT += widgets +} + contains(LINKCORE, [yY][eE][sS]) { LIBS += -l../bin/cppcheck-core DEFINES += CPPCHECKLIB_IMPORT diff --git a/gui/main.cpp b/gui/main.cpp index 6c69c674a..8422507c3 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -43,9 +43,13 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); +#if QT_VERSION < 0x050000 // Set codecs so that UTF-8 strings in sources are handled correctly. + // This is ONLY needed for Qt versions 4.x. + // Qt 5.x assumes UTF-8 by default. QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); +#endif QCoreApplication::setOrganizationName("Cppcheck"); QCoreApplication::setApplicationName("Cppcheck-GUI"); diff --git a/htdocs/archive/edit.c b/htdocs/archive/edit.c index c0bc78c0b..247cfc646 100644 --- a/htdocs/archive/edit.c +++ b/htdocs/archive/edit.c @@ -5,73 +5,65 @@ #include "webarchive.h" -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 "invalid edit command"; - - return NULL; -} int main() { const char *query_string = getenv("QUERY_STRING"); if (query_string == NULL) { generatepage("Internal error: invalid request"); - } else if (NULL != validate(query_string)) { - generatepage(validate(query_string)); - } else { - char *data[MAX_RECORDS] = {0}; - if (!readdata(data, MAX_RECORDS)) { - generatepage("Failed to read file data"); - return EXIT_SUCCESS; - } - - char name[32] = {0}; - strcpy(name, getname(query_string)); - int index = -1; - for (int i = 0; i < MAX_RECORDS && data[i]; i++) { - if (strcmp(name, getname(data[i])) == 0) { - index = i; - break; - } - } - - if (index == -1) { - generatepage("File not found"); - return EXIT_SUCCESS; - } - - const char *olddata = strstr(data[index], "&data="); - if (olddata) { - char *temp = malloc(strlen(olddata+6)); - unencode(olddata+6, temp); - olddata = temp; - } else - olddata = ""; - - puts("Content-type: text/html\r\n\r\n"); - puts(""); - puts(""); - puts("
"); - printf("
\n",name); - printf("
\n",olddata); - puts(" "); - puts("
"); - puts(""); - puts(""); + return EXIT_SUCCESS; } + if (NULL != validate_name_version(query_string)) { + generatepage(validate_name_version(query_string)); + return EXIT_SUCCESS; + } + + char *data[MAX_RECORDS] = {0}; + if (!readdata(data, MAX_RECORDS)) { + generatepage("Failed to read file data"); + return EXIT_SUCCESS; + } + + char name[32] = {0}; + strcpy(name, getname(query_string)); + int index = -1; + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { + if (strcmp(name, getname(data[i])) == 0) { + index = i; + break; + } + } + + if (index == -1) { + generatepage("File not found"); + return EXIT_SUCCESS; + } + + int version = getversion(data[index]); + if (version < 1) + version = 1; + + const char *olddata = strstr(data[index], "&data="); + if (olddata) { + char *temp = malloc(strlen(olddata+6)); + unencode(olddata+6, temp); + olddata = temp; + } else + olddata = ""; + + puts("Content-type: text/html\r\n\r\n"); + puts(""); + puts(""); + puts("
"); + printf(" \n",name); + printf(" \n",version); + printf("
\n",olddata); + puts(" "); + puts("
"); + puts(""); + puts(""); + return EXIT_SUCCESS; } diff --git a/htdocs/archive/report.c b/htdocs/archive/report.c deleted file mode 100644 index 6cc8fd54d..000000000 --- a/htdocs/archive/report.c +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include - -#include "webarchive.h" - -#define MAX_RECORDS 1000 - -void listAll(char **data) -{ - puts("Content-type: text/html\r\n\r\n"); - puts(""); - puts(""); - puts(""); - puts(""); - puts(""); - puts(""); - puts("
"); - for (int i = 0; i < MAX_RECORDS && data[i]; i++) { - const char *name = getname(data[i]); - printf("", name); - printf("", name); - printf("", name); - printf("", name); - printf("\n"); - } - puts("
%s
"); - puts(""); -} - -void listOne(char **data, const char name[]) -{ - int index = -1; - for (int i = 0; i < MAX_RECORDS && data[i]; i++) { - if (strcmp(getname(data[i]), name)==0) { - index = i; - break; - } - } - - puts("Content-type: text/plain\r\n\r\n"); - puts((index == -1) ? "Not found" : data[index]); -} - -int main() -{ - char *data[MAX_RECORDS] = {0}; - - // read - if (!readdata(data, MAX_RECORDS)) { - puts("Content-type: text/html\r\n\r\n"); - puts("Internal error: failed to load data"); - return 0; - } - - // sort - sortdata(data,MAX_RECORDS); - - const char *query_string = getenv("QUERY_STRING"); - if (query_string == NULL || *query_string == '\0') { - listAll(data); - } else if (strncmp(query_string, "name=", 5) == 0 && getname(query_string) != NULL) { - char name[32] = {0}; - strcpy(name, getname(query_string)); - listOne(data,name); - } else { - puts("Content-type: text/plain\r\n\r\n"); - puts("Invalid query"); - } - - return 0; -} diff --git a/htdocs/archive/setfiledata.c b/htdocs/archive/setfiledata.c index 3bbd62055..17ab49687 100644 --- a/htdocs/archive/setfiledata.c +++ b/htdocs/archive/setfiledata.c @@ -5,81 +5,63 @@ #include "webarchive.h" -const char *validate(const char *data) -{ - int i; - - // name - 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"; - - // filedata - if (strncmp(data+i, "&data=", 6) != 0) - return "invalid query string: invalid name / no data"; - - return NULL; -} - int main() { const char *query_string = getenv("QUERY_STRING"); if (query_string == NULL) { generatepage("Internal error: invalid request"); - } else if (NULL != validate(query_string)) { - generatepage(validate(query_string)); - } else { - char *data[MAX_RECORDS] = {0}; - if (!readdata(data, MAX_RECORDS)) { - generatepage("Failed to read file data"); - return EXIT_SUCCESS; - } - - char name[32] = {0}; - strcpy(name, getname(query_string)); - int index = -1; - for (int i = 0; i < MAX_RECORDS && data[i]; i++) { - if (strcmp(name, getname(data[i])) == 0) { - index = i; - break; - } - } - - if (index == -1) { - generatepage("File not found"); - return EXIT_SUCCESS; - } - - // cleanup data... - char str[1000] = {0}; - char *dst = str; - for (const char *src = query_string; *src; src++) { - *dst = *src; - dst++; - if (strncmp(src, "%25", 3) == 0 && isxdigit(src[3]) && isxdigit(src[4])) - src += 2; - } - - data[index] = str; - sortdata(data, MAX_RECORDS); - - FILE *f = fopen("data.txt", "wt"); - if (f == NULL) { - generatepage("Failed to add file (access denied). Try again."); - return EXIT_SUCCESS; - } - for (int i = 0; i < MAX_RECORDS && data[i]; i++) - fprintf(f, "%s\n", data[i]); - fclose(f); - generatepage("saved."); + return EXIT_SUCCESS; } + if (NULL != validate_name_version_data(query_string)) { + generatepage(validate_name_version_data(query_string)); + return EXIT_SUCCESS; + } + + char *data[MAX_RECORDS] = {0}; + if (!readdata(data, MAX_RECORDS)) { + generatepage("Failed to read file data"); + return EXIT_SUCCESS; + } + + char name[32] = {0}; + strcpy(name, getname(query_string)); + int index = -1; + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { + if (strcmp(name, getname(data[i])) == 0) { + index = i; + break; + } + } + + if (index == -1) { + generatepage("File not found"); + return EXIT_SUCCESS; + } + + // cleanup data... + char str[1000] = {0}; + char *dst = str; + for (const char *src = query_string; *src; src++) { + *dst = *src; + dst++; + if (strncmp(src, "%25", 3) == 0 && isxdigit(src[3]) && isxdigit(src[4])) + src += 2; + } + + data[index] = str; + sortdata(data, MAX_RECORDS); + + FILE *f = fopen("data.txt", "wt"); + if (f == NULL) { + generatepage("Failed to add file (access denied). Try again."); + return EXIT_SUCCESS; + } + for (int i = 0; i < MAX_RECORDS && data[i]; i++) + fprintf(f, "%s\n", data[i]); + fclose(f); + generatepage("saved."); + return EXIT_SUCCESS; } diff --git a/htdocs/archive/webarchive.h b/htdocs/archive/webarchive.h index a8af76115..195b55cea 100644 --- a/htdocs/archive/webarchive.h +++ b/htdocs/archive/webarchive.h @@ -60,6 +60,25 @@ const char * getname(const char *data) return name; } +int getversion(const char *data) +{ + const char *name = getname(data); + if (name == NULL) + return 0; // invalid string => 0 + data = data + strlen("name=") + strlen(name); + if (strncmp(data,"&version=",9) != 0) + return 1; // no version => 1 + data = data + 9; + int ret = 0; + while (isdigit(*data)) { + ret = ret * 10 + *data - '0'; + data++; + } + if (*data != '\0' && *data != '&') + return -1; // invalid version => -1 + return ret; +} + void sortdata(char * * const data, int sz) { for (int i = 1; i < sz && data[i]; i++) { @@ -85,3 +104,67 @@ void generatepage(const char msg[]) puts("
"); } + +const char *validate_name_version(const char *data) +{ + int i = 0; + + // name + if (strncmp(data,"name=",5) != 0) + return "invalid query string: must start with 'name='"; + i += 5; + if (!isalnum(data[i])) + return "invalid query string: no name / invalid character in name"; + while (isalnum(data[i])) + i++; + if (i > 35) + return "invalid query string: max name size is 32"; + + // version + if (strncmp(&data[i], "&version=", 9) != 0) + return "invalid query string: 'version=' not seen at the expected location"; + i += strlen("&version="); + if (!isdigit(data[i])) + return "invalid query string: version must consist of digits 0-9"; + while (isdigit(data[i])) + i++; + + // end + if (data[i] != '\0') + return "invalid query"; + + return NULL; +} + +const char *validate_name_version_data(const char *data) +{ + int i = 0; + + // name + if (strncmp(data,"name=",5) != 0) + return "invalid query string: must start with 'name='"; + i += 5; + if (!isalnum(data[i])) + return "invalid query string: no name / invalid character in name"; + while (isalnum(data[i])) + i++; + if (i > 35) + return "invalid query string: max name size is 32"; + + // version + if (strncmp(&data[i], "&version=", 9) != 0) + return "invalid query string: 'version=' not seen at the expected location"; + i += strlen("&version="); + if (!isdigit(data[i])) + return "invalid query string: version must consist of digits 0-9"; + while (isdigit(data[i])) + i++; + + // filedata + if (strncmp(data+i, "&data=", 6) != 0) + return "invalid query string: 'data=' not seen at the expected location"; + + return NULL; +} + +