?? osal_win32.cpp
字號:
/* * osal_win32.c * $Id: osal_win32.c,v 1.11 2004/12/04 10:20:52 b081 Exp $ * * Copyright 2004 Bobi B., w1zard0f07@yahoo.com * * This file is part of hdl_dump. * * hdl_dump is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * hdl_dump is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with hdl_dump; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <windows.h>#include <winioctl.h>#include <stdio.h>#include "retcodes.h"#include "osal.h"#include "apa.h"
struct DeviceInfo
{
char device_name[MAX_PATH];
osal_handle_t handle;
struct DeviceInfo * next;
struct DeviceInfo * prev;
};
static DeviceInfo * g_opened_devices=NULL;
DeviceInfo * FindDevice(osal_handle_t handle)
{
DeviceInfo * p=NULL;
for(p=g_opened_devices;p&&p->handle!=handle;p=p->next);
return p;
}
const char * osal_get_device_name(osal_handle_t handle)
{
DeviceInfo * p=FindDevice(handle);
if(p)
return p->device_name;
return NULL;
}
bool RegisterDevice(osal_handle_t handle, const char * szName)
{
bool t=false;
DeviceInfo * p;
if(handle&&szName&&FindDevice(handle)==NULL)
{
p=new DeviceInfo;
if(p)
{
if(szName[strlen(szName)-1]!='\\')
sprintf(p->device_name,"%s\\",szName);
else
strcpy(p->device_name,szName);
p->handle=handle;
p->prev=NULL;
p->next=g_opened_devices;
if(g_opened_devices)
{
g_opened_devices->prev=p;
}
g_opened_devices=p;
t=true;
}
}
return t;
}
bool UnregisterDevice(osal_handle_t handle)
{
bool t=false;
DeviceInfo * p=FindDevice(handle);
if(p)
{
if(p==g_opened_devices)
{
g_opened_devices=p->next;
if(g_opened_devices)
g_opened_devices->prev=NULL;
}
else
{
if(p->next)
p->next->prev=p->prev;
if(p->prev)
p->prev->next=p->next;
}
delete p;
t=true;
}
return t;
}
static int osal_dlist_alloc (osal_dlist_t **dlist);static int osal_dlist_add (osal_dlist_t *dlist, const char *name, u_int64_t capacity, int is_ps2, unsigned long status);
typedef BOOL (WINAPI * SetFilePointerExProc)(HANDLE hFile,LARGE_INTEGER liDistanceToMove,
PLARGE_INTEGER lpNewFilePointer,DWORD dwMoveMethod);
BOOL SetFilePointerEx(
HANDLE hFile, // handle to file
LARGE_INTEGER liDistanceToMove, // bytes to move pointer
PLARGE_INTEGER lpNewFilePointer, // new file pointer
DWORD dwMoveMethod // starting point
)
{
LARGE_INTEGER lRet;
SetFilePointerExProc proc=NULL;
HMODULE hModule=GetModuleHandle("kernel32.dll");
if(hModule)
{
proc=(SetFilePointerExProc)GetProcAddress(hModule,"SetFilePointerEx");
if(proc)
{
return (* proc)(hFile,liDistanceToMove,lpNewFilePointer,dwMoveMethod);
}
}
lRet.HighPart=liDistanceToMove.HighPart;
lRet.LowPart=SetFilePointer(hFile,liDistanceToMove.LowPart,&lRet.HighPart,dwMoveMethod);
if(lRet.LowPart>=0||GetLastError()==NO_ERROR)
{
if(lpNewFilePointer)
lpNewFilePointer->QuadPart=lRet.QuadPart;
return TRUE;
}
return FALSE;
}
BOOL GetFileSizeEx(
HANDLE hFile, // handle to file
PLARGE_INTEGER lpFileSize // file size
)
{
LARGE_INTEGER lRet;
lRet.LowPart=GetFileSize(hFile,(DWORD *)&lRet.HighPart);
if(lRet.LowPart>=0||GetLastError()==NO_ERROR)
{
if(lpFileSize)
lpFileSize->QuadPart=lRet.QuadPart;
return TRUE;
}
return FALSE;
}
/**************************************************************/unsigned longosal_get_last_error_code (void){ return (GetLastError ());}/**************************************************************/char*osal_get_error_msg (unsigned long err){ char *error = NULL;
if(err==ERROR_EMPTY)
{
static const char szEmptyErr[]={"無碟\n"};
error=(char *)LocalAlloc(LPTR,sizeof(szEmptyErr));
if(error)
{
strcpy(error,szEmptyErr);
}
}
else
{
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &error, 0, NULL);
} return (error);}/**************************************************************/char*osal_get_last_error_msg (void){ return (osal_get_error_msg (GetLastError ()));}/**************************************************************/voidosal_dispose_error_msg (char *msg){ LocalFree (msg);}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_open (const char *name, osal_handle_t *handle, int no_cache){ *handle = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | (no_cache ? FILE_FLAG_NO_BUFFERING : 0), NULL);
if(*handle!=INVALID_HANDLE_VALUE)
{
RegisterDevice(*handle,name);
} return (*handle != INVALID_HANDLE_VALUE ? OSAL_OK : OSAL_ERR);}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_open_device_for_writing (const char *device_name, osal_handle_t *handle){ *handle = CreateFile (device_name, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL); return (*handle != INVALID_HANDLE_VALUE ? OSAL_OK : OSAL_ERR);}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_create_file (const char *path, osal_handle_t *handle, u_int64_t estimated_size){ *handle = CreateFile (path, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL); if (*handle != INVALID_HANDLE_VALUE) { if (estimated_size > 0) { /* set file size to reduce fragmentation */ LARGE_INTEGER zero, output_size; output_size.QuadPart = estimated_size; zero.QuadPart = 0; if (SetFilePointerEx (*handle, output_size, NULL, FILE_BEGIN) && SetEndOfFile (*handle) && SetFilePointerEx (*handle, zero, NULL, FILE_BEGIN)) return (OSAL_OK); else { CloseHandle (*handle); DeleteFile (path); return (OSAL_ERR); /* error setting file length */ } } return (OSAL_OK); } return (OSAL_ERR); /* error creating file */}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_get_estimated_device_size (osal_handle_t handle, u_int64_t *size_in_bytes){ DISK_GEOMETRY geo; DWORD len = 0; if (DeviceIoControl (handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof (DISK_GEOMETRY), &len, NULL)&&len>0) { *size_in_bytes = (geo.Cylinders.QuadPart * geo.TracksPerCylinder * geo.SectorsPerTrack * geo.BytesPerSector); return (OSAL_OK); } else
{
DWORD nFree=0;
DWORD dwClusters=0;
DWORD dwSectorsPerCluster=0;
ULARGE_INTEGER lFreeBytes,lTotalBytes,lTotalFreeBytes;
const char * name=osal_get_device_name(handle);
if(name&&GetDiskFreeSpace(name,&dwSectorsPerCluster,&geo.BytesPerSector,&nFree,&dwClusters)&&
GetDiskFreeSpaceEx(name,&lFreeBytes,&lTotalBytes,&lTotalFreeBytes))
{
*size_in_bytes=lTotalBytes.QuadPart;
return (OSAL_OK);
}
}
SetLastError(ERROR_EMPTY);
return (OSAL_ERR);
}/**************************************************************/int /* OSAL_OK, OSAL_ERR */osal_get_device_size (osal_handle_t handle, u_int64_t *size_in_bytes){ int result = osal_get_estimated_device_size (handle, size_in_bytes); if (result == OSAL_OK) { LARGE_INTEGER offs; offs.QuadPart = *size_in_bytes; if (SetFilePointerEx (handle, offs, NULL, FILE_BEGIN)) /* seek to "end" */ { const u_int32_t BUFF_SIZE = 1024 * 1024; void *buffer = LocalAlloc (LMEM_FIXED, BUFF_SIZE); if (buffer != NULL) { BOOL success; DWORD len; do { /* count the number of bytes readen after the end */ success = ReadFile (handle, buffer, BUFF_SIZE, &len, NULL); if (success) *size_in_bytes += len; } while (success && len > 0); LocalFree (buffer); offs.QuadPart = 0; return (SetFilePointerEx (handle, offs, NULL, FILE_BEGIN) ? OSAL_OK : OSAL_ERR); } else return (OSAL_NO_MEM); /* out-of-memory */ } else return (OSAL_ERR); /* seek fails */ } else return (result); /* error */}/**************************************************************/intosal_get_device_sect_size (osal_handle_t handle, u_int32_t *size_in_bytes){ DISK_GEOMETRY geo; DWORD len = 0;
*size_in_bytes=0;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -