int main( int argc, char** argv ) { test: int mode = argc<2 ? 1 : argv[1][0]!='d'; FILE* f = fopen( argc<3 ? mode ? "book1" : "book1.ari" : argv[2], "rb" ); FILE* g = fopen( argc<4 ? mode ? "book1.ari" : "book1.unp" : argv[3], "wb" ); if( (f==0) || (g==0) ) return 1; if( mode ) { uint f_len = flen( f ); fwrite( &f_len, 1,4, g ); Model_5_0_process( f, g, f_len ); } else { uint f_len; fread( &f_len, 1,4, f ); Model_5_1_process( g, f, f_len ); } fclose(f); fclose(g); if( (argv[1][0]=='t') && (argc==4) ) { argv[2]=argv[3]; argv[3]=&argv[1][1]; argv[1][0] = 'd'; goto test; } return 0; }
int main(int argc,char **argv) { if(argc < 2) { fprintf(stderr,"Usage...\n"); exit(1); } printf("%lld\n",(long long)flen(argv[1])); exit(0); }
kstring_t salted_rand_string(char *infname, size_t n_rand) { if(std::strchr(infname, '/')) infname = strrchr(infname, '/') + 1; std::string tmp(infname); while(std::strchr(tmp.c_str(), '.')) { int n(tmp.c_str() + tmp.size() - std::strchr(tmp.c_str(), '.') + 1); while(--n) tmp.pop_back(); } tmp.push_back('.'); size_t flen(tmp.size() + n_rand + 1); const char cstr[] {"ABCDEFGHIJKLMNOPQRTSUVWXYZ1234567890"}; while(tmp.size() < flen) tmp.push_back(cstr[rand() % (sizeof(cstr) - 1)]); kstring_t ret{0}; kputs(tmp.data(), &ret); return ret; }
// allocates a buffer and reads in a text file -- used for shader loading static char *loadText( char *fname ) { char *text = NULL; FILE *fp = fopen( fname, "r" ); if( fp ) { long len = flen( fp ); text = malloc( len+1 ); fread( text, 1, len, fp ); fclose( fp ); text[len] = 0; // null-terminate string } else { printf( "Error opening '%s'.\n", fname ); } return text; }
cl_byte *cl_load_bin(char *filename, size_t *pNumBytes) { cl_byte *bin = NULL; FILE *fp = fopen(filename, "rb"); if (fp != NULL) { size_t numBytes = flen(fp); bin = (cl_byte *)cl_malloc(numBytes); if (bin) { size_t b = fread(bin, 1, numBytes, fp); #ifdef CL_DEBUG printf("Read "FMT_SIZE_T" bytes\n", b); #endif fclose(fp); } } return bin; }
static int fast_cp(FILE *fpr, FILE *fpw) { int buf[BUFSIZE]; int nil = 0, nilline = 0; while (1) { fread(buf, BUFSIZE * 4, 1, fpr); if (feof(fpr)) break; if ((nil = buf_nil(buf)) == BUFSIZE) { nilline++; } else { fseeko(fpw, nilline * BUFSIZE * sizeof(int) + nil * sizeof(int), SEEK_CUR); fwrite(buf + nil, sizeof(int), BUFSIZE - nil, fpw); /* if error */ nilline = 0; nil = 0; } } fseeko(fpw, nilline * BUFSIZE * sizeof(int) + nil * sizeof(int), SEEK_CUR); fwrite(buf, 1, flen(fpr) % (BUFSIZE * sizeof(int)), fpw); /*last line*/ return 0; }
int main(int argc, const char * argv[]) { size_t i; if (argc==1) // no arguments { show_basics(); return 0; } for (i = 1; i < argc; i++) // parse arguments { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'f':strcpy(infile,argv[++i]); break; case 'o':strcpy(outfile,argv[++i]); break; case 'd':diags=TRUE; break; } } } if (strlen(infile)>0) // read file for tags { num_tags=flen(infile); get_file(); } else // read string from stdin for tags { i=0; char *s,*t,*nt; t = strdup(argv[argc-1]); // string of tags, space-delimted nt = strdup(argv[argc-1]); num_tags = num_splits(nt); tags = malloc(sizeof(*tags) * num_tags); s=strtok(t," "); // ashen while (s != NULL) { tags[i] = strdup(s); s = strtok (NULL, " "); ++i; } num_tags=i; } matches = malloc(num_tags * sizeof(struct match)); setup_screen(); get_current(); items[strlen(items)-1]='\0'; // remove trailing space-separation char endwin(); if (!exiting) { if (strlen(outfile)>0) put_file(); else printf("%s\n",items); } // be kind and free up allocations for(i = 0; i < num_tags; i++) free(tags[i]); // free tags free(tags); free(matches); return 0; }
bool parse_mds(wchar *mds_filename,bool verbose) { /*"returns the supposable bin_filename and raw entries, if something went \ wrong it throws an exception"*/ // read the file FILE*mds_file = _tfopen (mds_filename,L"rb"); if(!mds_file) { fprintf(stderr,"Could not open the mds-file <%s>\n",mds_filename); return false; } int mds_size = flen(mds_file); //if its too small .. if (mds_size<16) return false; //do NOT load whole file to memory before checking ... cna be deadly for .nrg/.iso/ anything :p char mds_key[16]; fseek(mds_file,0,SEEK_SET); fread(mds_key,16,1,mds_file); if(memcmp(mds_key,"MEDIA DESCRIPTOR",16)!=0) { fwprintf(stderr,L"Invalid data in <%s>. It is not an MDF/MDS file.\n",mds_filename); fclose(mds_file); return false; } fseek(mds_file,0,SEEK_SET); char *mds_content=(char*)malloc(mds_size); if(!mds_content) { fwprintf(stderr,L"Could not allocate a buffer to read <%s>\n",mds_filename); fclose(mds_file); return false; } fread(mds_content,mds_size,1,mds_file); if(memcmp(mds_content,"MEDIA DESCRIPTOR",16)!=0) { fwprintf(stderr,L"Invalid data in <%s>. It is not an MDF/MDS file.\n",mds_filename); free(mds_content); fclose(mds_file); return false; } // get some data from header int mds_header_size = 0x70; int mds_datablock_size = 0x50; int mds_extrablock_size = 0x08; int mds_footer_size = 0x16; int mds_version = read_binary(char, mds_content, 0x0010); int mds_revision = read_binary(char, mds_content, 0x0011); int mds_sessions = read_binary(unsigned char, mds_content, 0x0014); if(verbose) { printf("MDS/MDF version: %d.%d\n",mds_version, mds_revision); } nsessions=mds_sessions; int mds_session_offset = 0x58; int mds_extrablocks_offset=0; for(int mds_current_session=0;mds_current_session<mds_sessions;mds_current_session++) { sessions[mds_current_session].pregap = read_binary(int, mds_content, mds_session_offset+0x0000); sessions[mds_current_session].sectors = read_binary(int, mds_content, mds_session_offset+0x0004); sessions[mds_current_session].something1 = read_binary(unsigned short, mds_content, mds_session_offset+0x0008); sessions[mds_current_session].datablocks = read_binary(unsigned char, mds_content, mds_session_offset+0x000a); sessions[mds_current_session].leadinblocks = read_binary(unsigned char, mds_content, mds_session_offset+0x000b); sessions[mds_current_session].session = read_binary(unsigned short, mds_content, mds_session_offset+0x000c); sessions[mds_current_session].last_track = read_binary(unsigned short, mds_content, mds_session_offset+0x000e); sessions[mds_current_session].something2 = read_binary(int, mds_content, mds_session_offset+0x0010); sessions[mds_current_session].datablocks_offset = read_binary(int, mds_content, mds_session_offset+0x0014); mds_extrablocks_offset=sessions[mds_current_session].datablocks_offset + mds_datablock_size*sessions[mds_current_session].datablocks; mds_session_offset = mds_session_offset + 0x18; } int mds_extrablocks_num=0; for(int mds_current_session=0;mds_current_session<mds_sessions;mds_current_session++) { sessions[mds_current_session].extrablocks_offset = mds_extrablocks_offset + mds_extrablock_size * mds_extrablocks_num; mds_extrablocks_num+=sessions[mds_current_session].datablocks; } for(int mds_current_session=0;mds_current_session<mds_sessions;mds_current_session++) { // making sure table is empty sessions[mds_current_session].ntracks=0; // read datablocks and extrablocks for(int datablock=0;datablock<sessions[mds_current_session].datablocks;datablock++) { int datablock_offset = sessions[mds_current_session].datablocks_offset+mds_datablock_size*datablock; int extrablock_offset = sessions[mds_current_session].extrablocks_offset+mds_extrablock_size*datablock; int mode = read_binary(unsigned char, mds_content, datablock_offset+0x0000); int smth1 = read_binary(unsigned char, mds_content, datablock_offset+0x0001); int flags = read_binary(unsigned short, mds_content, datablock_offset+0x0002); int track = read_binary(unsigned char, mds_content, datablock_offset+0x0004); int smth2 = read_binary(int, mds_content, datablock_offset+0x0005); int pmin = read_binary(unsigned char, mds_content, datablock_offset+0x0009); int psec = read_binary(unsigned char, mds_content, datablock_offset+0x000a); int pfrm = read_binary(unsigned char, mds_content, datablock_offset+0x000b); int smth3 = read_binary(int, mds_content, datablock_offset+0x000c); int sectorsize = read_binary(unsigned short, mds_content, datablock_offset+0x0010); int smth4 = read_binary(int, mds_content, datablock_offset+0x0014); int smth5 = read_binary(int, mds_content, datablock_offset+0x0018); int smth6 = read_binary(int, mds_content, datablock_offset+0x001c); int smth7 = read_binary(int, mds_content, datablock_offset+0x0020); int sector = read_binary(int, mds_content, datablock_offset+0x0024); __int64 offset = read_binary(__int64, mds_content, datablock_offset+0x0028); int pregap = read_binary(int, mds_content, extrablock_offset+0x0000); int sectors = read_binary(int, mds_content, extrablock_offset+0x0004); if(verbose) printf("datablock: %3d, track: %2x, mode: %2x, flags: %x, sector size: %d, MSF: %02d:%02d.%02d, sector: %d, offset: %d, pregap: %d, sectors: %d\n" , datablock, track, mode, flags, sectorsize, pmin, psec, pfrm, sector, (int)offset, pregap, sectors); // writing data to entries if (track < 0xa0) { strack *t=&(sessions[mds_current_session].tracks[sessions[mds_current_session].ntracks]); t->track =track; t->mode =mds_mode(mode); t->flags =flags; t->pmin = pmin; t->psec = psec; t->pfrm = pfrm; t->sectorsize=sectorsize; t->pregap =pregap; t->sector =sector; t->sectors =sectors; t->offset =offset; sessions[mds_current_session].ntracks++; } } } free(mds_content); fclose(mds_file); return true; }
bool parse_nrg(wchar*nrg_filename,bool verbose) { /*"returns the supposable bin_filename and raw entries, if something went \ wrong it throws an exception"*/ // read the file FILE*nrg_file = _tfopen (nrg_filename,L"rb"); if(!nrg_file) { fwprintf(stderr,L"Could not open the nrg-file <%s>\n",nrg_filename); return false; } // last 8 or 12 bytes gives us important information, like the signature // and where to find the rest of the image information. int nrg_filesize = flen(nrg_file); int nrg_signature; __int64 nrg_trailer_offset=0; int oldformat; fseek(nrg_file,-12,SEEK_END); fread(&nrg_signature,4,1,nrg_file); #define NER5 0x3552454E #define NERO 0x3552454E if(nrg_signature == NER5) // Nero 5.5.x or newer { if(verbose) printf("File format: NER5.\n"); fread(&nrg_trailer_offset,8,1,nrg_file); byteswap(&nrg_trailer_offset,8); oldformat = 0; // Offsets are 64 bit. } else { fseek(nrg_file,-8,SEEK_END); fread(&nrg_signature,4,1,nrg_file); if(nrg_signature == NERO) // Nero older than 5.5.x { if(verbose) printf("File format: NERO.\n"); fread(&nrg_trailer_offset,4,1,nrg_file); byteswap(&nrg_trailer_offset,4); oldformat = 1; // Offsets are 32 bit. } else { fwprintf(stderr,L"Invalid data in <%s>. It is not an NRG file.",nrg_filename); return false; } } fseek(nrg_file,nrg_trailer_offset, SEEK_SET); char *nrg_content = (char*)malloc(nrg_filesize - nrg_trailer_offset); if(!nrg_content) { fwprintf(stderr,L"Could not allocate a buffer to read the nrg info from <%s>\n",nrg_filename); fclose(nrg_file); return false; } fread(nrg_content,(nrg_filesize - nrg_trailer_offset),1,nrg_file); nsessions=1; // find relative offsets for different blocks __int64 offs = 0; // CUEX is 64-bit variant of CUES, same goes for DAOX vs. DAOI and ETN2 vs. ETNF. // DAO is DiscAtOnce, ETN is TrackAtOnce. SINF is Session Information, and MTYP is Media Type. // CDTX is CD-Text. END! is a marker for the last block. char block_ids[10][5] = {"CUEX", "CUES", "DAOX", "DAOI", "CDTX", "ETNF", "ETN2", "SINF", "MTYP", "END!"}; enum block_nums {CUEX, CUES, DAOX, DAOI, CDTX, ETNF, ETN2, SINF, MTYP, END}; struct _block { struct info { __int64 offset; __int64 size; } infos[255]; int found; } block[10]; for(int block_num=0;block_num<10;block_num++) block[block_num].found=0; while(offs < (nrg_filesize - nrg_trailer_offset - 12)) { bool found=false; for(int block_num=0;block_num<10;block_num++) { char *block_id = block_ids[block_num]; if(memcmp(block_id,nrg_content+offs,4)==0) { int size = read_binary(unsigned int, nrg_content, offs+4); byteswap(&size,4); block[block_num].infos[block[block_num].found].offset=offs; block[block_num].infos[block[block_num].found].size=size; if(verbose) printf("%s [0x%x:0x%x]\n",block_id, offs, size); offs += size+8; block[block_num].found++; found=true; break; } } if(!found) { fprintf(stderr,"Error in parsing Nero CD-Image.\n"); free(nrg_content); fclose(nrg_file); return false; } }