Get rid of "exceptions" in CBC_Encoder::Encode().

Just return a boolean instead. Fix all Encode() impls. Also change
Encode() to pass the context by pointer instead of by non-const ref.

Change-Id: I1a97e5cf28d3b00f77accd386c1dc0e6f282288c
Reviewed-on: https://pdfium-review.googlesource.com/c/45758
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp b/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp
index 95eac09..96bb09d 100644
--- a/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp
+++ b/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp
@@ -51,57 +51,56 @@
   return ASCII_ENCODATION;
 }
 
-void CBC_ASCIIEncoder::Encode(CBC_EncoderContext& context, int32_t& e) {
+bool CBC_ASCIIEncoder::Encode(CBC_EncoderContext* context) {
   int32_t n = CBC_HighLevelEncoder::determineConsecutiveDigitCount(
-      context.m_msg, context.m_pos);
+      context->m_msg, context->m_pos);
   if (n >= 2) {
     Optional<wchar_t> code = EncodeASCIIDigits(
-        context.m_msg[context.m_pos], context.m_msg[context.m_pos + 1]);
-    if (!code) {
-      e = BCExceptionGeneric;
-      return;
-    }
-    context.writeCodeword(*code);
-    context.m_pos += 2;
-    return;
+        context->m_msg[context->m_pos], context->m_msg[context->m_pos + 1]);
+    if (!code)
+      return false;
+
+    context->writeCodeword(*code);
+    context->m_pos += 2;
+    return true;
   }
 
-  wchar_t c = context.getCurrentChar();
+  wchar_t c = context->getCurrentChar();
   int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
-      context.m_msg, context.m_pos, getEncodingMode());
+      context->m_msg, context->m_pos, getEncodingMode());
   if (newMode != getEncodingMode()) {
     switch (newMode) {
       case BASE256_ENCODATION:
-        context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256);
-        context.signalEncoderChange(BASE256_ENCODATION);
-        return;
+        context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256);
+        context->signalEncoderChange(BASE256_ENCODATION);
+        return true;
       case C40_ENCODATION:
-        context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40);
-        context.signalEncoderChange(C40_ENCODATION);
-        return;
+        context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40);
+        context->signalEncoderChange(C40_ENCODATION);
+        return true;
       case X12_ENCODATION:
-        context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12);
-        context.signalEncoderChange(X12_ENCODATION);
-        return;
+        context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12);
+        context->signalEncoderChange(X12_ENCODATION);
+        return true;
       case TEXT_ENCODATION:
-        context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT);
-        context.signalEncoderChange(TEXT_ENCODATION);
-        return;
+        context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT);
+        context->signalEncoderChange(TEXT_ENCODATION);
+        return true;
       case EDIFACT_ENCODATION:
-        context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT);
-        context.signalEncoderChange(EDIFACT_ENCODATION);
-        return;
+        context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT);
+        context->signalEncoderChange(EDIFACT_ENCODATION);
+        return true;
       default:
-        e = BCExceptionGeneric;
-        return;
+        return false;
     }
   }
 
   if (CBC_HighLevelEncoder::isExtendedASCII(c)) {
-    context.writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT);
-    context.writeCodeword(static_cast<wchar_t>(c - 128 + 1));
+    context->writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT);
+    context->writeCodeword(static_cast<wchar_t>(c - 128 + 1));
   } else {
-    context.writeCodeword(static_cast<wchar_t>(c + 1));
+    context->writeCodeword(static_cast<wchar_t>(c + 1));
   }
-  context.m_pos++;
+  context->m_pos++;
+  return true;
 }
diff --git a/fxbarcode/datamatrix/BC_ASCIIEncoder.h b/fxbarcode/datamatrix/BC_ASCIIEncoder.h
index c9d8568..1b210ed 100644
--- a/fxbarcode/datamatrix/BC_ASCIIEncoder.h
+++ b/fxbarcode/datamatrix/BC_ASCIIEncoder.h
@@ -18,7 +18,7 @@
 
   // CBC_Encoder
   int32_t getEncodingMode() override;
