void checkSum(TreeNode *root, int sum, vector<int> tmp) { if(root == NULL) return; tmp.push_back(root->val); if(sum == root->val && root->left == NULL && root->right == NULL) { result.push_back(tmp); return; } if(root->left != NULL) checkSum(root->left, sum - root->val, tmp); if(root->right != NULL) checkSum(root->right, sum - root->val, tmp); }
int main() { int x,y; BSTNode *root=0; Interval *interval=0; size_t len; char buf[4096]; FILE *fi = fopen("TestOutput.out", "rb"); FILE *fo=fopen("logs.txt","a"); len = fread(buf, sizeof(char), sizeof(buf), fi); displayTime(fo); fprintf(fo,"Before execution:\n"); fprintf(fo,"The checksum of %s is %#x\n","output.out", checkSum(buf, len, 0)); fi=fopen("TestInput.in","r+"); fo=fopen("TestOutput.out","w"); //generateInput(nodeNr,intervalNr,pointNr,fi); fi=fopen("TestInput.in","r"); for(int i=1; i<=nodeNr; i++) { interval=createNewInterval(interval,fi); root=insert(root,interval); free(interval); } fprintf(fo,"\n"); for(int i=1; i<=intervalNr; i++) { interval=createNewInterval(interval,fi); fprintf(fo,"\n\nQuerry interval: [%d,%d] \n",interval->lo,interval->hi); fprintf(fo,"Intersected intervals:\n"); segmentIntersect(root,interval,fo); free(interval); } for(int i=1; i<=pointNr; i++) { fprintf(fo,"\n"); fscanf(fi,"%d",&x); fprintf(fo,"\n\nQuerry point:%d \n",x); searchPoint(root,x,fo); fprintf(fo,"\n"); } fi = fopen("TestOutput.out", "rb"); fo=fopen("logs.txt","a"); len = fread(buf, sizeof(char), sizeof(buf), fi); fprintf(fo,"After execution:\n"); fprintf(fo,"The checksum of %s is %#x\n\n","output.out", checkSum(buf, len, 0)); return 0; }
void bq34z100::chg48Table(uint16_t designCap, uint16_t designEnergy, uint16_t CellVT1T2, uint16_t CellVT2T3, uint16_t CellVT3T4, uint16_t CellVT4T5) { readFlash(48, 24); chgFlashPair(21, designCap); chgFlashPair(23, designEnergy); chgFlashPair(28, CellVT1T2);//0x0E10 = 3600 chgFlashPair(30, CellVT2T3);//0x0E10 = 3600 checkSum(48, 24); delay(300); readFlash(48, 35); chgFlashPair(32, CellVT3T4);//0x0E10 = 3600 chgFlashPair(34, CellVT4T5);//0x0E10 = 3600 checkSum(48, 35); }
void cmdHelp(void) // Lists Available Commands { char outstr[10]; int k; for(k=0;k<cmdNumber;k++) // iterate through all { myStringPut( cmdSet[k].cmdName ); // output command name outstr[0]=checkSumDigit1(checkSum(cmdSet[k].cmdName)); outstr[1]=checkSumDigit0(checkSum(cmdSet[k].cmdName)); outstr[2]='\0'; myStringPut(outstr); myNLPut(); } }
*/ uint8_t WaspRFID::read(uint8_t address, uint8_t *readData) //!Reading { delay(2); digitalWrite(MUX_USB_XBEE, HIGH); delay(2); printString(" ", _uart); dataTX[0] = 0x05; lengthCheckSum(dataTX); // Length Checksum dataTX[2] = HOSTTOPN532; // Code dataTX[3] = INDATAEXCHANGE; // Code dataTX[4] = 0x01; // Number of targets dataTX[5] = MIFARE_READ; //ReadCode dataTX[6] = address; //Read address dataTX[7] = 0x00; checkSum(dataTX); sendTX(dataTX , 8, 30); memset(readData, 0x00, 16); if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) { for (int i = 12; i < 28; i++) { readData[i-12] = dataRX[i]; } return 0; } else { return 1; } }
*/ uint8_t WaspRFID::write(uint8_t address, uint8_t *blockData) //!Writing { delay(_delay); digitalWrite(MUX_USB_XBEE, HIGH); delay(2); printString(" ", _uart); dataTX[0] = 0x15; lengthCheckSum(dataTX); // Length Checksum dataTX[2] = HOSTTOPN532; dataTX[3] = 0x40; //inDataEchange CODE dataTX[4] = 0x01; //Number of targets dataTX[5] = MIFARE_WRITE; //Write Command dataTX[6] = address; //Address for (int i = 0; i<16; i++) { dataTX[i+7] = blockData[i]; } dataTX[23] = 0x00; checkSum(dataTX); sendTX(dataTX , 24 ,14); if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) { return 0; } else { return 1; } }
*/ uint8_t WaspRFID::authenticate(uint8_t *UID, uint8_t blockAddress, uint8_t *keyAccess) { delay(_delay); digitalWrite(MUX_USB_XBEE, HIGH); delay(_delay); dataTX[0] = 0x0F; lengthCheckSum(dataTX); dataTX[2] = HOSTTOPN532; dataTX[3] = INDATAEXCHANGE; // inDataEchange dataTX[4] = 0x01; //Number of targets dataTX[5] = AUTH_WITH_KEYA; // Authentication code dataTX[6] = blockAddress; for (int i = 0; i<6 ; i++) { dataTX[i + 7] = keyAccess[i]; } dataTX[13] = UID[0]; dataTX[14] = UID[1]; dataTX[15] = UID[2]; dataTX[16] = UID[3]; dataTX[17] = 0x00; checkSum(dataTX); sendTX(dataTX , 18 ,14); if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) { return 0; } else { return 1; } }
void run() { createAccumulator(); accumulator()->process(Value(numeric_limits<long long>::max()), false); accumulator()->process(Value(numeric_limits<long long>::max()), false); accumulator()->process(Value(1.0), false); checkSum(); }
void run() { createAccumulator(); accumulator()->process(Value(5), false); accumulator()->process(Value(99), false); accumulator()->process(Value(0.2), false); checkSum(); }
int main(int argc, char* argv[]) { FILE *fp; FILE *newfile; size_t len; char *buf; long fsize; int cs; char ch; if (argc != 2) { printf("Usage: %s <filename>\n", argv[0]); exit(-1); } /* Open the specified file */ fp = fopen(argv[1], "r"); if (fp == NULL) { printf("Unable to open %s\n", argv[1]); exit(-1); } /*Open the file that is to be copied to */ newfile = fopen("copyfile", "w+"); if (newfile == NULL) { printf("Unable to create file"); exit(-1); } /* Obtain the file size */ fseek(fp, 0, SEEK_END); fsize = ftell(fp); rewind(fp); /* Copy file to buf */ buf = (char*) malloc(sizeof(char)*fsize); /*read and write the files in 1kb blocks*/ while (! feof (fp)) { printf("reading block\n"); len += fread(buf, 1, 1024, fp); printf("writing block\n"); fwrite (buf, 1, len, newfile); printf("1kb read and written\n"); } if (len != fsize) { printf("Error reading file %s", argv[1]); exit(-1); } cs = checkSum(buf, len, 0); printf("%d bytes were read in total\n", len); printf("Checksum of %s is %d\n", argv[1], cs); fclose(newfile); fclose(fp); free(buf); }
/* void findWord(string); findWOrd will search the hashtable and tell you if the word is found in it. if it is found it prints out the index and the words found with it. Pre- Hashtable exists Post- prints out if word is found or not */ void Program::findWord(std::string word) { int h = checkSum(word); bool found = false; for(int i = 0; i < hashTable[h].words.size(); i++) { if(hashTable[h].words[i] == word) { found = true; break; } } if(found) { std::cout<<"found at hash value: "<<h<<std::endl; std::cout<<"along with: "; for(int i = 0; i < hashTable[h].words.size(); i++) { if(hashTable[h].words[i] != word) { std::cout<<hashTable[h].words[i]<<"; "; } } std::cout<<std::endl; } else { std::cout<<"word not found"<<std::endl; } }
void Program::addWord(std::string word) { if(wordExist(word) == false) { hashTable[checkSum(word)].words.push_back(word); } }
*/ uint8_t WaspRFID::init(uint8_t *UID , uint8_t *ATQ) //! Request InListPassive { USB.flush(); delay(_delay); digitalWrite(MUX_USB_XBEE, HIGH); delay(_delay); dataTX[0] = 0x04; // Length lengthCheckSum(dataTX); // Length Checksum dataTX[2] = HOSTTOPN532; dataTX[3] = INLISTPASSIVETARGET; // Code dataTX[4] = 0x01; //MaxTarget dataTX[5] = 0x00; //BaudRate = 106Kbps dataTX[6] = 0x00; // Clear checkSum position checkSum(dataTX); sendTX(dataTX , 7 ,23); for (int i = 17; i < (21) ; i++) { _UID[i-17] = dataRX[i]; UID[i-17] = _UID[i-17]; } ATQ[0] = dataRX[13]; ATQ[1] = dataRX[14]; if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x4B) & (dataRX[11] == 0x01)) { return 0; } else { return 1; } }
//********************************************************************** //!The goal of this command is to detect as many targets (maximum MaxTg) // as possible in passive mode. uint8_t init(uint8_t *UID , uint8_t *ATQ) //! Request InListPassive { Serial.flush(); dataTX[0] = 0x04; // Length lengthCheckSum(dataTX); // Length Checksum dataTX[2] = 0xD4; dataTX[3] = 0x4A; // Code dataTX[4] = 0x01; //MaxTarget dataTX[5] = 0x00; //BaudRate = 106Kbps dataTX[6] = 0x00; // Clear checkSum position checkSum(dataTX); sendTX(dataTX , 7 ,23); for (int i = 17; i < (21) ; i++){ _UID[i-17] = dataRX[i]; UID[i-17] = _UID[i-17]; } ATQ[0] = dataRX[13]; ATQ[1] = dataRX[14]; if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x4B) & (dataRX[11] == 0x01)) { return 0; } else { return 1; } }
uint64_t hokuyo::Laser::readTime(int timeout) { char buf[100]; laserReadline(buf, 100, timeout); if (!checkSum(buf, 6)) HOKUYO_EXCEPT(hokuyo::CorruptedDataException, "Checksum failed on time stamp."); unsigned int laser_time = ((buf[0]-0x30) << 18) | ((buf[1]-0x30) << 12) | ((buf[2]-0x30) << 6) | (buf[3] - 0x30); if (laser_time == last_time_) { if (++time_repeat_count_ > 2) { HOKUYO_EXCEPT(hokuyo::RepeatedTimeException, "The timestamp has not changed for %d reads", time_repeat_count_); } else if (time_repeat_count_ > 0) ROS_DEBUG("The timestamp has not changed for %d reads. Ignoring for now.", time_repeat_count_); } else { time_repeat_count_ = 0; } if (laser_time < last_time_) wrapped_++; last_time_ = laser_time; return (uint64_t)((wrapped_ << 24) | laser_time)*(uint64_t)(1000000); }
*/ bool WaspRFID::getFirmware(void) //! It is needed to launch a simple command to sycnchronize { delay(_delay); digitalWrite(MUX_USB_XBEE, HIGH); delay(_delay); printString(" ", _uart); memset(dataTX, 0x00, 35); dataTX[0] = 0x02; // Length lengthCheckSum(dataTX); // Length Checksum dataTX[2] = HOSTTOPN532; // CODE dataTX[3] = 0x02; //TFI checkSum(dataTX); //0x2A; //Checksum sendTX(dataTX , 5 , 17); USB.print("Your Firmware version is : "); for (int i = 11; i < (15) ; i++) { USB.printHex(dataRX[i]); USB.print(" "); } USB.print("\n"); }
void bq34z100::chg82Table(uint16_t QMaxCell, uint16_t CellVAtMaxChargeTerm) { readFlash(82, 5); chgFlashPair(0, QMaxCell);//0x4B00 chgFlashPair(5, CellVAtMaxChargeTerm);//0x0E10 checkSum(82, 5); }
//********************************************************************** //!Write 16 bytes in address . uint8_t writeData(uint8_t address, uint8_t *blockData) //!Writing { Serial.print(" "); dataTX[0] = 0x15; lengthCheckSum(dataTX); // Length Checksum dataTX[2] = 0xD4; dataTX[3] = 0x40;//inDataEchange CODE dataTX[4] = 0x01;//Number of targets dataTX[5] = 0xA0;//Write Command dataTX[6] = address; //Address for (int i = 0; i < 16; i++) { dataTX[i+7] = blockData[i]; } dataTX[23] = 0x00; checkSum(dataTX); sendTX(dataTX , 24 ,14); if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) { return 0; } else { return 1; } }
int hokuyo::Laser::sendCmd(const char* cmd, int timeout) { if (!portOpen()) HOKUYO_EXCEPT(hokuyo::Exception, "Port not open."); char buf[100]; //printf("sendreq: %s\n", cmd); laserWrite(cmd); laserWrite("\n");; laserReadlineAfter(buf, 100, cmd, timeout); laserReadline(buf,100,timeout); //printf("chksum: %s",buf); if (!checkSum(buf,4)) { //printf("chksum error\n"); HOKUYO_EXCEPT(hokuyo::CorruptedDataException, "Checksum failed on status code."); } buf[2] = 0; //printf("sendreq_end: %s\n", cmd); if (buf[0] - '0' >= 0 && buf[0] - '0' <= 9 && buf[1] - '0' >= 0 && buf[1] - '0' <= 9) return (buf[0] - '0')*10 + (buf[1] - '0'); else HOKUYO_EXCEPT(hokuyo::Exception, "Hokuyo error code returned. Cmd: %s -- Error: %s", cmd, buf); }
int main (void) { long long int realCardNumber; int checkSum (long long int realCardNumber); printf ("Type in the credit card number\n"); scanf ("%lli", &realCardNumber); if ( checkSum(realCardNumber) == 0 ) { printf ("Yup, it's good."); } else printf ("Nope...\n%i", checkSum(realCardNumber)); return 0; }
static void gpsLoop_(const char *msg, uint8_t) { ++gpsN; if (!checkSum(msg)) { ++gpsKo; return; } almalog("gps", msg); }
vector<vector<TreeNode*>> routeSum(TreeNode* root, int num) { if (root == nullptr) return vector<vector<TreeNode*>>(); vector<vector<TreeNode*>> result; vector<TreeNode*> route; checkSum(root, num, route, result); return result; }
virtual unsigned_32 cSum() const { if ( _namePlusVar._strLen > 0 ) { if ( *_namePlusVar._string > 0 ) { return checkSum(_namePlusVar._string); } } return NO_CHKSUM; }
void hokuyo::Laser::readData(hokuyo::LaserScan& scan, bool has_intensity, int timeout) { scan.ranges.clear(); scan.intensities.clear(); int data_size = 3; if (has_intensity) data_size = 6; char buf[100]; int ind = 0; scan.self_time_stamp = readTime(timeout); int bytes; float range; float intensity; for (;;) { bytes = laserReadline(&buf[ind], 100 - ind, timeout); if (bytes == 1) // This is \n\n so we should be done return; if (!checkSum(&buf[ind], bytes)) HOKUYO_EXCEPT(hokuyo::CorruptedDataException, "Checksum failed on data read."); bytes += ind - 2; // Read as many ranges as we can get for (int j = 0; j < bytes - (bytes % data_size); j+=data_size) { if (scan.ranges.size() < MAX_READINGS) { range = (((buf[j]-0x30) << 12) | ((buf[j+1]-0x30) << 6) | (buf[j+2]-0x30)) / 1000.0; scan.ranges.push_back(range); if (has_intensity) { intensity = (((buf[j+3]-0x30) << 12) | ((buf[j+4]-0x30) << 6) | (buf[j+5]-0x30)); scan.intensities.push_back(intensity); } } else { HOKUYO_EXCEPT(hokuyo::CorruptedDataException, "Got more readings than expected"); } } // Shuffle remaining bytes to front of buffer to get them on the next loop ind = 0; for (int j = bytes - (bytes % data_size); j < bytes ; j++) buf[ind++] = buf[j]; } }
static void ICACHE_FLASH_ATTR checkLazyWrite(void) { if (dirtyCount == 0) return; if (!checkSum()) { // data has changed saveSum(); CFG_Save(); TESTP("sysCfg updated\n"); } }
void bq34z100::chg64Table(uint16_t packConfigReg, uint16_t alertConfigReg, uint8_t ledChgReg, uint8_t CellsS) { readFlash(64, 6); chgFlashPair(0, packConfigReg); chgFlash(4, ledChgReg); chgFlashPair(5, alertConfigReg); chgFlash(7, CellsS); //cell count checkSum(64, 6); }
void ICACHE_FLASH_ATTR CFG_checkLazyWrite(void){ if (dirtyCount == 0) return; if ((system_get_time() - lastSetDirty) > 2000000) { if (!checkSum()) { // data has changed saveSum(); CFG_Save(); TESTP("sysCfg updated\n") } lastSetDirty = system_get_time(); }
void WriteThread::run() { connect(this,SIGNAL(internalStartOpen()), this,SLOT(internalOpen()), Qt::QueuedConnection); connect(this,SIGNAL(internalStartReopen()), this,SLOT(internalReopen()), Qt::QueuedConnection); connect(this,SIGNAL(internalStartWrite()), this,SLOT(internalWrite()), Qt::QueuedConnection); connect(this,SIGNAL(internalStartClose()), this,SLOT(internalClose()), Qt::QueuedConnection); connect(this,SIGNAL(internalStartEndOfFile()), this,SLOT(internalEndOfFile()), Qt::QueuedConnection); connect(this,SIGNAL(internalStartFlushAndSeekToZero()), this,SLOT(internalFlushAndSeekToZero()),Qt::QueuedConnection); connect(this,SIGNAL(internalStartChecksum()), this,SLOT(checkSum()), Qt::QueuedConnection); exec(); }
Recognizer::ErrorCode Recognizer::errorCorrection(const QStringList & encodedValue, QStringList * const variants) { // making map of possible characters for each illegible character // and calculating checksum for each combination for make list of // success combinations "variants" // possible characters have only one different item // if it has IllegibleValues we are testing ONLY them QString value = decodeValue(encodedValue); bool hasIllegibleValues = value.contains("?"); QList <QList<int> > map; for (int k=0; k<value.size(); ++k) { QList<int> list; if (hasIllegibleValues && value[k] != '?') { map.append(list); continue; } QStringList illegibleValue; for (int i=0; i<3; ++i) // 3 since last line is empty illegibleValue.append(encodedValue[i].mid(k*3,3)); list = possibleCharacters(illegibleValue); map.append(list); } // testing each possible variant for checksum and adding correct one to "variants" list for (int i=0; i<value.size(); ++i) { for (int j=0; j<map[i].size(); ++j) { QString testValue = value; testValue.replace(i,1,QString::number(map[i][j])); if (checkSum(testValue) == OK) variants->append(testValue); } } if (variants->count() == 0) return ILL; else if (variants->count() == 1) return OK; else return AMB; return ERR; }
void Message::validate( const ValidationRules* vrptr ) { /* try { */ if ( !ValidationRules::shouldValidate( vrptr ) ) return; if ( ValidationRules::shouldValidateLength( vrptr ) ) { const BodyLength& aBodyLength = FIELD_GET_REF( m_header, BodyLength ); const int expectedLength = (int)aBodyLength; const int actualLength = bodyLength(); if ( expectedLength != actualLength ) { std::stringstream text; text << "Expected BodyLength=" << expectedLength << ", Received BodyLength=" << actualLength; throw InvalidMessage(text.str()); } } if ( ValidationRules::shouldValidateChecksum( vrptr ) ) { const CheckSum& aCheckSum = FIELD_GET_REF( m_trailer, CheckSum ); const int expectedChecksum = (int)aCheckSum; const int actualChecksum = checkSum(); if ( expectedChecksum != actualChecksum ) { std::stringstream text; text << "Expected CheckSum=" << expectedChecksum << ", Received CheckSum=" << actualChecksum; throw InvalidMessage(text.str()); } } /* } catch ( FieldNotFound& e ) { const std::string fieldName = ( e.field == FIX::FIELD::BodyLength ) ? "BodyLength" : "CheckSum"; throw InvalidMessage( fieldName + std::string(" is missing") ); } catch ( IncorrectDataFormat& e ) { const std::string fieldName = ( e.field == FIX::FIELD::BodyLength ) ? "BodyLength" : "CheckSum"; throw InvalidMessage( fieldName + std::string(" has wrong format: ") + e.detail ); } */ }