Remove some "exceptions" from reedsolomon code.

Change-Id: If8c45af624ed6df7b6d7416bb4e195f4097b0574
Reviewed-on: https://pdfium-review.googlesource.com/43191
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp
index 3f76105..a5657e1 100644
--- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp
+++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp
@@ -76,28 +76,22 @@
 
 std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256::BuildMonomial(
     int32_t degree,
-    int32_t coefficient,
-    int32_t& e) {
-  if (degree < 0) {
-    e = BCExceptionDegreeIsNegative;
+    int32_t coefficient) {
+  if (degree < 0)
     return nullptr;
-  }
-  if (coefficient == 0) {
-    auto temp = m_zero->Clone();
-    if (!temp)
-      e = BCExceptionGeneric;
-    return temp;
-  }
+
+  if (coefficient == 0)
+    return m_zero->Clone();
+
   std::vector<int32_t> coefficients(degree + 1);
   coefficients[0] = coefficient;
   auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>();
-  if (!temp->Init(this, &coefficients)) {
-    e = BCExceptionGeneric;
+  if (!temp->Init(this, &coefficients))
     return nullptr;
-  }
   return temp;
 }
 
+// static
 int32_t CBC_ReedSolomonGF256::AddOrSubtract(int32_t a, int32_t b) {
   return a ^ b;
 }
@@ -106,19 +100,9 @@
   return m_expTable[a];
 }
 
-int32_t CBC_ReedSolomonGF256::Log(int32_t a, int32_t& e) {
-  if (a == 0) {
-    e = BCExceptionAIsZero;
-    return 0;
-  }
-  return m_logTable[a];
-}
-
-int32_t CBC_ReedSolomonGF256::Inverse(int32_t a, int32_t& e) {
-  if (a == 0) {
-    e = BCExceptionAIsZero;
-    return 0;
-  }
+Optional<int32_t> CBC_ReedSolomonGF256::Inverse(int32_t a) {
+  if (a == 0)
+    return {};
   return m_expTable[255 - m_logTable[a]];
 }
 
diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h
index 9f0fcd3..199e479 100644
--- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h
+++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h
@@ -10,6 +10,7 @@
 #include <memory>
 
 #include "fxbarcode/utils.h"
+#include "third_party/base/optional.h"
 
 class CBC_ReedSolomonGF256Poly;
 
@@ -24,12 +25,10 @@
   CBC_ReedSolomonGF256Poly* GetZero() const;
   CBC_ReedSolomonGF256Poly* GetOne() const;
   std::unique_ptr<CBC_ReedSolomonGF256Poly> BuildMonomial(int32_t degree,
-                                                          int32_t coefficient,
-                                                          int32_t& e);
+                                                          int32_t coefficient);
   static int32_t AddOrSubtract(int32_t a, int32_t b);
   int32_t Exp(int32_t a);
-  int32_t Log(int32_t a, int32_t& e);
-  int32_t Inverse(int32_t a, int32_t& e);
+  Optional<int32_t> Inverse(int32_t a);
   int32_t Multiply(int32_t a, int32_t b);
   void Init();
 
diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp
index ef83691..0c56c6e 100644
--- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp
+++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp
@@ -210,22 +210,22 @@
   if (!remainder)
     return nullptr;
 
-  int e = BCExceptionNO;
   int32_t denominatorLeadingTerm = other->GetCoefficients(other->GetDegree());
-  int32_t inverseDenominatorLeadingTeam =
-      m_field->Inverse(denominatorLeadingTerm, e);
-  if (e != BCExceptionNO)
+  Optional<int32_t> inverseDenominatorLeadingTeam =
+      m_field->Inverse(denominatorLeadingTerm);
+  if (!inverseDenominatorLeadingTeam.has_value())
     return nullptr;
+
   while (remainder->GetDegree() >= other->GetDegree() && !remainder->IsZero()) {
     int32_t degreeDifference = remainder->GetDegree() - other->GetDegree();
     int32_t scale =
         m_field->Multiply(remainder->GetCoefficients((remainder->GetDegree())),
-                          inverseDenominatorLeadingTeam);
+                          inverseDenominatorLeadingTeam.value());
     auto term = other->MultiplyByMonomial(degreeDifference, scale);
     if (!term)
       return nullptr;
-    auto iteratorQuotient = m_field->BuildMonomial(degreeDifference, scale, e);
-    if (e != BCExceptionNO)
+    auto iteratorQuotient = m_field->BuildMonomial(degreeDifference, scale);
+    if (!iteratorQuotient)
       return nullptr;
     quotient = quotient->AddOrSubtract(iteratorQuotient.get());
     if (!quotient)
diff --git a/fxbarcode/utils.h b/fxbarcode/utils.h
index 9e57d8f..fcdbaa5 100644
--- a/fxbarcode/utils.h
+++ b/fxbarcode/utils.h
@@ -44,8 +44,6 @@
 
 #define BCExceptionNO 0
 #define BCExceptionIllegalArgument 16
-#define BCExceptionDegreeIsNegative 31
-#define BCExceptionAIsZero 37
 #define BCExceptionValueMustBeEither0or1 50
 #define BCExceptionBadIndexException 52
 #define BCExceptionUnsupportedMode 64