static bool decodeIndex(NexasPackage* package) { fseek(package->file, -4, SEEK_END); u32 encodedLen; if (fread(&encodedLen, sizeof(u32), 1, package->file) != 1) { writeLog(LOG_QUIET, L"ERROR: Unable to read the length of the encoded index!"); return false; } writeLog(LOG_VERBOSE, L"The length of the compressed index is %d.", encodedLen); if (fseek(package->file, -4-encodedLen, SEEK_END) != 0) { writeLog(LOG_QUIET, L"ERROR: Unable to locate the compressed index!"); return false; } ByteArray* encodedData = newByteArray(encodedLen); byte* data = baData(encodedData); if (fread(data, sizeof(byte), encodedLen, package->file) != encodedLen) { writeLog(LOG_QUIET, L"ERROR: Unable to read the compressed index!"); deleteByteArray(encodedData); return false; } for (u32 i = 0; i < encodedLen; ++i) { data[i] ^= 0xFF; } u32 decodedLen = sizeof(IndexEntry) * package->header->entryCount; ByteArray* originalIndexes = huffmanDecode(L"Entry Indexes", data, encodedLen, decodedLen); deleteByteArray(encodedData); package->indexes = originalIndexes; return (package->indexes != NULL); }
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; }