void BinStreamable::readFromBinFile(const char *filename, Format compression) { switch(compression) { case BIN: { ifbinstream file(filename); if (!file.good()) { fthrow(Exception, "readFromBinFile(): could not open file."); } readBin(file); file.close(); break; } #ifdef NICE_USELIB_ZLIB case BIN_GZ: { igzbinstream file(filename); if (!file.good()) { fthrow(Exception, "readFromBinFile(): could not open file."); } readBin(file); file.close(); break; } #endif default: fthrow(Exception, "File cannot be read! Compression method not supported."); break; } }
Rank::Rank(){ string rankFile; rankFile.append("/home/vijay/workplace/Resources/ranking.txt"); if(existFile(rankFile)){ readBin(rankList,rankFile); } }
void encryptBin(){ Keys key = readPubKey(); FILE* fp; int* arr; size_t size; printf("File name to encrypt: "); char* name; scanf("%s",name); myFlush(); if(fp = fopen(name,"rb")){ arr = readBin(fp,&size,name); encryptBytes(arr, size, key); writeToFile(arr, size,'b'); free(arr); }else{ printf("Error reading %s", name); } Keys empty = {0}; key = empty; menu(); }
void InstanceParser::readData() { Q_ASSERT(isStartElement() && name() == "data"); while(!atEnd()) { readNext(); if(isEndElement()) break; if(isStartElement()) { if(name() == "bin") readBin(); else if(name() == "item") readItem(); else readUnknownElement(); } } }
int main() { try { auto file = readBin(L"C:\\Windows\\System32\\calc.exe"); } catch (const BadFile& e) { std::cerr << e.what() << "\n"; return -1; } catch (...) { //don't terminate. } if (g_hFile == INVALID_HANDLE_VALUE) { std::cout << "PASS!\n"; return 0; } std::cerr << "FAIL!\n"; return -1; }
int main(void){ char op_code[100]; uint16_t instAddr; uint16_t topAddr; uint16_t baseAddr; uint16_t mem; int run = 1; char fileName[100]; char in[100]; puts("Initializing memory"); puts("Usage:\n ~pm lower upper : prints memor from lower to upper, <mem> = 1 for secondary"); puts("~rm : resets memory"); puts("~in : import file into memory"); puts("~wr : writes current MEM to the RAM chip"); puts("~rr <mem> : reads current RAM into MEM, reads into secondary mem if <mem> = 1"); puts("~cm : checks secondary memory to main memory"); puts("~q : quit"); //setup setup_io(); initGPIOs(); initMem(); GPIO_SET = 1 << WE; //UNCOMMENT FOR PI AS RAM //pthread_t tid; //pthread_create(&tid, NULL, memThread, NULL); while(run){ printf("Input: "); fgets(in, 99, stdin); sscanf(in, "%s %hu %hu %hu", op_code, &instAddr, &topAddr, &mem); //Process input if(!strcmp(op_code, "~q")){ printf("Shuting down memory...\n"); run = 0; } else if(!strcmp(op_code, "~pm")){ printMem(instAddr, topAddr, mem); printf("Current Address: %hu\n", readAddress()); } else if(!strcmp(op_code, "~rm")){ freeMem(); initMem(); } else if(!strcmp(op_code, "~in")){ printf("Enter file name, followed by an address to load at: "); scanf("%s %hu", fileName, &baseAddr); puts("WARNING, make sure the base address is set correctly when assembling file"); while(getchar()!= '\n'); if(readBin(fileName, baseAddr) == -1){ puts("Could not open file"); } } else if(!strcmp(op_code, "~wr")){ printf("Writing current MEM to RAM...\n"); writeRAMChip(); } else if(!strcmp(op_code, "~rr")){ printf("Reading RAM into Memory\n"); readRAMChip(instAddr); } else if(!strcmp(op_code, "~off")){ initGPIOs(); } else if(!strcmp(op_code, "~cm")){ checkMEM(instAddr); } } }
int main(int argc, char **argv) { printf("\n======= GCOR v1.1 - Automatic Gain Correction =======\n\tR. Lica, IFIN-HH, Dec2013 \n\n"); int chNum, detNum, runstart, runstop, irun, idet; int minWIDTH, maxWIDTH, SHIFT, SWEEP, degree; int low, high; char name[20]; FILE *settings, *fi, *fo; initialize(settings, &chNum, &detNum, name, &runstart, &runstop, &minWIDTH, &maxWIDTH, &SHIFT, &SWEEP, °ree, &low, &high); degree++; // in the program degree represents the number of coefficients of the polynomial and NOT the degree of the polynomial. i know... int regions=(high-low)/SHIFT; struct Data2Fit shData[regions]; int i, j, k, nData=0; double refSpec[chNum], Spec[chNum]; double coeff[degree], chisq, norm; FILE * gnuplotPipe = popen ("gnuplot", "w"); char outfile[20], infile[20], answer; sprintf(outfile, "gcor.cal"); fo=fopen(outfile, "wt"); for(idet=0; idet<detNum; idet++) { fprintf(fo, "%5d%5d%5d%9.3f%10.6f\n", runstart, idet, 2, 0.0, 1.0); //each detector from the first run is set as reference for(irun=runstart+1; irun<=runstop; irun++) { for (i=0; i<regions; i++) { shData[i].ch=0; shData[i].chShift=0; shData[i].err=0; } sprintf(infile, "%2s.%04d", name, runstart); if(fopen(infile, "rb")) fi=fopen(infile, "rb"); else { printf("Cannot open %2s.%04d\n", name, runstart); exit(0); } readBin(fi, refSpec, idet, chNum, low, high); sprintf(infile, "%2s.%04d", name, irun); if(fopen(infile, "rb")) fi=fopen(infile, "rb"); else continue; readBin(fi, Spec, idet, chNum, low, high); normalize(refSpec, Spec, chNum, &norm); smooth(refSpec, Spec, minWIDTH/5, maxWIDTH/2, low, high); deriv(refSpec, Spec, low, high, 5); autoshift(refSpec, Spec, shData, low, high, minWIDTH, maxWIDTH, SHIFT, SWEEP, regions); nData = performFit(shData, degree, &chisq, coeff, regions); if (nData == 0) { printf("Warning! %2s#%02d.%04d: Could not extract data suitable for fit. Change settings!\n", name, idet, irun); goto skip; } gnuplot(gnuplotPipe, irun, idet, shData, nData, degree, chisq, coeff, regions, norm); writeCal(fo, degree, coeff, irun, idet); if (answer=='a') goto skip; printf("Going to %2s#%02d.%04d ([y]/n)? (Type 'a' for automatic fit)", name, idet, irun+1); answer = getchar(); skip: if (answer=='n') exit(0); } printf("---------------------\nGoing to Detector #%02d\n---------------------\n", idet+1); } exit(0); }
uint16_t ADM_ebml::readu16(void) { uint8_t v[2]; readBin(v,2); return (uint16_t)(v[0]<<8)+v[1]; }
uint8_t ADM_ebml::readu8(void) { uint8_t v; readBin(&v,1); return v; }
int getBin(void) { extern text_t *dport_ptr; return readBin(dport_ptr); }
/* * Main loop used to continuously ask for user input */ int mainloop(){ //Local variables int mode = 0; char op_code[100]; uint16_t instAddr; uint16_t topAddr; uint16_t baseAddr; char fileName[100]; char run = 1; char in[100]; regA.data = 0; regSTAT.data = 0; nibble currentInst; int tempAddress = 0; int instrRun = 0; struct timespec gettime_now; struct timespec newTime = {0, 0}; long totalFirstTime; long totalSecondTime; long firstTime; long secondTime; long period = 200000; char step = 0; while(run){ if(mode == USERMODE) printf("Input: "); fgets(in, 99, stdin); sscanf(in, "%s %hu %hu", op_code, &instAddr, &topAddr); //Process input if(!strcmp(op_code, "~q")){ printf("Halting\n"); run = 0; } else if(!strcmp(op_code, "~pm")){ printMem(instAddr, topAddr); } else if(!strcmp(op_code, "~pr")){ printReg(); } else if(!strcmp(op_code, "~in")){ printf("Enter file name, followed by an address to load at: "); scanf("%s %hu", fileName, &baseAddr); puts("WARNING, make sure the base address is set correctly when assembling file"); while(getchar()!= '\n'); if(readBin(fileName, baseAddr) == -1){ printf("Entering User Input Mode"); mode = USERMODE; } } else if(!strcmp(op_code, "~run")){ regPC = instAddr; instrRun = 0; regSTAT.data &= 0xD; mode = FILEMODE; step = 0; } else if(!strcmp(op_code, "~step")){ if(instAddr != NULL) { regPC = instAddr; regSTAT.data &= 0xD; } instrRun = 0; mode = FILEMODE; step = 1; } else if(!strcmp(op_code, "~cp")){ printf("Enter period: "); scanf("%li", &period); while(getchar()!= '\n'); } else if(!strcmp(op_code, "~rm")){ freeMem(); initMem(); } else { if(mode == USERMODE) decode(op_code, instAddr); } if(mode == FILEMODE){ if(step == 0) { puts("Program started..."); } else if (step == 1) { puts("Stepping forward..."); } //Runs while HLT is off while(!(regSTAT.data & 0x2)){ //Start of file code //EXECUTE FIRST 4 BITS currentInst = readMem(regPC); clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_CLR = 1<<CLKPIN; #endif clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_SET = 1<<CLKPIN; #endif //EXECUTE SECOND 4 BITS instAddr = 0; tempAddress = 0; tempAddress = readMem(++regPC).data; instAddr |= (tempAddress << 12); clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2); #ifdef RPI GPIO_CLR = 1 <<CLKPIN; #endif clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_SET = 1<<CLKPIN; #endif //EXECUTE THIRD 4 BITS tempAddress = readMem(++regPC).data; instAddr |= (tempAddress << 8); clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_CLR = 1<<CLKPIN; #endif clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_SET = 1<<CLKPIN; #endif //EXECUTE FOURTH 4 BITS tempAddress = readMem(++regPC).data; instAddr |= (tempAddress << 4); clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_CLR = 1 <<CLKPIN; #endif clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_SET = 1<<CLKPIN; #endif //EXECUTE FIFTH 4 BITS tempAddress = readMem(++regPC).data; instAddr |= (tempAddress); regPC++; clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_CLR = 1<<CLKPIN; #endif clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_SET = 1<<CLKPIN; #endif if(currentInst.data == HLT) decode("HLT", instAddr); else if(currentInst.data == LOD) decode("LOD", instAddr); else if(currentInst.data == STR) decode("STR", instAddr); else if(currentInst.data == ADD) decode("ADD", instAddr); else if(currentInst.data == NOP) decode("NOP", instAddr); else if(currentInst.data == NND) decode("NND", instAddr); else if(currentInst.data == CXA) decode("CXA", instAddr); else if(currentInst.data == JMP) decode("JMP", instAddr); else #ifndef __MINGW32__ shutdown(UNKNOWNINSTRUCTIONERROR); #endif #ifdef __MINGW32__ shutdown_vm4(UNKNOWNINSTRUCTIONERROR); #endif clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_CLR = 1 <<CLKPIN; #endif clock_gettime(CLOCK_REALTIME, &gettime_now); firstTime = gettime_now.tv_nsec; waitForPeriod(firstTime,gettime_now,period/2 ); #ifdef RPI GPIO_SET = 1<<CLKPIN; #endif if(step == 1) { break; } } if((regSTAT.data & 0x2) && step) { puts("Computer halted"); } mode = USERMODE; if(step == 0) { puts("Program finished"); printf("Instructions run %d\n", instrRun); #ifdef RPI GPIO_CLR = 1 << CLKPIN; #endif } } } return 1; }