openocd: revert workarounds for 'expr' syntax change
[openocd.git] / src / helper / binarybuffer.h
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 "list.h"
15
16 /** @file
17 * Support functions to access arbitrary bits in a byte array
18 */
19
20 /**
21 * Sets @c num bits in @c _buffer, starting at the @c first bit,
22 * using the bits in @c value. This routine fast-paths writes
23 * of little-endian, byte-aligned, 32-bit words.
24 * @param _buffer The buffer whose bits will be set.
25 * Do not use uninitialized buffer or clang static analyzer emits a warning.
26 * @param first The bit offset in @c _buffer to start writing (0-31).
27 * @param num The number of bits from @c value to copy (1-32).
28 * @param value Up to 32 bits that will be copied to _buffer.
29 */
30 static inline void buf_set_u32(uint8_t *_buffer,
31 unsigned first, unsigned num, uint32_t value)
32 {
33 uint8_t *buffer = _buffer;
34
35 if ((num == 32) && (first == 0)) {
36 buffer[3] = (value >> 24) & 0xff;
37 buffer[2] = (value >> 16) & 0xff;
38 buffer[1] = (value >> 8) & 0xff;
39 buffer[0] = (value >> 0) & 0xff;
40 } else {
41 for (unsigned i = first; i < first + num; i++) {
42 if (((value >> (i - first)) & 1) == 1)
43 buffer[i / 8] |= 1 << (i % 8);
44 else
45 buffer[i / 8] &= ~(1 << (i % 8));
46 }
47 }
48 }
49
50 /**
51 * Sets @c num bits in @c _buffer, starting at the @c first bit,
52 * using the bits in @c value. This routine fast-paths writes
53 * of little-endian, byte-aligned, 64-bit words.
54 * @param _buffer The buffer whose bits will be set.
55 * Do not use uninitialized buffer or clang static analyzer emits a warning.
56 * @param first The bit offset in @c _buffer to start writing (0-63).
57 * @param num The number of bits from @c value to copy (1-64).
58 * @param value Up to 64 bits that will be copied to _buffer.
59 */
60 static inline void buf_set_u64(uint8_t *_buffer,
61 unsigned first, unsigned num, uint64_t value)
62 {
63 uint8_t *buffer = _buffer;
64
65 if ((num == 32) && (first == 0)) {
66 buffer[3] = (value >> 24) & 0xff;
67 buffer[2] = (value >> 16) & 0xff;
68 buffer[1] = (value >> 8) & 0xff;
69 buffer[0] = (value >> 0) & 0xff;
70 } else if ((num == 64) && (first == 0)) {
71 buffer[7] = (value >> 56) & 0xff;
72 buffer[6] = (value >> 48) & 0xff;
73 buffer[5] = (value >> 40) & 0xff;
74 buffer[4] = (value >> 32) & 0xff;
75 buffer[3] = (value >> 24) & 0xff;
76 buffer[2] = (value >> 16) & 0xff;
77 buffer[1] = (value >> 8) & 0xff;
78 buffer[0] = (value >> 0) & 0xff;
79 } else {
80 for (unsigned i = first; i < first + num; i++) {
81 if (((value >> (i - first)) & 1) == 1)
82 buffer[i / 8] |= 1 << (i % 8);
83 else
84 buffer[i / 8] &= ~(1 << (i % 8));
85 }
86 }
87 }
88
89 /**
90 * Retrieves @c num bits from @c _buffer, starting at the @c first bit,
91 * returning the bits in a 32-bit word. This routine fast-paths reads
92 * of little-endian, byte-aligned, 32-bit words.
93 * @param _buffer The buffer whose bits will be read.
94 * @param first The bit offset in @c _buffer to start reading (0-31).
95 * @param num The number of bits from @c _buffer to read (1-32).
96 * @returns Up to 32-bits that were read from @c _buffer.
97 */
98 static inline uint32_t buf_get_u32(const uint8_t *_buffer,
99 unsigned first, unsigned num)
100 {
101 const uint8_t *buffer = _buffer;
102
103 if ((num == 32) && (first == 0)) {
104 return (((uint32_t)buffer[3]) << 24) |
105 (((uint32_t)buffer[2]) << 16) |
106 (((uint32_t)buffer[1]) << 8) |
107 (((uint32_t)buffer[0]) << 0);
108 } else {
109 uint32_t result = 0;
110 for (unsigned i = first; i < first + num; i++) {
111 if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
112 result |= 1U << (i - first);
113 }
114 return result;
115 }
116 }
117
118 /**
119 * Retrieves @c num bits from @c _buffer, starting at the @c first bit,
120 * returning the bits in a 64-bit word. This routine fast-paths reads
121 * of little-endian, byte-aligned, 64-bit words.
122 * @param _buffer The buffer whose bits will be read.
123 * @param first The bit offset in @c _buffer to start reading (0-63).
124 * @param num The number of bits from @c _buffer to read (1-64).
125 * @returns Up to 64-bits that were read from @c _buffer.
126 */
127 static inline uint64_t buf_get_u64(const uint8_t *_buffer,
128 unsigned first, unsigned num)
129 {
130 const uint8_t *buffer = _buffer;
131
132 if ((num == 32) && (first == 0)) {
133 return 0 + ((((uint32_t)buffer[3]) << 24) | /* Note - zero plus is to avoid a checkpatch bug */
134 (((uint32_t)buffer[2]) << 16) |
135 (((uint32_t)buffer[1]) << 8) |
136 (((uint32_t)buffer[0]) << 0));
137 } else if ((num == 64) && (first == 0)) {
138 return 0 + ((((uint64_t)buffer[7]) << 56) | /* Note - zero plus is to avoid a checkpatch bug */
139 (((uint64_t)buffer[6]) << 48) |
140 (((uint64_t)buffer[5]) << 40) |
141 (((uint64_t)buffer[4]) << 32) |
142 (((uint64_t)buffer[3]) << 24) |
143 (((uint64_t)buffer[2]) << 16) |
144 (((uint64_t)buffer[1]) << 8) |
145 (((uint64_t)buffer[0]) << 0));
146 } else {
147 uint64_t result = 0;
148 for (unsigned i = first; i < first + num; i++) {
149 if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
150 result = result | ((uint64_t)1 << (uint64_t)(i - first));
151 }
152 return result;
153 }
154 }
155
156
157 /**
158 * Inverts the ordering of bits inside a 32-bit word (e.g. 31..0 -> 0..31).
159 * This routine can be used to flip smaller data types by using smaller
160 * values for @c width.
161 * @param value The word to flip.
162 * @param width The number of bits in value (2-32).
163 * @returns A 32-bit word with @c value in reversed bit-order.
164 */
165 uint32_t flip_u32(uint32_t value, unsigned width);
166
167 bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
168 bool buf_cmp_mask(const void *buf1, const void *buf2,
169 const void *mask, unsigned size);
170
171 /**
172 * Copies @c size bits out of @c from and into @c to. Any extra
173 * bits in the final byte will be set to zero.
174 * @param from The buffer to copy into @c to.
175 * @param to The buffer that will receive the copy of @c from.
176 * @param size The number of bits to copy.
177 */
178 void *buf_cpy(const void *from, void *to, unsigned size);
179
180 /**
181 * Set the contents of @c buf with @c count bits, all set to 1.
182 * @param buf The buffer to fill with ones.
183 * @param size The number of bits.
184 * @returns The original buffer (@c buf).
185 */
186 void *buf_set_ones(void *buf, unsigned size);
187
188 void *buf_set_buf(const void *src, unsigned src_start,
189 void *dst, unsigned dst_start, unsigned len);
190
191 int str_to_buf(const char *str, unsigned len,
192 void *bin_buf, unsigned buf_size, unsigned radix);
193 char *buf_to_hex_str(const void *buf, unsigned size);
194
195 /* read a uint32_t from a buffer in target memory endianness */
196 static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
197 {
198 return le ? le_to_h_u32(p) : be_to_h_u32(p);
199 }
200
201 static inline void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src,
202 unsigned src_offset, unsigned bit_count)
203 {
204 buf_set_buf(src, src_offset, dst, dst_offset, bit_count);
205 }
206
207 struct bit_copy_queue {
208 struct list_head list;
209 };
210
211 struct bit_copy_queue_entry {
212 uint8_t *dst;
213 unsigned dst_offset;
214 const uint8_t *src;
215 unsigned src_offset;
216 unsigned bit_count;
217 struct list_head list;
218 };
219
220 void bit_copy_queue_init(struct bit_copy_queue *q);
221 int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned dst_offset, const uint8_t *src,
222 unsigned src_offset, unsigned bit_count);
223 void bit_copy_execute(struct bit_copy_queue *q);
224 void bit_copy_discard(struct bit_copy_queue *q);
225
226 /* functions to convert to/from hex encoded buffer
227 * used in ti-icdi driver and gdb server */
228 size_t unhexify(uint8_t *bin, const char *hex, size_t count);
229 size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t out_maxlen);
230 void buffer_shr(void *_buf, unsigned buf_len, unsigned count);
231
232 #endif /* OPENOCD_HELPER_BINARYBUFFER_H */

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)