Skip to content

Commit

Permalink
Remove format type from function names (#8)
Browse files Browse the repository at this point in the history
* Invert format and operation in name

* Remove compression name from function name
  • Loading branch information
AngheloAlf authored Dec 15, 2023
1 parent fe1780a commit d39e07f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 50 deletions.
12 changes: 6 additions & 6 deletions c_bindings/include/crunch64/yay0.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ extern "C"
* @param src_len Size of `src`
* @param src[in] Compressed Yay0 data
*/
Crunch64Error crunch64_decompress_yay0_bound(size_t *dst_size, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yay0_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_decompress_yay0_bound`.
* refer to `crunch64_yay0_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.
Expand All @@ -47,7 +47,7 @@ Crunch64Error crunch64_decompress_yay0_bound(size_t *dst_size, size_t src_len, c
* @param src_len The length of the data pointed by `src`.
* @param src[in] Pointer to compressed data. Must contain the Yay0 header.
*/
Crunch64Error crunch64_decompress_yay0(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yay0_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`.
Expand All @@ -62,13 +62,13 @@ Crunch64Error crunch64_decompress_yay0(size_t *dst_len, uint8_t *dst, size_t src
* @param src_len Size of `src`
* @param src[in] Data that would be compressed
*/
Crunch64Error crunch64_compress_yay0_bound(size_t *dst_size, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yay0_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_compress_yay0_bound`.
* refer to `crunch64_yay0_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.
Expand All @@ -85,7 +85,7 @@ Crunch64Error crunch64_compress_yay0_bound(size_t *dst_size, size_t src_len, con
* @param src_len The length of the data pointed by `src`.
* @param src[in] Pointer to the decompressed data.
*/
Crunch64Error crunch64_compress_yay0(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yay0_compress(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src);

#ifdef __cplusplus
}
Expand Down
12 changes: 6 additions & 6 deletions c_bindings/include/crunch64/yaz0.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ extern "C"
* @param src_len Size of `src`
* @param src[in] Compressed Yaz0 data
*/
Crunch64Error crunch64_decompress_yaz0_bound(size_t *dst_size, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yaz0_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_decompress_yaz0_bound`.
* refer to `crunch64_yaz0_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.
Expand All @@ -47,7 +47,7 @@ Crunch64Error crunch64_decompress_yaz0_bound(size_t *dst_size, size_t src_len, c
* @param src_len The length of the data pointed by `src`.
* @param src[in] Pointer to compressed data. Must contain the Yaz0 header.
*/
Crunch64Error crunch64_decompress_yaz0(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yaz0_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`.
Expand All @@ -62,13 +62,13 @@ Crunch64Error crunch64_decompress_yaz0(size_t *dst_len, uint8_t *dst, size_t src
* @param src_len Size of `src`
* @param src[in] Data that would be compressed
*/
Crunch64Error crunch64_compress_yaz0_bound(size_t *dst_size, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yaz0_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_compress_yaz0_bound`.
* refer to `crunch64_yaz0_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.
Expand All @@ -85,7 +85,7 @@ Crunch64Error crunch64_compress_yaz0_bound(size_t *dst_size, size_t src_len, con
* @param src_len The length of the data pointed by `src`.
* @param src[in] Pointer to the decompressed data.
*/
Crunch64Error crunch64_compress_yaz0(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src);
Crunch64Error crunch64_yaz0_compress(size_t *dst_len, uint8_t *dst, size_t src_len, const uint8_t *const src);

#ifdef __cplusplus
}
Expand Down
8 changes: 4 additions & 4 deletions c_bindings/tests/test_yay0.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bool decompress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t
size_t decompressed_size;
uint8_t *decompressed_data = NULL;

Crunch64Error size_request_ok = crunch64_decompress_yay0_bound(&decompressed_size, src_size, src);
Crunch64Error size_request_ok = crunch64_yay0_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));
Expand All @@ -25,7 +25,7 @@ bool decompress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t
return false;
}

Crunch64Error decompress_ok = crunch64_decompress_yay0(&decompressed_size, decompressed_data, src_size, src);
Crunch64Error decompress_ok = crunch64_yay0_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));
Expand All @@ -49,7 +49,7 @@ bool compress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t *s
assert(dst != NULL);
assert(src != NULL);

Crunch64Error size_request_ok = crunch64_compress_yay0_bound(&compressed_size, src_size, src);
Crunch64Error size_request_ok = crunch64_yay0_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));
Expand All @@ -63,7 +63,7 @@ bool compress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t *s
return false;
}

Crunch64Error compress_ok = crunch64_compress_yay0(&compressed_size, compressed_data, src_size, src);
Crunch64Error compress_ok = crunch64_yay0_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));
Expand Down
8 changes: 4 additions & 4 deletions c_bindings/tests/test_yaz0.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bool decompress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t
size_t decompressed_size;
uint8_t *decompressed_data = NULL;

Crunch64Error size_request_ok = crunch64_decompress_yaz0_bound(&decompressed_size, src_size, src);
Crunch64Error size_request_ok = crunch64_yaz0_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));
Expand All @@ -25,7 +25,7 @@ bool decompress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t
return false;
}

Crunch64Error decompress_ok = crunch64_decompress_yaz0(&decompressed_size, decompressed_data, src_size, src);
Crunch64Error decompress_ok = crunch64_yaz0_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));
Expand All @@ -49,7 +49,7 @@ bool compress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t *s
assert(dst != NULL);
assert(src != NULL);

