Skip to content

Commit

Permalink
added tinf bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowman554 committed Jan 21, 2024
1 parent b9a6f57 commit f5fe220
Show file tree
Hide file tree
Showing 14 changed files with 1,368 additions and 0 deletions.
75 changes: 75 additions & 0 deletions examples/gunzip/main.fl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
$include <std.fl>
$include <tinf.fl>

function read_le32(chr[] p) -> int {
return (p[0]) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}

function print_decompress_message(int dlen) -> void {
str dlen_str = string_from_int(dlen, 10);

str result_str_tmp = string_join("decompressed ", dlen_str);
str result_str = string_join(result_str_tmp, " bytes!");

prints(result_str);

string_delete(result_str);
string_delete(result_str_tmp);
string_delete(dlen_str);
}

function spark(int argc, str[] argv) -> int {
if argc != 3 {
prints("Usage: gunzip <in> <out>");
return 1;
}

tinf_init();

int fin = file_open(argv[1], "rb");
if !fin {
prints("unable to open input file");
return 1;
}

int fout = file_open(argv[2], "wb");
if !fout {
prints("unable to open output file");
return 1;
}

int slen = file_size(fin);
if slen < 18 {
prints("input too small to be gzip");
return 1;
}

chr[] source = allocate(slen);
file_read(fin, source, slen, 0);
file_close(fin);

int dlen = read_le32(source + slen - 4);

chr[] dest = allocate(dlen);

int[] outlen = allocate(8);
outlen[0] = dlen;

int res = tinf_gzip_uncompress(dest, outlen, source, slen);
if (res != 0) | (outlen[0] != dlen) {
prints("decompression failed");
return 1;
}

deallocate(outlen);
deallocate(source);


file_write(fout, dest, dlen, 0);
file_close(fout);

deallocate(dest);

print_decompress_message(dlen);
return 0;
}
23 changes: 23 additions & 0 deletions examples/gunzip/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "gunzip",
"version": "1.0.1",
"main": "main.fl",
"type": "executable",
"compiler": [
{
"target": "bytecode",
"mode": "flbb"
},
{
"target": "bytecode",
"mode": "flenc"
},
{
"target": "bytecode",
"mode": "flb"
}
],
"dependencies": [
"tinf@1.0.0-x64"
]
}
3 changes: 3 additions & 0 deletions libs/tinf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# libtinf

FireStorm bindings for [tinf](https://github.com/jibsen/tinf)
2 changes: 2 additions & 0 deletions libs/tinf/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deno run -A gen.ts
gcc c/*.c -o native.so -fpic --shared
95 changes: 95 additions & 0 deletions libs/tinf/c/adler32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Adler-32 checksum
*
* Copyright (c) 2003-2019 Joergen Ibsen
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must
* not claim that you wrote the original software. If you use this
* software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must
* not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/

/*
* Adler-32 algorithm taken from the zlib source, which is
* Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
*/

#include "tinf.h"

#define A32_BASE 65521
#define A32_NMAX 5552

unsigned int tinf_adler32(const void *data, unsigned int length)
{
const unsigned char *buf = (const unsigned char *) data;

unsigned int s1 = 1;
unsigned int s2 = 0;

while (length > 0) {
int k = length < A32_NMAX ? length : A32_NMAX;
int i;

for (i = k / 16; i; --i, buf += 16) {
s1 += buf[0];
s2 += s1;
s1 += buf[1];
s2 += s1;
s1 += buf[2];
s2 += s1;
s1 += buf[3];
s2 += s1;
s1 += buf[4];
s2 += s1;
s1 += buf[5];
s2 += s1;
s1 += buf[6];
s2 += s1;
s1 += buf[7];
s2 += s1;

s1 += buf[8];
s2 += s1;
s1 += buf[9];
s2 += s1;
s1 += buf[10];
s2 += s1;
s1 += buf[11];
s2 += s1;
s1 += buf[12];
s2 += s1;
s1 += buf[13];
s2 += s1;
s1 += buf[14];
s2 += s1;
s1 += buf[15];
s2 += s1;
}

for (i = k % 16; i; --i) {
s1 += *buf++;
s2 += s1;
}

s1 %= A32_BASE;
s2 %= A32_BASE;

length -= k;
}

return (s2 << 16) | s1;
}
57 changes: 57 additions & 0 deletions libs/tinf/c/crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* CRC32 checksum
*
* Copyright (c) 1998-2019 Joergen Ibsen
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must
* not claim that you wrote the original software. If you use this
* software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must
* not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/

/*
* CRC32 algorithm taken from the zlib source, which is
* Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
*/

#include "tinf.h"

static const unsigned int tinf_crc32tab[16] = {
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190,
0x6B6B51F4, 0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344,
0xD6D6A3E8, 0xCB61B38C, 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278,
0xBDBDF21C
};

unsigned int tinf_crc32(const void *data, unsigned int length)
{
const unsigned char *buf = (const unsigned char *) data;
unsigned int crc = 0xFFFFFFFF;
unsigned int i;

if (length == 0) {
return 0;
}

for (i = 0; i < length; ++i) {
crc ^= buf[i];
crc = tinf_crc32tab[crc & 0x0F] ^ (crc >> 4);
crc = tinf_crc32tab[crc & 0x0F] ^ (crc >> 4);
}

return crc ^ 0xFFFFFFFF;
}
33 changes: 33 additions & 0 deletions libs/tinf/c/native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "../../../src/flvm/vm.h"
#include "tinf.h"
void native_tinf_init(struct vm_instance* vm) {
tinf_init();
stack_push(vm, 0);
}
void native_tinf_uncompress(struct vm_instance* vm) {
unsigned int sourceLen = (unsigned int) stack_pop(vm);
const void* source = (const void*) stack_pop(vm);
unsigned int* destLen = (unsigned int*) stack_pop(vm);
void* dest = (void*) stack_pop(vm);
stack_push(vm, tinf_uncompress(dest, destLen, source, sourceLen));
}
void native_tinf_gzip_uncompress(struct vm_instance* vm) {
unsigned int sourceLen = (unsigned int) stack_pop(vm);
const void* source = (const void*) stack_pop(vm);
unsigned int* destLen = (unsigned int*) stack_pop(vm);
void* dest = (void*) stack_pop(vm);
stack_push(vm, tinf_gzip_uncompress(dest, destLen, source, sourceLen));
}
void native_tinf_zlib_uncompress(struct vm_instance* vm) {
unsigned int sourceLen = (unsigned int) stack_pop(vm);
const void* source = (const void*) stack_pop(vm);
unsigned int* destLen = (unsigned int*) stack_pop(vm);
void* dest = (void*) stack_pop(vm);
stack_push(vm, tinf_zlib_uncompress(dest, destLen, source, sourceLen));
}
void init() {
vm_native_register(35269, native_tinf_init);
vm_native_register(35270, native_tinf_uncompress);
vm_native_register(35271, native_tinf_gzip_uncompress);
vm_native_register(35272, native_tinf_zlib_uncompress);
}
Loading

0 comments on commit f5fe220

Please sign in to comment.