fgas/ code cleanup.

This CL shuffles code around in the fgas/ headers, removes unused functions
and adds anonymous namepaces for static methods and data.

Review-Url: https://codereview.chromium.org/1992033002
diff --git a/BUILD.gn b/BUILD.gn
index 6601f21..c8f1c2d 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -948,11 +948,9 @@
       "xfa/fde/xml/fde_xml.h",
       "xfa/fde/xml/fde_xml_imp.cpp",
       "xfa/fde/xml/fde_xml_imp.h",
-      "xfa/fgas/crt/fgas_algorithm.cpp",
       "xfa/fgas/crt/fgas_algorithm.h",
       "xfa/fgas/crt/fgas_codepage.cpp",
       "xfa/fgas/crt/fgas_codepage.h",
-      "xfa/fgas/crt/fgas_encode.cpp",
       "xfa/fgas/crt/fgas_language.h",
       "xfa/fgas/crt/fgas_memory.cpp",
       "xfa/fgas/crt/fgas_memory.h",
@@ -1639,6 +1637,7 @@
   include_dirs = []
   if (pdf_enable_xfa) {
     sources += [
+      "xfa/fde/css/fde_cssdatatable_unittest.cpp",
       "xfa/fde/xml/fde_xml_imp_unittest.cpp",
       "xfa/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp",
       "xfa/fxfa/app/xfa_textlayout_unittest.cpp",
diff --git a/pdfium.gyp b/pdfium.gyp
index d9e1cfb..2f0392a 100644
--- a/pdfium.gyp
+++ b/pdfium.gyp
@@ -943,6 +943,7 @@
       'conditions': [
         ['pdf_enable_xfa==1', {
           'sources': [
+            'xfa/fde/css/fde_cssdatatable.cpp',
             'xfa/fde/xml/fde_xml_imp_unittest.cpp',
             'xfa/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp',
             'xfa/fxfa/app/xfa_textlayout_unittest.cpp',
diff --git a/xfa.gyp b/xfa.gyp
index f47e9e8..186f680 100644
--- a/xfa.gyp
+++ b/xfa.gyp
@@ -91,11 +91,9 @@
         "xfa/fde/xml/fde_xml.h",
         "xfa/fde/xml/fde_xml_imp.cpp",
         "xfa/fde/xml/fde_xml_imp.h",
-        "xfa/fgas/crt/fgas_algorithm.cpp",
         "xfa/fgas/crt/fgas_algorithm.h",
         "xfa/fgas/crt/fgas_codepage.cpp",
         "xfa/fgas/crt/fgas_codepage.h",
-        "xfa/fgas/crt/fgas_encode.cpp",
         "xfa/fgas/crt/fgas_language.h",
         "xfa/fgas/crt/fgas_memory.cpp",
         "xfa/fgas/crt/fgas_memory.h",
diff --git a/xfa/fde/css/fde_cssdatatable.cpp b/xfa/fde/css/fde_cssdatatable.cpp
index 3b090a2..db4302b 100644
--- a/xfa/fde/css/fde_cssdatatable.cpp
+++ b/xfa/fde/css/fde_cssdatatable.cpp
@@ -11,6 +11,14 @@
 #include "xfa/fgas/crt/fgas_codepage.h"
 #include "xfa/fgas/crt/fgas_system.h"
 
+namespace {
+
+uint8_t Hex2Dec(uint8_t hexHigh, uint8_t hexLow) {
+  return (FXSYS_toHexDigit(hexHigh) << 4) + FXSYS_toHexDigit(hexLow);
+}
+
+}  // namespace
+
 FX_BOOL FDE_CSSLengthToFloat(const FDE_CSSLENGTH& len,
                              FX_FLOAT fPercentBase,
                              FX_FLOAT& fResult) {
@@ -666,16 +674,17 @@
   } while (iStart <= iEnd);
   return NULL;
 }
+
 FX_BOOL FDE_ParseCSSNumber(const FX_WCHAR* pszValue,
                            int32_t iValueLen,
                            FX_FLOAT& fValue,
                            FDE_CSSPRIMITIVETYPE& eUnit) {
-  ASSERT(pszValue != NULL && iValueLen > 0);
+  ASSERT(pszValue && iValueLen > 0);
   int32_t iUsedLen = 0;
   fValue = FX_wcstof(pszValue, iValueLen, &iUsedLen);
-  if (iUsedLen <= 0) {
+  if (iUsedLen <= 0)
     return FALSE;
-  }
+
   iValueLen -= iUsedLen;
   pszValue += iUsedLen;
   eUnit = FDE_CSSPRIMITIVETYPE_Number;
@@ -684,9 +693,8 @@
   } else if (iValueLen == 2) {
     FDE_LPCCSSLENGTHUNITTABLE pUnit =
         FDE_GetCSSLengthUnitByName(CFX_WideStringC(pszValue, 2));
-    if (pUnit != NULL) {
+    if (pUnit)
       eUnit = (FDE_CSSPRIMITIVETYPE)pUnit->wValue;
-    }
   }
   return TRUE;
 }
@@ -726,57 +734,60 @@
 FX_BOOL FDE_ParseCSSColor(const FX_WCHAR* pszValue,
                           int32_t iValueLen,
                           FX_ARGB& dwColor) {
-  ASSERT(pszValue != NULL && iValueLen > 0);
+  ASSERT(pszValue && iValueLen > 0);
+
   if (*pszValue == '#') {
     switch (iValueLen) {
       case 4: {
-        uint8_t red = FX_Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]);
-        uint8_t green = FX_Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]);
-        uint8_t blue = FX_Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]);
+        uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]);
+        uint8_t green = Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]);
+        uint8_t blue = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]);
         dwColor = ArgbEncode(255, red, green, blue);
-      }
         return TRUE;
+      }
       case 7: {
-        uint8_t red = FX_Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]);
-        uint8_t green = FX_Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]);
-        uint8_t blue = FX_Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]);
+        uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]);
+        uint8_t green = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]);
+        uint8_t blue = Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]);
         dwColor = ArgbEncode(255, red, green, blue);
-      }
         return TRUE;
+      }
+      default:
+        return FALSE;
     }
-  } else if (iValueLen >= 10) {
-    if (pszValue[iValueLen - 1] != ')' || FX_wcsnicmp(L"rgb(", pszValue, 4)) {
+  }
+
+  if (iValueLen >= 10) {
+    if (pszValue[iValueLen - 1] != ')' || FX_wcsnicmp(L"rgb(", pszValue, 4))
       return FALSE;
-    }
+
     uint8_t rgb[3] = {0};
     FX_FLOAT fValue;
     FDE_CSSPRIMITIVETYPE eType;
     CFDE_CSSValueListParser list(pszValue + 4, iValueLen - 5, ',');
     for (int32_t i = 0; i < 3; ++i) {
-      if (!list.NextValue(eType, pszValue, iValueLen)) {
+      if (!list.NextValue(eType, pszValue, iValueLen))
         return FALSE;
-      }
-      if (eType != FDE_CSSPRIMITIVETYPE_Number) {
+      if (eType != FDE_CSSPRIMITIVETYPE_Number)
         return FALSE;
-      }
-      if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eType)) {
+      if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eType))
         return FALSE;
-      }
+
       rgb[i] = eType == FDE_CSSPRIMITIVETYPE_Percent
                    ? FXSYS_round(fValue * 2.55f)
                    : FXSYS_round(fValue);
     }
     dwColor = ArgbEncode(255, rgb[0], rgb[1], rgb[2]);
     return TRUE;
-  } else {
-    FDE_LPCCSSCOLORTABLE pColor =
-        FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen));
-    if (pColor != NULL) {
-      dwColor = pColor->dwValue;
-      return TRUE;
-    }
   }
