예제 #1
0
파일: PEInfo.cpp 프로젝트: jwtab/WinQ
BOOL CPEInfo::MapFile(const char * pszPath)
{
	BOOL bFileMaped = TRUE;

	//取消先前的文件映射
	UnMapFile();

	//打开文件
	m_hFile = CreateFileA(pszPath, GENERIC_READ,
		FILE_SHARE_READ, NULL,
		OPEN_EXISTING, 
		FILE_ATTRIBUTE_NORMAL, NULL);
	if (m_hFile == INVALID_HANDLE_VALUE)
	{
		bFileMaped = FALSE;		
	}

	//创建文件映射
	m_hMap = CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
	if (m_hMap == NULL)
	{
		bFileMaped = FALSE;		
	}

	m_pFileMap = (PUCHAR)MapViewOfFile(m_hMap, FILE_MAP_READ, 0, 0, 0);
	if (m_pFileMap == NULL)
	{
		bFileMaped = FALSE;		
	}
	
	if (!bFileMaped)
	{
		UnMapFile();
	}
	
	return bFileMaped;
}
예제 #2
0
파일: wcMT_VTP.c 프로젝트: oneminot/wsp
/* Thread worker callback function to process a single file		*/
VOID CALLBACK wcfunc (PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WORK Work)
/* Count the number of characters, works, and lines in			*/
/* the file targ->filename					*/
/* NOTE: Simple version; results may differ from wc utility		*/
{
	WORK_OBJECT_ARG * threadArgs;
	MAPPED_FILE_HANDLE fhandle;
	int iThrd;

	unsigned int ch, c, nl, nw, nc;
    int isspace_c;       // Current character
    int isspace_ch = 1;  // Previous character. Assume space to count first word

    int error;
    unsigned int fsize;
    char *fin, *fend;
	
    iThrd = InterlockedIncrement (&WorkerId) - 1;
	threadArgs = (WORK_OBJECT_ARG *)Context;
	threadArgs->wcerror = 1; /* Error for now */
	
	fin = (char*) map_file(threadArgs->filename, &fsize, &error, &fhandle);
    if (NULL == fin) {
        return;
    }

    fend = fin + fsize;

	ch = nw = nc = nl = 0;
    while (fin < fend) {
        c = *fin++;
        isspace_c = is_a_space(c);
        if (!isspace_c && isspace_ch) {
            nw++;
        }
        isspace_ch = isspace_c;
        if (c == '\n') {
            nl++;
            isspace_ch = 1; // Start looking for a new word
        }
    }

    UnMapFile(&fhandle);
	threadArgs->kchar = fsize;
	threadArgs->kword = nw;
	threadArgs->kline = nl;
	threadArgs->wcerror = 0;
	return;
}
예제 #3
0
/*
 * Fatal error in copy of archive file.
 * Reject copy request.
 */
