diff options
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp | 38 | ||||
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h | 7 | ||||
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp | 14 | ||||
-rw-r--r-- | fxbarcode/utils.h | 2 |
4 files changed, 21 insertions, 40 deletions
diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp index 3f761054c4..a5657e1747 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp @@ -76,28 +76,22 @@ CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() const { 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 @@ int32_t CBC_ReedSolomonGF256::Exp(int32_t a) { 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 9f0fcd316f..199e479710 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 @@ class CBC_ReedSolomonGF256 { 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 ef83691d4f..0c56c6e60b 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp @@ -210,22 +210,22 @@ std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256Poly::Divide( 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 9e57d8f87d..fcdbaa5a74 100644 --- a/fxbarcode/utils.h +++ b/fxbarcode/utils.h @@ -44,8 +44,6 @@ enum BCFORMAT { #define BCExceptionNO 0 #define BCExceptionIllegalArgument 16 -#define BCExceptionDegreeIsNegative 31 -#define BCExceptionAIsZero 37 #define BCExceptionValueMustBeEither0or1 50 #define BCExceptionBadIndexException 52 #define BCExceptionUnsupportedMode 64 |