-  void Encode(CBC_EncoderContext& context, int32_t& e) override;
+  bool Encode(CBC_EncoderContext* context) override;
 };
 
 #endif  // FXBARCODE_DATAMATRIX_BC_ASCIIENCODER_H_
diff --git a/fxbarcode/datamatrix/BC_Base256Encoder.cpp b/fxbarcode/datamatrix/BC_Base256Encoder.cpp
index 23c2bd2..d7f717a 100644
--- a/fxbarcode/datamatrix/BC_Base256Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_Base256Encoder.cpp
@@ -48,18 +48,18 @@
   return BASE256_ENCODATION;
 }
 
-void CBC_Base256Encoder::Encode(CBC_EncoderContext& context, int32_t& e) {
+bool CBC_Base256Encoder::Encode(CBC_EncoderContext* context) {
   WideString buffer;
-  buffer.Reserve(context.getRemainingCharacters() + 1);
+  buffer.Reserve(context->getRemainingCharacters() + 1);
   buffer += L'\0';
-  while (context.hasMoreCharacters()) {
-    wchar_t c = context.getCurrentChar();
+  while (context->hasMoreCharacters()) {
+    wchar_t c = context->getCurrentChar();
     buffer += c;
-    context.m_pos++;
+    context->m_pos++;
     int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
-        context.m_msg, context.m_pos, getEncodingMode());
+        context->m_msg, context->m_pos, getEncodingMode());
     if (newMode != getEncodingMode()) {
-      context.signalEncoderChange(newMode);
+      context->signalEncoderChange(newMode);
       break;
     }
   }
@@ -69,24 +69,26 @@
   buffer.SetAt(0, static_cast<wchar_t>(*buf) - '0');
   int32_t lengthFieldSize = 1;
   int32_t currentSize =
-      context.getCodewordCount() + dataCount + lengthFieldSize;
-  context.updateSymbolInfo(currentSize, e);
-  if (e != BCExceptionNO) {
-    return;
-  }
-  bool mustPad = (context.m_symbolInfo->dataCapacity() - currentSize) > 0;
-  if (context.hasMoreCharacters() || mustPad) {
+      context->getCodewordCount() + dataCount + lengthFieldSize;
+  int32_t e = BCExceptionNO;
+  context->updateSymbolInfo(currentSize, e);
+  if (e != BCExceptionNO)
+    return false;
+
+  bool mustPad = (context->m_symbolInfo->dataCapacity() - currentSize) > 0;
+  if (context->hasMoreCharacters() || mustPad) {
     if (dataCount <= 249) {
       buffer.SetAt(0, static_cast<wchar_t>(dataCount));
     } else if (dataCount > 249 && dataCount <= 1555) {
       buffer.SetAt(0, static_cast<wchar_t>((dataCount / 250) + 249));
       buffer.Insert(1, static_cast<wchar_t>(dataCount % 250));
     } else {
-      e = BCExceptionIllegalStateMessageLengthInvalid;
-      return;
+      return false;
     }
   }
   for (const auto& c : buffer) {
-    context.writeCodeword(Randomize255State(c, context.getCodewordCount() + 1));
+    context->writeCodeword(
+        Randomize255State(c, context->getCodewordCount() + 1));
   }
+  return true;
 }
diff --git a/fxbarcode/datamatrix/BC_Base256Encoder.h b/fxbarcode/datamatrix/BC_Base256Encoder.h
index 5ca49b3..e60333f 100644
--- a/fxbarcode/datamatrix/BC_Base256Encoder.h
+++ b/fxbarcode/datamatrix/BC_Base256Encoder.h
@@ -16,7 +16,7 @@
 
   // CBC_Encoder
   int32_t getEncodingMode() override;
-  void Encode(CBC_EncoderContext& context, int32_t& e) override;
+  bool Encode(CBC_EncoderContext* context) override;
 };
 
 #endif  // FXBARCODE_DATAMATRIX_BC_BASE256ENCODER_H_
