Get rid of "exceptions" in CBC_C40Encoder::HandleEOD().

Also change more code to not pass by non-const reference.

Change-Id: I8bb4f6131ad2355f698a8317fc5c11a1af3ff174
Reviewed-on: https://pdfium-review.googlesource.com/c/45759
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/datamatrix/BC_C40Encoder.cpp b/fxbarcode/datamatrix/BC_C40Encoder.cpp
index 7e8f342..daceb89 100644
--- a/fxbarcode/datamatrix/BC_C40Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_C40Encoder.cpp
@@ -97,55 +97,51 @@
       }
     }
   }
-  int32_t e = BCExceptionNO;
-  handleEOD(*context, buffer, e);
-  return e == BCExceptionNO;
+  return HandleEOD(context, &buffer);
 }
 
-void CBC_C40Encoder::writeNextTriplet(CBC_EncoderContext& context,
-                                      WideString& buffer) {
-  context.writeCodewords(EncodeToC40Codewords(buffer, 0));
-  buffer.Delete(0, 3);
+void CBC_C40Encoder::WriteNextTriplet(CBC_EncoderContext* context,
+                                      WideString* buffer) {
+  context->writeCodewords(EncodeToC40Codewords(*buffer, 0));
+  buffer->Delete(0, 3);
 }
-void CBC_C40Encoder::handleEOD(CBC_EncoderContext& context,
-                               WideString& buffer,
-                               int32_t& e) {
-  int32_t unwritten = (buffer.GetLength() / 3) * 2;
-  int32_t rest = buffer.GetLength() % 3;
-  int32_t curCodewordCount = context.getCodewordCount() + unwritten;
-  context.updateSymbolInfo(curCodewordCount, e);
-  if (e != BCExceptionNO) {
-    return;
-  }
-  int32_t available = context.m_symbolInfo->dataCapacity() - curCodewordCount;
+
+bool CBC_C40Encoder::HandleEOD(CBC_EncoderContext* context,
+                               WideString* buffer) {
+  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)
+    return false;
+
+  int32_t available = context->m_symbolInfo->dataCapacity() - curCodewordCount;
   if (rest == 2) {
-    buffer += (wchar_t)'\0';
-    while (buffer.GetLength() >= 3) {
-      writeNextTriplet(context, buffer);
-    }
-    if (context.hasMoreCharacters()) {
-      context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
+    *buffer += (wchar_t)'\0';
+    while (buffer->GetLength() >= 3)
+      WriteNextTriplet(context, buffer);
+    if (context->hasMoreCharacters()) {
+      context->writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
     }
   } else if (available == 1 && rest == 1) {
-    while (buffer.GetLength() >= 3) {
-      writeNextTriplet(context, buffer);
+    while (buffer->GetLength() >= 3)
+      WriteNextTriplet(context, buffer);
+    if (context->hasMoreCharacters()) {
+      context->writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
     }
-    if (context.hasMoreCharacters()) {
-      context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
-    }
-    context.m_pos--;
+    context->m_pos--;
   } else if (rest == 0) {
-    while (buffer.GetLength() >= 3) {
-      writeNextTriplet(context, buffer);
-    }
-    if (available > 0 || context.hasMoreCharacters()) {
-      context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
+    while (buffer->GetLength() >= 3)
+      WriteNextTriplet(context, buffer);
+    if (available > 0 || context->hasMoreCharacters()) {
+      context->writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
     }
   } else {
-    e = BCExceptionIllegalStateUnexpectedCase;
-    return;
+    return false;
   }
-  context.signalEncoderChange(ASCII_ENCODATION);
+  context->signalEncoderChange(ASCII_ENCODATION);
+  return true;
 }
 
 int32_t CBC_C40Encoder::encodeChar(wchar_t c, WideString& sb, int32_t& e) {
diff --git a/fxbarcode/datamatrix/BC_C40Encoder.h b/fxbarcode/datamatrix/BC_C40Encoder.h
index e20aac6..e60040c 100644
--- a/fxbarcode/datamatrix/BC_C40Encoder.h
+++ b/fxbarcode/datamatrix/BC_C40Encoder.h
@@ -19,11 +19,9 @@
   int32_t getEncodingMode() override;
   bool Encode(CBC_EncoderContext* context) override;
 
-  static void writeNextTriplet(CBC_EncoderContext& context, WideString& buffer);
+  static void WriteNextTriplet(CBC_EncoderContext* context, WideString* buffer);
 
-  virtual void handleEOD(CBC_EncoderContext& context,
-                         WideString& buffer,
-                         int32_t& e);
+  virtual bool HandleEOD(CBC_EncoderContext* context, WideString* buffer);
   virtual int32_t encodeChar(wchar_t c, WideString& sb, int32_t& e);
 
  private:
diff --git a/fxbarcode/datamatrix/BC_X12Encoder.cpp b/fxbarcode/datamatrix/BC_X12Encoder.cpp
index d0ff232..fa2941a 100644
--- a/fxbarcode/datamatrix/BC_X12Encoder.cpp
+++ b/fxbarcode/datamatrix/BC_X12Encoder.cpp
@@ -51,7 +51,7 @@
 
     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());
       if (newMode != getEncodingMode()) {
@@ -60,33 +60,33 @@
       }
     }
   }
-  int32_t e = BCExceptionNO;
-  handleEOD(*context, buffer, e);
-  return e == BCExceptionNO;
+  return HandleEOD(context, &buffer);
 }
 
-void CBC_X12Encoder::handleEOD(CBC_EncoderContext& context,
-                               WideString& buffer,
-                               int32_t& e) {
-  context.updateSymbolInfo(e);
-  if (e != BCExceptionNO) {
-    return;
-  }
+bool CBC_X12Encoder::HandleEOD(CBC_EncoderContext* context,
+                               WideString* buffer) {
+  int32_t e = BCExceptionNO;
+  context->updateSymbolInfo(e);
+  if (e != BCExceptionNO)
+    return false;
+
   int32_t available =
-      context.m_symbolInfo->dataCapacity() - context.getCodewordCount();
-  int32_t count = buffer.GetLength();
+      context->m_symbolInfo->dataCapacity() - context->getCodewordCount();
+  int32_t count = buffer->GetLength();
   if (count == 2) {
-    context.writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH);
-    context.m_pos -= 2;
-    context.signalEncoderChange(ASCII_ENCODATION);
+    context->writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH);
+    context->m_pos -= 2;
+    context->signalEncoderChange(ASCII_ENCODATION);
   } else if (count == 1) {
-    context.m_pos--;
+    context->m_pos--;
     if (available > 1) {
-      context.writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH);
+      context->writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH);
     }
