2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
10 #include "./bitreader.h"
13 #include "vpx_ports/mem.h"
14 #include "vpx_mem/vpx_mem.h"
16 int vpx_reader_init(vpx_reader
*r
,
17 const uint8_t *buffer
,
19 vpx_decrypt_cb decrypt_cb
,
20 void *decrypt_state
) {
21 if (size
&& !buffer
) {
24 r
->buffer_end
= buffer
+ size
;
29 r
->decrypt_cb
= decrypt_cb
;
30 r
->decrypt_state
= decrypt_state
;
32 return vpx_read_bit(r
) != 0; // marker bit
36 void vpx_reader_fill(vpx_reader
*r
) {
37 const uint8_t *const buffer_end
= r
->buffer_end
;
38 const uint8_t *buffer
= r
->buffer
;
39 const uint8_t *buffer_start
= buffer
;
40 BD_VALUE value
= r
->value
;
42 int shift
= BD_VALUE_SIZE
- CHAR_BIT
- (count
+ CHAR_BIT
);
44 const size_t bytes_left
= buffer_end
- buffer
;
45 const size_t bits_left
= bytes_left
* CHAR_BIT
;
46 const int x
= (int)(shift
+ CHAR_BIT
- bits_left
);
49 size_t n
= MIN(sizeof(r
->clear_buffer
), bytes_left
);
50 r
->decrypt_cb(r
->decrypt_state
, buffer
, r
->clear_buffer
, (int)n
);
51 buffer
= r
->clear_buffer
;
52 buffer_start
= r
->clear_buffer
;
56 count
+= LOTS_OF_BITS
;
60 if (x
< 0 || bits_left
) {
61 while (shift
>= loop_end
) {
63 value
|= (BD_VALUE
)*buffer
++ << shift
;
68 // NOTE: Variable 'buffer' may not relate to 'r->buffer' after decryption,
69 // so we increase 'r->buffer' by the amount that 'buffer' moved, rather than
70 // assign 'buffer' to 'r->buffer'.
71 r
->buffer
+= buffer
- buffer_start
;
76 const uint8_t *vpx_reader_find_end(vpx_reader
*r
) {
77 // Find the end of the coded buffer
78 while (r
->count
> CHAR_BIT
&& r
->count
< BD_VALUE_SIZE
) {