Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ target_include_directories(${PROJECT_NAME}
# install binary
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}-targets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
Expand Down
3 changes: 2 additions & 1 deletion src/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ void decodeblock( unsigned char in[4], unsigned char out[3] )
char *str_base64_encode(char *str)
{
unsigned char in[3], out[4];
unsigned int i, len, blocksout = 0, linesize = strlen(str);
unsigned int i, len, blocksout = 0;
size_t linesize = strlen(str);
char *tmp = str;
char *result = (char *)malloc((linesize + 3 - linesize % 3) * 4 / 3 + 1);

Expand Down
54 changes: 49 additions & 5 deletions src/cgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
#include "cgi.h"
#include "error.h"

#ifdef _MSC_VER
#define strcasecmp _stricmp

char *strndup(const char *s, size_t n)
{
size_t len = strnlen(s, n);
char *new = (char *)malloc(len + 1);

if (new == NULL)
return NULL;

new[len] = '\0';
return (char *)memcpy(new, s, len);
}
#endif

// There's no reason to not have this initialised.
static const char hextable[256] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
Expand Down Expand Up @@ -157,18 +173,32 @@ formvars *process_data(const char *query, formvars **start, formvars **last,
formvars *cgi_process_form()
{
formvars *ret = NULL;
char *method = getenv("REQUEST_METHOD");
char *method;
#ifdef _MSC_VER
size_t len;
_dupenv_s(&method, &len, "REQUEST_METHOD");
#else
method = getenv("REQUEST_METHOD");
#endif

/* When METHOD has no contents, the default action is to process it as
* GET method
*/
if (! method || ! strcasecmp("GET", method))
{
char *q = getenv("QUERY_STRING");
char *q;
#ifdef _MSC_VER
_dupenv_s(&q, &len, "QUERY_STRING");
#else
q = getenv("QUERY_STRING");
#endif

// Sometimes, GET comes without any data
if (q && *q)
ret = process_data(q, &formvars_start, &formvars_last, '=', '&');
#ifdef _MSC_VER
free(q);
#endif
}
else if (! strcasecmp("POST", method))
{
Expand All @@ -181,7 +211,11 @@ formvars *cgi_process_form()
*/
const unsigned long content_max = 1024UL * 1024UL;

#ifdef _MSC_VER
_dupenv_s(&length_str, &len, "CONTENT_LENGTH");
#else
length_str = getenv("CONTENT_LENGTH");
#endif
if (! length_str || ! *length_str)
return NULL;

Expand All @@ -201,9 +235,15 @@ formvars *cgi_process_form()
'=', '&');
}

#ifdef _MSC_VER
free(length_str);
#endif
free(post_data);
}

#ifdef _MSC_VER
free(method);
#endif
return ret;
}

Expand Down Expand Up @@ -259,10 +299,14 @@ int cgi_include(const char *path)
if (stat (path, &fstats) == -1)
goto err_input;

if (! (fcontents = malloc (fstats.st_size)))
if ((fcontents = malloc (fstats.st_size)) == NULL)
goto err_memory;

if (! (fp = fopen (path, "r")))
#ifdef _MSC_VER
if (fopen_s(&fp, path, "r") != 0)
#else
if ((fp = fopen (path, "r")) == NULL)
#endif
goto err_input;

if (fread (fcontents, sizeof(char), fstats.st_size, fp) != fstats.st_size)
Expand All @@ -277,7 +321,7 @@ int cgi_include(const char *path)
fclose (fp);
free (fcontents);

return nwritten;
return (int)nwritten;

err_input:
libcgi_error(E_WARNING, "%s: file error: %s", __FUNCTION__, path);
Expand Down
14 changes: 14 additions & 0 deletions src/cookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ formvars *cgi_get_cookies()
register size_t position;
char *cookies, *aux, *str_unesc;

#ifdef _MSC_VER
size_t len;
_dupenv_s(&cookies, &len, "HTTP_COOKIE");
if (cookies == NULL)
#else
if ((cookies = getenv("HTTP_COOKIE")) == NULL)
#endif
return NULL;

str_unesc = cgi_unescape_special_chars(cookies);
Expand All @@ -108,7 +114,11 @@ formvars *cgi_get_cookies()
exit(EXIT_FAILURE);
}

#ifdef _MSC_VER
strncpy_s(data->name, position + 1, cookies, position);
#else
strncpy(data->name, cookies, position);
#endif
data->name[position] = '\0';

position = 0;
Expand All @@ -129,7 +139,11 @@ formvars *cgi_get_cookies()
exit(-1);
}

#ifdef _MSC_VER
strncpy_s(data->value, position + 1, cookies, position);
#else
strncpy(data->value, cookies, position);
#endif
data->value[position] = '\0';

slist_add(data, &cookies_start, &cookies_last);
Expand Down
18 changes: 15 additions & 3 deletions src/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static int ncodes = sizeof(he) / sizeof(struct iso8859_15);
char *htmlentities(const char *str)
{
char *buf;
int siz, len, i = 0, j;
size_t siz, len, i = 0, j;

siz = strlen(str) + 1;

Expand All @@ -83,7 +83,11 @@ char *htmlentities(const char *str)
if (!buf)
libcgi_error(E_MEMORY, "Failed to alloc memory at htmlentities, cgi.c");

#ifdef _MSC_VER
strcpy_s(buf + i, siz - i, he[j].html);
#else
strcpy(buf + i, he[j].html);
#endif
i += len;
break;
}
Expand Down Expand Up @@ -126,8 +130,12 @@ char **file(const char *filename, unsigned int *total)
unsigned int lines, columms, char_count, i;
char **str, *buf, ch;

#ifdef _MSC_VER
if (fopen_s(&fp, filename, "r") != 0) {
#else
fp = fopen(filename, "r");
if (fp == NULL) {
#endif
*total = 0;

return NULL;
Expand Down Expand Up @@ -157,7 +165,7 @@ char **file(const char *filename, unsigned int *total)
libcgi_error(E_MEMORY, "%s, line %s", __FILE__, __LINE__);

while (!feof(fp)) {
ch = fgetc(fp);
ch = (char)fgetc(fp);

// The next while() loop is to get all contents of actual line
while ((ch != '\n') && (ch != EOF)) {
Expand All @@ -172,7 +180,7 @@ char **file(const char *filename, unsigned int *total)
}
buf[i++] = ch;

ch = fgetc(fp);
ch = (char)fgetc(fp);
}

buf[i] = '\0';
Expand All @@ -184,7 +192,11 @@ char **file(const char *filename, unsigned int *total)
exit(EXIT_FAILURE);
}

#ifdef _MSC_VER
strncpy_s(str[lines - 1], char_count + 1, buf, char_count);
#else
strncpy(str[lines - 1], buf, char_count);
#endif
memset(buf, 0, char_count);

lines++;
Expand Down
4 changes: 4 additions & 0 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include "error.h"
#include "cgi.h"

#ifdef _MSC_VER
#define strcasecmp _stricmp
#endif

// Add a new item to the list
void slist_add(formvars *item, formvars **start, formvars **last)
{
Expand Down
Loading