-  return FALSE;
+
+  FDE_LPCCSSCOLORTABLE pColor =
+      FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen));
+  if (!pColor)
+    return FALSE;
+
+  dwColor = pColor->dwValue;
+  return TRUE;
 }
 
 CFDE_CSSValueList::CFDE_CSSValueList(IFX_MemoryAllocator* pStaticStore,
diff --git a/xfa/fde/css/fde_cssdatatable_unittest.cpp b/xfa/fde/css/fde_cssdatatable_unittest.cpp
new file mode 100644
index 0000000..d602bff
--- /dev/null
+++ b/xfa/fde/css/fde_cssdatatable_unittest.cpp
@@ -0,0 +1,60 @@
+// Copyright 2016 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "xfa/fde/css/fde_cssdatatable.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(FDE_ParseCSSColor, HexEncodingParsing) {
+  FX_ARGB color;
+
+  // Length value invalid.
+  EXPECT_FALSE(FDE_ParseCSSColor(L"#000", 3, color));
+  EXPECT_FALSE(FDE_ParseCSSColor(L"#000000", 5, color));
+  EXPECT_FALSE(FDE_ParseCSSColor(L"#000000", 8, color));
+
+  // Invalid characters
+  EXPECT_TRUE(FDE_ParseCSSColor(L"#zxytlm", 7, color));
+  EXPECT_EQ(0, FXARGB_R(color));
+  EXPECT_EQ(0, FXARGB_G(color));
+  EXPECT_EQ(0, FXARGB_B(color));
+
+  EXPECT_TRUE(FDE_ParseCSSColor(L"#000", 4, color));
+  EXPECT_EQ(0, FXARGB_R(color));
+  EXPECT_EQ(0, FXARGB_G(color));
+  EXPECT_EQ(0, FXARGB_B(color));
+
+  EXPECT_TRUE(FDE_ParseCSSColor(L"#FFF", 4, color));
+  EXPECT_EQ(255, FXARGB_R(color));
+  EXPECT_EQ(255, FXARGB_G(color));
+  EXPECT_EQ(255, FXARGB_B(color));
+
+  EXPECT_TRUE(FDE_ParseCSSColor(L"#F0F0F0", 7, color));
+  EXPECT_EQ(240, FXARGB_R(color));
+  EXPECT_EQ(240, FXARGB_G(color));
+  EXPECT_EQ(240, FXARGB_B(color));
+
+  // Upper and lower case characters.
+  EXPECT_TRUE(FDE_ParseCSSColor(L"#1b2F3c", 7, color));
+  EXPECT_EQ(27, FXARGB_R(color));
+  EXPECT_EQ(47, FXARGB_G(color));
+  EXPECT_EQ(60, FXARGB_B(color));
+}
+
+TEST(FDE_ParseCSSColor, RGBEncodingParsing) {
+  FX_ARGB color;
+
+  // Invalid input for rgb() syntax.
+  EXPECT_FALSE(FDE_ParseCSSColor(L"blahblahblah", 11, color));
+
+  EXPECT_TRUE(FDE_ParseCSSColor(L"rgb(0, 0, 0)", 12, color));
+  EXPECT_EQ(0, FXARGB_R(color));
+  EXPECT_EQ(0, FXARGB_G(color));
+  EXPECT_EQ(0, FXARGB_B(color));
+
+  EXPECT_TRUE(FDE_ParseCSSColor(L"rgb(128,255,48)", 15, color));
+  EXPECT_EQ(128, FXARGB_R(color));
+  EXPECT_EQ(255, FXARGB_G(color));
+  EXPECT_EQ(48, FXARGB_B(color));
+}
diff --git a/xfa/fde/css/fde_csssyntax.cpp b/xfa/fde/css/fde_csssyntax.cpp
index 26ddaf7..f948b58 100644
--- a/xfa/fde/css/fde_csssyntax.cpp
+++ b/xfa/fde/css/fde_csssyntax.cpp
@@ -334,7 +334,7 @@
               DisableCharset();
               if (m_iTextDatLen > 0) {
                 if (m_pStream != NULL) {
-                  uint16_t wCodePage = FX_GetCodePageFormStringW(
+                  uint16_t wCodePage = FX_GetCodePageFromStringW(
                       m_TextData.GetBuffer(), m_iTextDatLen);
                   if (wCodePage < 0xFFFF &&
                       m_pStream->GetCodePage() != wCodePage) {
diff --git a/xfa/fgas/crt/fgas_algorithm.cpp b/xfa/fgas/crt/fgas_algorithm.cpp
deleted file mode 100644
index ee557b1..0000000
--- a/xfa/fgas/crt/fgas_algorithm.cpp
+++ /dev/null
@@ -1,306 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "xfa/fgas/crt/fgas_algorithm.h"
-
-#include "core/fxcrt/include/fx_basic.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-static const FX_CHAR g_FXBase64EncoderMap[64] = {
-    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
-};
-
-struct FX_BASE64DATA {
-  uint32_t data1 : 2;
-  uint32_t data2 : 6;
-  uint32_t data3 : 4;
-  uint32_t data4 : 4;
-  uint32_t data5 : 6;
-  uint32_t data6 : 2;
-  uint32_t data7 : 8;
-};
-
-static void FX_Base64EncodePiece(const FX_BASE64DATA& src,
-                                 int32_t iBytes,
-                                 FX_CHAR dst[4]) {
-  dst[0] = g_FXBase64EncoderMap[src.data2];
-  uint32_t b = src.data1 << 4;
-  if (iBytes > 1) {
-    b |= src.data4;
-  }
-  dst[1] = g_FXBase64EncoderMap[b];
-  if (iBytes > 1) {
-    b = src.data3 << 2;
-    if (iBytes > 2) {
-      b |= src.data6;
-    }
-    dst[2] = g_FXBase64EncoderMap[b];
-    if (iBytes > 2) {
-      dst[3] = g_FXBase64EncoderMap[src.data5];
-    } else {
-      dst[3] = '=';
-    }
-  } else {
-    dst[2] = dst[3] = '=';
-  }
-}
-int32_t FX_Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, FX_CHAR* pDst) {
-  ASSERT(pSrc != NULL);
-  if (iSrcLen < 1) {
-    return 0;
-  }
-  if (pDst == NULL) {
-    int32_t iDstLen = iSrcLen / 3 * 4;
-    if ((iSrcLen % 3) != 0) {
-      iDstLen += 4;
-    }
-    return iDstLen;
-  }
-  FX_BASE64DATA srcData;
-  int32_t iBytes = 3;
-  FX_CHAR* pDstEnd = pDst;
-  while (iSrcLen > 0) {
-    if (iSrcLen > 2) {
-      ((uint8_t*)&srcData)[0] = *pSrc++;
-      ((uint8_t*)&srcData)[1] = *pSrc++;
-      ((uint8_t*)&srcData)[2] = *pSrc++;
-      iSrcLen -= 3;
-    } else {
-      *((uint32_t*)&srcData) = 0;
-      ((uint8_t*)&srcData)[0] = *pSrc++;
-      if (iSrcLen > 1) {
-        ((uint8_t*)&srcData)[1] = *pSrc++;
-      }
-      iBytes = iSrcLen;
-      iSrcLen = 0;
-    }
-    FX_Base64EncodePiece(srcData, iBytes, pDstEnd);
-    pDstEnd += 4;
-  }
-  return pDstEnd - pDst;
-}
-
-static const uint8_t g_FXBase64DecoderMap[256] = {
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
-    0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
-    0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
-    0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
-    0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
-    0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-    0xFF, 0xFF, 0xFF, 0xFF,
-};
-static void FX_Base64DecodePiece(const FX_CHAR src[4],
-                                 int32_t iChars,
-                                 FX_BASE64DATA& dst,
-                                 int32_t& iBytes) {
-  ASSERT(iChars > 0 && iChars < 5);
-  iBytes = 1;
-  dst.data2 = g_FXBase64DecoderMap[(uint8_t)src[0]];
-  if (iChars > 1) {
-    uint8_t b = g_FXBase64DecoderMap[(uint8_t)src[1]];
-    dst.data1 = b >> 4;
-    dst.data4 = b;
-    if (iChars > 2) {
-      iBytes = 2;
-      b = g_FXBase64DecoderMap[(uint8_t)src[2]];
-      dst.data3 = b >> 2;
-      dst.data6 = b;
-      if (iChars > 3) {
-        iBytes = 3;
-        dst.data5 = g_FXBase64DecoderMap[(uint8_t)src[3]];
-      } else {
-        dst.data5 = 0;
-      }
-    } else {
-      dst.data3 = 0;
-    }
-  } else {
-    dst.data1 = 0;
-  }
-}
-int32_t FX_Base64DecodeA(const FX_CHAR* pSrc, int32_t iSrcLen, uint8_t* pDst) {
-  ASSERT(pSrc != NULL);
-  if (iSrcLen < 1) {
-    return 0;
-  }
-  while (iSrcLen > 0 && pSrc[iSrcLen - 1] == '=') {
-    iSrcLen--;
-  }
-  if (iSrcLen < 1) {
-    return 0;
-  }
-  if (pDst == NULL) {
-    int32_t iDstLen = iSrcLen / 4 * 3;
-    iSrcLen %= 4;
-    if (iSrcLen == 1) {
-      iDstLen += 1;
-    } else if (iSrcLen == 2) {
-      iDstLen += 1;
-    } else if (iSrcLen == 3) {
-      iDstLen += 2;
-    }
-    return iDstLen;
-  }
-  FX_CHAR srcData[4];
-  FX_BASE64DATA dstData;
-  int32_t iChars = 4, iBytes;
-  uint8_t* pDstEnd = pDst;
-  while (iSrcLen > 0) {
-    if (iSrcLen > 3) {
-      *((uint32_t*)srcData) = *((uint32_t*)pSrc);
-      pSrc += 4;
-      iSrcLen -= 4;
-    } else {
-      *((uint32_t*)&dstData) = 0;
-      *((uint32_t*)srcData) = 0;
-      srcData[0] = *pSrc++;
-      if (iSrcLen > 1) {
-        srcData[1] = *pSrc++;
-      }
-      if (iSrcLen > 2) {
-        srcData[2] = *pSrc++;
-      }
-      iChars = iSrcLen;
-      iSrcLen = 0;
-    }
-    FX_Base64DecodePiece(srcData, iChars, dstData, iBytes);
-    *pDstEnd++ = ((uint8_t*)&dstData)[0];
-    if (iBytes > 1) {
-      *pDstEnd++ = ((uint8_t*)&dstData)[1];
-    }
-    if (iBytes > 2) {
-      *pDstEnd++ = ((uint8_t*)&dstData)[2];
-    }
-  }
-  return pDstEnd - pDst;
-}
-int32_t FX_Base64DecodeW(const FX_WCHAR* pSrc, int32_t iSrcLen, uint8_t* pDst) {
-  ASSERT(pSrc != NULL);
-  if (iSrcLen < 1) {
-    return 0;
-  }
-  while (iSrcLen > 0 && pSrc[iSrcLen - 1] == '=') {
-    iSrcLen--;
-  }
-  if (iSrcLen < 1) {
-    return 0;
-  }
-  if (pDst == NULL) {
-    int32_t iDstLen = iSrcLen / 4 * 3;
-    iSrcLen %= 4;
-    if (iSrcLen == 1) {
-      iDstLen += 1;
-    } else if (iSrcLen == 2) {
-      iDstLen += 1;
-    } else if (iSrcLen == 3) {
-      iDstLen += 2;
-    }
-    return iDstLen;
-  }
-  FX_CHAR srcData[4];
-  FX_BASE64DATA dstData;
-  int32_t iChars = 4, iBytes;
-  uint8_t* pDstEnd = pDst;
-  while (iSrcLen > 0) {
-    if (iSrcLen > 3) {
-      srcData[0] = (FX_CHAR)*pSrc++;
-      srcData[1] = (FX_CHAR)*pSrc++;
-      srcData[2] = (FX_CHAR)*pSrc++;
-      srcData[3] = (FX_CHAR)*pSrc++;
-      iSrcLen -= 4;
-    } else {
-      *((uint32_t*)&dstData) = 0;
-      *((uint32_t*)srcData) = 0;
-      srcData[0] = (FX_CHAR)*pSrc++;
-      if (iSrcLen > 1) {
-        srcData[1] = (FX_CHAR)*pSrc++;
-      }
-      if (iSrcLen > 2) {
-        srcData[2] = (FX_CHAR)*pSrc++;
-      }
-      iChars = iSrcLen;
-      iSrcLen = 0;
-    }
-    FX_Base64DecodePiece(srcData, iChars, dstData, iBytes);
-    *pDstEnd++ = ((uint8_t*)&dstData)[0];
-    if (iBytes > 1) {
-      *pDstEnd++ = ((uint8_t*)&dstData)[1];
-    }
-    if (iBytes > 2) {
-      *pDstEnd++ = ((uint8_t*)&dstData)[2];
-    }
-  }
-  return pDstEnd - pDst;
-}
-
-static const uint8_t g_FXHex2DecMap[256] = {
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  1,  2,  3, 4, 5, 6, 7, 8, 9,  0,  0,
-    0,  0,  0,  0, 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 10, 11, 12,
-    13, 14, 15, 0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0,  0,  0,
-    0,  0,  0,  0, 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0,
-};
-uint8_t FX_Hex2Dec(uint8_t hexHigh, uint8_t hexLow) {
-  return (g_FXHex2DecMap[hexHigh] << 4) + g_FXHex2DecMap[hexLow];
-}
-int32_t FX_SeparateStringW(const FX_WCHAR* pStr,
-                           int32_t iStrLen,
-                           FX_WCHAR delimiter,
-                           CFX_WideStringArray& pieces) {
-  if (pStr == NULL) {
-    return 0;
-  }
-  if (iStrLen < 0) {
-    iStrLen = FXSYS_wcslen(pStr);
-  }
-  const FX_WCHAR* pToken = pStr;
-  const FX_WCHAR* pEnd = pStr + iStrLen;
-  while (TRUE) {
-    if (pStr >= pEnd || delimiter == *pStr) {
-      CFX_WideString sub(pToken, pStr - pToken);
-      pieces.Add(sub);
-      pToken = pStr + 1;
-      if (pStr >= pEnd) {
-        break;
-      }
-    }
-    pStr++;
-  }
-  return pieces.GetSize();
-}
-#ifdef __cplusplus
-}
-#endif
diff --git a/xfa/fgas/crt/fgas_algorithm.h b/xfa/fgas/crt/fgas_algorithm.h
index 9fefb2b..f68b53f 100644
--- a/xfa/fgas/crt/fgas_algorithm.h
+++ b/xfa/fgas/crt/fgas_algorithm.h
@@ -11,41 +11,24 @@
 
 #include "core/fxcrt/include/fx_basic.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int32_t FX_Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, FX_CHAR* pDst);
-int32_t FX_Base64DecodeA(const FX_CHAR* pSrc, int32_t iSrcLen, uint8_t* pDst);
-int32_t FX_Base64DecodeW(const FX_WCHAR* pSrc, int32_t iSrcLen, uint8_t* pDst);
-uint8_t FX_Hex2Dec(uint8_t hexHigh, uint8_t hexLow);
-int32_t FX_SeparateStringW(const FX_WCHAR* pStr,
-                           int32_t iStrLen,
-                           FX_WCHAR delimiter,
-                           CFX_WideStringArray& pieces);
-#ifdef __cplusplus
-};
-#endif
-
 template <class baseType>
 class CFX_DSPATemplate {
  public:
   int32_t Lookup(const baseType& find, const baseType* pArray, int32_t iCount) {
-    ASSERT(pArray != NULL);
-    if (iCount < 1) {
+    ASSERT(pArray);
+    if (iCount < 1)
       return -1;
-    }
+
     int32_t iStart = 0, iEnd = iCount - 1, iMid;
     do {
       iMid = (iStart + iEnd) / 2;
       const baseType& v = pArray[iMid];
-      if (find == v) {
+      if (find == v)
         return iMid;
-      } else if (find < v) {
+      if (find < v)
         iEnd = iMid - 1;
-      } else {
+      else
         iStart = iMid + 1;
-      }
     } while (iStart <= iEnd);
     return -1;
   }
diff --git a/xfa/fgas/crt/fgas_codepage.cpp b/xfa/fgas/crt/fgas_codepage.cpp
index 7362a89..2087cac 100644
--- a/xfa/fgas/crt/fgas_codepage.cpp
+++ b/xfa/fgas/crt/fgas_codepage.cpp
@@ -8,7 +8,24 @@
 #include "xfa/fgas/crt/fgas_codepage.h"
 #include "xfa/fgas/crt/fgas_language.h"
 
-static const FX_CHARSET_MAP g_FXCharset2CodePageTable[] = {
+namespace {
+
+struct FX_STR2CPHASH {
+  uint32_t uHash;
+  uint16_t uCodePage;
+};
+
+struct FX_CHARSET_MAP {
+  uint16_t charset;
+  uint16_t codepage;
+};
+
+struct FX_LANG2CPMAP {
+  uint16_t wLanguage;
+  uint16_t wCodepage;
+};
+
+const FX_CHARSET_MAP g_FXCharset2CodePageTable[] = {
     {0, 1252},   {1, 0},      {2, 42},     {77, 10000}, {78, 10001},
     {79, 10003}, {80, 10008}, {81, 10002}, {83, 10005}, {84, 10004},
     {85, 10006}, {86, 10081}, {87, 10021}, {88, 10029}, {89, 10007},
@@ -17,24 +34,8 @@
     {186, 1257}, {204, 1251}, {222, 874},  {238, 1250}, {254, 437},
     {255, 850},
 };
-uint16_t FX_GetCodePageFromCharset(uint8_t charset) {
-  int32_t iEnd = sizeof(g_FXCharset2CodePageTable) / sizeof(FX_CHARSET_MAP) - 1;
-  ASSERT(iEnd >= 0);
-  int32_t iStart = 0, iMid;
-  do {
-    iMid = (iStart + iEnd) / 2;
-    const FX_CHARSET_MAP& cp = g_FXCharset2CodePageTable[iMid];
-    if (charset == cp.charset) {
-      return cp.codepage;
-    } else if (charset < cp.charset) {
-      iEnd = iMid - 1;
-    } else {
-      iStart = iMid + 1;
-    }
-  } while (iStart <= iEnd);
-  return 0xFFFF;
-}
-static const FX_CHARSET_MAP g_FXCodepage2CharsetTable[] = {
+
+const FX_CHARSET_MAP g_FXCodepage2CharsetTable[] = {
     {1, 0},      {2, 42},     {254, 437},  {255, 850},  {222, 874},
     {128, 932},  {134, 936},  {129, 949},  {136, 950},  {238, 1250},
     {204, 1251}, {0, 1252},   {161, 1253}, {162, 1254}, {177, 1255},
@@ -43,23 +44,7 @@
     {84, 10004}, {85, 10006}, {86, 10081}, {87, 10021}, {88, 10029},
     {89, 10007},
 };
-uint16_t FX_GetCharsetFromCodePage(uint16_t codepage) {
-  int32_t iEnd = sizeof(g_FXCodepage2CharsetTable) / sizeof(FX_CHARSET_MAP) - 1;
-  ASSERT(iEnd >= 0);
-  int32_t iStart = 0, iMid;
-  do {
-    iMid = (iStart + iEnd) / 2;
-    const FX_CHARSET_MAP& cp = g_FXCodepage2CharsetTable[iMid];
-    if (codepage == cp.codepage) {
-      return cp.charset;
-    } else if (codepage < cp.codepage) {
-      iEnd = iMid - 1;
-    } else {
-      iStart = iMid + 1;
-    }
-  } while (iStart <= iEnd);
-  return 0xFFFF;
-}
+
 const FX_LANG2CPMAP g_FXLang2CodepageTable[] = {
     {FX_LANG_Arabic_SaudiArabia, FX_CODEPAGE_MSWin_Arabic},
     {FX_LANG_Bulgarian_Bulgaria, FX_CODEPAGE_MSWin_Cyrillic},
@@ -197,24 +182,8 @@
     {FX_LANG_Spanish_Nicaragua, FX_CODEPAGE_MSWin_WesternEuropean},
     {FX_LANG_Spanish_PuertoRico, FX_CODEPAGE_MSWin_WesternEuropean},
 };
-uint16_t FX_GetDefCodePageByLanguage(uint16_t wLanguage) {
-  int32_t iEnd = sizeof(g_FXLang2CodepageTable) / sizeof(FX_LANG2CPMAP) - 1;
-  ASSERT(iEnd >= 0);
-  int32_t iStart = 0, iMid;
-  do {
-    iMid = (iStart + iEnd) / 2;
-    const FX_LANG2CPMAP& cp = g_FXLang2CodepageTable[iMid];
-    if (wLanguage == cp.wLanguage) {
-      return cp.wCodepage;
-    } else if (wLanguage < cp.wLanguage) {
-      iEnd = iMid - 1;
-    } else {
-      iStart = iMid + 1;
-    }
-  } while (iStart <= iEnd);
-  return 0xFFFF;
-}
-static const FX_STR2CPHASH g_FXCPHashTable[] = {
+
+const FX_STR2CPHASH g_FXCPHashTable[] = {
     {0xd45, 0x6faf},      {0xd46, 0x6fb0},      {0xd47, 0x6fb1},
     {0xd48, 0x6fb2},      {0xd49, 0x4e6},       {0xd4d, 0x6fbd},
     {0xe9e, 0x4e4},       {0xc998, 0x1b5},      {0x18ef0, 0x3a8},
@@ -301,7 +270,8 @@
     {0xf3d463c2, 0x3a4},  {0xf52a70a3, 0xc42e}, {0xf5693147, 0x6fb3},
     {0xf637e157, 0x478},  {0xfc213f3a, 0x2717}, {0xff654d14, 0x3b5},
 };
-uint16_t FX_GetCodePageFromStringA(const FX_CHAR* pStr, int32_t iLength) {
+
+uint16_t GetCodePageFromStringA(const FX_CHAR* pStr, int32_t iLength) {
   ASSERT(pStr != NULL);
   if (iLength < 0) {
     iLength = FXSYS_strlen(pStr);
@@ -326,7 +296,64 @@
   } while (iStart <= iEnd);
   return 0xFFFF;
 }
-uint16_t FX_GetCodePageFormStringW(const FX_WCHAR* pStr, int32_t iLength) {
+
+}  // namespace
+
+uint16_t FX_GetCodePageFromCharset(uint8_t charset) {
+  int32_t iEnd = sizeof(g_FXCharset2CodePageTable) / sizeof(FX_CHARSET_MAP) - 1;
+  ASSERT(iEnd >= 0);
+  int32_t iStart = 0, iMid;
+  do {
+    iMid = (iStart + iEnd) / 2;
+    const FX_CHARSET_MAP& cp = g_FXCharset2CodePageTable[iMid];
+    if (charset == cp.charset) {
+      return cp.codepage;
+    } else if (charset < cp.charset) {
+      iEnd = iMid - 1;
+    } else {
+      iStart = iMid + 1;
+    }
+  } while (iStart <= iEnd);
+  return 0xFFFF;
+}
+
+uint16_t FX_GetCharsetFromCodePage(uint16_t codepage) {
+  int32_t iEnd = sizeof(g_FXCodepage2CharsetTable) / sizeof(FX_CHARSET_MAP) - 1;
+  ASSERT(iEnd >= 0);
+  int32_t iStart = 0, iMid;
+  do {
+    iMid = (iStart + iEnd) / 2;
+    const FX_CHARSET_MAP& cp = g_FXCodepage2CharsetTable[iMid];
+    if (codepage == cp.codepage) {
+      return cp.charset;
+    } else if (codepage < cp.codepage) {
+      iEnd = iMid - 1;
+    } else {
+      iStart = iMid + 1;
+    }
+  } while (iStart <= iEnd);
+  return 0xFFFF;
+}
+
+uint16_t FX_GetDefCodePageByLanguage(uint16_t wLanguage) {
+  int32_t iEnd = sizeof(g_FXLang2CodepageTable) / sizeof(FX_LANG2CPMAP) - 1;
+  ASSERT(iEnd >= 0);
+  int32_t iStart = 0, iMid;
+  do {
+    iMid = (iStart + iEnd) / 2;
+    const FX_LANG2CPMAP& cp = g_FXLang2CodepageTable[iMid];
+    if (wLanguage == cp.wLanguage) {
+      return cp.wCodepage;
+    } else if (wLanguage < cp.wLanguage) {
+      iEnd = iMid - 1;
+    } else {
+      iStart = iMid + 1;
+    }
+  } while (iStart <= iEnd);
+  return 0xFFFF;
+}
+
+uint16_t FX_GetCodePageFromStringW(const FX_WCHAR* pStr, int32_t iLength) {
   if (iLength < 0) {
     iLength = FXSYS_wcslen(pStr);
   }
@@ -339,5 +366,141 @@
     *pBuf++ = (FX_CHAR)*pStr++;
   }
   csStr.ReleaseBuffer(iLength);
-  return FX_GetCodePageFromStringA(csStr.c_str(), iLength);
+  return GetCodePageFromStringA(csStr.c_str(), iLength);
+}
+
+void FX_SwapByteOrder(FX_WCHAR* pStr, int32_t iLength) {
+  ASSERT(pStr != NULL);
+  if (iLength < 0) {
+    iLength = FXSYS_wcslen(pStr);
+  }
+  uint16_t wch;
+  if (sizeof(FX_WCHAR) > 2) {
+    while (iLength-- > 0) {
+      wch = (uint16_t)*pStr;
+      wch = (wch >> 8) | (wch << 8);
+      wch &= 0x00FF;
+      *pStr++ = wch;
+    }
+  } else {
+    while (iLength-- > 0) {
+      wch = (uint16_t)*pStr;
+      wch = (wch >> 8) | (wch << 8);
+      *pStr++ = wch;
+    }
+  }
+}
+
+void FX_UTF16ToWChar(void* pBuffer, int32_t iLength) {
+  ASSERT(pBuffer != NULL && iLength > 0);
+  if (sizeof(FX_WCHAR) == 2) {
+    return;
+  }
+  uint16_t* pSrc = (uint16_t*)pBuffer;
+  FX_WCHAR* pDst = (FX_WCHAR*)pBuffer;
+  while (--iLength >= 0) {
+    pDst[iLength] = (FX_WCHAR)pSrc[iLength];
+  }
+}
+
+void FX_WCharToUTF16(void* pBuffer, int32_t iLength) {
+  ASSERT(pBuffer != NULL && iLength > 0);
+  if (sizeof(FX_WCHAR) == 2) {
+    return;
+  }
+  const FX_WCHAR* pSrc = (const FX_WCHAR*)pBuffer;
+  uint16_t* pDst = (uint16_t*)pBuffer;
+  while (--iLength >= 0) {
+    *pDst++ = (uint16_t)*pSrc++;
+  }
+}
+
+int32_t FX_DecodeString(uint16_t wCodePage,
+                        const FX_CHAR* pSrc,
+                        int32_t* pSrcLen,
+                        FX_WCHAR* pDst,
+                        int32_t* pDstLen,
+                        FX_BOOL bErrBreak) {
+  if (wCodePage == FX_CODEPAGE_UTF8) {
+    return FX_UTF8Decode(pSrc, pSrcLen, pDst, pDstLen);
+  }
+  return -1;
+}
+int32_t FX_UTF8Decode(const FX_CHAR* pSrc,
+                      int32_t* pSrcLen,
+                      FX_WCHAR* pDst,
+                      int32_t* pDstLen) {
+  if (pSrcLen == NULL || pDstLen == NULL) {
+    return -1;
+  }
+  int32_t iSrcLen = *pSrcLen;
+  if (iSrcLen < 1) {
+    *pSrcLen = *pDstLen = 0;
+    return 1;
+  }
+  int32_t iDstLen = *pDstLen;
+  FX_BOOL bValidDst = (pDst != NULL && iDstLen > 0);
+  uint32_t dwCode = 0;
+  int32_t iPending = 0;
+  int32_t iSrcNum = 0, iDstNum = 0;
+  int32_t k = 0;
+  int32_t iIndex = 0;
+  k = 1;
+  while (iIndex < iSrcLen) {
+    uint8_t byte = (uint8_t) * (pSrc + iIndex);
+    if (byte < 0x80) {
+      iPending = 0;
+      k = 1;
+      iDstNum++;
+      iSrcNum += k;
+      if (bValidDst) {
+        *pDst++ = byte;
+        if (iDstNum >= iDstLen) {
+          break;
+        }
+      }
+    } else if (byte < 0xc0) {
+      if (iPending < 1) {
+        break;
+      }
+      iPending--;
+      dwCode |= (byte & 0x3f) << (iPending * 6);
+      if (iPending == 0) {
+        iDstNum++;
+        iSrcNum += k;
+        if (bValidDst) {
+          *pDst++ = dwCode;
+          if (iDstNum >= iDstLen) {
+            break;
+          }
+        }
+      }
+    } else if (byte < 0xe0) {
+      iPending = 1;
+      k = 2;
+      dwCode = (byte & 0x1f) << 6;
+    } else if (byte < 0xf0) {
+      iPending = 2;
+      k = 3;
+      dwCode = (byte & 0x0f) << 12;
+    } else if (byte < 0xf8) {
+      iPending = 3;
+      k = 4;
+      dwCode = (byte & 0x07) << 18;
+    } else if (byte < 0xfc) {
+      iPending = 4;
+      k = 5;
+      dwCode = (byte & 0x03) << 24;
+    } else if (byte < 0xfe) {
+      iPending = 5;
+      k = 6;
+      dwCode = (byte & 0x01) << 30;
+    } else {
+      break;
+    }
+    iIndex++;
+  }
+  *pSrcLen = iSrcNum;
+  *pDstLen = iDstNum;
+  return 1;
 }
diff --git a/xfa/fgas/crt/fgas_codepage.h b/xfa/fgas/crt/fgas_codepage.h
index 15bdd74..5d867fe 100644
--- a/xfa/fgas/crt/fgas_codepage.h
+++ b/xfa/fgas/crt/fgas_codepage.h
@@ -135,21 +135,12 @@
 
 uint16_t FX_GetCodePageFromCharset(uint8_t charset);
 uint16_t FX_GetCharsetFromCodePage(uint16_t codepage);
-uint16_t FX_GetCodePageFromStringA(const FX_CHAR* pStr, int32_t iLength);
-uint16_t FX_GetCodePageFormStringW(const FX_WCHAR* pStr, int32_t iLength);
+uint16_t FX_GetCodePageFromStringW(const FX_WCHAR* pStr, int32_t iLength);
 uint16_t FX_GetDefCodePageByLanguage(uint16_t wLanguage);
 void FX_SwapByteOrder(FX_WCHAR* pStr, int32_t iLength);
-void FX_SwapByteOrderCopy(const FX_WCHAR* pSrc,
-                          FX_WCHAR* pDst,
-                          int32_t iLength);
+
 void FX_UTF16ToWChar(void* pBuffer, int32_t iLength);
-void FX_UTF16ToWCharCopy(const uint16_t* pUTF16,
-                         FX_WCHAR* pWChar,
-                         int32_t iLength);
 void FX_WCharToUTF16(void* pBuffer, int32_t iLength);
-void FX_WCharToUTF16Copy(const FX_WCHAR* pWChar,
-                         uint16_t* pUTF16,
-                         int32_t iLength);
 int32_t FX_DecodeString(uint16_t wCodePage,
                         const FX_CHAR* pSrc,
                         int32_t* pSrcLen,
@@ -161,19 +152,4 @@
                       FX_WCHAR* pDst,
                       int32_t* pDstLen);
 
-struct FX_STR2CPHASH {
-  uint32_t uHash;
-  uint16_t uCodePage;
-};
-
-struct FX_CHARSET_MAP {
-  uint16_t charset;
-  uint16_t codepage;
-};
-
-struct FX_LANG2CPMAP {
-  uint16_t wLanguage;
-  uint16_t wCodepage;
-};
-
 #endif  // XFA_FGAS_CRT_FGAS_CODEPAGE_H_
diff --git a/xfa/fgas/crt/fgas_encode.cpp b/xfa/fgas/crt/fgas_encode.cpp
deleted file mode 100644
index b84406f..0000000
--- a/xfa/fgas/crt/fgas_encode.cpp
+++ /dev/null
@@ -1,193 +0,0 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "xfa/fgas/crt/fgas_codepage.h"
-
-void FX_SwapByteOrder(FX_WCHAR* pStr, int32_t iLength) {
-  ASSERT(pStr != NULL);
-  if (iLength < 0) {
-    iLength = FXSYS_wcslen(pStr);
-  }
-  uint16_t wch;
-  if (sizeof(FX_WCHAR) > 2) {
-    while (iLength-- > 0) {
-      wch = (uint16_t)*pStr;
-      wch = (wch >> 8) | (wch << 8);
-      wch &= 0x00FF;
-      *pStr++ = wch;
-    }
-  } else {
-    while (iLength-- > 0) {
-      wch = (uint16_t)*pStr;
-      wch = (wch >> 8) | (wch << 8);
-      *pStr++ = wch;
-    }
-  }
-}
-void FX_SwapByteOrderCopy(const FX_WCHAR* pSrc,
-                          FX_WCHAR* pDst,
-                          int32_t iLength) {
-  ASSERT(pSrc != NULL && pDst != NULL);
-  if (iLength < 0) {
-    iLength = FXSYS_wcslen(pSrc);
-  }
-  uint16_t wch;
-  if (sizeof(FX_WCHAR) > 2) {
-    while (iLength-- > 0) {
-      wch = (uint16_t)*pSrc++;
-      wch = (wch >> 8) | (wch << 8);
-      wch &= 0x00FF;
-      *pDst++ = wch;
-    }
-  } else {
-    while (iLength-- > 0) {
-      wch = (uint16_t)*pSrc++;
-      wch = (wch >> 8) | (wch << 8);
-      *pDst++ = wch;
-    }
-  }
-}
-void FX_UTF16ToWChar(void* pBuffer, int32_t iLength) {
-  ASSERT(pBuffer != NULL && iLength > 0);
-  if (sizeof(FX_WCHAR) == 2) {
-    return;
-  }
-  uint16_t* pSrc = (uint16_t*)pBuffer;
-  FX_WCHAR* pDst = (FX_WCHAR*)pBuffer;
-  while (--iLength >= 0) {
-    pDst[iLength] = (FX_WCHAR)pSrc[iLength];
-  }
-}
-void FX_UTF16ToWCharCopy(const uint16_t* pUTF16,
-                         FX_WCHAR* pWChar,
-                         int32_t iLength) {
-  ASSERT(pUTF16 != NULL && pWChar != NULL && iLength > 0);
-  if (sizeof(FX_WCHAR) == 2) {
-    FXSYS_memcpy(pWChar, pUTF16, iLength * sizeof(FX_WCHAR));
-  } else {
-    while (--iLength >= 0) {
-      pWChar[iLength] = (FX_WCHAR)pUTF16[iLength];
-    }
-  }
-}
-void FX_WCharToUTF16(void* pBuffer, int32_t iLength) {
-  ASSERT(pBuffer != NULL && iLength > 0);
-  if (sizeof(FX_WCHAR) == 2) {
-    return;
-  }
-  const FX_WCHAR* pSrc = (const FX_WCHAR*)pBuffer;
-  uint16_t* pDst = (uint16_t*)pBuffer;
-  while (--iLength >= 0) {
-    *pDst++ = (uint16_t)*pSrc++;
-  }
-}
-void FX_WCharToUTF16Copy(const FX_WCHAR* pWChar,
-                         uint16_t* pUTF16,
-                         int32_t iLength) {
-  ASSERT(pWChar != NULL && pUTF16 != NULL && iLength > 0);
-  if (sizeof(FX_WCHAR) == 2) {
-    FXSYS_memcpy(pUTF16, pWChar, iLength * sizeof(FX_WCHAR));
-  } else {
-    while (--iLength >= 0) {
-      *pUTF16++ = (uint16_t)*pWChar++;
-    }
-  }
-}
-inline uint32_t FX_DWordFromBytes(const uint8_t* pStr) {
-  return FXBSTR_ID(pStr[3], pStr[2], pStr[1], pStr[0]);
-}
-inline uint16_t FX_WordFromBytes(const uint8_t* pStr) {
-  return (pStr[1] << 8 | pStr[0]);
-}
-int32_t FX_DecodeString(uint16_t wCodePage,
-                        const FX_CHAR* pSrc,
-                        int32_t* pSrcLen,
-                        FX_WCHAR* pDst,
-                        int32_t* pDstLen,
-                        FX_BOOL bErrBreak) {
-  if (wCodePage == FX_CODEPAGE_UTF8) {
-    return FX_UTF8Decode(pSrc, pSrcLen, pDst, pDstLen);
-  }
-  return -1;
-}
-int32_t FX_UTF8Decode(const FX_CHAR* pSrc,
-                      int32_t* pSrcLen,
-                      FX_WCHAR* pDst,
-                      int32_t* pDstLen) {
-  if (pSrcLen == NULL || pDstLen == NULL) {
-    return -1;
-  }
-  int32_t iSrcLen = *pSrcLen;
-  if (iSrcLen < 1) {
-    *pSrcLen = *pDstLen = 0;
-    return 1;
-  }
-  int32_t iDstLen = *pDstLen;
-  FX_BOOL bValidDst = (pDst != NULL && iDstLen > 0);
-  uint32_t dwCode = 0;
-  int32_t iPending = 0;
-  int32_t iSrcNum = 0, iDstNum = 0;
-  int32_t k = 0;
-  int32_t iIndex = 0;
-  k = 1;
-  while (iIndex < iSrcLen) {
-    uint8_t byte = (uint8_t) * (pSrc + iIndex);
-    if (byte < 0x80) {
-      iPending = 0;
-      k = 1;
-      iDstNum++;
-      iSrcNum += k;
-      if (bValidDst) {
-        *pDst++ = byte;
-        if (iDstNum >= iDstLen) {
-          break;
-        }
-      }
-    } else if (byte < 0xc0) {
-      if (iPending < 1) {
-        break;
-      }
-      iPending--;
-      dwCode |= (byte & 0x3f) << (iPending * 6);
-      if (iPending == 0) {
-        iDstNum++;
-        iSrcNum += k;
-        if (bValidDst) {
-          *pDst++ = dwCode;
-          if (iDstNum >= iDstLen) {
-            break;
-          }
-        }
-      }
-    } else if (byte < 0xe0) {
-      iPending = 1;
-      k = 2;
-      dwCode = (byte & 0x1f) << 6;
-    } else if (byte < 0xf0) {
-      iPending = 2;
-      k = 3;
-      dwCode = (byte & 0x0f) << 12;
-    } else if (byte < 0xf8) {
-      iPending = 3;
-      k = 4;
-      dwCode = (byte & 0x07) << 18;
-    } else if (byte < 0xfc) {
-      iPending = 4;
-      k = 5;
-      dwCode = (byte & 0x03) << 24;
-    } else if (byte < 0xfe) {
-      iPending = 5;
-      k = 6;
-      dwCode = (byte & 0x01) << 30;
-    } else {
-      break;
-    }
-    iIndex++;
-  }
-  *pSrcLen = iSrcNum;
-  *pDstLen = iDstNum;
-  return 1;
-}
diff --git a/xfa/fgas/crt/fgas_memory.cpp b/xfa/fgas/crt/fgas_memory.cpp
index 9176466..d218f42 100644
--- a/xfa/fgas/crt/fgas_memory.cpp
+++ b/xfa/fgas/crt/fgas_memory.cpp
@@ -20,6 +20,7 @@
  public:
   CFX_DefStore() {}
   ~CFX_DefStore() override {}
+
   void* Alloc(size_t size) override { return FX_Alloc(uint8_t, size); }
   void Free(void* pBlock) override { FX_Free(pBlock); }
 };
@@ -44,12 +45,13 @@
 
 class CFX_StaticStore : public IFX_MemoryAllocator, public CFX_Target {
  public:
-  CFX_StaticStore(size_t iDefChunkSize = 4096);
+  CFX_StaticStore(size_t iDefChunkSize);
   ~CFX_StaticStore() override;
+
   void* Alloc(size_t size) override;
   void Free(void* pBlock) override {}
 
- protected:
+ private:
   size_t m_iAllocatedSize;
   size_t m_iDefChunkSize;
   FX_STATICSTORECHUNK* m_pChunk;
@@ -74,7 +76,7 @@
   void* Alloc(size_t size) override;
   void Free(void* pBlock) override;
 
- protected:
+ private:
   FX_FIXEDSTORECHUNK* AllocChunk();
 
   size_t m_iBlockSize;
diff --git a/xfa/fgas/crt/fgas_stream.cpp b/xfa/fgas/crt/fgas_stream.cpp
index 73e5a02..59eb99b 100644
--- a/xfa/fgas/crt/fgas_stream.cpp
+++ b/xfa/fgas/crt/fgas_stream.cpp
@@ -6,6 +6,11 @@
 
 #include "xfa/fgas/crt/fgas_stream.h"
 
+#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
+    _FX_OS_ == _FX_WIN64_
+#include <io.h>
+#endif
+
 #include <algorithm>
 #include <memory>
 
@@ -299,38 +304,34 @@
   IFX_Stream* m_pStream;
 };
 
-class CFX_BufferAccImp : public IFX_FileRead {
- public:
-  CFX_BufferAccImp(IFX_BufferRead* pBufferRead,
-                   FX_FILESIZE iFileSize,
-                   FX_BOOL bReleaseStream);
-  virtual ~CFX_BufferAccImp();
-  virtual void Release() { delete this; }
-  virtual FX_FILESIZE GetSize();
-  virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size);
+int32_t FileLength(FXSYS_FILE* file) {
+  ASSERT(file != NULL);
+#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_
+  return _filelength(_fileno(file));
+#else
+  int32_t iPos = FXSYS_ftell(file);
+  FXSYS_fseek(file, 0, FXSYS_SEEK_END);
+  int32_t iLen = FXSYS_ftell(file);
+  FXSYS_fseek(file, iPos, FXSYS_SEEK_SET);
+  return iLen;
+#endif
+}
 
- protected:
-  IFX_BufferRead* m_pBufferRead;
-  FX_BOOL m_bReleaseStream;
-  FX_FILESIZE m_iBufSize;
-};
-
-class CFGAS_FileWrite : public IFX_FileWrite {
- public:
-  CFGAS_FileWrite(IFX_Stream* pStream, FX_BOOL bReleaseStream);
-  virtual ~CFGAS_FileWrite();
-  virtual void Release() { delete this; }
-  virtual FX_FILESIZE GetSize();
-  virtual FX_BOOL Flush();
-  virtual FX_BOOL WriteBlock(const void* pData, size_t size);
-  virtual FX_BOOL WriteBlock(const void* pData,
-                             FX_FILESIZE offset,
-                             size_t size);
-
- protected:
-  IFX_Stream* m_pStream;
-  FX_BOOL m_bReleaseStream;
-};
+FX_BOOL FileSetSize(FXSYS_FILE* file, int32_t size) {
+  ASSERT(file != NULL);
+#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_
+  return _chsize(_fileno(file), size) == 0;
+#elif _FX_OS_ == _FX_WIN32_MOBILE_
+  HANDLE hFile = _fileno(file);
+  uint32_t dwPos = ::SetFilePointer(hFile, 0, 0, FILE_CURRENT);
+  ::SetFilePointer(hFile, size, 0, FILE_BEGIN);
+  FX_BOOL bRet = ::SetEndOfFile(hFile);
+  ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN);
+  return bRet;
+#else
+  return FALSE;
+#endif
+}
 
 }  // namespace
 
@@ -439,7 +440,7 @@
           return FALSE;
 
         if (dwAccess & FX_STREAMACCESS_Truncate)
-          FX_fsetsize(m_hFile, 0);
+          FileSetSize(m_hFile, 0);
       }
     } else {
       return FALSE;
@@ -469,7 +470,7 @@
           return FALSE;
         }
         if (dwAccess & FX_STREAMACCESS_Truncate) {
-          FX_fsetsize(m_hFile, 0);
+          FileSetSize(m_hFile, 0);
         }
       }
     } else {
@@ -482,7 +483,7 @@
       (FX_STREAMACCESS_Write | FX_STREAMACCESS_Truncate)) {
     m_iLength = 0;
   } else {
-    m_iLength = FX_filelength(m_hFile);
+    m_iLength = FileLength(m_hFile);
   }
   return TRUE;
 }
