+/* The lowest-level function to extract bits from a byte buffer.
+ SOURCE is the buffer. It is updated if we read to the end of a
+ byte.
+ SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
+ updated to reflect the number of bits actually read.
+ NBITS is the number of bits we want to read. It is updated to
+ reflect the number of bits actually read. This function may read
+ fewer bits.
+ BITS_BIG_ENDIAN is taken directly from gdbarch.
+ This function returns the extracted bits. */
+
+static unsigned int
+extract_bits_primitive (const gdb_byte **source,
+ unsigned int *source_offset_bits,
+ int *nbits, int bits_big_endian)
+{
+ unsigned int avail, mask, datum;
+
+ gdb_assert (*source_offset_bits < 8);
+
+ avail = 8 - *source_offset_bits;
+ if (avail > *nbits)
+ avail = *nbits;
+
+ mask = (1 << avail) - 1;
+ datum = **source;
+ if (bits_big_endian)
+ datum >>= 8 - (*source_offset_bits + *nbits);
+ else
+ datum >>= *source_offset_bits;
+ datum &= mask;
+
+ *nbits -= avail;
+ *source_offset_bits += avail;
+ if (*source_offset_bits >= 8)
+ {
+ *source_offset_bits -= 8;
+ ++*source;
+ }
+
+ return datum;
+}
+
+/* Extract some bits from a source buffer and move forward in the
+ buffer.
+
+ SOURCE is the source buffer. It is updated as bytes are read.
+ SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
+ bits are read.
+ NBITS is the number of bits to read.
+ BITS_BIG_ENDIAN is taken directly from gdbarch.
+
+ This function returns the bits that were read. */
+
+static unsigned int
+extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
+ int nbits, int bits_big_endian)
+{
+ unsigned int datum;
+
+ gdb_assert (nbits > 0 && nbits <= 8);
+
+ datum = extract_bits_primitive (source, source_offset_bits, &nbits,
+ bits_big_endian);
+ if (nbits > 0)
+ {
+ unsigned int more;
+
+ more = extract_bits_primitive (source, source_offset_bits, &nbits,
+ bits_big_endian);
+ if (bits_big_endian)
+ datum <<= nbits;
+ else
+ more <<= nbits;
+ datum |= more;
+ }
+
+ return datum;
+}
+
+/* Write some bits into a buffer and move forward in the buffer.
+
+ DATUM is the bits to write. The low-order bits of DATUM are used.
+ DEST is the destination buffer. It is updated as bytes are
+ written.
+ DEST_OFFSET_BITS is the bit offset in DEST at which writing is
+ done.
+ NBITS is the number of valid bits in DATUM.
+ BITS_BIG_ENDIAN is taken directly from gdbarch. */
+
+static void
+insert_bits (unsigned int datum,
+ gdb_byte *dest, unsigned int dest_offset_bits,
+ int nbits, int bits_big_endian)
+{
+ unsigned int mask;
+
+ gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
+
+ mask = (1 << nbits) - 1;
+ if (bits_big_endian)
+ {
+ datum <<= 8 - (dest_offset_bits + nbits);
+ mask <<= 8 - (dest_offset_bits + nbits);
+ }
+ else
+ {
+ datum <<= dest_offset_bits;
+ mask <<= dest_offset_bits;
+ }
+
+ gdb_assert ((datum & ~mask) == 0);
+
+ *dest = (*dest & ~mask) | datum;
+}
+
+/* Copy bits from a source to a destination.
+
+ DEST is where the bits should be written.
+ DEST_OFFSET_BITS is the bit offset into DEST.
+ SOURCE is the source of bits.
+ SOURCE_OFFSET_BITS is the bit offset into SOURCE.
+ BIT_COUNT is the number of bits to copy.
+ BITS_BIG_ENDIAN is taken directly from gdbarch. */
+
+static void
+copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
+ const gdb_byte *source, unsigned int source_offset_bits,
+ unsigned int bit_count,
+ int bits_big_endian)
+{
+ unsigned int dest_avail;
+ int datum;
+
+ /* Reduce everything to byte-size pieces. */
+ dest += dest_offset_bits / 8;
+ dest_offset_bits %= 8;
+ source += source_offset_bits / 8;
+ source_offset_bits %= 8;
+
+ dest_avail = 8 - dest_offset_bits % 8;
+
+ /* See if we can fill the first destination byte. */
+ if (dest_avail < bit_count)
+ {
+ datum = extract_bits (&source, &source_offset_bits, dest_avail,
+ bits_big_endian);
+ insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
+ ++dest;
+ dest_offset_bits = 0;
+ bit_count -= dest_avail;
+ }
+
+ /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
+ than 8 bits remaining. */
+ gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
+ for (; bit_count >= 8; bit_count -= 8)
+ {
+ datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
+ *dest++ = (gdb_byte) datum;
+ }
+
+ /* Finally, we may have a few leftover bits. */
+ gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
+ if (bit_count > 0)
+ {
+ datum = extract_bits (&source, &source_offset_bits, bit_count,
+ bits_big_endian);
+ insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
+ }
+}
+