Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libqb refactor #591

Merged
merged 10 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ include $(PATH_INTERNAL_C)/parts/video/font/build.mk
include $(PATH_INTERNAL_C)/parts/video/image/build.mk
include $(PATH_INTERNAL_C)/parts/gui/build.mk
include $(PATH_INTERNAL_C)/parts/network/http/build.mk
include $(PATH_INTERNAL_C)/parts/compression/build.mk
include $(PATH_INTERNAL_C)/parts/data/build.mk
include $(PATH_INTERNAL_C)/parts/os/clipboard/build.mk

.PHONY: all clean
Expand Down Expand Up @@ -286,7 +286,7 @@ else
endif

ifneq ($(filter y,$(DEP_FONT)),)
EXE_LIBS += $(FONT_OBJS) $(FREETYPE_EXE_LIBS)
EXE_LIBS += $(FREETYPE_EXE_LIBS)

LICENSE_IN_USE += freetype_ftl
else
Expand Down Expand Up @@ -327,11 +327,10 @@ else
EXE_LIBS += $(AUDIO_STUB_OBJS)
endif

# The audio library uses the decompression functions from miniz
ifneq ($(filter y,$(DEP_ZLIB) $(DEP_AUDIO_MINIAUDIO)),)
EXE_LIBS += $(COMPRESSION_LIB)
EXE_LIBS += $(DATA_PROCESSING_LIB)

LICENSE_IN_USE += miniz
LICENSE_IN_USE += miniz modp_b64
endif

ifneq ($(filter y,$(DEP_HTTP)),)
Expand Down
7 changes: 4 additions & 3 deletions internal/c/libqb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
# include <mach-o/dyld.h> //required for _NSGetExecutablePath
#endif


#include "audio.h"
#include "bitops.h"
#include "cmem.h"
#include "command.h"
#include "completion.h"
#include "compression.h"
#include "datetime.h"
#include "encoding.h"
#include "error_handle.h"
#include "event.h"
#include "extended_math.h"
Expand All @@ -31,11 +31,12 @@
#include "gfs.h"
#include "glut-thread.h"
#include "gui.h"
#include "hashing.h"
#include "http.h"
#include "image.h"
#include "keyhandler.h"
#include "mac-mouse-support.h"
#include "logging.h"
#include "mac-mouse-support.h"
#include "mem.h"
#include "mutex.h"
#include "qblist.h"
Expand Down Expand Up @@ -29392,7 +29393,7 @@ extern void set_dynamic_info();

#ifdef QB64_WINDOWS
static bool isValidCygwinPipe(int fd) {
HANDLE h = (HANDLE) _get_osfhandle(fd);
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
return false;
}
Expand Down
6 changes: 2 additions & 4 deletions internal/c/libqb/include/compression.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//----------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
// QB64-PE Compression Library
// Powered by miniz (https://github.com/richgel999/miniz)
//-----------------------------------------------------------------------------------------------------
Expand All @@ -9,7 +9,5 @@

struct qbs;

uint32_t func__adler32(qbs *text);
uint32_t func__crc32(qbs *text);
qbs *func__deflate(qbs *text);
qbs *func__deflate(qbs *src, int32_t level = -1, int32_t passed = 0);
qbs *func__inflate(qbs *text, int64_t originalsize, int32_t passed);
11 changes: 11 additions & 0 deletions internal/c/libqb/include/encoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//-----------------------------------------------------------------------------------------------------
// QB64-PE Encoding Library
// Powered by MODP_B64 (https://github.com/client9/stringencoders)
//-----------------------------------------------------------------------------------------------------

#pragma once

struct qbs;