@@ -564,8 +565,8 @@
 }
 FX_BOOL CFX_FileStreamImp::SetLength(int32_t iLength) {
   ASSERT(m_hFile && (GetAccessModes() & FX_STREAMACCESS_Write) != 0);
-  FX_BOOL bRet = FX_fsetsize(m_hFile, iLength);
-  m_iLength = FX_filelength(m_hFile);
+  FX_BOOL bRet = FileSetSize(m_hFile, iLength);
+  m_iLength = FileLength(m_hFile);
   return bRet;
 }
 CFX_FileReadStreamImp::CFX_FileReadStreamImp()
@@ -1509,131 +1510,3 @@
   int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size);
   return iLen == (int32_t)size;
 }
-
-IFX_FileRead* FX_CreateFileRead(IFX_BufferRead* pBufferRead,
-                                FX_FILESIZE iFileSize,
-                                FX_BOOL bReleaseStream) {
-  if (!pBufferRead) {
-    return NULL;
-  }
-  return new CFX_BufferAccImp(pBufferRead, iFileSize, bReleaseStream);
-}
-CFX_BufferAccImp::CFX_BufferAccImp(IFX_BufferRead* pBufferRead,
-                                   FX_FILESIZE iFileSize,
-                                   FX_BOOL bReleaseStream)
-    : m_pBufferRead(pBufferRead),
-      m_bReleaseStream(bReleaseStream),
-      m_iBufSize(iFileSize) {
-  ASSERT(m_pBufferRead);
-}
-CFX_BufferAccImp::~CFX_BufferAccImp() {
-  if (m_bReleaseStream && m_pBufferRead) {
-    m_pBufferRead->Release();
-  }
-}
-FX_FILESIZE CFX_BufferAccImp::GetSize() {
-  if (!m_pBufferRead) {
-    return 0;
-  }
-  if (m_iBufSize >= 0) {
-    return m_iBufSize;
-  }
-  if (!m_pBufferRead->ReadNextBlock(TRUE)) {
-    return 0;
-  }
-  m_iBufSize = (FX_FILESIZE)m_pBufferRead->GetBlockSize();
-  while (!m_pBufferRead->IsEOF()) {
-    m_pBufferRead->ReadNextBlock(FALSE);
-    m_iBufSize += (FX_FILESIZE)m_pBufferRead->GetBlockSize();
-  }
-  return m_iBufSize;
-}
-FX_BOOL CFX_BufferAccImp::ReadBlock(void* buffer,
-                                    FX_FILESIZE offset,
-                                    size_t size) {
-  if (!m_pBufferRead) {
-    return FALSE;
-  }
-  if (!buffer || !size) {
-    return TRUE;
-  }
-  FX_FILESIZE dwBufSize = GetSize();
-  if (offset >= dwBufSize) {
-    return FALSE;
-  }
-  size_t dwBlockSize = m_pBufferRead->GetBlockSize();
-  FX_FILESIZE dwBlockOffset = m_pBufferRead->GetBlockOffset();
-  if (offset < dwBlockOffset) {
-    if (!m_pBufferRead->ReadNextBlock(TRUE)) {
-      return FALSE;
-    }
-    dwBlockSize = m_pBufferRead->GetBlockSize();
-    dwBlockOffset = m_pBufferRead->GetBlockOffset();
-  }
-  while (offset < dwBlockOffset ||
-         offset >= (FX_FILESIZE)(dwBlockOffset + dwBlockSize)) {
-    if (m_pBufferRead->IsEOF() || !m_pBufferRead->ReadNextBlock(FALSE)) {
-      break;
-    }
-    dwBlockSize = m_pBufferRead->GetBlockSize();
-    dwBlockOffset = m_pBufferRead->GetBlockOffset();
-  }
-  if (offset < dwBlockOffset ||
-      offset >= (FX_FILESIZE)(dwBlockOffset + dwBlockSize)) {
-    return FALSE;
-  }
-  const uint8_t* pBuffer = m_pBufferRead->GetBlockBuffer();
-  const FX_FILESIZE dwOffset = offset - dwBlockOffset;
-  size_t dwCopySize =
-      std::min(size, static_cast<size_t>(dwBlockSize - dwOffset));
-  FXSYS_memcpy(buffer, pBuffer + dwOffset, dwCopySize);
-  offset = dwCopySize;
-  size -= dwCopySize;
-  while (size) {
-    if (!m_pBufferRead->ReadNextBlock(FALSE)) {
-      break;
-    }
-    dwBlockOffset = m_pBufferRead->GetBlockOffset();
-    dwBlockSize = m_pBufferRead->GetBlockSize();
-    pBuffer = m_pBufferRead->GetBlockBuffer();
-    dwCopySize = std::min(size, dwBlockSize);
-    FXSYS_memcpy(((uint8_t*)buffer) + offset, pBuffer, dwCopySize);
-    offset += dwCopySize;
-    size -= dwCopySize;
-  }
-  return TRUE;
-}
-
-IFX_FileWrite* FX_CreateFileWrite(IFX_Stream* pBaseStream,
-                                  FX_BOOL bReleaseStream) {
-  ASSERT(pBaseStream != NULL);
-  return new CFGAS_FileWrite(pBaseStream, bReleaseStream);
-}
-
-CFGAS_FileWrite::CFGAS_FileWrite(IFX_Stream* pStream, FX_BOOL bReleaseStream)
-    : m_pStream(pStream), m_bReleaseStream(bReleaseStream) {
-  ASSERT(m_pStream != NULL);
-}
-CFGAS_FileWrite::~CFGAS_FileWrite() {
-  if (m_bReleaseStream) {
-    m_pStream->Release();
-  }
-}
-FX_FILESIZE CFGAS_FileWrite::GetSize() {
-  return m_pStream->GetLength();
-}
-FX_BOOL CFGAS_FileWrite::Flush() {
-  m_pStream->Flush();
-  return TRUE;
-}
-FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData, size_t size) {
-  return m_pStream->WriteData((const uint8_t*)pData, (int32_t)size) ==
-         (int32_t)size;
-}
-FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData,
-                                    FX_FILESIZE offset,
-                                    size_t size) {
-  m_pStream->Seek(FX_STREAMSEEK_Begin, offset);
-  int32_t iLen = m_pStream->WriteData((uint8_t*)pData, (int32_t)size);
-  return iLen == (int32_t)size;
-}
diff --git a/xfa/fgas/crt/fgas_stream.h b/xfa/fgas/crt/fgas_stream.h
index 3d644cf..2da06fc 100644
--- a/xfa/fgas/crt/fgas_stream.h
+++ b/xfa/fgas/crt/fgas_stream.h
@@ -14,11 +14,7 @@
 
 IFX_FileRead* FX_CreateFileRead(IFX_Stream* pBaseStream,
                                 FX_BOOL bReleaseStream = FALSE);
