static sInt32_t prepareApmStatus(uInt8_t *rsiResponseFrame) { sInt32_t frameLength = STATUS_FAILURE; // response frame length uInt8_t crcVal[CRC_SIZE]; // variable for CRC Value // Checking if input rsiResponseFrame pointer is invalid if (NULL == rsiResponseFrame) return frameLength; // Standard RSD_STATUS_IDLE Response rsiResponseFrame[0] = RSI_SOF; // SOF rsiResponseFrame[1] = ACP_ADDR; // ACP address rsiResponseFrame[2] = APM_STATUS; // response type rsiResponseFrame[3] = 3; rsiResponseFrame[4] = 0; rsiResponseFrame[5] = 0; rsiResponseFrame[6] = 0x80; //bit 7 = lock status (unlocked) // Calculate response frame CRC calculateCrc( &rsiResponseFrame[1], 6, crcVal ); // Add CRC bytes in the response frame rsiResponseFrame[7] = crcVal[CRC_LSB_INDEX]; rsiResponseFrame[8] = crcVal[CRC_MSB_INDEX]; frameLength = RSI_HEADER_LENGTH + 3 + CRC_SIZE; return frameLength; } // end prepareApmStatus()
/** * @brief Checks CRC of frame * * @param buffer Buffer containing message to check * @param msgLength Length of message in bytes * * @return success/failure */ sInt32_t rsi_validateFrame( uInt8_t *buffer, uInt16_t msgLength ) { uInt8_t crcVal[CRC_SIZE]; // CRC value sInt32_t retVal = STATUS_FAILURE; // Variable for Return value //assume frame has CRC... calculateCrc( buffer, msgLength-2, crcVal); //uInt32_t crc = (crcVal[1] << 8) | crcVal[0]; //printf("expected crc = 0x%04x\n", crc); if ((crcVal[CRC_LSB_INDEX] == buffer[msgLength - 2]) && (crcVal[CRC_MSB_INDEX] == buffer[msgLength - 1])) { //printf("validate CRC: OK!\n"); retVal = STATUS_SUCCESS; } else { //frame must have 1-byte checksum... uInt8_t checkSum = calculateChecksum( buffer, msgLength-1 ); if (checkSum == buffer[msgLength - 1]) { //printf("validate checksum: OK!\n"); retVal = STATUS_SUCCESS; } else printf("invalid RSI frame check sequence!\n"); } //FIXME: validate RSD/ACP frame address here? return retVal; }
ConstCharCrcString::ConstCharCrcString(const char *string, uint32 crc) : CrcString(), m_buffer(string) { #ifdef _DEBUG calculateCrc(); DEBUG_FATAL(m_crc != crc, ("calculated and specified crc values do not match")); #endif m_crc = crc; }
void PsiSection::finalize() { Section::finalize(); Section::setSectionSyntaxIndicator(true); /* reserved 2 (after table_id_extension) */ setByte(5, getByte(5) | 0xC0); /* 1100 0000 */ /* crc 32 */ unsigned sectionLength = getSectionLength(); unsigned crc = calculateCrc(data, sectionLength - 1); setByte(sectionLength - 1, (crc & 0xFF000000) >> 24); setByte(sectionLength , (crc & 0x00FF0000) >> 16); setByte(sectionLength + 1, (crc & 0x0000FF00) >> 8); setByte(sectionLength + 2, (crc & 0x000000FF) ); }
bool PsiSection::verify() const { if (Section::verify() && getSectionSyntaxIndicator()) /* crc32 exists */ return getCrc32() == calculateCrc(data, getSectionLength() - 1); return false; }
ConstCharCrcString::ConstCharCrcString(const char *string) : CrcString(), m_buffer(string) { calculateCrc (); }
int main() { char * inFp = "C:\\abc.txt"; //char * inFpZeroes= "C:\\abc_zeroes.txt"; char * inFpCrc = "C:\\abc_crc.txt"; char * outFp = "C:\\abc_compressed.txt"; char * decompressedFpCrc = "C:\\abc_decompressed_crc.txt"; char * decompressedFp = "C:\\abc_decompressed.txt"; copyFile(inFp, inFpCrc); //appendZeroes(inFpZeroes, SIZECRC); unsigned char * restPoly = calculateCrc(inFp); printf("Generated polynomial:\n"); printPolynomial(restPoly, SIZECRC); appendCrcToFile(inFpCrc, SIZECRC, restPoly); int lettersFrequencies[LETTERS_COUNT]; // = {81, 15, 28, 43, 128, 23, 20, 61, 71, 2, 1, 40, 24, 69, 76, 20, 1, 61, 64, 91, 28, 10, 24, 1, 20, 1, 130};; //char letters[LETTERS_COUNT] = {'?', '?', '?', '?', '?', '?', '?', '?', '?', '?','?', '?', '?', '?', '?','?', '?', '?', '?', '?','?', '?', '?', '?', '?','?', '?', '?', '?', '?', '?', '?', '!', '"', '#', '$', '%', '&', '(', ')', '*', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' '}; //printTabInt(lettersFrequencies, LETTERS_COUNT); //printTabChar(letters, LETTERS_COUNT); //printf("Letter indices: "); calculateFreq(inFpCrc, lettersFrequencies); //printTabInt(lettersFrequencies, 255); Node *nodes[LETTERS_COUNT]; int i; for(i=0; i<LETTERS_COUNT; i++) { nodes[i] = malloc(sizeof(Node)); nodes[i]->value = lettersFrequencies[i]; nodes[i]->letterIndex = i; nodes[i]->left = NULL; nodes[i]->right = NULL; } //printLetterIndices(nodes); Node *tree = buildHuffmanTree(nodes); int codeTable[LETTERS_COUNT]; int invertedCodeTable2[LETTERS_COUNT]; resetIntTable(codeTable, LETTERS_COUNT); //printf("codeTable: \n"); //printTabInt(codeTable, LETTERS_COUNT); fillTable(codeTable, tree, 0); invertCodeTable(codeTable, invertedCodeTable2); //printf("codeTable: \n"); //printTabInt(codeTable, LETTERS_COUNT); //printf("inverted codeTable: \n"); //printTabInt(invertedCodeTable2, LETTERS_COUNT); //printCodeTable(letters, codeTable, LETTERS_COUNT); //printCodeTableForIdx(codeTable, LETTERS_COUNT); //printTabInt(codeTable, LETTERS_COUNT); //createCrcFile(inFpCrc, SIZECRC, restPoly); //appendFileToFile(inFp, inFpCrc); compressFile(inFpCrc, outFp, invertedCodeTable2); printf("\n\ndecompressed file:\n"); decompressFile(outFp, decompressedFpCrc, tree); unsigned char * restPolyFromDec = calulateRest(decompressedFpCrc); printf("\n\nRest from decopressed file:\n"); printPolynomial(restPolyFromDec, SIZECRC); if(!isIntegral()){ printf("Integrity check failed!"); exit(EXIT_FAILURE); }else{ copySkip(decompressedFpCrc, decompressedFp, SIZECRC); } //checkIntegrity(); //FILE * output = fopen("C:\\codeblocks\\Huffman\\huffman\\to.txt","w"); //compressFile(input, output, codeTable); return 0; }