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 2006 Jeremias Maerki.
      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/datamatrix/BC_ErrorCorrection.h"
     24 
     25 #include <vector>
     26 
     27 #include "fxbarcode/datamatrix/BC_Encoder.h"
     28 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
     29 #include "fxbarcode/utils.h"
     30 
     31 namespace {
     32 
     33 const uint8_t FACTOR_SETS[] = {5,  7,  10, 11, 12, 14, 18, 20,
     34                                24, 28, 36, 42, 48, 56, 62, 68};
     35 
     36 const uint8_t FACTORS_0[5] = {228, 48, 15, 111, 62};
     37 const uint8_t FACTORS_1[7] = {23, 68, 144, 134, 240, 92, 254};
     38 const uint8_t FACTORS_2[10] = {28, 24, 185, 166, 223, 248, 116, 255, 110, 61};
     39 const uint8_t FACTORS_3[11] = {175, 138, 205, 12, 194, 168,
     40                                39,  245, 60,  97, 120};
     41 
     42 const uint8_t FACTORS_4[12] = {41,  153, 158, 91,  61,  42,
     43                                142, 213, 97,  178, 100, 242};
     44 
     45 const uint8_t FACTORS_5[14] = {156, 97,  192, 252, 95,  9,  157,
     46                                119, 138, 45,  18,  186, 83, 185};
     47 
     48 const uint8_t FACTORS_6[18] = {83,  195, 100, 39, 188, 75,  66, 61, 241,
     49                                213, 109, 129, 94, 254, 225, 48, 90, 188};
     50 
     51 const uint8_t FACTORS_7[20] = {15,  195, 244, 9,  233, 71, 168, 2,   188, 160,
     52                                153, 145, 253, 79, 108, 82, 27,  174, 186, 172};
     53 
     54 const uint8_t FACTORS_8[24] = {52,  190, 88,  205, 109, 39, 176, 21,
     55                                155, 197, 251, 223, 155, 21, 5,   172,
     56                                254, 124, 12,  181, 184, 96, 50,  193};
     57 
     58 const uint8_t FACTORS_9[28] = {211, 231, 43,  97,  71,  96,  103, 174, 37,  151,
     59                                170, 53,  75,  34,  249, 121, 17,  138, 110, 213,
     60                                141, 136, 120, 151, 233, 168, 93,  255};
     61 
     62 const uint8_t FACTORS_10[36] = {245, 127, 242, 218, 130, 250, 162, 181, 102,
     63                                 120, 84,  179, 220, 251, 80,  182, 229, 18,
     64                                 2,   4,   68,  33,  101, 137, 95,  119, 115,
     65                                 44,  175, 184, 59,  25,  225, 98,  81,  112};
     66 
     67 const uint8_t FACTORS_11[42] = {
     68     77,  193, 137, 31,  19,  38,  22,  153, 247, 105, 122, 2,   245, 133,
     69     242, 8,   175, 95,  100, 9,   167, 105, 214, 111, 57,  121, 21,  1,
     70     253, 57,  54,  101, 248, 202, 69,  50,  150, 177, 226, 5,   9,   5};
     71 
     72 const uint8_t FACTORS_12[48] = {
     73     245, 132, 172, 223, 96,  32,  117, 22,  238, 133, 238, 231,
     74     205, 188, 237, 87,  191, 106, 16,  147, 118, 23,  37,  90,
     75     170, 205, 131, 88,  120, 100, 66,  138, 186, 240, 82,  44,
     76     176, 87,  187, 147, 160, 175, 69,  213, 92,  253, 225, 19};
     77 
     78 const uint8_t FACTORS_13[56] = {
     79     175, 9,   223, 238, 12,  17,  220, 208, 100, 29,  175, 170, 230, 192,
     80     215, 235, 150, 159, 36,  223, 38,  200, 132, 54,  228, 146, 218, 234,
     81     117, 203, 29,  232, 144, 238, 22,  150, 201, 117, 62,  207, 164, 13,
     82     137, 245, 127, 67,  247, 28,  155, 43,  203, 107, 233, 53,  143, 46};
     83 
     84 const uint8_t FACTORS_14[62] = {
     85     242, 93,  169, 50,  144, 210, 39,  118, 202, 188, 201, 189, 143,
     86     108, 196, 37,  185, 112, 134, 230, 245, 63,  197, 190, 250, 106,
     87     185, 221, 175, 64,  114, 71,  161, 44,  147, 6,   27,  218, 51,
     88     63,  87,  10,  40,  130, 188, 17,  163, 31,  176, 170, 4,   107,
     89     232, 7,   94,  166, 224, 124, 86,  47,  11,  204};
     90 
     91 const uint8_t FACTORS_15[68] = {
     92     220, 228, 173, 89,  251, 149, 159, 56,  89,  33,  147, 244, 154, 36,
     93     73,  127, 213, 136, 248, 180, 234, 197, 158, 177, 68,  122, 93,  213,
     94     15,  160, 227, 236, 66,  139, 153, 185, 202, 167, 179, 25,  220, 232,
     95     96,  210, 231, 136, 223, 239, 181, 241, 59,  52,  172, 25,  49,  232,
     96     211, 189, 64,  54,  108, 153, 132, 63,  96,  103, 82,  186};
     97 
     98 const uint8_t* const FACTORS[16] = {
     99     FACTORS_0,  FACTORS_1,  FACTORS_2,  FACTORS_3, FACTORS_4,  FACTORS_5,
    100     FACTORS_6,  FACTORS_7,  FACTORS_8,  FACTORS_9, FACTORS_10, FACTORS_11,
    101     FACTORS_12, FACTORS_13, FACTORS_14, FACTORS_15};
    102 
    103 }  // namespace
    104 
    105 int32_t CBC_ErrorCorrection::MODULO_VALUE = 0x12D;
    106 int32_t CBC_ErrorCorrection::LOG[256] = {0};
    107 int32_t CBC_ErrorCorrection::ALOG[256] = {0};
    108 void CBC_ErrorCorrection::Initialize() {
    109   int32_t p = 1;
    110   for (int32_t i = 0; i < 255; i++) {
    111     ALOG[i] = p;
    112     LOG[p] = i;
    113     p <<= 1;
    114     if (p >= 256) {
    115       p ^= MODULO_VALUE;
    116     }
    117   }
    118 }
    119 void CBC_ErrorCorrection::Finalize() {}
    120 CBC_ErrorCorrection::CBC_ErrorCorrection() {}
    121 CBC_ErrorCorrection::~CBC_ErrorCorrection() {}
    122 WideString CBC_ErrorCorrection::encodeECC200(WideString codewords,
    123                                              CBC_SymbolInfo* symbolInfo,
    124                                              int32_t& e) {
    125   if (pdfium::base::checked_cast<int32_t>(codewords.GetLength()) !=
    126       symbolInfo->dataCapacity()) {
    127     e = BCExceptionIllegalArgument;
    128     return WideString();
    129   }
    130   WideString sb;
    131   sb += codewords;
    132   int32_t blockCount = symbolInfo->getInterleavedBlockCount();
    133   if (blockCount == 1) {
    134     WideString ecc = createECCBlock(codewords, symbolInfo->errorCodewords(), e);
    135     if (e != BCExceptionNO)
    136       return WideString();
    137     sb += ecc;
    138   } else {
    139     std::vector<int32_t> dataSizes(blockCount);
    140     std::vector<int32_t> errorSizes(blockCount);
    141     std::vector<int32_t> startPos(blockCount);
    142     for (int32_t i = 0; i < blockCount; i++) {
    143       dataSizes[i] = symbolInfo->getDataLengthForInterleavedBlock(i + 1);
    144       errorSizes[i] = symbolInfo->getErrorLengthForInterleavedBlock(i + 1);
    145       startPos[i] = 0;
    146       if (i > 0) {
    147         startPos[i] = startPos[i - 1] + dataSizes[i];
    148       }
    149     }
    150     for (int32_t block = 0; block < blockCount; block++) {
    151       WideString temp;
    152       for (int32_t d = block; d < symbolInfo->dataCapacity(); d += blockCount) {
    153         temp += (wchar_t)codewords[d];
    154       }
    155       WideString ecc = createECCBlock(temp, errorSizes[block], e);
    156       if (e != BCExceptionNO)
    157         return WideString();
    158       int32_t pos = 0;
    159       for (int32_t l = block; l < errorSizes[block] * blockCount;
    160            l += blockCount) {
    161         sb.SetAt(symbolInfo->dataCapacity() + l, ecc[pos++]);
    162       }
    163     }
    164   }
    165   return sb;
    166 }
    167 WideString CBC_ErrorCorrection::createECCBlock(WideString codewords,
    168                                                int32_t numECWords,
    169                                                int32_t& e) {
    170   return createECCBlock(codewords, 0, codewords.GetLength(), numECWords, e);
    171 }
    172 WideString CBC_ErrorCorrection::createECCBlock(WideString codewords,
    173                                                int32_t start,
    174                                                int32_t len,
    175                                                int32_t numECWords,
    176                                                int32_t& e) {
    177   static const size_t kFactorTableNum = sizeof(FACTOR_SETS) / sizeof(int32_t);
    178   size_t table = 0;
    179   while (table < kFactorTableNum && FACTOR_SETS[table] != numECWords)
    180     table++;
    181 
    182   if (table >= kFactorTableNum) {
    183     e = BCExceptionIllegalArgument;
    184     return WideString();
    185   }
    186   uint16_t* ecc = FX_Alloc(uint16_t, numECWords);
    187   memset(ecc, 0, numECWords * sizeof(uint16_t));
    188   for (int32_t l = start; l < start + len; l++) {
    189     uint16_t m = ecc[numECWords - 1] ^ codewords[l];
    190     for (int32_t k = numECWords - 1; k > 0; k--) {
    191       if (m != 0 && FACTORS[table][k] != 0) {
    192         ecc[k] = (uint16_t)(ecc[k - 1] ^
    193                             ALOG[(LOG[m] + LOG[FACTORS[table][k]]) % 255]);
    194       } else {
    195         ecc[k] = ecc[k - 1];
    196       }
    197     }
    198     if (m != 0 && FACTORS[table][0] != 0) {
    199       ecc[0] = (uint16_t)ALOG[(LOG[m] + LOG[FACTORS[table][0]]) % 255];
    200     } else {
    201       ecc[0] = 0;
    202     }
    203   }
    204   WideString strecc;
    205   for (int32_t j = 0; j < numECWords; j++) {
    206     strecc += (wchar_t)ecc[numECWords - j - 1];
    207   }
    208   FX_Free(ecc);
    209   return strecc;
    210 }
    211