#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <fstream>
using namespace std;
// phep chia mod-2 hay con goi la phep chia tren truong da thuc (truong galois ) voi so bi chia la 1011 trong he nhi phan va la 11 trong he thap phan
unsigned mod (unsigned b)
{
unsigned a = b;
if (a&64) a = a ^ 88;
if (a&32) a = a ^ 44;
if (a&16) a = a ^ 22;
if (a&8) a = a ^ 11;
return(a);
}
//kiem tra xem file nhan vao co thoa man chia het theo truong galois khong
unsigned int test (char c)
{
unsigned a ;
a = mod(c);
if (a)return 0;
else return 1;
}
//ham truyen lai // ham sua loi // ham nay se vao file transmit ban dau de timl bl
unsigned char retransmit (int i)
{
char *reblock = new char[i + 1], a;
ifstream re_in("transmit", ios::binary | ios::in);
re_in.seekg(i);
re_in.read(reblock, 1);
a = *reblock;
re_in.close();
return(a);
}
// y tuong cua phan nay la su li file name vao ! de co the vua co the la name hoac link deu chap nhan duoc
char* namefile (char *name)
{
int n, x, y, z = 0;
char *b = new char[255];
n = strlen(name);
for (x = n;x > 0;x--)
{
if (name[x] == 92) // neu la dau '/' thi dung lai
{
for (y = x + 1; y < n; y++)
{
b[z] = name[y];
z++;
}
return b;
}
}
for (z = 0;z < n;z++)
b[z] = name[z];
return b;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888*/
void main()
{
char filename[100];
char c;
begin:
cout << " ---- CHUONG TRINH MA HOA CRC TREN DOS ---- \n\n";
cout << " xin nhap ten cua file can truyen \n";
cout << " file name : ";
cin >> filename;
// phan xu ly ten file . de file ra se co dang fileout.xxx voi xxx la duoi file can sua
int x, y, z = 0;
char outfile[40] = "fileout";
x = strlen(filename);
y = strlen(outfile);
for (int i2 = x - 1;i2 > 0;i2--)
{
if (filename[i2] == 46)
{
// cout<< "co tim ra roi";
int j2 = y;
while ( j2 <= y + x - i2)
{
outfile[j2] = filename[i2+z];
j2++;
z++;
}
outfile[j2] = '\0';
}
}
//phan input////////////////////////////////////////////////////////////////////////
char *block1, *p1, low_block, high_block;
block1 = new char[100];
p1 = new char[100];
int crc;
ifstream input(filename, ios::binary | ios::in);
ofstream output("transmit", ios::trunc | ios::binary | ios::ate);
//kiem tra xem file nhap vao co ton tai khong
if (!input)
{
cout << "\n\n-----------------------------------------------------------\n\n";
cout << "\n\nloi khong mo duoc file " ;
input.close();
output.close();
remove("transmit");
}
else
{
//thuc hien neu file co ton tai
input.seekg(0, ios::end);
long filesize = input.tellg();
input.seekg(0, ios::beg);
while (filesize)
{
input.read(block1, 1);
//tach bloc1 ra lam 2 byte chua 4 bit dau va cuoi
//ta nhan duoc 2 byte moi chua 4 bit du lieu cua byte ban dau voi vi tri 01111000 de su dung trong phep sau
low_block = *block1 & (0xF);
low_block = low_block << 3;
high_block = *block1 >> 4;
high_block = high_block << 3;
// chia gia cac gia tri nay cho so G =11 hay 1011 xay dung khung 7bit
crc = mod (high_block);
high_block = high_block | crc;
*p1 = high_block;
output.write(p1, 1);
crc = mod (low_block);
low_block = low_block | crc;
*p1 = low_block ;
output.write(p1, 1);
// ta co file transmit chua nhung file co du lieu duoc ma hoa khung 7 bit mot
// vi du file truyen vao se co gia tri la 11001110 thi 2 file ma hoa la 01100crc va 01110crc
//voi crc la phan du cua phep chia mod-2 o tren
filesize--;
}
input.close();
output.close();
//phan transmit////////////////////////////////////////////////////////////////////////
//phan nay co nhieu vu là làm hong cac bit cua file theo mot random nao dó
char *block = new char[100], *p = new char[100], ra = 0;
//lay du lieu cua phan input de su ly // pha hong file nay theo mot random nao do
ifstream in("transmit", ios::binary | ios::in);
ofstream out("transmi2", ios::trunc | ios::binary); // dau ra se la file transmi2 cai nay quan trong
in.seekg(0, ios::end); // coi transmi2 se la file truyen den ben nhan
filesize = in.tellg();
in.seekg(0, ios::beg);
srand(100);
while (filesize) //chay den het file
{
in.read(block, 1);
ra = rand();
if (ra < 8)
{
if (ra < 4) *block = *block & 239 ;
else *block = *block & 223 ;
*p = *block;
out.write(p, 1);
}
else
{
*p = *block;
out.write(p, 1);
}
filesize--;
}
in.close();
out.close();
/////////////////////////////////////////////////////////////////////////////////////
//phan output////////////////////////////////////////////////////////////////////////
unsigned long loi = 0;
char *block2 = new char[100], *p2 = new char[100], low, high;
unsigned long j = 0;
//phan kiem tra xem co loi hay khong va yeu cau truyen lai
ifstream in3("transmi2", ios::binary | ios::in);
ofstream out3("data", ios::trunc | ios::binary);
in3.seekg(0, ios::end);
filesize = in3.tellg();
in3.seekg(0, ios::beg);
while (filesize)
{
in3.read(block, 1);
if (test(*block))
{
*p = *block;
out3.write(p, 1);
}
else
{
//yeu cau truyen lai//goi ham truyen lai
*p = retransmit(j);
out3.write(p, 1);
loi++;
}
++j;
filesize--;
}
in3.close();
out3.close();
//giai ma///////////////////////////////////////////////////////////////////////////////////
//phan ket hop de duoc file ban dau
ifstream in2("data", ios::in | ios::binary);
ofstream out2(outfile, ios::trunc | ios::binary);
in2.seekg(0, ios::end);
filesize = in2.tellg();
in2.seekg(0, ios::beg);
while (filesize)
{
in2.read(block2, 2);// cho nay con tro chi vao 2 byte
//tu 2 byte co dang 01111crc va 01101crc chuyen ve 1 byte co dang 11111101
high = (char) * block2;
low = block2[1];
low = low >> 3;
high = high >> 3;
high = high << 4;
*p2 = high | low;
out2.write(p2, 1);
filesize -= 2;
}
//xoa cac file nhap trong qua trinh
remove("data");
remove("transmi2");
remove("transmit");
in2.close();
out2.close();
cout << " ---- CHUONG TRINH MA HOA CRC TREN DOS ---- \n\n";
cout << " xin nhap ten cua file can truyen \n";
cout << "\n\n-----------------------------------------------------------\n\n";
cout << "\nfile co ten la " << filename << " da duoc truyen di \n\n";
cout << "so loi tren duong truyen la " << loi << " loi";
cout << "\n\nnhung tat ca cac loi da duoc sua \n\n";
cout << "file truyen di duoc luu co ten la " << outfile;
cout << "\n\n----------------------------------------------------------\n\n";
} //dong else
cout << "\n\nban co muon truyen tiep khong (c\\k) \n";
c = getch();
if (c == 'c' || c == 'C')
goto begin;
}