Home | History | Annotate | Download | only in datamatrix
      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 2008 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/barcode.h"
     24 #include "BC_DataMatrixVersion.h"
     25 #include "BC_DataMatrixDataBlock.h"
     26 CBC_DataMatrixDataBlock::~CBC_DataMatrixDataBlock() {}
     27 CBC_DataMatrixDataBlock::CBC_DataMatrixDataBlock(int32_t numDataCodewords,
     28                                                  CFX_ByteArray* codewords) {
     29   m_codewords.Copy(*codewords);
     30   m_numDataCodewords = numDataCodewords;
     31 }
     32 CFX_PtrArray* CBC_DataMatrixDataBlock::GetDataBlocks(
     33     CFX_ByteArray* rawCodewords,
     34     CBC_DataMatrixVersion* version,
     35     int32_t& e) {
     36   ECBlocks* ecBlocks = version->GetECBlocks();
     37   int32_t totalBlocks = 0;
     38   const CFX_PtrArray& ecBlockArray = ecBlocks->GetECBlocks();
     39   int32_t i;
     40   for (i = 0; i < ecBlockArray.GetSize(); i++) {
     41     totalBlocks += ((ECB*)ecBlockArray[i])->GetCount();
     42   }
     43   CBC_AutoPtr<CFX_PtrArray> result(new CFX_PtrArray());
     44   result->SetSize(totalBlocks);
     45   int32_t numResultBlocks = 0;
     46   int32_t j;
     47   for (j = 0; j < ecBlockArray.GetSize(); j++) {
     48     for (i = 0; i < ((ECB*)ecBlockArray[j])->GetCount(); i++) {
     49       int32_t numDataCodewords = ((ECB*)ecBlockArray[j])->GetDataCodewords();
     50       int32_t numBlockCodewords = ecBlocks->GetECCodewords() + numDataCodewords;
     51       CFX_ByteArray codewords;
     52       codewords.SetSize(numBlockCodewords);
     53       (*result)[numResultBlocks++] =
     54           new CBC_DataMatrixDataBlock(numDataCodewords, &codewords);
     55       codewords.SetSize(0);
     56     }
     57   }
     58   int32_t longerBlocksTotalCodewords =
     59       ((CBC_DataMatrixDataBlock*)(*result)[0])->GetCodewords()->GetSize();
     60   int32_t longerBlocksNumDataCodewords =
     61       longerBlocksTotalCodewords - ecBlocks->GetECCodewords();
     62   int32_t shorterBlocksNumDataCodewords = longerBlocksNumDataCodewords - 1;
     63   int32_t rawCodewordsOffset = 0;
     64   for (i = 0; i < shorterBlocksNumDataCodewords; i++) {
     65     int32_t j;
     66     for (j = 0; j < numResultBlocks; j++) {
     67       if (rawCodewordsOffset < rawCodewords->GetSize()) {
     68         ((CBC_DataMatrixDataBlock*)(*result)[j])
     69             ->GetCodewords()
     70             ->
     71             operator[](i) = (*rawCodewords)[rawCodewordsOffset++];
     72       }
     73     }
     74   }
     75   FX_BOOL specialVersion = version->GetVersionNumber() == 24;
     76   int32_t numLongerBlocks = specialVersion ? 8 : numResultBlocks;
     77   for (j = 0; j < numLongerBlocks; j++) {
     78     if (rawCodewordsOffset < rawCodewords->GetSize()) {
     79       ((CBC_DataMatrixDataBlock*)(*result)[j])
     80           ->GetCodewords()
     81           ->
     82           operator[](longerBlocksNumDataCodewords - 1) =
     83           (*rawCodewords)[rawCodewordsOffset++];
     84     }
     85   }
     86   int32_t max =
     87       ((CBC_DataMatrixDataBlock*)(*result)[0])->GetCodewords()->GetSize();
     88   for (i = longerBlocksNumDataCodewords; i < max; i++) {
     89     int32_t j;
     90     for (j = 0; j < numResultBlocks; j++) {
     91       int32_t iOffset = specialVersion && j > 7 ? i - 1 : i;
     92       if (rawCodewordsOffset < rawCodewords->GetSize()) {
     93         ((CBC_DataMatrixDataBlock*)(*result)[j])
     94             ->GetCodewords()
     95             ->
     96             operator[](iOffset) = (*rawCodewords)[rawCodewordsOffset++];
     97       }
     98     }
     99   }
    100   if (rawCodewordsOffset != rawCodewords->GetSize()) {
    101     e = BCExceptionIllegalArgument;
    102     return NULL;
    103   }
    104   return result.release();
    105 }
    106 int32_t CBC_DataMatrixDataBlock::GetNumDataCodewords() {
    107   return m_numDataCodewords;
    108 }
    109 CFX_ByteArray* CBC_DataMatrixDataBlock::GetCodewords() {
    110   return &m_codewords;
    111 }
    112