Simplify CPDF_CryptoHandler::EncryptContent()

Currently, EncryptContent() calls EncryptGetSize() to get an estimate
for the returned vector's size, and then resizing the vector the actual
size just before returning it. Instead of doing this, just use the
intended actual size to start with. Then EncryptGetSize() can go away.

Change-Id: I7d84b810aa96d30c8a926c3dd08bb1451ea8e379
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/121170
Reviewed-by: Tom Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.cpp b/core/fpdfapi/parser/cpdf_crypto_handler.cpp
index 70d4466..f323469 100644
--- a/core/fpdfapi/parser/cpdf_crypto_handler.cpp
+++ b/core/fpdfapi/parser/cpdf_crypto_handler.cpp
@@ -51,11 +51,8 @@
     uint32_t objnum,
     uint32_t gennum,
     pdfium::span<const uint8_t> source) const {
-  DataVector<uint8_t> dest(EncryptGetSize(source));
   if (m_Cipher == Cipher::kNone) {
-    fxcrt::Copy(source, dest);
-    dest.resize(source.size());
-    return dest;
+    return DataVector<uint8_t>(source.begin(), source.end());
   }
   uint8_t realkey[16];
   size_t realkeylen = sizeof(realkey);
@@ -78,9 +75,10 @@
       v = (uint8_t)rand();
     }
     CRYPT_AESSetIV(m_pAESContext.get(), iv);
+    const int nblocks = source.size() / 16;
+    DataVector<uint8_t> dest(32 + nblocks * 16);
     auto dest_span = pdfium::make_span(dest);
     fxcrt::Copy(iv, dest_span);
-    int nblocks = source.size() / 16;
     CRYPT_AESEncrypt(m_pAESContext.get(), dest_span.subspan(16).data(),
                      source.data(), nblocks * 16);
     uint8_t padding[16];
@@ -89,11 +87,9 @@
                 16 - source.size() % 16);
     CRYPT_AESEncrypt(m_pAESContext.get(),
                      dest_span.subspan(nblocks * 16 + 16).data(), padding, 16);
-    dest.resize(32 + nblocks * 16);
     return dest;
   }
-  CHECK_EQ(dest.size(), source.size());
-  fxcrt::Copy(source, dest);
+  DataVector<uint8_t> dest(source.begin(), source.end());
   CRYPT_ArcFourCryptBlock(dest, pdfium::make_span(realkey).first(realkeylen));
   return dest;
 }
@@ -314,11 +310,6 @@
   return true;
 }
 
-size_t CPDF_CryptoHandler::EncryptGetSize(
-    pdfium::span<const uint8_t> source) const {
-  return m_Cipher == Cipher::kAES ? source.size() + 32 : source.size();
-}
-
 CPDF_CryptoHandler::CPDF_CryptoHandler(Cipher cipher,
                                        pdfium::span<const uint8_t> key)
     : m_KeyLen(std::min<size_t>(key.size(), 32)), m_Cipher(cipher) {
diff --git a/core/fpdfapi/parser/cpdf_crypto_handler.h b/core/fpdfapi/parser/cpdf_crypto_handler.h
index aef533d..0792b7a 100644
--- a/core/fpdfapi/parser/cpdf_crypto_handler.h
+++ b/core/fpdfapi/parser/cpdf_crypto_handler.h
@@ -47,7 +47,6 @@
   bool IsCipherAES() const;
 
  private:
-  size_t EncryptGetSize(pdfium::span<const uint8_t> source) const;
   size_t DecryptGetSize(size_t src_size);
   void* DecryptStart(uint32_t objnum, uint32_t gennum);
   ByteString Decrypt(uint32_t objnum, uint32_t gennum, const ByteString& str);