Debian Codegraph @

src/bitreader.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2000, 2001, 2002, 2003 Håkan Hjort <[email protected]>
3 *
4 * This file is part of libdvdread.
5 *
6 * libdvdread is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * libdvdread is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <inttypes.h>
26 #include <assert.h>
27
28 #include "dvdread/bitreader.h"
29
30 int dvdread_getbits_init(getbits_state_t *state, const uint8_t *start) {
31 if ((state == NULL) || (start == NULL)) return 0;
32 state->start = start;
33 state->bit_position = 0;
34 state->byte_position = 0;
35 return 1;
36 }
37
38 /* Non-optimized getbits. */
39 /* This can easily be optimized for particular platforms. */
40 uint32_t dvdread_getbits(getbits_state_t *state, uint32_t number_of_bits) {
41 uint32_t result=0;
42 uint8_t byte=0;
43 if (number_of_bits > 32) {
44 printf("Number of bits > 32 in getbits\n");
45 abort();
46 }
47
48 if ((state->bit_position) > 0) { /* Last getbits left us in the middle of a byte. */
49 if (number_of_bits > (8-state->bit_position)) { /* this getbits will span 2 or more bytes. */
50 byte = state->start[state->byte_position] << state->bit_position;
51 byte = byte >> (state->bit_position);
52 result = byte;
53 number_of_bits -= (8-state->bit_position);
54 state->bit_position = 0;
55 state->byte_position++;
56 } else {
57 byte = state->start[state->byte_position] << state->bit_position;
58 byte = byte >> (8 - number_of_bits);
59 result = byte;
60 state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 8 */
61 if (state->bit_position == 8) {
62 state->bit_position = 0;
63 state->byte_position++;
64 }
65 number_of_bits = 0;
66 }
67 }
68 if ((state->bit_position) == 0) {
69 while (number_of_bits > 7) {
70 result = (result << 8) + state->start[state->byte_position];
71 state->byte_position++;
72 number_of_bits -= 8;
73 }
74 if (number_of_bits > 0) { /* number_of_bits < 8 */
75 byte = state->start[state->byte_position] << state->bit_position;
76 state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 7 */
77 byte = byte >> (8 - number_of_bits);
78 result = (result << number_of_bits) + byte;
79 number_of_bits = 0;
80 }
81 }
82
83 return result;
84 }
85
86 #if 0 /* TODO: optimized versions not yet used */
87
88 /* WARNING: This function can only be used on a byte boundary.
89 No checks are made that we are in fact on a byte boundary.
90 */
91 uint16_t dvdread_get16bits(getbits_state_t *state) {
92 uint16_t result;
93 state->byte_position++;
94 result = (state->byte << 8) + state->start[state->byte_position++];
95 state->byte = state->start[state->byte_position];
96 return result;
97 }
98
99 /* WARNING: This function can only be used on a byte boundary.
100 No checks are made that we are in fact on a byte boundary.
101 */
102 uint32_t dvdread_get32bits(getbits_state_t *state) {
103 uint32_t result;
104 state->byte_position++;
105 result = (state->byte << 8) + state->start[state->byte_position++];
106 result = (result << 8) + state->start[state->byte_position++];
107 result = (result << 8) + state->start[state->byte_position++];
108 state->byte = state->start[state->byte_position];
109 return result;
110 }
111
112 #endif
113
114 #ifdef BITREADER_TESTS
115
116 int main()
117 {
118 uint8_t buff[2] = {
119 0x6E, 0xC2
120 // 0b 01101110 11000010
121 };
122 getbits_state_t state;
123 dvdread_getbits_init(&state, buff);
124
125 uint32_t bits = dvdread_getbits(&state, 3);
126 assert(bits == 3);
127
128 bits = dvdread_getbits(&state, 3);
129 assert(bits == 3);
130
131 bits = dvdread_getbits(&state, 4);
132 assert(bits == 11);
133
134 bits = dvdread_getbits(&state, 6);
135 assert(bits == 2);
136
137 dvdread_getbits_init(&state, buff);
138 bits = dvdread_getbits(&state, 10);
139 assert(bits == 443);
140
141 bits = dvdread_getbits(&state, 6);
142 assert(bits == 2);
143
144 dvdread_getbits_init(&state, buff);
145 bits = dvdread_getbits(&state, 16);
146 assert(bits == 28354);
147
148 buff[0] = buff[1] = 0xFF;
149 dvdread_getbits_init(&state, buff);
150 bits = dvdread_getbits(&state, 16);
151 assert(bits == 0xFFFF);
152
153 uint8_t large[5] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
154 dvdread_getbits_init(&state, large);
155 bits = dvdread_getbits(&state, 8);
156 assert(bits == 0xFF);
157 bits = dvdread_getbits(&state, 32);
158 assert(bits == 0xFFFFFFFF);
159
160 return 0;
161 }
162
163 #endif