-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5bit.c
More file actions
265 lines (212 loc) · 7.81 KB
/
5bit.c
File metadata and controls
265 lines (212 loc) · 7.81 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* ***************************************************************** */
/* 5bit */
/* author John Cade Griffin */
/*
5bit: take one file name as its input on the command line and produce on standard
output a translated file. Turns 8 bit sequences (bytes) into 5 bit sequences for encoding
and reverses this for decoding
modified to allow translation to hex, octal, base 4 or binary.
Written around 2013.
*/
/* ***************************************************************** */
/* missing type from C language */
typedef short Boolean;
#define TRUE 1
#define FALSE 0
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
#include <stdio.h>
#include <stdlib.h>
FILE * input;
Boolean DECODE = FALSE;
int newCharSize = 5; /* 5 for encoding, 8 for decoding */
void process(void);
void encodeFile(); /* called if DECODE = False, encodes the file into newCharSize sequences */
void printChar(int temp); /* called from encodeFile, prints a char representation of a shifted int */
void decodeFile(); /* called if DECODE = True, decodes the file into newCharSize sequences */
int decodeChar(int byte); /* called from decodeFile, returns an shifted int in the form previous of printChar */
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void scanargs(char * s)
{
/* check each character of the option list for
its meaning. */
while (*++s != '\0')
switch (*s)
{
case 'd': /* decode option */
DECODE = TRUE;
newCharSize = 8;
break;
default:
fprintf (stderr,"PROGR: Bad option %c\n", *s);
fprintf (stderr,"usage: PROGR [-D] file\n");
exit(1);
}
}
/* ***************************************************************** */
int main(int argc, char **argv)
{
Boolean namenotgiven = TRUE;
/* main driver program. Define the input file from either standard input or
a name on the command line. Process all arguments. */
while (argc > 1)
{
argc--, argv++;
if (**argv == '-')
scanargs(*argv);
else
{
namenotgiven = FALSE;
input = fopen(*argv, "r");
if (input == NULL)
{
fprintf(stderr, "Can't open %s\n", *argv);
exit(1);
}
process();
fclose(input);
}
}
/* input from stdin if not file named on the input line */
if (namenotgiven)
{
input = stdin;
process();
}
exit(0);
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void process(void)
{
if(!DECODE)
{
encodeFile();
}
else{
decodeFile();
}
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void encodeFile()
{
int byte;
int temp = 0;
int charCount = 0; /*counts the characters before a new line */
int bSize = 0; /*size of string of bits, 5 if encoding, 8 if decoding, initialize to 0 */
while ((byte = getc(input)) != EOF) /* while there are still characters, grab the next */
{
int bitCount = 8; /* re-initialize bitCount */
while(bitCount > 0) /* while there are still bits left in this byte, grab the next */
{
/* shift byte to isolate desired bit, then reshift that bit to the position in the newChar which
is the equivalent of multiplying by powers of two, we want to total up temp */
temp += (((byte >> (bitCount-1)) & 1) << (newCharSize - (bSize + 1)));
/*we are now in the next position of our new char */
bSize++;
/*we now need one less bit from byte */
bitCount--;
/*if encoding newCharSize = 5, else newCharSize = 8. if bSize = newCharSize temp is ready to become
a newCharSize character. Print new character */
if(bSize == newCharSize)
{
if (charCount == 72)
{
printf("\n");
charCount = 0;
}
printChar(temp);
bSize=0;
temp=0;
charCount++;
}
}
}
/* check for remaining bits */
if (bSize > 0);
{
printChar(temp);
printf("\n");
}
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void decodeFile()
{
int tempNum = 0;
int byte;
int bitCount = 0; /*counts the # of bits processed so far, stop ever 5 */
while((byte = getc(input))!=EOF)
{
/*check for newlines */
if(byte != '\n')
{
/* decode the character to its previous value prior to printChar */
char decodedChar = decodeChar(byte);
/* count bits processed. Size will equal 5 */
int bSize = 0;
while(bSize < 5)
{
/*we have processed 1 bit */
bSize++;
/* remove 1 bit */
char bit = decodedChar >> (5 - bSize) & 1;
bitCount++;
tempNum+= bit << (newCharSize - bitCount);
/*if we have processed all bits */
if(bitCount == newCharSize)
{
printf("%c", tempNum);
tempNum = 0;
bitCount = 0;
}
}
}
}
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void printChar(int temp)
{
if(temp < 26)
{
temp += 65;
printf("%c",temp );
}
else
{
temp += 22;
printf("%c",temp );
}
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
int decodeChar(int temp)
{
if(temp >= 65)
{
temp = temp - 65;
return temp;
}
else
{
temp = temp - 22;
}
return temp;
}