int DumpArticle(char *fname, History *h, const char *msgid) { int fd; int rv = 0; int wireFormat = 0; int artSize = 0; int headLen; int oldFormat = 0; int compressedFormat = 0; SpoolArtHdr ah; if ((fd = open(fname, O_RDONLY)) >= 0) { char *base = NULL; const char *ptr; int extra = (h->boffset == 0) ? 0 : 1; errno = 0; if (ShowFileHeader) { rv = DumpFileHeader(fd, h->boffset); close(fd); return(rv); } if (MapArticle(fd, fname, &base, h, &extra, &artSize, &compressedFormat) != 0) return(1); /* * check for prior terminating zero, article body does not * begin with a null, and post terminating zero */ ptr = base; if (rv == 0 && extra) { if (*ptr != 0) { fprintf(LogFo, " missingPreNul"); rv = 1; } ++ptr; } if (rv == 0 && ptr[0] == 0) { fprintf(LogFo, " nullArtBody"); rv = 1; } if (rv == 0 && ptr[artSize] != 0) { fprintf(LogFo, " missingPostNul"); rv = 1; } bcopy(ptr, &ah, sizeof(ah)); if (DebugOpt) { printf("magic1=%x magic2=%x\n", (uint8)ah.Magic1, (uint8)ah.Magic2); } if ((uint8)ah.Magic1 == (uint8)STORE_MAGIC1 && (uint8)ah.Magic2 == (uint8)STORE_MAGIC2) { headLen = (uint32)ah.ArtHdrLen; artSize -= (uint8)ah.HeadLen; ptr += (uint8)ah.HeadLen; wireFormat = 0; if ((uint8)ah.StoreType & STORETYPE_WIRE) wireFormat = 1; } else { headLen = artSize; wireFormat = 0; oldFormat = 1; } /* * Locate Message-ID header and test */ if (rv == 0 && msgid) { const char *l; int haveMsgId = 0; for (l = ptr - 1; l < ptr + headLen; l = strchr(l, '\n')) { if (l == NULL) { rv = 1; break; } ++l; if (strncasecmp(l, "Message-ID:", 11) == 0) { int i; haveMsgId = 1; l += 11; while (*l && *l != '<' && *l != '\n') ++l; for (i = 0; l[i] && l[i] != '>' && l[i] != '\n' && l[i] != '\r'; ++i) { if (msgid[i] != l[i]) rv = 1; } if (msgid[i] != l[i]) rv = 1; } if (!wireFormat && l[0] == '\n') /* end of headers */ break; else if (wireFormat && l[0] == '\r' && l[1] == '\n') break; } if (rv) { fprintf(LogFo, " messageID-MisMatch"); } else if (haveMsgId == 0) { fprintf(LogFo, " missing-MessageID"); rv = 1; } } if (rv == 0) fprintf(LogFo, "%sOK", oldFormat ? "(old spool) " : ""); fprintf(LogFo, "\n"); if (rv == 0 && VerifyOnly == 0) { int i; int lastNl = 1; int lineLen = 0; fflush(LogFo); fflush(stdout); if (HeadOnly) { for (i = 0; i < headLen; ++i) { if (StripCR && wireFormat && i > 0 && ptr[i-1] == '\r' && ptr[i] == '\n') { if (!QuietOpt) { write(1, ptr + i - lineLen, lineLen - 1); write(1, "\n", 1); } if (lineLen == 1) break; lineLen = 0; } else if (wireFormat && i > 0 && ptr[i-1] == '\r' && ptr[i] == '\n') { if (lineLen == 1) break; lastNl = 1; lineLen = 0; } else if (ptr[i] == '\n') { if (lastNl) break; lastNl = 1; } else { lastNl = 0; lineLen++; } } if ((!StripCR || !wireFormat) && !QuietOpt) write(1, ptr, i); } else { if (StripCR && wireFormat) { for (i = 0; i < artSize; ++i) { if (i > 0 && ptr[i-1] == '\r' && ptr[i] == '\n') { if (!QuietOpt) { write(1, ptr + i - lineLen, lineLen - 1); write(1, "\n", 1); } lineLen = 0; } else { lineLen++; } } } else if (!QuietOpt) { write(1, ptr, artSize); } } fflush(stdout); } if (base != NULL) { if (compressedFormat) free(base); else xunmap((void *)base, h->bsize + extra + 1); } if (fd != -1 && !compressedFormat) close(fd); } else { fprintf(LogFo, "Unable to open %s\n", fname); rv = 1; } return(rv); }
int Img2File (char *InputFilePath, char *OutputFilePath) { int Res = 0; int Index = 0; int FwFileSize = 0; FILE *FwFileImg; /* Load .fw file and create memory image. */ printf ("Open %s ... ", InputFilePath) ; FwFileImg = fopen (InputFilePath, "rb") ; if (FwFileImg) { printf ("success!\n") ; /* Get fw file size */ fseek (FwFileImg, 0, SEEK_END) ; FwFileSize = ftell (FwFileImg) ; fseek (FwFileImg, 0, SEEK_SET) ; printf ("File Size : %d Kb\n", FwFileSize / 1024) ; /* Request ram space */ FwFileRam = malloc (FwFileSize) ; if (FwFileRam) { //printf ("*Allocate %d bytes @ %08p\n", FwFileSize, FwFileRam) ; /* Copy image to ram */ Res = fread (FwFileRam, FwFileSize, 1, FwFileImg) ; Res = mkdir (OUTPUT_ROOT_FOLDER, FOLDER_PERMISSION) ; if (Res == -1) { perror ("Make folder fail, reason") ; } /* Dump fw file header information to user */ Res = DumpFileHeader (FwFileRam) ; /* Dump each sector header information to user */ Res = DumpSecterHeader (FwFileRam, FileAmount) ; free (FwFileRam) ; } fclose (FwFileImg) ; /* Find check sum */ FindChecksum (InputFilePath) ; printf ("Try Checksum:%08X\n", Checksum) ; printf ("Try CRC32 :%08X\n", Crc32) ; } else { printf ("fail! (Path of Filename error?)\n") ; } return 0; }
BOOL BeginFileScan(HANDLE hFileView, BOOL *pf64bit) { PIMAGE_DOS_HEADER pDOSHeader = (PIMAGE_DOS_HEADER)hFileView; PIMAGE_NT_HEADERS pNTHeaders = NULL; #ifdef _DEBUG wprintf_s(L"Filebase/DOSHeader: 0x%08x\n", (DWORD)pDOSHeader); #endif // verify "MZ" in the DOS header if (!(pDOSHeader->e_magic == IMAGE_DOS_SIGNATURE)) { wprintf_s(L"Valid DOS stub not found. Aborting...\n"); return FALSE; } pNTHeaders = (PIMAGE_NT_HEADERS)((BYTE*)pDOSHeader + pDOSHeader->e_lfanew); // set pointer to NTHeaders in the global info struct g_binFileInfo.pNTHeaders = pNTHeaders; if (g_fHeaders) { wprintf_s(L"\n* NT Headers *\n"); wprintf_s(L"Valid DOS stub found\n"); // verify "PE00" at offset given by e_lfanew in IMAGE_DOS_HEADER if (!(pNTHeaders->Signature == IMAGE_NT_SIGNATURE)) { wprintf_s(L"Valid PE signature not found. Aborting...\n"); return FALSE; } wprintf_s(L"Valid PE signature found at FilePtr:0x%08x\n", (DWORD)pNTHeaders - (DWORD)hFileView); if (!DumpFileHeader(pNTHeaders, pf64bit)) return FALSE; // optional header: IMAGE_OPTIONAL_HEADER wprintf_s(L"\n* IMAGE_OPTIONAL_HEADER *\n"); if (!(pNTHeaders->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) { wprintf_s(L"Unsupported IMAGE_OPTIONAL_HEADER magic: %d\n", pNTHeaders->OptionalHeader.Magic); return FALSE; } wprintf_s(L"Size of code : 0x%x\n", pNTHeaders->OptionalHeader.SizeOfCode); wprintf_s(L"Size of idata : 0x%x\n", pNTHeaders->OptionalHeader.SizeOfInitializedData); wprintf_s(L"Size of udata : 0x%x\n", pNTHeaders->OptionalHeader.SizeOfUninitializedData); wprintf_s(L"Preferred Image Base : 0x%x\n", pNTHeaders->OptionalHeader.ImageBase); wprintf_s(L"Entry Point : 0x%x\n", pNTHeaders->OptionalHeader.AddressOfEntryPoint); wprintf_s(L"Base of code : 0x%x\n", pNTHeaders->OptionalHeader.BaseOfCode); wprintf_s(L"Base of data : 0x%x\n", pNTHeaders->OptionalHeader.BaseOfData); wprintf_s(L"Size of headers : 0x%08x\n", pNTHeaders->OptionalHeader.SizeOfHeaders); wprintf_s(L"Size of image : 0x%x\n", pNTHeaders->OptionalHeader.SizeOfImage); wprintf_s(L"File alignment : 0x%08x\n", pNTHeaders->OptionalHeader.FileAlignment); wprintf_s(L"Section alignment : 0x%08x\n", pNTHeaders->OptionalHeader.SectionAlignment); if (g_dwInputFileType == DASM_FTYPE_EXE) { wprintf_s(L"Subsystem required : "); switch (pNTHeaders->OptionalHeader.Subsystem) { case IMAGE_SUBSYSTEM_NATIVE: wprintf_s(L"Native. No subsystem required.\n"); break; case IMAGE_SUBSYSTEM_WINDOWS_CUI: wprintf_s(L"Windows CommandLine\n"); break; case IMAGE_SUBSYSTEM_WINDOWS_GUI: wprintf_s(L"Windows GUI\n"); break; default: wprintf_s(L"Unknown\n"); }// switch(subsystem) } DumpDataDirectory(pNTHeaders); }// if(g_fHeaders) if (g_fExports) DumpExports((DWORD)pDOSHeader, pNTHeaders, &(pNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT])); if (g_fImports) Util_DumpIMAGE_IMPORT_DESCRIPTORS(pNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress, pNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size, pNTHeaders, (DWORD)hFileView); return TRUE; }// BeginFileScan()