CODER include stdio include math int coder char inputTextName 255 prin

  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
//CODER
#include <stdio.h>
#include <math.h>
int coder()
{
char inputTextName[255];
printf("Enter text file name:\n");
scanf("%s",inputTextName);
FILE* inputText=fopen(inputTextName,"rb");
fseek(inputText,0,SEEK_END);
int fileSize=ftell(inputText);
fseek(inputText,0,SEEK_SET);
char* textData=new char[fileSize];
for(int i=0; !feof(inputText); i++)
textData[i]=getc(inputText);
char inputBmpName[255];
printf("Enter bmp file name:\n");
scanf("%s", inputBmpName);
int numBits;
printf("Enter number of bits in one byte:\n");
scanf("%d",&numBits);
if ((numBits < 1) || (numBits > 8)) return -1;
numBits--;
FILE* inputBmp=fopen(inputBmpName,"rb");
FILE* outputBmp=fopen("out.bmp","wb");
for(int i=0;i<54;i++)
{
int buffer=getc(inputBmp);
putc(buffer, outputBmp);
}
int mask=pow(2.0,31);
for(int i=0; i<32; i++, mask>>=1)
{
int buffer=getc(inputBmp);
if(fileSize & mask)
buffer|=1;
else
buffer&=~1;
putc(buffer, outputBmp);
}
mask=pow(2.0,2);
for(int i=0;i<3;i++, mask>>=1) //Compression putting cycle
{
int buffer=getc(inputBmp);
if(numBits & mask)
buffer|=1;
else
buffer&=~1;
putc(buffer, outputBmp);
}
for(int i=0; i<fileSize; i++)
{
char buffer_char=textData[i];
mask=pow(2.0,7);
int compr_mask=0;
for(int j=0; j<8; j++, mask>>=1, compr_mask>>=1)
{
int buffer=getc(inputBmp);
if(!compr_mask)
compr_mask=pow(2.0,numBits);
if(buffer_char & mask)
buffer |= compr_mask;
else
buffer &=~ compr_mask;
putc(buffer,outputBmp);
}
}
while(!feof(inputBmp))
{
int buffer=getc(inputBmp); //other file copying cycle
putc(buffer, outputBmp);
}
fclose(inputBmp);
fclose(outputBmp);
return 1;
}
int decoder(const char* path)
{
FILE* opened_read, * opened_write;
unsigned char buffer, temp_mask_char, compr_mask, mask_1=1;
unsigned int buffer_text, temp_mask_int;
int compr=0, size_text=0;
if(!((opened_read=fopen(path,"rb")) && (opened_write=fopen("uncoded.txt", "wb"))))
return -1;//throw (Exception(Exception::Unopened));
int c=fseek(opened_read,54,SEEK_SET);
temp_mask_int=pow(2.0,31);
for(int i=0;i<32;i++, temp_mask_int>>=1) //Size getting cycle
{
buffer=getc(opened_read);
if(buffer&mask_1)
size_text|=temp_mask_int;
}
temp_mask_int=pow(2.0,2);
for(int i=0;i<3;i++, temp_mask_int>>=1) //Compression getting cycle
{
buffer=getc(opened_read);
if(buffer&mask_1)
compr|=temp_mask_int;
}
compr_mask=(char)pow(2.0,compr);
for(int i=0;i<size_text;i++)
{
if(feof(opened_read))
return -1;//throw (Exception(Exception::End_reach));
temp_mask_char=128;
buffer_text=0;
for(int j=0;j<8;j++,temp_mask_char>>=1,compr_mask>>=1)
{
if(!compr_mask)
compr_mask=pow(2.0,compr);
buffer=getc(opened_read);
if(buffer&compr_mask)
buffer_text|=temp_mask_char;
}
putc(buffer_text,opened_write);
}
fclose(opened_read);
fclose(opened_write);
return 1;
}
int main() {
//coder();
decoder("out.bmp");
return 1;
}