Fixed bugs: C128 arbitrary data, C128 wrong checksum

This commit is contained in:
Harald Oehlmann 2016-09-12 15:49:17 +02:00
parent 2cf489eac2
commit 7b4009d19f

View File

@ -42,7 +42,7 @@
#define uchar unsigned char #define uchar unsigned char
/* FTab Flagzeichen - Addierbar */ /* FTab C128 flags - may be added */
#define CodeA 1 #define CodeA 1
#define CodeB 2 #define CodeB 2
#define CodeC 4 #define CodeC 4
@ -168,7 +168,7 @@ void CreateCharacterSetTable(CharacterSetTable T[], unsigned char *data, int dat
} }
} }
/* Calculate the amount of numerical characters in pairs which will fit in /* Find the amount of numerical characters in pairs which will fit in
* one bundle into the line (up to here). This is calculated online because * one bundle into the line (up to here). This is calculated online because
* it depends on the space in the line. * it depends on the space in the line.
*/ */
@ -192,17 +192,17 @@ int RemainingDigits(CharacterSetTable *T, int charCur,int emptyColumns)
return digitCount; return digitCount;
} }
/* Calculates the used Characterset (Set) from columns. /* Find the Character distribution at a given column count.
* If to many rows (>44) are requested the columns is extended. * If to many rows (>44) are requested the columns is extended.
* A oneLigner may be shorter. Error is set to eCorrectWidthInOneLigner. * A oneLigner may be choosen if shorter.
* Parameters : * Parameters :
* Pcolumns Pointer on the Characters who fits in the Line * T Pointer on the Characters which fit in the row
* If a different count is calculated it is corrected * If a different count is calculated it is corrected
* in the callers workspace. * in the callers workspace.
* pFillings Output of filling characters * pFillings Output of filling characters
* pSet Output of the character sets used, allocated by me. * pSet Output of the character sets used, allocated by me.
* Data The Data string to encode, exceptionnally not an out * Data The Data string to encode, exceptionnally not an out
* Return value The lines requested. * Return value Resulting row count
*/ */
int Columns2Rows(CharacterSetTable *T, unsigned char *data, int dataLength, int Columns2Rows(CharacterSetTable *T, unsigned char *data, int dataLength,
@ -416,26 +416,19 @@ int Columns2Rows(CharacterSetTable *T, unsigned char *data, int dataLength,
*pFillings=fillings; *pFillings=fillings;
return 0; return 0;
} }
/* Find columns if rows are given. /* Find columns if row count is given.
* Error may be :
* eLessLines : The exakt number of lines may only be constructed
* with a filing line.
* eMoreLines : Only Maddin knows why...
* eCorrectWidthInOneLigner : A one-Ligner is shorter than demanded.
* eExtendWidth : The Code is wider because otherwise the data amount.
* would not fit in 50 lines.
*/ */
int Rows2Columns(CharacterSetTable *T, unsigned char *data, int dataLength, int Rows2Columns(CharacterSetTable *T, unsigned char *data, int dataLength,
int * pRows, int * pUseColumns, int * pSet, int * pFillings) int * pRows, int * pUseColumns, int * pSet, int * pFillings)
{ {
int errorCur; int errorCur;
int rowsCur; int rowsCur;
int rowsRequested; /* Number of Lines Requested */ int rowsRequested; /* Number of requested rows */
int backupRows; int backupRows;
int fillings; int fillings;
int backupFillings; int backupFillings;
int useColumns; int useColumns;
int testColumns; /* To enter into Width2Lines */ int testColumns; /* To enter into Width2Rows */
int backupColumns; int backupColumns;
int fBackupOk; /* The memorysed set is o.k. */ int fBackupOk; /* The memorysed set is o.k. */
int testListSize = 0; int testListSize = 0;
@ -449,7 +442,7 @@ int Rows2Columns(CharacterSetTable *T, unsigned char *data, int dataLength,
rowsRequested=*pRows; rowsRequested=*pRows;
#ifdef _DEBUG #ifdef _DEBUG
fprintf(stderr,"Optimizer : Searching Lines <%i>\n",rowsRequested); fprintf(stderr,"Optimizer : Searching <%i> rows\n",rowsRequested);
#endif #endif
fBackupOk=0; fBackupOk=0;
@ -499,7 +492,7 @@ int Rows2Columns(CharacterSetTable *T, unsigned char *data, int dataLength,
pTestList[0] = testColumns; pTestList[0] = testColumns;
testListSize = 1; testListSize = 1;
} }
/* > Test more Lines (shorter CDB) */ /* > Test more rows (shorter CDB) */
fBackupOk=(rowsCur==rowsRequested); fBackupOk=(rowsCur==rowsRequested);
memcpy(pBackupSet,pSet,dataLength*sizeof(int)); memcpy(pBackupSet,pSet,dataLength*sizeof(int));
backupFillings=fillings; backupFillings=fillings;
@ -507,7 +500,7 @@ int Rows2Columns(CharacterSetTable *T, unsigned char *data, int dataLength,
backupRows=rowsCur; backupRows=rowsCur;
--testColumns; --testColumns;
} else { } else {
/* > To many lines */ /* > To many rows */
int fInTestList = fBackupOk; int fInTestList = fBackupOk;
int posCur; int posCur;
for (posCur = 0; posCur < testListSize && ! fInTestList; posCur++) { for (posCur = 0; posCur < testListSize && ! fInTestList; posCur++) {
@ -515,16 +508,17 @@ int Rows2Columns(CharacterSetTable *T, unsigned char *data, int dataLength,
fInTestList = 1; fInTestList = 1;
} }
if (fInTestList) { if (fInTestList) {
/* The next less-lines (larger) code was /* The next less-rows (larger) code was
* already tested. So give the larger * already tested. So give the larger
* back. * back.
*/ */
memcpy(pSet,pBackupSet,dataLength*sizeof(int)); memcpy(pSet,pBackupSet,dataLength*sizeof(int));
*pFillings=backupFillings; *pFillings=backupFillings;
*pRows=backupRows; *pRows=backupRows;
*pUseColumns=backupColumns;
return 0; return 0;
} }
/* > Test less lines (longer code) */ /* > Test less rows (longer code) */
backupRows=rowsCur; backupRows=rowsCur;
memcpy(pBackupSet,pSet,dataLength*sizeof(int)); memcpy(pBackupSet,pSet,dataLength*sizeof(int));
backupFillings=fillings; backupFillings=fillings;
@ -601,7 +595,7 @@ void ASCIIZ128(uchar **ppOutPos, int CharacterSet,uchar c1, uchar c2)
} }
/* XLate Table A of Codablock-F Specification and call output /* XLate Table A of Codablock-F Specification and call output
*/ */
void SummeASCII(uchar **ppOutPos, int Sum, int CharacterSet) void SumASCII(uchar **ppOutPos, int Sum, int CharacterSet)
{ {
switch (CharacterSet){ switch (CharacterSet){
case CodeA: case CodeA:
@ -622,6 +616,8 @@ void SummeASCII(uchar **ppOutPos, int Sum, int CharacterSet)
} }
} }
/* Main function called by zint framework
*/
int codablock(struct zint_symbol *symbol, unsigned char source[], int length) { int codablock(struct zint_symbol *symbol, unsigned char source[], int length) {
int charCur; int charCur;
int dataLength; int dataLength;
@ -775,15 +771,18 @@ int codablock(struct zint_symbol *symbol, unsigned char source[], int length) {
#endif #endif
pOutPos = pOutput; pOutPos = pOutput;
charCur=0; charCur=0;
/* >> Loop over Lines */ /* >> Loop over rows */
for (rowCur=0 ; rowCur<rows ; rowCur++) { for (rowCur=0 ; rowCur<rows ; rowCur++) {
if (charCur>=dataLength) if (charCur>=dataLength)
{ {
/* >> Empty line with StartCCodeBCodeC */ /* >> Empty line with StartCCodeBCodeC */
characterSetCur=CodeC; characterSetCur=CodeC;
/* CDB Start C*/ /* CDB Start C*/
pOutPos+=sprintf((char*)pOutPos,"\x67\x63"); *pOutPos='\x67';
SummeASCII(&pOutPos,rowCur+42,CodeC); pOutPos++;
*pOutPos='\x63';
pOutPos++;
SumASCII(&pOutPos,rowCur+42,CodeC);
emptyColumns=useColumns-2; emptyColumns=useColumns-2;
while (emptyColumns>0) while (emptyColumns>0)
{ {
@ -802,33 +801,45 @@ int codablock(struct zint_symbol *symbol, unsigned char source[], int length) {
/* > Startcode */ /* > Startcode */
switch (pSet[charCur] & (CodeA+CodeB+CodeC)){ switch (pSet[charCur] & (CodeA+CodeB+CodeC)){
case CodeA: case CodeA:
if (rows==1) *pOutPos = '\x67';
pOutPos+=sprintf((char *)pOutPos,"\x67"); pOutPos++;
else if (rows>1) {
pOutPos+=sprintf((char *)pOutPos,"\x67\x62"); *pOutPos = '\x62';
pOutPos++;
}
characterSetCur=CodeA; characterSetCur=CodeA;
break; break;
case CodeB: case CodeB:
if (rows==1) if (rows==1) {
pOutPos+=sprintf((char *)pOutPos,"\x68"); *pOutPos = '\x68';
else pOutPos++;
pOutPos+=sprintf((char *)pOutPos,"\x67\x64"); } else {
*pOutPos = '\x67';
pOutPos++;
*pOutPos = '\x64';
pOutPos++;
}
characterSetCur=CodeB; characterSetCur=CodeB;
break; break;
case CodeC: case CodeC:
if (rows==1) if (rows==1) {
pOutPos+=sprintf((char *)pOutPos,"\x69"); *pOutPos = '\x69';
else pOutPos++;
pOutPos+=sprintf((char *)pOutPos,"\x67\x63"); } else {
*pOutPos = '\x67';
pOutPos++;
*pOutPos = '\x63';
pOutPos++;
}
characterSetCur=CodeC; characterSetCur=CodeC;
break; break;
} }
if (rows>1) if (rows>1)
{ {
/* > Set F1 */ /* > Set F1 */
/* In first line : # of Lines */ /* In first line : # of rows */
/* In Case of CodeA we shifted to CodeB */ /* In Case of CodeA we shifted to CodeB */
SummeASCII(&pOutPos SumASCII(&pOutPos
,(rowCur==0)?rows-2:rowCur+42 ,(rowCur==0)?rows-2:rowCur+42
,(characterSetCur==CodeA)?CodeB:characterSetCur ,(characterSetCur==CodeA)?CodeB:characterSetCur
); );
@ -844,7 +855,7 @@ int codablock(struct zint_symbol *symbol, unsigned char source[], int length) {
/* ? Change character set */ /* ? Change character set */
/* not at first possition (It was then the start set) */ /* not at first possition (It was then the start set) */
/* +++ special case for one-ligner */ /* +++ special case for one-ligner */
if (emptyColumns<useColumns && !(rows == 1 && charCur==0) ) if (emptyColumns<useColumns || (rows == 1 && charCur!=0) )
{ {
if ((pSet[charCur]&CodeA)!=0) if ((pSet[charCur]&CodeA)!=0)
{ {
@ -925,14 +936,14 @@ int codablock(struct zint_symbol *symbol, unsigned char source[], int length) {
/* Add checksum in last line */ /* Add checksum in last line */
if(rows>1 && rowCur==rows-1) if(rows>1 && rowCur==rows-1)
{ {
SummeASCII(&pOutPos,Sum1,characterSetCur); SumASCII(&pOutPos,Sum1,characterSetCur);
SummeASCII(&pOutPos,Sum2,characterSetCur); SumASCII(&pOutPos,Sum2,characterSetCur);
} }
/* Add Code 128 checksum */ /* Add Code 128 checksum */
{ {
int Sum=0; int Sum=0;
int Pos=0; int Pos=0;
for ( ; Pos < useColumns+2 ; Pos++) for ( ; Pos < useColumns+3 ; Pos++)
{ {
Sum = (Sum + Sum = (Sum +
((Pos==0?1:Pos) * pOutput[columns*rowCur+Pos]) % 103 ((Pos==0?1:Pos) * pOutput[columns*rowCur+Pos]) % 103
@ -964,7 +975,7 @@ int codablock(struct zint_symbol *symbol, unsigned char source[], int length) {
printf("rows=%i columns=%i fillings=%i\n", rows, columns, fillings); printf("rows=%i columns=%i fillings=%i\n", rows, columns, fillings);
#endif #endif
/* Paint the C128 patterns */
for (r = 0; r < rows; r++) { for (r = 0; r < rows; r++) {
strcpy(dest, ""); strcpy(dest, "");
for(c = 0; c < columns; c++) { for(c = 0; c < columns; c++) {