Skip to content

Commit 2bf797f

Browse files
authored
Merge pull request #23 from eanon/mingw-support
Added support for Windows native MinGW toolchain
2 parents fcc5d84 + 1e2d258 commit 2bf797f

12 files changed

Lines changed: 160 additions & 0 deletions

File tree

INSTALL.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,13 @@ And "index.html" file is provided for your tests.
6767

6868
Analyzing the example codes is probably best starting point.
6969
But be aware that the examples were written as simple as it can be just to give you straight-forward ideas, so please refer included API document for more details.
70+
71+
## Windows
72+
73+
Windows native MinGW compiler/toolchain (i.e. not using Cygwin or MSYS/MSYS2) support has been added to qDecoder and tested with TDM64-GCC 4.7.1 in Windows 7 64-bit.
74+
75+
These Windows shell scripts will respectively build the 64-bit flavors of qdecoder.dll and the qDecoder's examples towards a "bin" directory. If you need or prefer to target a 32-bit architecture, simply replace "-m64" with "-m32" in these batch files.
76+
```
77+
$ build_dll.bat
78+
$ build_examples.bat
79+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ The following people have helped with suggestions, ideas, code or fixing bugs:
100100
(in alphabetical order by first name)
101101

102102
* [Seungyoung "Steve" Kim](https://github.com/wolkykim) - Project Lead
103+
* [Eric Lequien Esposti](https://github.com/eanon)
103104
* [Fabrice Fontaine](https://github.com/ffontaine)
104105
* [Jonh Wendell](https://github.com/jwendell)
105106
* [nyov](https://github.com/nyov)

build_dll.bat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
:: Build qDecoder DLL for Windows against MinGW64
2+
:: Tested under Windows 7 Pro 64-bit against TDM64-GCC 4.7.1
3+
@echo off
4+
echo Build qDecoder DLL...
5+
if not exist bin\ mkdir bin
6+
gcc -m64 -Wall -O2 -s -shared -D_GNU_SOURCE src\msw_missing.c src\internal.c src\qcgireq.c src\qcgires.c src\qcgisess.c src\qentry.c -o bin\qdecoder.dll

build_examples.bat

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:: Build qDecoder examples for Windows against MinGW64
2+
:: Tested under Windows 7 Pro 64-bit against TDM64-GCC 4.7.1
3+
@echo off
4+
echo Build qDecoder examples...
5+
for %%f in (examples\*.c) do (
6+
echo . %%~nf.cgi
7+
gcc -m64 -Wall -O2 -s -Isrc examples\%%~nf.c -Lbin -lqdecoder -o bin\%%~nf.cgi
8+
)
9+
if not exist bin\index.html copy examples\index.html bin\

examples/upload.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include <sys/stat.h>
4040
#include "qdecoder.h"
4141

42+
#if defined(__MINGW32__) && defined(_WIN32) && !defined(__CYGWIN__)
43+
#include "msw_missing.h"
44+
#endif
45+
4246
#define BASEPATH "upload"
4347

4448
ssize_t savefile(const char *filepath, const void *buf, size_t size)

src/internal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
#include "qdecoder.h"
4848
#include "internal.h"
4949

50+
#if defined(__MINGW32__) && defined(_WIN32) && !defined(__CYGWIN__)
51+
#include "msw_missing.h"
52+
#endif
53+
5054
// Change two hex character to one hex value.
5155
char _q_x2c(char hex_up, char hex_low)
5256
{

src/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@
6666
/*
6767
* Internal Definitions
6868
*/
69+
#if defined(__MINGW32__) && defined(_WIN32) && !defined(__CYGWIN__)
70+
#define CRLF "\n"
71+
#else
6972
#define CRLF "\r\n"
73+
#endif
74+
7075
#define MAX_LINEBUF (1023+1)
7176
#define DEF_DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
7277
#define DEF_FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)

src/msw_missing.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Missing declarations and/or definitions in Windows when building against MinGW
3+
*
4+
* @file msw_missing.c
5+
*/
6+
7+
#include "msw_missing.h"
8+
9+
static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
10+
int mkstemp (char *tmpl)
11+
{
12+
// mkstemp extracted from libc/sysdeps/posix/tempname.c
13+
int len;
14+
char *XXXXXX;
15+
static unsigned long long value;
16+
unsigned long long random_time_bits;
17+
unsigned int count;
18+
int fd = -1;
19+
int save_errno = errno;
20+
21+
#define ATTEMPTS_MIN (62 * 62 * 62)
22+
#if ATTEMPTS_MIN < TMP_MAX
23+
unsigned int attempts = TMP_MAX;
24+
#else
25+
unsigned int attempts = ATTEMPTS_MIN;
26+
#endif
27+
28+
len = strlen (tmpl);
29+
if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
30+
{
31+
errno = EINVAL;
32+
return -1;
33+
}
34+
35+
XXXXXX = &tmpl[len - 6];
36+
{
37+
SYSTEMTIME stNow;
38+
FILETIME ftNow;
39+
40+
// get system time
41+
GetSystemTime(&stNow);
42+
stNow.wMilliseconds = 500;
43+
if (!SystemTimeToFileTime(&stNow, &ftNow))
44+
{
45+
errno = -1;
46+
return -1;
47+
}
48+
49+
random_time_bits = (((unsigned long long)ftNow.dwHighDateTime << 32)
50+
| (unsigned long long)ftNow.dwLowDateTime);
51+
}
52+
value += random_time_bits ^ (unsigned long long)GetCurrentThreadId ();
53+
54+
for (count = 0; count < attempts; value += 7777, ++count)
55+
{
56+
unsigned long long v = value;
57+
XXXXXX[0] = letters[v % 62];
58+
v /= 62;
59+
XXXXXX[1] = letters[v % 62];
60+
v /= 62;
61+
XXXXXX[2] = letters[v % 62];
62+
v /= 62;
63+
XXXXXX[3] = letters[v % 62];
64+
v /= 62;
65+
XXXXXX[4] = letters[v % 62];
66+
v /= 62;
67+
XXXXXX[5] = letters[v % 62];
68+
69+
fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, _S_IREAD | _S_IWRITE);
70+
if (fd >= 0)
71+
{
72+
errno = save_errno;
73+
return fd;
74+
}
75+
else if (errno != EEXIST)
76+
return -1;
77+
}
78+
79+
errno = EEXIST;
80+
return -1;
81+
}

src/msw_missing.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Missing declarations and/or definitions in Windows when building against MinGW
3+
*
4+
* @file msw_missing.h
5+
*/
6+
7+
#ifndef MSW_MISSING_H
8+
#define MSW_MISSING_H
9+
10+
#include <windows.h>
11+
#include <errno.h>
12+
#include <sys/stat.h>
13+
#include <fcntl.h>
14+
15+
#define S_IRGRP 0040
16+
#define S_IROTH 0004
17+
18+
int mkstemp (char *tmpl);
19+
20+
#endif /* MSW_MISSING_H */

src/qcgireq.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@
143143
#include "qdecoder.h"
144144
#include "internal.h"
145145

146+
#if defined(__MINGW32__) && defined(_WIN32) && !defined(__CYGWIN__)
147+
#include "msw_missing.h"
148+
#endif
149+
146150
#ifndef _DOXYGEN_SKIP
147151
static int _parse_multipart(qentry_t *request);
148152
static char *_parse_multipart_value_into_memory(char *boundary, int *valuelen,
@@ -660,7 +664,11 @@ static char *_parse_multipart_value_into_disk(const char *boundary,
660664
}
661665

662666
// change permission
667+
#if defined(__MINGW32__) && defined(_WIN32) && !defined(__CYGWIN__)
668+
chmod(upload_path, DEF_FILE_MODE);
669+
#else
663670
fchmod(upload_fd, DEF_FILE_MODE);
671+
#endif
664672

665673
// read stream
666674
bool ioerror = false;

0 commit comments

Comments
 (0)