/* ============ = = initFromFile: = ============ */ void initFromFile(void) { wadinfo_t wad; lumpinfo_t *lumps; int i; /* read in the header */ fread(&wad, sizeof(wad), 1, wad_i.handle); if (strncmp(wad.identification, "PWAD", 4)) { Error("initFromFile: specified WAD is not a PWAD"); } /* read in the lumpinfo */ fseek(wad_i.handle, wad.infotableofs, SEEK_SET); wad_i.info = (STORAGE *) SafeMalloc(sizeof(STORAGE)); wad_i.info->data = (lumpinfo_t *) SafeCalloc(wad.numlumps, sizeof(lumpinfo_t)); wad_i.info->count = wad.numlumps; wad_i.info->size = sizeof(lumpinfo_t); lumps = wad_i.info->data; fread(lumps, sizeof(lumpinfo_t), wad.numlumps, wad_i.handle); for (i = 0; i < wad.numlumps; i++, lumps++) { lumps->filepos = bswapl(lumps->filepos); lumps->size = bswapl(lumps->size); } return; }
int main ( void ) { #if defined(__x86_64__) printf("0x%llx\n", bswapq( 0x8877665544332211ULL )); #endif printf("0x%x\n", bswapl( 0x44332211ULL )); return 0; }
/* ================ = = writeDirectory: = char identification[4]; // should be IWAD int numlumps; int infotableofs; ================ */ void writeDirectory(void) { wadinfo_t wad; int i; int count; lumpinfo_t *inf; /* write the directory */ count = wad_i.info->count; inf = wad_i.info->data; for (i = 0; i < count; i++) { inf[i].filepos = bswapl(inf[i].filepos); inf[i].size = bswapl(inf[i].size); } fseek(wad_i.handle, 0, SEEK_END); wad.infotableofs = ftell(wad_i.handle); wad.infotableofs = bswapl(wad.infotableofs); fwrite(inf, sizeof(lumpinfo_t), count, wad_i.handle); for (i = 0; i < count; i++) { inf[i].filepos = bswapl(inf[i].filepos); inf[i].size = bswapl(inf[i].size); } /* write the header */ strncpy(wad.identification, "PWAD", 4); wad.numlumps = bswapl(wad_i.info->count); fseek(wad_i.handle, 0, SEEK_SET); fwrite(&wad, sizeof(wad), 1, wad_i.handle); return; }