int check_burning_wind(int** forest, int row_index, int col_index, int neighbourhood_type, int wind_speed, int wind_direction, long double pImmune,int rows, int cols) { int i=row_index; int j=col_index; int neighbour_status = -5; switch(wind_speed) { case 0: return TREE; break; case 2: switch(wind_direction) { case SOUTH: neighbour_status = forest[Nr(Nr(i))%rows][Nc(Nc(j))%cols]; break; case NORTH: neighbour_status = forest[Sr(Sr(i))%rows][Sc(Sc(j))%cols]; break; case EAST: neighbour_status = forest[Wr(Wr(i))%rows][Wc(Wc(j))%cols]; break; case WEST: neighbour_status = forest[Er(Er(i))%rows][Ec(Ec(j))%cols]; break; } if (pImmune<U && neighbour_status == BURNING) return BURNING; case 1: neighbour_status = -5; switch(wind_direction) { case SOUTH: neighbour_status = forest[Nr(i)][Nc(j)]; break; case NORTH: neighbour_status = forest[Sr(i)][Sc(j)]; break; case EAST: neighbour_status = forest[Wr(i)][Wc(j)]; break; case WEST: neighbour_status = forest[Er(i)][Ec(j)]; break; } if (pImmune<U && neighbour_status == BURNING) return BURNING; else return TREE; } }
struct Node_t * E(void) { struct Node_t * left_node; left_node = T(); return Ec(left_node); }
int do_neighbours_burn(int** forest,int row_index,int col_index) { return (forest[Nr(row_index)][Nc(col_index)]==BURNING || forest[Er(row_index)][Ec(col_index)]==BURNING || forest[Wr(row_index)][Wc(col_index)]==BURNING || forest[Sr(row_index)][Sc(col_index)]==BURNING ); }
struct Node_t * Ec(struct Node_t * left) { struct Node_t * node; if ((last == '+') || (last == '-')) { node = free_node++; node->oper = last; last = GetNextChar(); node->left = left; node->right = F(); return Ec(node); } node = Fc(left); if ((last == '+') || (last == '-')) return Ec(node); return node; }
/* This counts the number of burning neighbours */ int count_burning_neighbours(int** forest, int row_index, int col_index, int neighbourhood_type){ int neighbors_on_fire=0; int i=row_index; int j=col_index; switch(neighbourhood_type){ case VON_NEUMANN: if(forest[Nr(i)][Nc(j)]>=BURNING && forest[Nr(i)][Nc(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[Er(i)][Ec(j)]>=BURNING && forest[Er(i)][Ec(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[Wr(i)][Wc(j)]>=BURNING && forest[Wr(i)][Wc(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[Sr(i)][Sc(j)]>=BURNING && forest[Sr(i)][Sc(j)]<=OLD_BURNING) neighbors_on_fire++; break; case MOORE: if(forest[Nr(i)][Nc(j)]>=BURNING && forest[Nr(i)][Nc(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[Er(i)][Ec(j)]>=BURNING && forest[Er(i)][Ec(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[Wr(i)][Wc(j)]>=BURNING && forest[Wr(i)][Wc(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[Sr(i)][Sc(j)]>=BURNING && forest[Sr(i)][Sc(j)]<=OLD_BURNING) neighbors_on_fire++; /* Diagonals */ if(forest[NEr(i)][NEc(j)]>=BURNING && forest[NEr(i)][NEc(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[SEr(i)][SEc(j)]>=BURNING && forest[SEr(i)][SEc(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[NWr(i)][NWc(j)]>=BURNING && forest[NWr(i)][NWc(j)]<=OLD_BURNING) neighbors_on_fire++; if(forest[SWr(i)][SWc(j)]>=BURNING && forest[SWr(i)][SWc(j)]<=OLD_BURNING) neighbors_on_fire++; break; default: break; } return neighbors_on_fire; }
/* This returns 1 if any neighbours burn, 0 otherwise */ int do_neighbours_burn(int** forest, int row_index, int col_index, int neighbourhood_type){ int i=row_index; int j=col_index; switch(neighbourhood_type){ case VON_NEUMANN: return ((forest[Nr(i)][Nc(j)]>=BURNING && forest[Nr(i)][Nc(j)]<=OLD_BURNING) || (forest[Er(i)][Ec(j)]>=BURNING && forest[Er(i)][Ec(j)]<=OLD_BURNING) || (forest[Wr(i)][Wc(j)]>=BURNING && forest[Wr(i)][Wc(j)]<=OLD_BURNING) || (forest[Sr(i)][Sc(j)]>=BURNING && forest[Sr(i)][Sc(j)]<=OLD_BURNING) ); case MOORE: return ((forest[Nr(i)][Nc(j)]>=BURNING && forest[Nr(i)][Nc(j)]<=OLD_BURNING) || (forest[Er(i)][Ec(j)]>=BURNING && forest[Er(i)][Ec(j)]<=OLD_BURNING) || (forest[Wr(i)][Wc(j)]>=BURNING && forest[Wr(i)][Wc(j)]<=OLD_BURNING) || (forest[Sr(i)][Sc(j)]>=BURNING && forest[Sr(i)][Sc(j)]<=OLD_BURNING) || //Diagonals (forest[NEr(i)][NEc(j)]>=BURNING && forest[NEr(i)][NEc(j)]<=OLD_BURNING) || (forest[SEr(i)][SEc(j)]>=BURNING && forest[SEr(i)][SEc(j)]<=OLD_BURNING) || (forest[NWr(i)][NWc(j)]>=BURNING && forest[NWr(i)][NWc(j)]<=OLD_BURNING) || (forest[SWr(i)][SWc(j)]>=BURNING && forest[SWr(i)][SWc(j)]<=OLD_BURNING) ); default: return 0; } }
bool LOCATOR::FCrackExeOrDbg(const wchar_t *wszFilename, bool fCheckNgenImage) { FILE *fd = NULL; m_pfnReadCodeViewDebugData = (PfnPDBReadCodeViewDebugData) QueryCallback(povcReadCodeViewDebugData); if (m_pfnReadCodeViewDebugData != 0) { // We'll read the actual codeview data // from the client in FLocatePdb(). m_fCvInExe = true; return true; } m_pfnReadMiscDebugData = (PfnPDBReadMiscDebugData) QueryCallback(povcReadMiscDebugData); if (m_pfnReadMiscDebugData != 0) { if (FReadMiscDebugData()) { // UNDONE : This is really true only if header has // IMAGE_FILE_DEBUG_STRIPPED bit set. But // in this case, there is no way to verify! m_fStripped = true; return FSaveFileNames(wszFilename); } } m_pfnReadExecutableAt = (PfnPDBReadExecutableAt) QueryCallback(povcReadExecutableAt); if (m_pfnReadExecutableAt == 0) { m_pfnReadExecutableAtRVA = (PfnPDBReadExecutableAtRVA) QueryCallback(povcReadExecutableAtRVA); if (m_pfnReadExecutableAtRVA == 0) { fd = PDB_wfsopen(wszFilename, L"rb", SH_DENYWR); if (fd == NULL) { SetError(EC_NOT_FOUND, wszFilename); return false; } } } IMAGE_DOS_HEADER DosHeader; if (!FReadHeader(fd, 0, sizeof(DosHeader), &DosHeader)) { InvalidExecutable: SetError(EC_INVALID_EXECUTABLE, wszFilename); Close(fd); return false; } if (fd != NULL) { // We only check for a DBG file when accessing a file locally if (DosHeader.e_magic == IMAGE_SEPARATE_DEBUG_SIGNATURE) { // The file is a DBG file Close(fd); // Set m_dwTimeStampExe so that this DBG is recognized as valid m_dwTimeStampExe = ((IMAGE_SEPARATE_DEBUG_HEADER *) &DosHeader)->TimeDateStamp; bool f = FLocateDbgValidate(wszFilename); if (!f && (Ec() == EC_FORMAT)) { // If this is an invalid format DBG then we guessed goto InvalidExecutable; } return f; } } if (DosHeader.e_magic != IMAGE_DOS_SIGNATURE) { // The file isn't an MS-DOS executable goto InvalidExecutable; } if (!FSaveFileNames(wszFilename)) { Close(fd); return false; } if (DosHeader.e_lfanew <= 0) { // There is no pointer to a PE header goto InvalidExecutable; } // Read the PE header. IMAGE_NT_HEADERS64 NtHeader; if (!FReadHeader(fd, (DWORD) DosHeader.e_lfanew, sizeof(NtHeader), &NtHeader)) { // The file isn't large enough to contain a PE header goto InvalidExecutable; } if (NtHeader.Signature != IMAGE_NT_SIGNATURE) { // The file isn't a PE executable goto InvalidExecutable; } DWORD rva = 0, cb = 0; DWORD rvaComDescriptor = 0, cbComDescriptor = 0; switch (NtHeader.OptionalHeader.Magic) { case IMAGE_NT_OPTIONAL_HDR32_MAGIC : { const IMAGE_OPTIONAL_HEADER32 *pImgOptHdr32 = (IMAGE_OPTIONAL_HEADER32 *) &NtHeader.OptionalHeader; m_dwSizeOfImage = pImgOptHdr32->SizeOfImage; if (pImgOptHdr32->NumberOfRvaAndSizes >= IMAGE_DIRECTORY_ENTRY_DEBUG) { rva = pImgOptHdr32->DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress; cb = pImgOptHdr32->DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size; } if (fCheckNgenImage && (pImgOptHdr32->NumberOfRvaAndSizes >= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR)) { rvaComDescriptor = pImgOptHdr32->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress; cbComDescriptor = pImgOptHdr32->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size; } break; } case IMAGE_NT_OPTIONAL_HDR64_MAGIC : { const IMAGE_OPTIONAL_HEADER64 *pImgOptHdr64 = &NtHeader.OptionalHeader; m_dwSizeOfImage = pImgOptHdr64->SizeOfImage; if (pImgOptHdr64->NumberOfRvaAndSizes >= IMAGE_DIRECTORY_ENTRY_DEBUG) { rva = pImgOptHdr64->DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress; cb = pImgOptHdr64->DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size; } if (fCheckNgenImage && (pImgOptHdr64->NumberOfRvaAndSizes >= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR)) { rvaComDescriptor = pImgOptHdr64->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress; cbComDescriptor = pImgOptHdr64->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size; } break; } default : // We don't recognize this format optional header goto InvalidExecutable; } if ((rva == 0) || (cb == 0)) { // There are no debug directories in the executable. SetError(EC_NO_DEBUG_INFO, wszFilename); Close(fd); return false; } if (fCheckNgenImage && ((rvaComDescriptor == 0) || (cbComDescriptor == 0))) { // Valid ngen images should have COM descriptor directories. goto InvalidExecutable; } DWORD fo; DWORD foComDescriptor = 0; if ((m_pfnReadExecutableAt == 0) && (m_pfnReadExecutableAtRVA != 0)) { // We can avoid mapping RVA to file offset if we are using // RVAs to read from the source file fo = 0; } else { fo = (DWORD) DosHeader.e_lfanew + offsetof(IMAGE_NT_HEADERS32, OptionalHeader) + NtHeader.FileHeader.SizeOfOptionalHeader; size_t csec = (size_t) NtHeader.FileHeader.NumberOfSections; IMAGE_SECTION_HEADER SecHeader; DWORD ptrToRawDataDbgDir = 0, vaDbgDir = 0; DWORD ptrToRawDataComDescriptor = 0, vaComDescriptor = 0; size_t isec = 0; for (isec = 0; isec < csec; isec++) { if (!FReadHeader(fd, fo, sizeof(IMAGE_SECTION_HEADER), &SecHeader)) { break; } fo += sizeof(IMAGE_SECTION_HEADER); if ((SecHeader.VirtualAddress <= rva) && ((SecHeader.VirtualAddress + SecHeader.SizeOfRawData) >= (rva + cb))) { ptrToRawDataDbgDir = SecHeader.PointerToRawData; vaDbgDir = SecHeader.VirtualAddress; } if (fCheckNgenImage && (SecHeader.VirtualAddress <= rvaComDescriptor) && ((SecHeader.VirtualAddress + SecHeader.SizeOfRawData) >= (rvaComDescriptor + cbComDescriptor))) { ptrToRawDataComDescriptor = SecHeader.PointerToRawData; vaComDescriptor = SecHeader.VirtualAddress; } if ((ptrToRawDataDbgDir != 0) && (!fCheckNgenImage || ptrToRawDataComDescriptor != 0)) { break; } } if (isec == csec) { // Can't find debug directories, or // Can't find COM descriptor directories for ngen image goto InvalidExecutable; } fo = ptrToRawDataDbgDir + (rva - vaDbgDir); if (fCheckNgenImage) { foComDescriptor = ptrToRawDataComDescriptor + (rvaComDescriptor - vaComDescriptor); } } if (fCheckNgenImage) { IMAGE_COR20_HEADER corHdr; if (!FReadAt(fd, foComDescriptor, rvaComDescriptor, sizeof(IMAGE_COR20_HEADER), &corHdr)) { // Can't read the COR20 header goto InvalidExecutable; } if ((corHdr.ManagedNativeHeader.VirtualAddress == 0) || (corHdr.ManagedNativeHeader.Size == 0)) { // Invalid data directory for ManagedNativeHeader in an ngen image goto InvalidExecutable; } } IMAGE_DEBUG_DIRECTORY dbgdirCv = {0}; IMAGE_DEBUG_DIRECTORY dbgdirMisc = {0}; dbgdirCv.Type = IMAGE_DEBUG_TYPE_UNKNOWN; dbgdirMisc.Type = IMAGE_DEBUG_TYPE_UNKNOWN; size_t cdbgdir = cb / sizeof(IMAGE_DEBUG_DIRECTORY); wchar_t wszNgenFName[_MAX_FNAME]; if (fCheckNgenImage) { _wsplitpath_s(wszFilename, NULL, 0, NULL, 0, wszNgenFName, _countof(wszNgenFName), NULL, 0); } for (size_t idbgdir = 0; idbgdir < cdbgdir; idbgdir++) { IMAGE_DEBUG_DIRECTORY dbgdir; if (!FReadAt(fd, fo, rva, sizeof(IMAGE_DEBUG_DIRECTORY), &dbgdir)) { // Can't read debug directories goto InvalidExecutable; } rva += sizeof(IMAGE_DEBUG_DIRECTORY); if ((m_pfnReadExecutableAt != 0) || (m_pfnReadExecutableAtRVA == 0)) { fo += sizeof(IMAGE_DEBUG_DIRECTORY); } NotifyDebugDir(true, &dbgdir); if (dbgdir.Type == IMAGE_DEBUG_TYPE_CODEVIEW) { if (fCheckNgenImage) { m_fCvInExe = true; m_fdExe = fd; m_cbCv = dbgdir.SizeOfData; m_rvaCv = dbgdir.AddressOfRawData; m_foCv = dbgdir.PointerToRawData; if (FReadDebugDirInfo() && m_fGuid && (m_age != 0) && (m_wszPdb != NULL)) { wchar_t wszPdbFName[_MAX_FNAME]; _wsplitpath_s(m_wszPdb, NULL, 0, NULL, 0, wszPdbFName, _countof(wszPdbFName), NULL, 0); if (_wcsicmp(wszNgenFName, wszPdbFName) == 0) { dbgdirCv = dbgdir; return true; } } } else { dbgdirCv = dbgdir; } } else if (dbgdir.Type == IMAGE_DEBUG_TYPE_MISC) { dbgdirMisc = dbgdir; } } m_fdExe = fd; bool fDebugInfo = false; if (dbgdirCv.Type != IMAGE_DEBUG_TYPE_UNKNOWN) { fDebugInfo = true; m_fCvInExe = true; m_cbCv = dbgdirCv.SizeOfData; m_rvaCv = dbgdirCv.AddressOfRawData; m_foCv = dbgdirCv.PointerToRawData; } else if (fCheckNgenImage) { // When opening PDB for an ngen image, we should have found // (in the loop above) a debug directory entry of type // IMAGE_DEBUG_TYPE_CODEVIEW and the contained PDB's filename // should the same as that of the ngen image. goto InvalidExecutable; } m_fStripped = (NtHeader.FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0; if (m_fStripped && (dbgdirMisc.Type != IMAGE_DEBUG_TYPE_UNKNOWN)) { fDebugInfo = true; m_dwTimeStampExe = NtHeader.FileHeader.TimeDateStamp; m_dwTimeStampDbg = dbgdirMisc.TimeDateStamp; ReadMiscPath(NULL, fd, dbgdirMisc.PointerToRawData, dbgdirMisc.AddressOfRawData, dbgdirMisc.SizeOfData); } if (!fDebugInfo) { SetError(EC_NO_DEBUG_INFO, wszFilename); } return fDebugInfo; }