-    context.signalEncoderChange(ASCII_ENCODATION);
+    context->signalEncoderChange(ASCII_ENCODATION);
   }
+  return true;
 }
+
 int32_t CBC_X12Encoder::encodeChar(wchar_t c, WideString& sb, int32_t& e) {
   if (c == '\r') {
     sb += (wchar_t)'\0';
diff --git a/fxbarcode/datamatrix/BC_X12Encoder.h b/fxbarcode/datamatrix/BC_X12Encoder.h
index b7950f6..cf78466 100644
--- a/fxbarcode/datamatrix/BC_X12Encoder.h
+++ b/fxbarcode/datamatrix/BC_X12Encoder.h
@@ -17,9 +17,7 @@
   // CBC_C40Encoder
   int32_t getEncodingMode() override;
   bool Encode(CBC_EncoderContext* context) override;
-  void handleEOD(CBC_EncoderContext& context,
-                 WideString& buffer,
-                 int32_t& e) override;
+  bool HandleEOD(CBC_EncoderContext* context, WideString* buffer) override;
   int32_t encodeChar(wchar_t c, WideString& sb, int32_t& e) override;
 };
 
diff --git a/fxbarcode/utils.h b/fxbarcode/utils.h
index 2ed6a0f..9fd1400 100644
--- a/fxbarcode/utils.h
+++ b/fxbarcode/utils.h
@@ -51,7 +51,6 @@
 #define BCExceptionCharacterNotThisMode 75
 #define BCExceptionCharactersOutsideISO88591Encoding 87
 #define BCExceptionIllegalDataCodewords 88
-#define BCExceptionIllegalStateUnexpectedCase 90
 #define BCExceptionGeneric 107
 
 #endif  // FXBARCODE_UTILS_H_