Beispiel #1
0
static int
p_unlock2(value v, type t, value vl, type tl)
{
   module_item	*m;

   Check_Atom_Or_Nil(v, t);
   Check_String(tl);

   if (!IsModule(v.did))
   {
       Bip_Error(MODULENAME);
   }
   if (!IsLocked(v.did))
   {
       Succeed_;
   }
   if (DidModule(v.did) == HARD_LOCK_MODULE)
   {
       Bip_Error(LOCKED);
   }
   m = ModuleItem(v.did);
   if (!strcmp(m->lock, StringStart(vl)))
   {
       hg_free((generic_ptr) m->lock);
       DidModule(v.did) = UNLOCK_MODULE;
       m->lock = (char *) 0;
       Succeed_;
   }
   else
   {
       Bip_Error(WRONG_UNLOCK_STRING);
   }
}
// --[  Method  ]---------------------------------------------------------------
//
//  - Class     : CSoundSystem
//  - prototype : CSound* LoadSound(CInput* pInput, const std::string& strExt)
//
//  - Purpose   : Loads a sound,  setting it ready to play.
//                If succesfull, it returns a valid handle to operate
//                with, otherwise it returns NULL.
//
// -----------------------------------------------------------------------------
CSound* CSoundSystem::LoadSound(CInput* pInput, const std::string& strExt)
{
    if(m_bActive == false)
    {
        return NULL;
    }

    // Put it into a buffer:

    pInput->SetPosition(0, CInput::SEEKFROM_START);
    char* pBuffer = new char[pInput->GetLength()];

    assert(pBuffer);

    if(pInput->ReadRawData(pInput->GetLength(), 1, pBuffer) == 0)
    {
        CLogger::ErrorWindow("ERROR - CSoundSystem::LoadSound():\npInput->ReadRawData() failed");
        SAFE_DELETE_A(pBuffer);
        return NULL;
    }

    CSound* pSound = NULL;

    if(IsModule(strExt))
    {
        HMUSIC module = BASS_MusicLoad(TRUE, pBuffer, 0, pInput->GetLength(), 0);

        if(!module)
        {
            CLogger::ErrorWindow("ERROR - CSoundSystem::LoadSound():\nBASS_MusicLoad() failed");
            return NULL;
        }

        CSound* pSound = new CSoundModule(module, pBuffer);
        m_vecSounds.push_back(pSound);
        return pSound;
    }
    else if(IsStream(strExt))
    {
        HSTREAM stream = BASS_StreamCreateFile(TRUE, pBuffer, 0, pInput->GetLength(), BASS_MP3_SETPOS);

        if(!stream)
        {
            CLogger::ErrorWindow("ERROR - CSoundSystem::LoadSound():\nBASS_StreamCreateFile() failed");
            return NULL;
        }

        CSound* pSound = new CSoundStream(stream, pBuffer);
        m_vecSounds.push_back(pSound);
        return pSound;
    }

    SAFE_DELETE_A(pBuffer);
    CLogger::ErrorWindow("ERROR - CSoundSystem::LoadSound():\nUnknown format (%s)", strExt.data());
    return NULL;
}
Beispiel #3
0
static int
p_erase_module(value module, type module_tag, value from_mod, type tfrom_mod)
{
	module_item	*pm, *import_pm;
	int		 i;
	didlist		*lib_scan;
	pword		*prop;

	Check_Module(tfrom_mod, from_mod);

	Check_Atom_Or_Nil(module, module_tag);
	if (!IsModule(module.did))
	{
	    Succeed_;
	} else if (IsLocked(module.did)
		&& (from_mod.did != d_.kernel_sepia
			|| !IsModuleTag(from_mod.did, tfrom_mod)))
	{
	    Bip_Error(LOCKED);
	}

	/*
	 * This is a big mess with respect to locking. The erased module's
	 * descriptor is unprotected. It should be first removed as property
	 * and then cleaned up.
	 */

	pm = ModuleItem(module.did);

	/* first, clean the procedures, we can reclaim the space	*/
	erase_module_procs(pm->procedures);

	hg_free_size((generic_ptr) pm->syntax, sizeof(syntax_desc));

	/* reclaim the properties					*/

	erase_module_props(pm->properties);

	/* reclaim module descriptor */

	(void) erase_property(module.did, MODULE_PROP);

	DidPtr(module.did)->module = 0;

	Succeed_;
}
Beispiel #4
0
static int
p_is_locked(value v, type t)
{
    Check_Atom_Or_Nil(v, t);

    if (!IsModule(v.did))
    {
        Bip_Error(MODULENAME)
    }
    if (IsLocked(v.did))
    {
        Succeed_;
    }
    else
    {
        Fail_;
    }
}
Beispiel #5
0
void Program::SetSymAddrs () {
    Module* m;
    Base* b = inf;
    
    do {
	if (IsModule(b)) {
	    m = (Module*) b;
	    m->SetSymAddrs();
	} else {
	    m = (Module*) b->inf;	/* b = pointer to library */
	    if (m != nil) {
		do {
		    m->SetSymAddrs();
		    m = (Module*) m->nn;
		} while (m != b->inf);
	    }
	}
	b = b->nn;
    } while (b != inf);
}
Beispiel #6
0
void
pri_statistics(void)
{
    int	idx = 0;
    dident mod;
    int count[6];

    while (next_functor(&idx, &mod))
    {
	if (IsModule(mod))
	{
	    pri *pd;
	    int i;
	    for(i=0;i<6;++i) count[i] = 0;

	    for (pd = ModuleItem(mod)->procedures; pd; pd = pd->next_in_mod)
	    {
		switch(PriScope(pd))
		{
		case QUALI:	++count[0]; break;
		case LOCAL:	++count[1]; break;
		case EXPORT:	++count[2]; break;
		case IMPORT:	++count[3]; break;
		case DEFAULT:	++count[4]; break;
		case IMPEXP:	++count[5]; break;
		default:	p_fprintf(current_err_, "Illegal scope %s\n", PriScope(pd)); break;
		}
	    }
	    p_fprintf(log_output_, "\nModule: %s\n", DidName(mod));
	    p_fprintf(log_output_, " QUALI=%d", count[0]);
	    p_fprintf(log_output_, " LOCAL=%d", count[1]);
	    p_fprintf(log_output_, " EXPORT=%d", count[2]);
	    p_fprintf(log_output_, " IMPORT=%d", count[3]);
	    p_fprintf(log_output_, " DEFAULT=%d", count[4]);
	    p_fprintf(log_output_, " IMPEXP=%d", count[5]);
	    ec_newline(log_output_);
	}
    }
}
Beispiel #7
0
int
ec_create_module(dident module_did)	/* also called from megalog */
{
    pword		*prop;
    module_item		*m;

    /* Not quite right, should be atomic lookup & enter */
    a_mutex_lock(&ModuleLock);

    if (IsModule(module_did))
    {
	a_mutex_unlock(&ModuleLock);
	Bip_Error(MODULE_EXISTS);
    }

    DidModule(module_did) = UNLOCK_MODULE;
    prop = (pword *) get_property(module_did, MODULE_PROP);
    if (!prop)
    {
        prop = (pword *) set_property(module_did, MODULE_PROP);
        /* the module did not exist before, no need to test prop */
	m = (module_item *) hg_alloc(sizeof(module_item));
        prop->tag.kernel = TPTR;
        prop->val.ptr = (pword *) m;
    }
    else
	m = (module_item *) prop->val.ptr;

    m->syntax = copy_syntax_desc(default_syntax);
    m->lock = (char *) 0;
    m->procedures = 0;
    m->properties = 0;
    m->imports = 0;

    a_mutex_unlock(&ModuleLock);
    Succeed_;
}
Beispiel #8
0
	void Host::FindBasicModules(std::string& dir)
	{
		ScopedLock lock(&moduleMutex);

		Poco::DirectoryIterator iter = Poco::DirectoryIterator(dir);
		Poco::DirectoryIterator end;
		while (iter != end)
		{
			Poco::File f = *iter;
			if (!f.isDirectory() && !f.isHidden())
			{
				std::string fpath = iter.path().absolute().toString();
				if (IsModule(fpath))
				{
					this->LoadModule(fpath, this);
				}
				else
				{
					this->AddInvalidModuleFile(fpath);
				}
			}
			iter++;
		}
	}
