Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch for Windows MSVC from W32TeX #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/ppload.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

#include "pplib.h"

#ifdef _WIN32 /* --ak */
extern FILE *ppu8open(const char *filename, const char *mode);
#define fopen ppu8open
#endif /* _WIN32 --ak */

const char * ppobj_kind[] = { "none", "null", "bool", "integer", "number", "name", "string", "array", "dict", "stream", "ref" };

#define ignored_char(c) (c == 0x20 || c == 0x0A || c == 0x0D || c == 0x09 || c == 0x00)
Expand Down
8 changes: 8 additions & 0 deletions src/pptest1.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ static int usage (const char *argv0)
return 0;
}

#ifdef _WIN32 /* --ak */
#include <fcntl.h>
#endif /* _WIN32 --ak */

int main (int argc, const char **argv)
{
const char *filepath;
Expand All @@ -69,6 +73,10 @@ int main (int argc, const char **argv)
const void *data;
size_t size;

#ifdef _WIN32 /* --ak */
_setmode(fileno(stdout), _O_BINARY);
#endif /* _WIN32 --ak */

if (argc < 2)
return usage(argv[0]);
for (a = 1; a < argc; ++a)
Expand Down
8 changes: 8 additions & 0 deletions src/pptest2.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ static void log_callback (const char *message, void *alien)
fprintf((FILE *)alien, "\nooops: %s\n", message);
}

#ifdef _WIN32 /* --ak */
#include <fcntl.h>
#endif /* _WIN32 --ak */

int main (int argc, const char **argv)
{
const char *filepath;
Expand All @@ -54,6 +58,10 @@ int main (int argc, const char **argv)
ppname *op;
size_t operators;

#ifdef _WIN32 /* --ak */
_setmode(fileno(stdout), _O_BINARY);
#endif /* _WIN32 --ak */

if (argc < 2)
return usage(argv[0]);
ppstream_init_buffers();
Expand Down
8 changes: 8 additions & 0 deletions src/pptest3.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ static void check_stream_chunks (ppstream *stream)

#define USE_BUFFERS_POOL 1

#ifdef _WIN32 /* --ak */
#include <fcntl.h>
#endif /* _WIN32 --ak */

int main (int argc, const char **argv)
{
const char *filepath;
Expand All @@ -87,6 +91,10 @@ int main (int argc, const char **argv)
ppuint refnum;
ppref *ref;

#ifdef _WIN32 /* --ak */
_setmode(fileno(stdout), _O_BINARY);
#endif /* _WIN32 --ak */

if (argc < 2)
return usage(argv[0]);
if (USE_BUFFERS_POOL)
Expand Down
103 changes: 85 additions & 18 deletions src/util/utiliof.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "utillog.h"
#include "utiliof.h"

#ifdef _WIN32 /* --ak */
FILE *ppu8open(const char *filename, const char *mode);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps there should be util_fopen() function explicitly called in other places, instead of hiding fopen() by a macro?

#define fopen ppu8open
#endif /* _WIN32 --ak */

/* commons */

void * iof_copy_data (const void *data, size_t size)
Expand Down Expand Up @@ -2969,25 +2974,87 @@ iof * iof_filter_reader_replacement (iof *P, iof_handler handler, size_t statesi
return F;
}

#ifdef _WIN32 /* --ak */
#include <malloc.h>
#include <sys/stat.h>
#include <wchar.h>
#include <windows.h>
/*
Get wide string from multibyte string. (by T. Tanaka)
*/
static wchar_t *
get_wstring_from_mbstring(int cp, const char *mbstr, wchar_t *wstr)
{
int len;

len = MultiByteToWideChar(cp, 0, mbstr, -1, wstr, 0);
if (len==0) {
return NULL;
}
if (wstr==NULL) {
wstr = malloc(sizeof(wchar_t)*(len+1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should consequently use util_malloc(), as probably all code in pplib. afair there was two reasons to avoid direct calls to malloc():

  • easier replacement with some other base allocator (some day maybe)
  • low-level check for NULL (and rather brutal solution..)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand pplib defines util_malloc() at util/utilmem.c and is using util_malloc() instead of malloc().
I agree util_malloc() is better.

}
len = MultiByteToWideChar(cp, 0, mbstr, -1, wstr, len+1);
if (len==0) {
return NULL;
}
return wstr;
}

FILE *ppu8open(const char *filename, const char *mode)
{
wchar_t *wfilename, *wmode;
FILE *ret;
int cp;
unsigned char *fnn;
unsigned char *p;
size_t len = strlen(filename);

fnn = malloc(len + 10);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or meybe unsigned char fnn[MAX_PATH + 10]?

Copy link
Author

@t-tk t-tk Aug 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have found a document about very long path name:
https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd

In short, very long file name is available after the \\?\ prefix and it could be longer than MAX_PATH.
So, util_malloc() should be better.

p = strstr(filename, ".\\");
if (!p) {
p = strstr(filename, "./");
}
if (!p && len > 2) {
p = strstr(filename + 2, "//");
}
if (!p && len > 2) {
p = strstr(filename + 2, "\\\\");
}
if (!p && len > 2) {
p = strstr(filename + 2, "\\/");
}
if (!p && len > 2) {
p = strstr(filename + 2, "/\\");
}
if (!p && len > 2 && ((filename[0] == '/' && filename[1] == '/') ||
(filename[0] == '\\' && filename[1] == '\\' &&
filename[2] != '?'))) {
filename += 2;
strcpy (fnn, "\\\\?\\UNC\\");
strcat (fnn, filename);
} else if (!p && len > 2 && filename[1] == ':') {
strcpy (fnn, "\\\\?\\");
strcat (fnn, filename);
} else {
strcpy (fnn, filename);
}
for (p = fnn; *p; p++) {
if (*p == '/')
*p = '\\';
}



















cp = CP_UTF8;
wfilename = get_wstring_from_mbstring(cp, fnn, wfilename=NULL);
free(fnn);
if (wfilename == NULL)
return NULL;
wmode = get_wstring_from_mbstring(cp, mode, wmode=NULL);
if (wmode == NULL)
return NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity, if wmode is NULL, wfilename should be freed before return.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fixed by d315123

ret = _wfopen((const wchar_t *)wfilename, (const wchar_t *)wmode);
free(wfilename);
free(wmode);
return ret;
}
#endif /* _WIN32 --ak */
5 changes: 5 additions & 0 deletions src/util/utilmd5.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@

#include "utilmd5.h"

#ifdef _WIN32 /* --ak */
extern FILE *ppu8open(const char *filename, const char *mode);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO would be perfect if such redefinitions were in some dedicated place, like utildecl.h, utilplat.h or a new one, containing only platform-dependent code.

#define fopen ppu8open
#endif /* _WIN32 --ak */

#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
#ifdef ARCH_IS_BIG_ENDIAN
# define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
Expand Down
5 changes: 5 additions & 0 deletions src/util/utilsha.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
//#include <assert.h> /* assert() */
#include "utilsha.h"

#ifdef _WIN32 /* --ak */
extern FILE *ppu8open(const char *filename, const char *mode);
#define fopen ppu8open
#endif /* _WIN32 --ak */

/*
* UNROLLED TRANSFORM LOOP NOTE:
* You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
Expand Down