build: add helper/types.h to config.h
[openocd.git] / src / helper / binarybuffer.h
1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #ifndef BINARYBUFFER_H
25 #define BINARYBUFFER_H
26
27 /** @file
28 * Support functions to access arbitrary bits in a byte array
29 */
30
31 /**
32 * Sets @c num bits in @c _buffer, starting at the @c first bit,
33 * using the bits in @c value. This routine fast-paths writes
34 * of little-endian, byte-aligned, 32-bit words.
35 * @param _buffer The buffer whose bits will be set.
36 * @param first The bit offset in @c _buffer to start writing (0-31).
37 * @param num The number of bits from @c value to copy (1-32).
38 * @param value Up to 32 bits that will be copied to _buffer.
39 */
40 static inline void buf_set_u32(void *_buffer,
41 unsigned first, unsigned num, uint32_t value)
42 {
43 uint8_t *buffer = (uint8_t *)_buffer;
44
45 if ((num == 32) && (first == 0)) {
46 buffer[3] = (value >> 24) & 0xff;
47 buffer[2] = (value >> 16) & 0xff;
48 buffer[1] = (value >> 8) & 0xff;
49 buffer[0] = (value >> 0) & 0xff;
50 } else {
51 for (unsigned i = first; i < first + num; i++) {
52 if (((value >> (i - first)) & 1) == 1)
53 buffer[i / 8] |= 1 << (i % 8);
54 else
55 buffer[i / 8] &= ~(1 << (i % 8));
56 }
57 }
58 }
59 /**
60 * Retrieves @c num bits from @c _buffer, starting at the @c first bit,
61 * returning the bits in a 32-bit word. This routine fast-paths reads
62 * of little-endian, byte-aligned, 32-bit words.
63 * @param _buffer The buffer whose bits will be read.
64 * @param first The bit offset in @c _buffer to start reading (0-31).
65 * @param num The number of bits from @c _buffer to read (1-32).
66 * @returns Up to 32-bits that were read from @c _buffer.
67 */
68 static inline uint32_t buf_get_u32(const void *_buffer,
69 unsigned first, unsigned num)
70 {
71 uint8_t *buffer = (uint8_t *)_buffer;
72
73 if ((num == 32) && (first == 0)) {
74 return (((uint32_t)buffer[3]) << 24) |
75 (((uint32_t)buffer[2]) << 16) |
76 (((uint32_t)buffer[1]) << 8) |
77 (((uint32_t)buffer[0]) << 0);
78 } else {
79 uint32_t result = 0;
80 for (unsigned i = first; i < first + num; i++) {
81 if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
82 result |= 1 << (i - first);
83 }
84 return result;
85 }
86 }
87
88 /**
89 * Inverts the ordering of bits inside a 32-bit word (e.g. 31..0 -> 0..31).
90 * This routine can be used to flip smaller data types by using smaller
91 * values for @c width.
92 * @param value The word to flip.
93 * @param width The number of bits in value (2-32).
94 * @returns A 32-bit word with @c value in reversed bit-order.
95 */
96 uint32_t flip_u32(uint32_t value, unsigned width);
97
98 bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
99 bool buf_cmp_mask(const void *buf1, const void *buf2,
100 const void *mask, unsigned size);
101
102 /**
103 * Copies @c size bits out of @c from and into @c to. Any extra
104 * bits in the final byte will be set to zero.
105 * @param from The buffer to copy into @c to.
106 * @param to The buffer that will receive the copy of @c from.
107 * @param size The number of bits to copy.
108 */
109 void *buf_cpy(const void *from, void *to, unsigned size);
110
111 /**
112 * Set the contents of @c buf with @c count bits, all set to 1.
113 * @param buf The buffer to fill with ones.
114 * @param size The number of bits.
115 * @returns The original buffer (@c buf).
116 */
117 void *buf_set_ones(void *buf, unsigned size);
118
119 void *buf_set_buf(const void *src, unsigned src_start,
120 void *dst, unsigned dst_start, unsigned len);
121
122 int str_to_buf(const char *str, unsigned len,
123 void *bin_buf, unsigned buf_size, unsigned radix);
124 char *buf_to_str(const void *buf, unsigned size, unsigned radix);
125
126 /* read a uint32_t from a buffer in target memory endianness */
127 static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
128 {
129 return le ? le_to_h_u32(p) : be_to_h_u32(p);
130 }
131
132 #endif /* BINARYBUFFER_H */