Beispiel #9
0
void Program::MarkSegment (int segType, boolean status) {
    Module* m;
    Chunk* c;
    Base* b = inf;
    
    b = inf;
    do {
	if (IsModule(b)) {
	    m = (Module*) b;
	    c = m->DetermineChunk(segType);
	    c->NeedToReloc(status);
	} else {
	    m = (Module*) b->inf;	/* b = pointer to library */
	    if (m != nil) {
		do {
		    c = m->DetermineChunk(segType);
		    c->NeedToReloc(status);
		    m = (Module*) m->nn;
		} while (m != b->inf);
	    }
	}
	b = b->nn;
    } while (b != inf);
}
Beispiel #10
0
void Program::SetChunkAddrs () {
    register Module* m;
    register Base* b = inf;

    do {
	if (IsModule(b)) {
	    m = (Module*) b;
	    textAlloc += m->text->SetAddrs(textAlloc);
	    dataAlloc += m->data->SetAddrs(dataAlloc);
	    bssAlloc += m->bss->SetAddrs(bssAlloc);
	} else {
	    m = (Module*) b->inf;	/* b = pointer to library */
	    if (m != nil) {
		do {
		    textAlloc += m->text->SetAddrs(textAlloc);
		    dataAlloc += m->data->SetAddrs(dataAlloc);
		    bssAlloc += m->bss->SetAddrs(bssAlloc);
		    m = (Module*) m->nn;
		} while (m != b->inf);
	    }
	}
	b = b->nn;
    } while (b != inf);
}
Beispiel #11
0
void UpdateMemoryMap(void)
{
	MemMap.clear();
	MemMapCode.clear();
	MemMapData.clear();
	MemMapModule.clear();
	Exports.clear();
	CustomNames.clear();

	if (hProcess == NULL)
		return;
	if (!IsProcHandleValid(hProcess))
	{
		hProcess = NULL;
		return;
	}

	SYSTEM_INFO SysInfo;
	GetSystemInfo(&SysInfo);

	MEMORY_BASIC_INFORMATION MBI;
	ULONG_PTR pMemory = (ULONG_PTR)SysInfo.lpMinimumApplicationAddress;
	while(pMemory < (ULONG_PTR)SysInfo.lpMaximumApplicationAddress)
	{
		if ( VirtualQueryEx(hProcess,(LPCVOID)pMemory,&MBI,sizeof(MBI)) != 0)
		{
			if (MBI.State == MEM_COMMIT /*&& MBI.Type == MEM_PRIVATE*/)
			{
				MemMapInfo Mem;
				Mem.Start = (DWORD_PTR)pMemory;
				Mem.End = (DWORD_PTR)pMemory + MBI.RegionSize - 1;
				MemMap.push_back(Mem);
			}
			pMemory = (ULONG_PTR)MBI.BaseAddress + MBI.RegionSize;
		}
		else
		{
			pMemory += 1024;
		}
	}

	MODULEENTRY32	ModInfo;
	ModInfo.dwSize		= sizeof(MODULEENTRY32);

	HANDLE ModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,GetProcessId(hProcess));
	if ((int)ModuleList != -1)
	{
		BOOL rm = Module32First(ModuleList,&ModInfo);
		while(rm)
		{
			MemMapInfo Mem;
			Mem.Start = (DWORD_PTR)ModInfo.modBaseAddr;
			Mem.End = Mem.Start + ModInfo.modBaseSize;
			Mem.Name = ModInfo.szModule;
			MemMapModule.push_back(Mem);

			rm = Module32Next(ModuleList,&ModInfo);
		}
		CloseHandle(ModuleList);
	}

	AddressName Entry;
	ModuleList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,GetProcessId(hProcess));
	if ((int)ModuleList != -1)
	{
		BOOL rm = Module32First(ModuleList,&ModInfo);
		while(rm)
		{
			IMAGE_DOS_HEADER DH;
			IMAGE_NT_HEADERS pe;

			ReadProcessMemory(hProcess,ModInfo.modBaseAddr,&DH,sizeof(DH),NULL);
			ReadProcessMemory(hProcess,ModInfo.modBaseAddr + DH.e_lfanew,&pe,sizeof(pe),NULL);
			DWORD ssize = (DWORD)pe.FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
			PIMAGE_SECTION_HEADER sh = (PIMAGE_SECTION_HEADER)(new char[ssize]);
			ReadProcessMemory(hProcess,ModInfo.modBaseAddr + DH.e_lfanew + sizeof(IMAGE_NT_HEADERS),sh,ssize,NULL);
			for (int i=0; i < pe.FileHeader.NumberOfSections; i++)
			{
				CString txt;
				MemMapInfo Mem;
				txt.Format("%.8s",sh[i].Name); txt.MakeLower();
				if (txt == ".text" || txt == "code")
				{
					Mem.Start	= (DWORD_PTR)ModInfo.modBaseAddr + sh[i].VirtualAddress;
					Mem.End		= Mem.Start + sh[i].Misc.VirtualSize;
					Mem.Name	= ModInfo.szModule;
					MemMapCode.push_back(Mem);
				}
				if (txt == ".data" || txt == "data" || txt == ".rdata" || txt == ".idata")
				{
					Mem.Start	= (DWORD_PTR)ModInfo.modBaseAddr + sh[i].VirtualAddress;
					Mem.End		= Mem.Start + sh[i].Misc.VirtualSize;
					Mem.Name	= ModInfo.szModule;
					MemMapData.push_back(Mem);
				}
			}
			delete sh;

			rm = Module32Next(ModuleList,&ModInfo);
		}
		CloseHandle(ModuleList);
	}

	for (UINT i=0; i<MemMap.size();i++)
	{
		if (IsModule(MemMap[i].Start)) MemMap[i].Name = GetModuleName(MemMap[i].Start);
	}
}
Beispiel #12
0
bool Directory::IsModuleBasis( const unsigned_16 sub1,
                               const unsigned_16 sub2 )
