Header byte | Data following the header byte |
---|---|
0 to 127 | (1 + n) literal bytes of data |
-1 to -127 | One byte of data, repeated (1 – n) times in the decompressed output |
-128 | No operation (skip and treat next byte as a header byte) |
#include <vector>
#include <fstream>
#include <cstdio>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
vector<char> v;
vector<char> d;
ifstream in;
ofstream out;
int cont = 0;
for(int i=0;i<argc;i++){
cout << argv[i] << "\n";
}
in.open(argv[1], ios_base::binary);
if(in.is_open()){
printf("File %s trovato\n", argv[1]);
}
else{
printf("File %s NON trovato\n", argv[1]);
}
out.open(argv[2]);
if(out.is_open()){
printf("File %s creato\n", argv[2]);
}
else{
printf("File %s NON creato\n", argv[2]);
}
v.push_back(in.get());
//cout << "Letto " << v.at(cont) << "\n";
cont++;
int occ;
int div;
int pos;
while(1){
v.push_back(in.get());
if(v.at(cont) == EOF)
break;
//cout << "Letto fuori " << v.at(cont)<<" "<< cont << "\n";
cont++;
if(v.at(cont-1) == v.at(cont-2)){//se è uguale
occ = 2;
do{
v.push_back(in.get());
//cout << "Letto " << v.at(cont)<<" "<< cont << "\n";
cont++;
if(v.at(cont-1) == v.at(cont-2))
occ++;
if(occ == 128){
pos = 0;
pos = 257 - occ;
cout << pos << v.at(cont-2);
out.put(pos);
out.put(v.at(cont-2));
occ = 0;
}
}
while(v.at(cont-1) == v.at(cont-2));//esci quando ne trovi uno diverso
pos = 0;
pos = 257 - occ;
cout << pos << v.at(cont-2);
out.put(pos);
out.put(v.at(cont-2));
occ = 2;
}
else{
div = 2;
do{
v.push_back(in.get());
//cout << "Letto " << v.at(cont)<<" "<< cont << "\n";
cont++;
if(v.at(cont-1) != v.at(cont-2) && v.at(cont-1) != EOF)
div++;
if(div == 128){
pos = 0;
pos = div - 1;
cout << pos;
out.put(pos);
for(int j=0;j<div;j++){
cout << v.at(cont-1-div+j);
out.put(v.at(cont-1-div+j));
}
div = 0;
}
}
while(v.at(cont-1) != v.at(cont-2) && v.at(cont-1) != EOF);
pos = 0;
pos = div - 1;
cout << pos;
out.put(pos);
for(int j=0;j<div;j++){
cout << v.at(cont-1-div+j);
out.put(v.at(cont-1-div+j));
}
div = 2;
}
}
cout << 128;
out.put(128);
in.close();
out.close();
scanf("%d");
}
Nessun commento:
Posta un commento