7#include "absl/status/status.h"
8#include "absl/status/statusor.h"
13#define DEBUG_LOG(msg) std::cout << msg << std::endl
19 int const oldsize,
int*
const size,
22 (
unsigned char*)malloc(0x1000);
24 int i, j, k, l, m = 0, n, o = 0, bd = 0, p, q = 0, r;
26 for (i = 0; i < oldsize;) {
34 for (j = 0; j < i - 1; j++) {
38 for (n = 0; n < m; n++)
39 if (src[n + j] != src[n + i])
break;
41 if (n > k) k = n, o = j;
45 for (n = i + 1; n < oldsize; n++) {
62 for (n = i + 2; n < oldsize; n++) {
63 if (src[n] != l)
break;
67 if (src[n] != m)
break;
77 for (n = 1; n < m; n++)
78 if (src[i + n] != l + n)
break;
87 if (k > 3 + r && k > n + (p & 1)) p = 4, n = k;
96 b2[bd++] = (
unsigned char)(224 + (q >> 8));
99 b2[bd++] = (
unsigned char)q;
102 memcpy(b2 + bd, src + i - q, q);
112 b2[bd++] = (
unsigned char)(224 + (n >> 8) + (p << 2));
113 b2[bd++] = (
unsigned char)n;
115 b2[bd++] = (
unsigned char)((p << 5) + n);
120 b2[bd++] = (
unsigned char)l;
124 b2[bd++] = (
unsigned char)l;
125 b2[bd++] = (
unsigned char)m;
131 b2[bd++] = (
unsigned char)(o >> 8);
132 b2[bd++] = (
unsigned char)o;
134 b2[bd++] = (
unsigned char)o;
135 b2[bd++] = (
unsigned char)(o >> 8);
147 b2[bd++] = (
unsigned char)(224 + (q >> 8));
150 b2[bd++] = (
unsigned char)q;
153 memcpy(b2 + bd, src + i - q, q);
159 b2 = (
unsigned char*)realloc(b2, bd);
162 std::vector<uint8_t> compressed_data(b2, b2 + bd);
164 return compressed_data;
168 int const p_big_endian) {
169 unsigned char* b2 = (
unsigned char*)malloc(1024);
171 int bd = 0, bs = 1024;
182 if (a == 0xff)
break;
193 c = ((a & 0x0003) << 8);
197 c = (uint16_t)(a & 31);
201 if ((bd + c) > (bs - 512)) {
204 b2 = (uint8_t*)realloc(b2, bs);
216 memcpy(b2 + bd, src, c);
230 memset(b2 + bd, *(src++), c);
278 d = (*src << 8) + src[1];
292 b2 = (
unsigned char*)realloc(b2, bd);
294 if (size) (*size) = bd;
297 std::vector<uint8_t> decompressed_data(b2, b2 + bd);
299 return decompressed_data;
305 std::cout <<
"Command: " << std::to_string(piece->command) <<
"\n";
306 std::cout <<
"Command Length: " << piece->length <<
"\n";
307 std::cout <<
"Argument: ";
308 auto arg_size = piece->argument.size();
309 for (
int i = 0; i < arg_size; ++i) {
310 printf(
"%02X ", piece->argument.at(i));
312 std::cout <<
"\nArgument Length: " << piece->argument_length <<
"\n";
316 auto compressed_chain = chain_head->next;
317 while (compressed_chain !=
nullptr) {
318 std::cout <<
"- Compression Piece -\n";
320 compressed_chain = compressed_chain->next;
326 const unsigned int last_pos) {
327 unsigned int pos = src_data_pos;
328 char byte_to_repeat = rom_data[pos];
329 while (pos <= last_pos && rom_data[pos] == byte_to_repeat) {
338 const unsigned int last_pos) {
339 if (src_data_pos + 2 <= last_pos &&
340 rom_data[src_data_pos] != rom_data[src_data_pos + 1]) {
341 unsigned int pos = src_data_pos;
342 char byte1 = rom_data[pos];
343 char byte2 = rom_data[pos + 1];
346 while (pos + 1 <= last_pos) {
347 if (rom_data[pos] == byte1 && rom_data[pos + 1] == byte2)
360 const unsigned int last_pos) {
361 unsigned int pos = src_data_pos;
362 char byte = rom_data[pos];
366 while (pos <= last_pos &&
byte == rom_data[pos]) {
376 const unsigned int last_pos,
unsigned int start) {
377 if (src_data_pos != start) {
378 unsigned int searching_pos = start;
379 unsigned int current_pos_u = src_data_pos;
380 unsigned int copied_size = 0;
381 unsigned int search_start = start;
383 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
384 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
385 searching_pos < src_data_pos)
387 search_start = searching_pos;
388 while (current_pos_u <= last_pos &&
389 rom_data[current_pos_u] == rom_data[searching_pos] &&
390 searching_pos < src_data_pos) {
396 search_start -= start;
397 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
402 current_pos_u = src_data_pos;
412 uint& cmd_with_max) {
413 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
414 unsigned int cmd_size_taken = data_size_taken[cmd_i];
419 if (cmd_size_taken > max_win && cmd_size_taken > cmd_size[cmd_i] &&
421 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
422 cmd_with_max = cmd_i;
423 max_win = cmd_size_taken;
432 uint& src_data_pos, uint& comp_accumulator,
433 uint& cmd_with_max, uint& max_win) {
434 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
436 buffer.push_back(cmd_args[cmd_with_max][0]);
437 if (cmd_size[cmd_with_max] == 2) {
438 buffer.push_back(cmd_args[cmd_with_max][1]);
441 auto new_comp_piece = std::make_shared<CompressionPiece>(
442 cmd_with_max, max_win, buffer, cmd_size[cmd_with_max]);
445 if (comp_accumulator != 0) {
446 std::string copy_buff;
447 copy_buff.resize(comp_accumulator);
448 for (
int i = 0; i < comp_accumulator; ++i) {
449 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
451 auto copy_chunk = std::make_shared<CompressionPiece>(
453 compressed_chain->next = copy_chunk;
454 compressed_chain = copy_chunk;
456 compressed_chain->next = new_comp_piece;
457 compressed_chain = new_comp_piece;
459 src_data_pos += max_win;
460 comp_accumulator = 0;
466 while (src_pos + i < last_pos && data[src_pos] == data[src_pos + i]) {
475 if (src_pos + 2 <= last_pos && data[src_pos] != data[src_pos + 1]) {
476 unsigned int pos = src_pos;
477 char byte1 = data[pos];
478 char byte2 = data[pos + 1];
481 while (pos + 1 <= last_pos) {
482 if (data[pos] == byte1 && data[pos + 1] == byte2)
495 unsigned int pos = src_data_pos;
496 char byte = rom_data[pos];
500 while (pos <= last_pos &&
byte == rom_data[pos]) {
509 const unsigned int last_pos,
unsigned int start,
511 if (src_data_pos != start) {
512 unsigned int searching_pos = start;
513 unsigned int current_pos_u = src_data_pos;
514 unsigned int copied_size = 0;
515 unsigned int search_start = start;
517 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
518 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
519 searching_pos < src_data_pos)
521 search_start = searching_pos;
522 while (current_pos_u <= last_pos &&
523 rom_data[current_pos_u] == rom_data[searching_pos] &&
524 searching_pos < src_data_pos) {
530 search_start -= start;
531 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
536 current_pos_u = src_data_pos;
547 uint& cmd_with_max) {
548 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
549 unsigned int cmd_size_taken = cmd.
data_size[cmd_i];
553 if (cmd_size_taken > max_win && cmd_size_taken >
kCommandSizes[cmd_i] &&
555 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
556 cmd_with_max = cmd_i;
557 max_win = cmd_size_taken;
565 uint& src_data_pos, uint& comp_accumulator,
566 uint& cmd_with_max, uint& max_win) {
567 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
569 buffer.push_back(cmd.
arguments[cmd_with_max][0]);
570 if (cmd.
cmd_size[cmd_with_max] == 2) {
571 buffer.push_back(cmd.
arguments[cmd_with_max][1]);
574 auto new_comp_piece = std::make_shared<CompressionPiece>(
575 cmd_with_max, max_win, buffer, cmd.
cmd_size[cmd_with_max]);
578 if (comp_accumulator != 0) {
579 std::string copy_buff;
580 copy_buff.resize(comp_accumulator);
581 for (
int i = 0; i < comp_accumulator; ++i) {
582 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
584 auto copy_chunk = std::make_shared<CompressionPiece>(
586 compressed_chain->next = copy_chunk;
587 compressed_chain = copy_chunk;
589 compressed_chain->next = new_comp_piece;
590 compressed_chain = new_comp_piece;
592 src_data_pos += max_win;
593 comp_accumulator = 0;
599 uint& uncompressed_data_size, uint& best_command, uint& best_command_gain) {
600 std::cout <<
"- Identified a gain from command: " << best_command
604 std::string argument_buffer;
605 argument_buffer.push_back(command.
arguments[best_command][0]);
606 if (command.
cmd_size[best_command] == 2) {
607 argument_buffer.push_back(command.
arguments[best_command][1]);
611 auto new_compression_piece = std::make_shared<CompressionPiece>(
612 best_command, best_command_gain, argument_buffer,
618 if (uncompressed_data_size != 0) {
619 std::string copy_buffer(uncompressed_data_size, 0);
620 for (
int i = 0; i < uncompressed_data_size; ++i) {
622 rom_data[i + source_data_position - uncompressed_data_size];
624 auto direct_copy_piece = std::make_shared<CompressionPiece>(
626 uncompressed_data_size);
629 compressed_chain->next = direct_copy_piece;
630 compressed_chain = direct_copy_piece;
634 compressed_chain->next = new_compression_piece;
635 compressed_chain = new_compression_piece;
639 source_data_position += best_command_gain;
640 uncompressed_data_size = 0;
649 switch (piece->command) {
652 new_piece = std::make_shared<CompressionPiece>(
653 piece->command, length_left, piece->argument, piece->argument_length);
656 new_piece = std::make_shared<CompressionPiece>(
657 piece->command, length_left, piece->argument, piece->argument_length);
658 new_piece->argument[0] =
664 new_piece = std::make_shared<CompressionPiece>(
665 piece->command, length_left, empty, length_left);
667 for (
int i = 0; i < length_left; ++i) {
674 unsigned int offset = piece->argument[0] + (piece->argument[1] << 8);
675 new_piece = std::make_shared<CompressionPiece>(
676 piece->command, length_left, piece->argument, piece->argument_length);
678 new_piece->argument[0] =
683 new_piece->argument[1] =
689 return absl::InvalidArgumentError(
690 "SplitCompressionCommand: Invalid Command");
698 unsigned int pos = 0;
700 std::vector<uint8_t> output;
702 while (piece !=
nullptr) {
704 output.push_back(
BUILD_HEADER(piece->command, piece->length));
709 ((uint8_t)piece->command << 2) |
710 (((piece->length - 1) & 0xFF00) >> 8));
712 printf(
"Building extended header : cmd: %d, length: %d - %02X\n",
713 piece->command, piece->length, output[pos - 1]);
714 output.push_back(((piece->length - 1) & 0x00FF));
719 if (!new_piece.ok()) {
720 std::cout << new_piece.status().ToString() << std::endl;
722 printf(
"New added piece\n");
723 auto piece_data = new_piece.value();
725 piece_data->next = piece->next;
726 piece->next = piece_data;
733 tmp[0] = piece->argument[0];
734 tmp[1] = piece->argument[1];
736 tmp[0] = piece->argument[1];
737 tmp[1] = piece->argument[0];
739 for (
const auto& each : tmp) {
740 output.push_back(each);
744 for (
int i = 0; i < piece->argument_length; ++i) {
745 output.push_back(piece->argument[i]);
749 pos += piece->argument_length;
757 int mode,
int start,
int src_data_pos) {
758 if (chain_head->next !=
nullptr) {
764 if (!std::equal(decomp_data.begin() + start, decomp_data.end(),
766 return absl::InternalError(absl::StrFormat(
767 "Compressed data does not match uncompressed data at %d\n",
768 (uint)(src_data_pos - start)));
771 return absl::OkStatus();
778 while (piece !=
nullptr) {
782 unsigned int previous_length = piece->length;
783 piece->length = piece->length + piece->next->length;
785 for (
int i = 0; i < piece->next->argument_length; ++i) {
786 piece->argument[i + previous_length] = piece->next->argument[i];
788 piece->argument_length = piece->length;
791 auto p_next_next = piece->next->next;
792 piece->next = p_next_next;
800absl::StatusOr<std::vector<uint8_t>>
CompressV2(
const uint8_t* data,
802 const int length,
int mode,
806 return std::vector<uint8_t>();
810 std::string worst_case =
"aaa";
811 auto compressed_chain =
812 std::make_shared<CompressionPiece>(1, 1, worst_case, 2);
813 auto compressed_chain_start = compressed_chain;
819 unsigned int src_pos = start;
820 unsigned int last_pos = start + length - 1;
821 unsigned int comp_accumulator = 0;
832 unsigned int max_win = 2;
846 if (comp_accumulator == 32 || src_pos > last_pos) {
847 std::string buffer =
SetBuffer(data, src_pos, comp_accumulator);
848 auto new_comp_piece = std::make_shared<CompressionPiece>(
850 compressed_chain->next = new_comp_piece;
851 comp_accumulator = 0;
855 src_pos, comp_accumulator, cmd_with_max,
859 if (src_pos > last_pos) {
860 printf(
"Breaking compression loop\n");
889 const std::vector<uint8_t> data,
const int pos,
const int length) {
894 unsigned int pos = context.
src_pos;
897 if (pos == 0 || context.
data[pos - 1] != context.
data[pos]) {
898 char byte_to_repeat = context.
data[pos];
899 while (pos <= context.
last_pos && context.
data[pos] == byte_to_repeat) {
907 DEBUG_LOG(
"CheckByteRepeatV3: byte_to_repeat = "
908 << (
int)byte_to_repeat <<
", size = "
915 unsigned int pos = context.
src_pos;
916 char byte1 = context.
data[pos];
917 char byte2 = context.
data[pos + 1];
920 while (pos + 1 <= context.
last_pos) {
921 if (context.
data[pos] == byte1 && context.
data[pos + 1] == byte2)
940 unsigned int pos = context.
src_pos;
941 uint8_t
byte = context.
data[pos];
946 while (pos <= context.
last_pos &&
byte == context.
data[pos]) {
955 context.
src_pos > 0 && pos < context.
data.size() &&
972 const int window_size =
978 context.
src_pos + window_size <= context.
data.size()) {
979 unsigned int max_copied_size = 0;
980 unsigned int best_search_start = 0;
983 for (
int win_pos = 1; win_pos < window_size && win_pos < context.
src_pos;
985 auto start_search_from = context.
data.begin() + context.
src_pos - win_pos;
986 auto search_end = context.
data.begin() + context.
src_pos;
990 auto found_pos = std::search(
991 start_search_from, search_end, context.
data.begin() + context.
src_pos,
994 if (found_pos != search_end) {
996 unsigned int len = 0;
997 while (context.
src_pos + len < context.
data.size() &&
998 context.
data[context.
src_pos + len] == *(found_pos + len)) {
1002 if (len > max_copied_size) {
1003 max_copied_size = len;
1004 best_search_start = found_pos - context.
data.begin();
1009 if (max_copied_size >
1011 DEBUG_LOG(
"CheckIntraCopyV3: Detected repeating sequence of length "
1012 << max_copied_size <<
" starting from " << best_search_start);
1017 best_search_start >> 8;
1020 DEBUG_LOG(
"CheckIntraCopyV3: max_copied_size = " << max_copied_size
1021 <<
", best_search_start = "
1022 << best_search_start);
1043 DEBUG_LOG(
"CheckAvailableCompressionCommands: src_pos = " << context.
src_pos);
1047 int max_net_savings = -1;
1052 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
1063 context.
src_pos + cmd_size_taken < context.
data.size()) {
1064 char prev_byte = context.
data[context.
src_pos - 1];
1065 char next_byte = context.
data[context.
src_pos + cmd_size_taken];
1066 if (prev_byte != next_byte && cmd_size_taken == 3) {
1072 if (net_savings > max_net_savings) {
1074 max_net_savings = net_savings;
1078 DEBUG_LOG(
"DetermineBestCompression: cmd_with_max = "
1090 std::vector<uint8_t> uncompressed_data(
1094 uncompressed_data.begin(),
1095 uncompressed_data.end());
1121 std::vector<uint8_t> uncompressed_data(
1125 uncompressed_data.begin(),
1126 uncompressed_data.end());
1131 <<
", compressed_data size = "
1136 DEBUG_LOG(
"AddCompressionToChain: Adding command arguments: ");
1143 std::vector<uint8_t> uncompressed_data(
1147 uncompressed_data.begin(),
1148 uncompressed_data.end());
1158 DEBUG_LOG(
"AddCompressionToChain: (Before) src_pos = "
1173 DEBUG_LOG(
"AddCompressionToChain: (After) src_pos = "
1185 if (!std::equal(decomp_data.begin() + context.
start, decomp_data.end(),
1186 temp_rom.
begin())) {
1187 return absl::InternalError(absl::StrFormat(
1188 "Compressed data does not match uncompressed data at %d\n",
1192 return absl::OkStatus();
1214 std::string empty_string =
"";
1218 for (
int i = 0; i < length_left; ++i) {
1238 return absl::InvalidArgumentError(
1239 "SplitCompressionCommand: Invalid Command");
1247 unsigned int pos = 0;
1258 (((piece.
length - 1) & 0xFF00) >> 8));
1260 std::cout <<
"Building extended header : cmd: " << piece.
command
1261 <<
", length: " << piece.
length <<
" - "
1264 ((piece.
length - 1) & 0x00FF));
1268 if (!new_piece.ok()) {
1269 std::cout << new_piece.status().ToString() << std::endl;
1285 for (
const auto& each : tmp) {
1309 DEBUG_LOG(
"FinalizeCompression: compressed_data size = "
1314 const std::vector<uint8_t>& data,
const int start,
const int length,
1315 int mode,
bool check) {
1317 return std::vector<uint8_t>();
1345std::string
SetBuffer(
const uint8_t* data,
int src_pos,
int comp_accumulator) {
1347 for (
int i = 0; i < comp_accumulator; ++i) {
1348 buffer.push_back(data[i + src_pos - comp_accumulator]);
1353std::string
SetBuffer(
const std::vector<uint8_t>& data,
int src_pos,
1354 int comp_accumulator) {
1356 for (
int i = 0; i < comp_accumulator; ++i) {
1357 buffer.push_back(data[i + src_pos - comp_accumulator]);
1362void memfill(
const uint8_t* data, std::vector<uint8_t>& buffer,
int buffer_pos,
1363 int offset,
int length) {
1364 auto a = data[offset];
1365 auto b = data[offset + 1];
1366 for (
int i = 0; i < length; i = i + 2) {
1367 buffer[buffer_pos + i] = a;
1368 if ((i + 1) < length) buffer[buffer_pos + i + 1] = b;
1373 int offset,
int size,
1376 return std::vector<uint8_t>();
1379 std::vector<uint8_t> buffer(size, 0);
1380 unsigned int length = 0;
1381 unsigned int buffer_pos = 0;
1382 uint8_t command = 0;
1383 uint8_t header = data[offset];
1401 memcpy(buffer.data() + buffer_pos, data + offset, length);
1402 buffer_pos += length;
1406 memset(buffer.data() + buffer_pos, (
int)(data[offset]), length);
1407 buffer_pos += length;
1411 memfill(data, buffer, buffer_pos, offset, length);
1412 buffer_pos += length;
1416 auto inc_byte = data[offset];
1417 for (
int i = 0; i < length; i++) {
1418 buffer[buffer_pos] = inc_byte++;
1424 uint16_t s1 = ((data[offset + 1] &
kSnesByteMax) << 8);
1426 int addr = (s1 | s2);
1432 if (addr > offset) {
1433 return absl::InternalError(
1434 absl::StrFormat(
"Decompress: Offset for command copy exceeds "
1436 "(Offset : %#04x | Pos : %#06x)\n",
1439 if (buffer_pos + length >= size) {
1441 buffer.resize(size);
1443 memcpy(buffer.data() + buffer_pos, buffer.data() + addr, length);
1444 buffer_pos += length;
1448 std::cout << absl::StrFormat(
1449 "Decompress: Invalid header (Offset : %#06x, Command: %#04x)\n",
1454 header = data[offset];
1461 int pos,
int size) {
1466 int pos,
int size) {
1471 const std::vector<uint8_t> data,
int pos,
int size) {
The Rom class is used to load, save, and modify Rom data.
absl::Status LoadFromData(const std::vector< uint8_t > &data, bool z3_load=true)
#define BUILD_HEADER(command, length)
#define RETURN_IF_ERROR(expression)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Contains the LC_LZ2 compression algorithm.
void CheckIntraCopyV3(CompressionContext &context)
absl::StatusOr< std::vector< uint8_t > > CompressGraphics(const uint8_t *data, const int pos, const int length)
absl::StatusOr< std::vector< uint8_t > > CompressV3(const std::vector< uint8_t > &data, const int start, const int length, int mode, bool check)
Compresses a buffer of data using the LC_LZ2 algorithm.
constexpr int kExpandedLengthMod
constexpr int kCommandDirectCopy
void memfill(const uint8_t *data, std::vector< uint8_t > &buffer, int buffer_pos, int offset, int length)
void FinalizeCompression(CompressionContext &context)
void CheckWordRepeatV2(const uint8_t *data, uint &src_pos, const unsigned int last_pos, CompressionCommand &cmd)
void InitializeCompression(CompressionContext &context)
struct CompressionPiece CompressionPiece
absl::StatusOr< std::vector< uint8_t > > CompressOverworld(const uint8_t *data, const int pos, const int length)
constexpr int kSnesByteMax
constexpr int kNintendoMode1
void AddAlternativeCompressionCommand(const uint8_t *rom_data, CompressionPiecePointer &compressed_chain, const CompressionCommand &command, uint &source_data_position, uint &uncompressed_data_size, uint &best_command, uint &best_command_gain)
std::shared_ptr< CompressionPiece > CompressionPiecePointer
void CheckIncByteV3(CompressionContext &context)
absl::Status ValidateCompressionResultV3(const CompressionContext &context)
constexpr int kNintendoMode2
std::string SetBuffer(const uint8_t *data, int src_pos, int comp_accumulator)
constexpr int kCommandRepeatingBytes
std::array< std::array< char, 2 >, 5 > CommandArgumentArray
void CheckByteRepeat(const uint8_t *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const unsigned int last_pos)
void CheckByteRepeatV2(const uint8_t *data, uint &src_pos, const unsigned int last_pos, CompressionCommand &cmd)
void HandleDirectCopy(CompressionContext &context)
void CheckIncByteV2(const uint8_t *rom_data, uint &src_data_pos, const unsigned int last_pos, CompressionCommand &cmd)
absl::StatusOr< std::vector< uint8_t > > DecompressGraphics(const uint8_t *data, int pos, int size)
constexpr int kCommandMod
void PrintCompressionPiece(const CompressionPiecePointer &piece)
constexpr int kCommandWordFill
constexpr int kMaxLengthNormalHeader
void CheckAvailableCompressionCommands(CompressionContext &context)
constexpr int kMaxLengthCompression
void CompressionCommandAlternativeV2(const uint8_t *rom_data, const CompressionCommand &cmd, CompressionPiecePointer &compressed_chain, uint &src_data_pos, uint &comp_accumulator, uint &cmd_with_max, uint &max_win)
void CheckIntraCopyV2(const uint8_t *rom_data, uint &src_data_pos, const unsigned int last_pos, unsigned int start, CompressionCommand &cmd)
std::vector< uint8_t > CreateCompressionString(CompressionPiecePointer &start, int mode)
absl::StatusOr< std::vector< uint8_t > > DecompressOverworld(const uint8_t *data, int pos, int size)
std::array< uint, 5 > DataSizeArray
constexpr int kCommandIncreasingFill
void ValidateForByteGainV2(const CompressionCommand &cmd, uint &max_win, uint &cmd_with_max)
void ValidateForByteGain(const DataSizeArray &data_size_taken, const CommandSizeArray &cmd_size, uint &max_win, uint &cmd_with_max)
constexpr int kCommandByteFill
void PrintCompressionChain(const CompressionPiecePointer &chain_head)
void CheckIncByte(const uint8_t *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const unsigned int last_pos)
constexpr int kExpandedMod
const std::array< int, 5 > kCommandSizes
absl::StatusOr< std::vector< uint8_t > > DecompressV2(const uint8_t *data, int offset, int size, int mode)
Decompresses a buffer of data using the LC_LZ2 algorithm.
void AddCompressionToChain(CompressionContext &context)
absl::StatusOr< CompressionPiecePointer > SplitCompressionPiece(CompressionPiecePointer &piece, int mode)
void CompressionCommandAlternative(const uint8_t *rom_data, CompressionPiecePointer &compressed_chain, const CommandSizeArray &cmd_size, const CommandArgumentArray &cmd_args, uint &src_data_pos, uint &comp_accumulator, uint &cmd_with_max, uint &max_win)
constexpr int kNormalLengthMod
constexpr int kCompressionStringMod
void CheckWordRepeat(const uint8_t *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const unsigned int last_pos)
void CheckByteRepeatV3(CompressionContext &context)
CompressionPiecePointer MergeCopy(CompressionPiecePointer &start)
std::array< uint, 5 > CommandSizeArray
void DetermineBestCompression(CompressionContext &context)
absl::StatusOr< CompressionPiece > SplitCompressionPieceV3(CompressionPiece &piece, int mode)
absl::StatusOr< std::vector< uint8_t > > CompressV2(const uint8_t *data, const int start, const int length, int mode, bool check)
Compresses a buffer of data using the LC_LZ2 algorithm.
void CheckWordRepeatV3(CompressionContext &context)
void CheckIntraCopy(const uint8_t *rom_data, DataSizeArray &data_size_taken, CommandArgumentArray &cmd_args, uint &src_data_pos, const unsigned int last_pos, unsigned int start)
absl::Status ValidateCompressionResult(CompressionPiecePointer &chain_head, int mode, int start, int src_data_pos)
std::vector< uint8_t > HyruleMagicDecompress(uint8_t const *src, int *const size, int const p_big_endian)
std::vector< uint8_t > HyruleMagicCompress(uint8_t const *const src, int const oldsize, int *const size, int const flag)
uint16_t ldle16b(uint8_t const *const p_arr)
void stle16b(uint8_t *const p_arr, uint16_t const p_val)
Main namespace for the application.
std::array< uint, 5 > data_size
std::array< uint, 5 > cmd_size
std::array< std::array< char, 2 >, 5 > arguments
std::vector< uint8_t > data
CompressionCommand current_cmd
std::vector< CompressionPiece > compression_pieces
std::vector< uint8_t > compression_string
unsigned int cmd_with_max
std::vector< uint8_t > compressed_data
unsigned int comp_accumulator