-
Notifications
You must be signed in to change notification settings - Fork 0
/
stuff.c
109 lines (101 loc) · 1.92 KB
/
stuff.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Bit stuffing and unstuffing library.
*/
#include <stdio.h>
#include <stdlib.h>
#include "stream.h"
stream_t *stuff( FILE *in );
/** Return a stuffed stream for a given file input. */
stream_t *stuff( FILE *in )
{
char c;
stream_t *stream;
stream = stream_new();
int count = 0;
// read char from file
while( stream && in && EOF != ( c = fgetc( in ) ) )
{
int i, n;
// for each bit in the byte
for( i = NBITS - 1; i >= 0; i-- )
{
char bit = get( c, i );
// stuff the bit
if( 5 == count )
{
add( stream, 0 );
count = 0;
}
if( 1 == bit )
{
count++;
}
else
{
count = 0;
}
// add to stream
add( stream, bit );
}
}
return( stream );
}
/** Print unstuffed output for file for a given stuffed stream. */
void unstuff( stream_t *stream, FILE *out )
{
int count = 0;
stream_t *sout = stream_new();
if( sout )
{
for( int i = 0; i < stream->pos; i++ )
{
if( count == 5 )
{
// skip this bit
count = 0;
}
else
{
char bit = get( stream->s[i/NBITS], ( NBITS - 1 ) - ( i % NBITS ) );
add( sout, bit );
if( bit == 1 )
{
count++;
}
else
{
count = 0;
}
}
}
for( int i = 0; i < sout->pos / NBITS; i++ )
{
fprintf( out, "%c", sout->s[i] );
}
}
free( sout );
}
void
usage()
{
printf( "Usage: ./stuff [infile]\n" );
printf( " [infile] input file to stuff\n" );
printf( " the output file will be saved to 'data.out'\n" );
}
int main( int argc, char** argv )
{
if( argc == 2 )
{
FILE *in = fopen( argv[1], "r" );
FILE *out = fopen( "data.out", "w" );
stream_t *stream = stuff( in );
unstuff( stream, out );
fclose( in );
fclose( out );
free( stream );
}
else
{
usage();
}
}