Coverage Report

Created: 2025-10-10 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtheora/lib/bitpack.c
Line
Count
Source
1
/********************************************************************
2
 *                                                                  *
3
 * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE.   *
4
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7
 *                                                                  *
8
 * THE OggTheora SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
9
 * by the Xiph.Org Foundation and contributors                      *
10
 * https://www.xiph.org/                                            *
11
 *                                                                  *
12
 ********************************************************************
13
14
  function: packing variable sized words into an octet stream
15
16
 ********************************************************************/
17
#include <string.h>
18
#include <stdlib.h>
19
#include "bitpack.h"
20
21
/*We're 'MSb' endian; if we write a word but read individual bits,
22
   then we'll read the MSb first.*/
23
24
6.04k
void oc_pack_readinit(oc_pack_buf *_b,unsigned char *_buf,long _bytes){
25
6.04k
  memset(_b,0,sizeof(*_b));
26
6.04k
  _b->ptr=_buf;
27
6.04k
  _b->stop=_buf+_bytes;
28
6.04k
}
29
30
345k
static oc_pb_window oc_pack_refill(oc_pack_buf *_b,int _bits){
31
345k
  const unsigned char *ptr;
32
345k
  const unsigned char *stop;
33
345k
  oc_pb_window         window;
34
345k
  int                  available;
35
345k
  unsigned             shift;
36
345k
  stop=_b->stop;
37
345k
  ptr=_b->ptr;
38
345k
  window=_b->window;
39
345k
  available=_b->bits;
40
345k
  shift=OC_PB_WINDOW_SIZE-available;
41
3.04M
  while(7<shift&&ptr<stop){
42
2.69M
    shift-=8;
43
2.69M
    window|=(oc_pb_window)*ptr++<<shift;
44
2.69M
  }
45
345k
  _b->ptr=ptr;
46
345k
  available=OC_PB_WINDOW_SIZE-shift;
47
345k
  if(_bits>available){
48
2.40k
    if(ptr>=stop){
49
2.40k
      _b->eof=1;
50
2.40k
      available=OC_LOTS_OF_BITS;
51
2.40k
    }
52
0
    else window|=*ptr>>(available&7);
53
2.40k
  }
54
345k
  _b->bits=available;
55
345k
  return window;
56
345k
}
57
58
0
int oc_pack_look1(oc_pack_buf *_b){
59
0
  oc_pb_window window;
60
0
  int          available;
61
0
  window=_b->window;
62
0
  available=_b->bits;
63
0
  if(available<1)_b->window=window=oc_pack_refill(_b,1);
64
0
  return window>>OC_PB_WINDOW_SIZE-1;
65
0
}
66
67
0
void oc_pack_adv1(oc_pack_buf *_b){
68
0
  _b->window<<=1;
69
0
  _b->bits--;
70
0
}
71
72
/*Here we assume that 0<=_bits&&_bits<=32.*/
73
51.2M
long oc_pack_read_c(oc_pack_buf *_b,int _bits){
74
51.2M
  oc_pb_window window;
75
51.2M
  int          available;
76
51.2M
  long         result;
77
51.2M
  window=_b->window;
78
51.2M
  available=_b->bits;
79
51.2M
  if(_bits==0)return 0;
80
51.1M
  if(available<_bits){
81
343k
    window=oc_pack_refill(_b,_bits);
82
343k
    available=_b->bits;
83
343k
  }
84
51.1M
  result=window>>OC_PB_WINDOW_SIZE-_bits;
85
51.1M
  available-=_bits;
86
51.1M
  window<<=1;
87
51.1M
  window<<=_bits-1;
88
51.1M
  _b->window=window;
89
51.1M
  _b->bits=available;
90
51.1M
  return result;
91
51.2M
}
92
93
110k
int oc_pack_read1_c(oc_pack_buf *_b){
94
110k
  oc_pb_window window;
95
110k
  int          available;
96
110k
  int          result;
97
110k
  window=_b->window;
98
110k
  available=_b->bits;
99
110k
  if(available<1){
100
2.14k
    window=oc_pack_refill(_b,1);
101
2.14k
    available=_b->bits;
102
2.14k
  }
103
110k
  result=window>>OC_PB_WINDOW_SIZE-1;
104
110k
  available--;
105
110k
  window<<=1;
106
110k
  _b->window=window;
107
110k
  _b->bits=available;
108
110k
  return result;
109
110k
}
110
111
114k
long oc_pack_bytes_left(oc_pack_buf *_b){
112
114k
  if(_b->eof)return -1;
113
114k
  return _b->stop-_b->ptr+(_b->bits>>3);
114
114k
}