Exemplo n.º 1
0
R_API int r_buf_seek (RBuffer *b, st64 addr, int whence) {
	ut64 min, max = 0LL;
	if (b->sparse) {
		sparse_limits (b->sparse, &min, &max);
		switch (whence) {
		case R_IO_SEEK_SET: b->cur = addr; break;
		case R_IO_SEEK_CUR: b->cur = b->cur + addr; break;
		case R_IO_SEEK_END: 
			    if (sparse_limits (b->sparse, NULL, &max)) {
				    return max; // -min
			    }
			    b->cur = max + addr; break; //b->base + b->length + addr; break;
		}
	} else {
		min = b->base;
		max = b->base + b->length;
		switch (whence) {
		//case 0: b->cur = b->base + addr; break;
		case R_IO_SEEK_SET: b->cur = addr; break;
		case R_IO_SEEK_CUR: b->cur = b->cur + addr; break;
		case R_IO_SEEK_END: b->cur = b->base + b->length + addr; break;
		}
	}
	/* avoid out-of-bounds */
	if (b->cur < min)
		b->cur = min;
	if (b->cur >= max)
		b->cur = max;
	return (int)b->cur;
}
Exemplo n.º 2
0
R_API ut64 r_buf_size (RBuffer *b) {
	if (!b) return 0LL;
	if (b->sparse) {
		ut64 max = 0LL;
		if (sparse_limits (b->sparse, NULL, &max)) {
			return max; // -min
		}
		return 0LL;
	}
	if (b->empty) return 0;
	else return b->length;
	return UT64_MAX;
}
Exemplo n.º 3
0
Arquivo: buf.c Projeto: P4N74/radare2
R_API int r_buf_seek (RBuffer *b, st64 addr, int whence) {
	ut64 min = 0LL, max = 0LL;
	if (b->fd != -1) {
		if (r_sandbox_lseek (b->fd, addr, whence) == -1) {
			// seek failed - print error here?
			return -1;
		}
	} else if (b->sparse) {
		sparse_limits (b->sparse, &min, &max);
		switch (whence) {
		case R_IO_SEEK_SET: b->cur = addr; break;
		case R_IO_SEEK_CUR: b->cur = b->cur + addr; break;
		case R_IO_SEEK_END:
			    if (sparse_limits (b->sparse, NULL, &max)) {
				    return max; // -min
			    }
			    b->cur = max + addr; break; //b->base + b->length + addr; break;
		}
	} else {
		min = b->base;
		max = b->base + b->length;
		switch (whence) {
		//case 0: b->cur = b->base + addr; break;
		case R_IO_SEEK_SET: b->cur = addr; break;
		case R_IO_SEEK_CUR: b->cur = b->cur + addr; break;
		case R_IO_SEEK_END: b->cur = b->base + b->length + addr; break;
		}
	}
	/* avoid out-of-bounds */
	if (b->cur < min) {
		b->cur = min;
	}
	if (b->cur >= max) {
		b->cur = max;
	}
	return (int)b->cur;
}
Exemplo n.º 4
0
Arquivo: buf.c Projeto: HKingz/radare2
R_API ut64 r_buf_size (RBuffer *b) {
	if (!b) return 0LL;
	if (b->fd != -1) {
		return b->length;
	}
	if (b->sparse) {
		ut64 max = 0LL;
eprintf ("GETTING SIZE\n");
		if (sparse_limits (b->sparse, NULL, &max)) {
			return max; // -min
		}
		return 0LL;
	}
	return b->empty? 0: b->length;
}
Exemplo n.º 5
0
Arquivo: buf.c Projeto: P4N74/radare2
R_API bool r_buf_resize (RBuffer *b, ut64 newsize) {
	if (b->mmap) {
		return false;
	}
	if ((!b->sparse && !b->buf) || newsize < 1) {
		return false;
	}
	if (b->sparse) {
		ut64 last_addr = 0;
		sparse_limits (b->sparse, 0, &last_addr);
		int buf_len = newsize - last_addr;
		if (buf_len > 0) {
			ut8 *buf = malloc (buf_len);
			if (buf) {
				memset (buf, 0xff, buf_len);
				sparse_write (b->sparse, last_addr, buf, buf_len);
				free (buf);
				return true;
			}
		}
		eprintf ("Invalid resize for an sparse RBuffer\n");
		return false;
	}
	ut8 *buf = calloc (newsize, 1);
	if (buf) {
		ut32 len = R_MIN (newsize, b->length);
		memcpy (buf, b->buf, len);
		memset (buf + len, 0xff, newsize - len);
		/* commit */
		free (b->buf);
		b->buf = buf;
		b->length = newsize;
		return true;
	}
	return false;
}