Crunch64Error size_request_ok = crunch64_compress_yaz0_bound(&compressed_size, src_size, src);
Crunch64Error size_request_ok = crunch64_yaz0_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));
Expand All @@ -63,7 +63,7 @@ bool compress(size_t *dst_size, uint8_t **dst, size_t src_size, const uint8_t *s
return false;
}

Crunch64Error compress_ok = crunch64_compress_yaz0(&compressed_size, compressed_data, src_size, src);
Crunch64Error compress_ok = crunch64_yaz0_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));
Expand Down
10 changes: 4 additions & 6 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ pub mod yaz0;
mod utils;

use thiserror::Error;
use yay0::{compress_yay0, decompress_yay0};
use yaz0::{compress_yaz0, decompress_yaz0};

/* This needs to be in sync with the C equivalent at `crunch64_error.h` */
#[cfg_attr(feature = "c_bindings", repr(u32))]
Expand Down Expand Up @@ -42,16 +40,16 @@ pub enum CompressionType {
impl CompressionType {
pub fn decompress(self: CompressionType, bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
match self {
CompressionType::Yay0 => decompress_yay0(bytes),
CompressionType::Yaz0 => decompress_yaz0(bytes),
CompressionType::Yay0 => yay0::decompress(bytes),
CompressionType::Yaz0 => yaz0::decompress(bytes),
_ => Err(Crunch64Error::UnsupportedCompressionType),
}
}

pub fn compress(self: CompressionType, bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
match self {
CompressionType::Yay0 => compress_yay0(bytes),
CompressionType::Yaz0 => compress_yaz0(bytes),
CompressionType::Yay0 => yay0::compress(bytes),
CompressionType::Yaz0 => yaz0::compress(bytes),
_ => Err(Crunch64Error::UnsupportedCompressionType),
}
}
Expand Down
24 changes: 12 additions & 12 deletions lib/src/yay0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn write_header(
Ok(())
}

pub fn decompress_yay0(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
pub fn decompress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
let (decompressed_size, link_table_offset, chunk_offset) = parse_header(bytes)?;

let mut link_table_idx = link_table_offset;
Expand Down Expand Up @@ -94,7 +94,7 @@ fn size_for_compressed_buffer(input_size: usize) -> Result<usize, Crunch64Error>
Ok(input_size + divide_round_up(input_size, 8) + 0x10)
}

pub fn compress_yay0(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
let input_size = bytes.len();

let mut pp: usize = 0;
Expand Down Expand Up @@ -212,7 +212,7 @@ pub fn compress_yay0(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
#[cfg(feature = "c_bindings")]
mod c_bindings {
#[no_mangle]
pub extern "C" fn crunch64_decompress_yay0_bound(
pub extern "C" fn crunch64_yay0_decompress_bound(
dst_size: *mut usize,
src_len: usize,
src: *const u8,
Expand All @@ -239,7 +239,7 @@ mod c_bindings {
}

#[no_mangle]
pub extern "C" fn crunch64_decompress_yay0(
pub extern "C" fn crunch64_yay0_decompress(
dst_len: *mut usize,
dst: *mut u8,
src_len: usize,
Expand All @@ -254,7 +254,7 @@ mod c_bindings {
Ok(d) => d,
};

let data = match super::decompress_yay0(&bytes) {
let data = match super::decompress(&bytes) {
Err(e) => return e,
Ok(d) => d,
};
Expand All @@ -267,7 +267,7 @@ mod c_bindings {
}

#[no_mangle]
pub extern "C" fn crunch64_compress_yay0_bound(
pub extern "C" fn crunch64_yay0_compress_bound(
dst_size: *mut usize,
src_len: usize,
src: *const u8,
Expand All @@ -285,7 +285,7 @@ mod c_bindings {
}

#[no_mangle]
pub extern "C" fn crunch64_compress_yay0(
pub extern "C" fn crunch64_yay0_compress(
dst_len: *mut usize,
dst: *mut u8,
src_len: usize,
Expand All @@ -300,7 +300,7 @@ mod c_bindings {
Ok(d) => d,
};

let data = match super::compress_yay0(&bytes) {
let data = match super::compress(&bytes) {
Err(e) => return e,
Ok(d) => d,
};
Expand Down Expand Up @@ -347,7 +347,7 @@ mod tests {
let compressed_file = &read_test_file(path.clone());
let decompressed_file = &read_test_file(path.with_extension(""));

let decompressed = super::decompress_yay0(compressed_file)?;
let decompressed = super::decompress(compressed_file)?;
assert_eq!(decompressed_file, decompressed.as_ref());
Ok(())
}
Expand All @@ -359,7 +359,7 @@ mod tests {
let compressed_file = &read_test_file(path.clone());
let decompressed_file = &read_test_file(path.with_extension(""));

let compressed = super::compress_yay0(decompressed_file.as_slice())?;
let compressed = super::compress(decompressed_file.as_slice())?;
assert_eq!(compressed_file, compressed.as_ref());
Ok(())
}
Expand All @@ -372,7 +372,7 @@ mod tests {

assert_eq!(
decompressed_file,
super::decompress_yay0(&super::compress_yay0(decompressed_file.as_ref())?)?.as_ref()
super::decompress(&super::compress(decompressed_file.as_ref())?)?.as_ref()
);
Ok(())
}
Expand All @@ -385,7 +385,7 @@ mod tests {

assert_eq!(
compressed_file,
super::compress_yay0(&super::decompress_yay0(compressed_file.as_ref())?)?.as_ref()
super::compress(&super::decompress(compressed_file.as_ref())?)?.as_ref()
);
Ok(())
}
Expand Down
Loading

0 comments on commit d39e07f

Please sign in to comment.