diff --git a/MashZip.cpp b/MashZip.cpp new file mode 100644 index 0000000..6b394be --- /dev/null +++ b/MashZip.cpp @@ -0,0 +1,66 @@ +#include "MashZip.h" +#include + +void MashZip::unmashzip_stream(std::basic_istream &is, std::basic_ostream &os) +{ + char magic[5] = { 0 }; + char header[HEADER_SIZE] = { 0 }; + + is.read(magic, 4); + if (strcmp(magic, MAGIC) != 0) + { + throw std::runtime_error("MAGIC mismatch! Must start with MASH!"); + } + + is.read(header, HEADER_SIZE); + + // for (size_t i = 0; i < 128; i++) { + // std::cerr << "Header: " << (int) header[i] << std::endl; + // } + + // load huffman table + HuffmanTable ht(header); + ibitstream ibs(is); + + char sym = '\0'; + + while (true) + { + try { + sym = ht.decode_one_symbol(ibs); + } catch (std::runtime_error &e) { + std::cerr << e.what() << std::endl; + break; + } + os << sym; + } + os.flush(); +} + +void MashZip::mashzip_file(std::basic_istream &cs, std::basic_istream &is, std::basic_ostream &os) +{ + // std::ifstream ifs(name, std::ios::binary); + // is.seekg(0); + + os.write(MAGIC, 4); + + // build huffman table by file + HuffmanTable ht(cs); + // is.seekg(0); // reset position as ht reads whole file + + os.write(ht.to_header(), HEADER_SIZE); + + obitstream obs(os); + + char c; + + // std::cerr << "Pos in stream: " << is. << std::endl; + + while (is >> c) + { + + ht.write_symbol(obs, c); + } + obs.flush(); + os.flush(); +}