11 #ifndef OPENOCD_HELPER_BINARYBUFFER_H
12 #define OPENOCD_HELPER_BINARYBUFFER_H
17 #define ERROR_INVALID_NUMBER (-1700)
18 #define ERROR_NUMBER_EXCEEDS_BUFFER (-1701)
35 unsigned int first,
unsigned int num, uint32_t value)
37 assert(num >= 1 && num <= 32);
40 if ((num == 32) && (first == 0)) {
41 buffer[3] = (value >> 24) & 0xff;
42 buffer[2] = (value >> 16) & 0xff;
43 buffer[1] = (value >> 8) & 0xff;
44 buffer[0] = (value >> 0) & 0xff;
46 for (
unsigned int i = first; i < first + num; i++) {
47 if (((value >> (i - first)) & 1) == 1)
48 buffer[i / 8] |= 1 << (i % 8);
50 buffer[i / 8] &= ~(1 << (i % 8));
66 unsigned int first,
unsigned int num, uint64_t value)
68 assert(num >= 1 && num <= 64);
71 if ((num == 32) && (first == 0)) {
72 buffer[3] = (value >> 24) & 0xff;
73 buffer[2] = (value >> 16) & 0xff;
74 buffer[1] = (value >> 8) & 0xff;
75 buffer[0] = (value >> 0) & 0xff;
76 }
else if ((num == 64) && (first == 0)) {
77 buffer[7] = (value >> 56) & 0xff;
78 buffer[6] = (value >> 48) & 0xff;
79 buffer[5] = (value >> 40) & 0xff;
80 buffer[4] = (value >> 32) & 0xff;
81 buffer[3] = (value >> 24) & 0xff;
82 buffer[2] = (value >> 16) & 0xff;
83 buffer[1] = (value >> 8) & 0xff;
84 buffer[0] = (value >> 0) & 0xff;
86 for (
unsigned int i = first; i < first + num; i++) {
87 if (((value >> (i - first)) & 1) == 1)
88 buffer[i / 8] |= 1 << (i % 8);
90 buffer[i / 8] &= ~(1 << (i % 8));
105 unsigned int first,
unsigned int num)
107 assert(num >= 1 && num <= 32);
108 const uint8_t *
buffer = _buffer;
110 if ((num == 32) && (first == 0)) {
111 return (((uint32_t)
buffer[3]) << 24) |
112 (((uint32_t)
buffer[2]) << 16) |
113 (((uint32_t)
buffer[1]) << 8) |
114 (((uint32_t)
buffer[0]) << 0);
117 for (
unsigned int i = first; i < first + num; i++) {
118 if (((
buffer[i / 8] >> (i % 8)) & 1) == 1)
119 result |= 1U << (i - first);
135 unsigned int first,
unsigned int num)
137 assert(num >= 1 && num <= 64);
138 const uint8_t *
buffer = _buffer;
140 if ((num == 32) && (first == 0)) {
141 return 0 + ((((uint32_t)
buffer[3]) << 24) |
142 (((uint32_t)
buffer[2]) << 16) |
143 (((uint32_t)
buffer[1]) << 8) |
144 (((uint32_t)
buffer[0]) << 0));
145 }
else if ((num == 64) && (first == 0)) {
146 return 0 + ((((uint64_t)
buffer[7]) << 56) |
147 (((uint64_t)
buffer[6]) << 48) |
148 (((uint64_t)
buffer[5]) << 40) |
149 (((uint64_t)
buffer[4]) << 32) |
150 (((uint64_t)
buffer[3]) << 24) |
151 (((uint64_t)
buffer[2]) << 16) |
152 (((uint64_t)
buffer[1]) << 8) |
153 (((uint64_t)
buffer[0]) << 0));
156 for (
unsigned int i = first; i < first + num; i++) {
157 if (((
buffer[i / 8] >> (i % 8)) & 1) == 1)
158 result = result | ((uint64_t)1 << (uint64_t)(i - first));
175 bool buf_eq(
const void *buf1,
const void *buf2,
unsigned int size);
176 bool buf_eq_mask(
const void *buf1,
const void *buf2,
177 const void *
mask,
unsigned int size);
186 void *
buf_cpy(
const void *from,
void *to,
unsigned int size);
196 void *
buf_set_buf(
const void *src,
unsigned int src_start,
197 void *dst,
unsigned int dst_start,
unsigned int len);
208 int str_to_buf(
const char *str,
void *_buf,
unsigned int buf_bitsize);
218 static inline void bit_copy(uint8_t *dst,
unsigned int dst_offset,
const uint8_t *src,
219 unsigned int src_offset,
unsigned int bit_count)
221 buf_set_buf(src, src_offset, dst, dst_offset, bit_count);
239 unsigned int src_offset,
unsigned int bit_count);
245 size_t unhexify(uint8_t *bin,
const char *hex,
size_t count);
246 size_t hexify(
char *hex,
const uint8_t *bin,
size_t count,
size_t out_maxlen);
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
uint32_t flip_u32(uint32_t value, unsigned int width)
Inverts the ordering of bits inside a 32-bit word (e.g.
bool buf_eq_mask(const void *buf1, const void *buf2, const void *mask, unsigned int size)
bool buf_eq(const void *buf1, const void *buf2, unsigned int size)
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
void bit_copy_queue_init(struct bit_copy_queue *q)
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
void bit_copy_discard(struct bit_copy_queue *q)
void bit_copy_execute(struct bit_copy_queue *q)
void * buf_set_buf(const void *src, unsigned int src_start, void *dst, unsigned int dst_start, unsigned int len)
static uint64_t buf_get_u64(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 64-bit word.
char * buf_to_hex_str(const void *buf, unsigned int size)
void * buf_set_ones(void *buf, unsigned int size)
Set the contents of buf with count bits, all set to 1.
int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize)
Parse an unsigned number (provided as a zero-terminated string) into a bit buffer whose size is buf_l...
void * buf_cpy(const void *from, void *to, unsigned int size)
Copies size bits out of from and into to.
static uint32_t fast_target_buffer_get_u32(const void *p, bool le)
static void buf_set_u64(uint8_t *_buffer, unsigned int first, unsigned int num, uint64_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
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)
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
void buffer_shr(void *_buf, unsigned int buf_len, unsigned int count)
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t out_maxlen)
Convert binary data into a string of hexadecimal pairs.
size_t size
Size of the control block search area.
static uint32_t be_to_h_u32(const uint8_t *buf)
static uint32_t le_to_h_u32(const uint8_t *buf)