Skip to content

Commit

Permalink
Basic compression on Windows, Unix placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisd1100 committed Dec 19, 2023
1 parent 61436ed commit 57a6baf
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ LOCAL_SRC_FILES := \
src/gfx/vk/vk-ctx.c \
src/gfx/vk/vk-ui.c \
src/hid/utils.c \
src/unix/compress.c \
src/unix/file.c \
src/unix/memory.c \
src/unix/system.c \
Expand Down
1 change: 1 addition & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ OBJS = \
src/tlocal.o \
src/version.o \
src/hid/utils.o \
src/unix/compress.o \
src/unix/file.o \
src/unix/memory.o \
src/unix/thread.o \
Expand Down
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ OBJS = \
src\windows\aes-gcm.obj \
src\windows\appw.obj \
src\windows\audio.obj \
src\windows\compress.obj \
src\windows\cryptow.obj \
src\windows\dialog.obj \
src\windows\dtlsw.obj \
Expand Down
26 changes: 26 additions & 0 deletions src/matoya.h
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,32 @@ MTY_EXPORT void
MTY_ResamplerReset(MTY_Resampler *ctx);


//- #module Compression
//- #mbrief Basic compression.
//- #mdetails These functions are platform specific and any data compressed with
//- these functions should only be decompressed on the same platform.

/// @brief Compress data with a balanced algorithm.
/// @param input Uncompressed input buffer.
/// @param inputSize Size in bytes of `input`.
/// @param outputSize Set to the size in bytes of the returned compressed buffer.
/// @returns On failure, NULL is returned. Call MTY_GetLog for details.\n\n
/// The returned buffer must be destroyed with MTY_Free.
MTY_EXPORT void *
MTY_Compress(const void *input, size_t inputSize, size_t *outputSize);

/// @brief Decompress data compressed with MTY_Compress.
/// @details This function should only be called on the same platform that originally
/// called MTY_Compress.
/// @param input Compressed input buffer.
/// @param inputSize Size in bytes of `input`.
/// @param outputSize Set to the size in bytes of the returned decompressed buffer.
/// @returns On failure, NULL is returned. Call MTY_GetLog for details.\n\n
/// The returned buffer must be destroyed with MTY_Free.
MTY_EXPORT void *
MTY_Decompress(const void *input, size_t inputSize, size_t *outputSize);


//- #module Crypto
//- #mbrief Common cryptography tasks.

Expand Down
19 changes: 19 additions & 0 deletions src/unix/compress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this file,
// You can obtain one at https://spdx.org/licenses/MIT.html.

#include "matoya.h"

void *MTY_Compress(const void *input, size_t inputSize, size_t *outputSize)
{
*outputSize = inputSize;

return MTY_Dup(input, inputSize);
}

void *MTY_Decompress(const void *input, size_t inputSize, size_t *outputSize)
{
*outputSize = inputSize;

return MTY_Dup(input, inputSize);
}
54 changes: 54 additions & 0 deletions src/windows/compress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this file,
// You can obtain one at https://spdx.org/licenses/MIT.html.

#include "matoya.h"

#include <windows.h>
#include <compressapi.h>

void *MTY_Compress(const void *input, size_t inputSize, size_t *outputSize)
{
COMPRESSOR_HANDLE h = NULL;

if (!CreateCompressor(COMPRESS_ALGORITHM_MSZIP, NULL, &h))
return NULL;

BOOL success = Compress(h, input, inputSize, NULL, 0, outputSize);
if (!success && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return NULL;

void *output = MTY_Alloc(*outputSize, 1);

if (!Compress(h, input, inputSize, output, *outputSize, outputSize)) {
MTY_Free(output);
output = NULL;
}

CloseCompressor(h);

return output;
}

void *MTY_Decompress(const void *input, size_t inputSize, size_t *outputSize)
{
DECOMPRESSOR_HANDLE h = NULL;

if (!CreateDecompressor(COMPRESS_ALGORITHM_MSZIP, NULL, &h))
return NULL;

BOOL success = Decompress(h, input, inputSize, NULL, 0, outputSize);
if (!success && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return NULL;

void *output = MTY_Alloc(*outputSize, 1);

if (!Decompress(h, input, inputSize, output, *outputSize, outputSize)) {
MTY_Free(output);
output = NULL;
}

CloseDecompressor(h);

return output;
}

0 comments on commit 57a6baf

Please sign in to comment.