Support building with PNaCl.

PNaCl needs OPENSSL_NO_ASM to work and a couple of cases were missing
because it hasn't previously been tested.

Additionally, it defined _BSD_SOURCE and others on the command line,
causing duplicate definition errors when defined in source code.

It's missing readdir_r.

It uses newlib, which appears to use u_short in socket.h without ever
defining it.

Change-Id: Ieccfc7365723d0521f6327eebe9f44a2afc57406
Reviewed-on: https://boringssl-review.googlesource.com/1140
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/base.h b/crypto/base.h
index 632d551..be9973f 100644
--- a/crypto/base.h
+++ b/crypto/base.h
@@ -58,7 +58,6 @@
 #include <string.h>
 #include <sys/types.h>
 
-
 #if defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64)
 #define OPENSSL_64_BIT
 #define OPENSSL_X86_64
@@ -74,6 +73,9 @@
 #elif defined(__mips__)
 #define OPENSSL_32_BIT
 #define OPENSSL_MIPS
+#elif defined(__pnacl__)
+#define OPENSSL_32_BIT
+#define OPENSSL_PNACL
 #else
 #error "Unknown target CPU"
 #endif
diff --git a/crypto/bio/internal.h b/crypto/bio/internal.h
index d498708..2eb2875 100644
--- a/crypto/bio/internal.h
+++ b/crypto/bio/internal.h
@@ -60,6 +60,11 @@
 #include <openssl/base.h>
 
 #if !defined(OPENSSL_WINDOWS)
+#if defined(OPENSSL_PNACL)
+/* newlib uses u_short in socket.h without defining it. */
+typedef unsigned short u_short;
+#endif
+#include <sys/types.h>
 #include <sys/socket.h>
 #else
 #include <WinSock2.h>
diff --git a/crypto/bio/printf.c b/crypto/bio/printf.c
index e513ff3..7f7106f 100644
--- a/crypto/bio/printf.c
+++ b/crypto/bio/printf.c
@@ -54,7 +54,9 @@
  * copied and put under another distribution licence
  * [including the GNU Public Licence.] */
 
+#if !defined(_BSD_SOURCE)
 #define _BSD_SOURCE  /* for snprintf, vprintf etc */
+#endif
 
 #include <openssl/bio.h>
 
diff --git a/crypto/bn/mul.c b/crypto/bn/mul.c
index af26eba..8c74e2f 100644
--- a/crypto/bn/mul.c
+++ b/crypto/bn/mul.c
@@ -131,7 +131,7 @@
   }
 }
 
-#if !defined(OPENSSL_X86)
+#if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
 /* Here follows specialised variants of bn_add_words() and bn_sub_words(). They
  * have the property performing operations on arrays of different sizes. The
  * sizes of those arrays is expressed through cl, which is the common length (
diff --git a/crypto/bn/rsaz_exp.c b/crypto/bn/rsaz_exp.c
index 1037687..c802752 100644
--- a/crypto/bn/rsaz_exp.c
+++ b/crypto/bn/rsaz_exp.c
@@ -42,7 +42,7 @@
 
 #include <openssl/base.h>
 
-#if defined(OPENSSL_X86_64)
+#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64)
 
 #include "rsaz_exp.h"
 
diff --git a/crypto/chacha/chacha_generic.c b/crypto/chacha/chacha_generic.c
index b5daed2..40a6f20 100644
--- a/crypto/chacha/chacha_generic.c
+++ b/crypto/chacha/chacha_generic.c
@@ -18,7 +18,7 @@
 
 #include <openssl/cpu.h>
 
-#if defined(OPENSSL_WINDOWS) || !defined(OPENSSL_X86_64) && !defined(OPENSSL_X86)
+#if defined(OPENSSL_WINDOWS) || (!defined(OPENSSL_X86_64) && !defined(OPENSSL_X86)) || !defined(__SSE2__)
 
 /* sigma contains the ChaCha constants, which happen to be an ASCII string. */
 static const char sigma[16] = "expand 32-byte k";
