Use non-deprecated methods on windows.

Use of strdup, close, lseek, read, and write prevent linking
statically againt libcmt.lib.

Change-Id: I04f7876ec0f03f29f000bbcc6b2ccdec844452d2
Reviewed-on: https://boringssl-review.googlesource.com/8010
Reviewed-by: David Benjamin <davidben@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ec82e1d..3c5db63 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -74,8 +74,6 @@
       "C4800" # 'int' : forcing value to bool 'true' or 'false'
               # (performance warning)
       "C4820" # 'bytes' bytes padding added after construct 'member_name'
-      "C4996" # 'read': The POSIX name for this item is deprecated. Instead,
-              # use the ISO C++ conformant name: _read.
       "C5027" # move assignment operator was implicitly defined as deleted
       )
   set(MSVC_LEVEL4_WARNINGS_LIST
@@ -91,6 +89,7 @@
   add_definitions(-D_HAS_EXCEPTIONS=0)
   add_definitions(-DWIN32_LEAN_AND_MEAN)
   add_definitions(-DNOMINMAX)
+  add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
 endif()
 
 if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
diff --git a/crypto/bio/fd.c b/crypto/bio/fd.c
index 12e6a72..7d94843 100644
--- a/crypto/bio/fd.c
+++ b/crypto/bio/fd.c
@@ -108,20 +108,25 @@
 }
 
 #if defined(OPENSSL_WINDOWS)
-int bio_fd_should_retry(int i) {
-  if (i == -1) {
-    return bio_fd_non_fatal_error((int)GetLastError());
-  }
-  return 0;
-}
+  #define BORINGSSL_ERRNO (int)GetLastError()
+  #define BORINGSSL_CLOSE _close
+  #define BORINGSSL_LSEEK _lseek
+  #define BORINGSSL_READ _read
+  #define BORINGSSL_WRITE _write
 #else
+  #define BORINGSSL_ERRNO errno
+  #define BORINGSSL_CLOSE close
+  #define BORINGSSL_LSEEK lseek
+  #define BORINGSSL_READ read
+  #define BORINGSSL_WRITE write
+#endif
+
 int bio_fd_should_retry(int i) {
   if (i == -1) {
-    return bio_fd_non_fatal_error(errno);
+    return bio_fd_non_fatal_error(BORINGSSL_ERRNO);
   }
   return 0;
 }
