Get rid of more "exceptions" in fxbarcode.

Change CBC_HighLevelEncoder::EncodeHighLevel() to return Optional and
change CBC_EncoderContext::UpdateSymbolInfo() to return bool.

Change-Id: I9686276d8b9412ad62bf1a1d0a6e48dad74d4aaa
Reviewed-on: https://pdfium-review.googlesource.com/c/45762
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/datamatrix/BC_Base256Encoder.cpp b/fxbarcode/datamatrix/BC_Base256Encoder.cpp
index d7f717a..ffb0058 100644
--- a/fxbarcode/datamatrix/BC_Base256Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_Base256Encoder.cpp
@@ -70,9 +70,7 @@
   int32_t lengthFieldSize = 1;
   int32_t currentSize =
       context->getCodewordCount() + dataCount + lengthFieldSize;
-  int32_t e = BCExceptionNO;
-  context->updateSymbolInfo(currentSize, e);
-  if (e != BCExceptionNO)
+  if (!context->UpdateSymbolInfo(currentSize))
     return false;
 
   bool mustPad = (context->m_symbolInfo->dataCapacity() - currentSize) > 0;
diff --git a/fxbarcode/datamatrix/BC_C40Encoder.cpp b/fxbarcode/datamatrix/BC_C40Encoder.cpp
index daceb89..5e0c779 100644
--- a/fxbarcode/datamatrix/BC_C40Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_C40Encoder.cpp
@@ -65,8 +65,7 @@
 
     int32_t unwritten = (buffer.GetLength() / 3) * 2;
     int32_t curCodewordCount = context->getCodewordCount() + unwritten;
-    context->updateSymbolInfo(curCodewordCount, e);
-    if (e != BCExceptionNO)
+    if (!context->UpdateSymbolInfo(curCodewordCount))
       return false;
 
     int32_t available =
@@ -111,9 +110,7 @@
   int32_t unwritten = (buffer->GetLength() / 3) * 2;
   int32_t rest = buffer->GetLength() % 3;
   int32_t curCodewordCount = context->getCodewordCount() + unwritten;
-  int32_t e = BCExceptionNO;
-  context->updateSymbolInfo(curCodewordCount, e);
-  if (e != BCExceptionNO)
+  if (!context->UpdateSymbolInfo(curCodewordCount))
     return false;
 
   int32_t available = context->m_symbolInfo->dataCapacity() - curCodewordCount;
diff --git a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
index 799b774..8871492 100644
--- a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
+++ b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
@@ -114,17 +114,17 @@
     return nullptr;
 
   WideString ecLevel;
-  int32_t e = BCExceptionNO;
-  WideString encoded =
-      CBC_HighLevelEncoder::encodeHighLevel(contents, ecLevel, false, e);
-  if (e != BCExceptionNO)
+  Optional<WideString> encoded =
+      CBC_HighLevelEncoder::EncodeHighLevel(contents, ecLevel, false);
+  if (!encoded.has_value())
     return nullptr;
+  int32_t e = BCExceptionNO;
   CBC_SymbolInfo* symbolInfo =
-      CBC_SymbolInfo::lookup(encoded.GetLength(), false, e);
+      CBC_SymbolInfo::lookup(encoded.value().GetLength(), false, e);
   if (e != BCExceptionNO)
     return nullptr;
   WideString codewords =
-      CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
+      CBC_ErrorCorrection::encodeECC200(encoded.value(), symbolInfo, e);
   if (e != BCExceptionNO)
     return nullptr;
 