-IFX_FileRead* FX_CreateFileRead(IFX_BufferRead* pBufferRead,
-                                FX_FILESIZE iFileSize = -1,
-                                FX_BOOL bReleaseStream = TRUE);
-IFX_FileWrite* FX_CreateFileWrite(IFX_Stream* pBaseStream,
-                                  FX_BOOL bReleaseStream = FALSE);
+
 enum FX_STREAMACCESS {
   FX_STREAMACCESS_Binary = 0x00,
   FX_STREAMACCESS_Text = 0x01,
diff --git a/xfa/fgas/crt/fgas_system.cpp b/xfa/fgas/crt/fgas_system.cpp
index 3ab3926..43d55b1 100644
--- a/xfa/fgas/crt/fgas_system.cpp
+++ b/xfa/fgas/crt/fgas_system.cpp
@@ -10,13 +10,6 @@
 
 #include "core/fxcrt/include/fx_system.h"
 
-#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
-    _FX_OS_ == _FX_WIN64_
-#include <io.h>
-#elif _FX_OS_ == _FX_LINUX_DESKTOP_ || _FX_OS_ == _FX_LINUX_Mini_
-#include <sys/times.h>
-#endif
-
 namespace {
 
 inline FX_BOOL FX_isupper(int32_t ch) {
@@ -43,35 +36,6 @@
   return wch1 - wch2;
 }
 
-int32_t FX_filelength(FXSYS_FILE* file) {
-  ASSERT(file != NULL);
-#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_
-  return _filelength(_fileno(file));
-#else
-  int32_t iPos = FXSYS_ftell(file);
-  FXSYS_fseek(file, 0, FXSYS_SEEK_END);
-  int32_t iLen = FXSYS_ftell(file);
-  FXSYS_fseek(file, iPos, FXSYS_SEEK_SET);
-  return iLen;
-#endif
-}
-
-FX_BOOL FX_fsetsize(FXSYS_FILE* file, int32_t size) {
-  ASSERT(file != NULL);
-#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_
-  return _chsize(_fileno(file), size) == 0;
-#elif _FX_OS_ == _FX_WIN32_MOBILE_
-  HANDLE hFile = _fileno(file);
-  uint32_t dwPos = ::SetFilePointer(hFile, 0, 0, FILE_CURRENT);
-  ::SetFilePointer(hFile, size, 0, FILE_BEGIN);
-  FX_BOOL bRet = ::SetEndOfFile(hFile);
-  ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN);
-  return bRet;
-#else
-  return FALSE;
-#endif
-}
-
 FX_FLOAT FX_wcstof(const FX_WCHAR* pwsStr, int32_t iLength, int32_t* pUsedLen) {
   ASSERT(pwsStr != NULL);
   if (iLength < 0) {
diff --git a/xfa/fgas/crt/fgas_system.h b/xfa/fgas/crt/fgas_system.h
index d4bebca..a3215ff 100644
--- a/xfa/fgas/crt/fgas_system.h
+++ b/xfa/fgas/crt/fgas_system.h
@@ -9,15 +9,9 @@
 
 #include "core/fxcrt/include/fx_system.h"
 
-#define FX_RAD2DEG(r) ((r)*180.0f / FX_PI)
-#define FX_DEG2RAD(a) ((a)*FX_PI / 180.0f)
-
 FX_FLOAT FX_wcstof(const FX_WCHAR* pwsStr,
                    int32_t iLength = -1,
                    int32_t* pUsedLen = NULL);
 int32_t FX_wcsnicmp(const FX_WCHAR* s1, const FX_WCHAR* s2, size_t count);
 
-int32_t FX_filelength(FXSYS_FILE* file);
-FX_BOOL FX_fsetsize(FXSYS_FILE* file, int32_t size);
-
 #endif  // XFA_FGAS_CRT_FGAS_SYSTEM_H_
diff --git a/xfa/fxfa/app/xfa_checksum.cpp b/xfa/fxfa/app/xfa_checksum.cpp
index fab14c9..b5d9fde 100644
--- a/xfa/fxfa/app/xfa_checksum.cpp
+++ b/xfa/fxfa/app/xfa_checksum.cpp
@@ -7,7 +7,89 @@
 #include "xfa/fxfa/include/xfa_checksum.h"
 
 #include "core/fdrm/crypto/include/fx_crypt.h"
-#include "xfa/fgas/crt/fgas_algorithm.h"
+
+namespace {
+
+struct FX_BASE64DATA {
+  uint32_t data1 : 2;
+  uint32_t data2 : 6;
+  uint32_t data3 : 4;
+  uint32_t data4 : 4;
+  uint32_t data5 : 6;
+  uint32_t data6 : 2;
+  uint32_t data7 : 8;
+};
+
+const FX_CHAR g_FXBase64EncoderMap[64] = {
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
+};
+
+void Base64EncodePiece(const FX_BASE64DATA& src,
+                       int32_t iBytes,
+                       FX_CHAR dst[4]) {
+  dst[0] = g_FXBase64EncoderMap[src.data2];
+  uint32_t b = src.data1 << 4;
+  if (iBytes > 1) {
+    b |= src.data4;
+  }
+  dst[1] = g_FXBase64EncoderMap[b];
+  if (iBytes > 1) {
+    b = src.data3 << 2;
+    if (iBytes > 2) {
+      b |= src.data6;
+    }
+    dst[2] = g_FXBase64EncoderMap[b];
+    if (iBytes > 2) {
+      dst[3] = g_FXBase64EncoderMap[src.data5];
+    } else {
+      dst[3] = '=';
+    }
+  } else {
+    dst[2] = dst[3] = '=';
+  }
+}
+
+int32_t Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, FX_CHAR* pDst) {
+  ASSERT(pSrc != NULL);
+  if (iSrcLen < 1) {
+    return 0;
+  }
+  if (pDst == NULL) {
+    int32_t iDstLen = iSrcLen / 3 * 4;
+    if ((iSrcLen % 3) != 0) {
+      iDstLen += 4;
+    }
+    return iDstLen;
+  }
+  FX_BASE64DATA srcData;
+  int32_t iBytes = 3;
+  FX_CHAR* pDstEnd = pDst;
+  while (iSrcLen > 0) {
+    if (iSrcLen > 2) {
+      ((uint8_t*)&srcData)[0] = *pSrc++;
+      ((uint8_t*)&srcData)[1] = *pSrc++;
+      ((uint8_t*)&srcData)[2] = *pSrc++;
+      iSrcLen -= 3;
+    } else {
+      *((uint32_t*)&srcData) = 0;
+      ((uint8_t*)&srcData)[0] = *pSrc++;
+      if (iSrcLen > 1) {
+        ((uint8_t*)&srcData)[1] = *pSrc++;
+      }
+      iBytes = iSrcLen;
+      iSrcLen = 0;
+    }
+    Base64EncodePiece(srcData, iBytes, pDstEnd);
+    pDstEnd += 4;
+  }
+  return pDstEnd - pDst;
+}
+
+}  // namespace
 
 CXFA_SAXReaderHandler::CXFA_SAXReaderHandler(CXFA_ChecksumContext* pContext)
     : m_pContext(pContext) {
@@ -167,9 +249,9 @@
     uint8_t digest[20];
     FXSYS_memset(digest, 0, 20);
     CRYPT_SHA1Finish(m_pByteContext, digest);
-    int32_t nLen = FX_Base64EncodeA(digest, 20, NULL);
+    int32_t nLen = Base64EncodeA(digest, 20, NULL);
     FX_CHAR* pBuffer = m_bsChecksum.GetBuffer(nLen);
-    FX_Base64EncodeA(digest, 20, pBuffer);
+    Base64EncodeA(digest, 20, pBuffer);
     m_bsChecksum.ReleaseBuffer(nLen);
     FX_Free(m_pByteContext);
     m_pByteContext = NULL;
diff --git a/xfa/fxfa/app/xfa_ffdoc.cpp b/xfa/fxfa/app/xfa_ffdoc.cpp
index f938815..ce7628e 100644
--- a/xfa/fxfa/app/xfa_ffdoc.cpp
+++ b/xfa/fxfa/app/xfa_ffdoc.cpp
@@ -14,7 +14,6 @@
 #include "core/fxcrt/include/fx_ext.h"
 #include "core/fxcrt/include/fx_memory.h"
 #include "xfa/fde/xml/fde_xml_imp.h"
-#include "xfa/fgas/crt/fgas_algorithm.h"
 #include "xfa/fwl/core/fwl_noteimp.h"
 #include "xfa/fxfa/app/xfa_ffnotify.h"
 #include "xfa/fxfa/include/xfa_checksum.h"
@@ -28,6 +27,127 @@
 #include "xfa/fxfa/parser/xfa_parser_imp.h"
 #include "xfa/fxfa/parser/xfa_parser_imp.h"
 
+namespace {
+
+struct FX_BASE64DATA {
+  uint32_t data1 : 2;
+  uint32_t data2 : 6;
+  uint32_t data3 : 4;
+  uint32_t data4 : 4;
+  uint32_t data5 : 6;
+  uint32_t data6 : 2;
+  uint32_t data7 : 8;
+};
+
+const uint8_t kStartValuesRemoved = 43;
+const uint8_t kDecoderMapSize = 80;
+const uint8_t g_FXBase64DecoderMap[kDecoderMapSize] = {
+    0x3E, 0xFF, 0xFF, 0xFF, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
+    0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01,
+    0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
+    0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
+    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B,
+    0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33,
+};
+
+uint8_t base64DecoderValue(uint8_t val) {
+  if (val < kStartValuesRemoved || val >= kStartValuesRemoved + kDecoderMapSize)
+    return 0xFF;
+  return g_FXBase64DecoderMap[val - kStartValuesRemoved];
+}
+
+void Base64DecodePiece(const FX_CHAR src[4],
+                       int32_t iChars,
+                       FX_BASE64DATA& dst,
+                       int32_t& iBytes) {
+  ASSERT(iChars > 0 && iChars < 5);
+  iBytes = 1;
+  dst.data2 = base64DecoderValue(static_cast<uint8_t>(src[0]));
+  if (iChars > 1) {
+    uint8_t b = base64DecoderValue(static_cast<uint8_t>(src[1]));
+    dst.data1 = b >> 4;
+    dst.data4 = b;
+    if (iChars > 2) {
+      iBytes = 2;
+      b = base64DecoderValue(static_cast<uint8_t>(src[2]));
+      dst.data3 = b >> 2;
+      dst.data6 = b;
+      if (iChars > 3) {
+        iBytes = 3;
+        dst.data5 = base64DecoderValue(static_cast<uint8_t>(src[3]));
+      } else {
+        dst.data5 = 0;
+      }
+    } else {
+      dst.data3 = 0;
+    }
+  } else {
+    dst.data1 = 0;
+  }
+}
+
+int32_t Base64DecodeW(const FX_WCHAR* pSrc, int32_t iSrcLen, uint8_t* pDst) {
+  ASSERT(pSrc);
+  if (iSrcLen < 1) {
+    return 0;
+  }
+  while (iSrcLen > 0 && pSrc[iSrcLen - 1] == '=') {
+    iSrcLen--;
+  }
+  if (iSrcLen < 1) {
+    return 0;
+  }
+  if (!pDst) {
+    int32_t iDstLen = iSrcLen / 4 * 3;
+    iSrcLen %= 4;
+    if (iSrcLen == 1) {
+      iDstLen += 1;
+    } else if (iSrcLen == 2) {
+      iDstLen += 1;
+    } else if (iSrcLen == 3) {
+      iDstLen += 2;
+    }
+    return iDstLen;
+  }
+  FX_CHAR srcData[4];
+  FX_BASE64DATA dstData;
+  int32_t iChars = 4, iBytes;
+  uint8_t* pDstEnd = pDst;
+  while (iSrcLen > 0) {
+    if (iSrcLen > 3) {
+      srcData[0] = (FX_CHAR)*pSrc++;
+      srcData[1] = (FX_CHAR)*pSrc++;
+      srcData[2] = (FX_CHAR)*pSrc++;
+      srcData[3] = (FX_CHAR)*pSrc++;
+      iSrcLen -= 4;
+    } else {
+      *((uint32_t*)&dstData) = 0;
+      *((uint32_t*)srcData) = 0;
+      srcData[0] = (FX_CHAR)*pSrc++;
+      if (iSrcLen > 1) {
+        srcData[1] = (FX_CHAR)*pSrc++;
+      }
+      if (iSrcLen > 2) {
+        srcData[2] = (FX_CHAR)*pSrc++;
+      }
+      iChars = iSrcLen;
+      iSrcLen = 0;
+    }
+    Base64DecodePiece(srcData, iChars, dstData, iBytes);
+    *pDstEnd++ = ((uint8_t*)&dstData)[0];
+    if (iBytes > 1) {
+      *pDstEnd++ = ((uint8_t*)&dstData)[1];
+    }
+    if (iBytes > 2) {
+      *pDstEnd++ = ((uint8_t*)&dstData)[2];
+    }
+  }
+  return pDstEnd - pDst;
+}
+
+}  // namespace
+
 CXFA_FFDoc::CXFA_FFDoc(CXFA_FFApp* pApp, IXFA_DocProvider* pDocProvider)
     : m_pDocProvider(pDocProvider),
       m_pDocument(nullptr),
@@ -53,7 +173,7 @@
 FX_BOOL XFA_GetPDFContentsFromPDFXML(CFDE_XMLNode* pPDFElement,
                                      uint8_t*& pByteBuffer,
                                      int32_t& iBufferSize) {
-  CFDE_XMLElement* pDocumentElement = NULL;
+  CFDE_XMLElement* pDocumentElement = nullptr;
   for (CFDE_XMLNode* pXMLNode =
            pPDFElement->GetNodeItem(CFDE_XMLNode::FirstChild);
        pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) {
@@ -70,7 +190,7 @@
   if (!pDocumentElement) {
     return FALSE;
   }
-  CFDE_XMLElement* pChunkElement = NULL;
+  CFDE_XMLElement* pChunkElement = nullptr;
   for (CFDE_XMLNode* pXMLNode =
            pDocumentElement->GetNodeItem(CFDE_XMLNode::FirstChild);
        pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) {
@@ -90,10 +210,10 @@
   CFX_WideString wsPDFContent;
   pChunkElement->GetTextData(wsPDFContent);
   iBufferSize =
-      FX_Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), NULL);
+      Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), nullptr);
   pByteBuffer = FX_Alloc(uint8_t, iBufferSize + 1);
   pByteBuffer[iBufferSize] = '0';  // FIXME: I bet this is wrong.
-  FX_Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), pByteBuffer);
+  Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), pByteBuffer);
   return TRUE;
 }
 void XFA_XPDPacket_MergeRootNode(CXFA_Node* pOriginRoot, CXFA_Node* pNewRoot) {
@@ -109,7 +229,7 @@
       pNewRoot->RemoveChild(pChildNode);
       pOriginRoot->InsertChild(pChildNode);
       pChildNode = pNextSibling;
-      pNextSibling = NULL;
+      pNextSibling = nullptr;
     }
   }
 }
