Skip to content

Commit 8c78906

Browse files
authored
Merge pull request dzagalskii#1 from FS-make-simple/master
1.1: added Makefile and cmdline args
2 parents c141881 + 8c743a1 commit 8c78906

File tree

7 files changed

+633
-571
lines changed

7 files changed

+633
-571
lines changed

LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Public Domain Mark 1.0
2+
No Copyright
3+
4+
This work has been identified as being free of known restrictions
5+
under copyright law, including all related and neighboring rights.
6+
7+
You can copy, modify, distribute and perform the work, even for
8+
commercial purposes, all without asking permission. See Other
9+
Information below.
10+
11+
Other Information
12+
13+
The work may not be free of known copyright restrictions in all
14+
jurisdictions.
15+
16+
Persons may have other rights in or related to the work, such as
17+
patent or trademark rights, and others may have rights in how the
18+
work is used, such as publicity or privacy rights.
19+
20+
In some jurisdictions moral rights of the author may persist beyond
21+
the term of copyright. These rights may include the right to be
22+
identified as the author and the right to object to derogatory
23+
treatments.
24+
25+
Unless expressly stated otherwise, the person who identified the work
26+
makes no warranties about the work, and disclaims liability for all
27+
uses of the work, to the fullest extent permitted by applicable law.
28+
29+
When using or citing the work, you should not imply endorsement by
30+
the author or the person who identified the work.

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CC = gcc
2+
CFLAGS = -Wall -std=c99
3+
LDFLAGS = -s
4+
PROGS = hammingenc hammingdec
5+
6+
all: $(PROGS)
7+
8+
hammingenc: hammingenc.c
9+
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
10+
11+
hammingdec: hammingdec.c
12+
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
13+
14+
clean: $(PROGS)
15+
rm -f $^

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
Familiarization with the Hamming method of error-correcting coding, which allows to detect and automatically correct errors that occur during the transmission of information.
1+
# HAMMING ENC/DEC
2+
3+
Familiarization with the Hamming method of error-correcting coding, which allows to detect and automatically correct errors that occur during the transmission of information.
4+
5+
# USAGE
6+
7+
```sh
8+
./hammingenc input.file output.hmng [8/12/16/32/64]
9+
./hammingdec input.hmng output.file [8/12/16/32/64]
10+
```
11+
12+
2019

