Skip to content

Commit

Permalink
Initial port of io-mmap to w32
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Mar 12, 2013
1 parent 8f4d23a commit dacede5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 82 deletions.
3 changes: 3 additions & 0 deletions libr/include/r_types_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#define UT8_MAX 0xFFU
#define UT8_MIN 0x00U

#define UT32_LO(x) ((ut32)((x)&UT32_MAX))
#define UT32_HI(x) ((ut32)(((ut64)(x))>>32)&UT32_MAX)

/* copied from bithacks.h */
#define B_IS_SET(x, n) (((x) & (1<<(n)))?1:0)
#define B_SET(x, n) ((x) |= (1<<(n)))
Expand Down
167 changes: 103 additions & 64 deletions libr/util/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,33 @@ R_API boolt r_file_rm(const char *file) {
}

R_API int r_file_mmap_write(const char *file, ut64 addr, const ut8 *buf, int len) {
int fd;
#if __WINDOWS__
fd = r_sandbox_open (file, O_BINARY, 0644);
#else
fd = r_sandbox_open (file, O_RDWR|O_SYNC, 0644);
#endif
#if __UNIX__
HANDLE fm, fh;
if (r_sandbox_enable (0)) return -1;
fh = CreateFile (file, rw?GENERIC_WRITE:GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
free (m);
return -1;
}
fm = CreateFileMapping (m->fh, NULL,
rw? PAGE_READWRITE:PAGE_READONLY, 0, 0, NULL);
if (fm == NULL) {
CloseHandle (fh);
return -1;
}
if (fm != INVALID_HANDLE_VALUE) {
ut8 *obuf = MapViewOfFile (m->fm, rw?
(FILE_MAP_READ|FILE_MAP_WRITE):FILE_MAP_READ,
UT32_HI (base), UT32_LO (base), 0);
memcpy (obuf, buf, len);
UnmapViewOfFile (obuf);
}
CloseHandle (fh);
CloseHandle (fm);
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDWR|O_SYNC, 0644);
const int pagesize = 4096;
int mmlen = len+pagesize;
int rest = addr%pagesize;
Expand All @@ -272,17 +292,39 @@ R_API int r_file_mmap_write(const char *file, ut64 addr, const ut8 *buf, int len
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
#else
return -1;
#endif
return 0;
}

