-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_output.c
85 lines (46 loc) · 1.65 KB
/
input_output.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
#include<stdio.h>
#include<stdlib.h>
#include"input_output.h"
int read_binary(FILE *ip, int size)
{
int read = fgetc(ip);
if(read == EOF)return -1;
int code;
if((leftOver + 8) >= size)
{
int code1 = (leftValue << (size - leftOver));
int code2 = (read >> ((8-(size-leftOver))));
code = code1 + code2;
int tmp = leftOver;
leftOver = (8-(size-tmp));
leftValue = read & ((1<< (8-(size-tmp)))-1);
}
else
{
code = (leftValue << 8 )+ read;
leftValue = 0;
read = fgetc(ip);
size = size- (8+leftOver);
code = (code << size); // first 8-leftover bits are extracted
code = code + (read >> (8-size)); //last leftover bits are extracted from the next input
leftOver = 8-size; // update the leftover size of the input 2
leftValue = (read & ((1<<(8-size))-1)); // update the leftvalue
}
return code;
}
void write_binary(FILE *op, int code, int size)
{
int pre = (leftValue << (8-leftOver)); // first leftover bits are written with pre value
int curr = (code>>(size-(8-leftOver))); // last remaining bits are written with this
leftOver = leftOver+size-8; //updating leftover
leftValue = code&((1<<(size-(8-leftOver)))-1); //updating leftvalue
int print = pre + curr; //oring to get 8 bits
fputc(print,op); // fputc writes only 8 bits so every time we should maintain buffer
while(leftOver >=8 )
{
print = (leftValue >> (leftOver-8));
leftValue = leftValue & ((1<<(leftOver-8))-1);
leftOver = leftOver-8;
fputc(print,op);
}
}