|
| 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