-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathputbits.c
executable file
·90 lines (77 loc) · 1.57 KB
/
putbits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "misc.h"
/* private data */
static unsigned char outbfr;
static int outcnt;
static int bytecnt;
FILE *_o_stream;
/* initialize buffer, call once before first putbits or alignbits */
void initbits(CONTEXT context)
{
_o_stream = context.o_stream;
outcnt = 8;
bytecnt = 0;
}
/* write rightmost n (0<=n<=32) bits of val to outfile */
void putbits(int n, int val)
{
int i;
unsigned int mask;
char bitstring[32];
if (debug > 1)
{
if (n > 0)
{
BitPrint(n, val, bitstring);
trace(bitstring);
}
}
mask = 1 << (n - 1); /* selects first (leftmost) bit */
for (i = 0; i < n; i++)
{
outbfr <<= 1;
if (val & mask)
outbfr |= 1;
mask >>= 1; /* select next bit */
outcnt--;
if (outcnt == 0) /* 8 bit buffer full */
{
putc(outbfr, _o_stream);
outcnt = 8;
bytecnt++;
}
}
}
/* zero bit stuffing to next byte boundary (5.2.3, 6.2.1) */
int alignbits()
{
int ret_value;
if (outcnt != 8)
{
ret_value = outcnt; /* outcnt is reset in call to putbits () */
trace("align:\t");
putbits (outcnt, 0);
// fflush(o_stream);
return ret_value;
} else
return 0;
}
/* return total number of generated bits */
int bitcount()
{
return 8 * bytecnt + (8 - outcnt);
}
/* convert to binary number */
void BitPrint(int length, int val, char *bit)
{
int m;
m = length;
bit[0] = '"';
while (m--)
{
bit[length - m] = (val & (1 << m)) ? '1' : '0';
}
bit[length + 1] = '"';
bit[length + 2] = '\n';
bit[length + 3] = '\0';
return;
}