8#include "absl/status/status.h"
9#include "absl/status/statusor.h"
14#define DEBUG_LOG(msg) std::cout << msg << std::endl
20 int const oldsize,
int*
const size,
23 (
unsigned char*)malloc(0x1000);
25 int i, j, k, l, m = 0, n, o = 0, bd = 0, p, q = 0, r;
27 for (i = 0; i < oldsize;) {
35 for (j = 0; j < i - 1; j++) {
39 for (n = 0; n < m; n++)
40 if (src[n + j] != src[n + i])
48 for (n = i + 1; n < oldsize; n++) {
65 for (n = i + 2; n < oldsize; n++) {
82 for (n = 1; n < m; n++)
83 if (src[i + n] != l + n)
93 if (k > 3 + r && k > n + (p & 1))
103 b2[bd++] = (
unsigned char)(224 + (q >> 8));
106 b2[bd++] = (
unsigned char)q;
109 memcpy(b2 + bd, src + i - q, q);
119 b2[bd++] = (
unsigned char)(224 + (n >> 8) + (p << 2));
120 b2[bd++] = (
unsigned char)n;
122 b2[bd++] = (
unsigned char)((p << 5) + n);
127 b2[bd++] = (
unsigned char)l;
131 b2[bd++] = (
unsigned char)l;
132 b2[bd++] = (
unsigned char)m;
138 b2[bd++] = (
unsigned char)(o >> 8);
139 b2[bd++] = (
unsigned char)o;
141 b2[bd++] = (
unsigned char)o;
142 b2[bd++] = (
unsigned char)(o >> 8);
154 b2[bd++] = (
unsigned char)(224 + (q >> 8));
157 b2[bd++] = (
unsigned char)q;
160 memcpy(b2 + bd, src + i - q, q);
166 b2 = (
unsigned char*)realloc(b2, bd);
169 std::vector<uint8_t> compressed_data(b2, b2 + bd);
171 return compressed_data;
175 int const p_big_endian,
176 size_t max_src_size) {
177 unsigned char* b2 = (
unsigned char*)malloc(1024);
178 const uint8_t*
const src_start = src;
180 int bd = 0, bs = 1024;
188 if (max_src_size !=
static_cast<size_t>(-1) &&
189 (size_t)(src - src_start) >= max_src_size) {
190 std::cerr <<
"HyruleMagicDecompress: Reached end of buffer unexpectedly."
211 c = ((a & 0x0003) << 8);
214 if (max_src_size !=
static_cast<size_t>(-1) &&
215 (size_t)(src - src_start) >= max_src_size) {
217 <<
"HyruleMagicDecompress: Reached end of buffer reading extended len"
224 c = (uint16_t)(a & 31);
228 if ((bd + c) > (bs - 512)) {
232 if (bs > 1024 * 1024 * 16) {
233 std::cerr <<
"HyruleMagicDecompress: Excessive allocation detected." << std::endl;
235 return std::vector<uint8_t>();
237 unsigned char* new_b2 = (
unsigned char*)realloc(b2, bs);
240 return std::vector<uint8_t>();
253 if (max_src_size !=
static_cast<size_t>(-1) &&
254 (
size_t)(src - src_start + c) > max_src_size) {
255 std::cerr <<
"HyruleMagicDecompress: Raw copy exceeds buffer."
257 goto end_decompression;
262 memcpy(b2 + bd, src, c);
276 if (max_src_size !=
static_cast<size_t>(-1) &&
277 (
size_t)(src - src_start) >= max_src_size) {
278 std::cerr <<
"HyruleMagicDecompress: RLE copy exceeds buffer."
280 goto end_decompression;
284 memset(b2 + bd, *(src++), c);
296 if (max_src_size !=
static_cast<size_t>(-1) &&
297 (
size_t)(src - src_start + 2) > max_src_size) {
298 std::cerr <<
"HyruleMagicDecompress: RLE 16-bit copy exceeds buffer."
300 goto end_decompression;
325 if (max_src_size !=
static_cast<size_t>(-1) &&
326 (size_t)(src - src_start) >= max_src_size) {
327 std::cerr <<
"HyruleMagicDecompress: Inc copy exceeds buffer."
329 goto end_decompression;
348 if (max_src_size !=
static_cast<size_t>(-1) &&
349 (size_t)(src - src_start + 2) > max_src_size) {
350 std::cerr <<
"HyruleMagicDecompress: LZ copy exceeds buffer."
352 goto end_decompression;
356 d = (*src << 8) + src[1];
391 std::cerr <<
"HyruleMagicDecompress: LZ ref out of bounds." << std::endl;
392 goto end_decompression;
403 b2 = (
unsigned char*)realloc(b2, bd);
409 std::vector<uint8_t> decompressed_data(b2, b2 + bd);
411 return decompressed_data;
417 std::cout <<
"Command: " << std::to_string(piece->command) <<
"\n";
418 std::cout <<
"Command Length: " << piece->length <<
"\n";
419 std::cout <<
"Argument: ";
420 auto arg_size = piece->argument.size();
421 for (
int i = 0; i < arg_size; ++i) {
422 printf(
"%02X ", piece->argument.at(i));
424 std::cout <<
"\nArgument Length: " << piece->argument_length <<
"\n";
428 auto compressed_chain = chain_head->next;
429 while (compressed_chain !=
nullptr) {
430 std::cout <<
"- Compression Piece -\n";
432 compressed_chain = compressed_chain->next;
438 const unsigned int last_pos) {
439 unsigned int pos = src_data_pos;
440 char byte_to_repeat = rom_data[pos];
441 while (pos <= last_pos && rom_data[pos] == byte_to_repeat) {
450 const unsigned int last_pos) {
451 if (src_data_pos + 2 <= last_pos &&
452 rom_data[src_data_pos] != rom_data[src_data_pos + 1]) {
453 unsigned int pos = src_data_pos;
454 char byte1 = rom_data[pos];
455 char byte2 = rom_data[pos + 1];
458 while (pos + 1 <= last_pos) {
459 if (rom_data[pos] == byte1 && rom_data[pos + 1] == byte2)
472 const unsigned int last_pos) {
473 unsigned int pos = src_data_pos;
474 char byte = rom_data[pos];
478 while (pos <= last_pos &&
byte == rom_data[pos]) {
488 const unsigned int last_pos,
unsigned int start) {
489 if (src_data_pos != start) {
490 unsigned int searching_pos = start;
491 unsigned int current_pos_u = src_data_pos;
492 unsigned int copied_size = 0;
493 unsigned int search_start = start;
495 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
496 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
497 searching_pos < src_data_pos)
499 search_start = searching_pos;
500 while (current_pos_u <= last_pos &&
501 rom_data[current_pos_u] == rom_data[searching_pos] &&
502 searching_pos < src_data_pos) {
508 search_start -= start;
509 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
514 current_pos_u = src_data_pos;
524 uint& cmd_with_max) {
525 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
526 unsigned int cmd_size_taken = data_size_taken[cmd_i];
531 if (cmd_size_taken > max_win && cmd_size_taken > cmd_size[cmd_i] &&
533 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
534 cmd_with_max = cmd_i;
535 max_win = cmd_size_taken;
544 uint& src_data_pos,
uint& comp_accumulator,
545 uint& cmd_with_max,
uint& max_win) {
546 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
548 buffer.push_back(cmd_args[cmd_with_max][0]);
549 if (cmd_size[cmd_with_max] == 2) {
550 buffer.push_back(cmd_args[cmd_with_max][1]);
553 auto new_comp_piece = std::make_shared<CompressionPiece>(
554 cmd_with_max, max_win, buffer, cmd_size[cmd_with_max]);
557 if (comp_accumulator != 0) {
558 std::string copy_buff;
559 copy_buff.resize(comp_accumulator);
560 for (
int i = 0; i < comp_accumulator; ++i) {
561 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
563 auto copy_chunk = std::make_shared<CompressionPiece>(
565 compressed_chain->next = copy_chunk;
566 compressed_chain = copy_chunk;
568 compressed_chain->next = new_comp_piece;
569 compressed_chain = new_comp_piece;
571 src_data_pos += max_win;
572 comp_accumulator = 0;
578 while (src_pos + i < last_pos && data[src_pos] == data[src_pos + i]) {
587 if (src_pos + 2 <= last_pos && data[src_pos] != data[src_pos + 1]) {
588 unsigned int pos = src_pos;
589 char byte1 = data[pos];
590 char byte2 = data[pos + 1];
593 while (pos + 1 <= last_pos) {
594 if (data[pos] == byte1 && data[pos + 1] == byte2)
607 unsigned int pos = src_data_pos;
608 char byte = rom_data[pos];
612 while (pos <= last_pos &&
byte == rom_data[pos]) {
621 const unsigned int last_pos,
unsigned int start,
623 if (src_data_pos != start) {
624 unsigned int searching_pos = start;
625 unsigned int current_pos_u = src_data_pos;
626 unsigned int copied_size = 0;
627 unsigned int search_start = start;
629 while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
630 while (rom_data[current_pos_u] != rom_data[searching_pos] &&
631 searching_pos < src_data_pos)
633 search_start = searching_pos;
634 while (current_pos_u <= last_pos &&
635 rom_data[current_pos_u] == rom_data[searching_pos] &&
636 searching_pos < src_data_pos) {
642 search_start -= start;
643 printf(
"- Found repeat of %d at %d\n", copied_size, search_start);
648 current_pos_u = src_data_pos;
659 uint& cmd_with_max) {
660 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
661 unsigned int cmd_size_taken = cmd.
data_size[cmd_i];
665 if (cmd_size_taken > max_win && cmd_size_taken >
kCommandSizes[cmd_i] &&
667 printf(
"==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
668 cmd_with_max = cmd_i;
669 max_win = cmd_size_taken;
677 uint& src_data_pos,
uint& comp_accumulator,
678 uint& cmd_with_max,
uint& max_win) {
679 printf(
"- Ok we get a gain from %d\n", cmd_with_max);
681 buffer.push_back(cmd.
arguments[cmd_with_max][0]);
682 if (cmd.
cmd_size[cmd_with_max] == 2) {
683 buffer.push_back(cmd.
arguments[cmd_with_max][1]);
686 auto new_comp_piece = std::make_shared<CompressionPiece>(
687 cmd_with_max, max_win, buffer, cmd.
cmd_size[cmd_with_max]);
690 if (comp_accumulator != 0) {
691 std::string copy_buff;
692 copy_buff.resize(comp_accumulator);
693 for (
int i = 0; i < comp_accumulator; ++i) {
694 copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
696 auto copy_chunk = std::make_shared<CompressionPiece>(
698 compressed_chain->next = copy_chunk;
699 compressed_chain = copy_chunk;
701 compressed_chain->next = new_comp_piece;
702 compressed_chain = new_comp_piece;
704 src_data_pos += max_win;
705 comp_accumulator = 0;
711 uint& uncompressed_data_size,
uint& best_command,
uint& best_command_gain) {
712 std::cout <<
"- Identified a gain from command: " << best_command
716 std::string argument_buffer;
717 argument_buffer.push_back(command.
arguments[best_command][0]);
718 if (command.
cmd_size[best_command] == 2) {
719 argument_buffer.push_back(command.
arguments[best_command][1]);
723 auto new_compression_piece = std::make_shared<CompressionPiece>(
724 best_command, best_command_gain, argument_buffer,
730 if (uncompressed_data_size != 0) {
731 std::string copy_buffer(uncompressed_data_size, 0);
732 for (
int i = 0; i < uncompressed_data_size; ++i) {
734 rom_data[i + source_data_position - uncompressed_data_size];
736 auto direct_copy_piece = std::make_shared<CompressionPiece>(
738 uncompressed_data_size);
741 compressed_chain->next = direct_copy_piece;
742 compressed_chain = direct_copy_piece;
746 compressed_chain->next = new_compression_piece;
747 compressed_chain = new_compression_piece;
751 source_data_position += best_command_gain;
752 uncompressed_data_size = 0;
761 switch (piece->command) {
764 new_piece = std::make_shared<CompressionPiece>(
765 piece->command, length_left, piece->argument, piece->argument_length);
768 new_piece = std::make_shared<CompressionPiece>(
769 piece->command, length_left, piece->argument, piece->argument_length);
770 new_piece->argument[0] =
776 new_piece = std::make_shared<CompressionPiece>(
777 piece->command, length_left, empty, length_left);
779 for (
int i = 0; i < length_left; ++i) {
786 unsigned int offset = piece->argument[0] + (piece->argument[1] << 8);
787 new_piece = std::make_shared<CompressionPiece>(
788 piece->command, length_left, piece->argument, piece->argument_length);
790 new_piece->argument[0] =
795 new_piece->argument[1] =
801 return absl::InvalidArgumentError(
802 "SplitCompressionCommand: Invalid Command");
810 unsigned int pos = 0;
812 std::vector<uint8_t> output;
814 while (piece !=
nullptr) {
816 output.push_back(
BUILD_HEADER(piece->command, piece->length));
821 ((uint8_t)piece->command << 2) |
822 (((piece->length - 1) & 0xFF00) >> 8));
824 printf(
"Building extended header : cmd: %d, length: %d - %02X\n",
825 piece->command, piece->length, output[pos - 1]);
826 output.push_back(((piece->length - 1) & 0x00FF));
831 if (!new_piece.ok()) {
832 std::cout << new_piece.status().ToString() << std::endl;
834 printf(
"New added piece\n");
835 auto piece_data = new_piece.value();
837 piece_data->next = piece->next;
838 piece->next = piece_data;
845 tmp[0] = piece->argument[0];
846 tmp[1] = piece->argument[1];
848 tmp[0] = piece->argument[1];
849 tmp[1] = piece->argument[0];
851 for (
const auto& each : tmp) {
852 output.push_back(each);
856 for (
int i = 0; i < piece->argument_length; ++i) {
857 output.push_back(piece->argument[i]);
861 pos += piece->argument_length;
869 int mode,
int start,
int src_data_pos) {
870 if (chain_head->next !=
nullptr) {
877 if (!std::equal(decomp_data.begin() + start, decomp_data.end(),
879 return absl::InternalError(absl::StrFormat(
880 "Compressed data does not match uncompressed data at %d\n",
881 (
uint)(src_data_pos - start)));
884 return absl::OkStatus();
891 while (piece !=
nullptr) {
895 unsigned int previous_length = piece->length;
896 piece->length = piece->length + piece->next->length;
898 for (
int i = 0; i < piece->next->argument_length; ++i) {
899 piece->argument[i + previous_length] = piece->next->argument[i];
901 piece->argument_length = piece->length;
904 auto p_next_next = piece->next->next;
905 piece->next = p_next_next;
913absl::StatusOr<std::vector<uint8_t>>
CompressV2(
const uint8_t* data,
915 const int length,
int mode,
919 return std::vector<uint8_t>();
923 std::string worst_case =
"aaa";
924 auto compressed_chain =
925 std::make_shared<CompressionPiece>(1, 1, worst_case, 2);
926 auto compressed_chain_start = compressed_chain;
932 unsigned int src_pos = start;
933 unsigned int last_pos = start + length - 1;
934 unsigned int comp_accumulator = 0;
945 unsigned int max_win = 2;
959 if (comp_accumulator == 32 || src_pos > last_pos) {
960 std::string buffer =
SetBuffer(data, src_pos, comp_accumulator);
961 auto new_comp_piece = std::make_shared<CompressionPiece>(
963 compressed_chain->next = new_comp_piece;
964 comp_accumulator = 0;
968 src_pos, comp_accumulator, cmd_with_max,
972 if (src_pos > last_pos) {
973 printf(
"Breaking compression loop\n");
1002 const std::vector<uint8_t> data,
const int pos,
const int length) {
1007 unsigned int pos = context.
src_pos;
1010 if (pos == 0 || context.
data[pos - 1] != context.
data[pos]) {
1011 char byte_to_repeat = context.
data[pos];
1012 while (pos <= context.
last_pos && context.
data[pos] == byte_to_repeat) {
1020 DEBUG_LOG(
"CheckByteRepeatV3: byte_to_repeat = "
1021 << (
int)byte_to_repeat <<
", size = "
1028 unsigned int pos = context.
src_pos;
1029 char byte1 = context.
data[pos];
1030 char byte2 = context.
data[pos + 1];
1033 while (pos + 1 <= context.
last_pos) {
1034 if (context.
data[pos] == byte1 && context.
data[pos + 1] == byte2)
1053 unsigned int pos = context.
src_pos;
1054 uint8_t
byte = context.
data[pos];
1059 while (pos <= context.
last_pos &&
byte == context.
data[pos]) {
1068 context.
src_pos > 0 && pos < context.
data.size() &&
1085 const int window_size =
1091 context.
src_pos + window_size <= context.
data.size()) {
1092 unsigned int max_copied_size = 0;
1093 unsigned int best_search_start = 0;
1096 for (
int win_pos = 1; win_pos < window_size && win_pos < context.
src_pos;
1098 auto start_search_from = context.
data.begin() + context.
src_pos - win_pos;
1099 auto search_end = context.
data.begin() + context.
src_pos;
1103 auto found_pos = std::search(
1104 start_search_from, search_end, context.
data.begin() + context.
src_pos,
1105 context.
data.begin() + context.
src_pos + win_pos);
1107 if (found_pos != search_end) {
1109 unsigned int len = 0;
1110 while (context.
src_pos + len < context.
data.size() &&
1111 context.
data[context.
src_pos + len] == *(found_pos + len)) {
1115 if (len > max_copied_size) {
1116 max_copied_size = len;
1117 best_search_start = found_pos - context.
data.begin();
1122 if (max_copied_size >
1124 DEBUG_LOG(
"CheckIntraCopyV3: Detected repeating sequence of length "
1125 << max_copied_size <<
" starting from " << best_search_start);
1130 best_search_start >> 8;
1133 DEBUG_LOG(
"CheckIntraCopyV3: max_copied_size = " << max_copied_size
1134 <<
", best_search_start = "
1135 << best_search_start);
1156 DEBUG_LOG(
"CheckAvailableCompressionCommands: src_pos = " << context.
src_pos);
1160 int max_net_savings = -1;
1165 for (
unsigned int cmd_i = 1; cmd_i < 5; cmd_i++) {
1176 context.
src_pos + cmd_size_taken < context.
data.size()) {
1177 char prev_byte = context.
data[context.
src_pos - 1];
1178 char next_byte = context.
data[context.
src_pos + cmd_size_taken];
1179 if (prev_byte != next_byte && cmd_size_taken == 3) {
1185 if (net_savings > max_net_savings) {
1187 max_net_savings = net_savings;
1191 DEBUG_LOG(
"DetermineBestCompression: cmd_with_max = "
1203 std::vector<uint8_t> uncompressed_data(
1207 uncompressed_data.begin(),
1208 uncompressed_data.end());
1234 std::vector<uint8_t> uncompressed_data(
1238 uncompressed_data.begin(),
1239 uncompressed_data.end());
1244 <<
", compressed_data size = "
1249 DEBUG_LOG(
"AddCompressionToChain: Adding command arguments: ");
1256 std::vector<uint8_t> uncompressed_data(
1260 uncompressed_data.begin(),
1261 uncompressed_data.end());
1271 DEBUG_LOG(
"AddCompressionToChain: (Before) src_pos = "
1286 DEBUG_LOG(
"AddCompressionToChain: (After) src_pos = "
1299 if (!std::equal(decomp_data.begin() + context.
start, decomp_data.end(),
1300 temp_rom.
begin())) {
1301 return absl::InternalError(absl::StrFormat(
1302 "Compressed data does not match uncompressed data at %d\n",
1306 return absl::OkStatus();
1328 std::string empty_string =
"";
1332 for (
int i = 0; i < length_left; ++i) {
1352 return absl::InvalidArgumentError(
1353 "SplitCompressionCommand: Invalid Command");
1361 unsigned int pos = 0;
1372 (((piece.length - 1) & 0xFF00) >> 8));
1374 std::cout <<
"Building extended header : cmd: " << piece.command
1375 <<
", length: " << piece.length <<
" - "
1378 ((piece.length - 1) & 0x00FF));
1382 if (!new_piece.ok()) {
1383 std::cout << new_piece.status().ToString() << std::endl;
1393 tmp[0] = piece.argument[0];
1394 tmp[1] = piece.argument[1];
1396 tmp[0] = piece.argument[1];
1397 tmp[1] = piece.argument[0];
1399 for (
const auto& each : tmp) {
1404 for (
int i = 0; i < piece.argument_length; ++i) {
1409 pos += piece.argument_length;
1423 DEBUG_LOG(
"FinalizeCompression: compressed_data size = "
1428 const std::vector<uint8_t>& data,
const int start,
const int length,
1429 int mode,
bool check) {
1431 return std::vector<uint8_t>();
1459std::string
SetBuffer(
const uint8_t* data,
int src_pos,
int comp_accumulator) {
1461 for (
int i = 0; i < comp_accumulator; ++i) {
1462 buffer.push_back(data[i + src_pos - comp_accumulator]);
1467std::string
SetBuffer(
const std::vector<uint8_t>& data,
int src_pos,
1468 int comp_accumulator) {
1470 for (
int i = 0; i < comp_accumulator; ++i) {
1471 buffer.push_back(data[i + src_pos - comp_accumulator]);
1476void memfill(
const uint8_t* data, std::vector<uint8_t>& buffer,
int buffer_pos,
1477 int offset,
int length) {
1478 auto a = data[offset];
1479 auto b = data[offset + 1];
1480 for (
int i = 0; i < length; i = i + 2) {
1481 buffer[buffer_pos + i] = a;
1482 if ((i + 1) < length)
1483 buffer[buffer_pos + i + 1] = b;
1488 int offset,
int size,
1489 int mode,
size_t rom_size) {
1491 return std::vector<uint8_t>();
1496 if (rom_size !=
static_cast<size_t>(-1) && (offset < 0 ||
static_cast<size_t>(offset) >= rom_size)) {
1497 return absl::OutOfRangeError(
1498 absl::StrFormat(
"DecompressV2: Initial offset %d exceeds ROM size %zu",
1502 std::vector<uint8_t> buffer(size, 0);
1503 unsigned int length = 0;
1504 unsigned int buffer_pos = 0;
1505 uint8_t command = 0;
1508 if (rom_size !=
static_cast<size_t>(-1) &&
static_cast<size_t>(offset) >= rom_size) {
1509 return absl::OutOfRangeError(
1510 absl::StrFormat(
"DecompressV2: Initial offset %d exceeds ROM size %zu",
1513 uint8_t header = data[offset];
1517 if (rom_size !=
static_cast<size_t>(-1) &&
static_cast<size_t>(offset) >= rom_size) {
1518 return absl::OutOfRangeError(
1519 absl::StrFormat(
"DecompressV2: Offset %d exceeds ROM size %zu while reading command",
1525 if (rom_size !=
static_cast<size_t>(-1) &&
static_cast<size_t>(offset + 1) >= rom_size) {
1526 return absl::OutOfRangeError(
1527 absl::StrFormat(
"DecompressV2: Offset %d+1 exceeds ROM size %zu for expanded command",
1544 if (buffer_pos + length >
static_cast<unsigned int>(size)) {
1546 int new_size = size;
1547 while (buffer_pos + length >
static_cast<unsigned int>(new_size)) {
1548 if (new_size > INT_MAX / 2) {
1549 return absl::ResourceExhaustedError(
1550 absl::StrFormat(
"DecompressV2: Buffer size overflow at pos %u, length %u",
1551 buffer_pos, length));
1556 buffer.resize(size);
1562 if (rom_size !=
static_cast<size_t>(-1) &&
1563 static_cast<size_t>(offset + length) > rom_size) {
1564 return absl::OutOfRangeError(
1565 absl::StrFormat(
"DecompressV2: DirectCopy offset %d + length %u exceeds ROM size %zu",
1566 offset, length, rom_size));
1568 memcpy(buffer.data() + buffer_pos, data + offset, length);
1569 buffer_pos += length;
1574 if (rom_size !=
static_cast<size_t>(-1) &&
1575 static_cast<size_t>(offset) >= rom_size) {
1576 return absl::OutOfRangeError(
1577 absl::StrFormat(
"DecompressV2: ByteFill offset %d exceeds ROM size %zu",
1580 memset(buffer.data() + buffer_pos, (
int)(data[offset]), length);
1581 buffer_pos += length;
1586 if (rom_size !=
static_cast<size_t>(-1) &&
1587 static_cast<size_t>(offset + 1) >= rom_size) {
1588 return absl::OutOfRangeError(
1589 absl::StrFormat(
"DecompressV2: WordFill offset %d+1 exceeds ROM size %zu",
1592 memfill(data, buffer, buffer_pos, offset, length);
1593 buffer_pos += length;
1598 if (rom_size !=
static_cast<size_t>(-1) &&
1599 static_cast<size_t>(offset) >= rom_size) {
1600 return absl::OutOfRangeError(
1601 absl::StrFormat(
"DecompressV2: IncreasingFill offset %d exceeds ROM size %zu",
1604 auto inc_byte = data[offset];
1605 for (
unsigned int i = 0; i < length; i++) {
1606 buffer[buffer_pos] = inc_byte++;
1613 if (rom_size !=
static_cast<size_t>(-1) &&
1614 static_cast<size_t>(offset + 1) >= rom_size) {
1615 return absl::OutOfRangeError(
1616 absl::StrFormat(
"DecompressV2: RepeatingBytes offset %d+1 exceeds ROM size %zu",
1619 uint16_t s1 = ((data[offset + 1] &
kSnesByteMax) << 8);
1621 int addr = (s1 | s2);
1627 if (addr > offset) {
1628 return absl::InternalError(
1629 absl::StrFormat(
"Decompress: Offset for command copy exceeds "
1631 "(Offset : %#04x | Pos : %#06x)\n",
1635 memcpy(buffer.data() + buffer_pos, buffer.data() + addr, length);
1636 buffer_pos += length;
1640 std::cout << absl::StrFormat(
1641 "Decompress: Invalid header (Offset : %#06x, Command: %#04x)\n",
1646 if (rom_size !=
static_cast<size_t>(-1) &&
1647 static_cast<size_t>(offset) >= rom_size) {
1648 return absl::OutOfRangeError(
1649 absl::StrFormat(
"DecompressV2: Offset %d exceeds ROM size %zu while reading next header",
1652 header = data[offset];
1659 int pos,
int size) {
1664 int pos,
int size) {
1669 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