@@ -125,8 +245,8 @@
       return XFA_PARSESTATUS_SyntaxErr;
     }
     int32_t iBufferSize = 0;
-    uint8_t* pByteBuffer = NULL;
-    IFX_FileRead* pXFAReader = NULL;
+    uint8_t* pByteBuffer = nullptr;
+    IFX_FileRead* pXFAReader = nullptr;
     if (XFA_GetPDFContentsFromPDFXML(pPDFXML, pByteBuffer, iBufferSize)) {
       pXFAReader = FX_CreateMemoryStream(pByteBuffer, iBufferSize, TRUE);
     } else {
@@ -149,9 +269,9 @@
     if (!pParser) {
       return XFA_PARSESTATUS_SyntaxErr;
     }
-    CXFA_Node* pRootNode = NULL;
+    CXFA_Node* pRootNode = nullptr;
     if (pParser->StartParse(m_pStream) == XFA_PARSESTATUS_Ready &&
-        pParser->DoParse(NULL) == XFA_PARSESTATUS_Done) {
+        pParser->DoParse(nullptr) == XFA_PARSESTATUS_Done) {
       pRootNode = pParser->GetRootNode();
     }
     if (pRootNode && m_pDocument->GetRoot()) {
@@ -161,7 +281,7 @@
       iStatus = XFA_PARSESTATUS_StatusErr;
     }
     pParser->Release();
-    pParser = NULL;
+    pParser = nullptr;
   }
   return iStatus;
 }
