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 std::vector<uint8_t> b2(std::max(0x1000, oldsize * 2));
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++) {
62 for (n = i + 2; n < oldsize; n++) {
79 for (n = 1; n < m; n++)
80 if (src[i + n] != l + n)
90 if (k > 3 + r && k > n + (p & 1))
100 b2[bd++] = (
unsigned char)(224 + (q >> 8));
103 b2[bd++] = (
unsigned char)q;
106 memcpy(b2.data() + bd, src + i - q, q);
116 b2[bd++] = (
unsigned char)(224 + (n >> 8) + (p << 2));
117 b2[bd++] = (
unsigned char)n;
119 b2[bd++] = (
unsigned char)((p << 5) + n);
124 b2[bd++] = (
unsigned char)l;
128 b2[bd++] = (
unsigned char)l;
129 b2[bd++] = (
unsigned char)m;
135 b2[bd++] = (
unsigned char)(o >> 8);
136 b2[bd++] = (
unsigned char)o;
138 b2[bd++] = (
unsigned char)o;
139 b2[bd++] = (
unsigned char)(o >> 8);
151 b2[bd++] = (
unsigned char)(224 + (q >> 8));
154 b2[bd++] = (
unsigned char)q;
157 memcpy(b2.data() + bd, src + i - q, q);
170 int const p_big_endian,
171 size_t max_src_size) {
172 std::vector<uint8_t> b2(1024);
173 const uint8_t*
const src_start = src;
175 int bd = 0, bs = 1024;
182 auto decompress_loop = [&]() {
185 if (max_src_size !=
static_cast<size_t>(-1) &&
186 (size_t)(src - src_start) >= max_src_size) {
188 <<
"HyruleMagicDecompress: Reached end of buffer unexpectedly."
209 c = ((a & 0x0003) << 8);
212 if (max_src_size !=
static_cast<size_t>(-1) &&
213 (size_t)(src - src_start) >= max_src_size) {
214 std::cerr <<
"HyruleMagicDecompress: Reached end of buffer reading "
222 c = (uint16_t)(a & 31);
226 if ((bd + c) > (bs - 512)) {
230 if (bs > 1024 * 1024 * 16) {
231 std::cerr <<
"HyruleMagicDecompress: Excessive allocation detected."
247 if (max_src_size !=
static_cast<size_t>(-1) &&
248 (size_t)(src - src_start + c) > max_src_size) {
249 std::cerr <<
"HyruleMagicDecompress: Raw copy exceeds buffer."
255 memcpy(b2.data() + bd, src, c);
269 if (max_src_size !=
static_cast<size_t>(-1) &&
270 (
size_t)(src - src_start) >= max_src_size) {
271 std::cerr <<
"HyruleMagicDecompress: RLE copy exceeds buffer."
277 memset(b2.data() + bd, *(src++), c);
289 if (max_src_size !=
static_cast<size_t>(-1) &&
290 (
size_t)(src - src_start + 2) > max_src_size) {
292 <<
"HyruleMagicDecompress: RLE 16-bit copy exceeds buffer."
319 if (max_src_size !=
static_cast<size_t>(-1) &&
320 (size_t)(src - src_start) >= max_src_size) {
321 std::cerr <<
"HyruleMagicDecompress: Inc copy exceeds buffer."
340 if (max_src_size !=
static_cast<size_t>(-1) &&
341 (size_t)(src - src_start + 2) > max_src_size) {
342 std::cerr <<
"HyruleMagicDecompress: LZ copy exceeds buffer."
348 d = (*src << 8) + src[1];
354 if (d >=
static_cast<unsigned short>(bs)) {
355 std::cerr <<
"HyruleMagicDecompress: LZ ref out of bounds."
380 std::cout <<
"Command: " << std::to_string(piece->command) <<
"\n";
381 std::cout <<
"Command Length: " << piece->length <<
"\n";
382 std::cout <<
"Argument: ";
383 auto arg_size = piece->argument.size();
384 for (
int i = 0; i < arg_size; ++i) {
385 printf(
"%02X ", piece->argument.at(i));
387 std::cout <<
"\nArgument Length: " << piece->argument_length <<
"\n";
391 auto compressed_chain = chain_head->next;
392 while (compressed_chain !=
nullptr) {
393 std::cout <<
"- Compression Piece -\n";
395 compressed_chain = compressed_chain->next;
401 const unsigned int last_pos) {
402 unsigned int pos = src_data_pos;
403 char byte_to_repeat = rom_data[pos];
404 while (pos <= last_pos && rom_data[pos] == byte_to_repeat) {
413 const unsigned int last_pos) {
414 if (src_data_pos + 2 <= last_pos &&
415 rom_data[src_data_pos] != rom_data[src_data_pos + 1]) {
416 unsigned int pos = src_data_pos;
417 char byte1 = rom_data[pos];
418 char byte2 = rom_data[pos + 1];
421 while (pos + 1 <= last_pos) {
422 if (rom_data[pos] == byte1 && rom_data[pos + 1] == byte2)
435 const unsigned int last_pos) {
436 unsigned int pos = src_data_pos;
437 char byte = rom_data[pos];
441 while (pos <= last_pos &&
byte == rom_data[pos]) {
451 const unsigned int last_pos,
unsigned int start) {
452 if (src_data_pos != start) {
453 unsigned int searching_pos = start;
454 unsigned int current_pos_u = src_data_pos;
455 unsigned int copied_size = 0;
456 unsigned int search_start = start;
458 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
459 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
460 searching_pos < src_data_pos)
462 search_start = searching_pos;
463 while (current_pos_u <= last_pos &&
464 rom_data[current_pos_u] == rom_data[searching_pos] &&
465 searching_pos < src_data_pos) {
471 search_start -= start;
472 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
477 current_pos_u = src_data_pos;
487 uint& cmd_with_max) {
488 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
489 unsigned int cmd_size_taken = data_size_taken[cmd_i];
494 if (cmd_size_taken > max_win && cmd_size_taken > cmd_size[cmd_i] &&
496 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
497 cmd_with_max = cmd_i;
498 max_win = cmd_size_taken;
507 uint& src_data_pos,
uint& comp_accumulator,
508 uint& cmd_with_max,
uint& max_win) {
509 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
511 buffer.push_back(cmd_args[cmd_with_max][0]);
512 if (cmd_size[cmd_with_max] == 2) {
513 buffer.push_back(cmd_args[cmd_with_max][1]);
516 auto new_comp_piece = std::make_shared<CompressionPiece>(
517 cmd_with_max, max_win, buffer, cmd_size[cmd_with_max]);
520 if (comp_accumulator != 0) {
521 std::string copy_buff;
522 copy_buff.resize(comp_accumulator);
523 for (
int i = 0; i < comp_accumulator; ++i) {
524 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
526 auto copy_chunk = std::make_shared<CompressionPiece>(
528 compressed_chain->next = copy_chunk;
529 compressed_chain = copy_chunk;
531 compressed_chain->next = new_comp_piece;
532 compressed_chain = new_comp_piece;
534 src_data_pos += max_win;
535 comp_accumulator = 0;
541 while (src_pos + i < last_pos && data[src_pos] == data[src_pos + i]) {
550 if (src_pos + 2 <= last_pos && data[src_pos] != data[src_pos + 1]) {
551 unsigned int pos = src_pos;
552 char byte1 = data[pos];
553 char byte2 = data[pos + 1];
556 while (pos + 1 <= last_pos) {
557 if (data[pos] == byte1 && data[pos + 1] == byte2)
570 unsigned int pos = src_data_pos;
571 char byte = rom_data[pos];
575 while (pos <= last_pos &&
byte == rom_data[pos]) {
584 const unsigned int last_pos,
unsigned int start,
586 if (src_data_pos != start) {
587 unsigned int searching_pos = start;
588 unsigned int current_pos_u = src_data_pos;
589 unsigned int copied_size = 0;
590 unsigned int search_start = start;
592 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
593 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
594 searching_pos < src_data_pos)
596 search_start = searching_pos;
597 while (current_pos_u <= last_pos &&
598 rom_data[current_pos_u] == rom_data[searching_pos] &&
599 searching_pos < src_data_pos) {
605 search_start -= start;
606 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
611 current_pos_u = src_data_pos;
622 uint& cmd_with_max) {
623 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
624 unsigned int cmd_size_taken = cmd.
data_size[cmd_i];
628 if (cmd_size_taken > max_win && cmd_size_taken >
kCommandSizes[cmd_i] &&
630 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
631 cmd_with_max = cmd_i;
632 max_win = cmd_size_taken;
640 uint& src_data_pos,
uint& comp_accumulator,
641 uint& cmd_with_max,
uint& max_win) {
642 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
644 buffer.push_back(cmd.
arguments[cmd_with_max][0]);
645 if (cmd.
cmd_size[cmd_with_max] == 2) {
646 buffer.push_back(cmd.
arguments[cmd_with_max][1]);
649 auto new_comp_piece = std::make_shared<CompressionPiece>(
650 cmd_with_max, max_win, buffer, cmd.
cmd_size[cmd_with_max]);
653 if (comp_accumulator != 0) {
654 std::string copy_buff;
655 copy_buff.resize(comp_accumulator);
656 for (
int i = 0; i < comp_accumulator; ++i) {
657 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
659 auto copy_chunk = std::make_shared<CompressionPiece>(
661 compressed_chain->next = copy_chunk;
662 compressed_chain = copy_chunk;
664 compressed_chain->next = new_comp_piece;
665 compressed_chain = new_comp_piece;
667 src_data_pos += max_win;
668 comp_accumulator = 0;
674 uint& uncompressed_data_size,
uint& best_command,
uint& best_command_gain) {
675 std::cout <<
"- Identified a gain from command: " << best_command
679 std::string argument_buffer;
680 argument_buffer.push_back(command.
arguments[best_command][0]);
681 if (command.
cmd_size[best_command] == 2) {
682 argument_buffer.push_back(command.
arguments[best_command][1]);
686 auto new_compression_piece = std::make_shared<CompressionPiece>(
687 best_command, best_command_gain, argument_buffer,
693 if (uncompressed_data_size != 0) {
694 std::string copy_buffer(uncompressed_data_size, 0);
695 for (
int i = 0; i < uncompressed_data_size; ++i) {
697 rom_data[i + source_data_position - uncompressed_data_size];
699 auto direct_copy_piece = std::make_shared<CompressionPiece>(
701 uncompressed_data_size);
704 compressed_chain->next = direct_copy_piece;
705 compressed_chain = direct_copy_piece;
709 compressed_chain->next = new_compression_piece;
710 compressed_chain = new_compression_piece;
714 source_data_position += best_command_gain;
715 uncompressed_data_size = 0;
724 switch (piece->command) {
727 new_piece = std::make_shared<CompressionPiece>(
728 piece->command, length_left, piece->argument, piece->argument_length);
731 new_piece = std::make_shared<CompressionPiece>(
732 piece->command, length_left, piece->argument, piece->argument_length);
733 new_piece->argument[0] =
739 new_piece = std::make_shared<CompressionPiece>(
740 piece->command, length_left, empty, length_left);
742 for (
int i = 0; i < length_left; ++i) {
749 unsigned int offset = piece->argument[0] + (piece->argument[1] << 8);
750 new_piece = std::make_shared<CompressionPiece>(
751 piece->command, length_left, piece->argument, piece->argument_length);
753 new_piece->argument[0] =
758 new_piece->argument[1] =
764 return absl::InvalidArgumentError(
765 "SplitCompressionCommand: Invalid Command");
773 unsigned int pos = 0;
775 std::vector<uint8_t> output;
777 while (piece !=
nullptr) {
779 output.push_back(
BUILD_HEADER(piece->command, piece->length));
784 ((uint8_t)piece->command << 2) |
785 (((piece->length - 1) & 0xFF00) >> 8));
787 printf(
"Building extended header : cmd: %d, length: %d - %02X\n",
788 piece->command, piece->length, output[pos - 1]);
789 output.push_back(((piece->length - 1) & 0x00FF));
794 if (!new_piece.ok()) {
795 std::cout << new_piece.status().ToString() << std::endl;
797 printf(
"New added piece\n");
798 auto piece_data = new_piece.value();
800 piece_data->next = piece->next;
801 piece->next = piece_data;
808 tmp[0] = piece->argument[0];
809 tmp[1] = piece->argument[1];
811 tmp[0] = piece->argument[1];
812 tmp[1] = piece->argument[0];
814 for (
const auto& each : tmp) {
815 output.push_back(each);
819 for (
int i = 0; i < piece->argument_length; ++i) {
820 output.push_back(piece->argument[i]);
824 pos += piece->argument_length;
832 int mode,
int start,
int src_data_pos) {
833 if (chain_head->next !=
nullptr) {
840 if (!std::equal(decomp_data.begin() + start, decomp_data.end(),
842 return absl::InternalError(absl::StrFormat(
843 "Compressed data does not match uncompressed data at %d\n",
844 (
uint)(src_data_pos - start)));
847 return absl::OkStatus();
854 while (piece !=
nullptr) {
858 unsigned int previous_length = piece->length;
859 piece->length = piece->length + piece->next->length;
861 for (
int i = 0; i < piece->next->argument_length; ++i) {
862 piece->argument[i + previous_length] = piece->next->argument[i];
864 piece->argument_length = piece->length;
867 auto p_next_next = piece->next->next;
868 piece->next = p_next_next;
876absl::StatusOr<std::vector<uint8_t>>
CompressV2(
const uint8_t* data,
878 const int length,
int mode,
882 return std::vector<uint8_t>();
886 std::string worst_case =
"aaa";
887 auto compressed_chain =
888 std::make_shared<CompressionPiece>(1, 1, worst_case, 2);
889 auto compressed_chain_start = compressed_chain;
895 unsigned int src_pos = start;
896 unsigned int last_pos = start + length - 1;
897 unsigned int comp_accumulator = 0;
908 unsigned int max_win = 2;
922 if (comp_accumulator == 32 || src_pos > last_pos) {
923 std::string buffer =
SetBuffer(data, src_pos, comp_accumulator);
924 auto new_comp_piece = std::make_shared<CompressionPiece>(
926 compressed_chain->next = new_comp_piece;
927 comp_accumulator = 0;
931 src_pos, comp_accumulator, cmd_with_max,
935 if (src_pos > last_pos) {
936 printf(
"Breaking compression loop\n");
965 const std::vector<uint8_t> data,
const int pos,
const int length) {
970 unsigned int pos = context.
src_pos;
973 if (pos == 0 || context.
data[pos - 1] != context.
data[pos]) {
974 char byte_to_repeat = context.
data[pos];
975 while (pos <= context.
last_pos && context.
data[pos] == byte_to_repeat) {
983 DEBUG_LOG(
"CheckByteRepeatV3: byte_to_repeat = "
984 << (
int)byte_to_repeat <<
", size = "
991 unsigned int pos = context.
src_pos;
992 char byte1 = context.
data[pos];
993 char byte2 = context.
data[pos + 1];
996 while (pos + 1 <= context.
last_pos) {
997 if (context.
data[pos] == byte1 && context.
data[pos + 1] == byte2)
1016 unsigned int pos = context.
src_pos;
1017 uint8_t
byte = context.
data[pos];
1022 while (pos <= context.
last_pos &&
byte == context.
data[pos]) {
1031 context.
src_pos > 0 && pos < context.
data.size() &&
1048 const int window_size =
1054 context.
src_pos + window_size <= context.
data.size()) {
1055 unsigned int max_copied_size = 0;
1056 unsigned int best_search_start = 0;
1059 for (
int win_pos = 1; win_pos < window_size && win_pos < context.
src_pos;
1061 auto start_search_from = context.
data.begin() + context.
src_pos - win_pos;
1062 auto search_end = context.
data.begin() + context.
src_pos;
1066 auto found_pos = std::search(
1067 start_search_from, search_end, context.
data.begin() + context.
src_pos,
1068 context.
data.begin() + context.
src_pos + win_pos);
1070 if (found_pos != search_end) {
1072 unsigned int len = 0;
1073 while (context.
src_pos + len < context.
data.size() &&
1074 context.
data[context.
src_pos + len] == *(found_pos + len)) {
1078 if (len > max_copied_size) {
1079 max_copied_size = len;
1080 best_search_start = found_pos - context.
data.begin();
1085 if (max_copied_size >
1087 DEBUG_LOG(
"CheckIntraCopyV3: Detected repeating sequence of length "
1088 << max_copied_size <<
" starting from " << best_search_start);
1093 best_search_start >> 8;
1096 DEBUG_LOG(
"CheckIntraCopyV3: max_copied_size = " << max_copied_size
1097 <<
", best_search_start = "
1098 << best_search_start);
1119 DEBUG_LOG(
"CheckAvailableCompressionCommands: src_pos = " << context.
src_pos);
1123 int max_net_savings = -1;
1128 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
1139 context.
src_pos + cmd_size_taken < context.
data.size()) {
1140 char prev_byte = context.
data[context.
src_pos - 1];
1141 char next_byte = context.
data[context.
src_pos + cmd_size_taken];
1142 if (prev_byte != next_byte && cmd_size_taken == 3) {
1148 if (net_savings > max_net_savings) {
1150 max_net_savings = net_savings;
1154 DEBUG_LOG(
"DetermineBestCompression: cmd_with_max = "
1166 std::vector<uint8_t> uncompressed_data(
1170 uncompressed_data.begin(),
1171 uncompressed_data.end());
1197 std::vector<uint8_t> uncompressed_data(
1201 uncompressed_data.begin(),
1202 uncompressed_data.end());
1207 <<
", compressed_data size = "
1212 DEBUG_LOG(
"AddCompressionToChain: Adding command arguments: ");
1219 std::vector<uint8_t> uncompressed_data(
1223 uncompressed_data.begin(),
1224 uncompressed_data.end());
1234 DEBUG_LOG(
"AddCompressionToChain: (Before) src_pos = "
1249 DEBUG_LOG(
"AddCompressionToChain: (After) src_pos = "
1262 if (!std::equal(decomp_data.begin() + context.
start, decomp_data.end(),
1263 temp_rom.
begin())) {
1264 return absl::InternalError(absl::StrFormat(
1265 "Compressed data does not match uncompressed data at %d\n",
1269 return absl::OkStatus();
1291 std::string empty_string =
"";
1295 for (
int i = 0; i < length_left; ++i) {
1315 return absl::InvalidArgumentError(
1316 "SplitCompressionCommand: Invalid Command");
1324 unsigned int pos = 0;
1335 (((piece.length - 1) & 0xFF00) >> 8));
1337 std::cout <<
"Building extended header : cmd: " << piece.command
1338 <<
", length: " << piece.length <<
" - "
1341 ((piece.length - 1) & 0x00FF));
1345 if (!new_piece.ok()) {
1346 std::cout << new_piece.status().ToString() << std::endl;
1356 tmp[0] = piece.argument[0];
1357 tmp[1] = piece.argument[1];
1359 tmp[0] = piece.argument[1];
1360 tmp[1] = piece.argument[0];
1362 for (
const auto& each : tmp) {
1367 for (
int i = 0; i < piece.argument_length; ++i) {
1372 pos += piece.argument_length;
1386 DEBUG_LOG(
"FinalizeCompression: compressed_data size = "
1391 const std::vector<uint8_t>& data,
const int start,
const int length,
1392 int mode,
bool check) {
1394 return std::vector<uint8_t>();
1422std::string
SetBuffer(
const uint8_t* data,
int src_pos,
int comp_accumulator) {
1424 for (
int i = 0; i < comp_accumulator; ++i) {
1425 buffer.push_back(data[i + src_pos - comp_accumulator]);
1430std::string
SetBuffer(
const std::vector<uint8_t>& data,
int src_pos,
1431 int comp_accumulator) {
1433 for (
int i = 0; i < comp_accumulator; ++i) {
1434 buffer.push_back(data[i + src_pos - comp_accumulator]);
1439void memfill(
const uint8_t* data, std::vector<uint8_t>& buffer,
int buffer_pos,
1440 int offset,
int length) {
1441 auto a = data[offset];
1442 auto b = data[offset + 1];
1443 for (
int i = 0; i < length; i = i + 2) {
1444 buffer[buffer_pos + i] = a;
1445 if ((i + 1) < length)
1446 buffer[buffer_pos + i + 1] = b;
1451 int offset,
int size,
1452 int mode,
size_t rom_size) {
1454 return std::vector<uint8_t>();
1459 if (rom_size !=
static_cast<size_t>(-1) &&
1460 (offset < 0 ||
static_cast<size_t>(offset) >= rom_size)) {
1461 return absl::OutOfRangeError(
1462 absl::StrFormat(
"DecompressV2: Initial offset %d exceeds ROM size %zu",
1466 std::vector<uint8_t> buffer(size, 0);
1467 unsigned int length = 0;
1468 unsigned int buffer_pos = 0;
1469 uint8_t command = 0;
1472 if (rom_size !=
static_cast<size_t>(-1) &&
1473 static_cast<size_t>(offset) >= rom_size) {
1474 return absl::OutOfRangeError(
1475 absl::StrFormat(
"DecompressV2: Initial offset %d exceeds ROM size %zu",
1478 uint8_t header = data[offset];
1482 if (rom_size !=
static_cast<size_t>(-1) &&
1483 static_cast<size_t>(offset) >= rom_size) {
1484 return absl::OutOfRangeError(absl::StrFormat(
1485 "DecompressV2: Offset %d exceeds ROM size %zu while reading command",
1491 if (rom_size !=
static_cast<size_t>(-1) &&
1492 static_cast<size_t>(offset + 1) >= rom_size) {
1493 return absl::OutOfRangeError(
1494 absl::StrFormat(
"DecompressV2: Offset %d+1 exceeds ROM size %zu "
1495 "for expanded command",
1512 if (buffer_pos + length >
static_cast<unsigned int>(size)) {
1514 int new_size = size;
1515 while (buffer_pos + length >
static_cast<unsigned int>(new_size)) {
1516 if (new_size > INT_MAX / 2) {
1517 return absl::ResourceExhaustedError(absl::StrFormat(
1518 "DecompressV2: Buffer size overflow at pos %u, length %u",
1519 buffer_pos, length));
1524 buffer.resize(size);
1530 if (rom_size !=
static_cast<size_t>(-1) &&
1531 static_cast<size_t>(offset + length) > rom_size) {
1532 return absl::OutOfRangeError(
1533 absl::StrFormat(
"DecompressV2: DirectCopy offset %d + length %u "
1534 "exceeds ROM size %zu",
1535 offset, length, rom_size));
1537 memcpy(buffer.data() + buffer_pos, data + offset, length);
1538 buffer_pos += length;
1543 if (rom_size !=
static_cast<size_t>(-1) &&
1544 static_cast<size_t>(offset) >= rom_size) {
1545 return absl::OutOfRangeError(absl::StrFormat(
1546 "DecompressV2: ByteFill offset %d exceeds ROM size %zu", offset,
1549 memset(buffer.data() + buffer_pos, (
int)(data[offset]), length);
1550 buffer_pos += length;
1555 if (rom_size !=
static_cast<size_t>(-1) &&
1556 static_cast<size_t>(offset + 1) >= rom_size) {
1557 return absl::OutOfRangeError(absl::StrFormat(
1558 "DecompressV2: WordFill offset %d+1 exceeds ROM size %zu", offset,
1561 memfill(data, buffer, buffer_pos, offset, length);
1562 buffer_pos += length;
1567 if (rom_size !=
static_cast<size_t>(-1) &&
1568 static_cast<size_t>(offset) >= rom_size) {
1569 return absl::OutOfRangeError(absl::StrFormat(
1570 "DecompressV2: IncreasingFill offset %d exceeds ROM size %zu",
1573 auto inc_byte = data[offset];
1574 for (
unsigned int i = 0; i < length; i++) {
1575 buffer[buffer_pos] = inc_byte++;
1582 if (rom_size !=
static_cast<size_t>(-1) &&
1583 static_cast<size_t>(offset + 1) >= rom_size) {
1584 return absl::OutOfRangeError(absl::StrFormat(
1585 "DecompressV2: RepeatingBytes offset %d+1 exceeds ROM size %zu",
1588 uint16_t s1 = ((data[offset + 1] &
kSnesByteMax) << 8);
1590 int addr = (s1 | s2);
1596 if (addr > offset) {
1597 return absl::InternalError(
1598 absl::StrFormat(
"Decompress: Offset for command copy exceeds "
1600 "(Offset : %#04x | Pos : %#06x)\n",
1604 memcpy(buffer.data() + buffer_pos, buffer.data() + addr, length);
1605 buffer_pos += length;
1609 std::cout << absl::StrFormat(
1610 "Decompress: Invalid header (Offset : %#06x, Command: %#04x)\n",
1615 if (rom_size !=
static_cast<size_t>(-1) &&
1616 static_cast<size_t>(offset) >= rom_size) {
1617 return absl::OutOfRangeError(
1618 absl::StrFormat(
"DecompressV2: Offset %d exceeds ROM size %zu while "
1619 "reading next header",
1622 header = data[offset];
1629 int pos,
int size) {
1634 int pos,
int size) {
1639 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