/*****************************************************/
{
    return ( IsModule(sub1) && IsModule(sub2) );
}
Beispiel #13
0
bool UpdateMemoryMap(void)
{
	MemMap.clear();
	MemMapCode.clear();
	MemMapData.clear();
	MemMapModule.clear();
	Exports.clear();
	CustomNames.clear();

	if (g_hProcess == NULL)
	{
		#ifdef _DEBUG
		PrintOut(_T("[UpdateMemoryMap]: Process handle NULL!"));
		#endif
		return 0;
	}

	if (!IsProcHandleValid(g_hProcess))
	{
		g_hProcess = NULL;
		#ifdef _DEBUG
		PrintOut(_T("[UpdateMemoryMap]: Process handle invalid!"));
		#endif
		return 0;
	}

	SYSTEM_INFO SysInfo;
	GetSystemInfo(&SysInfo);

	MEMORY_BASIC_INFORMATION MemInfo;
	size_t pMemory = (size_t)SysInfo.lpMinimumApplicationAddress;
	while (pMemory < (size_t)SysInfo.lpMaximumApplicationAddress)
	{
		if (VirtualQueryEx(g_hProcess, (LPCVOID)pMemory, &MemInfo, sizeof(MEMORY_BASIC_INFORMATION)) != 0)
		{
			if (MemInfo.State == MEM_COMMIT /*&& MemInfo.Type == MEM_PRIVATE*/)
			{
				MemMapInfo Mem;
				Mem.Start = (size_t)pMemory;
				Mem.End = (size_t)pMemory + MemInfo.RegionSize - 1;
				MemMap.push_back(Mem);
			}
			pMemory = (ULONG_PTR)MemInfo.BaseAddress + MemInfo.RegionSize;
		}
		else
		{
			pMemory += 1024;
		}
	}

	static HMODULE hNtDll = (HMODULE)Utils::GetLocalModuleHandle("ntdll.dll");
	static tNtQueryInformationProcess NtQueryInformationProcess = (tNtQueryInformationProcess)Utils::GetProcAddress(hNtDll, "NtQueryInformationProcess");

	PPROCESS_BASIC_INFORMATION ProcessInfo = NULL;
	PEB Peb;
	PEB_LDR_DATA LdrData;

	// Try to allocate buffer 
	HANDLE hHeap = GetProcessHeap();
	DWORD dwSize = sizeof(PROCESS_BASIC_INFORMATION);
	ProcessInfo = (PPROCESS_BASIC_INFORMATION)HeapAlloc(hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, dwSize);

	ULONG dwSizeNeeded = 0;
	NTSTATUS status = NtQueryInformationProcess(g_hProcess, ProcessBasicInformation, ProcessInfo, dwSize, &dwSizeNeeded);
	if (status >= 0 && dwSize < dwSizeNeeded)
	{
		if (ProcessInfo)
			HeapFree(hHeap, 0, ProcessInfo);

		ProcessInfo = (PPROCESS_BASIC_INFORMATION)HeapAlloc(hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, dwSizeNeeded);
		if (!ProcessInfo)
		{
			#ifdef _DEBUG
			PrintOut(_T("[UpdateMemoryMap]: Couldn't allocate heap buffer!"));
			#endif
			return 0;
		}

		status = NtQueryInformationProcess(g_hProcess, ProcessBasicInformation, ProcessInfo, dwSizeNeeded, &dwSizeNeeded);
	}

	// Did we successfully get basic info on process
	if (NT_SUCCESS(status))
	{	
		// Check for PEB
		if (!ProcessInfo->PebBaseAddress)
		{
			#ifdef _DEBUG
			PrintOut(_T("[UpdateMemoryMap]: PEB is null! Aborting UpdateExports!"));
			#endif
			if (ProcessInfo)
				HeapFree(hHeap, 0, ProcessInfo);
			return 0;
		}

		// Read Process Environment Block (PEB)
		SIZE_T dwBytesRead = 0;
		if ( ReadMemory(  ProcessInfo->PebBaseAddress, &Peb, sizeof(PEB), &dwBytesRead) == 0)
		{
			#ifdef _DEBUG
			PrintOut(_T("[UpdateMemoryMap]: Failed to read PEB! Aborting UpdateExports!"));
			#endif
			if (ProcessInfo)
				HeapFree(hHeap, 0, ProcessInfo);
			return 0;
		}

		// Get Ldr
		dwBytesRead = 0;
		if ( ReadMemory( Peb.Ldr, &LdrData, sizeof(LdrData), &dwBytesRead) == 0)
		{
			#ifdef _DEBUG
			PrintOut(_T("[UpdateMemoryMap]: Failed to read PEB Ldr Data! Aborting UpdateExports!"));
			#endif
			if (ProcessInfo)
				HeapFree(hHeap, 0, ProcessInfo);
			return 0;
		}

		LIST_ENTRY *pLdrListHead = (LIST_ENTRY *)LdrData.InLoadOrderModuleList.Flink;
		LIST_ENTRY *pLdrCurrentNode = LdrData.InLoadOrderModuleList.Flink;
		do
		{
			LDR_DATA_TABLE_ENTRY lstEntry = { 0 };
			dwBytesRead = 0;
			if (! ReadMemory( (void*)pLdrCurrentNode, &lstEntry, sizeof(LDR_DATA_TABLE_ENTRY), &dwBytesRead))
			{
				#ifdef _DEBUG
				PrintOut(_T("[UpdateMemoryMap]: Could not read list entry from LDR list. Error = %s"), Utils::GetLastErrorString().GetString());
				#endif
				if (ProcessInfo)
					HeapFree(hHeap, 0, ProcessInfo);
				return 0;
			}

			pLdrCurrentNode = lstEntry.InLoadOrderLinks.Flink;

			if (lstEntry.DllBase != NULL /*&& lstEntry.SizeOfImage != 0*/)
			{
				unsigned char* ModuleBase = (unsigned char*)lstEntry.DllBase;
				DWORD ModuleSize = lstEntry.SizeOfImage;
				wchar_t wcsFullDllName[MAX_PATH] = { 0 };
				wchar_t* wcsModule = 0;
				if (lstEntry.FullDllName.Length > 0)
				{
					dwBytesRead = 0;
					if ( ReadMemory( (LPVOID)lstEntry.FullDllName.Buffer, &wcsFullDllName, lstEntry.FullDllName.Length, &dwBytesRead))
					{
						wcsModule = wcsrchr(wcsFullDllName, L'\\');
						if (!wcsModule)
							wcsModule = wcsrchr(wcsFullDllName, L'/');
						wcsModule++;

						if (g_AttachedProcessAddress == NULL)
						{
							wchar_t filename[MAX_PATH];
							GetModuleFileNameExW(g_hProcess, NULL, filename, MAX_PATH);
							if (_wcsicmp(filename, wcsFullDllName) == 0)
							{
								g_AttachedProcessAddress = (size_t)ModuleBase;
								g_AttachedProcessSize = ModuleSize;
							}
						}
					} 
				}

				// module info
				MemMapInfo Mem;
				Mem.Start = (size_t)ModuleBase;
				Mem.End = Mem.Start + ModuleSize;
				Mem.Size = ModuleSize;
				Mem.Name = wcsModule;
				Mem.Path = wcsFullDllName;

				//PrintOut(_T("%s: %IX"), Mem.Name.GetBuffer(), Mem.Start);

				MemMapModule.push_back(Mem);

				// module code
				IMAGE_DOS_HEADER DosHdr;
				IMAGE_NT_HEADERS NtHdr;

				ReadMemory( ModuleBase, &DosHdr, sizeof(IMAGE_DOS_HEADER), NULL);
				ReadMemory( ModuleBase + DosHdr.e_lfanew, &NtHdr, sizeof(IMAGE_NT_HEADERS), NULL);
				DWORD sectionsSize = (DWORD)NtHdr.FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
				PIMAGE_SECTION_HEADER sections = (PIMAGE_SECTION_HEADER)malloc(sectionsSize);
				ReadMemory( ModuleBase + DosHdr.e_lfanew + sizeof(IMAGE_NT_HEADERS), sections, sectionsSize, NULL);
				for (int i = 0; i < NtHdr.FileHeader.NumberOfSections; i++)
				{
					CString txt;
					MemMapInfo Mem;
					txt.Format(_T("%.8s"), sections[i].Name); txt.MakeLower();
					if (txt == ".text" || txt == "code")
					{
						Mem.Start = (size_t)ModuleBase + sections[i].VirtualAddress;
						Mem.End = Mem.Start + sections[i].Misc.VirtualSize;
						Mem.Name = wcsModule;
						MemMapCode.push_back(Mem);
					}
					if (txt == ".data" || txt == "data" || txt == ".rdata" || txt == ".idata")
					{
						Mem.Start = (size_t)ModuleBase + sections[i].VirtualAddress;
						Mem.End = Mem.Start + sections[i].Misc.VirtualSize;
						Mem.Name = wcsModule;
						MemMapData.push_back(Mem);
					}
				}
				delete sections;
			}

		} while (pLdrListHead != pLdrCurrentNode);
	}
	else
	{
		#ifdef _DEBUG
		PrintOut(_T("[UpdateExports]: NtQueryInformationProcess failed! Aborting..."));
		#endif
		if (ProcessInfo)
			HeapFree(hHeap, 0, ProcessInfo);
		return 0;
	}

	if (ProcessInfo)
		HeapFree(hHeap, 0, ProcessInfo);

	for (UINT i = 0; i < MemMap.size(); i++)
	{
		if (IsModule(MemMap[i].Start)) 
			MemMap[i].Name = GetModuleName(MemMap[i].Start);
	}

	return 1;
}
Beispiel #14
0
static int
p_authorized_module(value v, type t)
{
    Check_Atom_Or_Nil(v, t);
    Succeed_If(IsModule(v.did) && (!IsLocked(v.did) || IsModuleTag(v.did, t)));
}
Beispiel #15
0
static int
p_is_module(value v, type t)
{
    Check_Atom_Or_Nil(v, t);
    Succeed_If(IsModule(v.did));
}