@@ -217,21 +337,21 @@
   return TRUE;
 }
 FX_BOOL CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) {
-  if (pPDFDoc == NULL) {
+  if (!pPDFDoc)
     return FALSE;
-  }
+
   CPDF_Dictionary* pRoot = pPDFDoc->GetRoot();
-  if (pRoot == NULL) {
+  if (!pRoot)
     return FALSE;
-  }
+
   CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm");
-  if (pAcroForm == NULL) {
+  if (!pAcroForm)
     return FALSE;
-  }
+
   CPDF_Object* pElementXFA = pAcroForm->GetDirectObjectBy("XFA");
-  if (pElementXFA == NULL) {
+  if (!pElementXFA)
     return FALSE;
-  }
+
   CFX_ArrayTemplate<CPDF_Stream*> xfaStreams;
   if (pElementXFA->IsArray()) {
     CPDF_Array* pXFAArray = (CPDF_Array*)pElementXFA;
@@ -249,7 +369,7 @@
   m_pPDFDoc = pPDFDoc;
   if (m_pStream) {
     m_pStream->Release();
-    m_pStream = NULL;
+    m_pStream = nullptr;
   }
   m_pStream = pFileRead;
   m_bOwnStream = TRUE;
diff --git a/xfa/fxfa/parser/xfa_layout_itemlayout.cpp b/xfa/fxfa/parser/xfa_layout_itemlayout.cpp
index ef2df84..8b85ba8 100644
--- a/xfa/fxfa/parser/xfa_layout_itemlayout.cpp
+++ b/xfa/fxfa/parser/xfa_layout_itemlayout.cpp
@@ -9,7 +9,6 @@
 #include <algorithm>
 #include <memory>
 
-#include "xfa/fgas/crt/fgas_algorithm.h"
 #include "xfa/fxfa/app/xfa_ffnotify.h"
 #include "xfa/fxfa/fm2js/xfa_fm2jsapi.h"
 #include "xfa/fxfa/parser/cxfa_occur.h"
@@ -25,6 +24,34 @@
 #include "xfa/fxfa/parser/xfa_script.h"
 #include "xfa/fxfa/parser/xfa_utils.h"
 
+namespace {
+
+int32_t SeparateStringW(const FX_WCHAR* pStr,
+                        int32_t iStrLen,
+                        FX_WCHAR delimiter,
+                        CFX_WideStringArray& pieces) {
+  if (!pStr)
+    return 0;
+  if (iStrLen < 0)
+    iStrLen = FXSYS_wcslen(pStr);
+
+  const FX_WCHAR* pToken = pStr;
+  const FX_WCHAR* pEnd = pStr + iStrLen;
+  while (TRUE) {
+    if (pStr >= pEnd || delimiter == *pStr) {
+      CFX_WideString sub(pToken, pStr - pToken);
+      pieces.Add(sub);
+      pToken = pStr + 1;
+      if (pStr >= pEnd)
+        break;
+    }
+    pStr++;
+  }
+  return pieces.GetSize();
+}
+
+}  // namespace
+
 CXFA_ItemLayoutProcessor::CXFA_ItemLayoutProcessor(CXFA_Node* pNode,
                                                    CXFA_LayoutPageMgr* pPageMgr)
     : m_bKeepBreakFinish(FALSE),
@@ -1380,8 +1407,8 @@
   CFX_WideStringC wsColumnWidths;
   if (pLayoutNode->TryCData(XFA_ATTRIBUTE_ColumnWidths, wsColumnWidths)) {
     CFX_WideStringArray widths;
-    if (FX_SeparateStringW(wsColumnWidths.c_str(), wsColumnWidths.GetLength(),
-                           L' ', widths) > 0) {
+    if (SeparateStringW(wsColumnWidths.c_str(), wsColumnWidths.GetLength(),
+                        L' ', widths) > 0) {
       int32_t iCols = widths.GetSize();
       CFX_WideString wsWidth;
       for (int32_t i = 0; i < iCols; i++) {