8#include "absl/status/status.h"
9#include "absl/status/statusor.h"
10#include "absl/strings/str_format.h"
15#define DEBUG_LOG(msg) std::cout << msg << std::endl
21 int const oldsize,
int*
const size,
24 (
unsigned char*)malloc(0x1000);
26 int i, j, k, l, m = 0, n, o = 0, bd = 0, p, q = 0, r;
28 for (i = 0; i < oldsize;) {
36 for (j = 0; j < i - 1; j++) {
40 for (n = 0; n < m; n++)
41 if (src[n + j] != src[n + i])
49 for (n = i + 1; n < oldsize; n++) {
66 for (n = i + 2; n < oldsize; n++) {
83 for (n = 1; n < m; n++)
84 if (src[i + n] != l + n)
94 if (k > 3 + r && k > n + (p & 1))
104 b2[bd++] = (
unsigned char)(224 + (q >> 8));
107 b2[bd++] = (
unsigned char)q;
110 memcpy(b2 + bd, src + i - q, q);
120 b2[bd++] = (
unsigned char)(224 + (n >> 8) + (p << 2));
121 b2[bd++] = (
unsigned char)n;
123 b2[bd++] = (
unsigned char)((p << 5) + n);
128 b2[bd++] = (
unsigned char)l;
132 b2[bd++] = (
unsigned char)l;
133 b2[bd++] = (
unsigned char)m;
139 b2[bd++] = (
unsigned char)(o >> 8);
140 b2[bd++] = (
unsigned char)o;
142 b2[bd++] = (
unsigned char)o;
143 b2[bd++] = (
unsigned char)(o >> 8);
155 b2[bd++] = (
unsigned char)(224 + (q >> 8));
158 b2[bd++] = (
unsigned char)q;
161 memcpy(b2 + bd, src + i - q, q);
167 b2 = (
unsigned char*)realloc(b2, bd);
170 std::vector<uint8_t> compressed_data(b2, b2 + bd);
172 return compressed_data;
176 int const p_big_endian,
177 size_t max_src_size) {
178 unsigned char* b2 = (
unsigned char*)malloc(1024);
179 const uint8_t*
const src_start = src;
181 int bd = 0, bs = 1024;
189 if (max_src_size !=
static_cast<size_t>(-1) &&
190 (size_t)(src - src_start) >= max_src_size) {
191 std::cerr <<
"HyruleMagicDecompress: Reached end of buffer unexpectedly."
212 c = ((a & 0x0003) << 8);
215 if (max_src_size !=
static_cast<size_t>(-1) &&
216 (size_t)(src - src_start) >= max_src_size) {
217 std::cerr <<
"HyruleMagicDecompress: Reached end of buffer reading "
225 c = (uint16_t)(a & 31);
229 if ((bd + c) > (bs - 512)) {
233 if (bs > 1024 * 1024 * 16) {
234 std::cerr <<
"HyruleMagicDecompress: Excessive allocation detected."
237 return std::vector<uint8_t>();
239 unsigned char* new_b2 = (
unsigned char*)realloc(b2, bs);
242 return std::vector<uint8_t>();
255 if (max_src_size !=
static_cast<size_t>(-1) &&
256 (
size_t)(src - src_start + c) > max_src_size) {
257 std::cerr <<
"HyruleMagicDecompress: Raw copy exceeds buffer."
259 goto end_decompression;
264 memcpy(b2 + bd, src, c);
278 if (max_src_size !=
static_cast<size_t>(-1) &&
279 (
size_t)(src - src_start) >= max_src_size) {
280 std::cerr <<
"HyruleMagicDecompress: RLE copy exceeds buffer."
282 goto end_decompression;
286 memset(b2 + bd, *(src++), c);
298 if (max_src_size !=
static_cast<size_t>(-1) &&
299 (
size_t)(src - src_start + 2) > max_src_size) {
300 std::cerr <<
"HyruleMagicDecompress: RLE 16-bit copy exceeds buffer."
302 goto end_decompression;
327 if (max_src_size !=
static_cast<size_t>(-1) &&
328 (size_t)(src - src_start) >= max_src_size) {
329 std::cerr <<
"HyruleMagicDecompress: Inc copy exceeds buffer."
331 goto end_decompression;
350 if (max_src_size !=
static_cast<size_t>(-1) &&
351 (size_t)(src - src_start + 2) > max_src_size) {
352 std::cerr <<
"HyruleMagicDecompress: LZ copy exceeds buffer."
354 goto end_decompression;
358 d = (*src << 8) + src[1];
393 std::cerr <<
"HyruleMagicDecompress: LZ ref out of bounds."
395 goto end_decompression;
406 b2 = (
unsigned char*)realloc(b2, bd);
412 std::vector<uint8_t> decompressed_data(b2, b2 + bd);
414 return decompressed_data;
420 std::cout <<
"Command: " << std::to_string(piece->command) <<
"\n";
421 std::cout <<
"Command Length: " << piece->length <<
"\n";
422 std::cout <<
"Argument: ";
423 auto arg_size = piece->argument.size();
424 for (
int i = 0; i < arg_size; ++i) {
425 printf(
"%02X ", piece->argument.at(i));
427 std::cout <<
"\nArgument Length: " << piece->argument_length <<
"\n";
431 auto compressed_chain = chain_head->next;
432 while (compressed_chain !=
nullptr) {
433 std::cout <<
"- Compression Piece -\n";
435 compressed_chain = compressed_chain->next;
441 const unsigned int last_pos) {
442 unsigned int pos = src_data_pos;
443 char byte_to_repeat = rom_data[pos];
444 while (pos <= last_pos && rom_data[pos] == byte_to_repeat) {
453 const unsigned int last_pos) {
454 if (src_data_pos + 2 <= last_pos &&
455 rom_data[src_data_pos] != rom_data[src_data_pos + 1]) {
456 unsigned int pos = src_data_pos;
457 char byte1 = rom_data[pos];
458 char byte2 = rom_data[pos + 1];
461 while (pos + 1 <= last_pos) {
462 if (rom_data[pos] == byte1 && rom_data[pos + 1] == byte2)
475 const unsigned int last_pos) {
476 unsigned int pos = src_data_pos;
477 char byte = rom_data[pos];
481 while (pos <= last_pos &&
byte == rom_data[pos]) {
491 const unsigned int last_pos,
unsigned int start) {
492 if (src_data_pos != start) {
493 unsigned int searching_pos = start;
494 unsigned int current_pos_u = src_data_pos;
495 unsigned int copied_size = 0;
496 unsigned int search_start = start;
498 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
499 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
500 searching_pos < src_data_pos)
502 search_start = searching_pos;
503 while (current_pos_u <= last_pos &&
504 rom_data[current_pos_u] == rom_data[searching_pos] &&
505 searching_pos < src_data_pos) {
511 search_start -= start;
512 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
517 current_pos_u = src_data_pos;
527 uint& cmd_with_max) {
528 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
529 unsigned int cmd_size_taken = data_size_taken[cmd_i];
534 if (cmd_size_taken > max_win && cmd_size_taken > cmd_size[cmd_i] &&
536 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
537 cmd_with_max = cmd_i;
538 max_win = cmd_size_taken;
547 uint& src_data_pos,
uint& comp_accumulator,
548 uint& cmd_with_max,
uint& max_win) {
549 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
551 buffer.push_back(cmd_args[cmd_with_max][0]);
552 if (cmd_size[cmd_with_max] == 2) {
553 buffer.push_back(cmd_args[cmd_with_max][1]);
556 auto new_comp_piece = std::make_shared<CompressionPiece>(
557 cmd_with_max, max_win, buffer, cmd_size[cmd_with_max]);
560 if (comp_accumulator != 0) {
561 std::string copy_buff;
562 copy_buff.resize(comp_accumulator);
563 for (
int i = 0; i < comp_accumulator; ++i) {
564 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
566 auto copy_chunk = std::make_shared<CompressionPiece>(
568 compressed_chain->next = copy_chunk;
569 compressed_chain = copy_chunk;
571 compressed_chain->next = new_comp_piece;
572 compressed_chain = new_comp_piece;
574 src_data_pos += max_win;
575 comp_accumulator = 0;
581 while (src_pos + i < last_pos && data[src_pos] == data[src_pos + i]) {
590 if (src_pos + 2 <= last_pos && data[src_pos] != data[src_pos + 1]) {
591 unsigned int pos = src_pos;
592 char byte1 = data[pos];
593 char byte2 = data[pos + 1];
596 while (pos + 1 <= last_pos) {
597 if (data[pos] == byte1 && data[pos + 1] == byte2)
610 unsigned int pos = src_data_pos;
611 char byte = rom_data[pos];
615 while (pos <= last_pos &&
byte == rom_data[pos]) {
624 const unsigned int last_pos,
unsigned int start,
626 if (src_data_pos != start) {
627 unsigned int searching_pos = start;
628 unsigned int current_pos_u = src_data_pos;
629 unsigned int copied_size = 0;
630 unsigned int search_start = start;
632 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
633 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
634 searching_pos < src_data_pos)
636 search_start = searching_pos;
637 while (current_pos_u <= last_pos &&
638 rom_data[current_pos_u] == rom_data[searching_pos] &&
639 searching_pos < src_data_pos) {
645 search_start -= start;
646 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
651 current_pos_u = src_data_pos;
662 uint& cmd_with_max) {
663 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
664 unsigned int cmd_size_taken = cmd.
data_size[cmd_i];
668 if (cmd_size_taken > max_win && cmd_size_taken >
kCommandSizes[cmd_i] &&
670 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
671 cmd_with_max = cmd_i;
672 max_win = cmd_size_taken;
680 uint& src_data_pos,
uint& comp_accumulator,
681 uint& cmd_with_max,
uint& max_win) {
682 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
684 buffer.push_back(cmd.
arguments[cmd_with_max][0]);
685 if (cmd.
cmd_size[cmd_with_max] == 2) {
686 buffer.push_back(cmd.
arguments[cmd_with_max][1]);
689 auto new_comp_piece = std::make_shared<CompressionPiece>(
690 cmd_with_max, max_win, buffer, cmd.
cmd_size[cmd_with_max]);
693 if (comp_accumulator != 0) {
694 std::string copy_buff;
695 copy_buff.resize(comp_accumulator);
696 for (
int i = 0; i < comp_accumulator; ++i) {
697 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
699 auto copy_chunk = std::make_shared<CompressionPiece>(
701 compressed_chain->next = copy_chunk;
702 compressed_chain = copy_chunk;
704 compressed_chain->next = new_comp_piece;
705 compressed_chain = new_comp_piece;
707 src_data_pos += max_win;
708 comp_accumulator = 0;
714 uint& uncompressed_data_size,
uint& best_command,
uint& best_command_gain) {
715 std::cout <<
"- Identified a gain from command: " << best_command
719 std::string argument_buffer;
720 argument_buffer.push_back(command.
arguments[best_command][0]);
721 if (command.
cmd_size[best_command] == 2) {
722 argument_buffer.push_back(command.
arguments[best_command][1]);
726 auto new_compression_piece = std::make_shared<CompressionPiece>(
727 best_command, best_command_gain, argument_buffer,
733 if (uncompressed_data_size != 0) {
734 std::string copy_buffer(uncompressed_data_size, 0);
735 for (
int i = 0; i < uncompressed_data_size; ++i) {
737 rom_data[i + source_data_position - uncompressed_data_size];
739 auto direct_copy_piece = std::make_shared<CompressionPiece>(
741 uncompressed_data_size);
744 compressed_chain->next = direct_copy_piece;
745 compressed_chain = direct_copy_piece;
749 compressed_chain->next = new_compression_piece;
750 compressed_chain = new_compression_piece;
754 source_data_position += best_command_gain;
755 uncompressed_data_size = 0;
764 switch (piece->command) {
767 new_piece = std::make_shared<CompressionPiece>(
768 piece->command, length_left, piece->argument, piece->argument_length);
771 new_piece = std::make_shared<CompressionPiece>(
772 piece->command, length_left, piece->argument, piece->argument_length);
773 new_piece->argument[0] =
779 new_piece = std::make_shared<CompressionPiece>(
780 piece->command, length_left, empty, length_left);
782 for (
int i = 0; i < length_left; ++i) {
789 unsigned int offset = piece->argument[0] + (piece->argument[1] << 8);
790 new_piece = std::make_shared<CompressionPiece>(
791 piece->command, length_left, piece->argument, piece->argument_length);
793 new_piece->argument[0] =
798 new_piece->argument[1] =
804 return absl::InvalidArgumentError(
805 "SplitCompressionCommand: Invalid Command");
813 unsigned int pos = 0;
815 std::vector<uint8_t> output;
817 while (piece !=
nullptr) {
819 output.push_back(
BUILD_HEADER(piece->command, piece->length));
824 ((uint8_t)piece->command << 2) |
825 (((piece->length - 1) & 0xFF00) >> 8));
827 printf(
"Building extended header : cmd: %d, length: %d - %02X\n",
828 piece->command, piece->length, output[pos - 1]);
829 output.push_back(((piece->length - 1) & 0x00FF));
834 if (!new_piece.ok()) {
835 std::cout << new_piece.status().ToString() << std::endl;
837 printf(
"New added piece\n");
838 auto piece_data = new_piece.value();
840 piece_data->next = piece->next;
841 piece->next = piece_data;
848 tmp[0] = piece->argument[0];
849 tmp[1] = piece->argument[1];
851 tmp[0] = piece->argument[1];
852 tmp[1] = piece->argument[0];
854 for (
const auto& each : tmp) {
855 output.push_back(each);
859 for (
int i = 0; i < piece->argument_length; ++i) {
860 output.push_back(piece->argument[i]);
864 pos += piece->argument_length;
872 int mode,
int start,
int src_data_pos) {
873 if (chain_head->next !=
nullptr) {
880 if (!std::equal(decomp_data.begin() + start, decomp_data.end(),
882 return absl::InternalError(absl::StrFormat(
883 "Compressed data does not match uncompressed data at %d\n",
884 (
uint)(src_data_pos - start)));
887 return absl::OkStatus();
894 while (piece !=
nullptr) {
898 unsigned int previous_length = piece->length;
899 piece->length = piece->length + piece->next->length;
901 for (
int i = 0; i < piece->next->argument_length; ++i) {
902 piece->argument[i + previous_length] = piece->next->argument[i];
904 piece->argument_length = piece->length;
907 auto p_next_next = piece->next->next;
908 piece->next = p_next_next;
916absl::StatusOr<std::vector<uint8_t>>
CompressV2(
const uint8_t* data,
918 const int length,
int mode,
922 return std::vector<uint8_t>();
926 std::string worst_case =
"aaa";
927 auto compressed_chain =
928 std::make_shared<CompressionPiece>(1, 1, worst_case, 2);
929 auto compressed_chain_start = compressed_chain;
935 unsigned int src_pos = start;
936 unsigned int last_pos = start + length - 1;
937 unsigned int comp_accumulator = 0;
948 unsigned int max_win = 2;
962 if (comp_accumulator == 32 || src_pos > last_pos) {
963 std::string buffer =
SetBuffer(data, src_pos, comp_accumulator);
964 auto new_comp_piece = std::make_shared<CompressionPiece>(
966 compressed_chain->next = new_comp_piece;
967 comp_accumulator = 0;
971 src_pos, comp_accumulator, cmd_with_max,
975 if (src_pos > last_pos) {
976 printf(
"Breaking compression loop\n");
1005 const std::vector<uint8_t> data,
const int pos,
const int length) {
1010 unsigned int pos = context.
src_pos;
1013 if (pos == 0 || context.
data[pos - 1] != context.
data[pos]) {
1014 char byte_to_repeat = context.
data[pos];
1015 while (pos <= context.
last_pos && context.
data[pos] == byte_to_repeat) {
1023 DEBUG_LOG(
"CheckByteRepeatV3: byte_to_repeat = "
1024 << (
int)byte_to_repeat <<
", size = "
1031 unsigned int pos = context.
src_pos;
1032 char byte1 = context.
data[pos];
1033 char byte2 = context.
data[pos + 1];
1036 while (pos + 1 <= context.
last_pos) {
1037 if (context.
data[pos] == byte1 && context.
data[pos + 1] == byte2)
1056 unsigned int pos = context.
src_pos;
1057 uint8_t
byte = context.
data[pos];
1062 while (pos <= context.
last_pos &&
byte == context.
data[pos]) {
1071 context.
src_pos > 0 && pos < context.
data.size() &&
1088 const int window_size =
1094 context.
src_pos + window_size <= context.
data.size()) {
1095 unsigned int max_copied_size = 0;
1096 unsigned int best_search_start = 0;
1099 for (
int win_pos = 1; win_pos < window_size && win_pos < context.
src_pos;
1101 auto start_search_from = context.
data.begin() + context.
src_pos - win_pos;
1102 auto search_end = context.
data.begin() + context.
src_pos;
1106 auto found_pos = std::search(
1107 start_search_from, search_end, context.
data.begin() + context.
src_pos,
1108 context.
data.begin() + context.
src_pos + win_pos);
1110 if (found_pos != search_end) {
1112 unsigned int len = 0;
1113 while (context.
src_pos + len < context.
data.size() &&
1114 context.
data[context.
src_pos + len] == *(found_pos + len)) {
1118 if (len > max_copied_size) {
1119 max_copied_size = len;
1120 best_search_start = found_pos - context.
data.begin();
1125 if (max_copied_size >
1127 DEBUG_LOG(
"CheckIntraCopyV3: Detected repeating sequence of length "
1128 << max_copied_size <<
" starting from " << best_search_start);
1133 best_search_start >> 8;
1136 DEBUG_LOG(
"CheckIntraCopyV3: max_copied_size = " << max_copied_size
1137 <<
", best_search_start = "
1138 << best_search_start);
1159 DEBUG_LOG(
"CheckAvailableCompressionCommands: src_pos = " << context.
src_pos);
1163 int max_net_savings = -1;
1168 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
1179 context.
src_pos + cmd_size_taken < context.
data.size()) {
1180 char prev_byte = context.
data[context.
src_pos - 1];
1181 char next_byte = context.
data[context.
src_pos + cmd_size_taken];
1182 if (prev_byte != next_byte && cmd_size_taken == 3) {
1188 if (net_savings > max_net_savings) {
1190 max_net_savings = net_savings;
1194 DEBUG_LOG(
"DetermineBestCompression: cmd_with_max = "
1206 std::vector<uint8_t> uncompressed_data(
1210 uncompressed_data.begin(),
1211 uncompressed_data.end());
1237 std::vector<uint8_t> uncompressed_data(
1241 uncompressed_data.begin(),
1242 uncompressed_data.end());
1247 <<
", compressed_data size = "
1252 DEBUG_LOG(
"AddCompressionToChain: Adding command arguments: ");
1259 std::vector<uint8_t> uncompressed_data(
1263 uncompressed_data.begin(),
1264 uncompressed_data.end());
1274 DEBUG_LOG(
"AddCompressionToChain: (Before) src_pos = "
1289 DEBUG_LOG(
"AddCompressionToChain: (After) src_pos = "
1302 if (!std::equal(decomp_data.begin() + context.
start, decomp_data.end(),
1303 temp_rom.
begin())) {
1304 return absl::InternalError(absl::StrFormat(
1305 "Compressed data does not match uncompressed data at %d\n",
1309 return absl::OkStatus();
1331 std::string empty_string =
"";
1335 for (
int i = 0; i < length_left; ++i) {
1355 return absl::InvalidArgumentError(
1356 "SplitCompressionCommand: Invalid Command");
1364 unsigned int pos = 0;
1375 (((piece.length - 1) & 0xFF00) >> 8));
1377 std::cout <<
"Building extended header : cmd: " << piece.command
1378 <<
", length: " << piece.length <<
" - "
1381 ((piece.length - 1) & 0x00FF));
1385 if (!new_piece.ok()) {
1386 std::cout << new_piece.status().ToString() << std::endl;
1396 tmp[0] = piece.argument[0];
1397 tmp[1] = piece.argument[1];
1399 tmp[0] = piece.argument[1];
1400 tmp[1] = piece.argument[0];
1402 for (
const auto& each : tmp) {
1407 for (
int i = 0; i < piece.argument_length; ++i) {
1412 pos += piece.argument_length;
1426 DEBUG_LOG(
"FinalizeCompression: compressed_data size = "
1431 const std::vector<uint8_t>& data,
const int start,
const int length,
1432 int mode,
bool check) {
1434 return std::vector<uint8_t>();
1462std::string
SetBuffer(
const uint8_t* data,
int src_pos,
int comp_accumulator) {
1464 for (
int i = 0; i < comp_accumulator; ++i) {
1465 buffer.push_back(data[i + src_pos - comp_accumulator]);
1470std::string
SetBuffer(
const std::vector<uint8_t>& data,
int src_pos,
1471 int comp_accumulator) {
1473 for (
int i = 0; i < comp_accumulator; ++i) {
1474 buffer.push_back(data[i + src_pos - comp_accumulator]);
1479void memfill(
const uint8_t* data, std::vector<uint8_t>& buffer,
int buffer_pos,
1480 int offset,
int length) {
1481 auto a = data[offset];
1482 auto b = data[offset + 1];
1483 for (
int i = 0; i < length; i = i + 2) {
1484 buffer[buffer_pos + i] = a;
1485 if ((i + 1) < length)
1486 buffer[buffer_pos + i + 1] = b;
1491 int offset,
int size,
1492 int mode,
size_t rom_size) {
1494 return std::vector<uint8_t>();
1499 if (rom_size !=
static_cast<size_t>(-1) &&
1500 (offset < 0 ||
static_cast<size_t>(offset) >= rom_size)) {
1501 return absl::OutOfRangeError(
1502 absl::StrFormat(
"DecompressV2: Initial offset %d exceeds ROM size %zu",
1506 std::vector<uint8_t> buffer(size, 0);
1507 unsigned int length = 0;
1508 unsigned int buffer_pos = 0;
1509 uint8_t command = 0;
1512 if (rom_size !=
static_cast<size_t>(-1) &&
1513 static_cast<size_t>(offset) >= rom_size) {
1514 return absl::OutOfRangeError(
1515 absl::StrFormat(
"DecompressV2: Initial offset %d exceeds ROM size %zu",
1518 uint8_t header = data[offset];
1522 if (rom_size !=
static_cast<size_t>(-1) &&
1523 static_cast<size_t>(offset) >= rom_size) {
1524 return absl::OutOfRangeError(absl::StrFormat(
1525 "DecompressV2: Offset %d exceeds ROM size %zu while reading command",
1531 if (rom_size !=
static_cast<size_t>(-1) &&
1532 static_cast<size_t>(offset + 1) >= rom_size) {
1533 return absl::OutOfRangeError(
1534 absl::StrFormat(
"DecompressV2: Offset %d+1 exceeds ROM size %zu "
1535 "for expanded command",
1552 if (buffer_pos + length >
static_cast<unsigned int>(size)) {
1554 int new_size = size;
1555 while (buffer_pos + length >
static_cast<unsigned int>(new_size)) {
1556 if (new_size > INT_MAX / 2) {
1557 return absl::ResourceExhaustedError(absl::StrFormat(
1558 "DecompressV2: Buffer size overflow at pos %u, length %u",
1559 buffer_pos, length));
1564 buffer.resize(size);
1570 if (rom_size !=
static_cast<size_t>(-1) &&
1571 static_cast<size_t>(offset + length) > rom_size) {
1572 return absl::OutOfRangeError(
1573 absl::StrFormat(
"DecompressV2: DirectCopy offset %d + length %u "
1574 "exceeds ROM size %zu",
1575 offset, length, rom_size));
1577 memcpy(buffer.data() + buffer_pos, data + offset, length);
1578 buffer_pos += length;
1583 if (rom_size !=
static_cast<size_t>(-1) &&
1584 static_cast<size_t>(offset) >= rom_size) {
1585 return absl::OutOfRangeError(absl::StrFormat(
1586 "DecompressV2: ByteFill offset %d exceeds ROM size %zu", offset,
1589 memset(buffer.data() + buffer_pos, (
int)(data[offset]), length);
1590 buffer_pos += length;
1595 if (rom_size !=
static_cast<size_t>(-1) &&
1596 static_cast<size_t>(offset + 1) >= rom_size) {
1597 return absl::OutOfRangeError(absl::StrFormat(
1598 "DecompressV2: WordFill offset %d+1 exceeds ROM size %zu", offset,
1601 memfill(data, buffer, buffer_pos, offset, length);
1602 buffer_pos += length;
1607 if (rom_size !=
static_cast<size_t>(-1) &&
1608 static_cast<size_t>(offset) >= rom_size) {
1609 return absl::OutOfRangeError(absl::StrFormat(
1610 "DecompressV2: IncreasingFill offset %d exceeds ROM size %zu",
1613 auto inc_byte = data[offset];
1614 for (
unsigned int i = 0; i < length; i++) {
1615 buffer[buffer_pos] = inc_byte++;
1622 if (rom_size !=
static_cast<size_t>(-1) &&
1623 static_cast<size_t>(offset + 1) >= rom_size) {
1624 return absl::OutOfRangeError(absl::StrFormat(
1625 "DecompressV2: RepeatingBytes offset %d+1 exceeds ROM size %zu",
1628 uint16_t s1 = ((data[offset + 1] &
kSnesByteMax) << 8);
1630 int addr = (s1 | s2);
1636 if (addr > offset) {
1637 return absl::InternalError(
1638 absl::StrFormat(
"Decompress: Offset for command copy exceeds "
1640 "(Offset : %#04x | Pos : %#06x)\n",
1644 memcpy(buffer.data() + buffer_pos, buffer.data() + addr, length);
1645 buffer_pos += length;
1649 std::cout << absl::StrFormat(
1650 "Decompress: Invalid header (Offset : %#06x, Command: %#04x)\n",
1655 if (rom_size !=
static_cast<size_t>(-1) &&
1656 static_cast<size_t>(offset) >= rom_size) {
1657 return absl::OutOfRangeError(
1658 absl::StrFormat(
"DecompressV2: Offset %d exceeds ROM size %zu while "
1659 "reading next header",
1662 header = data[offset];
1669 int pos,
int size) {
1674 int pos,
int size) {
1679 const std::vector<uint8_t> data,
int pos,
int size) {
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
absl::Status LoadFromData(const std::vector< uint8_t > &data, const LoadOptions &options=LoadOptions::Defaults())
#define BUILD_HEADER(command, length)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
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)
absl::StatusOr< std::vector< uint8_t > > DecompressV2(const uint8_t *data, int offset, int size, int mode, size_t rom_size)
Decompresses a buffer of data using the LC_LZ2 algorithm.
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
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, size_t max_src_size)
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)
#define RETURN_IF_ERROR(expr)
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