diff --git a/fxbarcode/datamatrix/BC_C40Encoder.cpp b/fxbarcode/datamatrix/BC_C40Encoder.cpp
index 9aae76f..7e8f342 100644
--- a/fxbarcode/datamatrix/BC_C40Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_C40Encoder.cpp
@@ -53,54 +53,55 @@
   return C40_ENCODATION;
 }
 
-void CBC_C40Encoder::Encode(CBC_EncoderContext& context, int32_t& e) {
+bool CBC_C40Encoder::Encode(CBC_EncoderContext* context) {
   WideString buffer;
-  while (context.hasMoreCharacters()) {
-    wchar_t c = context.getCurrentChar();
-    context.m_pos++;
+  while (context->hasMoreCharacters()) {
+    wchar_t c = context->getCurrentChar();
+    context->m_pos++;
+    int32_t e = BCExceptionNO;
     int32_t lastCharSize = encodeChar(c, buffer, e);
-    if (e != BCExceptionNO) {
-      return;
-    }
+    if (e != BCExceptionNO)
+      return false;
+
     int32_t unwritten = (buffer.GetLength() / 3) * 2;
-    int32_t curCodewordCount = context.getCodewordCount() + unwritten;
-    context.updateSymbolInfo(curCodewordCount, e);
-    if (e != BCExceptionNO) {
-      return;
-    }
-    int32_t available = context.m_symbolInfo->dataCapacity() - curCodewordCount;
-    if (!context.hasMoreCharacters()) {
+    int32_t curCodewordCount = context->getCodewordCount() + unwritten;
+    context->updateSymbolInfo(curCodewordCount, e);
+    if (e != BCExceptionNO)
+      return false;
+
+    int32_t available =
+        context->m_symbolInfo->dataCapacity() - curCodewordCount;
+    if (!context->hasMoreCharacters()) {
       if ((buffer.GetLength() % 3) == 2) {
         if (available < 2 || available > 2) {
-          lastCharSize = BacktrackOneCharacter(&context, &buffer, lastCharSize);
-          if (lastCharSize < 0) {
-            e = BCExceptionGeneric;
-            return;
-          }
+          lastCharSize = BacktrackOneCharacter(context, &buffer, lastCharSize);
+          if (lastCharSize < 0)
+            return false;
         }
       }
       while ((buffer.GetLength() % 3) == 1 &&
              ((lastCharSize <= 3 && available != 1) || lastCharSize > 3)) {
-        lastCharSize = BacktrackOneCharacter(&context, &buffer, lastCharSize);
-        if (lastCharSize < 0) {
-          e = BCExceptionGeneric;
-          return;
-        }
+        lastCharSize = BacktrackOneCharacter(context, &buffer, lastCharSize);
+        if (lastCharSize < 0)
+          return false;
       }
       break;
     }
     int32_t count = buffer.GetLength();
     if ((count % 3) == 0) {
       int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
-          context.m_msg, context.m_pos, getEncodingMode());
+          context->m_msg, context->m_pos, getEncodingMode());
       if (newMode != getEncodingMode()) {
-        context.signalEncoderChange(newMode);
+        context->signalEncoderChange(newMode);
         break;
       }
     }
   }
-  handleEOD(context, buffer, e);
+  int32_t e = BCExceptionNO;
+  handleEOD(*context, buffer, e);
+  return e == BCExceptionNO;
 }
+
 void CBC_C40Encoder::writeNextTriplet(CBC_EncoderContext& context,
                                       WideString& buffer) {
   context.writeCodewords(EncodeToC40Codewords(buffer, 0));
diff --git a/fxbarcode/datamatrix/BC_C40Encoder.h b/fxbarcode/datamatrix/BC_C40Encoder.h
index 5ddad92..e20aac6 100644
--- a/fxbarcode/datamatrix/BC_C40Encoder.h
+++ b/fxbarcode/datamatrix/BC_C40Encoder.h
@@ -17,7 +17,7 @@
 
   // CBC_Encoder
   int32_t getEncodingMode() override;
-  void Encode(CBC_EncoderContext& context, int32_t& e) override;
+  bool Encode(CBC_EncoderContext* context) override;
 
   static void writeNextTriplet(CBC_EncoderContext& context, WideString& buffer);
 
diff --git a/fxbarcode/datamatrix/BC_EdifactEncoder.cpp b/fxbarcode/datamatrix/BC_EdifactEncoder.cpp
index 3e814a1..5674618 100644
--- a/fxbarcode/datamatrix/BC_EdifactEncoder.cpp
+++ b/fxbarcode/datamatrix/BC_EdifactEncoder.cpp
@@ -125,33 +125,32 @@
   return EDIFACT_ENCODATION;
 }
 
-void CBC_EdifactEncoder::Encode(CBC_EncoderContext& context, int32_t& e) {
+bool CBC_EdifactEncoder::Encode(CBC_EncoderContext* context) {
   WideString buffer;
-  while (context.hasMoreCharacters()) {
-    wchar_t c = context.getCurrentChar();
+  while (context->hasMoreCharacters()) {
+    wchar_t c = context->getCurrentChar();
+    int32_t e = BCExceptionNO;
     encodeChar(c, &buffer, e);
-    if (e != BCExceptionNO) {
-      return;
-    }
-    context.m_pos++;
+    if (e != BCExceptionNO)
+      return false;
+
+    context->m_pos++;
     int32_t count = buffer.GetLength();
     if (count >= 4) {
       WideString encoded = EncodeToEdifactCodewords(buffer, 0);
-      if (encoded.IsEmpty()) {
-        e = BCExceptionGeneric;
-        return;
-      }
-      context.writeCodewords(encoded);
+      if (encoded.IsEmpty())
+        return false;
+
+      context->writeCodewords(encoded);
       buffer.Delete(0, 4);
       int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
-          context.m_msg, context.m_pos, getEncodingMode());
+          context->m_msg, context->m_pos, getEncodingMode());
       if (newMode != getEncodingMode()) {
-        context.signalEncoderChange(ASCII_ENCODATION);
+        context->signalEncoderChange(ASCII_ENCODATION);
         break;
       }
     }
   }
   buffer += static_cast<wchar_t>(31);
-  if (!HandleEOD(&context, buffer))
-    e = BCExceptionGeneric;
+  return HandleEOD(context, buffer);
 }
diff --git a/fxbarcode/datamatrix/BC_EdifactEncoder.h b/fxbarcode/datamatrix/BC_EdifactEncoder.h
index 8c72303..070e763 100644
--- a/fxbarcode/datamatrix/BC_EdifactEncoder.h
+++ b/fxbarcode/datamatrix/BC_EdifactEncoder.h
@@ -16,7 +16,7 @@
 
   // CBC_Encoder
   int32_t getEncodingMode() override;
-  void Encode(CBC_EncoderContext& context, int32_t& e) override;
+  bool Encode(CBC_EncoderContext* context) override;
 };
 
 #endif  // FXBARCODE_DATAMATRIX_BC_EDIFACTENCODER_H_
