Skip to content

Commit

Permalink
add_inode: support LZX compression
Browse files Browse the repository at this point in the history
  • Loading branch information
maharmstone committed Jan 3, 2021
1 parent 9800230 commit 2195f8f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
35 changes: 35 additions & 0 deletions src/decomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
* along with Ntfs2btrfs. If not, see <https://www.gnu.org/licenses/>. */

#include "ntfs2btrfs.h"
#include "ebiggers/system_compression.h"

#define LZX_CHUNK_SIZE 32768

using namespace std;

Expand Down Expand Up @@ -144,3 +147,35 @@ string lznt1_decompress(string_view compdata, uint64_t size) {

return ret;
}

string do_lzx_decompress(const string_view& compdata, uint64_t size) {
auto ctx = lzx_allocate_decompressor(LZX_CHUNK_SIZE);

if (!ctx)
throw formatted_error(FMT_STRING("lzx_allocate_decompressor returned NULL."));

uint64_t num_chunks = (size + LZX_CHUNK_SIZE - 1) / LZX_CHUNK_SIZE;
auto offsets = (uint32_t*)compdata.data();

string ret;

ret.resize(size);

for (uint64_t i = 0; i < num_chunks; i++) {
uint64_t off = (num_chunks - 1) * sizeof(uint32_t);
if (i != 0)
off += offsets[i - 1];

auto err = lzx_decompress(ctx, compdata.data() + off, compdata.length() - off, ret.data() + (i * LZX_CHUNK_SIZE),
i == num_chunks - 1 ? (ret.length() - (i * LZX_CHUNK_SIZE)) : LZX_CHUNK_SIZE);

if (err != 0) {
lzx_free_decompressor(ctx);
throw formatted_error(FMT_STRING("lzx_decompress returned {}."), err);
}
}

lzx_free_decompressor(ctx);

return ret;
}
11 changes: 8 additions & 3 deletions src/ebiggers/system_compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
* this program. If not, see <http:https://www.gnu.org/licenses/>.
*/

#ifndef _NTFS_SYSTEM_COMPRESSION_H
#define _NTFS_SYSTEM_COMPRESSION_H
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <sys/types.h>
Expand Down Expand Up @@ -55,4 +58,6 @@ extern int lzx_decompress(struct lzx_decompressor *decompressor,

extern void lzx_free_decompressor(struct lzx_decompressor *decompressor);

#endif /* _NTFS_SYSTEM_COMPRESSION_H */
#ifdef __cplusplus
}
#endif
4 changes: 3 additions & 1 deletion src/ntfs2btrfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,9 @@ static void add_inode(root& r, uint64_t inode, uint64_t ntfs_inode, bool& is_dir
throw formatted_error(FMT_STRING("FIXME - FILE_PROVIDER_COMPRESSION_XPRESS4K WofCompressedData"));

case FILE_PROVIDER_COMPRESSION_LZX:
throw formatted_error(FMT_STRING("FIXME - FILE_PROVIDER_COMPRESSION_LZX WofCompressedData"));
mappings.clear();
inline_data = do_lzx_decompress(wof_compressed_data, file_size);
break;

case FILE_PROVIDER_COMPRESSION_XPRESS8K:
throw formatted_error(FMT_STRING("FIXME - FILE_PROVIDER_COMPRESSION_XPRESS8K WofCompressedData"));
Expand Down
1 change: 1 addition & 0 deletions src/ntfs2btrfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,4 @@ void process_fixups(MULTI_SECTOR_HEADER* header, uint64_t length, unsigned int s

// decomp.cpp
std::string lznt1_decompress(std::string_view compdata, uint64_t size);
std::string do_lzx_decompress(const std::string_view& compdata, uint64_t size);

0 comments on commit 2195f8f

Please sign in to comment.