示例#1
0
	//! Read a complete line from a file and return it as a string, treats CR, LF, CRLF and LFCR as valid line ends
	std::string FileGets(FileHandle File)
	{
		std::string Line;
		if(!FileValid(File)) return Line;

		while(!FileEof(File))
		{
			char c = static_cast<char>(FileGetc(File));
			
			// Enf of line?
			if((c == '\n') || (c == '\r'))
			{
				// If not the last item byte in the file...
				if(!FileEof(File))
				{
					// Check the next byte, and if it is something we should not have read, push it back by seeking back
					UInt64 Pos = FileTell(File);
					char c2 = static_cast<char>(FileGetc(File));

					// We only discard a second character if it is also a \n or \r, but not a duplicate of the first
					if(((c2 != '\n') && (c2 != '\r')) || (c2 == c)) FileSeek(File, Pos);
				}
				break;
			}

			Line += c;
		}

		return Line;
	}
示例#2
0
	static unsigned int FileSeek( HANDLE hFile, unsigned int distance, DWORD MoveMethod )
	{
		if ( fseeko( (FILE *)hFile, distance, MoveMethod ) == 0 )
		{
			return FileTell( hFile );
		}
		return 0;
	}
示例#3
0
/*============================================================================

  Description: See documentation for standard C library ftell

  ==========================================================================*/
long palm_ftell( PALM_FILE* io_pSF )
{
    Int32 fileSizeP = 0;    // unused
    Err err = 0;

    ChASSERT(io_pSF->volRef == ((UInt16)-1));

    return FileTell( io_pSF->file.fh, &fileSizeP, &err );
}
示例#4
0
bool BinaryReader::CheckCapacity(uint64_t numbytes)
{
   //Function to check number of bytes left to be read from file against number of bytes user wants to read
   //returns false if not enough bytes left in file
   uint64_t by=this->filesize - FileTell(this->filein);
   //DEBUGPRINT(by<<" "<<numbytes);
   if (by >= numbytes)
      return true;
   else
      return false;
}
示例#5
0
long fileTell(FILE *stream)
{
    Err     error   = errNone;
    UInt32  pos     = 0;

#ifdef USE_FILE_API
    if (!stream || !stream->fileHand)
        return -1;
        
    pos = FileTell(stream->fileHand, NULL, &error);
#else
    if (!stream || !stream->fileRef)
        return -1;
        
    error = VFSFileTell(stream->fileRef, &pos);
#endif

    return (error)?-1:pos;
}
示例#6
0
文件: drivers.c 项目: kfreezen/kos
void KernelSymbolsLoad() {
	// Load kernel.map
	File* file = GetFileFromPath("/sys/kernel.map");
	
	FileSeek(SEEK_EOF, file);
	int length = FileTell(file);

	FileSeek(0, file);
	
	#ifdef DRIVERS_DEBUG
	kprintf("length=%d,%x\n", length, length);
	#endif

	void* buf = kalloc(length);

	ReadFile(buf, length, file);
	
	Header* hdr = (Header*) buf;
	kernelSyms = buf + hdr->symentries_offset;
	kernelMapStrtab = buf+hdr->strtab_offset;
	
	numKernelSyms = hdr->entries;
	CloseFile(file);
}
	///////////////////////////////////////////////////////////////////////////////
	//                                                                           //
	// Setup                                                                     //
	//                                                                           //
	///////////////////////////////////////////////////////////////////////////////
	aalError StreamWAV::SetStream(FILE * _stream)
	{
		if (!_stream) return AAL_ERROR_FILEIO;

		stream = _stream;

		format = malloc(sizeof(WAVEFORMATEX));

		if (!format) return AAL_ERROR_MEMORY;

		ChunkFile wave(stream);

		// Check for 'RIFF' chunk id and skip file size
		if (wave.Check("RIFF") ||
		        wave.Skip(4) ||
		        wave.Check("WAVE") ||
		        wave.Find("fmt ") ||
		        wave.Read(&AS_FORMAT_PCM(format)->wFormatTag, 2) ||
		        wave.Read(&AS_FORMAT_PCM(format)->nChannels, 2) ||
		        wave.Read(&AS_FORMAT_PCM(format)->nSamplesPerSec, 4) ||
		        wave.Read(&AS_FORMAT_PCM(format)->nAvgBytesPerSec, 4) ||
		        wave.Read(&AS_FORMAT_PCM(format)->nBlockAlign, 2) ||
		        wave.Read(&AS_FORMAT_PCM(format)->wBitsPerSample, 2))
			return AAL_ERROR_FORMAT;

		// Get codec specific infos from header for non-PCM format
		if (AS_FORMAT_PCM(format)->wFormatTag != WAVE_FORMAT_PCM)
		{
			aalVoid * ptr;

			//Load extra bytes from header
			if (wave.Read(&AS_FORMAT_PCM(format)->cbSize, 2)) return AAL_ERROR_FORMAT;

			ptr = realloc(format, sizeof(WAVEFORMATEX) + AS_FORMAT_PCM(format)->cbSize);

			if (!ptr) return AAL_ERROR_MEMORY;

			format = ptr;
			wave.Read((char *)format + sizeof(WAVEFORMATEX), AS_FORMAT_PCM(format)->cbSize);

			// Get sample count from the 'fact' chunk
			wave.Find("fact");
			wave.Read(&outsize, 4);
		}

		// Create codec
		switch (AS_FORMAT_PCM(format)->wFormatTag)
		{
			case WAVE_FORMAT_PCM   :
				codec = new CodecRAW;
				break;
			case WAVE_FORMAT_ADPCM :
				outsize <<= 1;
				codec = new CodecADPCM;
				break;
			default                :
				return AAL_ERROR_FORMAT;
		}

		// Check for 'data' chunk id, get data size and offset
		wave.Restart();
		wave.Skip(12);

		if (wave.Find("data")) return AAL_ERROR_FORMAT;

		size = wave.Size();

		if (AS_FORMAT_PCM(format)->wFormatTag == WAVE_FORMAT_PCM) outsize = size;
		else outsize *= AS_FORMAT_PCM(format)->nChannels;

		offset = FileTell(stream);

		aalError error;
		error = codec->SetStream(stream);

		if (error) return error;

		error = codec->SetHeader(format);

		if (error) return error;

		return AAL_OK;
	}
