OpenOCD
binarybuffer.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2004, 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  ***************************************************************************/
10 
11 #ifndef OPENOCD_HELPER_BINARYBUFFER_H
12 #define OPENOCD_HELPER_BINARYBUFFER_H
13 
14 #include <helper/list.h>
15 #include <helper/types.h>
16 
17 #define ERROR_INVALID_NUMBER (-1700)
18 #define ERROR_NUMBER_EXCEEDS_BUFFER (-1701)
19 
34 static inline void buf_set_u32(uint8_t *_buffer,
35  unsigned int first, unsigned int num, uint32_t value)
36 {
37  assert(num >= 1 && num <= 32);
38  uint8_t *buffer = _buffer;
39 
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;
45  } else {
46  for (unsigned int i = first; i < first + num; i++) {
47  if (((value >> (i - first)) & 1) == 1)
48  buffer[i / 8] |= 1 << (i % 8);
49  else
50  buffer[i / 8] &= ~(1 << (i % 8));
51  }
52  }
53 }
54 
65 static inline void buf_set_u64(uint8_t *_buffer,
66  unsigned int first, unsigned int num, uint64_t value)
67 {
68  assert(num >= 1 && num <= 64);
69  uint8_t *buffer = _buffer;
70 
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;
85  } else {
86  for (unsigned int i = first; i < first + num; i++) {
87  if (((value >> (i - first)) & 1) == 1)
88  buffer[i / 8] |= 1 << (i % 8);
89  else
90  buffer[i / 8] &= ~(1 << (i % 8));
91  }
92  }
93 }
94 
104 static inline uint32_t buf_get_u32(const uint8_t *_buffer,
105  unsigned int first, unsigned int num)
106 {
107  assert(num >= 1 && num <= 32);
108  const uint8_t *buffer = _buffer;
109 
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);
115  } else {
116  uint32_t result = 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);
120  }
121  return result;
122  }
123 }
124 
134 static inline uint64_t buf_get_u64(const uint8_t *_buffer,
135  unsigned int first, unsigned int num)
136 {
137  assert(num >= 1 && num <= 64);
138  const uint8_t *buffer = _buffer;
139 
140  if ((num == 32) && (first == 0)) {
141  return 0 + ((((uint32_t)buffer[3]) << 24) | /* Note - zero plus is to avoid a checkpatch bug */
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) | /* Note - zero plus is to avoid a checkpatch bug */
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));
154  } else {
155  uint64_t result = 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));
159  }
160  return result;
161  }
162 }
163 
164 
173 uint32_t flip_u32(uint32_t value, unsigned int width);
174 
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);
178 
186 void *buf_cpy(const void *from, void *to, unsigned int size);
187 
194 void *buf_set_ones(void *buf, unsigned int size);
195 
196 void *buf_set_buf(const void *src, unsigned int src_start,
197  void *dst, unsigned int dst_start, unsigned int len);
198 
208 int str_to_buf(const char *str, void *_buf, unsigned int buf_bitsize);
209 
210 char *buf_to_hex_str(const void *buf, unsigned int size);
211 
212 /* read a uint32_t from a buffer in target memory endianness */
213 static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
214 {
215  return le ? le_to_h_u32(p) : be_to_h_u32(p);
216 }
217 
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)
220 {
221  buf_set_buf(src, src_offset, dst, dst_offset, bit_count);
222 }
223 
225  struct list_head list;
226 };
227 
229  uint8_t *dst;
230  unsigned int dst_offset;
231  const uint8_t *src;
232  unsigned int src_offset;
233  unsigned int bit_count;
234  struct list_head list;
235 };
236 
237 void bit_copy_queue_init(struct bit_copy_queue *q);
238 int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned int dst_offset, const uint8_t *src,
239  unsigned int src_offset, unsigned int bit_count);
240 void bit_copy_execute(struct bit_copy_queue *q);
241 void bit_copy_discard(struct bit_copy_queue *q);
242 
243 /* functions to convert to/from hex encoded buffer
244  * used in ti-icdi driver and gdb server */
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);
247 void buffer_shr(void *_buf, unsigned int buf_len, unsigned int count);
248 
249 #endif /* OPENOCD_HELPER_BINARYBUFFER_H */
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
Definition: binarybuffer.h:218
uint32_t flip_u32(uint32_t value, unsigned int width)
Inverts the ordering of bits inside a 32-bit word (e.g.
Definition: binarybuffer.c:165
bool buf_eq_mask(const void *buf1, const void *buf2, const void *mask, unsigned int size)
Definition: binarybuffer.c:87
bool buf_eq(const void *buf1, const void *buf2, unsigned int size)
Definition: binarybuffer.c:70
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.
Definition: binarybuffer.h:104
void bit_copy_queue_init(struct bit_copy_queue *q)
Definition: binarybuffer.c:287
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.
Definition: binarybuffer.h:34
void bit_copy_discard(struct bit_copy_queue *q)
Definition: binarybuffer.c:320
void bit_copy_execute(struct bit_copy_queue *q)
Definition: binarybuffer.c:309
void * buf_set_buf(const void *src, unsigned int src_start, void *dst, unsigned int dst_start, unsigned int len)
Definition: binarybuffer.c:120
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.
Definition: binarybuffer.h:134
char * buf_to_hex_str(const void *buf, unsigned int size)
Definition: binarybuffer.c:178
void * buf_set_ones(void *buf, unsigned int size)
Set the contents of buf with count bits, all set to 1.
Definition: binarybuffer.c:105
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...
Definition: binarybuffer.c:201
void * buf_cpy(const void *from, void *to, unsigned int size)
Copies size bits out of from and into to.
Definition: binarybuffer.c:43
static uint32_t fast_target_buffer_get_u32(const void *p, bool le)
Definition: binarybuffer.h:213
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.
Definition: binarybuffer.h:65
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)
Definition: binarybuffer.c:292
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
Definition: binarybuffer.c:342
void buffer_shr(void *_buf, unsigned int buf_len, unsigned int count)
Definition: binarybuffer.c:398
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.
Definition: binarybuffer.c:380
unsigned short width
Definition: embeddedice.c:47
int mask
Definition: esirisc.c:1741
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
Definition: binarybuffer.h:228
uint8_t * dst
Definition: binarybuffer.h:229
unsigned int dst_offset
Definition: binarybuffer.h:230
struct list_head list
Definition: binarybuffer.h:234
unsigned int bit_count
Definition: binarybuffer.h:233
const uint8_t * src
Definition: binarybuffer.h:231
unsigned int src_offset
Definition: binarybuffer.h:232
struct list_head list
Definition: binarybuffer.h:225
Definition: list.h:40
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
uint8_t count[4]
Definition: vdebug.c:22