// ---------------------------------------------------------------------------- template<bool U> CString<U> CString<U>::MidAbs(dword Start, dword EndX) const { ErrIf(Start > EndX); ErrIf(EndX > m_Data.Size() - 1); ErrIf(Start > m_Data.Size() - 1); return CString(m_Data._ptr() + Start, EndX - Start); }
//! This is a simple server to test the ntee program. //! The server simply sends back to the client the number of bytes it //! just recieved from the client. //! int main(int argc, char** argv) { std::string USAGE("Usage: testSrv <server port>\n"); ErrIf( argc < 2 ).info(USAGE); // Convert the port number arg to an int in_port_t port; ErrIfCatch(boost::bad_lexical_cast, port = boost::lexical_cast<in_port_t>( argv[1] )) .info("%s is not a port number!\n",argv[1]); // Put the supplied ip addr into the sockaddr_in struct, we will reuse this // memory later to store client addresses in it as they connect. sockaddr_in addr; memset( &addr, 0, sizeof(addr) ); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); // Wildcard address kernel picks best bind addr.sin_port = htons(port); //** Get the server to accept stage Socket sock; SysErrIf( (sock=socket(AF_INET, SOCK_STREAM, 0)) == -1 ); SysErrIf( bind(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1 ); SysErrIf( listen(sock, 5) == -1 ); //** Forever accept new connections and reply for( ;; ) { Socket cliSock; memset( &addr, 0, sizeof(addr) ); socklen_t len = sizeof(addr); SysErrIf( (cliSock=accept(sock,reinterpret_cast<sockaddr*>(&addr),&len)) == -1 ); char cliName[INET_ADDRSTRLEN]; WarnIf( inet_ntop(AF_INET, &addr, cliName, sizeof(cliName) ) == 0 ); std::cout << "CONNECTED to: " << ((cliName)?cliName:"Unknown") << ":" << addr.sin_port << "\n"; handleClient( cliSock ); } return 0; }
// ---------------------------------------------------------------------------- void Entry() { // Move cursor off-screen _outpw(0x3D4, 0x070E); _outpw(0x3D4, 0xD00F); CKernelHeader* KH = (CKernelHeader*)CMemMap::c_KernelImageBase; dword* BootInfo = (dword*)(*(dword*)(CMemMap::c_KernelImageBase + 0x100)); RawOutString("Checking Kernel...", 0, 0, 0xA); if (IsKernelOK(*KH, BootInfo)) { RawOutString("OK", 18, 0, 0xA); } else { RawOutString("Fail", 18, 0, 0xC); ErrIf(true); } dword BootType = BootInfo[1]; dword DriversCount = BootInfo[2] - 1; CDriverInfo DriverInfos[12]; for (dword i = 0; i < DriversCount; i++) { DriverInfos[i].m_BytesSize = BootInfo[3 + (i + 1) * 3]; DriverInfos[i].m_LoadPage = BootInfo[4 + (i + 1) * 3]; char* Name = PC(BootInfo[5 + (i + 1) * 3]); for (int j = 0;; j++) { DriverInfos[i].m_Name[j] = Name[j]; if (Name[j] == 0) break; } } InitPIT(); CPhysMemManager PMM; PMM.AllocBlockAt(KH->GetKernelCodeBase(), KH->m_KernelCodePageCount, false); PMM.AllocBlockAt(KH->GetKernelRDataBase(), KH->m_KernelRDataPageCount, false); PMM.AllocBlockAt(KH->GetKernelDataBase(), KH->m_KernelDataPageCount, true); PMM.AllocBlockAt((byte*)CMemMap::c_VideoRamTextBase, 8, true); __writecr3(CMemMap::c_PmmPageDirectory); SetupCR0(); for (dword i = 0; i < DriversCount; i++) { PMM.AllocBlockAt( DriverInfos[i].GetImageBase(), DriverInfos[i].GetImagePageCount(), false); } CGDT GDT(PMM); GDT.CreateNewDescriptor(0, 0xFFFFF, 0x92, 1); GDT.CreateNewDescriptor(0, 0xFFFFF, 0x98, 1); GDT.CreateNewDescriptor(0, 0xFFFFF, 0xF2, 1); GDT.CreateNewDescriptor(0, 0xFFFFF, 0xF8, 1); static const dword HeapPageCount = 32; void* HeapBlock = PMM.AllocBlock(HeapPageCount); CHeap SysHeap(PB(HeapBlock), HeapPageCount * 4096); g_SysHeap = &SysHeap; CTask KernelTask(GDT, true, 0, 0, 0, 0, CMemMap::c_PmmPageDirectory); KernelTask._setActive(); CIntManager IM(PMM, KernelTask.GetTSS().GetSelector()); CKernel K(KernelTask, PMM, IM, GDT, IM.GetIDT(), BootType, DriverInfos, DriversCount); }
// ---------------------------------------------------------------------------- template<bool U> CString<U> CString<U>::MidRel(dword Start, dword Count) const { ErrIf(Start + Count > m_Data.Size() - 1); return CString(m_Data._ptr() + Start, Count); }
// ---------------------------------------------------------------------------- template<bool U> CString<U> CString<U>::RightAbs(dword Start) const { ErrIf(Start > m_Data.Size() - 1); return CString(m_Data._ptr() + Start); }
// ---------------------------------------------------------------------------- template<bool U> void CString<U>::SetCh(dword Index, const char_t& Char) { ErrIf(Char == '\0'); ErrIf(Index >= m_Data.Size() - 1); m_Data[Index] = Char; }
// ---------------------------------------------------------------------------- template<bool U> CString<U> CString<U>::Left(dword Count) const { ErrIf(Count > m_Data.Size() - 1); return CString(m_Data._ptr(), Count); }
// ---------------------------------------------------------------------------- template<bool U> typename _chartype<U>::char_t CString<U>::GetCh(dword Index) const { ErrIf(Index >= m_Data.Size()); return m_Data[Index]; }
// ---------------------------------------------------------------------------- template<bool U> dword CString<U>::Len() const { ErrIf(m_Data.Size() == 0); return m_Data.Size() - 1; }
// ---------------------------------------------------------------------------- template<bool U> CString<U>::CString(const char_t* Src, dword SrcLen) : m_Data(Src, SrcLen) { ErrIf(SrcLen > strlen(Src)); m_Data.PushBack('\0'); }
// ---------------------------------------------------------------------------- template<bool U> CString<U> CString<U>::RightRel(dword Count) const { ErrIf(Count > m_Data.Size() - 1); return CString(m_Data._ptr() + m_Data.Size() - 1 - Count); }
int ReadFromExeFile(BYTE *filename) { /* Reads data attached to the exe file and calls ProcessData(pointertodata, datasize). Return values: * ERR_READFAILED - read from exe file had failed; * ERR_BADFORMAT - invalid format of the exe file; * ERR_NOINFO - no info was attached. If the data were read OK, it returns the return value of ProcessData. */ printf("file= %s\n", filename); #define ErrIf(a) if(a) goto HANDLE_BADFORMAT; BYTE buff[4096]; DWORD read; BYTE* data; // Open exe file //GetModuleFileNameA(NULL, (CHAR*)buff, sizeof(buff)); HANDLE hFile = CreateFileA((CHAR*)filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(INVALID_HANDLE_VALUE == hFile) return ERR_READFAILED; if(!ReadFile(hFile, buff, sizeof(buff), &read, NULL)) goto HANDLE_READFAILED; IMAGE_DOS_HEADER* dosheader = (IMAGE_DOS_HEADER*)buff; ErrIf(dosheader->e_magic != IMAGE_DOS_SIGNATURE); ErrIf(ULONG(dosheader->e_lfanew) >= ULONG(sizeof(buff) - sizeof(IMAGE_NT_HEADERS32))); // Locate PE header IMAGE_NT_HEADERS32* header = (IMAGE_NT_HEADERS32*)(buff + dosheader->e_lfanew); ErrIf(header->Signature != IMAGE_NT_SIGNATURE); IMAGE_SECTION_HEADER* sectiontable = (IMAGE_SECTION_HEADER*)((BYTE*)header + sizeof(IMAGE_NT_HEADERS32)); ErrIf((BYTE*)sectiontable >= buff + sizeof(buff)); DWORD maxpointer = 0, exesize = 0; // For each section for(int i = 0; i < header->FileHeader.NumberOfSections; ++i) { if(sectiontable->PointerToRawData > maxpointer) { maxpointer = sectiontable->PointerToRawData; exesize = sectiontable->PointerToRawData + sectiontable->SizeOfRawData; } sectiontable++; } // Seek to the overlay DWORD filesize = GetFileSize(hFile, NULL); if(exesize == filesize) goto HANDLE_NOINFO; ErrIf(filesize == INVALID_FILE_SIZE || exesize > filesize); if(SetFilePointer(hFile, exesize, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) goto HANDLE_READFAILED; data = (BYTE*)malloc(filesize - exesize + 8); if(!ReadFile(hFile, data, filesize - exesize, &read, NULL)) goto HANDLE_WITHFREE; CloseHandle(hFile); // Process the data int result = ProcessData(data, filesize - exesize); free(data); return result; HANDLE_WITHFREE: free(data); HANDLE_READFAILED: CloseHandle(hFile); return ERR_READFAILED; HANDLE_BADFORMAT: CloseHandle(hFile); return ERR_BADFORMAT; HANDLE_NOINFO: CloseHandle(hFile); return ERR_NOINFO; #undef ErrIf }