using namespace std;
-static string read_file(const char *filename)
-{
- FILE *fp = fopen(filename, "rb");
- if (fp == nullptr) {
- perror(filename);
- exit(1);
- }
-
- string ret;
- while (!feof(fp)) {
- int ch = getc(fp);
- if (ch == -1) {
- break;
- }
- ret.push_back(ch);
- }
-
- fclose(fp);
-
- return ret;
-}
-
static uint16_t read16(const char *ptr)
{
// We don't care about the endianness, so native is fine.
(*states)[dst_pos].match_distance = match_distance;
}
-int main(int argc, char **argv)
+string compress_konamilz77(const string &input)
{
- if (argc != 3) {
- fprintf(stderr, "USAGE: compress INFILE OUTFILE\n");
- exit(1);
- }
- string input = read_file(argv[1]);
-
// First hash all possible 2-byte matches in the file.
unordered_map<uint16_t, vector<int>> match_to_pos;
for (size_t i = 0; i < input.size() - 2; ++i) {
}
}
- if (pos % 100 == 0) {
+ if (pos % 1000 == 0) {
fprintf(stderr, "Compressing %d/%zu (%.2f%%)...\r", pos, input.size(), 100.0 * pos / input.size());
}
}
output.push_back(0xff); // EOF tag.
output[control_byte_pos] |= (1u << control_bits_used);
+ return output;
+}
+
+#ifdef COMPRESS_MAIN
+static string read_file(const char *filename)
+{
+ FILE *fp = fopen(filename, "rb");
+ if (fp == nullptr) {
+ perror(filename);
+ exit(1);
+ }
+
+ string ret;
+ while (!feof(fp)) {
+ int ch = getc(fp);
+ if (ch == -1) {
+ break;
+ }
+ ret.push_back(ch);
+ }
+
+ fclose(fp);
+
+ return ret;
+}
+
+int main(int argc, char **argv)
+{
+ if (argc != 3) {
+ fprintf(stderr, "USAGE: compress INFILE OUTFILE\n");
+ exit(1);
+ }
+ string input = read_file(argv[1]);
+ string output = compress_konamilz77(input);
+
fprintf(stderr, "End result: %zu bytes. \n", output.size());
// Write the file to disk.
exit(1);
}
}
+#endif