static void
rejectRequest(
	int error,
	boolean_t clean)
{
	PthreadMutexLock(&Stream->mutex);

	Stream->pid = 0;

	if (error == 0) {
		if (GET_FLAG(Stream->flags, SR_DCACHE_CLOSE)) {
			SET_FLAG(Stream->flags, SR_CLEAR);
		} else {
			Stream->flags = 0;
			if (Stream->first == EOS) {
				/*
				 * Set active and done flags on stream
				 * for delete.
				 */
				Stream->last = EOS;
				SET_FLAG(Stream->flags, SR_ACTIVE);
				SET_FLAG(Stream->flags, SR_DONE);
			} else {
				Stream->priority = SP_start;
			}
		}
	} else {
		SET_FLAG(Stream->flags, SR_ERROR);
		Stream->error = error;
	}

	PthreadMutexUnlock(&Stream->mutex);

	if (clean) {
		/*
		 * Unmap pages of memory.  Stream's memory
		 * mapped file is removed in parent.
		 */
		UnMapFile(Stream, sizeof (StreamInfo_t));
		Stream = NULL;

		/* Remove copy request. */
		RemoveMapFile(copyRequestPath, Request,
		    sizeof (CopyRequestInfo_t));
		Request = NULL;

		Instance->ci_first = Instance->ci_last = NULL;
	}
}
예제 #4
0
static BOOLEAN
CheckForValidPEAndVendor(
    IN HANDLE RootDirectory OPTIONAL,
    IN PCWSTR PathNameToFile,
    OUT PUNICODE_STRING VendorName
    )
{
    BOOLEAN Success = FALSE;
    NTSTATUS Status;
    HANDLE FileHandle, SectionHandle;
    // SIZE_T ViewSize;
    PVOID ViewBase;
    PVOID VersionBuffer = NULL; // Read-only
    PVOID pvData = NULL;
    UINT BufLen = 0;

    if (VendorName->MaximumLength < sizeof(UNICODE_NULL))
        return FALSE;

    *VendorName->Buffer = UNICODE_NULL;
    VendorName->Length = 0;

    Status = OpenAndMapFile(RootDirectory, PathNameToFile,
                            &FileHandle, &SectionHandle, &ViewBase,
                            NULL, FALSE);
    if (!NT_SUCCESS(Status))
    {
        DPRINT1("Failed to open and map file '%S', Status 0x%08lx\n", PathNameToFile, Status);
        return FALSE; // Status;
    }

    /* Make sure it's a valid PE file */
    if (!RtlImageNtHeader(ViewBase))
    {
        DPRINT1("File '%S' does not seem to be a valid PE, bail out\n", PathNameToFile);
        Status = STATUS_INVALID_IMAGE_FORMAT;
        goto UnmapFile;
    }

    /*
     * Search for a valid executable version and vendor.
     * NOTE: The module is loaded as a data file, it should be marked as such.
     */
    Status = NtGetVersionResource((PVOID)((ULONG_PTR)ViewBase | 1), &VersionBuffer, NULL);
    if (!NT_SUCCESS(Status))
    {
        DPRINT1("Failed to get version resource for file '%S', Status 0x%08lx\n", PathNameToFile, Status);
        goto UnmapFile;
    }

    Status = NtVerQueryValue(VersionBuffer, L"\\VarFileInfo\\Translation", &pvData, &BufLen);
    if (NT_SUCCESS(Status))
    {
        USHORT wCodePage = 0, wLangID = 0;
        WCHAR FileInfo[MAX_PATH];

        wCodePage = LOWORD(*(ULONG*)pvData);
        wLangID   = HIWORD(*(ULONG*)pvData);

        StringCchPrintfW(FileInfo, ARRAYSIZE(FileInfo),
                         L"StringFileInfo\\%04X%04X\\CompanyName",
                         wCodePage, wLangID);

        Status = NtVerQueryValue(VersionBuffer, FileInfo, &pvData, &BufLen);

        /* Fixup the Status in case pvData is NULL */
        if (NT_SUCCESS(Status) && !pvData)
            Status = STATUS_NOT_FOUND;

        if (NT_SUCCESS(Status) /*&& pvData*/)
        {
            /* BufLen includes the NULL terminator count */
            DPRINT1("Found version vendor: \"%S\" for file '%S'\n", pvData, PathNameToFile);

            StringCbCopyNW(VendorName->Buffer, VendorName->MaximumLength,
                           pvData, BufLen * sizeof(WCHAR));
            VendorName->Length = wcslen(VendorName->Buffer) * sizeof(WCHAR);

            Success = TRUE;
        }
    }

    if (!NT_SUCCESS(Status))
        DPRINT1("No version vendor found for file '%S'\n", PathNameToFile);

UnmapFile:
    /* Finally, unmap and close the file */
    UnMapFile(SectionHandle, ViewBase);
    NtClose(FileHandle);

    return Success;
}
예제 #5
0
파일: PEInfo.cpp 프로젝트: jwtab/WinQ
CPEInfo::~CPEInfo()
{
	UnMapFile();
}
예제 #6
0
/*
 * Stage all files in the stream.
 */