diff --git a/fxbarcode/datamatrix/BC_EdifactEncoder.cpp b/fxbarcode/datamatrix/BC_EdifactEncoder.cpp
index 5674618..0c40d7d 100644
--- a/fxbarcode/datamatrix/BC_EdifactEncoder.cpp
+++ b/fxbarcode/datamatrix/BC_EdifactEncoder.cpp
@@ -59,9 +59,7 @@
     return false;
 
   if (count == 1) {
-    int32_t e = BCExceptionNO;
-    context->updateSymbolInfo(e);
-    if (e != BCExceptionNO)
+    if (!context->UpdateSymbolInfo())
       return false;
 
     int32_t available =
@@ -79,19 +77,17 @@
   bool endOfSymbolReached = !context->hasMoreCharacters();
   bool restInAscii = endOfSymbolReached && restChars <= 2;
   if (restChars <= 2) {
-    int32_t e = BCExceptionNO;
-    context->updateSymbolInfo(context->getCodewordCount() + restChars, e);
-    if (e != BCExceptionNO)
+    if (!context->UpdateSymbolInfo(context->getCodewordCount() + restChars))
       return false;
 
     int32_t available =
         context->m_symbolInfo->dataCapacity() - context->getCodewordCount();
     if (available >= 3) {
       restInAscii = false;
-      context->updateSymbolInfo(
-          context->getCodewordCount() + encoded.GetLength(), e);
-      if (e != BCExceptionNO)
+      if (!context->UpdateSymbolInfo(context->getCodewordCount() +
+                                     encoded.GetLength())) {
         return false;
+      }
     }
   }
 
diff --git a/fxbarcode/datamatrix/BC_EncoderContext.cpp b/fxbarcode/datamatrix/BC_EncoderContext.cpp
index e72d1e4..b183ec3 100644
--- a/fxbarcode/datamatrix/BC_EncoderContext.cpp
+++ b/fxbarcode/datamatrix/BC_EncoderContext.cpp
@@ -92,15 +92,19 @@
 size_t CBC_EncoderContext::getRemainingCharacters() {
   return getTotalMessageCharCount() - m_pos;
 }
-void CBC_EncoderContext::updateSymbolInfo(int32_t& e) {
-  updateSymbolInfo(getCodewordCount(), e);
+
+bool CBC_EncoderContext::UpdateSymbolInfo() {
+  return UpdateSymbolInfo(getCodewordCount());
 }
-void CBC_EncoderContext::updateSymbolInfo(int32_t len, int32_t& e) {
+
+bool CBC_EncoderContext::UpdateSymbolInfo(int32_t len) {
   if (!m_symbolInfo || len > m_symbolInfo->dataCapacity()) {
+    int32_t e = BCExceptionNO;
     m_symbolInfo = CBC_SymbolInfo::lookup(len, m_allowRectangular, e);
     if (e != BCExceptionNO)
-      return;
+      return false;
   }
+  return true;
 }
 
 void CBC_EncoderContext::resetSymbolInfo() {
diff --git a/fxbarcode/datamatrix/BC_EncoderContext.h b/fxbarcode/datamatrix/BC_EncoderContext.h
index c0ddb20..9048540 100644
--- a/fxbarcode/datamatrix/BC_EncoderContext.h
+++ b/fxbarcode/datamatrix/BC_EncoderContext.h
@@ -30,8 +30,8 @@
   void resetEncoderSignal();
   bool hasMoreCharacters();
   size_t getRemainingCharacters();
-  void updateSymbolInfo(int32_t& e);
-  void updateSymbolInfo(int32_t len, int32_t& e);
+  bool UpdateSymbolInfo();
+  bool UpdateSymbolInfo(int32_t len);
   void resetSymbolInfo();
 
   WideString m_msg;
diff --git a/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
index 3efafae..c157866 100644
--- a/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
+++ b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
@@ -67,13 +67,14 @@
 }
 
 // static
-WideString CBC_HighLevelEncoder::encodeHighLevel(WideString msg,
-                                                 WideString ecLevel,
-                                                 bool allowRectangular,
-                                                 int32_t& e) {
+Optional<WideString> CBC_HighLevelEncoder::EncodeHighLevel(
+    const WideString& msg,
+    const WideString& ecLevel,
+    bool allowRectangular) {
+  int32_t e = BCExceptionNO;
   CBC_EncoderContext context(msg, ecLevel, e);
   if (e != BCExceptionNO)
-    return WideString();
+    return {};
 
   context.setAllowRectangular(allowRectangular);
   if ((msg.Left(6) == MACRO_05_HEADER) && (msg.Last() == MACRO_TRAILER)) {
@@ -96,10 +97,8 @@
   encoders.push_back(pdfium::MakeUnique<CBC_Base256Encoder>());
   int32_t encodingMode = ASCII_ENCODATION;
   while (context.hasMoreCharacters()) {
-    if (!encoders[encodingMode]->Encode(&context)) {
-      e = BCExceptionGeneric;
-      return L"";
-    }
+    if (!encoders[encodingMode]->Encode(&context))
+      return {};
 
     if (context.m_newEncoding >= 0) {
       encodingMode = context.m_newEncoding;
@@ -107,9 +106,8 @@
     }
   }
   int32_t len = context.m_codewords.GetLength();
-  context.updateSymbolInfo(e);
-  if (e != BCExceptionNO)
-    return L"";
+  if (!context.UpdateSymbolInfo())
+    return {};
 
   int32_t capacity = context.m_symbolInfo->dataCapacity();
   if (len < capacity) {
@@ -129,6 +127,7 @@
   }
   return codewords;
 }
+
 int32_t CBC_HighLevelEncoder::lookAheadTest(const WideString& msg,
                                             int32_t startpos,
                                             int32_t currentMode) {
diff --git a/fxbarcode/datamatrix/BC_HighLevelEncoder.h b/fxbarcode/datamatrix/BC_HighLevelEncoder.h
index 0179129..f092dda 100644
--- a/fxbarcode/datamatrix/BC_HighLevelEncoder.h
+++ b/fxbarcode/datamatrix/BC_HighLevelEncoder.h
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include "core/fxcrt/widestring.h"
+#include "third_party/base/optional.h"
 
 #define ASCII_ENCODATION 0
 #define C40_ENCODATION 1
@@ -25,10 +26,9 @@
 
   std::vector<uint8_t>& getBytesForMessage(WideString msg);
 
-  static WideString encodeHighLevel(WideString msg,
-                                    WideString ecLevel,
-                                    bool allowRectangular,
-                                    int32_t& e);
+  static Optional<WideString> EncodeHighLevel(const WideString& msg,
+                                              const WideString& ecLevel,
+                                              bool allowRectangular);
   static int32_t lookAheadTest(const WideString& msg,
                                int32_t startpos,
                                int32_t currentMode);
diff --git a/fxbarcode/datamatrix/BC_X12Encoder.cpp b/fxbarcode/datamatrix/BC_X12Encoder.cpp
index fa2941a..da3ccfa 100644
--- a/fxbarcode/datamatrix/BC_X12Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_X12Encoder.cpp
@@ -65,9 +65,7 @@
 
 bool CBC_X12Encoder::HandleEOD(CBC_EncoderContext* context,
                                WideString* buffer) {
-  int32_t e = BCExceptionNO;
-  context->updateSymbolInfo(e);
-  if (e != BCExceptionNO)
+  if (!context->UpdateSymbolInfo())
     return false;
 
   int32_t available =