int main(int argc, char** argv) { char *input_path = 0; char *output_path = 0; char *dictionary_path = 0; int force = 0; int quality = 11; int decompress = 0; int repeat = 1; int verbose = 0; int lgwin = 0; clock_t clock_start; int i; ParseArgv(argc, argv, &input_path, &output_path, &dictionary_path, &force, &quality, &decompress, &repeat, &verbose, &lgwin); clock_start = clock(); for (i = 0; i < repeat; ++i) { FILE* fin = OpenInputFile(input_path); FILE* fout = OpenOutputFile(output_path, force || repeat); int is_ok = 0; if (decompress) { is_ok = Decompress(fin, fout, dictionary_path); } else { is_ok = Compress(quality, lgwin, fin, fout, dictionary_path); } if (!is_ok) { unlink(output_path); exit(1); } if (fclose(fin) != 0) { perror("fclose"); exit(1); } if (fclose(fout) != 0) { perror("fclose"); exit(1); } } if (verbose) { clock_t clock_end = clock(); double duration = (double)(clock_end - clock_start) / CLOCKS_PER_SEC; int64_t uncompressed_size; double uncompressed_bytes_in_MB; if (duration < 1e-9) { duration = 1e-9; } uncompressed_size = FileSize(decompress ? output_path : input_path); if (uncompressed_size == -1) { fprintf(stderr, "failed to determine uncompressed file size\n"); exit(1); } uncompressed_bytes_in_MB = (double)(repeat * uncompressed_size) / (1024.0 * 1024.0); if (decompress) { printf("Brotli decompression speed: "); } else { printf("Brotli compression speed: "); } printf("%g MB/s\n", uncompressed_bytes_in_MB / duration); } return 0; }
////////////////////////////////////////////////////////// // // GetExeFileSize // // // ////////////////////////////////////////////////////////// uint64 GetExeFileSize( bool bUseExeCopy ) { SString strGTAEXEPath = GetExePathFilename( bUseExeCopy ); return FileSize( strGTAEXEPath ); }
bool CPPF::OpenAll() { bool RetVal; RetVal=true; if(m_pParam->GetString(TYPE_ORIGINALNAME) != NULL) { m_pOriginal=OPEN(m_pParam->GetString(TYPE_ORIGINALNAME),"rb+"); if(m_pOriginal==NULL) { printf("Cannot open file \"%s\"\n",m_pParam->GetString(TYPE_ORIGINALNAME)); RetVal=false; } else { m_oOriginal=FileSize(m_pOriginal); } } if(m_pParam->GetString(TYPE_BINARYNAME) != NULL) { m_pPatched=OPEN(m_pParam->GetString(TYPE_BINARYNAME),"rb"); if(m_pPatched==NULL) { printf("Cannot open file \"%s\"\n",m_pParam->GetString(TYPE_BINARYNAME)); RetVal=false; } else { m_oPatched=FileSize(m_pPatched); } } if(m_pParam->GetString(TYPE_FILEIDNAME) != NULL) { m_pFileID=OPEN(m_pParam->GetString(TYPE_FILEIDNAME),"rb"); if(m_pFileID==NULL) { printf("Cannot open file \"%s\"\n",m_pParam->GetString(TYPE_FILEIDNAME)); RetVal=false; } else { m_oFileID=FileSize(m_pFileID); } } if(m_pParam->GetString(TYPE_PPFNAME) != NULL) { if(m_pParam->GetParameters().apply==1) { m_pPPF=OPEN(m_pParam->GetString(TYPE_PPFNAME),"rb+"); } else { m_pPPF=OPEN(m_pParam->GetString(TYPE_PPFNAME),"wb"); } if(m_pPPF==NULL) { printf("Cannot open file \"%s\"\n",m_pParam->GetString(TYPE_PPFNAME)); RetVal=false; } else { m_oPPF=FileSize(m_pPPF); } } return(RetVal); }
RandomAccessFileDevice::RandomAccessFileDevice( std::string filename, DeviceMode mode) : FileDevice(filename, mode, FileSize(0)) { }
int main(int argc, char *argv[]) { int argn = 1; char filename[512]; char cartname[32]; FILE *f; ulOptions = 0; if ((--argc) == 0) PrintUsage(); while (*argv[argn] == '-') { argc -= 1; switch (argv[argn++][1]) { case '?': case 'h': PrintUsage(); break; case 'd': ulOptions |= OPTF_DEBUG; break; case 'p': ulOptions |= OPTF_PAD; break; case 'r': ulOptions |= OPTF_TRUNCATE; break; case 'v': ulOptions |= OPTF_VALIDATE; break; case 't': strncpy(cartname, argv[argn - 1] + 2, 16); ulOptions |= OPTF_TITLE; break; } } strcpy(filename, argv[argn++]); if (!FileExists(filename)) strcat(filename, ".gb"); f = fopen(filename, "rb+"); if (!f) FatalError("Unable to open file"); /* * -d (Debug) option code * */ if (ulOptions & OPTF_DEBUG) { printf("-d (Debug) option enabled...\n"); } /* * -p (Pad) option code * */ if (ulOptions & OPTF_PAD) { long size, padto; long bytesadded = 0; size = FileSize(f); padto = 0x8000L; while (size > padto) padto *= 2; printf("Padding to %ldkB:\n", padto / 1024); /* if( padto<=0x80000L ) { */ if (size != padto) { fflush(stdout); fseek(f, 0, SEEK_END); while (size < padto) { size += 1; if ((ulOptions & OPTF_DEBUG) == 0) fputc(0, f); bytesadded += 1; } fflush(f); printf("\tAdded %ld bytes\n", bytesadded); } else printf("\tNo padding needed\n"); /* } else FatalError( "Image size exceeds 512kB" ); */ } /* * -r (Truncate) option code * */ if (ulOptions & OPTF_TRUNCATE) { long size, padto; char tempfile[] = "/tmp/rgbfix-XXXXXX"; FILE *tf; size = FileSize(f); padto = 256 * 32768; while (size < padto) padto /= 2; printf("Truncating to %ldkB:\n", padto / 1024); mkstemp(tempfile); if ((ulOptions & OPTF_DEBUG) == 0) { if ((tf = fopen(tempfile, "wb")) != NULL) { fseek(f, 0, SEEK_SET); while (padto--) { fputc(fgetc(f), tf); } fclose(f); fclose(tf); remove(filename); rename(tempfile, filename); f = fopen(filename, "rb+"); } } } /* * -t (Set carttitle) option code * */ if (ulOptions & OPTF_TITLE) { printf("Setting cartridge title:\n"); if ((ulOptions & OPTF_DEBUG) == 0) { fflush(f); fseek(f, 0x0134L, SEEK_SET); fwrite(cartname, 16, 1, f); fflush(f); } printf("\tTitle set to %s\n", cartname); } /* * -v (Validate header) option code * */ if (ulOptions & OPTF_VALIDATE) { long i, byteschanged = 0; long cartromsize, calcromsize = 0, filesize; long carttype; unsigned short cartchecksum = 0, calcchecksum = 0; unsigned char cartcompchecksum = 0, calccompchecksum = 0; int ch; printf("Validating header:\n"); fflush(stdout); /* Nintendo Character Area */ fflush(f); fseek(f, 0x0104L, SEEK_SET); for (i = 0; i < 48; i += 1) { int ch; ch = fgetc(f); if (ch == EOF) ch = 0x00; if (ch != NintendoChar[i]) { byteschanged += 1; if ((ulOptions & OPTF_DEBUG) == 0) { fseek(f, -1, SEEK_CUR); fputc(NintendoChar[i], f); fflush(f); } } } fflush(f); if (byteschanged) printf ("\tChanged %ld bytes in the Nintendo Character Area\n", byteschanged); else printf("\tNintendo Character Area is OK\n"); /* ROM size */ fflush(f); fseek(f, 0x0148L, SEEK_SET); cartromsize = fgetc(f); if (cartromsize == EOF) cartromsize = 0x00; filesize = FileSize(f); while (filesize > (0x8000L << calcromsize)) calcromsize += 1; if (calcromsize != cartromsize) { if ((ulOptions & OPTF_DEBUG) == 0) { fseek(f, -1, SEEK_CUR); fputc(calcromsize, f); fflush(f); } printf("\tChanged ROM size byte from 0x%02lX (%ldkB) to 0x%02lX (%ldkB)\n", cartromsize, (0x8000L << cartromsize) / 1024, calcromsize, (0x8000L << calcromsize) / 1024); } else printf("\tROM size byte is OK\n"); /* Cartridge type */ fflush(f); fseek(f, 0x0147L, SEEK_SET); carttype = fgetc(f); if (carttype == EOF) carttype = 0x00; if (FileSize(f) > 0x8000L) { /* carttype byte must != 0x00 */ if (carttype == 0x00) { if ((ulOptions & OPTF_DEBUG) == 0) { fseek(f, -1, SEEK_CUR); fputc(0x01, f); fflush(f); } printf ("\tCartridge type byte changed to 0x01\n"); } else printf("\tCartridge type byte is OK\n"); } else { /* carttype byte can be anything? */ printf("\tCartridge type byte is OK\n"); } /* Checksum */ fflush(f); fseek(f, 0, SEEK_SET); for (i = 0; i < (0x8000L << calcromsize); i += 1) { ch = fgetc(f); if (ch == EOF) ch = 0; if (i < 0x0134L) calcchecksum += ch; else if (i < 0x014DL) { calccompchecksum += ch; calcchecksum += ch; } else if (i == 0x014DL) cartcompchecksum = ch; else if (i == 0x014EL) cartchecksum = ch << 8; else if (i == 0x014FL) cartchecksum |= ch; else calcchecksum += ch; } calccompchecksum = 0xE7 - calccompchecksum; calcchecksum += calccompchecksum; if (cartchecksum != calcchecksum) { fflush(f); fseek(f, 0x014EL, SEEK_SET); if ((ulOptions & OPTF_DEBUG) == 0) { fputc(calcchecksum >> 8, f); fputc(calcchecksum & 0xFF, f); }