/**************************************** B-N(伀) *********************************/
/**************************************** B-N(仾) *********************************/
/*********************************************************************
* Module Header
*
* Identification: ECC - Calculate ECC Polynomial
* Copyright (c) 1993-1997 Symbol Technologies, Inc.
*
* Edit History:
* $Log: ECC.C $
* Revision 1.2 1997/04/18 19:36:26 toml
* Update for yet another rev of MicroPDF. Now we no longer add
* the symbol length descriptor codeword therefore all the alpha
* arrays were increased by one.
* Revision 1.1 1997/01/21 18:22:52 toml
* Initial revision
*
* Description: Contains inline assembly for increased performance.
* This is conditionally compiled in: ASM_I86.
*
* For a portable, fast method of ECC calculation
* refer to module ECCFAST.C
*
* Call Tree:
*
**********************************************************************/
/*********************************************************************
Include Files
*********************************************************************/
#include "pdfapi.h"
#include "ecc.h"
#include "memfunc.h"
/*********************************************************************
Defines
*********************************************************************/
/* // 98.04.14 #ifdef __BORLANDC__ */
/* // 98.04.14 #define ASM asm */
/* // 98.04.14 #define BCC 1 */
/* // 98.04.14 #endif */
/* // 98.04.14 #ifdef _MSC_VER */
#define MSC 1
#define ASM _asm
/* // 98.04.14 #endif */
/*********************************************************************
Local Data
*********************************************************************/
/*
Map ECC level to ECC/EDC length (referenced thru macro in ECC.H)
*/
const uint16 g_anEccToCWs[9] = { 2,4,8,16,32,64,128,256,512 };
/*
* Map the nMicroECC index value into the number of ECC codewords
*/
const uint16 ganMicroEccToCws[ 17 ] = {
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 21, 26, 32, 38, 44, 50
};
/*
The following arrays contain the coefficients from the polynomial
equations used to calculate PDF417 security codewords
*/
static const uint16 alpha0[] =
{
27, 917
};
static const uint16 alpha1[] =
{
522, 568, 723, 809
};
static const uint16 alpha2[] =
{
237, 308, 436, 284, 646, 653, 428, 379
};
static const uint16 alpha3[] =
{
274, 562, 232, 755, 599, 524, 801, 132, 295, 116, 442, 428, 295, 42, 176, 65
};
static const uint16 alpha4[] =
{
361, 575, 922, 525, 176, 586, 640, 321, 536, 742, 677, 742, 687, 284, 193, 517,
273, 494, 263, 147, 593, 800, 571, 320, 803, 133, 231, 390, 685, 330, 63, 410
};
static const uint16 alpha5[] =
{
539, 422, 6, 93, 862, 771, 453, 106, 610, 287, 107, 505, 733, 877, 381, 612,
723, 476, 462, 172, 430, 609, 858, 822, 543, 376, 511, 400, 672, 762, 283, 184,
440, 35, 519, 31, 460, 594, 225, 535, 517, 352, 605, 158, 651, 201, 488, 502,
648, 733, 717, 83, 404, 97, 280, 771, 840, 629, 4, 381, 843, 623, 264, 543
};
static const uint16 alpha6[] =
{
521, 310, 864, 547, 858, 580, 296, 379, 53, 779, 897, 444, 400, 925, 749, 415,
822, 93, 217, 208, 928, 244, 583, 620, 246, 148, 447, 631, 292, 908, 490, 704,
516, 258, 457, 907, 594, 723, 674, 292, 272, 96, 684, 432, 686, 606, 860, 569,
193, 219, 129, 186, 236, 287, 192, 775, 278, 173, 40, 379, 712, 463, 646, 776,
171, 491, 297, 763, 156, 732, 95, 270, 447, 90, 507, 48, 228, 821, 808, 898,
784, 663, 627, 378, 382, 262, 380, 602, 754, 336, 89, 614, 87, 432, 670, 616,
157, 374, 242, 726, 600, 269, 375, 898, 845, 454, 354, 130, 814, 587, 804, 34,
211, 330, 539, 297, 827, 865, 37, 517, 834, 315, 550, 86, 801, 4, 108, 539
};
static const uint16 alpha7[] =
{
524, 894, 75, 766, 882, 857, 74, 204, 82, 586, 708, 250, 905, 786, 138, 720,
858, 194, 311, 913, 275, 190, 375, 850, 438, 733, 194, 280, 201, 280, 828, 757,
710, 814, 919, 89, 68, 569, 11, 204, 796, 605, 540, 913, 801, 700, 799, 137,
439, 418, 592, 668, 353, 859, 370, 694, 325, 240, 216, 257, 284, 549, 209, 884,
315, 70, 329, 793, 490, 274, 877, 162, 749, 812, 684, 461, 334, 376, 849, 521,
307, 291, 803, 712, 19, 358, 399, 908, 103, 511, 51, 8, 517, 225, 289, 470,
637, 731, 66, 255, 917, 269, 463, 830, 730, 433, 848, 585, 136, 538, 906, 90,
2, 290, 743, 199, 655, 903, 329, 49, 802, 580, 355, 588, 188, 462, 10, 134,
628, 320, 479, 130, 739, 71, 263, 318, 374, 601, 192, 605, 142, 673, 687, 234,
722, 384, 177, 752, 607, 640, 455, 193, 689, 707, 805, 641, 48, 60, 732, 621,
895, 544, 261, 852, 655, 309, 697, 755, 756, 60, 231, 773, 434, 421, 726, 528,
503, 118, 49, 795, 32, 144, 500, 238, 836, 394, 280, 566, 319, 9, 647, 550,
73, 914, 342, 126, 32, 681, 331, 792, 620, 60, 609, 441, 180, 791, 893, 754,
605, 383, 228, 749, 760, 213, 54, 297, 134, 54, 834, 299, 922, 191, 910, 532,
609, 829, 189, 20, 167, 29, 872, 449, 83, 402, 41, 656, 505, 579, 481, 173,
404, 251, 688, 95, 497, 555, 642, 543, 307, 159, 924, 558, 648, 55, 497, 10
};
static const uint16 alpha8[] =
{
352, 77, 373, 504, 35, 599, 428, 207, 409, 574, 118, 498, 285, 380, 350, 492,
197, 265, 920, 155, 914, 299, 229, 643, 294, 871, 306, 88, 87, 193, 352, 781,
846, 75, 327, 520, 435, 543, 203, 666, 249, 346, 781, 621, 640, 268, 794, 534,
539, 781, 408, 390, 644, 102, 476, 499, 290, 632, 545, 37, 858, 916, 552, 41,
542, 289, 122, 272, 383, 800, 485, 98, 752, 472, 761, 107, 784, 860, 658, 741,
290, 204, 681, 407, 855, 85, 99, 62, 482, 180, 20, 297, 451, 593, 913, 142,
808, 684, 287, 536, 561, 76, 653, 899, 729, 567, 744, 390, 513, 192, 516, 258,
240, 518, 794, 395, 768, 848, 51, 610, 384, 168, 190, 826, 328, 596, 786, 303,
570, 381, 415, 641, 156, 237, 151, 429, 531, 207, 676, 710, 89, 168, 304, 402,
40, 708, 575, 162, 864, 229, 65, 861, 841, 512, 164, 477, 221, 92, 358, 785,
288, 357, 850, 836, 827, 736, 707, 94, 8, 494, 114, 521, 2, 499, 851, 543,
152, 729, 771, 95, 248, 361, 578, 323, 856, 797, 289, 51, 684, 466, 533, 820,
669, 45, 902, 452, 167, 342, 244, 173, 35, 463, 651, 51, 699, 591, 452, 578,
37, 124, 298, 332, 552, 43, 427, 119, 662, 777, 475, 850, 764, 364, 578, 911,
283, 711, 472, 420, 245, 288, 594, 394, 511, 327, 589, 777, 699, 688, 43, 408,
842, 383, 721, 521, 560, 644, 714, 559, 62, 145, 873, 663, 713, 159, 672, 729,
624, 59, 193, 417, 158, 209, 563, 564, 343, 693, 109, 608, 563, 365, 181, 772,
677, 310, 248, 353, 708, 410, 579, 870, 617, 841, 632, 860, 289, 536, 35, 777,
618, 586, 424, 833, 77, 597, 346, 269, 757, 632, 695, 751, 331, 247, 184, 45,
787, 680, 18, 66, 407, 369, 54, 492, 228, 613, 830, 922, 437, 519, 644, 905,
789, 420, 305, 441, 207, 300, 892, 827, 141, 537, 381, 662, 513, 56, 252, 341,
242, 797, 838, 837, 720, 224, 307, 631, 61, 87, 560, 310, 756, 665, 397, 808,
851, 309, 473, 795, 378, 31, 647, 915, 459, 806, 590, 731, 425, 216, 548, 249,
321, 881, 699, 535, 673, 782, 210, 815, 905, 303, 843, 922, 281, 73, 469, 791,
660, 162, 498, 308, 155, 422, 907, 817, 187, 62, 16, 425, 535, 336, 286, 437,
375, 273, 610, 296, 183, 923, 116, 667, 751, 353, 62, 366, 691, 379, 687, 842,
37, 357, 720, 742, 330, 5, 39, 923, 311, 424, 242, 749, 321, 54, 669, 316,
342, 299, 534, 105, 667, 488, 640, 672, 576, 540, 316, 486, 721, 610, 46, 656,
447, 171, 616, 464, 190, 531, 297, 321, 762, 752, 533, 175, 134, 14, 381, 433,
717, 45, 111, 20, 596, 284, 736, 138, 646, 411, 877, 669, 141, 919, 45, 780,
407, 164, 332, 899, 165, 726, 600, 325, 498, 655, 357, 752, 768, 223, 849, 647,
63, 310, 863, 251, 366, 304, 282, 738, 675, 410, 389, 244, 31, 121, 303, 263
};
static const uint16 *const alpha_arrays[] =
{
alpha0,
alpha1,
alpha2,
alpha3,
alpha4,
alpha5,
alpha6,
alpha7,
alpha8
};
static const uint16 alphaMicro7[7] = { 76, 925, 537, 597, 784, 691,
437 };
static const uint16 alphaMicro8[8] = { 237, 308, 436, 284,
646, 653, 428, 379 };
static const uint16 alphaMicro9[9] = { 567, 527, 622, 257, 289, 362,
501, 441, 205 };
static const uint16 alphaMicro10[10] = { 377, 457, 64, 244, 826, 841,
818, 691, 266, 612 };
static const uint16 alphaMicro11[11] = { 462, 45, 565, 708, 825, 213,
15, 68, 327, 602, 904 };
static const uint16 alphaMicro12[12] = { 597, 864, 757, 201, 646, 684,
347, 127, 388, 7, 69,
851 };
static const uint16 alphaMicro13[ 13 ] = {
764, 713, 342, 384, 606, 583, 322, 592, 678, 204,
184, 394, 692
};
static const uint16 alphaMicro14[14] = { 669, 677, 154, 187, 241, 286,
274, 354, 478, 915, 691,
833, 105, 215 };
static const uint16 alphaMicro15[ 15 ] = {
460, 829, 476, 109, 904, 664, 230, 5, 80, 74,
550, 575, 147, 868, 642
};
static const uint16 alphaMicro16[ 16 ] = {
274, 562, 232, 755, 599, 524, 801, 132, 295, 116,
442, 428, 295, 42, 176, 65
};
static const uint16 alphaMicro18[ 18 ] = {
279, 577, 315, 624, 37, 855, 275, 739, 120, 297,
312, 202, 560, 321, 233, 756, 760, 573
};
static const uint16 alphaMicro21[ 21 ] = {
108, 519, 781, 534, 129, 425, 681, 553, 422, 716,
763, 693, 624, 610, 310, 691, 347, 165, 193, 259,
568
};
static const uint16 alphaMicro26[ 26 ] = {
443, 284, 887, 544, 788, 93, 477, 760, 331, 608,
269, 121, 159, 830, 446, 893, 699, 245, 441, 454,
325, 858, 131, 847, 764, 169
};
static const uint16 alphaMicro32[ 32 ] = {
361, 575, 922, 525, 176, 586, 640, 321, 536, 742,
677, 742, 687, 284, 193, 517, 273, 494, 263, 147,
593, 800, 571, 320, 803, 133, 231, 390, 685, 330,
63, 410
};
static const uint16 alphaMicro38[ 38 ] = {
234, 228, 438, 848, 133, 703, 529, 721, 788, 322,
280, 159, 738, 586, 388, 684, 445, 680, 245, 595,
614, 233, 812, 32, 284, 658, 745, 229, 95, 689,
920, 771, 554, 289, 231, 125, 117, 518
};
static const uint16 alphaMicro44[ 44 ] = {
476, 36, 659, 848, 678, 64, 764, 840, 157, 915,
470, 876, 109, 25, 632, 405, 417, 436, 714, 60,
376, 97, 413, 706, 446, 21, 3, 773, 569, 267,
272, 213, 31, 560, 231, 758, 103, 271, 572, 436,
339, 730, 82, 285
};
static const uint16 alphaMicro50[ 50 ] = {
923, 797, 576, 875, 156, 706, 63, 81, 257, 874,
411, 416, 778, 50, 205, 303, 188, 535, 909, 155,
637, 230, 534, 96, 575, 102, 264, 233, 919, 593,
865, 26, 579, 623, 766, 146, 10, 739, 246, 127,
71, 244, 211, 477, 920, 876, 427, 820, 718, 435
};
static const uint16 *const aAlphaMicro[] =
{
alphaMicro7 ,
alphaMicro8 ,
alphaMicro9 ,
alphaMicro10,
alphaMicro11,
alphaMicro12,
alphaMicro13,
alphaMicro14,
alphaMicro15,
alphaMicro16,
alphaMicro18,
alphaMicro21,
alphaMicro26,
alphaMicro32,
alphaMicro38,
alphaMicro44,
alphaMicro50
};
static void GenPoly( LPCCW src, uint16 n, const uint16 *alpha, uint16 k, LPCW dst )
{
/**************************************** B-N(伀) *********************************/
uint16 j;
LPCW lpEcc;
/*
uint32 wk;
uint16 wk1;
*/
/* // 98.04.14 #ifndef ASM_I86 */
/* // 98.04.14 uint32 t1; */
/* // 98.04.14 uint16 j; */
/* // 98.04.14 LPCW lpEcc; */
/* // 98.04.14 #else */
/**************************************** B-N(仾) *********************************/
uint32 t1;
/* uint16 t1; */
/* // 98.04.14 #endif */
/* Reset error correction/detection codeword array */
MEMSET(dst, 0, k * sizeof(CW) );
k--;
/*
For each data codeword d[i]
*/
while( n-- ) {
t1 = (uint16)(*src++ + dst[0]); /* both in range 0 thru 928 */
if ( t1 >= 929 )
t1 -= 929;
/*
iterate over all ECC/EDC
*/
/**************************************** B-N(伀) *********************************/
for( lpEcc=dst, j = k; j >= 1; j--, lpEcc++ )
*lpEcc = (uint16)(929 - ( ((929 - lpEcc[1]) + (t1 * alpha[j]) ) % 929));
*lpEcc = (uint16)(929 - ((t1 * alpha[0]) % 929));
/*
for( lpEcc=dst, j = k; j >= 1; j--, lpEcc++ ){
wk = (uint32)((uint32)t1 * (uint32)alpha[j]);
wk -= (uint32)lpEcc[1];
wk += 929;
wk = wk % 929;
wk1 = 929 - (uint16)wk;
*lpEcc = wk1;
}
*lpEcc = (uint16)(929 - ((t1 * alpha[0]) % 929));
*/
/* // 98.04.14 #ifndef ASM_I86 */
/* // 98.04.14 for( lpEcc=dst, j = k; j >= 1; j--, lpEcc++ ) */
/* // 98.04.14 *lpEcc = (uint16)(929 - ( ((929 - lpEcc[1]) + (t1 * alpha[j]) ) % 929)); */
/* // 98.04.14 */
/* // 98.04.14 *lpEcc = (uint16)(929 - ((t1 * alpha[0]) % 929)); */
/* // 98.04.14 #else */
/**************************************** B-N(仾) *********************************/
/**************************************** B-N(伀) *********************************/
/* ASM { */
/*
* Get number of ECC/EDC codewords (k) and convert to word array
* offset value. Setup loop variable for alpha[]
* array access:
* si - alpha[j] (local therefore same DGROUP)
* es:di - lpEcc
* cx - countdown on k-1
* bx - constant 929 used in calculations
*/
/* mov cx,k */
/* mov si,cx */
/* shl si,1 *//* cx <-- (k-1) * 2 */
/* add si,word ptr alpha *//* get alpha's contents */
/* mov bx,929 */
/* les di,dword ptr dst *//* ES:DI (LPCW lpEcc) */
/**************************************** B-N(仾) *********************************/
/* // 98.04.14 #ifdef BCC */
/* // 98.04.14 */
/* // 98.04.14 } */
/* // 98.04.14 loop1: */
/* // 98.04.14 ASM { */
/* // 98.04.14 */
/* // 98.04.14 #else */
/**************************************** B-N(伀) *********************************/
/* loop1: */
/* // 98.04.14 #endif */
/* mov ax,t1 */
/* mul word ptr [si] *//* dx:ax <-- t1 * alpha[ j ] */
/* sub si,2 *//* alpha-- */
/* sub ax,es:[di+2] *//* dx:ax -= c[1] */
/* sbb dx,0 */
/* add ax,bx *//* dx:ax += 929 */
/* adc dx,0 */
/* div bx *//* dx:ax / 929 --> ax:quot, dx:rem */
/* neg dx */
/* add dx,bx *//* remainder = -remainder + 929 */
/* mov es:[di],dx */
/* add di,2 *//* advance to next c[] */
/* loop loop1 */
/*
* c[0] = 929 - ((t1 * alpha[0]) % 929);
*/
/* mov ax,t1 */
/* mul word ptr [si] *//* dx:ax <-- t1 * alpha[0] */
/* div bx *//* dx:ax / 929 --> dx:remainder */
/* sub bx,dx *//* 929 - remainder */
/* mov es:[di],bx *//* c[0] <-- result */
/* } */
/**************************************** B-N(仾) *********************************/
/* // 98.04.14 #endif */
}
/*
calculate the complement for all ECC/EDC
*/
/**************************************** B-N(伀) *********************************/
for( lpEcc=dst, j = k+1; j; j--, lpEcc++ ) {
if ( *lpEcc )
*lpEcc = 929 - *lpEcc;
}
/* // 98.04.14 #ifndef ASM_I86 */
/* // 98.04.14 for( lpEcc=dst, j = k+1; j; j--, lpEcc++ ) { */
/* // 98.04.14 if ( *lpEcc ) */
/* // 98.04.14 *lpEcc = 929 - *lpEcc; */
/* // 98.04.14 } */
/* // 98.04.14 #else */
/**************************************** B-N(仾) *********************************/
/* // 98.04.14 */
/* // 98.04.14 #ifdef BCC */
/* // 98.04.14 */
/* // 98.04.14 ASM { */
/* // 98.04.14 mov cx,k */
/* // 98.04.14 inc cx */
/* // 98.04.14 mov bx,929 */
/* // 98.04.14 les di,dword ptr dst */
/* // 98.04.14 } */
/* // 98.04.14 loop2: */
/* // 98.04.14 ASM { */
/* // 98.04.14 mov ax,bx *//* ax <-- 929 */
/* // 98.04.14 sub ax,es:[di] *//* ax <-- 929 - *dst */
/* // 98.04.14 cmp ax,bx *//* if ax == 929 then don't store */
/* // 98.04.14 je skip */
/* // 98.04.14 mov es:[di],ax */
/* // 98.04.14 } */
/* // 98.04.14 skip: */
/* // 98.04.14 ASM { */
/* // 98.04.14 add di,2 */
/* // 98.04.14 loop loop2 */
/* // 98.04.14 } */
/* // 98.04.14 */
/* // 98.04.14 #else */
/**************************************** B-N(伀) *********************************/
/* ASM { */
/* mov cx,k */
/* inc cx */
/* mov bx,929 */
/* les di,dword ptr dst */
/* loop2: */
/* mov ax,bx *//* ax <-- 929 */
/* sub ax,es:[di] *//* ax <-- 929 - *dst */
/* cmp ax,bx *//* if ax == 929 then don't store */
/* je skip */
/* mov es:[di],ax */
/* skip: */
/* add di,2 */
/* loop loop2 */
/* } */
/**************************************** B-N(仾) *********************************/
/* // 98.04.14 #endif */
/* // 98.04.14 #endif */
}
/*********************************************************************
* Routine Header *****************************************************
*
* Routine Name: add_ecc_poly
*
* Description: Calculates the ECC polynomial from the source
* codeword polynomial, src, and places the result
* into, dst.
*
* Synopsis: void add_ecc_poly( LPCCW src, int n, int s, LPCW dst )
*
* Parameters: src - data codeword array
* n - number of codewords in d[]
* s - ECC level
* dst - destination for ECC poly when done
*
* Local Data: alpha_array[]
*
**********************************************************************/
/*********************************************************************
* Pseudocode:
* Calculate ECC polynomial
* End Pseudocode *****************************************************
**********************************************************************/
void add_ecc_poly( LPCCW src, uint16 n, uint16 s, LPCW dst )
{
/* Select the proper coefficient table based on ECC level 's' */
GenPoly( src, n, alpha_arrays[s], ECCLEN( s ), dst );
}
void AddMicroECCPoly( LPCCW src, uint16 n, uint16 nECCIndex, LPCW dst )
{
GenPoly( src, n, aAlphaMicro[nECCIndex], MICROECCLEN(nECCIndex), dst );
}
以下为.h文件:ECC.H
/*********************************************************************
* Module Header
*
* Identification: ECC.H - Error Correction Codewords Definitions
* Copyright (c) 1993-1997 Symbol Technologies, Inc.
*
* Edit History:
* $Log: ECC.H $
* Revision 1.1 1997/01/20 16:17:10 toml
* Initial revision
*
* Description: Definitions and prototypes for ECC operations.
*
* NOTE: This header requires PDFAPI.H.
*
**********************************************************************/
#ifndef __ECC_H__
#define __ECC_H__
extern const uint16 g_anEccToCWs[9]; /* ECC level to codewords map */
extern const uint16 ganMicroEccToCws[ 17 ];
#define ECCLEN( l ) ( g_anEccToCWs[ l ] )
#define MICROECCLEN( i ) ( ganMicroEccToCws[ i ] )
void add_ecc_poly( LPCCW src, uint16 n, uint16 s, LPCW dst );
void AddMicroECCPoly( LPCCW lpcwSrc, uint16 n, uint16 nECCIndex, LPCW lpcwDest );
#endif /* __ECC_H__ */