Fixup crypto key generation.

This CL fixes up the crypto key copying code to better handle big endian
machines.

BUG=pdfium:147

Review-Url: https://codereview.chromium.org/2190123002
diff --git a/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.cpp b/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.cpp
index 3cd973c..6dfe918 100644
--- a/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.cpp
+++ b/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.cpp
@@ -28,14 +28,8 @@
   int realkeylen = 16;
   if (m_Cipher != FXCIPHER_AES || m_KeyLen != 32) {
     uint8_t key1[32];
-    FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen);
-    key1[m_KeyLen + 0] = (uint8_t)objnum;
-    key1[m_KeyLen + 1] = (uint8_t)(objnum >> 8);
-    key1[m_KeyLen + 2] = (uint8_t)(objnum >> 16);
-    key1[m_KeyLen + 3] = (uint8_t)gennum;
-    key1[m_KeyLen + 4] = (uint8_t)(gennum >> 8);
-    FXSYS_memcpy(key1 + m_KeyLen, &objnum, 3);
-    FXSYS_memcpy(key1 + m_KeyLen + 3, &gennum, 2);
+    PopulateKey(objnum, gennum, key1);
+
     if (m_Cipher == FXCIPHER_AES) {
       FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
     }
@@ -107,9 +101,8 @@
     return pContext;
   }
   uint8_t key1[48];
-  FXSYS_memcpy(key1, m_EncryptKey, m_KeyLen);
-  FXSYS_memcpy(key1 + m_KeyLen, &objnum, 3);
-  FXSYS_memcpy(key1 + m_KeyLen + 3, &gennum, 2);
+  PopulateKey(objnum, gennum, key1);
+
   if (m_Cipher == FXCIPHER_AES) {
     FXSYS_memcpy(key1 + m_KeyLen + 5, "sAlT", 4);
   }
@@ -137,6 +130,7 @@
   CRYPT_ArcFourSetup(pContext, realkey, realkeylen);
   return pContext;
 }
+
 FX_BOOL CPDF_CryptoHandler::CryptStream(void* context,
                                         const uint8_t* src_buf,
                                         uint32_t src_size,
@@ -335,3 +329,14 @@
 CPDF_CryptoHandler::~CPDF_CryptoHandler() {
   FX_Free(m_pAESContext);
 }
+
+void CPDF_CryptoHandler::PopulateKey(uint32_t objnum,
+                                     uint32_t gennum,
+                                     uint8_t* key) {
+  FXSYS_memcpy(key, m_EncryptKey, m_KeyLen);
+  key[m_KeyLen + 0] = (uint8_t)objnum;
+  key[m_KeyLen + 1] = (uint8_t)(objnum >> 8);
+  key[m_KeyLen + 2] = (uint8_t)(objnum >> 16);
+  key[m_KeyLen + 3] = (uint8_t)gennum;
+  key[m_KeyLen + 4] = (uint8_t)(gennum >> 8);
+}
diff --git a/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h b/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h
index 3edc47b..52ad4f2 100644
--- a/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h
+++ b/core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h
@@ -62,6 +62,9 @@
   int m_KeyLen;
   int m_Cipher;
   uint8_t* m_pAESContext;
+
+ private:
+  void PopulateKey(uint32_t objnum, uint32_t gennum, uint8_t* key);
 };
 
 #endif  // CORE_FPDFAPI_FPDF_PARSER_CPDF_CRYPTO_HANDLER_H_