* Fix offbyone in asm.brainbuck bug

* Fix some segfaults and issues in r_magic
* Use the libr/socket/http library
* Added r_io_http plugin
* Magic search is extremely slow
This commit is contained in:
pancake 2011-10-07 01:16:45 +02:00
parent 508963677b
commit 9efe88a9ce
18 changed files with 227 additions and 69 deletions

View File

@ -14,12 +14,13 @@ static int disassemble(struct r_asm_t *a, struct r_asm_op_t *op, const ut8 *buf,
if ((b = buf_cp = malloc (len+1)) == NULL)
return 0;
memcpy (buf_cp, buf, len+1);
memcpy (buf_cp, buf, len);
buf_cp[len] = 0;
for (i=0; b[0] == b[1] && i<len; b++, i++);
b[1] = '\0';
switch(buf[0]) {
switch (*buf) {
case '[':
strcpy (op->buf_asm, "[ loop {");
break;
@ -43,10 +44,10 @@ static int disassemble(struct r_asm_t *a, struct r_asm_op_t *op, const ut8 *buf,
else strcpy (op->buf_asm, "- dec [ptr]");
break;
case ',':
strcpy (op->buf_asm, ", [ptr] = getch()");
strcpy (op->buf_asm, ", [ptr] = getch ()");
break;
case '.':
strcpy (op->buf_asm, ". print( [ptr] )");
strcpy (op->buf_asm, ". print ([ptr])");
break;
case '\x00':
strcpy (op->buf_asm, " trap");
@ -56,7 +57,7 @@ static int disassemble(struct r_asm_t *a, struct r_asm_op_t *op, const ut8 *buf,
break;
}
if (i>0) sprintf (op->buf_asm, "%s, %d", op->buf_asm, i+1);
if (i>0) snprintf (op->buf_asm, sizeof (op->buf_asm), "%s, %d", op->buf_asm, i+1);
if (i<1) i=1; else i++;
free (buf_cp);

View File

@ -1577,7 +1577,7 @@ static void r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth,
const char *fmt;
char *q, *p;
const char *str;
static RMagic *ck = NULL;
static RMagic *ck = NULL; // XXX: Use RCore->magic
static char *oldfile = NULL;
if (--depth<0)
@ -3298,6 +3298,13 @@ static int cmd_search(void *data, const char *input) {
if (from == UT64_MAX)
from = core->offset;
to = r_config_get_i (core->config, "search.to");
if (to == UT64_MAX) {
if (core->io->va) {
/* TODO: section size? */
} else {
to = core->file->size;
}
}
}
core->search->align = r_config_get_i (core->config, "search.align");
searchflags = r_config_get_i (core->config, "search.flags");
@ -4162,8 +4169,8 @@ static int r_core_cmd_pipe(RCore *core, char *radare_cmd, char *shell_cmd) {
stdout_fd = dup (1);
pipe (fds);
radare_cmd = r_str_trim_head (radare_cmd);
shell_cmd = r_str_trim_head (shell_cmd);
radare_cmd = (char*)r_str_trim_head (radare_cmd);
shell_cmd = (char*)r_str_trim_head (shell_cmd);
if (fork ()) {
dup2 (fds[1], 1);
close (fds[1]);

View File

@ -317,6 +317,7 @@ extern struct r_io_plugin_t r_io_plugin_debug;
extern struct r_io_plugin_t r_io_plugin_shm;
extern struct r_io_plugin_t r_io_plugin_gdb;
extern struct r_io_plugin_t r_io_plugin_rap;
extern struct r_io_plugin_t r_io_plugin_http;
extern struct r_io_plugin_t r_io_plugin_haret;
#endif

View File

@ -65,5 +65,8 @@ R_API int r_socket_proc_write (RSocketProc *sp, void *buf, int len);
R_API void r_socket_proc_printf (RSocketProc *sp, const char *fmt, ...);
R_API int r_socket_proc_ready (RSocketProc *sp, int secs, int usecs);
/* HTTP */
R_API char *r_socket_http_get (const char *url, int *code, int *rlen);
R_API char *r_socket_http_post (const char *url, const char *data, int *code, int *rlen);
#endif
#endif

View File

@ -305,7 +305,7 @@ R_API char *r_str_word_get_first(const char *string);
R_API char *r_str_chop(char *str);
R_API const char *r_str_chop_ro(const char *str);
R_API char *r_str_trim(char *str);
R_API char *r_str_trim_head(const char *str);
R_API const char *r_str_trim_head(const char *str);
R_API char *r_str_trim_tail(char *str);
R_API char *r_str_trim_head_tail(char *str);
R_API ut32 r_str_hash(const char *str);

20
libr/io/p/http.mk Normal file
View File

@ -0,0 +1,20 @@
OBJ_HTTP=io_http.o
STATIC_OBJ+=${OBJ_HTTP}
TARGET_HTTP=io_http.${EXT_SO}
ALL_TARGETS+=${TARGET_HTTP}
ifeq (${WITHPIC},0)
LINKFLAGS+=../../util/libr_util.a
LINKFLAGS+=../../lib/libr_lib.a
LINKFLAGS+=../../io/libr_io.a
LINKFLAGS+=../../io/libr_socket.a
else
LINKFLAGS+=-L../../lib -lr_lib
LINKFLAGS+=-L../../util -lr_util
LINKFLAGS+=-L../../socket -lr_socket
LINKFLAGS+=-L.. -L../../lib -lr_lib -lr_io
endif
${TARGET_HTTP}: ${OBJ_HTTP}
${CC_LIB} $(call libname,io_http) ${CFLAGS} -o ${TARGET_HTTP} ${OBJ_HTTP} ${LINKFLAGS}

111
libr/io/p/io_http.c Normal file
View File

@ -0,0 +1,111 @@
/* radare - LGPL - Copyright 2008-2011 pancake<nopcode.org> */
#include "r_io.h"
#include "r_lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
typedef struct {
int fd;
ut8 *buf;
ut32 size;
} RIOMalloc;
#define RIOHTTP_FD(x) (((RIOMalloc*)x->data)->fd)
#define RIOHTTP_SZ(x) (((RIOMalloc*)x->data)->size)
#define RIOHTTP_BUF(x) (((RIOMalloc*)x->data)->buf)
static int __write(struct r_io_t *io, RIODesc *fd, const ut8 *buf, int count) {
if (fd == NULL || fd->data == NULL)
return -1;
if (io->off+count >= RIOHTTP_SZ (fd))
return -1;
memcpy (RIOHTTP_BUF (fd)+io->off, buf, count);
return count;
}
static int __read(struct r_io_t *io, RIODesc *fd, ut8 *buf, int count) {
int sz = RIOHTTP_SZ (fd);
if (fd == NULL || fd->data == NULL)
return -1;
if (io->off >= sz)
return -1;
if (io->off+count >= sz)
count = sz-io->off;
memcpy (buf, RIOHTTP_BUF (fd)+io->off, count);
return count;
}
static int __close(RIODesc *fd) {
RIOMalloc *riom;
if (fd == NULL || fd->data == NULL)
return -1;
riom = fd->data;
free (riom->buf);
riom->buf = NULL;
free (fd->data);
fd->data = NULL;
fd->state = R_IO_DESC_TYPE_CLOSED;
return 0;
}
static ut64 __lseek(struct r_io_t *io, RIODesc *fd, ut64 offset, int whence) {
switch (whence) {
case SEEK_SET: return offset;
case SEEK_CUR: return io->off + offset;
case SEEK_END: return RIOHTTP_SZ (fd);
}
return offset;
}
static int __plugin_open(struct r_io_t *io, const char *pathname) {
return (!memcmp (pathname, "http://", 7));
}
static inline int getmalfd (RIOMalloc *mal) {
return 0xfffffff & (int)(size_t)mal->buf;
}
static RIODesc *__open(struct r_io_t *io, const char *pathname, int rw, int mode) {
char *out;
int rlen, code;
if (__plugin_open (io, pathname)) {
RIOMalloc *mal = R_NEW (RIOMalloc);
mal->fd = getmalfd (mal);
out = r_socket_http_get (pathname, &code, &rlen);
if (!out || rlen<1) {
return NULL;
}
eprintf ("http code = %d\n", code);
mal->size = rlen;
mal->buf = malloc (mal->size+1);
if (mal->buf != NULL) {
memcpy (mal->buf, out, rlen);
free (out);
return r_io_desc_new (&r_io_plugin_http, mal->fd, pathname, rw, mode, mal);
}
eprintf ("Cannot allocate (%s) %d bytes\n", pathname+9, mal->size);
free (mal);
free (out);
}
return NULL;
}
struct r_io_plugin_t r_io_plugin_http = {
.name = "http",
.desc = "http get ( http://www.radare.org/ )",
.open = __open,
.close = __close,
.read = __read,
.plugin_open = __plugin_open,
.lseek = __lseek,
.write = __write,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_IO,
.data = &r_io_plugin_http
};
#endif

View File

@ -194,6 +194,7 @@ static int apprentice_1(RMagic *ms, const char *fn, int action, struct mlist *ml
int rv = -1;
int mapped;
ms->haderr = 0;
if (magicsize != FILE_MAGICSIZE) {
file_error(ms, 0, "magic element size %lu != %lu",
(unsigned long)sizeof(*magic),
@ -203,7 +204,7 @@ static int apprentice_1(RMagic *ms, const char *fn, int action, struct mlist *ml
ms->file = fn; // fix use of ms->file before being initialized
if (action == FILE_COMPILE) {
rv = apprentice_load(ms, &magic, &nmagic, fn, action);
rv = apprentice_load (ms, &magic, &nmagic, fn, action);
if (rv != 0)
return -1;
rv = apprentice_compile(ms, &magic, &nmagic, fn);
@ -211,10 +212,10 @@ static int apprentice_1(RMagic *ms, const char *fn, int action, struct mlist *ml
return rv;
}
if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
if ((rv = apprentice_map (ms, &magic, &nmagic, fn)) == -1) {
//if (ms->flags & R_MAGIC_CHECK)
// file_magwarn(ms, "using regular magic file `%s'", fn);
rv = apprentice_load(ms, &magic, &nmagic, fn, action);
rv = apprentice_load (ms, &magic, &nmagic, fn, action);
if (rv != 0)
return -1;
}
@ -222,7 +223,7 @@ static int apprentice_1(RMagic *ms, const char *fn, int action, struct mlist *ml
mapped = rv;
if (magic == NULL) {
file_delmagic(magic, mapped, nmagic);
file_delmagic (magic, mapped, nmagic);
return -1;
}
@ -269,28 +270,28 @@ struct mlist * file_apprentice(RMagic *ms, const char *fn, int action) {
int file_err, errs = -1;
struct mlist *mlist;
init_file_tables();
init_file_tables ();
if (fn == NULL)
fn = getenv("MAGIC");
fn = getenv ("MAGIC");
if (fn == NULL)
fn = MAGIC;
if ((mfn = strdup(fn)) == NULL) {
file_oomem(ms, strlen(fn));
if ((mfn = strdup (fn)) == NULL) {
file_oomem (ms, strlen (fn));
return NULL;
}
fn = mfn;
if ((mlist = malloc (sizeof(*mlist))) == NULL) {
if ((mlist = malloc (sizeof (*mlist))) == NULL) {
free (mfn);
file_oomem (ms, sizeof(*mlist));
file_oomem (ms, sizeof (*mlist));
return NULL;
}
mlist->next = mlist->prev = mlist;
while (fn) {
p = strchr(fn, PATHSEP);
p = strchr (fn, PATHSEP);
if (p) *p++ = '\0';
if (*fn == '\0') break;
file_err = apprentice_1 (ms, fn, action, mlist);
@ -491,7 +492,7 @@ static void load_1(RMagic *ms, int action, const char *fn, int *errs, struct r_m
(*errs)++;
} else {
/* read and parse this file */
for (ms->line = 1; fgets (line, sizeof(line), f) != NULL; ms->line++) {
for (ms->line = 1; fgets (line, sizeof (line), f) != NULL; ms->line++) {
size_t len = strlen (line);
if (len == 0) /* null line, garbage, etc */
continue;
@ -504,9 +505,9 @@ static void load_1(RMagic *ms, int action, const char *fn, int *errs, struct r_m
if (line[0] == '#') /* comment, do not parse */
continue;
if (len > mime_marker_len &&
memcmp(line, mime_marker, mime_marker_len) == 0) {
memcmp (line, mime_marker, mime_marker_len) == 0) {
/* MIME type */
if (parse_mime(ms, marray, marraycount,
if (parse_mime (ms, marray, marraycount,
line + mime_marker_len) != 0)
(*errs)++;
continue;
@ -1802,7 +1803,7 @@ static char *mkdbname(const char *fn, int strip) {
}
fnlen = strlen (fn);
extlen = strlen (ext);
buf = malloc (fnlen + extlen);
buf = malloc (fnlen + extlen + 1);
memcpy (buf, fn, fnlen);
memcpy (buf+fnlen, ext, extlen);
buf[fnlen+extlen] = 0;
@ -1881,9 +1882,7 @@ static ut64 swap8(ut64 sv) {
/*
* byteswap a single magic entry
*/
static void
bs1(struct r_magic *m)
{
static void bs1(struct r_magic *m) {
m->cont_level = swap2(m->cont_level);
m->offset = swap4((ut32)m->offset);
m->in_offset = swap4((ut32)m->in_offset);

View File

@ -160,6 +160,7 @@ int file_buffer(RMagic *ms, int fd, const char *inname, const void *buf, size_t
/* try compression stuff */
if ((ms->flags & R_MAGIC_NO_CHECK_COMPRESS) != 0 ||
(m = file_zmagic(ms, fd, inname, buf, nb)) == 0) {
#endif
/* Check if we have a tar file */
if ((ms->flags & R_MAGIC_NO_CHECK_TAR) != 0 ||
(m = file_is_tar(ms, buf, nb)) == 0) {
@ -178,19 +179,20 @@ int file_buffer(RMagic *ms, int fd, const char *inname, const void *buf, size_t
}
}
}
#if 0
}
#endif
return m;
}
int file_reset(RMagic *ms) {
ms->o.buf = NULL;
ms->haderr = 0;
ms->error = -1;
if (ms->mlist == NULL) {
file_error (ms, 0, "no magic files loaded");
return -1;
}
ms->o.buf = NULL;
ms->haderr = 0;
ms->error = -1;
return 0;
}
@ -202,25 +204,31 @@ int file_reset(RMagic *ms) {
*(n)++ = (((ut32)*(o) >> 0) & 7) + '0', \
(o)++)
const char * file_getbuffer(RMagic *ms) {
const char *file_getbuffer(RMagic *ms) {
char *pbuf, *op, *np;
size_t psize, len;
if (ms->haderr)
return NULL;
if (ms->flags & R_MAGIC_RAW)
return ms->o.buf;
if (ms->o.buf == NULL) {
eprintf ("ms->o.buf = NULL\n");
return NULL;
}
/* * 4 is for octal representation, + 1 is for NUL */
len = strlen(ms->o.buf);
len = strlen (ms->o.buf);
if (len > (SIZE_MAX - 1) / 4) {
file_oomem(ms, len);
file_oomem (ms, len);
return NULL;
}
psize = len * 4 + 1;
if ((pbuf = realloc(ms->o.pbuf, psize)) == NULL) {
file_oomem(ms, psize);
if ((pbuf = realloc (ms->o.pbuf, psize)) == NULL) {
file_oomem (ms, psize);
return NULL;
}
ms->o.pbuf = pbuf;

View File

@ -137,16 +137,16 @@ static const char * file_or_fd(RMagic *ms, const char *inname, int fd) {
}
errno = 0;
if ((fd = open (inname, flags)) < 0) {
fprintf(stderr, "couldn't open file\n");
if (info_from_stat(ms, sb.st_mode) == -1)
eprintf ("couldn't open file\n");
if (info_from_stat (ms, sb.st_mode) == -1)
goto done;
rv = 0;
goto done;
}
#ifdef O_NONBLOCK
if ((flags = fcntl(fd, F_GETFL)) != -1) {
if ((flags = fcntl (fd, F_GETFL)) != -1) {
flags &= ~O_NONBLOCK;
(void)fcntl(fd, F_SETFL, flags);
(void)fcntl (fd, F_SETFL, flags);
}
#endif
}
@ -172,7 +172,6 @@ static const char * file_or_fd(RMagic *ms, const char *inname, int fd) {
rv = 0;
goto done;
}
} else {
#endif
if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
@ -183,31 +182,30 @@ static const char * file_or_fd(RMagic *ms, const char *inname, int fd) {
}
#endif
(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1)
(void)memset (buf + nbytes, 0, SLOP); /* NUL terminate */
if (file_buffer (ms, fd, inname, buf, (size_t)nbytes) == -1)
goto done;
rv = 0;
done:
free(buf);
free (buf);
close_and_restore (ms, inname, fd, &sb);
return rv == 0 ? file_getbuffer(ms) : NULL;
}
/* API */
// TODO: reinitialize all the time
R_API RMagic* r_magic_new(int flags) {
RMagic *ms = R_NEW0 (RMagic);
if (!ms)
return NULL;
if (!ms) return NULL;
r_magic_setflags (ms, flags);
ms->o.buf = ms->o.pbuf = NULL;
ms->c.li = malloc ((ms->c.len = 10) * sizeof(*ms->c.li));
ms->c.li = malloc ((ms->c.len = 10) * sizeof (*ms->c.li));
if (ms->c.li == NULL) {
free (ms);
return NULL;
}
ms->haderr = 0;
ms->error = -1;
file_reset (ms);
ms->mlist = NULL;
ms->file = "unknown";
ms->line = 0;
@ -224,7 +222,7 @@ R_API void r_magic_free(RMagic *ms) {
}
R_API int r_magic_load(RMagic* ms, const char *magicfile) {
struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
struct mlist *ml = file_apprentice (ms, magicfile, FILE_LOAD);
if (ml) {
free_mlist (ms->mlist);
ms->mlist = ml;
@ -241,7 +239,7 @@ R_API int r_magic_compile(RMagic *ms, const char *magicfile) {
R_API int r_magic_check(RMagic *ms, const char *magicfile) {
struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
free_mlist(ml);
free_mlist (ml);
return ml ? 0 : -1;
}

View File

@ -270,7 +270,7 @@ static int check_fmt(RMagic *ms, struct r_magic *m) {
return -1;
} else {
rc = r_regex_exec (&rx, R_MAGIC_DESC, 0, 0, 0);
r_regex_free (&rx);
r_regex_fini (&rx);
return !rc;
}
}

View File

@ -2,32 +2,39 @@
#include <r_socket.h>
static char *r_socket_http_response (RSocket *s, int *code) {
char buf[32768];
static char *r_socket_http_response (RSocket *s, int *code, int *rlen) {
char *p;
int i, len;
char *buf = malloc (32768); // XXX: use r_buffer here
/* Read Header */
i = 0;
do {
len = r_socket_gets (s, buf+i, sizeof (buf)-i);
i += len;
r_socket_gets (s, buf+i, 1);
buf[i++]='\n';
buf[i++] = '\n';
} while (len > 0);
buf[i] = 0;
/* Parse Code */
p = strchr (buf, ' ');
*code = (p)?atoi (p+1):-1;
if (code) *code = (p)? atoi (p+1):-1;
/* Parse Len */
p = strstr (buf, "Content-Length: ");
len = (p)?atoi (p+16):0;
/* Read Content */
len = r_socket_read_block (s, (unsigned char *)buf+i, len);
if (p) {
len = atoi (p+16);
if (len>0)
len = r_socket_read_block (s, (ut8*)buf+i, len);
else len = 0;
} else {
len = 32768-i;
len = r_socket_read (s, (ut8*)buf+i, len);
}
r_socket_close (s);
return strdup (buf);
if (rlen) *rlen = len+i;
return buf;
}
R_API char *r_socket_http_get (const char *url, int *code) {
R_API char *r_socket_http_get (const char *url, int *code, int *rlen) {
RSocket *s;
int ssl = !memcmp (url, "https://", 8);
char *response, *host, *path, *port = "80";
@ -68,12 +75,12 @@ R_API char *r_socket_http_get (const char *url, int *code) {
"Accept: */*\r\n"
"Host: %s\r\n"
"\r\n", path, host);
response = r_socket_http_response (s, code);
response = r_socket_http_response (s, code, rlen);
free (uri);
return response;
}
R_API char *r_socket_http_post (const char *url, const char *data, int *code) {
R_API char *r_socket_http_post (const char *url, const char *data, int *code, int *rlen) {
RSocket *s;
int ssl = !memcmp (url, "https://", 8);
char *response, *host, *path, *port = "80";
@ -117,7 +124,7 @@ R_API char *r_socket_http_post (const char *url, const char *data, int *code) {
"Content-Type: application/x-www-form-urlencoded\r\n"
"\r\n", path, host, strlen (data));
r_socket_write (s, (void *)data, strlen (data));
response = r_socket_http_response (s, code);
response = r_socket_http_response (s, code, rlen);
free (uri);
return response;
}

View File

@ -203,6 +203,7 @@ R_API void r_regex_free(RRegex *preg) {
r_regex_fini (preg);
free (preg);
}
/*
- regcomp - interface for parser and compilation
*/

View File

@ -351,7 +351,7 @@ R_API char *r_str_chop(char *str) {
return str;
}
R_API char *r_str_trim_head(const char *str) {
R_API const char *r_str_trim_head(const char *str) {
if (str)
while (*str && iswhitechar (*str))
str++;
@ -369,7 +369,7 @@ R_API char *r_str_trim_tail(char *str) {
}
R_API char *r_str_trim_head_tail(char *str) {
return r_str_trim_tail (r_str_trim_head (str));
return r_str_trim_tail ((char*)r_str_trim_head (str));
}
R_API char *r_str_trim(char *str) {

View File

@ -290,7 +290,7 @@ R_API char *r_sys_cmd_str_full(const char *cmd, const char *input, int *len, cha
close (sh_in[1]);
waitpid (pid, &status, 0);
if (status != 0) {
eprintf ("%s: command returned !0\n", __func__);
eprintf ("%s: command '%s' returned !0\n", __func__, cmd);
return (NULL);
}

View File

@ -1,7 +1,7 @@
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
#include "r_util.h"
void test_flist () {
int test_flist () {
int i;
void **it = r_flist_new (3);
char *pos = NULL;
@ -22,6 +22,6 @@ void test_flist () {
}
int main() {
test_flist();
return test_flist();
}

View File

@ -70,6 +70,7 @@ fs.fb
fs.sfs
io.debug
io.rap
io.http
io.gdb
io.haret
io.mach

View File

@ -40,6 +40,7 @@ DSTNAME=radare2-bindings-w32-$(VERSION)
DST=../$(DSTNAME)/Python27/Lib/r2
w32dist:
rm -rf ${DST}/*
mkdir -p ${DST}
cp -f python/*.dll ${DST}
cp -f python/r_*.py ${DST}