static bool writeIndexes(NexasPackage* package, bool isBfeFormat) { if (isBfeFormat) return writeBfeIndex(package); ByteArray* encodedIndexes = huffmanEncode(L"Entry Indexes", baData(package->indexes), baLength(package->indexes)); if (encodedIndexes == NULL) { writeLog(LOG_QUIET, L"ERROR: Unable to encode the indexes!"); return false; } byte* encodedData = baData(encodedIndexes); u32 encodedLen = baLength(encodedIndexes); writeLog(LOG_VERBOSE, L"The length of the compressed index is %u.", encodedLen); /// Important: XOR encryption! for (u32 i = 0; i < encodedLen; ++i) { encodedData[i] ^= 0xFF; } if (fwrite(encodedData, 1, encodedLen, package->file) != baLength(encodedIndexes)) { writeLog(LOG_QUIET, L"ERROR: Unable to write the indexes to the package!"); return false; } if (fwrite(&encodedLen, 4, 1, package->file) != 1) { writeLog(LOG_QUIET, L"ERROR: Unable to write the index length to the package!"); return false; } return true; }
void test_huffmanEncodeACC_given_run_and_bitsize_0x01_should_get_0x01_chg_to_0x4000(void){ uint8 runLength = 0x00; uint8 bitSize = 0x01; uint16 result; result = huffmanEncode(runLength, bitSize, acChrominanceTable); TEST_ASSERT_EQUAL(0x4000, result); }
void test_huffmanEncodeACC_given_run_and_bitsize_0xB3_should_get_0xFFD3(void){ uint8 runLength = 0x0B; uint8 bitSize = 0x03; uint16 result; result = huffmanEncode(runLength, bitSize, acChrominanceTable); TEST_ASSERT_EQUAL(0xFFD3, result); }
void test_huffmanEncodeACL_given_run_and_bitsize_0xF0_should_get_0x7F9_chg_to_0xFF20(void){ uint8 runLength = 0x0F; uint8 bitSize = 0x00; uint16 result; result = huffmanEncode(runLength, bitSize, acLuminanceTable); TEST_ASSERT_EQUAL(0xFF20, result); }
void test_huffmanEncodeACL_given_run_and_bitsize_0x79_should_get_0xFFB4(void){ uint8 runLength = 0x07; uint8 bitSize = 0x09; uint16 result; result = huffmanEncode(runLength, bitSize, acLuminanceTable); TEST_ASSERT_EQUAL(0xFFB4, result); }
void test_huffmanEncodeDCC_given_run_and_bitsize_0x0A_should_get_0x3FE_chg_to_0xFF80(void){ uint8 runLength = 0x00; uint8 bitSize = 0x0A; uint16 result; result = huffmanEncode(runLength, bitSize, dcChrominanceTable); TEST_ASSERT_EQUAL(0xFF80, result); }
void test_huffmanEncodeDCL_given_run_and_bitsize_0x02_should_get_0x02_chg_to_0x6000(void){ uint8 runLength = 0x00; uint8 bitSize = 0x02; uint16 result; result = huffmanEncode(runLength, bitSize, dcLuminanceTable); TEST_ASSERT_EQUAL(0x6000, result); }
int main(int argc, char **argv) { freopen("in.txt", "rb", stdin); freopen("out.txt", "wb", stdout); if (getchar() == 'c') { // Compress passNewline(stdin); huffmanEncode(stdin, stdout); } else { // Decompress passNewline(stdin); huffmanDecode(stdin, stdout); } return 0; }