#include "sysdeps.h"
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <errno.h>
#define TRACE_TAG TRACE_SYSDEPS
#include "adb.h"
extern void fatal(const char *fmt, ...);
#define assert(cond) do { if (!(cond)) fatal( "assertion failed '%s' on %s:%ld\n", #cond, __FILE__, __LINE__ ); } while (0)
/**************************************************************************/
/**************************************************************************/
/***** *****/
/***** replaces libs/cutils/load_file.c *****/
/***** *****/
/**************************************************************************/
/**************************************************************************/
void *load_file(const char *fn, unsigned *_sz)
{
HANDLE file;
char *data;
DWORD file_size;
file = CreateFile( fn,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL );
if (file == INVALID_HANDLE_VALUE)
return NULL;
file_size = GetFileSize( file, NULL );
data = NULL;
if (file_size > 0) {
data = (char*) malloc( file_size + 1 );
if (data == NULL) {
D("load_file: could not allocate %ld bytes\n", file_size );
file_size = 0;
} else {
DWORD out_bytes;
if ( !ReadFile( file, data, file_size, &out_bytes, NULL ) ||
out_bytes != file_size )
{
D("load_file: could not read %ld bytes from '%s'\n", file_size, fn);
free(data);
data = NULL;
file_size = 0;
}
}
}
CloseHandle( file );
*_sz = (unsigned) file_size;
return data;
}
/**************************************************************************/
/**************************************************************************/
/***** *****/
/***** common file descriptor handling *****/
/***** *****/
/**************************************************************************/
/**************************************************************************/
typedef const struct FHClassRec_* FHClass;
typedef struct FHRec_* FH;
typedef struct EventHookRec_* EventHook;
typedef struct FHClassRec_
{
void (*_fh_init) ( FH f );
int (*_fh_close)( FH f );
int (*_fh_lseek)( FH f, int pos, int origin );
int (*_fh_read) ( FH f, void* buf, int len );
int (*_fh_write)( FH f, const void* buf, int len );
void (*_fh_hook) ( FH f, int events, EventHook hook );
} FHClassRec;
/* used to emulate unix-domain socket pairs */
typedef struct SocketPairRec_* SocketPair;
typedef struct FHRec_
{
FHClass clazz;
int used;
int eof;
union {
HANDLE handle;
SOCKET socket;
SocketPair pair;
} u;
HANDLE event;
int mask;
char name[32];//32->1024
} FHRec;
#define fh_handle u.handle
#define fh_socket u.socket
#define fh_pair u.pair
#define WIN32_FH_BASE 100
#define WIN32_MAX_FHS 128
static adb_mutex_t _win32_lock;
static FHRec _win32_fhs[ WIN32_MAX_FHS ];
static int _win32_fh_count;
static FH
_fh_from_int( int fd )
{
FH f;
fd -= WIN32_FH_BASE;
if (fd < 0 || fd >= _win32_fh_count) {
D( "_fh_from_int: invalid fd %d\n", fd + WIN32_FH_BASE );
errno = EBADF;
return NULL;
}
f = &_win32_fhs[fd];
if (f->used == 0) {
D( "_fh_from_int: invalid fd %d\n", fd + WIN32_FH_BASE );
errno = EBADF;
return NULL;
}
return f;
}
static int
_fh_to_int( FH f )
{
if (f && f->used && f >= _win32_fhs && f < _win32_fhs + WIN32_MAX_FHS)
return (int)(f - _win32_fhs) + WIN32_FH_BASE;
return -1;
}
static FH
_fh_alloc( FHClass clazz )
{
int nn;
FH f = NULL;
adb_mutex_lock( &_win32_lock );
if (_win32_fh_count < WIN32_MAX_FHS) {
f = &_win32_fhs[ _win32_fh_count++ ];
goto Exit;
}
for (nn = 0; nn < WIN32_MAX_FHS; nn++) {
if ( _win32_fhs[nn].clazz == NULL) {
f = &_win32_fhs[nn];
goto Exit;
}
}
D( "_fh_alloc: no more free file descriptors\n" );
Exit:
if (f) {
f->clazz = clazz;
f->used = 1;
f->eof = 0;
clazz->_fh_init(f);
}
adb_mutex_unlock( &_win32_lock );
return f;
}
static int
_fh_close( FH f )
{
if ( f->used ) {
f->clazz->_fh_close( f );
f->used = 0;
f->eof = 0;
f->clazz = NULL;
}
return 0;
}
/* forward definitions */
//static const FHClassRec _fh_file_class;
//static const FHClassRec _fh_socket_class;
/**************************************************************************/
/**************************************************************************/
/***** *****/
/***** file-based descriptor handling *****/
/***** *****/
/**************************************************************************/
/**************************************************************************/
static void
_fh_file_init( FH f )
{
f->fh_handle = INVALID_HANDLE_VALUE;
}
static int
_fh_file_close( FH f )
{
CloseHandle( f->fh_handle );
f->fh_handle = INVALID_HANDLE_VALUE;
return 0;
}
static int
_fh_file_read( FH f, void* buf, int len )
{
DWORD read_bytes;
if ( !ReadFile( f->fh_handle, buf, (DWORD)len, &read_bytes, NULL ) ) {
D( "adb_read: could not read %d bytes from %s\n", len, f->name );
errno = EIO;
return -1;
} else if (read_bytes < (DWORD)len) {
f->eof = 1;
}
return (int)read_bytes;
}
static int
_fh_file_write( FH f, const void* buf, int len )
{
DWORD wrote_bytes;
if ( !WriteFile( f->fh_handle, buf, (DWORD)len, &wrote_bytes, NULL ) ) {
D( "adb_file_write: could not write %d bytes from %s\n", len, f->name );
errno = EIO;
return -1;
} else if (wrote_bytes < (DWORD)len) {
f->eof = 1;
}
return (int)wrote_bytes;
}
static int
_fh_file_lseek( FH f, int pos, int origin )
{
DWORD method;
DWORD result;
switch (origin)
{
case SEEK_SET: method = FILE_BEGIN; break;
case SEEK_CUR: method = FILE_CURRENT; break;
case SEEK_END: method = FILE_END; break;
default:
errno = EINVAL;
return -1;
}
result = SetFilePointer( f->fh_handle, pos, NULL, method );
if (result == INVALID_SET_FILE_POINTER) {
errno = EIO;
return -1;
} else {
f->eof = 0;
}
return (int)result;
}
static void _fh_file_hook( FH f, int event, EventHook eventhook ); /* forward */
static const FHClassRec _fh_file_class =
{
_fh_file_init,
_fh_file_close,
_fh_file_lseek,
_fh_file_read,
_fh_file_write,
_fh_file_hook
};
/**************************************************************************/
/**************************************************************************/
/***** *****/
/***** file-based descriptor handling *****/
/***** *****/
/**************************************************************************/
/**************************************************************************/
int adb_open(const char* path, int options)
{
FH f;
DWORD desiredAccess = 0;
DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
switch (options) {
case O_RDONLY:
desiredAccess = GENERIC_READ;
break;
case O_WRONLY:
desiredAccess = GENERIC_WRITE;
break;
case O_RDWR:
desiredAccess = GENERIC_READ | GENERIC_WRITE;
break;
default:
D("adb_open: invalid options (0x%0x)\n", options);
errno = EINVAL;
return -1;
}
f = _fh_alloc( &_fh_file_class );
if ( !f ) {
errno = ENOMEM;
return -1;
}
f->fh_handle = CreateFile(path, desiredAccess, shareMode, NULL, OPEN_EXISTING,
0, NULL );
if ( f->fh_handle == INVALID_HANDLE_VALUE ) {
_fh_close(f);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
adb for window (122个子文件)
sysdeps_win32.cpp 51KB
commandline.cpp 37KB
adb.cpp 32KB
transport.cpp 23KB
file_sync_client.cpp 23KB
sockets.cpp 18KB
usb_windows.cpp 15KB
services.cpp 13KB
transport_local.cpp 13KB
usb_vendors.cpp 7KB
centraldir.cpp 7KB
adb_client.cpp 6KB
zipfile.cpp 3KB
transport_usb.cpp 3KB
utils.cpp 2KB
get_my_path_windows.cpp 920B
console.cpp 893B
msvcr110d.dll 1.61MB
msvcr110.dll 844KB
msvcr110.dll 844KB
AdbWinApi.dll 670KB
AdbWinUsbApi.dll 527KB
AdbWinApi.dll 108KB
AdbWinUsbApi.dll 78KB
adb.exe 296KB
adb.exe 296KB
adb.exe 138KB
adb.exe 138KB
adb.vcxproj.filters 4KB
zlib.h 66KB
adb_api.h 26KB
adb.h 13KB
sysdeps.h 11KB
zconf.h 11KB
usb100.h 8KB
sockets.h 3KB
utils.h 2KB
file_sync_service.h 2KB
fdevent.h 2KB
zipfile.h 2KB
adb_client.h 2KB
private.h 1KB
mutex_list.h 827B
usb_vendors.h 763B
resource.h 387B
unistd.h 189B
vc110.idb 915KB
adb.ilk 727KB
adb.lastbuildstate 58B
adb.lastbuildstate 56B
zlib.lib 246KB
AdbWinApi.lib 28KB
AdbWinApi.lib 28KB
AdbWinApi.lib 28KB
AdbWinUsbApi.lib 2KB
AdbWinUsbApi.lib 2KB
AdbWinUsbApi.lib 2KB
adb.log 4KB
adb.log 4KB
adb.Build.CppClean.log 2KB
adb.Build.CppClean.log 2KB
sysdeps_win32.obj 220KB
sysdeps_win32.obj 194KB
commandline.obj 185KB
transport.obj 183KB
usb_vendors.obj 181KB
adb.obj 166KB
sockets.obj 154KB
file_sync_client.obj 151KB
commandline.obj 145KB
transport.obj 141KB
usb_windows.obj 130KB
transport_local.obj 124KB
adb_client.obj 121KB
adb.obj 117KB
services.obj 115KB
transport_usb.obj 112KB
console.obj 105KB
sockets.obj 102KB
file_sync_client.obj 89KB
usb_windows.obj 70KB
get_my_path_windows.obj 69KB
transport_local.obj 65KB
usb_vendors.obj 62KB
adb_client.obj 59KB
services.obj 49KB
transport_usb.obj 44KB
console.obj 31KB
get_my_path_windows.obj 26KB
centraldir.obj 21KB
zipfile.obj 19KB
centraldir.obj 14KB
utils.obj 13KB
zipfile.obj 13KB
utils.obj 6KB
adb.pdb 1.44MB
vc110.pdb 1020KB
adb.pdb 699KB
vc110.pdb 220KB
adb.sdf 16.13MB
共 122 条
- 1
- 2
ailudien
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 整卷预览.mhtml
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- 基于Javascript的结婚请帖设计源码 - Invitation
- mysql语句大全及用法
- mysql语句大全及用法
- mysql语句大全及用法
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页