示例#8
0
文件: cv.cpp 项目: mingpen/OpenNT
void
EmitCvInfo (
    PIMAGE pimage)

/*++

Routine Description:



Arguments:

    None.

Return Value:

    None.

--*/

{
    WORD i;
    DWORD li;
    DWORD lj;
    DWORD nameLen;
    DWORD numSubsections;
    DWORD numLocals = 0;
    DWORD numTypes = 0;
    DWORD numLinenums = 0;
    DWORD libStartSeek;
    DWORD libEndSeek;
    DWORD segTableStartSeek;
    DWORD segTableEndSeek;
    BYTE cbFilename;
    char *szFilename;
    ENM_SEC enm_sec;
    ENM_LIB enm_lib;
    PSEC psec;
    PLIB plib;
    DWORD csstPublicSym;
    DWORD cSstSrcModule=0;

    struct {
        WORD cbDirHeader;
        WORD cbDirEntry;
        DWORD cDir;
        DWORD lfoNextDir;
        DWORD flags;
    } dirHdr;

    struct {
        WORD subsection;
        WORD imod;
        DWORD lfo;
        DWORD cb;
    } subDir;

    struct {
        WORD ovlNumber;
        WORD iLib;
        WORD cSeg;
        WORD style;
    } entry;

    struct {
        WORD seg;
        WORD pad;
        DWORD offset;
        DWORD cbSeg;
    } entrySegArray;

    struct {
        WORD flags;
        WORD iovl;
        WORD igr;
        WORD isgPhy;
        WORD isegName;
        WORD iClassName;
        DWORD segOffset;
        DWORD cbSeg;
    } segTable;


    // Count the number of sstSymbols, sstTypes, sstSrcLnSeg
    // we have gathered from the object files.

    for (li = 0; li < NextCvObject; li++) {
         if (CvInfo[li].Locals.PointerToSubsection) {
             ++numLocals;
         }
         if (CvInfo[li].Types.PointerToSubsection) {
             ++numTypes;
         }
         if (CvInfo[li].Linenumbers.PointerToSubsection) {
             ++numLinenums;
         }
    }


    // Emit the sstModule subsection.

    entry.ovlNumber = 0;
    entry.style = 0x5643;               // "CV"

    for (li = 0; li < NextCvObject; li++) {
        CVSEG *pcvseg = PcvsegMapPmod(CvInfo[li].pmod, &entry.cSeg, pimage);
        WORD icvseg;

        szFilename = CvInfo[li].ObjectFilename;
        cbFilename = (BYTE) strlen(szFilename);
        nameLen = cbFilename;

        i = 0;

        if (FIsLibPMOD(CvInfo[li].pmod)) {
            InitEnmLib(&enm_lib, pimage->libs.plibHead);
            while (FNextEnmLib(&enm_lib)) {
                plib = enm_lib.plib;

                if (plib->szName != NULL) {
                    // Only named libraries are counted

                    i++;

                    if (plib == CvInfo[li].pmod->plibBack) {
                        break;
                    }
                }
            }
            EndEnmLib(&enm_lib);
        }

        entry.iLib = i;

        CvInfo[li].Module.PointerToSubsection = FileTell(FileWriteHandle);
        FileWrite(FileWriteHandle, &entry, sizeof(entry));

        // Generate the section array for this sstModule.

        for (icvseg = 0; icvseg < entry.cSeg; icvseg++) {
            CVSEG *pcvsegNext;

            entrySegArray.seg = PsecPCON(pcvseg->pconFirst)->isec;
            entrySegArray.pad = 0;
            entrySegArray.offset = pcvseg->pconFirst->rva -
                                    PsecPCON(pcvseg->pconFirst)->rva;
            entrySegArray.cbSeg =
              (pcvseg->pconLast->rva + pcvseg->pconLast->cbRawData) -
              pcvseg->pconFirst->rva;

            FileWrite(FileWriteHandle, &entrySegArray, sizeof(entrySegArray));

            pcvsegNext = pcvseg->pcvsegNext;
            FreePv(pcvseg);
            pcvseg = pcvsegNext;
        }
        assert(pcvseg == NULL);

        FileWrite(FileWriteHandle, &cbFilename, sizeof(BYTE));
        FileWrite(FileWriteHandle, szFilename, nameLen);
        CvInfo[li].Module.SizeOfSubsection =
            FileTell(FileWriteHandle) - CvInfo[li].Module.PointerToSubsection;
    }

    // Emit the sstPublicSym subsection.

    csstPublicSym = 0;   // actual number of such subsections
    ChainCvPublics(pimage);

    for (li = 0; li < NextCvObject; li++) {
        DWORD icvOther;

        if (CvInfo[li].Publics.PointerToSubsection == 0xFFFFFFFF) {
            // This is a dup of another module that has been emitted

            CvInfo[li].Publics.PointerToSubsection = 0;
            continue;
        }

        csstPublicSym++;

        CvInfo[li].Publics.PointerToSubsection = FileTell(FileWriteHandle);

        // Write signature

        lj = 1;
        FileWrite(FileWriteHandle, &lj, sizeof(DWORD));

        EmitCvPublics(pimage, &CvInfo[li]);

        if (FIsLibPMOD(CvInfo[li].pmod)) {
            for (icvOther = li + 1; icvOther < NextCvObject; icvOther++) {
                // Emit this module if
                //    (Module is from same library as current module AND
                //     Module has the same name as the current module)

                if (CvInfo[li].pmod->plibBack != CvInfo[icvOther].pmod->plibBack) {
                    continue;
                }

                if (strcmp(CvInfo[li].ObjectFilename, CvInfo[icvOther].ObjectFilename) != 0) {
                    continue;
                }

                EmitCvPublics(pimage, &CvInfo[icvOther]);
                CvInfo[icvOther].Publics.PointerToSubsection = 0xFFFFFFFF;
            }
        }

        CvInfo[li].Publics.SizeOfSubsection =
            FileTell(FileWriteHandle) - CvInfo[li].Publics.PointerToSubsection;
    }

    // The sstSymbols and sstTypes subsections have already been
    // emitted directly from the object files. The sstSrcLnSeg
    // have also been emitted indirectly from the object files.

    // Emit the sstLibraries subsection.

    libStartSeek = FileTell(FileWriteHandle);
    nameLen = 0;
    FileWrite(FileWriteHandle, &nameLen, sizeof(BYTE));

    InitEnmLib(&enm_lib, pimage->libs.plibHead);
    while (FNextEnmLib(&enm_lib)) {
        plib = enm_lib.plib;

        if (plib->szName != NULL) {
            nameLen = (BYTE) strlen(plib->szName);
            FileWrite(FileWriteHandle, &nameLen, sizeof(BYTE));
            FileWrite(FileWriteHandle, plib->szName, nameLen);
        }
    }

    libEndSeek = FileTell(FileWriteHandle);

    // Emit the sstSegTable subsection.

    segTableStartSeek = FileTell(FileWriteHandle);
    i = (WORD) (pimage->ImgFileHdr.NumberOfSections + 1);
    FileWrite(FileWriteHandle, &i, sizeof(WORD));
    FileWrite(FileWriteHandle, &i, sizeof(WORD));
    segTable.iovl = 0;
    segTable.igr = 0;
    for (i = 1; i <= pimage->ImgFileHdr.NumberOfSections; i++) {
        InitEnmSec(&enm_sec, &pimage->secs);
        while (FNextEnmSec(&enm_sec)) {
            psec = enm_sec.psec;

            if (psec->isec == i) {
                break;
            }
        }
        EndEnmSec(&enm_sec);

        segTable.flags = 0x0108;
        if (psec->flags & IMAGE_SCN_MEM_READ) {
            segTable.flags |= 0x1;
        }
        if (psec->flags & IMAGE_SCN_MEM_WRITE) {
            segTable.flags |= 0x2;
        }
        if (psec->flags & IMAGE_SCN_MEM_EXECUTE) {
            segTable.flags |= 0x4;
        }

        // In the case of M68K pass the MacResource number
        // instead of the actual section number.
        segTable.isgPhy = fM68K ? psec->iResMac : i;
        segTable.isegName = 0xffff;    // No name
        segTable.iClassName = 0xffff;  // No name
        
        // If it is M68K and the psec->iResMac is 0, then 
        // provide the offset from the beginning of the data0
        segTable.segOffset = fM68K ? psec->dwM68KDataOffset : 0;
        
        segTable.cbSeg = psec->cbVirtualSize;
        FileWrite(FileWriteHandle, &segTable, sizeof(segTable));
    }

    // Write another sstSegMap entry for all absolute symbols.

    segTable.flags = 0x0208;           // absolute
    segTable.isgPhy = 0;
    segTable.isegName = 0xffff;        // No name
    segTable.iClassName = 0xffff;      // No name
    segTable.cbSeg = 0xffffffff;       // Allow full 32 bit range
    FileWrite(FileWriteHandle, &segTable, sizeof(segTable));

    segTableEndSeek = FileTell(FileWriteHandle);

    // Write SstSrcModul entries

    for(li = 0; li < NextCvObject; li++)       // for each module
    {
        if (CvInfo[li].pmod->pModDebugInfoApi == NULL) {
            // Don't write linenumber records if there aren't any

            CvInfo[li].pmod->PointerToSubsection = 0;
        } else {
            CvInfo[li].pmod->PointerToSubsection = FileTell(FileWriteHandle);
            FileWrite(FileWriteHandle,CvInfo[li].pmod->pSstSrcModInfo,CvInfo[li].pmod->cbSstSrcModInfo);
            cSstSrcModule++;
        }
     }

    // Emit the Subsection directory.

    CvSeeks.SubsectionDir = FileTell(FileWriteHandle);

    // We'll have a sstModule for every object, an sstPublicSym for every
    // object with a unique name, and optionaly some
    // sstSymbols and sstTypes.

    numSubsections = NextCvObject + csstPublicSym +
                     numLocals +
                     numTypes +
                     numLinenums +
                     cSstSrcModule +
                     2; // include sstLibraries & sstSegTable

    dirHdr.cbDirHeader = sizeof(dirHdr);
    dirHdr.cbDirEntry = sizeof(subDir);
    dirHdr.cDir = numSubsections;
    dirHdr.lfoNextDir = 0;
    dirHdr.flags = 0;

    FileWrite(FileWriteHandle, &dirHdr, sizeof(dirHdr));

    // Emit the sstModule entries.

    subDir.subsection = 0x120;
    for (li = 0; li < NextCvObject; li++) {
        subDir.imod = (WORD)(li + 1);
        subDir.lfo = CvInfo[li].Module.PointerToSubsection - CvSeeks.Base;
        subDir.cb = CvInfo[li].Module.SizeOfSubsection;
        FileWrite(FileWriteHandle, &subDir, sizeof(subDir));
    }

    // Emit the sstPublicSym entries.

    subDir.subsection = 0x123;      // sstPublicSym
    for (li = 0; li < NextCvObject; li++) {
        if (CvInfo[li].Publics.PointerToSubsection == 0) {
            // this module doesn't have one (duplicate name)

            continue;
        }

        subDir.imod = (WORD)(li + 1);
        subDir.lfo = CvInfo[li].Publics.PointerToSubsection - CvSeeks.Base;
        subDir.cb = CvInfo[li].Publics.SizeOfSubsection;
        FileWrite(FileWriteHandle, &subDir, sizeof(subDir));
    }

    // Emit the sstSymbols entries.

    subDir.subsection = 0x124; // sstSymbols
    for (li = 0; li < NextCvObject; li++) {
        if (CvInfo[li].Locals.PointerToSubsection) {
            subDir.imod = (WORD)(li + 1);
            subDir.lfo = CvInfo[li].Locals.PointerToSubsection - CvSeeks.Base;
            subDir.cb = CvInfo[li].Locals.SizeOfSubsection;
            FileWrite(FileWriteHandle, &subDir, sizeof(subDir));
        }
    }

    // Emit the SstSrcModule entries

    subDir.subsection=0x127;  // SstSrcModule
    for(li = 0; li < NextCvObject ; li++) {
         if (CvInfo[li].pmod->PointerToSubsection) {
             subDir.imod = (WORD)(li + 1);
             subDir.lfo =  CvInfo[li].pmod->PointerToSubsection - CvSeeks.Base;
             subDir.cb = CvInfo[li].pmod->cbSstSrcModInfo;
             FileWrite(FileWriteHandle,&subDir,sizeof(subDir));
         }
    }

    // Emit the sstTypes entries.

    for (li = 0; li < NextCvObject; li++) {
        if (CvInfo[li].Types.PointerToSubsection) {
            subDir.subsection = (WORD) (CvInfo[li].Types.Precompiled ? 0x12f : 0x121);
            subDir.imod = (WORD)(li + 1);
            subDir.lfo = CvInfo[li].Types.PointerToSubsection - CvSeeks.Base;
            subDir.cb = CvInfo[li].Types.SizeOfSubsection;
            FileWrite(FileWriteHandle, &subDir, sizeof(subDir));
        }
    }

    // Emit the sstLibraries entry.

    subDir.subsection = 0x128;
    subDir.imod = 0xffff;  // -1
    subDir.lfo = libStartSeek - CvSeeks.Base;
    subDir.cb = libEndSeek - libStartSeek;
    FileWrite(FileWriteHandle, &subDir, sizeof(subDir));

    // Emit the sstSegTable entry.

    subDir.subsection = 0x12d;
    subDir.imod = 0xffff;  // -1
    subDir.lfo = segTableStartSeek - CvSeeks.Base;
    subDir.cb = segTableEndSeek - segTableStartSeek;
    FileWrite(FileWriteHandle, &subDir, sizeof(subDir));
}
示例#9
0
文件: main.c 项目: kfreezen/kos
int kmain(UInt32 initial_stack, MultibootHeader* mboot, UInt32 mboot_magic) {
	initial_esp = initial_stack;
	
	CLI_Init();
	Cls();
	
	UInt32 initrd_end = *(UInt32*)(mboot->mods_addr+4);
	
	placement_address = (Pointer) initrd_end;
	
	// Set up our new stack here.
	//UInt32 stack = (UInt32) kmalloc_a(8192, TRUE);
	MultibootHeader* mboot_hdr = mboot; //kmalloc(sizeof(MultibootHeader));
	//memcpy(mboot_hdr, mboot, sizeof(MultibootHeader));
	
	kprintf("Starting init...\n");

	//new_start(stack, mboot_hdr);
	GDT_Init();
	IDT_Init();
	ISR_Init();
	asm volatile("sti");
	
	kprintf("Basics\t\t\t[OK]\n");

	PIT_Init(PIT_MSTIME);

	kprintf("PIT\t\t\t[OK]\n");

	init_kheap();
	InitPaging((mboot_hdr->mem_lower+mboot_hdr->mem_upper)&~3);
	InitKernelHeap();

	kprintf("Heap\t\t\t[OK]\n");

	VFS_Init();
	DevFS_Init();
	
	kprintf("VFS\t\t\t[OK]\n");

	DriversInit();

	kprintf("Drivers\t\t\t[OK]\n");

	Screen_Init();

	FloppyInit();

	checkAllBuses();
	DumpPCIDeviceData();
	kprintf("PCI\t\t\t[OK]\n");

	/*kprintf("Keyboard Init... ");
	KB_Init(0);
	kprintf("[ok]\n");*/

	FAT12_Init(FAT12_GetContext(FloppyGetDevice()), "/", "sys");
	
	InitTasking();

	KernelSymbolsLoad();

	//Cls();

	kprintf("kOS v0.6.13\n");

	VFS_Node* rd = GetNodeFromFile(GetFileFromPath("/sys"));
	kprintf("rd = %x\n", rd);
	
	ArrayList* list = ListFiles(rd);
	ALIterator* itr = ALGetItr(list);

	while(ALItrHasNext(itr)) {
		VFS_Node* node = ALItrNext(itr);
		kprintf("file: %s\n", node->name);
	}

	ALFreeItr(itr);
	
	ALFreeList(list);

	//kprintf("kprintf symbol = %x\n", getKernelSymbol("kprintf"));
	File* initScript = GetFileFromPath("/sys/init.script");
	FileSeek(0, initScript); // Due to these being global objects, we have to do such ugly things as this.

	#ifdef INIT_DEBUG
	kprintf("initScript=%x\n", initScript);
	#endif

	char* lineBuf = kalloc(256);
	int doBreak = 0;
	while(!doBreak) {
		if(fgetline(initScript, lineBuf, 256, '\n')==-1) {
			if(strlen(lineBuf) > 0) {
				doBreak = 1;
			} else {
				break; // We've processed everything that needs to be processed.
			}
		}

		// Now parse it.
		char* tok = strtok(lineBuf, " ");
		kprintf("%s, %x\n", tok,tok);
		if(!strcmp(tok, "load_driver")) {
			#ifdef INIT_DEBUG
			kprintf("load_driver ");
			#endif

			tok = strtok(NULL, " ");

			// Load the driver specified.
			File* drv = GetFileFromPath(tok);
			if(drv != NULL) {
				int drvLength = FileSeek(SEEK_EOF, drv);
				FileSeek(0, drv);
				void* drvBuf = kalloc(drvLength);

				#ifdef INIT_DEBUG
				kprintf("%s\n", GetNodeFromFile(drv)->name);
				#endif

				ReadFile(drvBuf, drvLength, drv);
				ELF* elf = LoadKernelDriver(drvBuf);

				#ifdef INIT_DEBUG
				kprintf("elf->start=%x\n", elf->start);
				#endif

				if(elf->error == 0) {
					void (*driverInit)() = (void (*)()) elf->start;
					driverInit();
				}

				kfree(drvBuf);
				drvBuf = NULL;

				CloseFile(drv);
			}
		}
	}

	CloseFile(initScript);
	kfree(lineBuf);

	kprintf("Kernel init done...\n");

	File* elf = GetFileFromPath("/sys/helloworld");
	
	FileSeek(SEEK_EOF, elf);
	int length = FileTell(elf);
	FileSeek(0, elf);
	UInt8* elfBuf = kalloc(length);
	ReadFile(elfBuf, length, elf);
	ELF* elfExe = Parse_ELF(elfBuf);
	CreateTaskFromELF(elfExe);

	// Kernel main logic loop
	while(1) {
		asm volatile("hlt");
	}

	return 0;
}