// // 生成数据包 // void FirsPackDataPack( unsigned int msgFlag, unsigned char msgType, unsigned short packSn, unsigned int packType, unsigned char subType, unsigned char * dataBuf, unsigned short dataLen, unsigned char * packBuf, int & packLen ) { if ( packBuf != NULL ) { FIRS_PACK_DATA *pFirsPackData = ( FIRS_PACK_DATA * )packBuf; FirsPackDataHead( pFirsPackData->head, msgFlag, msgType, packSn, packType, subType, dataLen ); packLen = sizeof( pFirsPackData->head ); if ( dataBuf != NULL && dataLen > 0 ) { memcpy( (char *)packBuf + packLen, dataBuf, dataLen ); packLen += dataLen; } unsigned short check = htons( GetCheckSum( packBuf, packLen ) ); memcpy( (char *)packBuf + packLen, (char *)&check, sizeof(check) ); packLen += sizeof(check); } }
void AddStepSpy(void *start, long length, SpyKind spyKind, char *id) { StepSpy *newSpy; #ifdef _JB_DEBUG DecOnScreen(0, -16, kOnScreenBlue, kOnScreenWhite, ++gStepSpyCounter); #endif if(kStepSpyPointerToVoid == spyKind) { newSpy = new StepSpy(&gStepSpyListLong); newSpy->start = start; newSpy->value = *((long *) start); newSpy->length = length | 0x80000000; newSpy->checksum = *((long *) start); } else if((kStepSpyHandle == spyKind) || (length & 1) || (((long) start) & 3)) { newSpy = new StepSpy(&gStepSpyList); newSpy->start = start; newSpy->length = length | ((kStepSpyHandle == spyKind) ? 0x80000000 : 0); newSpy->checksum = GetCheckSum(newSpy); } else if(4 == length) { newSpy = new StepSpy(&gStepSpyListLong); newSpy->start = start; newSpy->length = length; newSpy->checksum = *((long *) start); } else // length is divisible by 2, start is divisible by 2 { newSpy = new StepSpy(&gStepSpyListEvenEven); newSpy->start = start; newSpy->length = length >> 1; newSpy->checksum = GetCheckSum2(newSpy); } newSpy->id = id; }
BuildingTypeManager::BuildingTypeManager() { if (s_instance) throw std::runtime_error("Attempted to create more than one BuildingTypeManager."); TraceLogger() << "BuildingTypeManager::BuildingTypeManager() about to parse buildings."; try { parse::buildings(m_building_types); } catch (const std::exception& e) { ErrorLogger() << "Failed parsing buildings: error: " << e.what(); throw e; } TraceLogger() << "Building Types:"; for (const auto& entry : m_building_types) { TraceLogger() << " ... " << entry.first; } // Only update the global pointer on sucessful construction. s_instance = this; DebugLogger() << "BuildingTypeManager checksum: " << GetCheckSum(); }
main (int argc, char * argv[]) { FILE * File1, * File2 ; // the input file and output file of sorting long BufferRecords = 0; // number of records in buffer long error = 0; // error flag long RecordNum1 = 0; // total record number in file1 long RecordNum2 = 0; // total record number in file2 unsigned long CheckSum1 = 0; // checksum of file1 unsigned long CheckSum2 = 0; // checksum of file2 char LastRecord[RecordSize]; // a copy of the last record in the buffer bool OrderSign = true; // sign of whether all the records in the file have the desired order /************************************************************************ * * * Put out greeting * * * ************************************************************************/ fprintf(stderr,"ChkSort (%s) - Verify your PennySort result with its checksum and order.", VERSION); fprintf(stderr,"\n Author: Peng Liu, Tsinghua University, China. [email protected] 12/2002\n"); /********************************************************************* * * * Decode parameters * * * *********************************************************************/ if (argc != 3) { usage(); error = 1; goto common_exit; } FileName1=argv[1]; FileName2=argv[2]; /************************************************************************/ /* */ /* Allocate file buffer */ /* */ /************************************************************************/ Buffer = (char *)malloc( BufferSize ) ; if( Buffer == NULL ) { fprintf(stderr,"\nCouldn't allocate the I/O buffer\n"); error = 1; goto common_exit; } /************************************************************************/ /* */ /* Calculate checksum of <filename1> */ /* */ /************************************************************************/ fprintf(stderr,"\nAnalysing file: %s \n", FileName1); // open <filename1> File1 = fopen( FileName1, "r+b"); if( File1 == NULL ) { fprintf(stderr,"\nError opening file %s\n", FileName1) ; error = 1; goto common_exit; } // check whether the record length in <filename1> is exactly 100 bytes long if (CheckRecordLen(File1, FileName1)==false) { error = 1; goto common_exit; } // count the total number of records and calculate its checksum RecordNum1=0; while ((BufferRecords=ReadToBuffer(File1)) !=0) { RecordNum1 += BufferRecords; CheckSum1 += GetCheckSum(BufferRecords); } fclose(File1); // tell the user about its record number and checksum fprintf(stderr, "\nRecord Number: %d Checksum: %lX\n",RecordNum1, CheckSum1); /************************************************************************/ /* */ /* Calculate checksum and check order of <filename2> */ /* */ /************************************************************************/ fprintf(stderr,"\n\nAnalysing file: %s \n", FileName2); // open <filename2> File2 = fopen( FileName2, "r+b"); if( File2 == NULL ) { fprintf(stderr,"\nError opening file %s\n", FileName2) ; error = 1; goto common_exit; } // check whether the record length in <filename2> is exactly 100 bytes long if (CheckRecordLen(File2, FileName2)==false) { error = 1; goto common_exit; } // count the number of records and checksum of <filename2> // and check the order of records in <filename2> one by one RecordNum2=0; LastRecord[0]=0; while ((BufferRecords=ReadToBuffer(File2)) !=0) { OrderSign=VerifyOrder(LastRecord, BufferRecords); // Compare the last record of previous buffer content with the first record of this one if (!OrderSign) break; RecordNum2 += BufferRecords; CheckSum2 += GetCheckSum(BufferRecords); } fclose(File2); if (!OrderSign) // if the order of the records is not correct goto common_exit; // tell the user about its record number and checksum fprintf(stderr, "\nRecord Number: %d Checksum: %lX\n",RecordNum2, CheckSum2); /************************************************************************/ /* */ /* Put out result message of comparing checksum and order */ /* */ /************************************************************************/ if (CheckSum1==CheckSum2) fprintf(stderr, "\n\nCongratulations: Verification Passed!\n"); else fprintf(stderr, "\n\nBad News: Verification failed due to different checksums \n(although order of records are correct)!\n"); /************************************************************************/ /* */ /* All Done */ /* close output file; and exit */ /* */ /************************************************************************/ common_exit: free(Buffer); // free record buffer return(error); // return status code } // End of ChkSort.cpp