WIP
This commit is contained in:
@ -20,6 +20,7 @@ void MashZip::unmashzip_stream(std::basic_istream<char> &is, std::basic_ostream<
|
|||||||
|
|
||||||
// load huffman table
|
// load huffman table
|
||||||
HuffmanTable ht(header);
|
HuffmanTable ht(header);
|
||||||
|
|
||||||
ibitstream ibs(is);
|
ibitstream ibs(is);
|
||||||
|
|
||||||
char sym = '\0';
|
char sym = '\0';
|
||||||
@ -40,13 +41,11 @@ void MashZip::unmashzip_stream(std::basic_istream<char> &is, std::basic_ostream<
|
|||||||
void MashZip::mashzip_file(std::basic_istream<char> &cs, std::basic_istream<char> &is, std::basic_ostream<char> &os)
|
void MashZip::mashzip_file(std::basic_istream<char> &cs, std::basic_istream<char> &is, std::basic_ostream<char> &os)
|
||||||
{
|
{
|
||||||
// std::ifstream ifs(name, std::ios::binary);
|
// std::ifstream ifs(name, std::ios::binary);
|
||||||
// is.seekg(0);
|
|
||||||
|
|
||||||
os.write(MAGIC, 4);
|
os.write(MAGIC, 4);
|
||||||
|
|
||||||
// build huffman table by file
|
// build huffman table by file
|
||||||
HuffmanTable ht(cs);
|
HuffmanTable ht(cs);
|
||||||
// is.seekg(0); // reset position as ht reads whole file
|
|
||||||
|
|
||||||
os.write(ht.to_header(), HEADER_SIZE);
|
os.write(ht.to_header(), HEADER_SIZE);
|
||||||
|
|
||||||
|
@ -6,11 +6,6 @@
|
|||||||
|
|
||||||
// i.e when stream is 0b12345678 0bABCDEFGH and command is
|
// i.e when stream is 0b12345678 0bABCDEFGH and command is
|
||||||
// to read 12 bits:
|
// to read 12 bits:
|
||||||
// out = 00000000 00000000 0000EFGH 12345678 -- wrong
|
|
||||||
// to read 2 bits:
|
|
||||||
// out = 00000000 00000000 00000000 00000078
|
|
||||||
// TODO:
|
|
||||||
// to read 12 bits:
|
|
||||||
// out = 00000000 00000000 00008765 4321HGFE
|
// out = 00000000 00000000 00008765 4321HGFE
|
||||||
// to read 2 bits:
|
// to read 2 bits:
|
||||||
// out = 00000000 00000000 00000000 00000087
|
// out = 00000000 00000000 00000000 00000087
|
||||||
@ -55,7 +50,7 @@ int ibitstream::getbits(size_t n) {
|
|||||||
// " inv " << std::bitset<8>(inv) << " out " << std::bitset<32>(out) << std::endl;
|
// " inv " << std::bitset<8>(inv) << " out " << std::bitset<32>(out) << std::endl;
|
||||||
|
|
||||||
this->count += to_read;
|
this->count += to_read;
|
||||||
read += to_read;
|
// read += to_read;
|
||||||
n -= to_read;
|
n -= to_read;
|
||||||
|
|
||||||
if (this->count == 8){
|
if (this->count == 8){
|
||||||
|
@ -200,7 +200,7 @@ int HuffmanTable::decode_one_symbol(ibitstream &bs)
|
|||||||
char *HuffmanTable::to_header() {
|
char *HuffmanTable::to_header() {
|
||||||
char *header = new char[HEADER_SIZE];
|
char *header = new char[HEADER_SIZE];
|
||||||
|
|
||||||
for (size_t i = 0; i < HEADER_SIZE; i++)
|
for (size_t i = 0; i < HEADER_SIZE; i++) // iterate over header bytes
|
||||||
{
|
{
|
||||||
if (huffmanCodes.find(2 * i) != huffmanCodes.end()) {
|
if (huffmanCodes.find(2 * i) != huffmanCodes.end()) {
|
||||||
int len = huffmanCodes[2 * i].first;
|
int len = huffmanCodes[2 * i].first;
|
||||||
|
102
main.cpp
102
main.cpp
@ -194,76 +194,76 @@ using namespace std;
|
|||||||
//// Huffman coding algorithm
|
//// Huffman coding algorithm
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
// // buildHuffmanTree(cin);
|
// buildHuffmanTree(cin);
|
||||||
// // 10111010 11110101
|
// 10111010 11110101
|
||||||
// stringstream ss("\xba\xf5");
|
stringstream ss("\xba\xf5");
|
||||||
// ibitstream ibs(ss);
|
ibitstream ibs(ss);
|
||||||
|
|
||||||
// // 10111010 11110101
|
// 10111010 11110101
|
||||||
// // ^^
|
// ^^
|
||||||
// int a = ibs.getbits(2);
|
int a = ibs.getbits(2);
|
||||||
// cout << bitset<2>(a) << endl; // 01
|
cout << bitset<2>(a) << endl; // 01
|
||||||
|
|
||||||
// // 10111010 11110101
|
// 10111010 11110101
|
||||||
// // ^^^^
|
// ^^^^
|
||||||
// int b = ibs.getbits(4);
|
int b = ibs.getbits(4);
|
||||||
// cout << bitset<4>(b) << endl; // 0111
|
cout << bitset<4>(b) << endl; // 0111
|
||||||
|
|
||||||
|
|
||||||
// // 10111010 11110101
|
// 10111010 11110101
|
||||||
// // ^^ ^^^^^^^
|
// ^^ ^^^^^^^
|
||||||
// b = ibs.getbits(9);
|
b = ibs.getbits(9);
|
||||||
// cout << bitset<9>(b) << endl; // 011010111
|
cout << bitset<9>(b) << endl; // 011010111
|
||||||
|
|
||||||
// // 10111010 11110101
|
// 10111010 11110101
|
||||||
// // ^ + overflow
|
// ^ + overflow
|
||||||
// try {
|
try {
|
||||||
// b = ibs.getbits(10);
|
b = ibs.getbits(10);
|
||||||
// cout << b << endl;
|
cout << b << endl;
|
||||||
// } catch (std::runtime_error &e) {
|
} catch (std::runtime_error &e) {
|
||||||
// cout << "Got runtime error: " << e.what() << endl;
|
cout << "Got runtime error: " << e.what() << endl;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// ostringstream so("bits: ");
|
ostringstream so("bits: ");
|
||||||
// obitstream obs(so);
|
obitstream obs(so);
|
||||||
|
|
||||||
// obs.writebits(0xf, 3);
|
obs.writebits(0xf, 3);
|
||||||
// cout << "After 3 bits: " << so.str() << endl;
|
cout << "After 3 bits: " << so.str() << endl;
|
||||||
// // cache here: 00000111
|
// cache here: 00000111
|
||||||
|
|
||||||
|
|
||||||
// // de = 11011110
|
// de = 11011110
|
||||||
// obs.writebits(0xde, 7);
|
obs.writebits(0xde, 7);
|
||||||
// // here: Flush: 11101111
|
// here: Flush: 11101111
|
||||||
// cout << "After 7 bits: " << so.str() << endl;
|
cout << "After 7 bits: " << so.str() << endl;
|
||||||
// obs.flush();
|
obs.flush();
|
||||||
// // here: Flush: 00000001
|
// here: Flush: 00000001
|
||||||
// cout << "After flush: " << so.str() << endl;
|
cout << "After flush: " << so.str() << endl;
|
||||||
|
|
||||||
|
|
||||||
// obs.writebits(0xbeef, 16);
|
obs.writebits(0xbeef, 16);
|
||||||
// cout << "After 0xbeef: " << so.str() << endl;
|
cout << "After 0xbeef: " << so.str() << endl;
|
||||||
// obs.flush();
|
obs.flush();
|
||||||
// // here: Flush: 0xEFBE - reversed!
|
cout << "After flush: " << so.str() << endl;
|
||||||
// cout << "After flush: " << so.str() << endl;
|
|
||||||
|
|
||||||
|
|
||||||
// string s = ;
|
// // string s = ;
|
||||||
|
|
||||||
stringstream ss1("Some long text!!!!\x01\x02\x03\x04\n123123456789 privet, masha!");
|
// stringstream ss1("Some long text!!!!\x01\x02\x03\x04\n123123456789 privet, masha!");
|
||||||
stringstream ss2("Some long text!!!!\x01\x02\x03\x04\n123123456789 privet, masha!");
|
// stringstream ss2("Some long text!!!!\x01\x02\x03\x04\n123123456789 privet, masha!");
|
||||||
stringstream so1;
|
// stringstream so1;
|
||||||
stringstream so2;
|
// stringstream so2;
|
||||||
|
|
||||||
MashZip mz;
|
// MashZip mz;
|
||||||
|
|
||||||
mz.mashzip_file(ss1, ss2, so1);
|
// mz.mashzip_file(ss1, ss2, so1);
|
||||||
|
|
||||||
std::cout << "After mashzip: " << so1.str();
|
// // std::cout << "After mashzip: " << so1.str();
|
||||||
|
// std::cout << so1.str();
|
||||||
|
|
||||||
mz.unmashzip_stream(so1, so2);
|
// mz.unmashzip_stream(so1, so2);
|
||||||
|
|
||||||
std::cout << "After unmashzip: " << so2.str();
|
// std::cout << "After unmashzip: " << so2.str();
|
||||||
|
|
||||||
// HuffmanTable ht(ss1);
|
// HuffmanTable ht(ss1);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user