-#endif
 
 BIO *BIO_new_fd(int fd, int close_flag) {
   BIO *ret = BIO_new(BIO_s_fd());
@@ -145,7 +150,7 @@
 
   if (bio->shutdown) {
     if (bio->init) {
-      close(bio->num);
+      BORINGSSL_CLOSE(bio->num);
     }
     bio->init = 0;
   }
@@ -155,7 +160,7 @@
 static int fd_read(BIO *b, char *out, int outl) {
   int ret = 0;
 
-  ret = read(b->num, out, outl);
+  ret = BORINGSSL_READ(b->num, out, outl);
   BIO_clear_retry_flags(b);
   if (ret <= 0) {
     if (bio_fd_should_retry(ret)) {
@@ -167,7 +172,7 @@
 }
 
 static int fd_write(BIO *b, const char *in, int inl) {
-  int ret = write(b->num, in, inl);
+  int ret = BORINGSSL_WRITE(b->num, in, inl);
   BIO_clear_retry_flags(b);
   if (ret <= 0) {
     if (bio_fd_should_retry(ret)) {
@@ -188,14 +193,14 @@
     case BIO_C_FILE_SEEK:
       ret = 0;
       if (b->init) {
-        ret = (long)lseek(b->num, num, SEEK_SET);
+        ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
       }
       break;
     case BIO_C_FILE_TELL:
     case BIO_CTRL_INFO:
       ret = 0;
       if (b->init) {
-        ret = (long)lseek(b->num, 0, SEEK_CUR);
+        ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
       }
       break;
     case BIO_C_SET_FD:
diff --git a/crypto/lhash/lhash_test.c b/crypto/lhash/lhash_test.c
index 63748e7..309b765 100644
--- a/crypto/lhash/lhash_test.c
+++ b/crypto/lhash/lhash_test.c
@@ -152,7 +152,11 @@
       case 1:
         s = rand_string();
         lh_insert(lh, (void **)&s1, s);
+#if defined(OPENSSL_WINDOWS)
+        dummy_lh_insert(&dummy_lh, &s2, _strdup(s));
+#else
         dummy_lh_insert(&dummy_lh, &s2, strdup(s));
+#endif
 
         if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
           fprintf(stderr, "lh_insert failure\n");
diff --git a/crypto/mem.c b/crypto/mem.c
index df8e0e3..7d73c73 100644
--- a/crypto/mem.c
+++ b/crypto/mem.c
@@ -147,8 +147,6 @@
   return h;
 }
 
-char *OPENSSL_strdup(const char *s) { return strdup(s); }
-
 size_t OPENSSL_strnlen(const char *s, size_t len) {
   size_t i;
 
@@ -163,6 +161,8 @@
 
 #if defined(OPENSSL_WINDOWS)
 
+char *OPENSSL_strdup(const char *s) { return _strdup(s); }
+
 int OPENSSL_strcasecmp(const char *a, const char *b) {
   return _stricmp(a, b);
 }
@@ -173,6 +173,8 @@
 
 #else
 
+char *OPENSSL_strdup(const char *s) { return strdup(s); }
+
 int OPENSSL_strcasecmp(const char *a, const char *b) {
   return strcasecmp(a, b);
 }
diff --git a/tool/digest.cc b/tool/digest.cc
index 012202c..c2cee7f 100644
--- a/tool/digest.cc
+++ b/tool/digest.cc
@@ -46,7 +46,7 @@
 
 struct close_delete {
   void operator()(int *fd) {
-    close(*fd);
+    BORINGSSL_CLOSE(*fd);
   }
 };
 
@@ -83,7 +83,7 @@
 static bool OpenFile(int *out_fd, const std::string &filename) {
   *out_fd = -1;
 
-  int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
+  int fd = BORINGSSL_OPEN(filename.c_str(), O_RDONLY | O_BINARY);
   if (fd < 0) {
     fprintf(stderr, "Failed to open input file '%s': %s\n", filename.c_str(),
             strerror(errno));
@@ -146,7 +146,7 @@
     ssize_t n;
 
     do {
-      n = read(fd, buf.get(), kBufSize);
+      n = BORINGSSL_READ(fd, buf.get(), kBufSize);
     } while (n == -1 && errno == EINTR);
 
     if (n == 0) {
@@ -234,10 +234,10 @@
       return false;
     }
 
-    file = fdopen(fd, "rb");
+    file = BORINGSSL_FDOPEN(fd, "rb");
     if (!file) {
       perror("fdopen");
-      close(fd);
+      BORINGSSL_CLOSE(fd);
       return false;
     }
 
diff --git a/tool/internal.h b/tool/internal.h
index 6d26209..7737f4c 100644
--- a/tool/internal.h
+++ b/tool/internal.h
@@ -34,6 +34,20 @@
 #pragma warning(pop)
 #endif
 
+#if defined(OPENSSL_WINDOWS)
+  #define BORINGSSL_OPEN _open
+  #define BORINGSSL_FDOPEN _fdopen
+  #define BORINGSSL_CLOSE _close
+  #define BORINGSSL_READ _read
+  #define BORINGSSL_WRITE _write
+#else
+  #define BORINGSSL_OPEN open
+  #define BORINGSSL_FDOPEN fdopen
+  #define BORINGSSL_CLOSE close
+  #define BORINGSSL_READ read
+  #define BORINGSSL_WRITE write
+#endif
+
 enum ArgumentType {
   kRequiredArgument,
   kOptionalArgument,
diff --git a/tool/pkcs12.cc b/tool/pkcs12.cc
index fe830d6..15e32d3 100644
--- a/tool/pkcs12.cc
+++ b/tool/pkcs12.cc
@@ -64,7 +64,7 @@
     return false;
   }
 
-  int fd = open(args_map["-dump"].c_str(), O_RDONLY);
+  int fd = BORINGSSL_OPEN(args_map["-dump"].c_str(), O_RDONLY);
   if (fd < 0) {
     perror("open");
     return false;
@@ -73,7 +73,7 @@
   struct stat st;
   if (fstat(fd, &st)) {
     perror("fstat");
-    close(fd);
+    BORINGSSL_CLOSE(fd);
     return false;
   }
   const size_t size = st.st_size;
@@ -82,7 +82,7 @@
   read_result_t n;
   size_t off = 0;
   do {
-    n = read(fd, &contents[off], size - off);
+    n = BORINGSSL_READ(fd, &contents[off], size - off);
     if (n >= 0) {
       off += static_cast<size_t>(n);
     }
@@ -90,11 +90,11 @@
 
   if (off != size) {
     perror("read");
-    close(fd);
+    BORINGSSL_CLOSE(fd);
     return false;
   }
 
-  close(fd);
+  BORINGSSL_CLOSE(fd);
 
   printf("Enter password: ");
   fflush(stdout);
@@ -102,7 +102,7 @@
   char password[256];
   off = 0;
   do {
-    n = read(0, &password[off], sizeof(password) - 1 - off);
+    n = BORINGSSL_READ(0, &password[off], sizeof(password) - 1 - off);
     if (n >= 0) {
       off += static_cast<size_t>(n);
     }
diff --git a/tool/transport_common.cc b/tool/transport_common.cc
index 9115214..b017179 100644
--- a/tool/transport_common.cc
+++ b/tool/transport_common.cc
@@ -265,7 +265,7 @@
       ssize_t n;
 
       do {
-        n = read(0, buffer, sizeof(buffer));
+        n = BORINGSSL_READ(0, buffer, sizeof(buffer));
       } while (n == -1 && errno == EINTR);
 
       if (n == 0) {
@@ -319,7 +319,7 @@
 
       ssize_t n;
       do {
-        n = write(1, buffer, ssl_ret);
+        n = BORINGSSL_WRITE(1, buffer, ssl_ret);
       } while (n == -1 && errno == EINTR);
 
       if (n != ssl_ret) {