Get rid of "exceptions" from CBC_EncoderContext ctor.

The caller can call HasCharactersOutsideISO88591Encoding() after
constructing the context to see if the context is valid.

Also initialize CBC_EncoderContext members in the header or initializer
list when possible.

Change-Id: Ieb06b7806fc4cbbde736c4fad6b52fb6f67f5db1
Reviewed-on: https://pdfium-review.googlesource.com/c/45765
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/datamatrix/BC_EncoderContext.cpp b/fxbarcode/datamatrix/BC_EncoderContext.cpp
index 2489258..f686605 100644
--- a/fxbarcode/datamatrix/BC_EncoderContext.cpp
+++ b/fxbarcode/datamatrix/BC_EncoderContext.cpp
@@ -31,7 +31,8 @@
 
 CBC_EncoderContext::CBC_EncoderContext(const WideString& msg,
                                        const WideString& ecLevel,
-                                       int32_t& e) {
+                                       bool bAllowRectangular)
+    : m_bAllowRectangular(bAllowRectangular) {
   ByteString dststr = msg.ToUTF8();
   size_t c = dststr.GetLength();
   WideString sb;
@@ -39,24 +40,15 @@
   for (size_t i = 0; i < c; i++) {
     wchar_t ch = static_cast<wchar_t>(dststr[i] & 0xff);
     if (ch == '?' && dststr[i] != '?') {
-      e = BCExceptionCharactersOutsideISO88591Encoding;
+      m_bHasCharactersOutsideISO88591Encoding = true;
     }
     sb += ch;
   }
   m_msg = std::move(sb);
   m_codewords.Reserve(m_msg.GetLength());
-  m_allowRectangular = true;
-  m_newEncoding = -1;
-  m_pos = 0;
-  m_symbolInfo = nullptr;
-  m_skipAtEnd = 0;
 }
 
-CBC_EncoderContext::~CBC_EncoderContext() {}
-
-void CBC_EncoderContext::setAllowRectangular(bool allow) {
-  m_allowRectangular = allow;
-}
+CBC_EncoderContext::~CBC_EncoderContext() = default;
 
 void CBC_EncoderContext::setSkipAtEnd(int32_t count) {
   m_skipAtEnd = count;
@@ -98,7 +90,7 @@
 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);
+    m_symbolInfo = CBC_SymbolInfo::lookup(len, m_bAllowRectangular, e);
     if (e != BCExceptionNO)
       return false;
   }
@@ -106,7 +98,7 @@
 }
 
 void CBC_EncoderContext::resetSymbolInfo() {
-  m_allowRectangular = true;
+  m_bAllowRectangular = true;
 }
 
 size_t CBC_EncoderContext::getTotalMessageCharCount() {
diff --git a/fxbarcode/datamatrix/BC_EncoderContext.h b/fxbarcode/datamatrix/BC_EncoderContext.h
index 9048540..afd202d 100644
--- a/fxbarcode/datamatrix/BC_EncoderContext.h
+++ b/fxbarcode/datamatrix/BC_EncoderContext.h
@@ -16,10 +16,9 @@
  public:
   CBC_EncoderContext(const WideString& msg,
                      const WideString& ecLevel,
-                     int32_t& e);
+                     bool bAllowRectangular);
   ~CBC_EncoderContext();
 
-  void setAllowRectangular(bool allow);
   void setSkipAtEnd(int32_t count);
   wchar_t getCurrentChar();
   wchar_t getCurrent();
@@ -34,17 +33,22 @@
   bool UpdateSymbolInfo(int32_t len);
   void resetSymbolInfo();
 
+  bool HasCharactersOutsideISO88591Encoding() const {
+    return m_bHasCharactersOutsideISO88591Encoding;
+  }
+
   WideString m_msg;
   WideString m_codewords;
-  size_t m_pos;
-  int32_t m_newEncoding;
+  size_t m_pos = 0;
+  int32_t m_newEncoding = -1;
   UnownedPtr<CBC_SymbolInfo> m_symbolInfo;
 
  private:
   size_t getTotalMessageCharCount();
 
-  bool m_allowRectangular;  // Force square when false.
-  size_t m_skipAtEnd;
+  bool m_bAllowRectangular;  // Force square when false.
+  bool m_bHasCharactersOutsideISO88591Encoding = false;
+  size_t m_skipAtEnd = 0;
 };
 
 #endif  // FXBARCODE_DATAMATRIX_BC_ENCODERCONTEXT_H_
diff --git a/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
index f4ae68c..4a9dcf1 100644
--- a/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
+++ b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
@@ -68,13 +68,11 @@
 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)
+    bool bAllowRectangular) {
+  CBC_EncoderContext context(msg, ecLevel, bAllowRectangular);
+  if (context.HasCharactersOutsideISO88591Encoding())
     return {};
 
-  context.setAllowRectangular(allowRectangular);
   if ((msg.Left(6) == MACRO_05_HEADER) && (msg.Last() == MACRO_TRAILER)) {
     context.writeCodeword(MACRO_05);
     context.setSkipAtEnd(2);
diff --git a/fxbarcode/datamatrix/BC_HighLevelEncoder.h b/fxbarcode/datamatrix/BC_HighLevelEncoder.h
index f092dda..dea7a94 100644
--- a/fxbarcode/datamatrix/BC_HighLevelEncoder.h
+++ b/fxbarcode/datamatrix/BC_HighLevelEncoder.h
@@ -28,7 +28,7 @@
 
   static Optional<WideString> EncodeHighLevel(const WideString& msg,
                                               const WideString& ecLevel,
-                                              bool allowRectangular);
+                                              bool bAllowRectangular);
   static int32_t lookAheadTest(const WideString& msg,
                                int32_t startpos,
                                int32_t currentMode);
diff --git a/fxbarcode/utils.h b/fxbarcode/utils.h
index 2b31f50..64d714e 100644
--- a/fxbarcode/utils.h
+++ b/fxbarcode/utils.h
@@ -28,7 +28,6 @@
 #define BCExceptionBadIndexException 52
 #define BCExceptionInvalidateMaskPattern 68
 #define BCExceptionCharacterNotThisMode 75
-#define BCExceptionCharactersOutsideISO88591Encoding 87
 #define BCExceptionIllegalDataCodewords 88
 #define BCExceptionGeneric 107