static void
copyStream()
{
	int rval;
	FileInfo_t *file;
	int dcache;
	boolean_t reject;

	StageInit(Stream->vsn);

	/* Set loading flag for this stream. */
	PthreadMutexLock(&Stream->mutex);
	SET_FLAG(Stream->flags, SR_LOADING);
	PthreadMutexUnlock(&Stream->mutex);

	rval = LoadVolume();

	/* Reject if mount/open failed. */
	if (rval != 0) {
		PthreadMutexLock(&Stream->mutex);
		removeDcachedFile(Stream, rval);

		if (rval == ENODEV) {
			Stream->context = 0;
			PthreadMutexUnlock(&Stream->mutex);
			rejectRequest(0, B_TRUE);
			SET_FLAG(Instance->ci_flags, CI_shutdown);
		} else {
			PthreadMutexUnlock(&Stream->mutex);
			SendCustMsg(HERE, 19017, Stream->vsn);
			rejectRequest(rval, B_TRUE);
		}

		StageEnd();
		return;
	}

	/* VSN load has completed. */
	checkBuffers(Stream->vsn);

	PthreadMutexLock(&Stream->mutex);
	CLEAR_FLAG(Stream->flags, SR_LOADING);

	Instance->ci_seqnum = Stream->seqnum;
	reject = B_FALSE;

	/*
	 * Copy all files in stage stream request.  The files have
	 * been ordered to eliminate backward media positioning.
	 */

	while (STREAM_IS_VALID() && reject == B_FALSE) {

		/* Stop staging if parent died. */
		if (getppid() == 1) {
			SetErrno = 0;		/* set for trace */
			Trace(TR_ERR, "Detected stager daemon exit");

			Stream->first = EOS;
			SET_FLAG(Instance->ci_flags, CI_shutdown);
			break;
		}

		file = GetFile(Stream->first);

		PthreadMutexLock(&file->mutex);
		PthreadMutexUnlock(&Stream->mutex);

		/*
		 * If the first vsn, clear bytes read count.
		 * And if multivolume and stage -n set, initialize
		 * residual length.
		 */
		if (file->vsn_cnt == 0) {
			file->read = 0;
			if (GET_FLAG(file->flags, FI_MULTIVOL) &&
			    GET_FLAG(file->flags, FI_STAGE_NEVER)) {
				file->residlen = file->len;
			} else {
				file->residlen = 0;
			}
		}

		SET_FLAG(file->flags, FI_ACTIVE);

		PthreadMutexUnlock(&file->mutex);

		/* Set file in io control structure for archive read thread. */
		setIoThread(file);

		/* Log stage start. */
		file->eq = IoThread->io_drive;
		LogIt(LOG_STAGE_START, file);

		/*
		 * Check if last request was canceled.  If the last request
		 * was canceled, invalidate i/o buffers and clear cancel
		 * flag in the control structure.
		 */
		if (GET_FLAG(IoThread->io_flags, IO_cancel)) {
			ResetBuffers();
			CLEAR_FLAG(IoThread->io_flags, IO_cancel);
		}

		/*
		 * Next archive file.  If disk archive, we may be opening
		 * a disk archive tarball.
		 */
		if ((rval = NextArchiveFile()) == 0) {
			/* Prepare filesystem to receive staged file. */
			dcache = DiskCacheOpen(file);

		} else {
			/* Unable to open disk archive.  Error request. */
			Trace(TR_ERR, "Unable to open disk archive "
			    "copy: %d inode: %d.%d errno: %d",
			    file->copy + 1, file->id.ino, file->id.gen, errno);
			dcache = -1;
			file->error = errno;
			SendErrorResponse(file);
		}

		if (dcache >= 0 && rval == 0) {
			/*
			 * Notify reader thread that next file in stream
			 * is ready to be staged.
			 */
			ThreadStatePost(&IoThread->io_readReady);

			/* Write data to disk cache. */
			rval = DiskCacheWrite(file, dcache);

			if (rval != 0) {
				SendErrorResponse(file);

				/* Check if number of stream errors exceeded. */
				reject = ifMaxStreamErrors(file);
			}

			ThreadStateWait(&IoThread->io_readDone);

		} else if (rval != 0 && dcache >= 0) {
			/* Setup for error handling. */
			SetFileError(file, dcache, 0, EIO);
			SendErrorResponse(file);
		}

		EndArchiveFile();

		/* Remove file from stream before marking it as done. */
		PthreadMutexLock(&Stream->mutex);
		Stream->first = file->next;

		/* Device not available. */
		if (file->error == ENODEV) {
			SetErrno = 0;	/* set for trace */
			Trace(TR_ERR, "No device available");

			reject = B_TRUE;
			if (NumOpenFiles <= 0 && Instance->ci_first == NULL) {
				SET_FLAG(Instance->ci_flags, CI_shutdown);
				Instance->ci_busy = B_TRUE;
			}
		}

		/* Mark file staging as done. */
		SetStageDone(file);
		Stream->count--;

		if (Stream->first == EOS) {
			Stream->last = EOS;
		}

	}

	/* Reject rest of stages in this stream. */
	if (reject == B_TRUE) {
		if (Stream->first > EOS) {
			removeDcachedFile(Stream, ENODEV);
		}
		PthreadMutexUnlock(&Stream->mutex);
		rejectRequest(ENODEV, B_FALSE);
		PthreadMutexLock(&Stream->mutex);
	}

	/* Remove copy request, no one is waiting on it. */
	RemoveMapFile(copyRequestPath, Request, sizeof (CopyRequestInfo_t));
	Request = NULL;

	/* Ready to unload.  Mark stream as done. */
	SET_FLAG(Stream->flags, SR_DONE);
	PthreadMutexUnlock(&Stream->mutex);

	UnloadVolume();

	/*
	 * Unmap pages of memory.  Stream's memory
	 * mapped file is removed in parent.
	 */
	UnMapFile(Stream, sizeof (StreamInfo_t));
	Stream = NULL;

	StageEnd();
}
예제 #7
0
internal ShaderProgram * LoadShaderProgram( const char * VertexShaderFileName, const char * PixelShaderFileName, ShaderGroup * ShaderGroup)
{
    ShaderProgram * Shader = &ShaderGroup->Shaders[ShaderGroup->NumberOfShaders++];
    *Shader = {};

    strncpy(Shader->PixelFileName, PixelShaderFileName, MAX_SHADER_FILENAME);
    strncpy(Shader->VertexFileName, VertexShaderFileName, MAX_SHADER_FILENAME);

    if(ShaderGroup->NumberOfShaders+1>= MAX_SHADER_NUMBER)
    {
        LogError("Not enough shaders allocated, failed to load shader program for %s, %s", VertexShaderFileName, PixelShaderFileName);
        return &NullShader;
    }
    MemoryMappedFile VertexShaderFile=MemoryMapFile(VertexShaderFileName);
    if(!VertexShaderFile.MMapBuffer)
    {
        LogError("Failed to load vertex shader file %s",VertexShaderFileName);
        return &NullShader;
    }

    MemoryMappedFile PixelShaderFile=MemoryMapFile(PixelShaderFileName);
    if(!PixelShaderFile.MMapBuffer)
    {
        UnMapFile(PixelShaderFile);
        LogError("Failed to load pixel shader file %s",PixelShaderFileName);
        return &NullShader;
    }

    Shader->ProgramID = glCreateProgram();
    GLenum ErrorValue = glGetError();
    if(ErrorValue!=GL_NO_ERROR)
    {
        LogError("OpenGL error in CreateProgram for %s,%s : %s", VertexShaderFileName,PixelShaderFileName,gluErrorString(ErrorValue));
        Shader->ProgramID=0;
        UnMapFile(VertexShaderFile);
        UnMapFile(PixelShaderFile);
        return &NullShader;
    }
    Shader->VertexFileModifiedTime = VertexShaderFile.ModifiedTime;
    Shader->PixelFileModifiedTime = PixelShaderFile.ModifiedTime;

    Shader->VertexID = LoadShader(GL_VERTEX_SHADER,VertexShaderFile,VertexShaderFileName);
    glAttachShader(Shader->ProgramID,Shader->VertexID);

    Shader->PixelID = LoadShader(GL_FRAGMENT_SHADER,PixelShaderFile,PixelShaderFileName);
    glAttachShader(Shader->ProgramID,Shader->PixelID);

    UnMapFile(VertexShaderFile);
    UnMapFile(PixelShaderFile);

    ErrorValue = glGetError();
    if(ErrorValue!=GL_NO_ERROR)
    {
        LogError("OpenGL error in shader loading for %s,%s : %s", VertexShaderFileName,PixelShaderFileName,gluErrorString(ErrorValue));
        Shader->ProgramID=Shader->VertexID = Shader->PixelID = 0;
        return &NullShader;
    }

    if(!Shader->PixelID || !Shader->VertexID)
    {
        UnloadShaderProgram(Shader);
        Shader->ProgramID=Shader->VertexID = Shader->PixelID = 0;
        return &NullShader;
    }
    glLinkProgram(Shader->ProgramID);
    glUseProgram(Shader->ProgramID);

    return Shader;
}