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 
31 static inline void buf_set_u32(uint8_t *_buffer,
32  unsigned first, unsigned num, uint32_t value)
33 {
34  uint8_t *buffer = _buffer;
35 
36  if ((num == 32) && (first == 0)) {
37  buffer[3] = (value >> 24) & 0xff;
38  buffer[2] = (value >> 16) & 0xff;
39  buffer[1] = (value >> 8) & 0xff;
40  buffer[0] = (value >> 0) & 0xff;
41  } else {
42  for (unsigned i = first; i < first + num; i++) {
43  if (((value >> (i - first)) & 1) == 1)
44  buffer[i / 8] |= 1 << (i % 8);
45  else
46  buffer[i / 8] &= ~(1 << (i % 8));
47  }
48  }
49 }
50 
61 static inline void buf_set_u64(uint8_t *_buffer,
62  unsigned first, unsigned num, uint64_t value)
63 {
64  uint8_t *buffer = _buffer;
65 
66  if ((num == 32) && (first == 0)) {
67  buffer[3] = (value >> 24) & 0xff;
68  buffer[2] = (value >> 16) & 0xff;
69  buffer[1] = (value >> 8) & 0xff;
70  buffer[0] = (value >> 0) & 0xff;
71  } else if ((num == 64) && (first == 0)) {
72  buffer[7] = (value >> 56) & 0xff;
73  buffer[6] = (value >> 48) & 0xff;
74  buffer[5] = (value >> 40) & 0xff;
75  buffer[4] = (value >> 32) & 0xff;
76  buffer[3] = (value >> 24) & 0xff;
77  buffer[2] = (value >> 16) & 0xff;
78  buffer[1] = (value >> 8) & 0xff;
79  buffer[0] = (value >> 0) & 0xff;
80  } else {
81  for (unsigned i = first; i < first + num; i++) {
82  if (((value >> (i - first)) & 1) == 1)
83  buffer[i / 8] |= 1 << (i % 8);
84  else
85  buffer[i / 8] &= ~(1 << (i % 8));
86  }
87  }
88 }
89 
99 static inline uint32_t buf_get_u32(const uint8_t *_buffer,
100  unsigned first, unsigned num)
101 {
102  const uint8_t *buffer = _buffer;
103 
104  if ((num == 32) && (first == 0)) {
105  return (((uint32_t)buffer[3]) << 24) |
106  (((uint32_t)buffer[2]) << 16) |
107  (((uint32_t)buffer[1]) << 8) |
108  (((uint32_t)buffer[0]) << 0);
109  } else {
110  uint32_t result = 0;
111  for (unsigned i = first; i < first + num; i++) {
112  if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
113  result |= 1U << (i - first);
114  }
115  return result;
116  }
117 }
118 
128 static inline uint64_t buf_get_u64(const uint8_t *_buffer,
129  unsigned first, unsigned num)
130 {
131  const uint8_t *buffer = _buffer;
132 
133  if ((num == 32) && (first == 0)) {
134  return 0 + ((((uint32_t)buffer[3]) << 24) | /* Note - zero plus is to avoid a checkpatch bug */
135  (((uint32_t)buffer[2]) << 16) |
136  (((uint32_t)buffer[1]) << 8) |
137  (((uint32_t)buffer[0]) << 0));
138  } else if ((num == 64) && (first == 0)) {
139  return 0 + ((((uint64_t)buffer[7]) << 56) | /* Note - zero plus is to avoid a checkpatch bug */
140  (((uint64_t)buffer[6]) << 48) |
141  (((uint64_t)buffer[5]) << 40) |
142  (((uint64_t)buffer[4]) << 32) |
143  (((uint64_t)buffer[3]) << 24) |
144  (((uint64_t)buffer[2]) << 16) |
145  (((uint64_t)buffer[1]) << 8) |
146  (((uint64_t)buffer[0]) << 0));
147  } else {
148  uint64_t result = 0;
149  for (unsigned i = first; i < first + num; i++) {
150  if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
151  result = result | ((uint64_t)1 << (uint64_t)(i - first));
152  }
153  return result;
154  }
155 }
156 
157 
166 uint32_t flip_u32(uint32_t value, unsigned width);
167 
168 bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
169 bool buf_cmp_mask(const void *buf1, const void *buf2,
170  const void *mask, unsigned size);
171 
179 void *buf_cpy(const void *from, void *to, unsigned size);
180 
187 void *buf_set_ones(void *buf, unsigned size);
188 
189 void *buf_set_buf(const void *src, unsigned src_start,
190  void *dst, unsigned dst_start, unsigned len);
191 
192 int str_to_buf(const char *str, unsigned len,
193  void *bin_buf, unsigned buf_size, unsigned radix);
194 char *buf_to_hex_str(const void *buf, unsigned size);
195 
196 /* read a uint32_t from a buffer in target memory endianness */
197 static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
198 {
199  return le ? le_to_h_u32(p) : be_to_h_u32(p);
200 }
201 
202 static inline void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src,
203  unsigned src_offset, unsigned bit_count)
204 {
205  buf_set_buf(src, src_offset, dst, dst_offset, bit_count);
206 }
207 
209  struct list_head list;
210 };
211 
213  uint8_t *dst;
214  unsigned dst_offset;
215  const uint8_t *src;
216  unsigned src_offset;
217  unsigned bit_count;
218  struct list_head list;
219 };
220 
221 void bit_copy_queue_init(struct bit_copy_queue *q);
222 int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned dst_offset, const uint8_t *src,
223  unsigned src_offset, unsigned bit_count);
224 void bit_copy_execute(struct bit_copy_queue *q);
225 void bit_copy_discard(struct bit_copy_queue *q);
226 
227 /* functions to convert to/from hex encoded buffer
228  * used in ti-icdi driver and gdb server */
229 size_t unhexify(uint8_t *bin, const char *hex, size_t count);
230 size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t out_maxlen);
231 void buffer_shr(void *_buf, unsigned buf_len, unsigned count);
232 
233 #endif /* OPENOCD_HELPER_BINARYBUFFER_H */
uint32_t flip_u32(uint32_t value, unsigned width)
Inverts the ordering of bits inside a 32-bit word (e.g.
void bit_copy_queue_init(struct bit_copy_queue *q)
Definition: binarybuffer.c:299
void * buf_set_ones(void *buf, unsigned size)
Set the contents of buf with count bits, all set to 1.
Definition: binarybuffer.c:106
void bit_copy_discard(struct bit_copy_queue *q)
Definition: binarybuffer.c:332
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)
Definition: binarybuffer.c:304
void bit_copy_execute(struct bit_copy_queue *q)
Definition: binarybuffer.c:321
static void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src, unsigned src_offset, unsigned bit_count)
Definition: binarybuffer.h:202
bool buf_cmp_mask(const void *buf1, const void *buf2, const void *mask, unsigned size)
Definition: binarybuffer.c:87
bool buf_cmp(const void *buf1, const void *buf2, unsigned size)
Definition: binarybuffer.c:70
static void buf_set_u64(uint8_t *_buffer, unsigned first, unsigned num, uint64_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:61
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:99
void buffer_shr(void *_buf, unsigned buf_len, unsigned count)
Definition: binarybuffer.c:410
int str_to_buf(const char *str, unsigned len, void *bin_buf, unsigned buf_size, unsigned radix)
Definition: binarybuffer.c:233
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:31
static uint32_t fast_target_buffer_get_u32(const void *p, bool le)
Definition: binarybuffer.h:197
static uint64_t buf_get_u64(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 64-bit word.
Definition: binarybuffer.h:128
void * buf_set_buf(const void *src, unsigned src_start, void *dst, unsigned dst_start, unsigned len)
Definition: binarybuffer.c:121
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:354
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:392
char * buf_to_hex_str(const void *buf, unsigned size)
Definition: binarybuffer.c:192
void * buf_cpy(const void *from, void *to, unsigned size)
Copies size bits out of from and into to.
Definition: binarybuffer.c:43
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:212
uint8_t * dst
Definition: binarybuffer.h:213
unsigned src_offset
Definition: binarybuffer.h:216
unsigned dst_offset
Definition: binarybuffer.h:214
struct list_head list
Definition: binarybuffer.h:218
unsigned bit_count
Definition: binarybuffer.h:217
const uint8_t * src
Definition: binarybuffer.h:215
struct list_head list
Definition: binarybuffer.h:209
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