Add htole32 replacement and proper header detection of endian.h.

(cherry picked from commit 1c525b40ec315897fb55fbcc3bf782aaa6276f07)