3232% IN THE SOFTWARE.
3333
3434% --BEGIN ENCODER hanxin--
35- % --REQUIRES preamble raiseerror processoptions parseinput setanycolor rendertext renmatrix--
35+ % --REQUIRES preamble raiseerror processoptions parseinput setanycolor fifocache rendertext renmatrix--
3636% --DESC: Han Xin Code
3737% --EXAM: This is Han Xin
3838% --EXOP:
@@ -44,6 +44,7 @@ true setglobal
4444dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
4545dup /processoptions dup /uk.co.terryburton.bwipp findresource put
4646dup /parseinput dup /uk.co.terryburton.bwipp findresource put
47+ dup /fifocache dup /uk.co.terryburton.bwipp findresource put
4748dup /renmatrix dup /uk.co.terryburton.bwipp findresource put
4849begin
4950
@@ -494,6 +495,64 @@ begin
494495 exch get /preload known {//hanxin.latevars /init get exec} if
495496} {pop} ifelse
496497
498+ %
499+ % Function to calculate the product in the field
500+ %
501+ /hanxin.rsprod {
502+ dup 0 ne { 1 index 0 ne {
503+ rslog exch get exch rslog exch get add rsgf-1 mod rsalog exch get
504+ } { pop pop 0 } ifelse } { pop pop 0 } ifelse
505+ } bind def
506+
507+ %
508+ % Generate Reed-Solomon coefficients for given rsnc
509+ %
510+ /hanxin.gencoeffs {
511+ /coeffs [ 1 rsnc {0} repeat ] def
512+ 1 1 rsnc { % i
513+ coeffs 1 index 2 copy 1 sub get put % coeffs[i] = coeffs[i-1]
514+ rsalog 1 index get % ai = rsalog[i]
515+ exch 1 sub -1 1 { % j
516+ coeffs exch 2 copy get % ... coeffs j coeffs[j]
517+ 3 index % ... ai
518+ //hanxin.rsprod exec % p = coeffs[j] * ai
519+ coeffs 2 index 1 sub get % ... coeffs[j-1]
520+ xor put % coeffs[j] = p ^ coeffs[j-1]
521+ } for
522+ coeffs 0 2 copy get % ... coeffs 0 coeffs[0]
523+ 3 index % ... ai
524+ //hanxin.rsprod exec put % coeffs[0] = coeffs[0] * ai
525+ pop % ai
526+ } for
527+ coeffs 0 coeffs length 1 sub getinterval
528+ } bind def
529+
530+ %
531+ % FIFO cache for ECC polynomials
532+ %
533+ % 15 unique keys: 1 format (GF16) + 14 data (GF256) with per-block 4-32
534+ %
535+ /hanxin.coeffscachemax 15 def % Override with global_ctx.hanxin.coeffscachemax
536+ /hanxin.coeffscachelimit 264 def % Override with global_ctx.hanxin.coeffscachelimit
537+ /uk.co.terryburton.bwipp.global_ctx dup where {
538+ exch get
539+ dup /hanxin.coeffscachemax 2 copy known {get /hanxin.coeffscachemax exch def} {pop pop} ifelse
540+ /hanxin.coeffscachelimit 2 copy known {get /hanxin.coeffscachelimit exch def} {pop pop} ifelse
541+ } {pop} ifelse
542+
543+ /hanxin.coeffscache hanxin.coeffscachemax hanxin.coeffscachelimit //fifocache exec def
544+
545+ /hanxin.coeffscachefetch {
546+ /rsbt exch def
547+ /rsnc exch def
548+ rstables rsbt get aload pop /rsalog exch def /rslog exch def
549+ /rsgf-1 rsalog length 1 sub def
550+ rsbt 10000 mul rsnc add % key = rsbt * 10000 + rsnc
551+ { //hanxin.gencoeffs exec }
552+ { length }
553+ //hanxin.coeffscache /fetch get exec
554+ } bind def
555+
497556/hanxin {
498557
499558 128 dict begin
@@ -636,35 +695,10 @@ begin
636695 /rsnc exch def
637696 /rscws exch def
638697
639- rstables rsbt get aload pop /rsalog exch def /rslog exch def
640- /rsgf-1 rsalog length 1 sub def
641-
642698 %
643- % Function to calculate the product in the field
699+ % Fetch the coefficients
644700 %
645- /rsprod {
646- dup 0 ne { 1 index 0 ne {
647- rslog exch get exch rslog exch get add rsgf-1 mod rsalog exch get
648- } { pop pop 0 } ifelse } { pop pop 0 } ifelse
649- } def
650-
651- %
652- % Generate the coefficients
653- %
654- /coeffs [ 1 rsnc {0} repeat ] def
655- 1 1 rsnc { % i
656- coeffs 1 index 2 copy 1 sub get put % coeffs[i] = coeffs[i-1]
657- rsalog 1 index get % ai = rsalog[i]
658- exch 1 sub -1 1 { % j
659- coeffs exch 2 copy get % ... coeffs j coeffs[j]
660- 3 index % ... ai
661- rsprod % p = coeffs[j] * ai
662- coeffs 2 index 1 sub get % ... coeffs[j-1]
663- xor put % coeffs[j] = p ^ coeffs[j-1]
664- } for
665- coeffs 0 2 copy get 3 index rsprod put % coeffs[0] = coeffs[0] * ai
666- pop % ai
667- } for
701+ /coeffs rsnc rsbt //hanxin.coeffscachefetch exec def
668702
669703 %
670704 % Extend the input with the error correction values
@@ -678,7 +712,8 @@ begin
678712 rscws exch nd add 2 copy 1 add get % ... rscws nd+j rscws[nd+j+1]
679713 coeffs rsnc 6 -1 roll sub 1 sub get % ... coeffs[rsnc-1-j]
680714 4 index % ... k
681- rsprod xor put % rscws[nd+j] = rscws[nd+j+1] ^ (coeffs[rsnc-1-j] * k)
715+ //hanxin.rsprod exec % p = coeffs[rsnc-1-j] * k
716+ xor put % rscws[nd+j] = rscws[nd+j+1] ^ p
682717 } for
683718 pop % k
684719 } for
0 commit comments