* Do not pass CFLAGS when linking asm/t/fastcall

* Populate r_io_lseek -> r_io_seek refactoring
* R_APIfy a bit the r_hash
* Also build hash/t/hello program
* Fix some warnings and build blockers
* Make rahash2 read from files (using slurp)
* Added r_buf API in r_util (interacts with r_io)
* Add initial template for r_io_bind
  - Needs some more work
This commit is contained in:
pancake 2009-09-08 01:08:46 +00:00
parent 16cf12c8d6
commit e5c83d6d26
20 changed files with 180 additions and 61 deletions

View File

@ -6,7 +6,7 @@ LIBS+=-ldl
all: ${BIN} fastcall
fastcall: fastcall.o
${CC} -I../../include fastcall.o -Wl,-R.. -L.. -lr_asm -o fastcall
${CC} fastcall.o -Wl,-R.. -L.. -lr_asm -o fastcall
myclean:
rm -f fastcall fastcall.o

View File

@ -776,7 +776,7 @@ static int cmd_write(void *data, const char *input)
{
ut64 off = r_num_math(&core->num, input+1);
r_io_set_fd(&core->io, core->file->fd);
r_io_lseek(&core->io, core->seek, R_IO_SEEK_SET);
r_io_seek(&core->io, core->seek, R_IO_SEEK_SET);
if (off&UT64_32U) {
/* 8 byte addr */
ut64 addr8;

View File

@ -71,7 +71,7 @@ R_API int r_core_seek(struct r_core_t *core, ut64 addr, int rb)
{
int ret;
r_io_set_fd(&core->io, core->file->fd);
ret = r_io_lseek(&core->io, addr, R_IO_SEEK_SET);
ret = r_io_seek(&core->io, addr, R_IO_SEEK_SET);
if (ret) {
core->seek = addr;
if (rb) return r_core_block_read (core, 0);
@ -96,7 +96,7 @@ R_API int r_core_block_read(struct r_core_t *core, int next)
if (core->file == NULL)
return -1;
r_io_set_fd (&core->io, core->file->fd);
r_io_lseek(&core->io, core->seek+((next)?core->blocksize:0), R_IO_SEEK_SET);
r_io_seek(&core->io, core->seek+((next)?core->blocksize:0), R_IO_SEEK_SET);
return r_io_read(&core->io, core->block, core->blocksize);
}

View File

@ -1,4 +1,5 @@
#include "r_db.h"
#include "r_util.h"
/*
struct r_db_table_t *table = \
r_db_table_new("cities", "ziu", "name people postal");
@ -13,7 +14,6 @@ sizes['i'] = 4;
struct r_db_table_t *r_db_table_new(const char *name, const char *fmt, const char *fields)
{
int i;
char *ptr;
int offset = 0;
struct r_db_table_t *table = MALLOC_STRUCT(struct r_db_table_t);
table->args = strdup(fields);

View File

@ -3,7 +3,7 @@
#include "r_hash.h"
/* returns 0-100 */
int r_hash_pcprint(ut8 *buffer, ut64 len)
R_API int r_hash_pcprint(ut8 *buffer, ut64 len)
{
ut8 *end = buffer + len;
int n;
@ -15,7 +15,7 @@ int r_hash_pcprint(ut8 *buffer, ut64 len)
return ((100*n)/len);
}
int r_hash_par(ut8 *buf, ut64 len)
R_API int r_hash_par(ut8 *buf, ut64 len)
{
ut8 *end = buf+len;
ut32 ones = 0;
@ -29,7 +29,7 @@ int r_hash_par(ut8 *buf, ut64 len)
/* These functions comes from 0xFFFF */
/* fmi: nopcode.org/0xFFFF */
ut16 r_hash_xorpair(const ut8 *a, ut64 len)
R_API ut16 r_hash_xorpair(const ut8 *a, ut64 len)
{
ut16 *b = (ut16 *)a;
ut16 result = 0;
@ -38,7 +38,7 @@ ut16 r_hash_xorpair(const ut8 *a, ut64 len)
return result;
}
ut8 r_hash_xor(const ut8 *b, ut64 len)
R_API ut8 r_hash_xor(const ut8 *b, ut64 len)
{
ut8 res = 0;
for(;len--;b=b+1)
@ -46,7 +46,7 @@ ut8 r_hash_xor(const ut8 *b, ut64 len)
return res;
}
ut8 r_hash_mod255(const ut8 *b, ut64 len)
R_API ut8 r_hash_mod255(const ut8 *b, ut64 len)
{
int i, c = 0;
/* from gdb */
@ -56,7 +56,7 @@ ut8 r_hash_mod255(const ut8 *b, ut64 len)
}
/* TODO: ignore case.. we have to use strcasestr */
ut64 r_hash_name_to_bits(const char *name)
R_API ut64 r_hash_name_to_bits(const char *name)
{
ut64 bits = R_HASH_NONE;
if (strstr(name, "md4"))

View File

@ -3,4 +3,12 @@ BIN=rahash2
BINDEPS=r_io r_hash r_util
LIBS=-lm
all: ${BIN} hello
hello: hello.o
${CC} -I../../include hello.o -Wl,-R.. -L.. -lr_hash -lr_io -o hello
myclean:
rm -f hello hello.o
include ../../rules.mk

View File

@ -24,9 +24,7 @@ int main(int argc, char **argv)
}
/* get file size */
r_io_lseek(&io, fd, 0, R_IO_SEEK_END);
size = r_io_lseek(&io, fd, 0, R_IO_SEEK_END);
r_io_lseek(&io, fd, 0, R_IO_SEEK_SET);
size = r_io_size(&io, fd);
/* read bytes */
buf = (ut8*) malloc(size);
@ -36,7 +34,8 @@ int main(int argc, char **argv)
r_io_close(&io, fd);
}
r_io_read(&io, fd, buf, size);
memset(buf, 0, size);
r_io_read(&io, buf, size);
printf("----\n%s\n----\n", buf);
printf("file size = %lld\n", size);

View File

@ -18,6 +18,7 @@ static int do_hash(const char *algo, const ut8 *buf, int len, int bsize)
bsize = len;
r_hash_state_init(&ctx, R_HASH_ALL);
//r_hash_state_init(&ctx, algobit);
/* TODO: Loop here for blocks */
if (algobit & R_HASH_MD4) {
c = r_hash_state_md4(&ctx, buf, len);
@ -48,7 +49,7 @@ static int do_help(int line)
int main(int argc, char **argv)
{
char *algo = "md5"; /* default hashing algorithm */
ut8 *buf = NULL;
const ut8 *buf = NULL;
int c, buf_len = 0;
int bsize = 0;
@ -62,13 +63,15 @@ int main(int argc, char **argv)
bsize = (int)r_num_math(NULL, optarg);
break;
case 's':
buf = optarg;
buf = (ut8*) optarg;
buf_len = strlen(optarg);
break;
case 'h':
return do_help(1);
}
}
if (optind<argc)
buf = r_file_slurp(argv[optind], &buf_len);
if (buf == NULL) {
do_help(0);

View File

@ -5,17 +5,17 @@
/* checksums */
/* XXX : crc16 should use 0 as arg0 by default */
ut16 r_hash_crc16(ut16 crc, const ut8 *buffer, ut64 len);
ut32 r_hash_crc32(const ut8 *buf, ut64 len);
ut8 r_hash_xor(const ut8 *b, ut64 len);
ut16 r_hash_xorpair(const ut8 *a, ut64 len);
ut8 r_hash_parity(ut8 *buf, ut64 len);
ut8 r_hash_mod255(const ut8 *b, ut64 len);
R_API ut16 r_hash_crc16(ut16 crc, const ut8 *buffer, ut64 len);
R_API ut32 r_hash_crc32(const ut8 *buf, ut64 len);
R_API ut8 r_hash_xor(const ut8 *b, ut64 len);
R_API ut16 r_hash_xorpair(const ut8 *a, ut64 len);
R_API ut8 r_hash_parity(ut8 *buf, ut64 len);
R_API ut8 r_hash_mod255(const ut8 *b, ut64 len);
/* analysis */
ut8 r_hash_hamdist(const ut8 *buf, ut64 len);
double r_hash_entropy(const ut8 *data, ut64 len);
int r_hash_pcprint(ut8 *buffer, ut64 len);
R_API ut8 r_hash_hamdist(const ut8 *buf, ut64 len);
R_API double r_hash_entropy(const ut8 *data, ut64 len);
R_API int r_hash_pcprint(ut8 *buffer, ut64 len);
/* hashing */
typedef struct {
@ -82,17 +82,20 @@ struct r_hash_t {
#define R_HASH_MOD255 16384
#define R_HASH_ALL 0xFFFF
const ut8 *r_hash_state_md4(struct r_hash_t *ctx, const ut8 *input, ut32 len);
const ut8 *r_hash_state_md5(struct r_hash_t *ctx, const ut8 *input, ut32 len);
const ut8 *r_hash_state_sha1(struct r_hash_t *ctx, const ut8 *input, ut32 len);
const ut8 *r_hash_state_sha256(struct r_hash_t *ctx, const ut8 *input, ut32 len);
const ut8 *r_hash_state_sha384(struct r_hash_t *ctx, const ut8 *input, ut32 len);
const ut8 *r_hash_state_sha512(struct r_hash_t *ctx, const ut8 *input, ut32 len);
R_API const ut8 *r_hash_state_md4(struct r_hash_t *ctx, const ut8 *input, ut32 len);
R_API const ut8 *r_hash_state_md5(struct r_hash_t *ctx, const ut8 *input, ut32 len);
R_API const ut8 *r_hash_state_sha1(struct r_hash_t *ctx, const ut8 *input, ut32 len);
R_API const ut8 *r_hash_state_sha256(struct r_hash_t *ctx, const ut8 *input, ut32 len);
R_API const ut8 *r_hash_state_sha384(struct r_hash_t *ctx, const ut8 *input, ut32 len);
R_API const ut8 *r_hash_state_sha512(struct r_hash_t *ctx, const ut8 *input, ut32 len);
/* OO */
struct r_hash_t *r_hash_state_new(int init);
void r_hash_init(struct r_hash_t *ptr, int flags);
void r_hash_state_init(struct r_hash_t *ctx, int flags);
void r_hash_state_free(struct r_hash_t *ctx);
R_API struct r_hash_t *r_hash_state_new(int init);
R_API void r_hash_init(struct r_hash_t *ptr, int flags);
R_API void r_hash_state_init(struct r_hash_t *ctx, int flags);
R_API void r_hash_state_free(struct r_hash_t *ctx);
R_API ut64 r_hash_name_to_bits(const char *name);
#endif

View File

@ -54,6 +54,12 @@ struct r_io_t {
struct list_head desc;
};
struct r_io_bind_t {
//int (*read_at)(void *user, );
int (*write_at)();
void *user;
};
//struct r_io_handle_fd_t {
// ... store io changes here
//};
@ -102,6 +108,7 @@ R_API int r_io_open(struct r_io_t *io, const char *file, int flags, int mode);
R_API int r_io_open_as(struct r_io_t *io, const char *urihandler, const char *file, int flags, int mode);
R_API int r_io_redirect(struct r_io_t *io, const char *file);
R_API int r_io_set_fd(struct r_io_t *io, int fd);
R_API struct r_buf_t *r_io_read_buf(struct r_io_t *io, ut64 addr, int len);
R_API int r_io_read(struct r_io_t *io, ut8 *buf, int len);
R_API int r_io_read_at(struct r_io_t *io, ut64 addr, ut8 *buf, int len);
R_API ut64 r_io_read_i(struct r_io_t *io, ut64 addr, int sz, int endian);
@ -112,6 +119,9 @@ R_API int r_io_system(struct r_io_t *io, const char *cmd);
R_API int r_io_close(struct r_io_t *io, int fd);
R_API ut64 r_io_size(struct r_io_t *io, int fd);
/* io/bind.c */
R_API int r_io_bind(struct r_io_t *io, struct r_io_bind_t *bnd);
/* io/map.c */
R_API void r_io_map_init(struct r_io_t *io);
R_API int r_io_map_add(struct r_io_t *io, int fd, int flags, ut64 delta, ut64 offset, ut64 size);

View File

@ -6,6 +6,12 @@
#include "list.h"
#include "iter.h"
struct r_buf_t {
ut8 *buf;
int length;
ut64 base;
};
/* r_cache */
// TOTHINK: move into a separated library?
struct r_cache_item_t {

View File

@ -9,7 +9,7 @@ STATIC_OBJS=
include ../config.mk
include ${STATIC_IO_PLUGINS}
STATIC_OBJS=$(subst ..,p/..,$(subst io_,p/io_,$(STATIC_OBJ)))
OBJ=${STATIC_OBJS} io.o handle.o map.o section.o desc.o
OBJ=${STATIC_OBJS} io.o handle.o map.o section.o desc.o bind.o
pre:
@echo STATICOBJS: ${STATIC_OBJS}

9
libr/io/bind.c Normal file
View File

@ -0,0 +1,9 @@
/* radare - LGPL - Copyright 2008-2009 pancake<nopcode.org> */
#include <r_io.h>
R_API int r_io_bind(struct r_io_t *io, struct r_io_bind_t *bnd)
{
return R_TRUE;
}

View File

@ -23,6 +23,22 @@ R_API struct r_io_t *r_io_new()
return r_io_init(io);
}
R_API struct r_buf_t *r_io_read_buf(struct r_io_t *io, ut64 addr, int len)
{
struct r_buf_t *b;
b = MALLOC_STRUCT(struct r_buf_t);
b->buf = malloc(len);
len = r_io_read_at(io, addr, b->buf, len);
if (len<0) len = 0;
b->length = len;
return b;
}
R_API int r_io_write_buf(struct r_io_t *io, struct r_buf_t *b)
{
return r_io_write_at(io, b->base, b->buf, b->length);
}
R_API struct r_io_t *r_io_free(struct r_io_t *io)
{
/* TODO: properly free inner nfo */

View File

@ -186,13 +186,15 @@ int r_macro_cmd_args(struct r_macro_t *mac, const char *ptr, const char *args, i
{
int i,j;
char *cmd = alloca(strlen(ptr)+1024);
char *arg = strdup(args);
cmd[0]='\0';
// eprintf("call(%s)\n", ptr);
for(i=j=0;ptr[j];i++,j++) {
if (ptr[j]=='$') {
if (ptr[j+1]>='0' && ptr[j+1]<='9') {
const char *word = r_str_word_get0(args, ptr[j+1]-'0');
char *word = r_str_word_get0(arg, ptr[j+1]-'0');
// TODO: use r_str_concat ??
strcat(cmd, word);
j++;
i = strlen(cmd)-1;
@ -214,9 +216,8 @@ int r_macro_cmd_args(struct r_macro_t *mac, const char *ptr, const char *args, i
}
while(*cmd==' '||*cmd=='\t')
cmd = cmd + 1;
if (*cmd==')')
return 0;
return mac->cmd(mac->user, cmd);
free(arg);
return (*cmd==')')?0:mac->cmd(mac->user, cmd);
}
char *r_macro_label_process(struct r_macro_t *mac, struct r_macro_label_t *labels, int *labels_n, char *ptr)

View File

@ -4,8 +4,8 @@ include ../../rules.mk
all: test test-str test-regexp rafind2
rafind2:
${CC} -g -I ../../include rafind2.c ${LDFLAGS} -o rafind2
rafind2: rafind2.o
${CC} -g -I ../../include rafind2.o ${LDFLAGS} -o rafind2
test:
${CC} -g -I ../../include test.c ${LDFLAGS} -o test

View File

@ -11,13 +11,14 @@ TODO:
#include <getopt.h>
#include <r_types.h>
#include <r_print.h>
#include <r_search.h>
#include <r_util.h>
#include <r_cons.h>
#include <r_lib.h>
#include <r_io.h>
static struct r_io_t io;
static int hexdump = 0;
static int fd = -1;
static int rad = 0;
struct r_search_t *rs;
@ -27,21 +28,22 @@ static char *mask = "";
static int nonstop = 0;
static int mode = R_SEARCH_KEYWORD;
static ut64 cur = 0;
static char *buffer = NULL;
static ut8 *buffer = NULL;
static char *curfile = NULL;
static ut64 bsize = 4096;
static int hexstr = 0;
static struct r_print_t *pr = NULL;
static int hit(struct r_search_kw_t *kw, void *user, ut64 addr)
{
const ut8 *buf = (ut8*)user;
//const ut8 *buf = (ut8*)user;
int delta = addr-cur;
if (rad) {
printf("f hit%d_%d 0x%08llx ; %s\n", 0, kw->count, addr, curfile);
} else {
printf("==> HIT %d AT 0x%llx ; %s\n", kw->count, addr, curfile);
if (hexdump) {
r_print_hexdump(addr, buffer+delta, 16, 78, R_TRUE);
printf("%s: %03d @ 0x%llx\n", curfile, kw->count, addr);
if (pr) {
r_print_hexdump(pr, addr, (ut8*)buffer+delta, 78, 16, R_TRUE);
r_cons_flush();
}
}
@ -67,7 +69,8 @@ static int show_help(char *argv0, int line)
int radiff_open(char *file)
{
int last = 0;
int ret, last = 0;
r_io_init(&io);
// TODO: add support for multiple files
fd = r_io_open(&io, file, R_IO_READ, 0);
@ -90,14 +93,14 @@ int radiff_open(char *file)
}
curfile = file;
r_search_begin(rs);
r_io_lseek(&io, fd, from, R_IO_SEEK_SET);
r_io_seek(&io, from, R_IO_SEEK_SET);
//printf("; %s 0x%08llx-0x%08llx\n", file, from, to);
for(cur=from; !last && cur<to;cur+=bsize) {
if ((cur+bsize)>to) {
bsize = to-cur;
last=1;
}
int ret = r_io_read(&io, fd, buffer, bsize);
ret = r_io_read(&io, buffer, bsize);
if (ret == 0) {
if (nonstop) continue;
// fprintf(stderr, "Error reading at 0x%08llx\n", cur);
@ -108,6 +111,7 @@ int radiff_open(char *file)
r_search_update_i(rs, cur, buffer, bsize);
}
rs = r_search_free(rs);
return 0;
}
int main(int argc, char **argv)
@ -149,7 +153,7 @@ int main(int argc, char **argv)
to = r_num_math(NULL, optarg);
break;
case 'X':
hexdump = 1;
pr = r_print_new();
break;
case 'h':
return show_help(argv[0], 0);

View File

@ -1,5 +1,5 @@
NAME=r_util
OBJ=mem.o num.o str.o re.o hex.o file.o alloca.o float.o prof.o cache.o sys.o btree.o iter.o
OBJ=mem.o num.o str.o re.o hex.o file.o alloca.o float.o prof.o cache.o sys.o btree.o iter.o buf.o
#CFLAGS+=-O2
#CC=gcc-4.1.1

55
libr/util/buf.c Normal file
View File

@ -0,0 +1,55 @@
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
#include "r_types.h"
#include "r_util.h"
R_API struct r_buf_t *r_buf_init(struct r_buf_t *b)
{
b->length = 0;
b->base = 0LL;
b->buf = NULL;
return b;
}
R_API struct r_buf_t *r_buf_new()
{
struct r_buf_t *b = MALLOC_STRUCT(struct r_buf_t);
return r_buf_init(b);
}
R_API void r_buf_set_bytes(struct r_buf_t *b, ut8 *buf, int length)
{
free(b->buf);
b->buf = malloc(length);
memcpy(b->buf, buf, length);
b->length = length;
}
static int r_buf_memcpy(struct r_buf_t *b, ut64 addr, ut8 *dst, ut8 *src, int len)
{
int end;
addr -= b->base;
if (addr > b->length)
return -1;
end = (int)(addr+len);
if (end > b->length)
len -= end-b->length;
memcpy(dst, src, len);
return len;
}
R_API int r_buf_read_at(struct r_buf_t *b, ut64 addr, ut8 *buf, int len)
{
return r_buf_memcpy(b, addr, buf, b->buf, len);
}
R_API int r_buf_write_at(struct r_buf_t *b, ut64 addr, ut8 *buf, int len)
{
return r_buf_memcpy(b, addr, b->buf, buf, len);
}
R_API void r_buf_free(struct r_buf_t *b)
{
free(b->buf);
free(b);
}

View File

@ -154,6 +154,11 @@ R_API const char *r_str_chop_ro(const char *str)
return str;
}
R_API char *r_str_new(char *str)
{
return strdup(str);
}
R_API char *r_str_chop(char *str)
{
int len;