@@ -137,4 +137,4 @@
   }
 }
 
-#endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 && !OPENSSL_X86 && !OPENSSL_ARM */
+#endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 && !OPENSSL_X86 || !__SSE2__ */
diff --git a/crypto/chacha/chacha_vec.c b/crypto/chacha/chacha_vec.c
index d06d1dd..90629a4 100644
--- a/crypto/chacha/chacha_vec.c
+++ b/crypto/chacha/chacha_vec.c
@@ -25,7 +25,7 @@
 
 #include <openssl/chacha.h>
 
-#if !defined(OPENSSL_WINDOWS) && (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
+#if !defined(OPENSSL_WINDOWS) && (defined(OPENSSL_X86_64) || defined(OPENSSL_X86)) && defined(__SSE2__)
 
 #define CHACHA_RNDS 20 /* 8 (high speed), 20 (conservative), 12 (middle) */
 
@@ -326,4 +326,4 @@
 		}
 	}
 
-#endif /* !OPENSSL_WINDOWS && (OPENSSL_X86_64 || OPENSSL_X86) */
+#endif /* !OPENSSL_WINDOWS && (OPENSSL_X86_64 || OPENSSL_X86) && SSE2 */
diff --git a/crypto/directory_posix.c b/crypto/directory_posix.c
index 990f663..6f0acc5 100644
--- a/crypto/directory_posix.c
+++ b/crypto/directory_posix.c
@@ -24,7 +24,9 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 
+#if !defined(_POSIX_C_SOURCE)
 #define _POSIX_C_SOURCE 1  /* for readdir_r */
+#endif
 
 #include "directory.h"
 
@@ -34,6 +36,21 @@
 #include <dirent.h>
 #include <errno.h>
 
+#if defined(OPENSSL_PNACL)
+/* pnacl doesn't include readdir_r! So we do the best we can. */
+int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
+  errno = 0;
+  *result = readdir(dirp);
+  if (*result != NULL) {
+    return 0;
+  }
+  if (errno) {
+    return 1;
+  }
+  return 0;
+}
+#endif
+
 struct OPENSSL_dir_context_st {
   DIR *dir;
   struct dirent dirent;
diff --git a/crypto/mem.c b/crypto/mem.c
index 9cf0aa0..a5f639b 100644
--- a/crypto/mem.c
+++ b/crypto/mem.c
@@ -53,7 +53,9 @@
  * copied and put under another distribution licence
  * [including the GNU Public Licence.] */
 
+#if !defined(_BSD_SOURCE)
 #define _BSD_SOURCE /* needed for strdup, snprintf, vprintf etc */
+#endif
 
 #include <openssl/mem.h>
 
diff --git a/crypto/mem_clear.c b/crypto/mem_clear.c
index c13ecd8..2c46ffe 100644
--- a/crypto/mem_clear.c
+++ b/crypto/mem_clear.c
@@ -58,7 +58,7 @@
 #include <string.h>
 
 
-#if !defined(OPENSSL_X86)
+#if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
 
 /* OPENSSL_cleanse is given its own compilation unit in the hopes that the
  * compiler won't be able to optimise it away. */
diff --git a/crypto/rand/urandom.c b/crypto/rand/urandom.c
index 44fe1d6..6cd9271 100644
--- a/crypto/rand/urandom.c
+++ b/crypto/rand/urandom.c
@@ -91,7 +91,7 @@
   if (urandom_fd != -2)
     return urandom_fd;
 
-  urandom_fd = open("/dev/urandom", O_RDONLY | O_NOCTTY);
+  urandom_fd = open("/dev/urandom", O_RDONLY);
   return urandom_fd;
 }
 
diff --git a/crypto/time_support.c b/crypto/time_support.c
index 5c6e9c0..f845e98 100644
--- a/crypto/time_support.c
+++ b/crypto/time_support.c
@@ -55,7 +55,9 @@
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com). */
 
+#if !defined(_BSD_SOURCE)
 #define _BSD_SOURCE /* for gmtime_r */
+#endif
 
 #include <openssl/time_support.h>