unsigned long GetCodeDWord (unsigned Addr) /* Get a dword from the given address */ { unsigned long Lo = GetCodeWord (Addr); unsigned long Hi = GetCodeWord (Addr+2); return Lo | (Hi << 16); }
void DataWordLine (unsigned ByteCount) /* Output a line with words */ { unsigned I; Indent (MCol); Output (".word"); Indent (ACol); for (I = 0; I < ByteCount; I += 2) { if (I > 0) { Output (",$%04X", GetCodeWord (PC+I)); } else { Output ("$%04X", GetCodeWord (PC+I)); } } LineComment (PC, ByteCount); LineFeed (); }
unsigned RtsTable (void) /* Output a table of RTS addresses (address - 1) */ { unsigned long BytesLeft = GetRemainingBytes (); unsigned long Start = PC; /* Loop while table bytes left and we don't need to create a label at the ** current position. */ while (BytesLeft && GetStyleAttr (PC) == atRtsTab) { unsigned Addr; /* If just one byte is left, define it and bail out */ if (BytesLeft == 1 || GetStyleAttr (PC+1) != atRtsTab) { DataByteLine (1); ++PC; break; } /* More than one byte left. Define a forward label if necessary */ ForwardLabel (1); /* Now get the address from the PC */ Addr = (GetCodeWord (PC) + 1) & 0xFFFF; /* In pass 1, define a label, in pass 2 output the line */ if (Pass == 1) { if (!HaveLabel (Addr)) { AddIntLabel (Addr); } } else { const char* Label = GetLabel (Addr, PC); if (Label == 0) { /* OOPS! Should not happen */ Internal ("OOPS - Label for address 0x%06X disappeard!", Addr); } Indent (MCol); Output (".word"); Indent (ACol); Output ("%s-1", Label); LineComment (PC, 2); LineFeed (); } /* Next table entry */ PC += 2; BytesLeft -= 2; /* If we must define a label here, bail out */ if (BytesLeft && MustDefLabel (PC)) { break; } } /* If the next line is not a return address table line, add a separator */ if (CodeLeft() && GetStyleAttr (PC) != atRtsTab) { SeparatorLine (); } /* Return the number of bytes output */ return PC - Start; }
bool ContentDecoder::DecodeData(int nCodeSize,int nVersion,unsigned char CodeData[MAX_MODULESIZE][MAX_MODULESIZE]) { int i,j; m_nCodeSize=nCodeSize; m_nVersion=nVersion; for(i=0;i<nCodeSize;i++) for(j=0;j<nCodeSize;j++) m_CodeData[i][j]=CodeData[i][j]; //获取版本信息(7~40有版本信息) //if(m_nVersion>=7) //{ // m_nVersion=GetVersionInfo(); //} //获取格式信息 int nFormatData=GetFormatInfo(); m_nLevel=((nFormatData & 0x18) >> 3);//纠错级别,前两位 switch(m_nLevel) { case 1: m_nLevel=QR_LEVEL_L;break; case 0: m_nLevel=QR_LEVEL_M;break; case 3: m_nLevel=QR_LEVEL_Q;break; case 2: m_nLevel=QR_LEVEL_H;break; } m_nMaskingNo=(nFormatData & 0x07);//掩膜图形参考,后三位 //去除掩膜图形 for(i=0;i<m_nCodeSize;++i) { for(j=0;j<m_nCodeSize;++j) { bool bMask; switch (m_nMaskingNo) { case 0: bMask=((i+j)%2==0);break; case 1: bMask=(i%2==0);break; case 2: bMask=(j%3==0);break; case 3: bMask=((i+j) % 3 == 0);break; case 4: bMask=(((i/2)+(j/3))%2==0);break; case 5: bMask=(((i*j)%2)+((i*j)%3)==0);break; case 6: bMask=((((i*j)%2)+((i* j)%3))%2==0);break; default: // case 7: bMask=((((i*j)%3)+((i+j)%2))%2==0);break; } m_CodeData[j][i] = (unsigned char)(m_CodeData[j][i] ^ bMask); } } //标记非数据位置 SetFinderPattern(); if(m_nVersion>1) SetAlignmentPattern(); SetFormatPattern(); SetVersionPattern(); //读取数据位置,得到数据位流 GetCodeWord(); //纠错 CorrectDataBlocks(); //解析数据码字,得到字符串 ParseDataCodeWord(); return true; }
/*************************************************************************** * Function : LZWDecodeFile * Description: This routine reads an input file 1 encoded string at a * time and decodes it using the LZW algorithm. * Parameters : inFile - Name of file to decode * outFile - Name of file to write decoded output to * Effects : File is decoded using the LZW algorithm with CODE_LEN * codes. * Returned : TRUE for success, otherwise FALSE. ***************************************************************************/ int LZWDecodeFile(char *inFile, char *outFile) { bit_file_t *bfpIn; /* encoded input */ FILE *fpOut; /* decoded output */ unsigned int nextCode; /* value of next code */ unsigned int lastCode; /* last decoded code word */ unsigned int code; /* code word to decode */ unsigned char c; /* last decoded character */ /* open input and output files */ if (NULL == (bfpIn = BitFileOpen(inFile, BF_READ))) { perror(inFile); return FALSE; } if (NULL == outFile) { fpOut = stdout; } else { if (NULL == (fpOut = fopen(outFile, "wb"))) { BitFileClose(bfpIn); perror(outFile); return FALSE; } } /* start with 9 bit code words */ currentCodeLen = 9; /* initialize for decoding */ nextCode = FIRST_CODE; /* code for next (first) string */ /* first code from file must be a character. use it for initial values */ lastCode = GetCodeWord(bfpIn); c = lastCode; fputc(lastCode, fpOut); /* decode rest of file */ while ((code = GetCodeWord(bfpIn)) != EOF) { /* look for code length increase marker */ if (((CURRENT_MAX_CODES(currentCodeLen) - 1) == code) && (currentCodeLen < MAX_CODE_LEN)) { currentCodeLen++; code = GetCodeWord(bfpIn); } if (code < nextCode) { /* we have a known code. decode it */ c = DecodeRecursive(code, fpOut); } else { /*************************************************************** * We got a code that's not in our dictionary. This must be due * to the string + char + string + char + string exception. * Build the decoded string using the last character + the * string from the last code. ***************************************************************/ unsigned char tmp; tmp = c; c = DecodeRecursive(lastCode, fpOut); fputc(tmp, fpOut); } /* if room, add new code to the dictionary */ if (nextCode < MAX_CODES) { dictionary[nextCode - FIRST_CODE].prefixCode = lastCode; dictionary[nextCode - FIRST_CODE].suffixChar = c; nextCode++; } /* save character and code for use in unknown code word case */ lastCode = code; } fclose(fpOut); BitFileClose(bfpIn); return TRUE; }