hammingdec.c

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void kontrolbit(int *kod, int lengh)
5+
{
6+
int i = 0, summ = 0, errorpos = 0;
7+
while(i < lengh)
8+
{
9+
if((i+1 == 1)||(i+1 == 2)||(i+1 == 4)||(i+1 == 8)||(i+1 == 16)||(i+1 == 32)||(i+1 == 64))
10+
{
11+
int start = i;
12+
while(start < lengh)
13+
{
14+
for(int g = 0; g < i + 1; g++)
15+
{
16+
if((start+g != i)&&(start+g < lengh))
17+
summ += kod[start+g];
18+
}
19+
start += (i + i + 2);
20+
}
21+
if(kod[i] != summ%2)
22+
{
23+
errorpos += i;
24+
}
25+
summ = 0;
26+
}
27+
i++;
28+
}
29+
kod[errorpos] = (errorpos != 0) ? !kod[errorpos] : kod[errorpos];
30+
}
31+
32+
void invert(int *byte)
33+
{
34+
int i, t;
35+
for(i = 0; i < 4; i++)
36+
{
37+
t = byte[i];
38+
byte[i] = byte[7 - i];
39+
byte[7 - i] = t;
40+
}
41+
}
42+
43+
void tobit(int x, int *byte)
44+
{
45+
int i;
46+
for(i = 0; i < 8; i++)
47+
{
48+
byte[i] = x%2;
49+
x >>= 1;
50+
}
51+
invert(byte);
52+
}
53+
54+
int todec(int *byte)
55+
{
56+
int i, x = 0, st = 7;
57+
for(i = 0; i < 8; i++)
58+
{
59+
x += (byte[i]<<st);
60+
st--;
61+
}
62+
return x;
63+
}
64+
65+
int main(int argc, char *argv[])
66+
{
67+
if (argc < 3)
68+
{
69+
printf("Usage: %s input.hmng output.file [8/12/16/32/64]\n", argv[0]);
70+
return 0;
71+
}
72+
FILE *f = fopen(argv[1], "rb");
73+
FILE *t = fopen(argv[2], "w+b");
74+
int byte[8];
75+
printf("coded: %s\n", argv[1]);
76+
printf("file: %s\n", argv[2]);
77+
int c = 8;
78+
if (argc > 3) c = atoi(argv[3]);
79+
printf("decoding length: %d\n\n", c);
80+
if(c == 8)
81+
{
82+
int x, kod[12], g = 0;
83+
x = fgetc(f);
84+
while(x != EOF)
85+
{
86+
tobit(x, byte);
87+
for(int i = 0; i < 8; i++)
88+
{
89+
if((i != 0)&&(i != 1))
90+
kod[g++] = byte[i];
91+
}
92+
if(g == 12)
93+
{
94+
kontrolbit(kod, 12);
95+
g = 0;
96+
for(int i = 0; i < 12; i++)
97+
{
98+
if((i+1 != 1)&&(i+1 != 2)&&(i+1 != 4)&&(i+1 != 8))
99+
{
100+
byte[g++] = kod[i];
101+
}
102+
}
103+
fputc(todec(byte), t);
104+
g = 0;
105+
}
106+
x = fgetc(f);
107+
}
108+
}
109+
else if(c == 12)
110+
{
111+
fprintf(stderr, "WARNIND: with coding length %d end of file is not always processed correctly\n\n", c);
112+
int tmp[4], x, kod[17], g = 0, flag = 0, schet = 0;
113+
x = fgetc(f);
114+
while(x != EOF)
115+
{
116+
tobit(x, byte);
117+
kod[g] = byte[7];
118+
g++;
119+
if(g == 17)
120+
{
121+
g = 0;
122+
kontrolbit(kod, 17);
123+
g = 0;
124+
if(flag == 1) //костыль
125+
{
126+
for(int i = 0; i < 4; i++)
127+
{
128+
byte[g++] = tmp[i];
129+
}
130+
flag = 0;
131+
}
132+
for(int i = 0; i < 17; i++)
133+
{
134+
if((i+1 != 1)&&(i+1 != 2)&&(i+1 != 4)&&(i+1 != 8)&&(i+1 != 16)&&(i+1 != 32)&&(i+1 != 64))
135+
byte[g++] = kod[i];
136+
if(g == 8)
137+
{
138+
fputc(todec(byte), t);
139+
g = 0;
140+
if((flag == 0)&&(schet%2 == 0)) //костыль 2
141+
{
142+
for(int i = 12; i < 17; i++)
143+
{
144+
if(i+1 != 16)
145+
{
146+
tmp[g++] = kod[i];
147+
}
148+
}
149+
flag = 1;
150+
}
151+
g = 0;
152+
}
153+
}
154+
g = 0;
155+
}
156+
schet++; //костыль 3
157+
x = fgetc(f);
158+
}
159+
}
160+
else if(c == 16)
161+
{
162+
fprintf(stderr, "WARNIND: with coding length %d end of file is not always processed correctly\n\n", c);
163+
int x, kod[21], g = 0;
164+
x = fgetc(f);
165+
while(x != EOF)
166+
{
167+
tobit(x, byte);
168+
for(int i = 0; i < 8; i++)
169+
{
170+
if(i != 0)
171+
kod[g++] = byte[i];
172+
}
173+
if(g == 21)
174+
{
175+
kontrolbit(kod, 21);
176+
g = 0;
177+
for(int i = 0; i < 21; i++)
178+
{
179+
if((i+1 != 1)&&(i+1 != 2)&&(i+1 != 4)&&(i+1 != 8)&&(i+1 != 16)&&(i+1 != 32)&&(i+1 != 64))
180+
byte[g++] = kod[i];
181+
if(g == 8)
182+
{
183+
fputc(todec(byte), t);
184+
g = 0;
185+
}
186+
}
187+
}
188+
x = fgetc(f);
189+
}
190+
}
191+
else if(c == 32)
192+
{
193+
fprintf(stderr, "WARNIND: with coding length %d end of file is not always processed correctly\n\n", c);
194+
int x, kod[38], g = 0;
195+
x = fgetc(f);
196+
while(x != EOF)
197+
{
198+
tobit(x, byte);
199+
for(int i = 0; i < 8; i++)
200+
{
201+
if((i == 6)||(i == 7))
202+
kod[g++] = byte[i];
203+
}
204+
if(g == 38)
205+
{
206+
kontrolbit(kod, 38);
207+
g = 0;
208+
for(int i = 0; i < 38; i++)
209+
{
210+
if((i+1 != 1)&&(i+1 != 2)&&(i+1 != 4)&&(i+1 != 8)&&(i+1 != 16)&&(i+1 != 32)&&(i+1 != 64))
211+
byte[g++] = kod[i];
212+
if(g == 8)
213+
{
214+
fputc(todec(byte), t);
215+
g = 0;
216+
}
217+
}
218+
}
219+
x = fgetc(f);
220+
}
221+
}
222+
else if(c == 64)
223+
{
224+
fprintf(stderr, "WARNIND: with coding length %d end of file is not always processed correctly\n\n", c);
225+
int x, kod[71], g = 0;
226+
x = fgetc(f);
227+
while(x != EOF)
228+
{
229+
tobit(x, byte);
230+
for(int i = 0; i < 8; i++)
231+
{
232+
if(i == 7)
233+
kod[g++] = byte[i];
234+
}
235+
if(g == 71)
236+
{
237+
kontrolbit(kod, 71);
238+
g = 0;
239+
for(int i = 0; i < 71; i++)
240+
{
241+
if((i+1 != 1)&&(i+1 != 2)&&(i+1 != 4)&&(i+1 != 8)&&(i+1 != 16)&&(i+1 != 32)&&(i+1 != 64))
242+
byte[g++] = kod[i];
243+
if(g == 8)
244+
{
245+
fputc(todec(byte), t);
246+
g = 0;
247+
}
248+
}
249+
}
250+
x = fgetc(f);
251+
}
252+
}
253+
else
254+
{
255+
fprintf(stderr, "ERROR: bad coding length %d, maybe 8/12/16/32/64\n\n", c);
256+
}
257+
fclose(f);
258+
fclose(t);
259+
return 0;
260+
}

0 commit comments

Comments
 (0)