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 "fxbarcode/common/reedsolomon/BC_ReedSolomon.h" 24 25#include <memory> 26#include <utility> 27 28#include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" 29#include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256Poly.h" 30#include "third_party/base/ptr_util.h" 31 32CBC_ReedSolomonEncoder::CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256* field) 33 : m_field(field) {} 34 35CBC_ReedSolomonEncoder::~CBC_ReedSolomonEncoder() {} 36 37void CBC_ReedSolomonEncoder::Init() { 38 m_cachedGenerators.push_back( 39 pdfium::MakeUnique<CBC_ReedSolomonGF256Poly>(m_field.Get(), 1)); 40} 41 42CBC_ReedSolomonGF256Poly* CBC_ReedSolomonEncoder::BuildGenerator( 43 size_t degree) { 44 if (degree >= m_cachedGenerators.size()) { 45 CBC_ReedSolomonGF256Poly* lastGenerator = m_cachedGenerators.back().get(); 46 for (size_t d = m_cachedGenerators.size(); d <= degree; ++d) { 47 std::vector<int32_t> temp = {1, m_field->Exp(d - 1)}; 48 CBC_ReedSolomonGF256Poly temp_poly; 49 if (!temp_poly.Init(m_field.Get(), &temp)) 50 return nullptr; 51 52 auto nextGenerator = lastGenerator->Multiply(&temp_poly); 53 if (!nextGenerator) 54 return nullptr; 55 56 lastGenerator = nextGenerator.get(); 57 m_cachedGenerators.push_back(std::move(nextGenerator)); 58 } 59 } 60 return m_cachedGenerators[degree].get(); 61} 62 63bool CBC_ReedSolomonEncoder::Encode(std::vector<int32_t>* toEncode, 64 size_t ecBytes) { 65 if (ecBytes == 0) 66 return false; 67 68 if (toEncode->size() <= ecBytes) 69 return false; 70 71 CBC_ReedSolomonGF256Poly* generator = BuildGenerator(ecBytes); 72 if (!generator) 73 return false; 74 75 size_t dataBytes = toEncode->size() - ecBytes; 76 std::vector<int32_t> infoCoefficients(dataBytes); 77 for (size_t x = 0; x < dataBytes; x++) 78 infoCoefficients[x] = (*toEncode)[x]; 79 80 CBC_ReedSolomonGF256Poly info; 81 if (!info.Init(m_field.Get(), &infoCoefficients)) 82 return false; 83 84 auto infoTemp = info.MultiplyByMonomial(ecBytes, 1); 85 if (!infoTemp) 86 return false; 87 88 auto remainder = infoTemp->Divide(generator); 89 if (!remainder) 90 return false; 91 92 const auto& coefficients = remainder->GetCoefficients(); 93 size_t numZeroCoefficients = 94 ecBytes > coefficients.size() ? ecBytes - coefficients.size() : 0; 95 for (size_t i = 0; i < numZeroCoefficients; i++) 96 (*toEncode)[dataBytes + i] = 0; 97 for (size_t y = 0; y < coefficients.size(); y++) 98 (*toEncode)[dataBytes + numZeroCoefficients + y] = coefficients[y]; 99 return true; 100} 101