Пример #1
0
//ret 0 or -1 if failed; ret copied length if success
R_API int r_buf_write_at(RBuffer *b, ut64 addr, const ut8 *buf, int len) {
	if (!b || !buf || len < 1) {
		return 0;
	}
	if (b->fd != -1) {
		ut64 newlen = addr + len;
		if (r_sandbox_lseek (b->fd, addr, SEEK_SET) == -1) {
			return 0;
		}
		if (newlen > b->length) {
			b->length = newlen;
#ifdef _MSC_VER
			_chsize (b->fd, newlen);
#else
			ftruncate (b->fd, newlen);
#endif
		}
		return r_sandbox_write (b->fd, buf, len);
	}
	if (b->sparse) {
		return (sparse_write (b->sparse, addr, buf, len) < 0) ? -1 : len;
	}
	if (b->empty) {
		b->empty = 0;
		free (b->buf);
		b->buf = (ut8 *) malloc (addr + len);
	}
	return r_buf_cpy (b, addr, b->buf, buf, len, true);
}
Пример #2
0
// ret copied length if successful, -1 if failed
static int r_buf_cpy(RBuffer *b, ut64 addr, ut8 *dst, const ut8 *src, int len, int write) {
	int end;
	if (!b || b->empty) {
		return 0;
	}
	if (b->fd != -1) {
		if (r_sandbox_lseek (b->fd, addr, SEEK_SET) == -1) {
			// seek failed - print error here?
			// return 0;
		}
		if (write) {
			return r_sandbox_write (b->fd, src, len);
		}
		memset (dst, 0, len);
		return r_sandbox_read (b->fd, dst, len);
	}
	if (b->sparse) {
		if (write) {
			// create new with src + len
			if (sparse_write (b->sparse, addr, src, len) < 0) {
				return -1;
			}
		} else {
			// read from sparse and write into dst
			memset (dst, 0xff, len);
			if (sparse_read (b->sparse, addr, dst, len) < 0) {
				return -1;
			}
		}
		return len;
	}
	addr = (addr == R_BUF_CUR) ? b->cur : addr - b->base;
	if (len < 1 || !dst || addr > b->length) {
		return -1;
	}
 	end = (int)(addr + len);
	if (end > b->length) {
		len -= end-b->length;
	}
	if (write) {
		dst += addr;
	} else {
		src += addr;
	}
	memmove (dst, src, len);
	b->cur = addr + len;
	return len;
}
Пример #3
0
R_API bool r_buf_append_bytes(RBuffer *b, const ut8 *buf, int length) {
	if (!b) return false;
	if (b->fd != -1) {
		r_sandbox_lseek (b->fd, 0, SEEK_END);
		r_sandbox_write (b->fd, buf, length);
		return true;
	}
	if (b->empty) b->length = b->empty = 0;
	if (!(b->buf = realloc (b->buf, 1 + b->length + length))) {
		return false;
	}
	memmove (b->buf+b->length, buf, length);
	b->buf[b->length+length] = 0;
	b->length += length;
	return true;
}
Пример #4
0
R_API bool r_buf_append_nbytes(RBuffer *b, int length) {
	if (!b) return false;
	if (b->fd != -1) {
		ut8 *buf = calloc (1, length);
		if (buf) {
			r_sandbox_lseek (b->fd, 0, SEEK_END);
			r_sandbox_write (b->fd, buf, length);
			free (buf);
			return true;
		}
		return false;
	}
	if (b->empty) b->length = b->empty = 0;
	if (!(b->buf = realloc (b->buf, b->length+length)))
		return false;
	memset (b->buf+b->length, 0, length);
	b->length += length;
	return true;
}