diff --git a/fxbarcode/datamatrix/BC_Encoder.h b/fxbarcode/datamatrix/BC_Encoder.h
index be1730f..27110b6 100644
--- a/fxbarcode/datamatrix/BC_Encoder.h
+++ b/fxbarcode/datamatrix/BC_Encoder.h
@@ -17,7 +17,7 @@
   virtual ~CBC_Encoder();
 
   virtual int32_t getEncodingMode() = 0;
-  virtual void Encode(CBC_EncoderContext& context, int32_t& e) = 0;
+  virtual bool Encode(CBC_EncoderContext* context) = 0;
 };
 
 #endif  // FXBARCODE_DATAMATRIX_BC_ENCODER_H_
diff --git a/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
index d7a1615..3efafae 100644
--- a/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
+++ b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp
@@ -96,9 +96,10 @@
   encoders.push_back(pdfium::MakeUnique<CBC_Base256Encoder>());
   int32_t encodingMode = ASCII_ENCODATION;
   while (context.hasMoreCharacters()) {
-    encoders[encodingMode]->Encode(context, e);
-    if (e != BCExceptionNO)
+    if (!encoders[encodingMode]->Encode(&context)) {
+      e = BCExceptionGeneric;
       return L"";
+    }
 
     if (context.m_newEncoding >= 0) {
       encodingMode = context.m_newEncoding;
diff --git a/fxbarcode/datamatrix/BC_X12Encoder.cpp b/fxbarcode/datamatrix/BC_X12Encoder.cpp
index cbefdb3..d0ff232 100644
--- a/fxbarcode/datamatrix/BC_X12Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_X12Encoder.cpp
@@ -31,33 +31,40 @@
 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
 #include "fxbarcode/utils.h"
 
-CBC_X12Encoder::CBC_X12Encoder() {}
-CBC_X12Encoder::~CBC_X12Encoder() {}
+CBC_X12Encoder::CBC_X12Encoder() = default;
+
+CBC_X12Encoder::~CBC_X12Encoder() = default;
+
 int32_t CBC_X12Encoder::getEncodingMode() {
   return X12_ENCODATION;
 }
-void CBC_X12Encoder::Encode(CBC_EncoderContext& context, int32_t& e) {
+
+bool CBC_X12Encoder::Encode(CBC_EncoderContext* context) {
   WideString buffer;
-  while (context.hasMoreCharacters()) {
-    wchar_t c = context.getCurrentChar();
-    context.m_pos++;
+  while (context->hasMoreCharacters()) {
+    wchar_t c = context->getCurrentChar();
+    context->m_pos++;
+    int32_t e = BCExceptionNO;
     encodeChar(c, buffer, e);
-    if (e != BCExceptionNO) {
-      return;
-    }
+    if (e != BCExceptionNO)
+      return false;
+
     int32_t count = buffer.GetLength();
     if ((count % 3) == 0) {
-      writeNextTriplet(context, buffer);
+      writeNextTriplet(*context, buffer);
       int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
-          context.m_msg, context.m_pos, getEncodingMode());
+          context->m_msg, context->m_pos, getEncodingMode());
       if (newMode != getEncodingMode()) {
-        context.signalEncoderChange(newMode);
+        context->signalEncoderChange(newMode);
         break;
       }
     }
   }
-  handleEOD(context, buffer, e);
+  int32_t e = BCExceptionNO;
+  handleEOD(*context, buffer, e);
+  return e == BCExceptionNO;
 }
+
 void CBC_X12Encoder::handleEOD(CBC_EncoderContext& context,
                                WideString& buffer,
                                int32_t& e) {
diff --git a/fxbarcode/datamatrix/BC_X12Encoder.h b/fxbarcode/datamatrix/BC_X12Encoder.h
index 7ef84d5..b7950f6 100644
--- a/fxbarcode/datamatrix/BC_X12Encoder.h
+++ b/fxbarcode/datamatrix/BC_X12Encoder.h
@@ -16,7 +16,7 @@
 
   // CBC_C40Encoder
   int32_t getEncodingMode() override;
-  void Encode(CBC_EncoderContext& context, int32_t& e) override;
+  bool Encode(CBC_EncoderContext* context) override;
   void handleEOD(CBC_EncoderContext& context,
                  WideString& buffer,
                  int32_t& e) override;
diff --git a/fxbarcode/utils.h b/fxbarcode/utils.h
index 7b9bb5d..2ed6a0f 100644
--- a/fxbarcode/utils.h
+++ b/fxbarcode/utils.h
@@ -52,7 +52,6 @@
 #define BCExceptionCharactersOutsideISO88591Encoding 87
 #define BCExceptionIllegalDataCodewords 88
 #define BCExceptionIllegalStateUnexpectedCase 90
-#define BCExceptionIllegalStateMessageLengthInvalid 92
 #define BCExceptionGeneric 107
 
 #endif  // FXBARCODE_UTILS_H_