-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MIO0 compression and decompression (#10)
* Implement mio0 decompression * Add mio0 test cases * fmt and clippy * a * Implement mio0 compression * Forgot to add tests to CI * rewrite mio0 to make it more similar to yay0 * version bump * cli version
- Loading branch information
1 parent
8e1f0d5
commit 08a8fec
Showing
32 changed files
with
800 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#ifndef CRUNCH64_MIO0_H | ||
#define CRUNCH64_MIO0_H | ||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#include "error.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" | ||
{ | ||
#endif | ||
|
||
/** | ||
* @brief Get a size big enough to allocate a buffer that can fit the uncompressed data produced by uncompressing `src`. | ||
* | ||
* The compressed data must include the Mio0 header. | ||
* | ||
* Returning `true` means the function succeeded and the requested size was put in `dst_size`. | ||
* | ||
* If this function fails to calculate said size then it will return `false` and `dst_size` may have garbage data. | ||
* | ||
* @param dst_size[out] Will be set to the requested size. | ||
* @param src_len Size of `src` | ||
* @param src[in] Compressed Mio0 data | ||
*/ | ||
Crunch64Error crunch64_mio0_decompress_bound(size_t *dst_size, size_t src_len, const uint8_t *const src); | ||
|
||
/** | ||
* @brief Decompresses the data pointed by `src` and puts that data into `dst`. | ||
* | ||
* The `dst` should point to a buffer big enough to hold the decompressed data. To know how big said buffer must be | ||
* refer to `crunch64_mio0_decompress_bound`. | ||
* | ||
* When this function is called, `dst_len` must point to the size of the `dst` pointer, allowing for range checking | ||
* and avoiding to write out of bounds. | ||
* | ||
* If the function succeedes it returns `true` and it puts the decompressed data on `dst` and the actual decompressed | ||
* size is put on `dst_len`. | ||
* | ||
* If this function fails it will return `false`. `dst_size` and `dst` may have garbage data. | ||
* | ||
* @param dst_len[in,out] Will be set to the decompressed size. It should point to the size of the `dst` buffer when the function is called. | ||
* @param dst[out] Pointer to buffer big enough to hold the decompressed data. | ||
* @param src_len The length of the data pointed by `src`. | ||
* @param src[in] Pointer to compressed data. Must contain the Mio0 header. | ||
*/ | ||
Crunch64Error crunch64_mio0_decompress(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src); | ||
|
||
/** | ||
* @brief Get a size big enough to allocate a buffer that can fit the compressed data produced by compressing `src`. | ||
* | ||
* The compressed data must include the Mio0 header. | ||
* | ||
* Returning `true` means the function succeeded and the requested size was put in `dst_size` | ||
* | ||
* If this function fails to calculate said size then it will return `false` and `dst_size` may not be a valid value. | ||
* | ||
* @param dst_size[out] Will be set to the requested size. | ||
* @param src_len Size of `src` | ||
* @param src[in] Data that would be compressed | ||
*/ | ||
Crunch64Error crunch64_mio0_compress_bound(size_t *dst_size, size_t src_len, const uint8_t *const src); | ||
|
||
/** | ||
* @brief Compresses the data pointed by `src` and puts that data into `dst`. | ||
* | ||
* The `dst` should point to a buffer big enough to hold the compressed data. To know how big said buffer must be | ||
* refer to `crunch64_mio0_compress_bound`. | ||
* | ||
* When this function is called, `dst_len` must point to the size of the `dst` pointer, allowing for range checking | ||
* and avoiding to write out of bounds. | ||
* | ||
* If the function succeedes it returns `true` and it puts the compressed data on `dst` and the actual compressed | ||
* size is put on `dst_len`. | ||
* | ||
* If this function fails it will return `false`. `dst_size` and `dst` may have garbage data. | ||
* | ||
* `dst` will include the Mio0 header. | ||
* | ||
* @param dst_len[in,out] Will be set to the compressed size. It should point to the size of the `dst` buffer when the function is called. | ||
* @param dst[out] Pointer to buffer big enough to hold the compressed data. | ||
* @param src_len The length of the data pointed by `src`. | ||
* @param src[in] Pointer to the decompressed data. | ||
*/ | ||
Crunch64Error crunch64_mio0_compress(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#include "crunch64.h" | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "utils.h" | ||
|
||
bool decompress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t *src) | ||
{ | ||
size_t decompressed_size; | ||
uint8_t *decompressed_data = NULL; | ||
|
||
Crunch64Error size_request_ok = crunch64_mio0_decompress_bound(&decompressed_size, src_size, src); | ||
if (size_request_ok != Crunch64Error_Okay) | ||
{ | ||
fprintf(stderr, " failed to request size for buffer. Reason: %s\n", get_crunch64_error_str(size_request_ok)); | ||
return false; | ||
} | ||
|
||
decompressed_data = malloc(decompressed_size * sizeof(uint8_t)); | ||
if (decompressed_data == NULL) | ||
{ | ||
fprintf(stderr, " malloc fail: 0x%zX bytes\n", decompressed_size * sizeof(uint8_t)); | ||
return false; | ||
} | ||
|
||
Crunch64Error decompress_ok = crunch64_mio0_decompress(&decompressed_size, decompressed_data, src_size, src); | ||
if (decompress_ok != Crunch64Error_Okay) | ||
{ | ||
fprintf(stderr, " failed to decompress data. Reason: %s\n", get_crunch64_error_str(decompress_ok)); | ||
free(decompressed_data); | ||
return false; | ||
} | ||
|
||
*dst_size = decompressed_size; | ||
*dst = decompressed_data; | ||
|
||
fprintf(stderr, " OK\n"); | ||
return true; | ||
} | ||
|
||
bool compress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t *src) | ||
{ | ||
size_t compressed_size; | ||
uint8_t *compressed_data = NULL; | ||
|
||
assert(dst_size != NULL); | ||
assert(dst != NULL); | ||
assert(src != NULL); | ||
|
||
Crunch64Error size_request_ok = crunch64_mio0_compress_bound(&compressed_size, src_size, src); | ||
if (size_request_ok != Crunch64Error_Okay) | ||
{ | ||
fprintf(stderr, " failed to request size for buffer. Reason: %s\n", get_crunch64_error_str(size_request_ok)); | ||
return false; | ||
} | ||
|
||
compressed_data = malloc(compressed_size * sizeof(uint8_t)); | ||
if (compressed_data == NULL) | ||
{ | ||
fprintf(stderr, " malloc fail: 0x%zX bytes\n", compressed_size * sizeof(uint8_t)); | ||
return false; | ||
} | ||
|
||
Crunch64Error compress_ok = crunch64_mio0_compress(&compressed_size, compressed_data, src_size, src); | ||
if (compress_ok != Crunch64Error_Okay) | ||
{ | ||
fprintf(stderr, " failed to decompress data. Reason: %s\n", get_crunch64_error_str(compress_ok)); | ||
free(compressed_data); | ||
return false; | ||
} | ||
|
||
*dst_size = compressed_size; | ||
*dst = compressed_data; | ||
|
||
fprintf(stderr, " OK\n"); | ||
|
||
return true; | ||
} | ||
|
||
void print_usage(int argc, char *argv[]) | ||
{ | ||
(void)argc; | ||
|
||
fprintf(stderr, "Usage: %s bin_file compressed_file\n", argv[0]); | ||
fprintf(stderr, "\n"); | ||
fprintf(stderr, "This programs tests compression and decompression produces matching output\n"); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int ret = 0; | ||
|
||
if (argc < 2) | ||
{ | ||
print_usage(argc, argv); | ||
return -1; | ||
} | ||
|
||
const char *bin_path = argv[1]; | ||
const char *compressed_path = argv[2]; | ||
|
||
fprintf(stderr, "Reading file %s\n", bin_path); | ||
size_t bin_size = 0; | ||
uint8_t *bin = read_binary_file(bin_path, &bin_size); | ||
assert(bin_size > 0); | ||
assert(bin != NULL); | ||
|
||
fprintf(stderr, "Reading file %s\n", compressed_path); | ||
size_t compressed_data_size = 0; | ||
uint8_t *compressed_data = read_binary_file(compressed_path, &compressed_data_size); | ||
assert(compressed_data_size > 0); | ||
assert(compressed_data != NULL); | ||
|
||
if (!test_matching_decompression(bin_size, bin, compressed_data_size, compressed_data)) | ||
{ | ||
ret++; | ||
} | ||
if (!test_matching_compression(bin_size, bin, compressed_data_size, compressed_data)) | ||
{ | ||
ret++; | ||
} | ||
if (!test_cycle_decompressed(bin_size, bin)) | ||
{ | ||
ret++; | ||
} | ||
if (!test_cycle_compressed(compressed_data_size, compressed_data)) | ||
{ | ||
ret++; | ||
} | ||
|
||
free(bin); | ||
free(compressed_data); | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#! /usr/bin/env bash | ||
|
||
set -e | ||
|
||
for i in test_data/*.MIO0; do | ||
[ -f "$i" ] || break | ||
echo "Processing:" $i | ||
|
||
# Remove the extension | ||
BIN_PATH=$(echo $i | sed 's/.MIO0//') | ||
|
||
c_bindings/tests/test_mio0.elf $BIN_PATH $i | ||
echo | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from __future__ import annotations | ||
|
||
from .crunch64 import decompress_mio0 as decompress | ||
from .crunch64 import compress_mio0 as compress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from __future__ import annotations | ||
|
||
def decompress(data: bytes) -> bytes: ... | ||
def compress(data: bytes) -> bytes: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.