R_API int r_file_mmap_read (const char *file, ut64 addr, ut8 *buf, int len) {
#if __WINDOWS__
int fd = r_sandbox_open (file, O_BINARY, 0644);
#else
HANDLE fm, fh;
if (r_sandbox_enable (0)) return -1;
fh = CreateFile (file, rw?GENERIC_WRITE:GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
free (m);
return -1;
}
fm = CreateFileMapping (m->fh, NULL,
rw? PAGE_READWRITE:PAGE_READONLY, 0, 0, NULL);
if (fm == NULL) {
CloseHandle (fh);
return -1;
}
if (fm != INVALID_HANDLE_VALUE) {
ut8 *obuf = MapViewOfFile (m->fm, rw?
(FILE_MAP_READ|FILE_MAP_WRITE):FILE_MAP_READ,
UT32_HI (base), UT32_LO (base), 0);
memcpy (obuf, buf, len);
UnmapViewOfFile (obuf);
}
CloseHandle (fh);
CloseHandle (fm);
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDONLY, 0644);
#endif
#if __UNIX__
const int pagesize = 4096;
int mmlen = len+pagesize;
int rest = addr%pagesize;
Expand All @@ -302,64 +344,61 @@ R_API int r_file_mmap_read (const char *file, ut64 addr, ut8 *buf, int len) {

// TODO: add rwx support?
R_API RMmap *r_file_mmap (const char *file, boolt rw, ut64 base) {
RMmap *m = NULL;
#if __WINDOWS__
int fd = r_sandbox_open (file, O_BINARY, 0644);
#else
RMmap *m;
int fd = r_sandbox_open (file, rw? O_RDWR: O_RDONLY, 0644);
#endif
if (fd != -1) {
m = R_NEW (RMmap);
if (!m) {
close (fd);
return NULL;
}
m->base = base;
m->rw = rw;
m->fd = fd;
m->len = lseek (fd, (off_t)0, SEEK_END);
if (fd == -1) return NULL;
m = R_NEW (RMmap);
if (!m) {
close (fd);
return NULL;
}
m->base = base;
m->rw = rw;
m->fd = fd;
m->len = lseek (fd, (off_t)0, SEEK_END);
#if __UNIX__
m->buf = mmap (NULL, m->len, rw?PROT_READ|PROT_WRITE:PROT_READ,
MAP_SHARED, fd, (off_t)base);
if (m->buf == MAP_FAILED) {
free (m);
m = NULL;
}
m->buf = mmap (NULL, m->len,
rw?PROT_READ|PROT_WRITE:PROT_READ,
MAP_SHARED, fd, (off_t)base);
if (m->buf == MAP_FAILED) {
free (m);
m = NULL;
}
#elif __WINDOWS__
close (fd);
m->fh = CreateFile (file, rw?GENERIC_WRITE:GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (m->fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
free (m);
return NULL;
}
m->fm = CreateFileMapping (m->fh, NULL,
rw?PAGE_READWRITE:PAGE_READONLY, 0, 0, NULL);
if (m->fm == NULL) {
CloseHandle (m->fh);
free (m);
return NULL;
}
if (m->fm != INVALID_HANDLE_VALUE) {
m->buf = MapViewOfFile (m->fm, rw?
FILE_MAP_READ|FILE_MAP_WRITE:FILE_MAP_READ, base, 0, 0);
} else {
CloseHandle (m->fh);
free (m);
m = NULL;
}
close (fd);
m->fh = CreateFile (file, rw?GENERIC_WRITE:GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (m->fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
free (m);
return NULL;
}
m->fm = CreateFileMapping (m->fh, NULL,
rw?PAGE_READWRITE:PAGE_READONLY, 0, 0, NULL);
if (m->fm == NULL) {
CloseHandle (m->fh);
free (m);
return NULL;
}
if (m->fm != INVALID_HANDLE_VALUE) {
m->buf = MapViewOfFile (m->fm, rw?
FILE_MAP_READ|FILE_MAP_WRITE:FILE_MAP_READ,
UT32_HI (base), UT32_LO (base), 0);
} else {
CloseHandle (m->fh);
free (m);
m = NULL;
}
#else
m->buf = malloc (m->len);
if (m->buf) {
lseek (fd, (off_t)0, SEEK_SET);
read (fd, m->buf, m->len);
} else {
free (m);
m = NULL;
}
#endif
m->buf = malloc (m->len);
if (m->buf) {
lseek (fd, (off_t)0, SEEK_SET);
read (fd, m->buf, m->len);
} else {
free (m);
m = NULL;
}
#endif
return m;
}

Expand Down
25 changes: 10 additions & 15 deletions libr/util/sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ R_API int r_sandbox_system (const char *x, int n) {

R_API int r_sandbox_creat (const char *path, int mode) {
if (enabled) {
// cannot create
if (mode & O_CREAT) return -1;
if (mode & O_RDWR) return -1;
if (!r_sandbox_check_path (path))
Expand All @@ -44,8 +43,10 @@ R_API int r_sandbox_creat (const char *path, int mode) {
}

R_API int r_sandbox_open (const char *path, int mode, int perm) {
#if __WINDOWS__
mode |= O_BINARY;
#endif
if (enabled) {
// cannot create
if (mode & O_CREAT) return -1;
if (mode & O_RDWR) return -1;
if (!r_sandbox_check_path (path))
Expand All @@ -70,25 +71,19 @@ R_API FILE *r_sandbox_fopen (const char *path, const char *mode) {
R_API int r_sandbox_chdir (const char *path) {
if (enabled) {
// TODO: check path
if (strstr (path, "../"))
return -1;
if (*path == '/')
return -1;
if (strstr (path, "../")) return -1;
if (*path == '/') return -1;
return -1;
}
return chdir (path);
}

R_API int r_sandbox_kill(int pid, int sig) {
if (enabled) // XXX: fine-tune. maybe we want to enable kill for child?
return -1;
// XXX: fine-tune. maybe we want to enable kill for child?
if (enabled) return -1;
#if __UNIX__
if (pid<1) {
eprintf ("r_sandbox_kill: Better not to kill negative pids.\n");
return -1;
}
return kill (pid, sig);
#else
return -1;
if (pid>=0) return kill (pid, sig);
eprintf ("r_sandbox_kill: Better not to kill negative pids.\n");
#endif
return -1;
}
5 changes: 2 additions & 3 deletions libr/util/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,11 @@ R_API void r_str_uri_decode (char *s) {
}

R_API char *r_str_uri_encode (const char *s) {
char ch[4], *o, *d, *od, *os;
char ch[4], *d, *od;
if (!s) return NULL;
os = s;
od = d = malloc (1+(strlen (s)*4));
if (!d) return NULL;
for (o=d; *s; s++) {
for (; *s; s++) {
if((*s>='0' && *s<='9')
|| (*s>='a' && *s<='z')
|| (*s>='A' && *s<='Z')) {
Expand Down

0 comments on commit dacede5

Please sign in to comment.