Public feed
#379192
#include <stdio.h>#include <windows.h>#include "imgfs.h"// listimgfs.cpp// -----------------------// author: dutty// based on code by mamaich// // mamaich, brother, you really need to comment your code ;)void fprint( HANDLE HOUT, char *string ){ DWORD byteswritten; WriteFile( HOUT, string, (DWORD)strlen(string), &byteswritten, 0 );}void main(int argc, char **argv){ /* wrong number of arguments results in usage output */ if( argc < 2 || argc > 3 ) { printf("listimgfs creates a list of all files from a raw imagefs file.\n"); printf("Usage: listimgfs (-screen) <imagefile.bin>\n\n"); printf("\t-screen | output to screen quiet all other output\n"); printf("\n"); printf("Example:\n\tlistimgfs imgfs_raw_data.bin\n"); return; } char filename[MAX_PATH]; bool outputscreen = false; if (argc==3) { strncpy_s( filename, sizeof(filename), argv[2], MAX_PATH ); outputscreen = true; } else strncpy_s( filename, sizeof(filename), argv[1], MAX_PATH ); /* create handle to the image file */ HANDLE HF = CreateFile(argv[1],GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ,0,OPEN_EXISTING,0,0); if ( !HF ) { printf("...Error: Couldn't create file handle %s", filename); return; } HANDLE HOUT; if ( outputscreen == false ) { char *out_filename = "listimgfs.txt"; HOUT = CreateFile( out_filename, GENERIC_WRITE, FILE_SHARE_WRITE, 0,CREATE_ALWAYS, 0, 0 ); if ( !HOUT ) { printf("...Error: Couldn't create output file %s", out_filename ); return; } } /* get file size */ DWORD Size = GetFileSize( HF, 0 ); /* create file mapping object */ HANDLE HM = CreateFileMapping(HF,0,PAGE_READWRITE,0,0,0); if ( !HM ) { printf("...Error: Couldnt create file mapping object\n"); return; } /* map view of file into memory */ byte *Base = (byte*)MapViewOfFile(HM,FILE_MAP_WRITE,0,0,0); if(!Base) { printf("...Error: Cannot map file %s\n",argv[1]); return; } /* the header of the image, see imgfs.h */ FS_BOOT_SECTOR *Boot = (FS_BOOT_SECTOR*)Base; if ( outputscreen == false ) { printf("...Imagefile load ok!\n"); printf("...Header information:\n"); printf("......guidBootSignature: "); for(int i=0; i<16; i++) printf("%X ",255&((byte*)&Boot->guidBootSignature)[i]); printf("\n......dwFSVersion: %08X\n",Boot->dwFSVersion); printf("......dwSectorsPerHeaderBlock: %08X\n",Boot->dwSectorsPerHeaderBlock); printf("......dwRunsPerFileHeader: %08X\n",Boot->dwRunsPerFileHeader); printf("......dwBytesPerHeader: %08X\n",Boot->dwBytesPerHeader); printf("......dwChunksPerSector: %08X\n",Boot->dwChunksPerSector); printf("......dwFirstHeaderBlockOffset: %08X\n",Boot->dwFirstHeaderBlockOffset); printf("......dwDataBlockSize: %08X\n",Boot->dwDataBlockSize); printf("......szCompressionType: %4s\n",Boot->szCompressionType); printf("......dwFreeSectorCount: %08X\n",Boot->dwFreeSectorCount); printf("......dwHiddenSectorCount: %08X\n",Boot->dwHiddenSectorCount); printf("......dwUpdateModeFlag: %08X\n",Boot->dwUpdateModeFlag); } int numfiles = 0; FS_BLOCK_HEADER *Block = (FS_BLOCK_HEADER*)(Boot->dwFirstHeaderBlockOffset+Base); do { for(unsigned int i=0; i<Boot->dwFirstHeaderBlockOffset/Boot->dwBytesPerHeader; i++) { FSHEADER *Hdr=(FSHEADER*)((byte*)Block+sizeof(FS_BLOCK_HEADER)+Boot->dwBytesPerHeader*i); /* only look for file headers */ if ( Hdr->dwHeaderFlags != 0xFFFFFEFB ) continue; wchar_t *name = (wchar_t *)&Hdr->hdrName; if ( outputscreen == true ) printf("%S ",name); else { char temp[MAX_PATH]; sprintf_s( temp, MAX_PATH, "%S\r\n", name ); fprint( HOUT, temp ); } numfiles++; } Block = (FS_BLOCK_HEADER*)(Block->dwNextHeaderBlock+Base); } while( (byte*)Block!=Base && (byte*)Block<Base+Size); CloseHandle( HF ); CloseHandle( HOUT ); if ( outputscreen ) return; printf("...%i files\n", numfiles ); printf("...written to: listimgfs.txt\n"); printf("...done!\n" );}