3
3
// Hold functions to read streams on a bit or byte oriented basis
4
4
// plus some data related helper functions.
5
5
6
+ #ifndef DISABLE_RUST
7
+ extern uint64_t ccxr_next_bits (struct bitstream * bs , uint32_t bnum );
8
+ extern uint64_t ccxr_read_bits (struct bitstream * bs , uint32_t bnum );
9
+ extern int ccxr_skip_bits (struct bitstream * bs , uint32_t bnum );
10
+ extern int ccxr_is_byte_aligned (struct bitstream * bs );
11
+ extern void ccxr_make_byte_aligned (struct bitstream * bs );
12
+ extern const uint8_t * ccxr_next_bytes (struct bitstream * bs , size_t bynum );
13
+ extern const uint8_t * ccxr_read_bytes (struct bitstream * bs , size_t bynum );
14
+ extern uint64_t ccxr_read_exp_golomb_unsigned (struct bitstream * bs );
15
+ extern int64_t ccxr_read_exp_golomb (struct bitstream * bs );
16
+ extern uint8_t ccxr_reverse8 (uint8_t data );
17
+ extern uint64_t ccxr_bitstream_get_num (struct bitstream * bs , unsigned bytes , int advance );
18
+ extern int64_t ccxr_read_int (struct bitstream * bs , unsigned bnum );
19
+ #endif
20
+
6
21
// Guidelines for all bitsream functions:
7
22
// * No function shall advance the pointer past the end marker
8
23
// * If bitstream.bitsleft < 0 do not attempt any read access,
@@ -35,6 +50,9 @@ int init_bitstream(struct bitstream *bstr, unsigned char *start, unsigned char *
35
50
// there are not enough bits left in the bitstream.
36
51
uint64_t next_bits (struct bitstream * bstr , unsigned bnum )
37
52
{
53
+ #ifndef DISABLE_RUST
54
+ return ccxr_next_bits (bstr , bnum );
55
+ #else
38
56
uint64_t res = 0 ;
39
57
40
58
if (bnum > 64 )
@@ -99,12 +117,16 @@ uint64_t next_bits(struct bitstream *bstr, unsigned bnum)
99
117
bstr -> _i_pos = vpos ;
100
118
101
119
return res ;
120
+ #endif
102
121
}
103
122
104
123
// Read bnum bits from bitstream bstr with the most significant
105
124
// bit read first. A 64 bit unsigned integer is returned.
106
125
uint64_t read_bits (struct bitstream * bstr , unsigned bnum )
107
126
{
127
+ #ifndef DISABLE_RUST
128
+ return ccxr_read_bits (bstr , bnum );
129
+ #else
108
130
uint64_t res = next_bits (bstr , bnum );
109
131
110
132
// Special case for reading zero bits. Also abort when not enough
@@ -117,13 +139,17 @@ uint64_t read_bits(struct bitstream *bstr, unsigned bnum)
117
139
bstr -> pos = bstr -> _i_pos ;
118
140
119
141
return res ;
142
+ #endif
120
143
}
121
144
122
145
// This function will advance the bitstream by bnum bits, if possible.
123
146
// Advancing of more than 64 bits is possible.
124
147
// Return TRUE when successful, otherwise FALSE
125
148
int skip_bits (struct bitstream * bstr , unsigned bnum )
126
149
{
150
+ #ifndef DISABLE_RUST
151
+ return ccxr_skip_bits (bstr , bnum );
152
+ #else
127
153
// Sanity check
128
154
if (bstr -> end - bstr -> pos < 0 )
129
155
fatal (CCX_COMMON_EXIT_BUG_BUG , "In skip_bits: bitstream length cannot be negative!" );
@@ -140,7 +166,7 @@ int skip_bits(struct bitstream *bstr, unsigned bnum)
140
166
if (bstr -> bitsleft < 0 )
141
167
return 0 ;
142
168
143
- // Special case for reading zero bits. Return zero
169
+ // Special case for reading zero bits. Return one == success
144
170
if (bnum == 0 )
145
171
return 1 ;
146
172
@@ -153,13 +179,17 @@ int skip_bits(struct bitstream *bstr, unsigned bnum)
153
179
bstr -> pos += 1 ;
154
180
}
155
181
return 1 ;
182
+ #endif
156
183
}
157
184
158
185
// Return TRUE if the current position in the bitstream is on a byte
159
186
// boundary, i.e., the next bit in the bitstream is the first bit in
160
187
// a byte, otherwise return FALSE
161
188
int is_byte_aligned (struct bitstream * bstr )
162
189
{
190
+ #ifndef DISABLE_RUST
191
+ return ccxr_is_byte_aligned (bstr );
192
+ #else
163
193
// Sanity check
164
194
if (bstr -> end - bstr -> pos < 0 )
165
195
fatal (CCX_COMMON_EXIT_BUG_BUG , "In is_byte_aligned: bitstream length can not be negative!" );
@@ -175,11 +205,15 @@ int is_byte_aligned(struct bitstream *bstr)
175
205
return 1 ;
176
206
else
177
207
return 0 ;
208
+ #endif
178
209
}
179
210
180
211
// Move bitstream to next byte border. Adjust bitsleft.
181
212
void make_byte_aligned (struct bitstream * bstr )
182
213
{
214
+ #ifndef DISABLE_RUST
215
+ ccxr_make_byte_aligned (bstr );
216
+ #else
183
217
// Sanity check
184
218
if (bstr -> end - bstr -> pos < 0 )
185
219
fatal (CCX_COMMON_EXIT_BUG_BUG , "In make_byte_aligned: bitstream length can not be negative!" );
@@ -208,6 +242,7 @@ void make_byte_aligned(struct bitstream *bstr)
208
242
bstr -> bitsleft = 0LL + 8 * (bstr -> end - bstr -> pos - 1 ) + bstr -> bpos ;
209
243
210
244
return ;
245
+ #endif
211
246
}
212
247
213
248
// Return pointer to first of bynum bytes from the bitstream if the
@@ -217,6 +252,9 @@ void make_byte_aligned(struct bitstream *bstr)
217
252
// This function does not advance the bitstream pointer.
218
253
unsigned char * next_bytes (struct bitstream * bstr , unsigned bynum )
219
254
{
255
+ #ifndef DISABLE_RUST
256
+ return (unsigned char * )ccxr_next_bytes (bstr , bynum );
257
+ #else
220
258
// Sanity check
221
259
if (bstr -> end - bstr -> pos < 0 )
222
260
fatal (CCX_COMMON_EXIT_BUG_BUG , "In next_bytes: bitstream length can not be negative!" );
@@ -238,6 +276,7 @@ unsigned char *next_bytes(struct bitstream *bstr, unsigned bynum)
238
276
bstr -> _i_pos = bstr -> pos + bynum ;
239
277
240
278
return bstr -> pos ;
279
+ #endif
241
280
}
242
281
243
282
// Return pointer to first of bynum bytes from the bitstream if the
@@ -247,6 +286,9 @@ unsigned char *next_bytes(struct bitstream *bstr, unsigned bynum)
247
286
// This function does advance the bitstream pointer.
248
287
unsigned char * read_bytes (struct bitstream * bstr , unsigned bynum )
249
288
{
289
+ #ifndef DISABLE_RUST
290
+ return (unsigned char * )ccxr_read_bytes (bstr , bynum );
291
+ #else
250
292
unsigned char * res = next_bytes (bstr , bynum );
251
293
252
294
// Advance the bitstream when a read was possible
@@ -256,6 +298,7 @@ unsigned char *read_bytes(struct bitstream *bstr, unsigned bynum)
256
298
bstr -> pos = bstr -> _i_pos ;
257
299
}
258
300
return res ;
301
+ #endif
259
302
}
260
303
261
304
// Return an integer number with "bytes" precision from the current
@@ -266,6 +309,9 @@ unsigned char *read_bytes(struct bitstream *bstr, unsigned bynum)
266
309
// little-endian and big-endian CPUs.
267
310
uint64_t bitstream_get_num (struct bitstream * bstr , unsigned bytes , int advance )
268
311
{
312
+ #ifndef DISABLE_RUST
313
+ return ccxr_bitstream_get_num (bstr , bytes , advance );
314
+ #else
269
315
void * bpos ;
270
316
uint64_t rval = 0 ;
271
317
@@ -296,11 +342,15 @@ uint64_t bitstream_get_num(struct bitstream *bstr, unsigned bytes, int advance)
296
342
rval = (rval << 8 ) + uc ;
297
343
}
298
344
return rval ;
345
+ #endif
299
346
}
300
347
301
348
// Read unsigned Exp-Golomb code from bitstream
302
349
uint64_t read_exp_golomb_unsigned (struct bitstream * bstr )
303
350
{
351
+ #ifndef DISABLE_RUST
352
+ return ccxr_read_exp_golomb_unsigned (bstr );
353
+ #else
304
354
uint64_t res = 0 ;
305
355
int zeros = 0 ;
306
356
@@ -310,11 +360,15 @@ uint64_t read_exp_golomb_unsigned(struct bitstream *bstr)
310
360
res = (0x01 << zeros ) - 1 + read_bits (bstr , zeros );
311
361
312
362
return res ;
363
+ #endif
313
364
}
314
365
315
366
// Read signed Exp-Golomb code from bitstream
316
367
int64_t read_exp_golomb (struct bitstream * bstr )
317
368
{
369
+ #ifndef DISABLE_RUST
370
+ return ccxr_read_exp_golomb (bstr );
371
+ #else
318
372
int64_t res = 0 ;
319
373
320
374
res = read_exp_golomb_unsigned (bstr );
@@ -325,6 +379,7 @@ int64_t read_exp_golomb(struct bitstream *bstr)
325
379
res = (res / 2 + (res % 2 ? 1 : 0 )) * (res % 2 ? 1 : -1 );
326
380
327
381
return res ;
382
+ #endif
328
383
}
329
384
330
385
// Read unsigned integer with bnum bits length. Basically an
@@ -337,18 +392,25 @@ uint64_t read_int_unsigned(struct bitstream *bstr, unsigned bnum)
337
392
// Read signed integer with bnum bits length.
338
393
int64_t read_int (struct bitstream * bstr , unsigned bnum )
339
394
{
395
+ #ifndef DISABLE_RUST
396
+ return ccxr_read_int (bstr , bnum );
397
+ #else
340
398
uint64_t res = read_bits (bstr , bnum );
341
399
342
400
// Special case for reading zero bits. Return zero
343
401
if (bnum == 0 )
344
402
return 0 ;
345
403
346
404
return (0xFFFFFFFFFFFFFFFFULL << bnum ) | res ;
405
+ #endif
347
406
}
348
407
349
408
// Return the value with the bit order reversed.
350
409
uint8_t reverse8 (uint8_t data )
351
410
{
411
+ #ifndef DISABLE_RUST
412
+ return ccxr_reverse8 (data );
413
+ #else
352
414
uint8_t res = 0 ;
353
415
354
416
for (int k = 0 ; k < 8 ; k ++ )
@@ -358,4 +420,5 @@ uint8_t reverse8(uint8_t data)
358
420
}
359
421
360
422
return res ;
423
+ #endif
361
424
}
0 commit comments