aboutsummaryrefslogtreecommitdiff
path: root/src/helper/binarybuffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/helper/binarybuffer.h')
-rw-r--r--src/helper/binarybuffer.h70
1 files changed, 43 insertions, 27 deletions
diff --git a/src/helper/binarybuffer.h b/src/helper/binarybuffer.h
index 3446296..9913279 100644
--- a/src/helper/binarybuffer.h
+++ b/src/helper/binarybuffer.h
@@ -14,6 +14,9 @@
#include <helper/list.h>
#include <helper/types.h>
+#define ERROR_INVALID_NUMBER (-1700)
+#define ERROR_NUMBER_EXCEEDS_BUFFER (-1701)
+
/** @file
* Support functions to access arbitrary bits in a byte array
*/
@@ -29,8 +32,9 @@
* @param value Up to 32 bits that will be copied to _buffer.
*/
static inline void buf_set_u32(uint8_t *_buffer,
- unsigned first, unsigned num, uint32_t value)
+ unsigned int first, unsigned int num, uint32_t value)
{
+ assert(num >= 1 && num <= 32);
uint8_t *buffer = _buffer;
if ((num == 32) && (first == 0)) {
@@ -39,7 +43,7 @@ static inline void buf_set_u32(uint8_t *_buffer,
buffer[1] = (value >> 8) & 0xff;
buffer[0] = (value >> 0) & 0xff;
} else {
- for (unsigned i = first; i < first + num; i++) {
+ for (unsigned int i = first; i < first + num; i++) {
if (((value >> (i - first)) & 1) == 1)
buffer[i / 8] |= 1 << (i % 8);
else
@@ -59,8 +63,9 @@ static inline void buf_set_u32(uint8_t *_buffer,
* @param value Up to 64 bits that will be copied to _buffer.
*/
static inline void buf_set_u64(uint8_t *_buffer,
- unsigned first, unsigned num, uint64_t value)
+ unsigned int first, unsigned int num, uint64_t value)
{
+ assert(num >= 1 && num <= 64);
uint8_t *buffer = _buffer;
if ((num == 32) && (first == 0)) {
@@ -78,7 +83,7 @@ static inline void buf_set_u64(uint8_t *_buffer,
buffer[1] = (value >> 8) & 0xff;
buffer[0] = (value >> 0) & 0xff;
} else {
- for (unsigned i = first; i < first + num; i++) {
+ for (unsigned int i = first; i < first + num; i++) {
if (((value >> (i - first)) & 1) == 1)
buffer[i / 8] |= 1 << (i % 8);
else
@@ -97,8 +102,9 @@ static inline void buf_set_u64(uint8_t *_buffer,
* @returns Up to 32-bits that were read from @c _buffer.
*/
static inline uint32_t buf_get_u32(const uint8_t *_buffer,
- unsigned first, unsigned num)
+ unsigned int first, unsigned int num)
{
+ assert(num >= 1 && num <= 32);
const uint8_t *buffer = _buffer;
if ((num == 32) && (first == 0)) {
@@ -108,7 +114,7 @@ static inline uint32_t buf_get_u32(const uint8_t *_buffer,
(((uint32_t)buffer[0]) << 0);
} else {
uint32_t result = 0;
- for (unsigned i = first; i < first + num; i++) {
+ for (unsigned int i = first; i < first + num; i++) {
if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
result |= 1U << (i - first);
}
@@ -126,8 +132,9 @@ static inline uint32_t buf_get_u32(const uint8_t *_buffer,
* @returns Up to 64-bits that were read from @c _buffer.
*/
static inline uint64_t buf_get_u64(const uint8_t *_buffer,
- unsigned first, unsigned num)
+ unsigned int first, unsigned int num)
{
+ assert(num >= 1 && num <= 64);
const uint8_t *buffer = _buffer;
if ((num == 32) && (first == 0)) {
@@ -146,7 +153,7 @@ static inline uint64_t buf_get_u64(const uint8_t *_buffer,
(((uint64_t)buffer[0]) << 0));
} else {
uint64_t result = 0;
- for (unsigned i = first; i < first + num; i++) {
+ for (unsigned int i = first; i < first + num; i++) {
if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
result = result | ((uint64_t)1 << (uint64_t)(i - first));
}
@@ -163,11 +170,11 @@ static inline uint64_t buf_get_u64(const uint8_t *_buffer,
* @param width The number of bits in value (2-32).
* @returns A 32-bit word with @c value in reversed bit-order.
*/
-uint32_t flip_u32(uint32_t value, unsigned width);
+uint32_t flip_u32(uint32_t value, unsigned int width);
-bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
-bool buf_cmp_mask(const void *buf1, const void *buf2,
- const void *mask, unsigned size);
+bool buf_eq(const void *buf1, const void *buf2, unsigned int size);
+bool buf_eq_mask(const void *buf1, const void *buf2,
+ const void *mask, unsigned int size);
/**
* Copies @c size bits out of @c from and into @c to. Any extra
@@ -176,7 +183,7 @@ bool buf_cmp_mask(const void *buf1, const void *buf2,
* @param to The buffer that will receive the copy of @c from.
* @param size The number of bits to copy.
*/
-void *buf_cpy(const void *from, void *to, unsigned size);
+void *buf_cpy(const void *from, void *to, unsigned int size);
/**
* Set the contents of @c buf with @c count bits, all set to 1.
@@ -184,14 +191,23 @@ void *buf_cpy(const void *from, void *to, unsigned size);
* @param size The number of bits.
* @returns The original buffer (@c buf).
*/
-void *buf_set_ones(void *buf, unsigned size);
+void *buf_set_ones(void *buf, unsigned int size);
+
+void *buf_set_buf(const void *src, unsigned int src_start,
+ void *dst, unsigned int dst_start, unsigned int len);
-void *buf_set_buf(const void *src, unsigned src_start,
- void *dst, unsigned dst_start, unsigned len);
+/**
+ * Parse an unsigned number (provided as a zero-terminated string)
+ * into a bit buffer whose size is buf_len bits. The base of the
+ * number is detected between decimal, hexadecimal and octal.
+ * @param str Input number, zero-terminated string
+ * @param _buf Output buffer, allocated by the caller
+ * @param buf_bitsize Output buffer size in bits
+ * @returns Error on invalid or overflowing number
+ */
+int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize);
-int str_to_buf(const char *str, unsigned len,
- void *bin_buf, unsigned buf_size, unsigned radix);
-char *buf_to_hex_str(const void *buf, unsigned size);
+char *buf_to_hex_str(const void *buf, unsigned int size);
/* read a uint32_t from a buffer in target memory endianness */
static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
@@ -199,8 +215,8 @@ static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
return le ? le_to_h_u32(p) : be_to_h_u32(p);
}
-static inline void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src,
- unsigned src_offset, unsigned bit_count)
+static inline void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src,
+ unsigned int src_offset, unsigned int bit_count)
{
buf_set_buf(src, src_offset, dst, dst_offset, bit_count);
}
@@ -211,16 +227,16 @@ struct bit_copy_queue {
struct bit_copy_queue_entry {
uint8_t *dst;
- unsigned dst_offset;
+ unsigned int dst_offset;
const uint8_t *src;
- unsigned src_offset;
- unsigned bit_count;
+ unsigned int src_offset;
+ unsigned int bit_count;
struct list_head list;
};
void bit_copy_queue_init(struct bit_copy_queue *q);
-int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned dst_offset, const uint8_t *src,
- unsigned src_offset, unsigned bit_count);
+int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned int dst_offset, const uint8_t *src,
+ unsigned int src_offset, unsigned int bit_count);
void bit_copy_execute(struct bit_copy_queue *q);
void bit_copy_discard(struct bit_copy_queue *q);
@@ -228,6 +244,6 @@ void bit_copy_discard(struct bit_copy_queue *q);
* used in ti-icdi driver and gdb server */
size_t unhexify(uint8_t *bin, const char *hex, size_t count);
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t out_maxlen);
-void buffer_shr(void *_buf, unsigned buf_len, unsigned count);
+void buffer_shr(void *_buf, unsigned int buf_len, unsigned int count);
#endif /* OPENOCD_HELPER_BINARYBUFFER_H */