| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 // Original code is licensed as follows: | |
| 7 /* | |
| 8 * Copyright 2007 ZXing authors | |
| 9 * | |
| 10 * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 11 * you may not use this file except in compliance with the License. | |
| 12 * You may obtain a copy of the License at | |
| 13 * | |
| 14 * http://www.apache.org/licenses/LICENSE-2.0 | |
| 15 * | |
| 16 * Unless required by applicable law or agreed to in writing, software | |
| 17 * distributed under the License is distributed on an "AS IS" BASIS, | |
| 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 19 * See the License for the specific language governing permissions and | |
| 20 * limitations under the License. | |
| 21 */ | |
| 22 | |
| 23 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomon.h" | |
| 24 | |
| 25 #include <memory> | |
| 26 | |
| 27 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" | |
| 28 #include "xfa/src/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" | |
| 29 | |
| 30 CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) { | |
| 31 m_field = field; | |
| 32 } | |
| 33 void CBC_ReedSolomonEncoder::Init() { | |
| 34 m_cachedGenerators.Add(new CBC_ReedSolomonGF256Poly(m_field, 1)); | |
| 35 } | |
| 36 CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator(int32_t degree, | |
| 37 int32_t& e) { | |
| 38 if (degree >= m_cachedGenerators.GetSize()) { | |
| 39 CBC_ReedSolomonGF256Poly* lastGenerator = | |
| 40 (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators | |
| 41 [m_cachedGenerators.GetSize() - 1]); | |
| 42 for (int32_t d = m_cachedGenerators.GetSize(); d <= degree; d++) { | |
| 43 CFX_Int32Array temp; | |
| 44 temp.Add(1); | |
| 45 temp.Add(m_field->Exp(d - 1)); | |
| 46 CBC_ReedSolomonGF256Poly temp_poly; | |
| 47 temp_poly.Init(m_field, &temp, e); | |
| 48 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 49 CBC_ReedSolomonGF256Poly* nextGenerator = | |
| 50 lastGenerator->Multiply(&temp_poly, e); | |
| 51 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 52 m_cachedGenerators.Add(nextGenerator); | |
| 53 lastGenerator = nextGenerator; | |
| 54 } | |
| 55 } | |
| 56 return (CBC_ReedSolomonGF256Poly*)(m_cachedGenerators[degree]); | |
| 57 } | |
| 58 void CBC_ReedSolomonEncoder::Encode(CFX_Int32Array* toEncode, | |
| 59 int32_t ecBytes, | |
| 60 int32_t& e) { | |
| 61 if (ecBytes == 0) { | |
| 62 e = BCExceptionNoCorrectionBytes; | |
| 63 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 64 } | |
| 65 int32_t dataBytes = toEncode->GetSize() - ecBytes; | |
| 66 if (dataBytes <= 0) { | |
| 67 e = BCExceptionNoDataBytesProvided; | |
| 68 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 69 } | |
| 70 CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes, e); | |
| 71 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 72 CFX_Int32Array infoCoefficients; | |
| 73 infoCoefficients.SetSize(dataBytes); | |
| 74 for (int32_t x = 0; x < dataBytes; x++) { | |
| 75 infoCoefficients[x] = toEncode->operator[](x); | |
| 76 } | |
| 77 CBC_ReedSolomonGF256Poly info; | |
| 78 info.Init(m_field, &infoCoefficients, e); | |
| 79 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 80 std::unique_ptr<CBC_ReedSolomonGF256Poly> infoTemp( | |
| 81 info.MultiplyByMonomial(ecBytes, 1, e)); | |
| 82 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 83 std::unique_ptr<CFX_PtrArray> temp(infoTemp->Divide(generator, e)); | |
| 84 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 85 CBC_ReedSolomonGF256Poly* remainder = | |
| 86 (CBC_ReedSolomonGF256Poly*)(temp->operator[](1)); | |
| 87 CFX_Int32Array* coefficients = remainder->GetCoefficients(); | |
| 88 int32_t numZeroCoefficients = ecBytes - coefficients->GetSize(); | |
| 89 for (int32_t i = 0; i < numZeroCoefficients; i++) { | |
| 90 (*toEncode)[dataBytes + i] = 0; | |
| 91 } | |
| 92 for (int32_t y = 0; y < coefficients->GetSize(); y++) { | |
| 93 (*toEncode)[dataBytes + numZeroCoefficients + y] = | |
| 94 coefficients->operator[](y); | |
| 95 } | |
| 96 for (int32_t k = 0; k < temp->GetSize(); k++) { | |
| 97 delete (CBC_ReedSolomonGF256Poly*)(*temp)[k]; | |
| 98 } | |
| 99 } | |
| 100 CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() { | |
| 101 for (int32_t i = 0; i < m_cachedGenerators.GetSize(); i++) { | |
| 102 delete (CBC_ReedSolomonGF256Poly*)m_cachedGenerators[i]; | |
| 103 } | |
| 104 } | |
| OLD | NEW |