diff options
author | Lei Zhang <thestig@chromium.org> | 2017-04-05 18:42:22 -0700 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-04-06 01:58:05 +0000 |
commit | 1b22880748c3f3b3740699ae4c953a33f65ad10f (patch) | |
tree | 57d7155639ea2f19dea22cb08ec3ce0b76bcbf9b /fxbarcode/common/reedsolomon | |
parent | 0b18e1599dc9d07355c4ab6a069de33a536f7ba8 (diff) | |
download | pdfium-1b22880748c3f3b3740699ae4c953a33f65ad10f.tar.xz |
Change some fxbarcode to use return values.
No caller cares about the exception values anyway. Remove the unused
ones. Also use more std::unique_ptr to stop potential leaks.
Change-Id: Ic5955fb0d879f55e1c6a005c0204df50246dab19
Reviewed-on: https://pdfium-review.googlesource.com/3715
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'fxbarcode/common/reedsolomon')
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp | 103 | ||||
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomon.h | 13 | ||||
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp | 15 | ||||
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h | 10 | ||||
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp | 169 | ||||
-rw-r--r-- | fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h | 40 |
6 files changed, 166 insertions, 184 deletions
diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp b/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp index bb33bbd0e9..ca65f72043 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomon.cpp @@ -23,83 +23,78 @@ #include "fxbarcode/common/reedsolomon/BC_ReedSolomon.h" #include <memory> +#include <utility> #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" +#include "third_party/base/ptr_util.h" -CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) { - m_field = field; -} +CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) + : m_field(field) {} + +CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {} void CBC_ReedSolomonEncoder::Init() { - m_cachedGenerators.push_back(new CBC_ReedSolomonGF256Poly(m_field, 1)); + m_cachedGenerators.push_back( + pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(m_field, 1)); } -CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(size_t degree, - int32_t& e) { +CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator( + size_t degree) { if (degree >= m_cachedGenerators.size()) { - CBC_ReedSolomonGF256Poly* lastGenerator = m_cachedGenerators.back(); + CBC_ReedSolomonGF256Poly* lastGenerator = m_cachedGenerators.back().get(); for (size_t d = m_cachedGenerators.size(); d <= degree; ++d) { std::vector<int32_t> temp = {1, m_field->Exp(d - 1)}; CBC_ReedSolomonGF256Poly temp_poly; - temp_poly.Init(m_field, &temp, e); - if (e != BCExceptionNO) + if (!temp_poly.Init(m_field, &temp)) return nullptr; - CBC_ReedSolomonGF256Poly* nextGenerator = - lastGenerator->Multiply(&temp_poly, e); - if (e != BCExceptionNO) + + auto nextGenerator = lastGenerator->Multiply(&temp_poly); + if (!nextGenerator) return nullptr; - m_cachedGenerators.push_back(nextGenerator); - lastGenerator = nextGenerator; + + lastGenerator = nextGenerator.get(); + m_cachedGenerators.push_back(std::move(nextGenerator)); } } - return m_cachedGenerators[degree]; + return m_cachedGenerators[degree].get(); } -void CBC_ReedSolomonEncoder::Encode(std::vector<int32_t>* toEncode, - size_t ecBytes, - int32_t& e) { - if (ecBytes == 0) { - e = BCExceptionNoCorrectionBytes; - return; - } - if (toEncode->size() <= ecBytes) { - e = BCExceptionNoDataBytesProvided; - return; - } - CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes, e); - if (e != BCExceptionNO) - return; +bool CBC_ReedSolomonEncoder::Encode(std::vector<int32_t>* toEncode, + size_t ecBytes) { + if (ecBytes == 0) + return false; + + if (toEncode->size() <= ecBytes) + return false; + + CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes); + if (!generator) + return false; + size_t dataBytes = toEncode->size() - ecBytes; std::vector<int32_t> infoCoefficients(dataBytes); - for (size_t x = 0; x < dataBytes; x++) { + for (size_t x = 0; x < dataBytes; x++) infoCoefficients[x] = (*toEncode)[x]; - } + CBC_ReedSolomonGF256Poly info; - info.Init(m_field, &infoCoefficients, e); - if (e != BCExceptionNO) - return; - std::unique_ptr<CBC_ReedSolomonGF256Poly> infoTemp( - info.MultiplyByMonomial(ecBytes, 1, e)); - if (e != BCExceptionNO) - return; - std::unique_ptr<std::vector<CBC_ReedSolomonGF256Poly*>> temp( - infoTemp->Divide(generator, e)); - if (e != BCExceptionNO) - return; - CBC_ReedSolomonGF256Poly* remainder = (*temp)[1]; - std::vector<int32_t>* coefficients = remainder->GetCoefficients(); + if (!info.Init(m_field, &infoCoefficients)) + return false; + + auto infoTemp = info.MultiplyByMonomial(ecBytes, 1); + if (!infoTemp) + return false; + + auto remainder = infoTemp->Divide(generator); + if (!remainder) + return false; + + const auto& coefficients = remainder->GetCoefficients(); size_t numZeroCoefficients = - ecBytes > coefficients->size() ? ecBytes - coefficients->size() : 0; + ecBytes > coefficients.size() ? ecBytes - coefficients.size() : 0; for (size_t i = 0; i < numZeroCoefficients; i++) (*toEncode)[dataBytes + i] = 0; - for (size_t y = 0; y < coefficients->size(); y++) - (*toEncode)[dataBytes + numZeroCoefficients + y] = (*coefficients)[y]; - for (size_t k = 0; k < temp->size(); k++) - delete (*temp)[k]; -} - -CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() { - for (size_t i = 0; i < m_cachedGenerators.size(); i++) - delete m_cachedGenerators[i]; + for (size_t y = 0; y < coefficients.size(); y++) + (*toEncode)[dataBytes + numZeroCoefficients + y] = coefficients[y]; + return true; } diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomon.h b/fxbarcode/common/reedsolomon/BC_ReedSolomon.h index 42a1e002ca..68d4ee0624 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomon.h +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomon.h @@ -7,6 +7,7 @@ #ifndef FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ #define FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ +#include <memory> #include <vector> #include "core/fxcrt/fx_basic.h" @@ -17,16 +18,16 @@ class CBC_ReedSolomonGF256Poly; class CBC_ReedSolomonEncoder { public: explicit CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field); - virtual ~CBC_ReedSolomonEncoder(); + ~CBC_ReedSolomonEncoder(); - void Encode(std::vector<int32_t>* toEncode, size_t ecBytes, int32_t& e); - virtual void Init(); + void Init(); + bool Encode(std::vector<int32_t>* toEncode, size_t ecBytes); private: - CBC_ReedSolomonGF256Poly* BuildGenerator(size_t degree, int32_t& e); + CBC_ReedSolomonGF256Poly* BuildGenerator(size_t degree); - CBC_ReedSolomonGF256* m_field; - std::vector<CBC_ReedSolomonGF256Poly*> m_cachedGenerators; + CBC_ReedSolomonGF256* const m_field; + std::vector<std::unique_ptr<CBC_ReedSolomonGF256Poly>> m_cachedGenerators; }; #endif // FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMON_H_ diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp index b2af2a54b4..3f761054c4 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.cpp @@ -74,7 +74,7 @@ CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::GetOne() const { return m_one.get(); } -CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial( +std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256::BuildMonomial( int32_t degree, int32_t coefficient, int32_t& e) { @@ -83,17 +83,18 @@ CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256::BuildMonomial( return nullptr; } if (coefficient == 0) { - CBC_ReedSolomonGF256Poly* temp = m_zero->Clone(e); - if (e != BCExceptionNO) - return nullptr; + auto temp = m_zero->Clone(); + if (!temp) + e = BCExceptionGeneric; return temp; } std::vector<int32_t> coefficients(degree + 1); coefficients[0] = coefficient; - CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); - temp->Init(this, &coefficients, e); - if (e != BCExceptionNO) + auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(); + if (!temp->Init(this, &coefficients)) { + e = BCExceptionGeneric; return nullptr; + } return temp; } diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h index d4b303c5ff..6d8225dfb0 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h @@ -17,22 +17,22 @@ class CBC_ReedSolomonGF256Poly; class CBC_ReedSolomonGF256 { public: explicit CBC_ReedSolomonGF256(int32_t primitive); - virtual ~CBC_ReedSolomonGF256(); + ~CBC_ReedSolomonGF256(); static void Initialize(); static void Finalize(); CBC_ReedSolomonGF256Poly* GetZero() const; CBC_ReedSolomonGF256Poly* GetOne() const; - CBC_ReedSolomonGF256Poly* BuildMonomial(int32_t degree, - int32_t coefficient, - int32_t& e); + std::unique_ptr<CBC_ReedSolomonGF256Poly> BuildMonomial(int32_t degree, + int32_t coefficient, + int32_t& e); 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); int32_t Multiply(int32_t a, int32_t b); - virtual void Init(); + void Init(); static CBC_ReedSolomonGF256* QRCodeField; static CBC_ReedSolomonGF256* DataMatrixField; diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp index a062589218..5c26188f53 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.cpp @@ -26,6 +26,7 @@ #include <utility> #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, @@ -41,13 +42,11 @@ CBC_ReedSolomonGF256Poly::CBC_ReedSolomonGF256Poly() { m_field = nullptr; } -void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field, - std::vector<int32_t>* coefficients, - int32_t& e) { - if (!coefficients || coefficients->empty()) { - e = BCExceptionCoefficientsSizeIsNull; - return; - } +bool CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field, + const std::vector<int32_t>* coefficients) { + if (!coefficients || coefficients->empty()) + return false; + m_field = field; size_t coefficientsLength = coefficients->size(); if (coefficientsLength > 1 && coefficients->front() == 0) { @@ -57,7 +56,7 @@ void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field, firstNonZero++; } if (firstNonZero == coefficientsLength) { - m_coefficients = *(m_field->GetZero()->GetCoefficients()); + m_coefficients = m_field->GetZero()->GetCoefficients(); } else { m_coefficients.resize(coefficientsLength - firstNonZero); for (size_t i = firstNonZero, j = 0; i < coefficientsLength; i++, j++) @@ -66,28 +65,29 @@ void CBC_ReedSolomonGF256Poly::Init(CBC_ReedSolomonGF256* field, } else { m_coefficients = *coefficients; } + return true; } -std::vector<int32_t>* CBC_ReedSolomonGF256Poly::GetCoefficients() { - return &m_coefficients; +const std::vector<int32_t>& CBC_ReedSolomonGF256Poly::GetCoefficients() const { + return m_coefficients; } -int32_t CBC_ReedSolomonGF256Poly::GetDegree() { +int32_t CBC_ReedSolomonGF256Poly::GetDegree() const { return pdfium::CollectionSize<int32_t>(m_coefficients) - 1; } -bool CBC_ReedSolomonGF256Poly::IsZero() { +bool CBC_ReedSolomonGF256Poly::IsZero() const { return m_coefficients.front() == 0; } -int32_t CBC_ReedSolomonGF256Poly::GetCoefficients(int32_t degree) { +int32_t CBC_ReedSolomonGF256Poly::GetCoefficients(int32_t degree) const { return m_coefficients[m_coefficients.size() - 1 - degree]; } int32_t CBC_ReedSolomonGF256Poly::EvaluateAt(int32_t a) { - if (a == 0) { + if (a == 0) return GetCoefficients(0); - } + size_t size = m_coefficients.size(); if (a == 1) { int32_t result = 0; @@ -103,121 +103,114 @@ int32_t CBC_ReedSolomonGF256Poly::EvaluateAt(int32_t a) { return result; } -CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Clone(int32_t& e) { - CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); - temp->Init(m_field, &m_coefficients, e); - if (e != BCExceptionNO) +std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256Poly::Clone() + const { + auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(); + if (!temp->Init(m_field, &m_coefficients)) return nullptr; return temp; } -CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::AddOrSubtract( - CBC_ReedSolomonGF256Poly* other, - int32_t& e) { + +std::unique_ptr<CBC_ReedSolomonGF256Poly> +CBC_ReedSolomonGF256Poly::AddOrSubtract(const CBC_ReedSolomonGF256Poly* other) { if (IsZero()) - return other->Clone(e); + return other->Clone(); if (other->IsZero()) - return Clone(e); + return Clone(); std::vector<int32_t> smallerCoefficients = m_coefficients; - std::vector<int32_t> largerCoefficients = *(other->GetCoefficients()); - if (smallerCoefficients.size() > largerCoefficients.size()) { + std::vector<int32_t> largerCoefficients = other->GetCoefficients(); + if (smallerCoefficients.size() > largerCoefficients.size()) std::swap(smallerCoefficients, largerCoefficients); - } + std::vector<int32_t> sumDiff(largerCoefficients.size()); size_t lengthDiff = largerCoefficients.size() - smallerCoefficients.size(); - for (size_t i = 0; i < lengthDiff; i++) { + for (size_t i = 0; i < lengthDiff; i++) sumDiff[i] = largerCoefficients[i]; - } + for (size_t j = lengthDiff; j < largerCoefficients.size(); j++) { sumDiff[j] = CBC_ReedSolomonGF256::AddOrSubtract( smallerCoefficients[j - lengthDiff], largerCoefficients[j]); } - CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); - temp->Init(m_field, &sumDiff, e); - if (e != BCExceptionNO) + auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(); + if (!temp->Init(m_field, &sumDiff)) return nullptr; return temp; } -CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply( - CBC_ReedSolomonGF256Poly* other, - int32_t& e) { + +std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256Poly::Multiply( + const CBC_ReedSolomonGF256Poly* other) { if (IsZero() || other->IsZero()) - return m_field->GetZero()->Clone(e); + return m_field->GetZero()->Clone(); - std::vector<int32_t> aCoefficients = m_coefficients; - std::vector<int32_t> bCoefficients = *(other->GetCoefficients()); + const std::vector<int32_t>& aCoefficients = m_coefficients; + const std::vector<int32_t>& bCoefficients = other->GetCoefficients(); size_t aLength = aCoefficients.size(); size_t bLength = bCoefficients.size(); std::vector<int32_t> product(aLength + bLength - 1); for (size_t i = 0; i < aLength; i++) { - int32_t aCoeff = m_coefficients[i]; + int32_t aCoeff = aCoefficients[i]; for (size_t j = 0; j < bLength; j++) { product[i + j] = CBC_ReedSolomonGF256::AddOrSubtract( - product[i + j], - m_field->Multiply(aCoeff, (*other->GetCoefficients())[j])); + product[i + j], m_field->Multiply(aCoeff, bCoefficients[j])); } } - CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); - temp->Init(m_field, &product, e); - if (e != BCExceptionNO) + auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(); + if (!temp->Init(m_field, &product)) return nullptr; return temp; } -CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::Multiply(int32_t scalar, - int32_t& e) { + +std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256Poly::Multiply( + int32_t scalar) { if (scalar == 0) - return m_field->GetZero()->Clone(e); + return m_field->GetZero()->Clone(); if (scalar == 1) - return Clone(e); + return Clone(); size_t size = m_coefficients.size(); std::vector<int32_t> product(size); - for (size_t i = 0; i < size; i++) { + for (size_t i = 0; i < size; i++) product[i] = m_field->Multiply(m_coefficients[i], scalar); - } - CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); - temp->Init(m_field, &product, e); - if (e != BCExceptionNO) + + auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(); + if (!temp->Init(m_field, &product)) return nullptr; return temp; } -CBC_ReedSolomonGF256Poly* CBC_ReedSolomonGF256Poly::MultiplyByMonomial( - int32_t degree, - int32_t coefficient, - int32_t& e) { - if (degree < 0) { - e = BCExceptionDegreeIsNegative; + +std::unique_ptr<CBC_ReedSolomonGF256Poly> +CBC_ReedSolomonGF256Poly::MultiplyByMonomial(int32_t degree, + int32_t coefficient) const { + if (degree < 0) return nullptr; - } if (coefficient == 0) - return m_field->GetZero()->Clone(e); + return m_field->GetZero()->Clone(); size_t size = m_coefficients.size(); std::vector<int32_t> product(size + degree); - for (size_t i = 0; i < size; i++) { + for (size_t i = 0; i < size; i++) product[i] = m_field->Multiply(m_coefficients[i], coefficient); - } - CBC_ReedSolomonGF256Poly* temp = new CBC_ReedSolomonGF256Poly(); - temp->Init(m_field, &product, e); - if (e != BCExceptionNO) + + auto temp = pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(); + if (!temp->Init(m_field, &product)) return nullptr; return temp; } -std::vector<CBC_ReedSolomonGF256Poly*>* CBC_ReedSolomonGF256Poly::Divide( - CBC_ReedSolomonGF256Poly* other, - int32_t& e) { - if (other->IsZero()) { - e = BCExceptionDivideByZero; +std::unique_ptr<CBC_ReedSolomonGF256Poly> CBC_ReedSolomonGF256Poly::Divide( + const CBC_ReedSolomonGF256Poly* other) { + if (other->IsZero()) return nullptr; - } - std::unique_ptr<CBC_ReedSolomonGF256Poly> quotient( - m_field->GetZero()->Clone(e)); - if (e != BCExceptionNO) + + auto quotient = m_field->GetZero()->Clone(); + if (!quotient) return nullptr; - std::unique_ptr<CBC_ReedSolomonGF256Poly> remainder(Clone(e)); - if (e != BCExceptionNO) + auto remainder = Clone(); + if (!remainder) return nullptr; + + int e = BCExceptionNO; int32_t denominatorLeadingTerm = other->GetCoefficients(other->GetDegree()); int32_t inverseDenominatorLeadingTeam = m_field->Inverse(denominatorLeadingTerm, e); @@ -228,26 +221,20 @@ std::vector<CBC_ReedSolomonGF256Poly*>* CBC_ReedSolomonGF256Poly::Divide( int32_t scale = m_field->Multiply(remainder->GetCoefficients((remainder->GetDegree())), inverseDenominatorLeadingTeam); - std::unique_ptr<CBC_ReedSolomonGF256Poly> term( - other->MultiplyByMonomial(degreeDifference, scale, e)); - if (e != BCExceptionNO) + auto term = other->MultiplyByMonomial(degreeDifference, scale); + if (!term) return nullptr; - std::unique_ptr<CBC_ReedSolomonGF256Poly> iteratorQuotient( - m_field->BuildMonomial(degreeDifference, scale, e)); + auto iteratorQuotient = m_field->BuildMonomial(degreeDifference, scale, e); if (e != BCExceptionNO) return nullptr; - quotient.reset(quotient->AddOrSubtract(iteratorQuotient.get(), e)); - if (e != BCExceptionNO) + quotient = quotient->AddOrSubtract(iteratorQuotient.get()); + if (!quotient) return nullptr; - remainder.reset(remainder->AddOrSubtract(term.get(), e)); - if (e != BCExceptionNO) + remainder = remainder->AddOrSubtract(term.get()); + if (!remainder) return nullptr; } - std::vector<CBC_ReedSolomonGF256Poly*>* tempPtrA = - new std::vector<CBC_ReedSolomonGF256Poly*>(); - tempPtrA->push_back(quotient.release()); - tempPtrA->push_back(remainder.release()); - return tempPtrA; + return remainder; } CBC_ReedSolomonGF256Poly::~CBC_ReedSolomonGF256Poly() {} diff --git a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h index 284b531e81..ce8a572704 100644 --- a/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h +++ b/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h @@ -7,6 +7,7 @@ #ifndef FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ #define FXBARCODE_COMMON_REEDSOLOMON_BC_REEDSOLOMONGF256POLY_H_ +#include <memory> #include <vector> #include "core/fxcrt/fx_basic.h" @@ -18,28 +19,25 @@ class CBC_ReedSolomonGF256Poly final { CBC_ReedSolomonGF256Poly(CBC_ReedSolomonGF256* field, int32_t coefficients); CBC_ReedSolomonGF256Poly(); ~CBC_ReedSolomonGF256Poly(); - void Init(CBC_ReedSolomonGF256* field, - std::vector<int32_t>* coefficients, - int32_t& e); - - int32_t GetCoefficients(int32_t degree); - std::vector<int32_t>* GetCoefficients(); - int32_t GetDegree(); - bool IsZero(); + bool Init(CBC_ReedSolomonGF256* field, + const std::vector<int32_t>* coefficients); + + int32_t GetCoefficients(int32_t degree) const; + const std::vector<int32_t>& GetCoefficients() const; + int32_t GetDegree() const; + bool IsZero() const; int32_t EvaluateAt(int32_t a); - CBC_ReedSolomonGF256Poly* AddOrSubtract(CBC_ReedSolomonGF256Poly* other, - int32_t& e); - CBC_ReedSolomonGF256Poly* Multiply(CBC_ReedSolomonGF256Poly* other, - int32_t& e); - CBC_ReedSolomonGF256Poly* Multiply(int32_t scalar, int32_t& e); - CBC_ReedSolomonGF256Poly* MultiplyByMonomial(int32_t degree, - int32_t coefficient, - int32_t& e); - std::vector<CBC_ReedSolomonGF256Poly*>* Divide( - CBC_ReedSolomonGF256Poly* other, - int32_t& e); - - CBC_ReedSolomonGF256Poly* Clone(int32_t& e); + std::unique_ptr<CBC_ReedSolomonGF256Poly> AddOrSubtract( + const CBC_ReedSolomonGF256Poly* other); + std::unique_ptr<CBC_ReedSolomonGF256Poly> Multiply( + const CBC_ReedSolomonGF256Poly* other); + std::unique_ptr<CBC_ReedSolomonGF256Poly> Multiply(int32_t scalar); + std::unique_ptr<CBC_ReedSolomonGF256Poly> MultiplyByMonomial( + int32_t degree, + int32_t coefficient) const; + std::unique_ptr<CBC_ReedSolomonGF256Poly> Divide( + const CBC_ReedSolomonGF256Poly* other); + std::unique_ptr<CBC_ReedSolomonGF256Poly> Clone() const; private: CBC_ReedSolomonGF256* m_field; |