-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitrw.c
More file actions
77 lines (63 loc) · 1.16 KB
/
bitrw.c
File metadata and controls
77 lines (63 loc) · 1.16 KB
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
/* Sergeev Artemiy, 33601/2 (3057/2), 07.10.2013 */
#include "bitrw.h"
#define BIT_BUF_SIZE 7
/* Bit read/write variables */
static unsigned char s_BitWRAccum;
static int s_BitWRPos;
static FILE *s_BitWRF;
void bitWriteInit( FILE *F )
{
s_BitWRPos = BIT_BUF_SIZE;
s_BitWRAccum = 0;
s_BitWRF = F;
}
void bitWrite( int bit )
{
if (bit)
s_BitWRAccum |= (1 << s_BitWRPos);
if (--s_BitWRPos < 0)
{
fputc(s_BitWRAccum, s_BitWRF);
s_BitWRPos = BIT_BUF_SIZE;
s_BitWRAccum = 0;
}
}
void bitsWrite( int Bits, int Len )
{
while (Len-- > 0)
bitWrite((Bits >> Len) & 1);
}
int bitWriteClose( void )
{
if (s_BitWRPos != BIT_BUF_SIZE)
fputc(s_BitWRAccum, s_BitWRF);
return BIT_BUF_SIZE - s_BitWRPos;
}
void bitReadInit( FILE *F )
{
s_BitWRPos = -1;
s_BitWRF = F;
}
int bitRead( void )
{
int b;
if (s_BitWRPos < 0)
{
if ((b = fgetc(s_BitWRF)) == EOF)
return EOF;
s_BitWRAccum = b;
s_BitWRPos = BIT_BUF_SIZE;
}
return (s_BitWRAccum >> s_BitWRPos--) & 1;
}
int bitsRead( int len )
{
int a = 0, b;
while (len-- > 0)
{
if ((b = bitRead()) == EOF)
return EOF;
a |= b << len;
}
return a;
}