1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
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. *
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. *
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 #ifndef BINARYBUFFER_H
24 #define BINARYBUFFER_H
29 * Support functions to access arbitrary bits in a byte array
33 * Sets @c num bits in @c _buffer, starting at the @c first bit,
34 * using the bits in @c value. This routine fast-paths writes
35 * of little-endian, byte-aligned, 32-bit words.
36 * @param _buffer The buffer whose bits will be set.
37 * @param first The bit offset in @c _buffer to start writing (0-31).
38 * @param num The number of bits from @c value to copy (1-32).
39 * @param value Up to 32 bits that will be copied to _buffer.
41 static inline void buf_set_u32(void *_buffer,
42 unsigned first, unsigned num, uint32_t value)
44 char *buffer = (char *)_buffer;
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;
51 for (unsigned i = first; i < first + num; i++)
53 if (((value >> (i - first)) & 1) == 1)
54 buffer[i / 8] |= 1 << (i % 8);
56 buffer[i / 8] &= ~(1 << (i % 8));
61 * Retrieves @c num bits from @c _buffer, starting at the @c first bit,
62 * returning the bits in a 32-bit word. This routine fast-paths reads
63 * of little-endian, byte-aligned, 32-bit words.
64 * @param _buffer The buffer whose bits will be read.
65 * @param first The bit offset in @c _buffer to start reading (0-31).
66 * @param num The number of bits from @c _buffer to read (1-32).
67 * @returns Up to 32-bits that were read from @c _buffer.
69 static inline uint32_t buf_get_u32(const void *_buffer,
70 unsigned first, unsigned num)
72 char *buffer = (char *)_buffer;
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);
80 for (unsigned i = first; i < first + num; i++)
82 if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
83 result |= 1 << (i - first);
90 * Inverts the ordering of bits inside a 32-bit word (e.g. 31..0 -> 0..31).
91 * This routine can be used to flip smaller data types by using smaller
92 * values for @c width.
93 * @param value The word to flip.
94 * @param width The number of bits in value (2-32).
95 * @returns A 32-bit word with @c value in reversed bit-order.
97 uint32_t flip_u32(uint32_t value, unsigned width);
99 bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
100 bool buf_cmp_mask(const void *buf1, const void *buf2,
101 const void *mask, unsigned size);
104 * Copies @c size bits out of @c from and into @c to. Any extra
105 * bits in the final byte will be set to zero.
106 * @param from The buffer to copy into @c to.
107 * @param to The buffer that will receive the copy of @c from.
108 * @param size The number of bits to copy.
110 void* buf_cpy(const void *from, void *to, unsigned size);
113 * Set the contents of @c buf with @c count bits, all set to 1.
114 * @param buf The buffer to fill with ones.
115 * @param size The number of bits.
116 * @returns The original buffer (@c buf).
118 void* buf_set_ones(void *buf, unsigned size);
120 void* buf_set_buf(const void *src, unsigned src_start,
121 void *dst, unsigned dst_start, unsigned len);
123 int str_to_buf(const char *str, unsigned len,
124 void *bin_buf, unsigned buf_size, unsigned radix);
125 char* buf_to_str(const void *buf, unsigned size, unsigned radix);
127 #define CEIL(m, n) (((m) + (n) - 1) / (n))
129 /* read a uint32_t from a buffer in target memory endianness */
130 static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
132 return le ? le_to_h_u32(p) : be_to_h_u32(p);
135 #endif /* BINARYBUFFER_H */