11diff --git a/src/connect.c b/src/connect.c
2- index 15cae644..840f3c36 100644
2+ index dd3bbcf5..91010853 100644
33--- a/src/connect.c
44+++ b/src/connect.c
55@@ -37,6 +37,7 @@
@@ -16,8 +16,8 @@ index 15cae644..840f3c36 100644
1616 #endif
1717+ #endif
1818
19- #if _MSC_VER >= 1400
20- #include <io .h>
19+ #include <winsock2.h>
20+ #include <ws2tcpip .h>
2121diff --git a/src/getpass.c b/src/getpass.c
2222index 6be33c77..70686735 100644
2323--- a/src/getpass.c
@@ -41,34 +41,38 @@ index 6be33c77..70686735 100644
4141
4242 #else
4343diff --git a/src/init.c b/src/init.c
44- index e516c331..6de564a1 100644
44+ index e516c331..bf61e0fb 100644
4545--- a/src/init.c
4646+++ b/src/init.c
47- @@ -35,17 +35,40 @@
47+ @@ -35,17 +35,44 @@
4848 #include <winsock2.h>
4949 #endif
5050
5151+ #if defined(_WIN32) && defined(_MSC_VER) && defined(LIBSSH_STATIC)
52+ + # ifdef read
53+ + # undef read
54+ + # define BACKUP_READ
55+ + # endif
5256+ # define CONSTRUCTOR_ATTRIBUTE_(_func,p) static void _func(void); \
5357+ static int _func ## _wrapper(void) { _func(); return 0; } \
5458+ __pragma(section(".CRT$XCU",read)) \
5559+ __declspec(allocate(".CRT$XCU")) int (* _func##_)(void) = _func ## _wrapper; \
5660+ __pragma(comment(linker,"/include:" p #_func "_"))
57- + #ifdef _WIN64
58- + #define CONSTRUCTOR_ATTRIBUTE(f) CONSTRUCTOR_ATTRIBUTE_(f,"")
59- + #else
60- + #define CONSTRUCTOR_ATTRIBUTE(f) CONSTRUCTOR_ATTRIBUTE_(f,"_")
61- + #endif
61+ + # ifdef _WIN64
62+ + # define CONSTRUCTOR_ATTRIBUTE(f) CONSTRUCTOR_ATTRIBUTE_(f,"")
63+ + # else
64+ + # define CONSTRUCTOR_ATTRIBUTE(f) CONSTRUCTOR_ATTRIBUTE_(f,"_")
65+ + # endif
6266+ # define DESTRUCTOR_ATTRIBUTE_(_func,p) static void _func(void); \
6367+ static int _func ## _constructor(void) { atexit (_func); return 0; } \
6468+ __pragma(section(".CRT$XCU",read)) \
6569+ __declspec(allocate(".CRT$XCU")) int (* _func##_)(void) = _func ## _constructor; \
6670+ __pragma(comment(linker,"/include:" p #_func "_"))
67- + #ifdef _WIN64
68- + #define DESTRUCTOR_ATTRIBUTE(f) DESTRUCTOR_ATTRIBUTE_(f,"")
69- + #else
70- + #define DESTRUCTOR_ATTRIBUTE(f) DESTRUCTOR_ATTRIBUTE_(f,"_")
71- + #endif
71+ + # ifdef _WIN64
72+ + # define DESTRUCTOR_ATTRIBUTE(f) DESTRUCTOR_ATTRIBUTE_(f,"")
73+ + # else
74+ + # define DESTRUCTOR_ATTRIBUTE(f) DESTRUCTOR_ATTRIBUTE_(f,"_")
75+ + # endif
7276+ #else
7377 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
7478- #define CONSTRUCTOR_ATTRIBUTE __attribute__((constructor))
@@ -89,56 +93,34 @@ index e516c331..6de564a1 100644
8993
9094 /* Declare static mutex */
9195 static SSH_MUTEX ssh_init_mutex = SSH_MUTEX_STATIC_INIT;
92- @@ -56,8 +79,8 @@ static int _ssh_initialized = 0;
96+ @@ -56,8 +83,12 @@ static int _ssh_initialized = 0;
9397 /* Cache the returned value */
9498 static int _ssh_init_ret = 0;
9599
96100- void libssh_constructor(void) CONSTRUCTOR_ATTRIBUTE;
97101- void libssh_destructor(void) DESTRUCTOR_ATTRIBUTE;
98102+ CONSTRUCTOR_ATTRIBUTE(libssh_constructor);
99103+ DESTRUCTOR_ATTRIBUTE(libssh_destructor);
104+ +
105+ + #ifdef BACKUP_READ
106+ + # define read _read
107+ + #endif
100108
101109 static int _ssh_init(unsigned constructor) {
102110
103- diff --git a/src/libgcrypt.c b/src/libgcrypt.c
104- index f410d997..ebb0e90b 100644
105- --- a/src/libgcrypt.c
106- +++ b/src/libgcrypt.c
107- @@ -145,12 +145,12 @@ static int blowfish_set_key(struct ssh_cipher_struct *cipher, void *key, void *I
108- }
109-
110- static void blowfish_encrypt(struct ssh_cipher_struct *cipher, void *in,
111- - void *out, unsigned long len) {
112- + void *out, size_t len) {
113- gcry_cipher_encrypt(cipher->key[0], out, len, in, len);
114- }
115-
116- static void blowfish_decrypt(struct ssh_cipher_struct *cipher, void *in,
117- - void *out, unsigned long len) {
118- + void *out, size_t len) {
119- gcry_cipher_decrypt(cipher->key[0], out, len, in, len);
120- }
121- #endif /* WITH_BLOWFISH_CIPHER */
122- @@ -416,12 +416,12 @@ static int des3_set_key(struct ssh_cipher_struct *cipher, void *key, void *IV) {
123- }
124-
125- static void des3_encrypt(struct ssh_cipher_struct *cipher, void *in,
126- - void *out, unsigned long len) {
127- + void *out, size_t len) {
128- gcry_cipher_encrypt(cipher->key[0], out, len, in, len);
129- }
130-
131- static void des3_decrypt(struct ssh_cipher_struct *cipher, void *in,
132- - void *out, unsigned long len) {
133- + void *out, size_t len) {
134- gcry_cipher_decrypt(cipher->key[0], out, len, in, len);
135- }
136-
137111diff --git a/src/misc.c b/src/misc.c
138- index 7081f12a..a4a1e2ed 100644
112+ index 6ae7cdf1..2cb2cb95 100644
139113--- a/src/misc.c
140114+++ b/src/misc.c
141- @@ -52,8 +52,10 @@
115+ @@ -33,7 +33,6 @@
116+ #include <pwd.h>
117+ #include <sys/socket.h>
118+ #include <sys/types.h>
119+ -
120+ #endif /* _WIN32 */
121+
122+ #include <errno.h>
123+ @@ -52,8 +51,13 @@
142124
143125 #ifdef _WIN32
144126
@@ -148,10 +130,13 @@ index 7081f12a..a4a1e2ed 100644
148130+ # ifndef _WIN32_IE
149131+ # define _WIN32_IE 0x0501 // SHGetSpecialFolderPath
150132+ # endif
133+ + # define WIN32_LEAN_AND_MEAN
134+ + # include <windows.h>
135+ + # include <iphlpapi.h>
151136 #endif
152137
153138 #include <winsock2.h> // Must be the first to include
154- @@ -110,6 +112 ,9 @@
139+ @@ -110,6 +114 ,9 @@
155140 #ifdef _WIN32
156141 char *ssh_get_user_home_dir(void)
157142 {
@@ -161,56 +146,60 @@ index 7081f12a..a4a1e2ed 100644
161146 char tmp[PATH_MAX] = {0};
162147 char *szPath = NULL;
163148
164- @@ -124,6 +129 ,7 @@ char *ssh_get_user_home_dir(void)
149+ @@ -124,6 +131 ,7 @@ char *ssh_get_user_home_dir(void)
165150 }
166151
167152 return NULL;
168153+ #endif
169154 }
170155
171156 /* we have read access on file */
172- @@ -180,22 +186,26 @@ int ssh_gettimeofday(struct timeval *__p, void *__t)
157+ @@ -180,19 +188,22 @@ int ssh_gettimeofday(struct timeval *__p, void *__t)
173158
174159 char *ssh_get_local_username(void)
175160 {
176161+ #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
177162+ return NULL;
178163+ #else
179164 DWORD size = 0;
180- char *user;
165+ char *user = NULL;
166+ int rc;
181167
182168 /* get the size */
183169- GetUserName(NULL, &size);
184170+ GetUserNameA(NULL, &size);
185171
186- user = (char *) malloc(size);
172+ user = (char *)malloc(size);
187173 if (user == NULL) {
188174 return NULL;
189175 }
190176
191177- if (GetUserName(user, &size)) {
192178+ if (GetUserNameA(user, &size)) {
193- return user;
194- }
179+ rc = ssh_check_username_syntax(user);
180+ if (rc == SSH_OK) {
181+ return user;
182+ @@ -202,6 +213,7 @@ char *ssh_get_local_username(void)
183+ free(user);
195184
196185 return NULL;
197186+ #endif
198187 }
199188
200189 int ssh_is_ipaddr_v4(const char *str)
201- diff --git a/src/pki_gcrypt.c b/src/pki_gcrypt.c
202- index 418a46b3..1d7dbaa7 100644
203- --- a/src/pki_gcrypt.c
204- +++ b/src/pki_gcrypt.c
205- @@ -2141,7 +2141,11 @@ ssh_signature pki_do_sign_hash(const ssh_key privkey,
206- size_t hlen,
207- enum ssh_digest_e hash_type)
208- {
209- + #if defined(_MSC_VER )
210- + unsigned char* ghash = (char*)_alloca(sizeof(char) * (hlen + 1));
211- + #else
212- unsigned char ghash[hlen + 1];
190+ @@ -240,6 +252,7 @@ int ssh_is_ipaddr(const char *str)
191+ int sslen = sizeof(ss);
192+ char *network_interface = strchr(s, '%');
193+
194+ + #if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
195+ /* link-local (IP:v6:addr%ifname). */
196+ if (network_interface != NULL) {
197+ rc = if_nametoindex(network_interface + 1);
198+ @@ -249,6 +262,7 @@ int ssh_is_ipaddr(const char *str )
199+ }
200+ *network_interface = '\0';
201+ }
213202+ #endif
214- const char *hash_c = NULL;
215- ssh_signature sig;
216- gcry_sexp_t sexp;
203+ rc = WSAStringToAddressA((LPSTR) s,
204+ AF_INET6,
205+ NULL,
0 commit comments