qbs *func__base64encode(qbs *src);
qbs *func__base64decode(qbs *src);
1 change: 0 additions & 1 deletion internal/c/libqb/include/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ bool FontRenderTextASCII(int32_t fh, const uint8_t *codepoint, int32_t codepoint
int32_t FontPrintWidthUTF32(int32_t fh, const char32_t *codepoint, int32_t codepoints);
int32_t FontPrintWidthASCII(int32_t fh, const uint8_t *codepoint, int32_t codepoints);

qbs *func__md5(qbs *text);
int32_t func__UFontHeight(int32_t qb64_fh, int32_t passed);
int32_t func__UPrintWidth(const qbs *text, int32_t utf_encoding, int32_t qb64_fh, int32_t passed);
int32_t func__ULineSpacing(int32_t qb64_fh, int32_t passed);
Expand Down
14 changes: 14 additions & 0 deletions internal/c/libqb/include/hashing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//-----------------------------------------------------------------------------------------------------
// QB64-PE Hashing Library
// Uses hash functions from miniz and FreeType
//-----------------------------------------------------------------------------------------------------

#pragma once

#include <stdint.h>

struct qbs;

uint32_t func__adler32(qbs *text);
uint32_t func__crc32(qbs *text);
qbs *func__md5(qbs *text);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//----------------------------------------------------------------------------------------------------------------------

#include "InstrumentBankManager.h"
#include "../../../compression/miniz.h"
#include "../../../data/miniz.h"

void InstrumentBankManager::SetPath(const char *path) {
if (path && path[0]) {
Expand Down
23 changes: 0 additions & 23 deletions internal/c/parts/compression/build.mk

This file was deleted.

27 changes: 27 additions & 0 deletions internal/c/parts/data/build.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

MINIZ_SRCS := miniz.c

MODP_B64_SRCS := modp_b64.cpp

DATA_PROCESSING_SRCS := \
compression.cpp \
encoding.cpp

MINIZ_OBJS := $(patsubst %.c,$(PATH_INTERNAL_C)/parts/data/%.o,$(MINIZ_SRCS))

MODP_B64_OBJS := $(patsubst %.cpp,$(PATH_INTERNAL_C)/parts/data/%.o,$(MODP_B64_SRCS))

DATA_PROCESSING_OBJS := $(patsubst %.cpp,$(PATH_INTERNAL_C)/parts/data/%.o,$(DATA_PROCESSING_SRCS))

$(PATH_INTERNAL_C)/parts/data/%.o: $(PATH_INTERNAL_C)/parts/data/%.c
$(CC) -O3 $(CFLAGS) -DDEPENDENCY_CONSOLE_ONLY -DMINIZ_NO_STDIO -DMINIZ_NO_TIME -DMINIZ_NO_ARCHIVE_APIS -DMINIZ_NO_ARCHIVE_WRITING_APIS -Wall $< -c -o $@

$(PATH_INTERNAL_C)/parts/data/%.o: $(PATH_INTERNAL_C)/parts/data/%.cpp
$(CXX) -O3 $(CXXFLAGS) -DDEPENDENCY_CONSOLE_ONLY -Wall $< -c -o $@

DATA_PROCESSING_LIB := $(PATH_INTERNAL_C)/parts/data/data_processing.a

$(DATA_PROCESSING_LIB): $(MINIZ_OBJS) $(MODP_B64_OBJS) $(DATA_PROCESSING_OBJS)
$(AR) rcs $@ $(MINIZ_OBJS) $(MODP_B64_OBJS) $(DATA_PROCESSING_OBJS)

CLEAN_LIST += $(DATA_PROCESSING_LIB) $(MINIZ_OBJS) $(MODP_B64_OBJS) $(DATA_PROCESSING_OBJS)
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
//----------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
// QB64-PE Compression Library
// Powered by miniz (https://github.com/richgel999/miniz)
//-----------------------------------------------------------------------------------------------------

#include "libqb-common.h"

#include "compression.h"
#include "qbs.h"

#include "hashing.h"
#include "libqb-common.h"
#include "miniz.h"

#include "qbs.h"
#include <vector>

/// @brief Computes the Adler-32 checksum of the given text.
/// @param text Pointer to the qbs structure containing the text data.
/// @return The Adler-32 checksum as a uint32_t value.
uint32_t func__adler32(qbs *text) {
if (!text->len)
if (!text->len) {
return 1;
return (uint32_t)adler32(1, text->chr, text->len);
}

return adler32(1, text->chr, text->len);
}

/// @brief Computes the CRC-32 checksum of the given text.
/// @param text Pointer to the qbs structure containing the text data.
/// @return The CRC-32 checksum as a uint32_t value.
uint32_t func__crc32(qbs *text) {
if (!text->len)
if (!text->len) {
return 0;
return (uint32_t)crc32(0, text->chr, text->len);
}

return crc32(0, text->chr, text->len);
}

qbs *func__deflate(qbs *text) {
auto fileSize = uLongf(text->len);
/// @brief Compresses a string using the DEFLATE algorithm.
/// @param text The qbs object containing the string to be compressed.
/// @param level The compression level (0-10). 10 is the highest level and 0 is no compression.
/// @param passed Flag indicating if level was passed by the caller.
/// @return A new qbs object containing the compressed data.
qbs *func__deflate(qbs *src, int32_t level, int32_t passed) {
auto fileSize = uLongf(src->len);
auto compSize = compressBound(fileSize);
auto dest = qbs_new(compSize, 1); // compressing directly to the qbs gives us a performance boost
compress(dest->chr, &compSize, text->chr, fileSize); // discard result because we do not do any error checking
return qbs_left(dest, compSize);
auto dest = qbs_new(compSize, 1); // compressing directly to the qbs gives us a performance boost

if (!passed) {
level = MZ_DEFAULT_COMPRESSION; // set default compression level
}

compress2(dest->chr, &compSize, src->chr, fileSize, level); // discard result because we do not do any error checking

return qbs_left(dest, compSize); // resize the qbs to the actual compressed size
}

/// @brief Decompresses a string using the INFLATE algorithm.
/// @param text The qbs object containing the compressed data.
/// @param originalSize The expected original size of the uncompressed data.
/// @param passed Flag indicating if originalSize was passed by the caller.
/// @return A new qbs object containing the uncompressed data.
qbs *func__inflate(qbs *text, int64_t originalSize, int32_t passed) {
if (passed) {
// Passing negative values can do bad things to qbs
Expand Down
27 changes: 27 additions & 0 deletions internal/c/parts/data/encoding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//-----------------------------------------------------------------------------------------------------
// QB64-PE Encoding Library
// Powered by MODP_B64 (https://github.com/client9/stringencoders)
//-----------------------------------------------------------------------------------------------------

#include "encoding.h"
#include "error_handle.h"
#include "libqb-common.h"
#include "modp_b64.h"
#include "qbs.h"

/// @brief Encodes the given qbs object to Base64 format.
/// @param src The qbs object containing the data to be encoded.
/// @return A new qbs object containing the Base64 encoded data.
qbs *func__base64encode(qbs *src) {
auto dst = qbs_new(modp_b64_encode_data_len(src->len), 1);
return qbs_left(dst, modp_b64_encode_data(reinterpret_cast<char *>(dst->chr), reinterpret_cast<const char *>(src->chr), src->len));
}

/// @brief Decodes the given qbs object from Base64 format.
/// @param src The qbs object containing the Base64 encoded data.
/// @return A new qbs object containing the decoded data.
qbs *func__base64decode(qbs *src) {
auto dst = qbs_new(modp_b64_decode_len(src->len), 1);
auto outputSize = modp_b64_decode(reinterpret_cast<char *>(dst->chr), reinterpret_cast<const char *>(src->chr), src->len);
return outputSize == MODP_B64_ERROR ? qbs_left(dst, 0) : qbs_left(dst, outputSize);
}
File renamed without changes.
File renamed without changes.
Loading