4 * Functions too long to declare as inline in decomp.h.
6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
10 * This library is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 2.1 of the License, or (at your option) any
15 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License along
20 * with this library; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 /* Reads @n bytes from the bitstream @stream into the location pointed to by @dest.
28 * The bitstream must be 16-bit aligned. */
29 int bitstream_read_bytes(struct input_bitstream *stream, size_t n, void *dest)
31 /* Precondition: The bitstream is 16-byte aligned. */
32 wimlib_assert(stream->bitsleft % 16 == 0);
36 /* Get the bytes currently in the buffer variable. */
37 while (stream->bitsleft != 0) {
40 *p++ = bitstream_peek_bits(stream, 8);
41 bitstream_remove_bits(stream, 8);
44 /* Get the rest directly from the pointer to the data. Of course, it's
45 * necessary to check there are really n bytes available. */
46 if (n > stream->data_bytes_left) {
47 ERROR("Unexpected end of input when "
48 "reading %zu bytes from bitstream "
49 "(only have %u bytes left)\n", n,
50 stream->data_bytes_left);
53 memcpy(p, stream->data, n);
55 stream->data_bytes_left -= n;
57 /* It's possible to copy an odd number of bytes and leave the stream in
58 * an inconsistent state. Fix it by reading the next byte, if it is
60 if ((n & 1) && stream->data_bytes_left != 0) {
62 stream->data_bytes_left--;
63 stream->bitbuf |= (input_bitbuf_t)(*stream->data) <<
64 (sizeof(input_bitbuf_t) * 8 - 8);
70 /* Aligns the bitstream on a 16-bit boundary.
72 * Note: M$'s idea of "alignment" means that for some reason, a 16-bit word
73 * should be skipped over if the buffer happens to already be aligned on such a
74 * boundary. This only applies for realigning the stream after the blocktype
75 * and length fields of an uncompressed block, however; it does not apply when
76 * realigning the stream after the end of the uncompressed block.
78 int align_input_bitstream(struct input_bitstream *stream,
79 bool skip_word_if_aligned)
82 if (stream->bitsleft % 16 != 0) {
83 bitstream_remove_bits(stream, stream->bitsleft % 16);
84 } else if (skip_word_if_aligned) {
85 if (stream->bitsleft == 0) {
86 ret = bitstream_ensure_bits(stream, 16);
88 ERROR("Unexpected end of input when "
89 "aligning bitstream!\n");
93 bitstream_remove_bits(stream, 16);