浏览代码

support zlib package and tests

Lyon 2 年之前
父节点
当前提交
9b64b84f82

+ 131 - 0
examples/zlib/zlib1.py

@@ -0,0 +1,131 @@
+import zlib
+import json
+
+data = {
+    "name": "John Doe",
+    "age": 30,
+    "cars": [
+        {"model": "BMW 230", "mpg": 27.5},
+        {"model": "Ford Edge", "mpg": 24.1}
+    ],
+    "data": [
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+    ]
+}
+
+# Convert the data to a JSON string
+json_str = json.dumps(data)
+
+# Original data
+original_data = json_str.encode()
+# print("size of original data: ", len(original_data))
+
+# Compress the data
+compressed_data = zlib.compress(original_data, level=1)
+
+# Decompress the data
+decompressed_data = zlib.decompress(compressed_data)
+
+# Check if the decompressed data is the same as the original data
+assert decompressed_data == original_data, 'Decompressed data does not match original data'
+
+# print("size of compressed data: ", len(compressed_data))
+
+print("PASS")

+ 16 - 0
examples/zlib/zlib_err.py

@@ -0,0 +1,16 @@
+import zlib
+try:
+    zlib.compress("This should cause a TypeError because it's a str not bytes")
+except:
+    print("TypeError")
+
+try:
+    zlib.decompress("This should also cause a TypeError because it's a str not bytes")
+except:
+    print("TypeError")
+
+try:
+    zlib.decompress(b"This should cause a zlib.error because it's not valid zlib compressed data")
+except:
+    print("zlib.error")
+

+ 21 - 0
package/zlib/LICENSE.MIT

@@ -0,0 +1,21 @@
+FastLZ - Byte-aligned LZ77 compression library
+Copyright (C) 2005-2020 Ariya Hidayat <ariya.hidayat@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+

+ 62 - 0
package/zlib/_zlib.c

@@ -0,0 +1,62 @@
+#include "_zlib.h"
+#include "fastlz.h"
+
+Arg* _zlib_compress(PikaObj* self, Arg* data, int level) {
+    if (arg_getType(data) != ARG_TYPE_BYTES) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "TypeError: a bytes is required");
+        return NULL;
+    }
+    Arg* aRet = NULL;
+    uint8_t* input = arg_getBytes(data);
+    size_t input_len = arg_getBytesSize(data);
+    size_t max_output_len =
+        input_len * 1.1 + 64;  // Fastlz's maximum output size guideline
+    uint8_t* ret = pikaMalloc(max_output_len);
+    size_t ret_len = fastlz_compress_level(level, input, input_len, ret);
+    if (ret_len == 0) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "Compression failed");
+        aRet = NULL;
+        goto __exit;
+    }
+    aRet = arg_newBytes(ret, ret_len);
+    goto __exit;
+__exit:
+    pikaFree(ret, max_output_len);
+    return aRet;
+}
+
+Arg* _zlib_decompress(PikaObj* self, Arg* data) {
+    if (arg_getType(data) != ARG_TYPE_BYTES) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "TypeError: a bytes is required");
+        return NULL;
+    }
+    uint8_t* input = arg_getBytes(data);
+    size_t input_len = arg_getBytesSize(data);
+    size_t max_output_len =
+        input_len * 4;  // Assume the decompressed data is no more than 4 times
+                        // the input data
+    size_t ret_len = 0;
+    uint8_t* ret = NULL;
+    Arg* aRet = NULL;
+    int multiplier = 1;
+    do {
+        multiplier *= 2;
+        max_output_len = input_len * multiplier;
+        ret = pikaMalloc(max_output_len);
+        ret_len = fastlz_decompress(input, input_len, ret, max_output_len);
+        if (ret_len == 0) {
+            pikaFree(ret, max_output_len);
+        }
+    } while (ret_len == 0 && multiplier <= 8);
+    if (ret_len == 0) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "Decompression failed");
+        return NULL;
+    }
+    aRet = arg_newBytes(ret, ret_len);
+    pikaFree(ret, max_output_len);
+    return aRet;
+}

+ 7 - 0
package/zlib/_zlib.pyi

@@ -0,0 +1,7 @@
+
+def compress(data: any, level: int) -> bytes:
+    pass
+
+
+def decompress(data: any) -> bytes:
+    pass

+ 544 - 0
package/zlib/fastlz.c

@@ -0,0 +1,544 @@
+/*
+  FastLZ - Byte-aligned LZ77 compression library
+  Copyright (C) 2005-2020 Ariya Hidayat <ariya.hidayat@gmail.com>
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+
+#include "fastlz.h"
+
+#include <stdint.h>
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
+
+/*
+ * Give hints to the compiler for branch prediction optimization.
+ */
+#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 2))
+#define FASTLZ_LIKELY(c) (__builtin_expect(!!(c), 1))
+#define FASTLZ_UNLIKELY(c) (__builtin_expect(!!(c), 0))
+#else
+#define FASTLZ_LIKELY(c) (c)
+#define FASTLZ_UNLIKELY(c) (c)
+#endif
+
+/*
+ * Specialize custom 64-bit implementation for speed improvements.
+ */
+#if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__)
+#define FLZ_ARCH64
+#endif
+
+/*
+ * Workaround for DJGPP to find uint8_t, uint16_t, etc.
+ */
+#if defined(__MSDOS__) && defined(__GNUC__)
+#include <stdint-gcc.h>
+#endif
+
+#if defined(FASTLZ_USE_MEMMOVE) && (FASTLZ_USE_MEMMOVE == 0)
+
+static void fastlz_memmove(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    do {
+        *dest++ = *src++;
+    } while (--count);
+}
+
+static void fastlz_memcpy(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    return fastlz_memmove(dest, src, count);
+}
+
+#else
+
+#include <string.h>
+
+static void fastlz_memmove(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    if ((count > 4) && (dest >= src + count)) {
+        memmove(dest, src, count);
+    } else {
+        switch (count) {
+            default:
+                do {
+                    *dest++ = *src++;
+                } while (--count);
+                break;
+            case 3:
+                *dest++ = *src++;
+            case 2:
+                *dest++ = *src++;
+            case 1:
+                *dest++ = *src++;
+            case 0:
+                break;
+        }
+    }
+}
+
+static void fastlz_memcpy(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    memcpy(dest, src, count);
+}
+
+#endif
+
+#if defined(FLZ_ARCH64)
+
+static uint32_t flz_readu32(const void* ptr) {
+    return *(const uint32_t*)ptr;
+}
+
+static uint32_t flz_cmp(const uint8_t* p, const uint8_t* q, const uint8_t* r) {
+    const uint8_t* start = p;
+
+    if (flz_readu32(p) == flz_readu32(q)) {
+        p += 4;
+        q += 4;
+    }
+    while (q < r)
+        if (*p++ != *q++)
+            break;
+    return p - start;
+}
+
+#endif /* FLZ_ARCH64 */
+
+#if !defined(FLZ_ARCH64)
+
+static uint32_t flz_readu32(const void* ptr) {
+    const uint8_t* p = (const uint8_t*)ptr;
+    return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
+}
+
+static uint32_t flz_cmp(const uint8_t* p, const uint8_t* q, const uint8_t* r) {
+    const uint8_t* start = p;
+    while (q < r)
+        if (*p++ != *q++)
+            break;
+    return p - start;
+}
+
+#endif /* !FLZ_ARCH64 */
+
+#define MAX_COPY 32
+#define MAX_LEN 264 /* 256 + 8 */
+#define MAX_L1_DISTANCE 8192
+#define MAX_L2_DISTANCE 8191
+#define MAX_FARDISTANCE (65535 + MAX_L2_DISTANCE - 1)
+
+#define HASH_LOG 13
+#define HASH_SIZE (1 << HASH_LOG)
+#define HASH_MASK (HASH_SIZE - 1)
+
+static uint16_t flz_hash(uint32_t v) {
+    uint32_t h = (v * 2654435769LL) >> (32 - HASH_LOG);
+    return h & HASH_MASK;
+}
+
+/* special case of memcpy: at most MAX_COPY bytes */
+static void flz_smallcopy(uint8_t* dest, const uint8_t* src, uint32_t count) {
+#if defined(FLZ_ARCH64)
+    if (count >= 4) {
+        const uint32_t* p = (const uint32_t*)src;
+        uint32_t* q = (uint32_t*)dest;
+        while (count > 4) {
+            *q++ = *p++;
+            count -= 4;
+            dest += 4;
+            src += 4;
+        }
+    }
+#endif
+    fastlz_memcpy(dest, src, count);
+}
+
+/* special case of memcpy: exactly MAX_COPY bytes */
+static void flz_maxcopy(void* dest, const void* src) {
+#if defined(FLZ_ARCH64)
+    const uint32_t* p = (const uint32_t*)src;
+    uint32_t* q = (uint32_t*)dest;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+#else
+    fastlz_memcpy(dest, src, MAX_COPY);
+#endif
+}
+
+static uint8_t* flz_literals(uint32_t runs, const uint8_t* src, uint8_t* dest) {
+    while (runs >= MAX_COPY) {
+        *dest++ = MAX_COPY - 1;
+        flz_maxcopy(dest, src);
+        src += MAX_COPY;
+        dest += MAX_COPY;
+        runs -= MAX_COPY;
+    }
+    if (runs > 0) {
+        *dest++ = runs - 1;
+        flz_smallcopy(dest, src, runs);
+        dest += runs;
+    }
+    return dest;
+}
+
+static uint8_t* flz1_match(uint32_t len, uint32_t distance, uint8_t* op) {
+    --distance;
+    if (FASTLZ_UNLIKELY(len > MAX_LEN - 2))
+        while (len > MAX_LEN - 2) {
+            *op++ = (7 << 5) + (distance >> 8);
+            *op++ = MAX_LEN - 2 - 7 - 2;
+            *op++ = (distance & 255);
+            len -= MAX_LEN - 2;
+        }
+    if (len < 7) {
+        *op++ = (len << 5) + (distance >> 8);
+        *op++ = (distance & 255);
+    } else {
+        *op++ = (7 << 5) + (distance >> 8);
+        *op++ = len - 7;
+        *op++ = (distance & 255);
+    }
+    return op;
+}
+
+#define FASTLZ_BOUND_CHECK(cond)  \
+    if (FASTLZ_UNLIKELY(!(cond))) \
+        return 0;
+
+int fastlz1_compress(const void* input, int length, void* output) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_start = ip;
+    const uint8_t* ip_bound = ip + length - 4; /* because readU32 */
+    const uint8_t* ip_limit = ip + length - 12 - 1;
+    uint8_t* op = (uint8_t*)output;
+
+    uint32_t htab[HASH_SIZE];
+    uint32_t seq, hash;
+
+    /* initializes hash table */
+    for (hash = 0; hash < HASH_SIZE; ++hash)
+        htab[hash] = 0;
+
+    /* we start with literal copy */
+    const uint8_t* anchor = ip;
+    ip += 2;
+
+    /* main loop */
+    while (FASTLZ_LIKELY(ip < ip_limit)) {
+        const uint8_t* ref;
+        uint32_t distance, cmp;
+
+        /* find potential match */
+        do {
+            seq = flz_readu32(ip) & 0xffffff;
+            hash = flz_hash(seq);
+            ref = ip_start + htab[hash];
+            htab[hash] = ip - ip_start;
+            distance = ip - ref;
+            cmp = FASTLZ_LIKELY(distance < MAX_L1_DISTANCE)
+                      ? flz_readu32(ref) & 0xffffff
+                      : 0x1000000;
+            if (FASTLZ_UNLIKELY(ip >= ip_limit))
+                break;
+            ++ip;
+        } while (seq != cmp);
+
+        if (FASTLZ_UNLIKELY(ip >= ip_limit))
+            break;
+        --ip;
+
+        if (FASTLZ_LIKELY(ip > anchor)) {
+            op = flz_literals(ip - anchor, anchor, op);
+        }
+
+        uint32_t len = flz_cmp(ref + 3, ip + 3, ip_bound);
+        op = flz1_match(len, distance, op);
+
+        /* update the hash at match boundary */
+        ip += len;
+        seq = flz_readu32(ip);
+        hash = flz_hash(seq & 0xffffff);
+        htab[hash] = ip++ - ip_start;
+        seq >>= 8;
+        hash = flz_hash(seq);
+        htab[hash] = ip++ - ip_start;
+
+        anchor = ip;
+    }
+
+    uint32_t copy = (uint8_t*)input + length - anchor;
+    op = flz_literals(copy, anchor, op);
+
+    return op - (uint8_t*)output;
+}
+
+int fastlz1_decompress(const void* input,
+                       int length,
+                       void* output,
+                       int maxout) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_limit = ip + length;
+    const uint8_t* ip_bound = ip_limit - 2;
+    uint8_t* op = (uint8_t*)output;
+    uint8_t* op_limit = op + maxout;
+    uint32_t ctrl = (*ip++) & 31;
+
+    while (1) {
+        if (ctrl >= 32) {
+            uint32_t len = (ctrl >> 5) - 1;
+            uint32_t ofs = (ctrl & 31) << 8;
+            const uint8_t* ref = op - ofs - 1;
+            if (len == 7 - 1) {
+                FASTLZ_BOUND_CHECK(ip <= ip_bound);
+                len += *ip++;
+            }
+            ref -= *ip++;
+            len += 3;
+            FASTLZ_BOUND_CHECK(op + len <= op_limit);
+            FASTLZ_BOUND_CHECK(ref >= (uint8_t*)output);
+            fastlz_memmove(op, ref, len);
+            op += len;
+        } else {
+            ctrl++;
+            FASTLZ_BOUND_CHECK(op + ctrl <= op_limit);
+            FASTLZ_BOUND_CHECK(ip + ctrl <= ip_limit);
+            fastlz_memcpy(op, ip, ctrl);
+            ip += ctrl;
+            op += ctrl;
+        }
+
+        if (FASTLZ_UNLIKELY(ip > ip_bound))
+            break;
+        ctrl = *ip++;
+    }
+
+    return op - (uint8_t*)output;
+}
+
+static uint8_t* flz2_match(uint32_t len, uint32_t distance, uint8_t* op) {
+    --distance;
+    if (distance < MAX_L2_DISTANCE) {
+        if (len < 7) {
+            *op++ = (len << 5) + (distance >> 8);
+            *op++ = (distance & 255);
+        } else {
+            *op++ = (7 << 5) + (distance >> 8);
+            for (len -= 7; len >= 255; len -= 255)
+                *op++ = 255;
+            *op++ = len;
+            *op++ = (distance & 255);
+        }
+    } else {
+        /* far away, but not yet in the another galaxy... */
+        if (len < 7) {
+            distance -= MAX_L2_DISTANCE;
+            *op++ = (len << 5) + 31;
+            *op++ = 255;
+            *op++ = distance >> 8;
+            *op++ = distance & 255;
+        } else {
+            distance -= MAX_L2_DISTANCE;
+            *op++ = (7 << 5) + 31;
+            for (len -= 7; len >= 255; len -= 255)
+                *op++ = 255;
+            *op++ = len;
+            *op++ = 255;
+            *op++ = distance >> 8;
+            *op++ = distance & 255;
+        }
+    }
+    return op;
+}
+
+int fastlz2_compress(const void* input, int length, void* output) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_start = ip;
+    const uint8_t* ip_bound = ip + length - 4; /* because readU32 */
+    const uint8_t* ip_limit = ip + length - 12 - 1;
+    uint8_t* op = (uint8_t*)output;
+
+    uint32_t htab[HASH_SIZE];
+    uint32_t seq, hash;
+
+    /* initializes hash table */
+    for (hash = 0; hash < HASH_SIZE; ++hash)
+        htab[hash] = 0;
+
+    /* we start with literal copy */
+    const uint8_t* anchor = ip;
+    ip += 2;
+
+    /* main loop */
+    while (FASTLZ_LIKELY(ip < ip_limit)) {
+        const uint8_t* ref;
+        uint32_t distance, cmp;
+
+        /* find potential match */
+        do {
+            seq = flz_readu32(ip) & 0xffffff;
+            hash = flz_hash(seq);
+            ref = ip_start + htab[hash];
+            htab[hash] = ip - ip_start;
+            distance = ip - ref;
+            cmp = FASTLZ_LIKELY(distance < MAX_FARDISTANCE)
+                      ? flz_readu32(ref) & 0xffffff
+                      : 0x1000000;
+            if (FASTLZ_UNLIKELY(ip >= ip_limit))
+                break;
+            ++ip;
+        } while (seq != cmp);
+
+        if (FASTLZ_UNLIKELY(ip >= ip_limit))
+            break;
+
+        --ip;
+
+        /* far, needs at least 5-byte match */
+        if (distance >= MAX_L2_DISTANCE) {
+            if (ref[3] != ip[3] || ref[4] != ip[4]) {
+                ++ip;
+                continue;
+            }
+        }
+
+        if (FASTLZ_LIKELY(ip > anchor)) {
+            op = flz_literals(ip - anchor, anchor, op);
+        }
+
+        uint32_t len = flz_cmp(ref + 3, ip + 3, ip_bound);
+        op = flz2_match(len, distance, op);
+
+        /* update the hash at match boundary */
+        ip += len;
+        seq = flz_readu32(ip);
+        hash = flz_hash(seq & 0xffffff);
+        htab[hash] = ip++ - ip_start;
+        seq >>= 8;
+        hash = flz_hash(seq);
+        htab[hash] = ip++ - ip_start;
+
+        anchor = ip;
+    }
+
+    uint32_t copy = (uint8_t*)input + length - anchor;
+    op = flz_literals(copy, anchor, op);
+
+    /* marker for fastlz2 */
+    *(uint8_t*)output |= (1 << 5);
+
+    return op - (uint8_t*)output;
+}
+
+int fastlz2_decompress(const void* input,
+                       int length,
+                       void* output,
+                       int maxout) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_limit = ip + length;
+    const uint8_t* ip_bound = ip_limit - 2;
+    uint8_t* op = (uint8_t*)output;
+    uint8_t* op_limit = op + maxout;
+    uint32_t ctrl = (*ip++) & 31;
+
+    while (1) {
+        if (ctrl >= 32) {
+            uint32_t len = (ctrl >> 5) - 1;
+            uint32_t ofs = (ctrl & 31) << 8;
+            const uint8_t* ref = op - ofs - 1;
+
+            uint8_t code;
+            if (len == 7 - 1)
+                do {
+                    FASTLZ_BOUND_CHECK(ip <= ip_bound);
+                    code = *ip++;
+                    len += code;
+                } while (code == 255);
+            code = *ip++;
+            ref -= code;
+            len += 3;
+
+            /* match from 16-bit distance */
+            if (FASTLZ_UNLIKELY(code == 255))
+                if (FASTLZ_LIKELY(ofs == (31 << 8))) {
+                    FASTLZ_BOUND_CHECK(ip < ip_bound);
+                    ofs = (*ip++) << 8;
+                    ofs += *ip++;
+                    ref = op - ofs - MAX_L2_DISTANCE - 1;
+                }
+
+            FASTLZ_BOUND_CHECK(op + len <= op_limit);
+            FASTLZ_BOUND_CHECK(ref >= (uint8_t*)output);
+            fastlz_memmove(op, ref, len);
+            op += len;
+        } else {
+            ctrl++;
+            FASTLZ_BOUND_CHECK(op + ctrl <= op_limit);
+            FASTLZ_BOUND_CHECK(ip + ctrl <= ip_limit);
+            fastlz_memcpy(op, ip, ctrl);
+            ip += ctrl;
+            op += ctrl;
+        }
+
+        if (FASTLZ_UNLIKELY(ip >= ip_limit))
+            break;
+        ctrl = *ip++;
+    }
+
+    return op - (uint8_t*)output;
+}
+
+int fastlz_compress(const void* input, int length, void* output) {
+    /* for short block, choose fastlz1 */
+    if (length < 65536)
+        return fastlz1_compress(input, length, output);
+
+    /* else... */
+    return fastlz2_compress(input, length, output);
+}
+
+int fastlz_decompress(const void* input, int length, void* output, int maxout) {
+    /* magic identifier for compression level */
+    int level = ((*(const uint8_t*)input) >> 5) + 1;
+
+    if (level == 1)
+        return fastlz1_decompress(input, length, output, maxout);
+    if (level == 2)
+        return fastlz2_decompress(input, length, output, maxout);
+
+    /* unknown level, trigger error */
+    return 0;
+}
+
+int fastlz_compress_level(int level,
+                          const void* input,
+                          int length,
+                          void* output) {
+    if (level == 1)
+        return fastlz1_compress(input, length, output);
+    if (level == 2)
+        return fastlz2_compress(input, length, output);
+
+    return 0;
+}
+
+#pragma GCC diagnostic pop

+ 100 - 0
package/zlib/fastlz.h

@@ -0,0 +1,100 @@
+/*
+  FastLZ - Byte-aligned LZ77 compression library
+  Copyright (C) 2005-2020 Ariya Hidayat <ariya.hidayat@gmail.com>
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+
+#ifndef FASTLZ_H
+#define FASTLZ_H
+
+#define FASTLZ_VERSION 0x000500
+
+#define FASTLZ_VERSION_MAJOR 0
+#define FASTLZ_VERSION_MINOR 5
+#define FASTLZ_VERSION_REVISION 0
+
+#define FASTLZ_VERSION_STRING "0.5.0"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+  Compress a block of data in the input buffer and returns the size of
+  compressed block. The size of input buffer is specified by length. The
+  minimum input buffer size is 16.
+
+  The output buffer must be at least 5% larger than the input buffer
+  and can not be smaller than 66 bytes.
+
+  If the input is not compressible, the return value might be larger than
+  length (input buffer size).
+
+  The input buffer and the output buffer can not overlap.
+
+  Compression level can be specified in parameter level. At the moment,
+  only level 1 and level 2 are supported.
+  Level 1 is the fastest compression and generally useful for short data.
+  Level 2 is slightly slower but it gives better compression ratio.
+
+  Note that the compressed data, regardless of the level, can always be
+  decompressed using the function fastlz_decompress below.
+*/
+
+int fastlz_compress_level(int level,
+                          const void* input,
+                          int length,
+                          void* output);
+
+/**
+  Decompress a block of compressed data and returns the size of the
+  decompressed block. If error occurs, e.g. the compressed data is
+  corrupted or the output buffer is not large enough, then 0 (zero)
+  will be returned instead.
+
+  The input buffer and the output buffer can not overlap.
+
+  Decompression is memory safe and guaranteed not to write the output buffer
+  more than what is specified in maxout.
+
+  Note that the decompression will always work, regardless of the
+  compression level specified in fastlz_compress_level above (when
+  producing the compressed block).
+ */
+
+int fastlz_decompress(const void* input, int length, void* output, int maxout);
+
+/**
+  DEPRECATED.
+
+  This is similar to fastlz_compress_level above, but with the level
+  automatically chosen.
+
+  This function is deprecated and it will be completely removed in some future
+  version.
+*/
+
+int fastlz_compress(const void* input, int length, void* output);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* FASTLZ_H */

+ 9 - 0
package/zlib/zlib.py

@@ -0,0 +1,9 @@
+import _zlib
+
+
+def compress(data: bytes, level: int = 1) -> bytes:
+    return _zlib.compress(data, level)
+
+
+def decompress(data: bytes) -> bytes:
+    return _zlib.decompress(data)

+ 1 - 1
port/linux/.vscode/launch.json

@@ -34,7 +34,7 @@
                 // "--gtest_filter=json.json_issue1"
                 // "--gtest_filter=json.err"
                 // "--gtest_filter=builtin.eval"
-                // "--gtest_filter=stddata.pikafs_open_err"
+                "--gtest_filter=zlib.*"
             ],
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",

+ 7 - 0
port/linux/package/pikascript/_zlib.pyi

@@ -0,0 +1,7 @@
+
+def compress(data: any, level: int) -> bytes:
+    pass
+
+
+def decompress(data: any) -> bytes:
+    pass

+ 1 - 1
port/linux/package/pikascript/main.py

@@ -5,7 +5,7 @@ import GTestTask, TempDevTest
 import cb_test
 import configparser, network
 import test_module1, test_cmodule, test_module4, import_test
-import hashlib, hmac, aes, base64, time, os
+import hashlib, hmac, aes, base64, time, os, zlib
 import _thread, weakref, eventloop
 import this
 import fsm

+ 21 - 0
port/linux/package/pikascript/pikascript-lib/zlib/LICENSE.MIT

@@ -0,0 +1,21 @@
+FastLZ - Byte-aligned LZ77 compression library
+Copyright (C) 2005-2020 Ariya Hidayat <ariya.hidayat@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+

+ 62 - 0
port/linux/package/pikascript/pikascript-lib/zlib/_zlib.c

@@ -0,0 +1,62 @@
+#include "_zlib.h"
+#include "fastlz.h"
+
+Arg* _zlib_compress(PikaObj* self, Arg* data, int level) {
+    if (arg_getType(data) != ARG_TYPE_BYTES) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "TypeError: a bytes is required");
+        return NULL;
+    }
+    Arg* aRet = NULL;
+    uint8_t* input = arg_getBytes(data);
+    size_t input_len = arg_getBytesSize(data);
+    size_t max_output_len =
+        input_len * 1.1 + 64;  // Fastlz's maximum output size guideline
+    uint8_t* ret = pikaMalloc(max_output_len);
+    size_t ret_len = fastlz_compress_level(level, input, input_len, ret);
+    if (ret_len == 0) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "Compression failed");
+        aRet = NULL;
+        goto __exit;
+    }
+    aRet = arg_newBytes(ret, ret_len);
+    goto __exit;
+__exit:
+    pikaFree(ret, max_output_len);
+    return aRet;
+}
+
+Arg* _zlib_decompress(PikaObj* self, Arg* data) {
+    if (arg_getType(data) != ARG_TYPE_BYTES) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "TypeError: a bytes is required");
+        return NULL;
+    }
+    uint8_t* input = arg_getBytes(data);
+    size_t input_len = arg_getBytesSize(data);
+    size_t max_output_len =
+        input_len * 4;  // Assume the decompressed data is no more than 4 times
+                        // the input data
+    size_t ret_len = 0;
+    uint8_t* ret = NULL;
+    Arg* aRet = NULL;
+    int multiplier = 1;
+    do {
+        multiplier *= 2;
+        max_output_len = input_len * multiplier;
+        ret = pikaMalloc(max_output_len);
+        ret_len = fastlz_decompress(input, input_len, ret, max_output_len);
+        if (ret_len == 0) {
+            pikaFree(ret, max_output_len);
+        }
+    } while (ret_len == 0 && multiplier <= 8);
+    if (ret_len == 0) {
+        obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
+        obj_setSysOut(self, "Decompression failed");
+        return NULL;
+    }
+    aRet = arg_newBytes(ret, ret_len);
+    pikaFree(ret, max_output_len);
+    return aRet;
+}

+ 544 - 0
port/linux/package/pikascript/pikascript-lib/zlib/fastlz.c

@@ -0,0 +1,544 @@
+/*
+  FastLZ - Byte-aligned LZ77 compression library
+  Copyright (C) 2005-2020 Ariya Hidayat <ariya.hidayat@gmail.com>
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+
+#include "fastlz.h"
+
+#include <stdint.h>
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
+
+/*
+ * Give hints to the compiler for branch prediction optimization.
+ */
+#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 2))
+#define FASTLZ_LIKELY(c) (__builtin_expect(!!(c), 1))
+#define FASTLZ_UNLIKELY(c) (__builtin_expect(!!(c), 0))
+#else
+#define FASTLZ_LIKELY(c) (c)
+#define FASTLZ_UNLIKELY(c) (c)
+#endif
+
+/*
+ * Specialize custom 64-bit implementation for speed improvements.
+ */
+#if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__)
+#define FLZ_ARCH64
+#endif
+
+/*
+ * Workaround for DJGPP to find uint8_t, uint16_t, etc.
+ */
+#if defined(__MSDOS__) && defined(__GNUC__)
+#include <stdint-gcc.h>
+#endif
+
+#if defined(FASTLZ_USE_MEMMOVE) && (FASTLZ_USE_MEMMOVE == 0)
+
+static void fastlz_memmove(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    do {
+        *dest++ = *src++;
+    } while (--count);
+}
+
+static void fastlz_memcpy(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    return fastlz_memmove(dest, src, count);
+}
+
+#else
+
+#include <string.h>
+
+static void fastlz_memmove(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    if ((count > 4) && (dest >= src + count)) {
+        memmove(dest, src, count);
+    } else {
+        switch (count) {
+            default:
+                do {
+                    *dest++ = *src++;
+                } while (--count);
+                break;
+            case 3:
+                *dest++ = *src++;
+            case 2:
+                *dest++ = *src++;
+            case 1:
+                *dest++ = *src++;
+            case 0:
+                break;
+        }
+    }
+}
+
+static void fastlz_memcpy(uint8_t* dest, const uint8_t* src, uint32_t count) {
+    memcpy(dest, src, count);
+}
+
+#endif
+
+#if defined(FLZ_ARCH64)
+
+static uint32_t flz_readu32(const void* ptr) {
+    return *(const uint32_t*)ptr;
+}
+
+static uint32_t flz_cmp(const uint8_t* p, const uint8_t* q, const uint8_t* r) {
+    const uint8_t* start = p;
+
+    if (flz_readu32(p) == flz_readu32(q)) {
+        p += 4;
+        q += 4;
+    }
+    while (q < r)
+        if (*p++ != *q++)
+            break;
+    return p - start;
+}
+
+#endif /* FLZ_ARCH64 */
+
+#if !defined(FLZ_ARCH64)
+
+static uint32_t flz_readu32(const void* ptr) {
+    const uint8_t* p = (const uint8_t*)ptr;
+    return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
+}
+
+static uint32_t flz_cmp(const uint8_t* p, const uint8_t* q, const uint8_t* r) {
+    const uint8_t* start = p;
+    while (q < r)
+        if (*p++ != *q++)
+            break;
+    return p - start;
+}
+
+#endif /* !FLZ_ARCH64 */
+
+#define MAX_COPY 32
+#define MAX_LEN 264 /* 256 + 8 */
+#define MAX_L1_DISTANCE 8192
+#define MAX_L2_DISTANCE 8191
+#define MAX_FARDISTANCE (65535 + MAX_L2_DISTANCE - 1)
+
+#define HASH_LOG 13
+#define HASH_SIZE (1 << HASH_LOG)
+#define HASH_MASK (HASH_SIZE - 1)
+
+static uint16_t flz_hash(uint32_t v) {
+    uint32_t h = (v * 2654435769LL) >> (32 - HASH_LOG);
+    return h & HASH_MASK;
+}
+
+/* special case of memcpy: at most MAX_COPY bytes */
+static void flz_smallcopy(uint8_t* dest, const uint8_t* src, uint32_t count) {
+#if defined(FLZ_ARCH64)
+    if (count >= 4) {
+        const uint32_t* p = (const uint32_t*)src;
+        uint32_t* q = (uint32_t*)dest;
+        while (count > 4) {
+            *q++ = *p++;
+            count -= 4;
+            dest += 4;
+            src += 4;
+        }
+    }
+#endif
+    fastlz_memcpy(dest, src, count);
+}
+
+/* special case of memcpy: exactly MAX_COPY bytes */
+static void flz_maxcopy(void* dest, const void* src) {
+#if defined(FLZ_ARCH64)
+    const uint32_t* p = (const uint32_t*)src;
+    uint32_t* q = (uint32_t*)dest;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+    *q++ = *p++;
+#else
+    fastlz_memcpy(dest, src, MAX_COPY);
+#endif
+}
+
+static uint8_t* flz_literals(uint32_t runs, const uint8_t* src, uint8_t* dest) {
+    while (runs >= MAX_COPY) {
+        *dest++ = MAX_COPY - 1;
+        flz_maxcopy(dest, src);
+        src += MAX_COPY;
+        dest += MAX_COPY;
+        runs -= MAX_COPY;
+    }
+    if (runs > 0) {
+        *dest++ = runs - 1;
+        flz_smallcopy(dest, src, runs);
+        dest += runs;
+    }
+    return dest;
+}
+
+static uint8_t* flz1_match(uint32_t len, uint32_t distance, uint8_t* op) {
+    --distance;
+    if (FASTLZ_UNLIKELY(len > MAX_LEN - 2))
+        while (len > MAX_LEN - 2) {
+            *op++ = (7 << 5) + (distance >> 8);
+            *op++ = MAX_LEN - 2 - 7 - 2;
+            *op++ = (distance & 255);
+            len -= MAX_LEN - 2;
+        }
+    if (len < 7) {
+        *op++ = (len << 5) + (distance >> 8);
+        *op++ = (distance & 255);
+    } else {
+        *op++ = (7 << 5) + (distance >> 8);
+        *op++ = len - 7;
+        *op++ = (distance & 255);
+    }
+    return op;
+}
+
+#define FASTLZ_BOUND_CHECK(cond)  \
+    if (FASTLZ_UNLIKELY(!(cond))) \
+        return 0;
+
+int fastlz1_compress(const void* input, int length, void* output) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_start = ip;
+    const uint8_t* ip_bound = ip + length - 4; /* because readU32 */
+    const uint8_t* ip_limit = ip + length - 12 - 1;
+    uint8_t* op = (uint8_t*)output;
+
+    uint32_t htab[HASH_SIZE];
+    uint32_t seq, hash;
+
+    /* initializes hash table */
+    for (hash = 0; hash < HASH_SIZE; ++hash)
+        htab[hash] = 0;
+
+    /* we start with literal copy */
+    const uint8_t* anchor = ip;
+    ip += 2;
+
+    /* main loop */
+    while (FASTLZ_LIKELY(ip < ip_limit)) {
+        const uint8_t* ref;
+        uint32_t distance, cmp;
+
+        /* find potential match */
+        do {
+            seq = flz_readu32(ip) & 0xffffff;
+            hash = flz_hash(seq);
+            ref = ip_start + htab[hash];
+            htab[hash] = ip - ip_start;
+            distance = ip - ref;
+            cmp = FASTLZ_LIKELY(distance < MAX_L1_DISTANCE)
+                      ? flz_readu32(ref) & 0xffffff
+                      : 0x1000000;
+            if (FASTLZ_UNLIKELY(ip >= ip_limit))
+                break;
+            ++ip;
+        } while (seq != cmp);
+
+        if (FASTLZ_UNLIKELY(ip >= ip_limit))
+            break;
+        --ip;
+
+        if (FASTLZ_LIKELY(ip > anchor)) {
+            op = flz_literals(ip - anchor, anchor, op);
+        }
+
+        uint32_t len = flz_cmp(ref + 3, ip + 3, ip_bound);
+        op = flz1_match(len, distance, op);
+
+        /* update the hash at match boundary */
+        ip += len;
+        seq = flz_readu32(ip);
+        hash = flz_hash(seq & 0xffffff);
+        htab[hash] = ip++ - ip_start;
+        seq >>= 8;
+        hash = flz_hash(seq);
+        htab[hash] = ip++ - ip_start;
+
+        anchor = ip;
+    }
+
+    uint32_t copy = (uint8_t*)input + length - anchor;
+    op = flz_literals(copy, anchor, op);
+
+    return op - (uint8_t*)output;
+}
+
+int fastlz1_decompress(const void* input,
+                       int length,
+                       void* output,
+                       int maxout) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_limit = ip + length;
+    const uint8_t* ip_bound = ip_limit - 2;
+    uint8_t* op = (uint8_t*)output;
+    uint8_t* op_limit = op + maxout;
+    uint32_t ctrl = (*ip++) & 31;
+
+    while (1) {
+        if (ctrl >= 32) {
+            uint32_t len = (ctrl >> 5) - 1;
+            uint32_t ofs = (ctrl & 31) << 8;
+            const uint8_t* ref = op - ofs - 1;
+            if (len == 7 - 1) {
+                FASTLZ_BOUND_CHECK(ip <= ip_bound);
+                len += *ip++;
+            }
+            ref -= *ip++;
+            len += 3;
+            FASTLZ_BOUND_CHECK(op + len <= op_limit);
+            FASTLZ_BOUND_CHECK(ref >= (uint8_t*)output);
+            fastlz_memmove(op, ref, len);
+            op += len;
+        } else {
+            ctrl++;
+            FASTLZ_BOUND_CHECK(op + ctrl <= op_limit);
+            FASTLZ_BOUND_CHECK(ip + ctrl <= ip_limit);
+            fastlz_memcpy(op, ip, ctrl);
+            ip += ctrl;
+            op += ctrl;
+        }
+
+        if (FASTLZ_UNLIKELY(ip > ip_bound))
+            break;
+        ctrl = *ip++;
+    }
+
+    return op - (uint8_t*)output;
+}
+
+static uint8_t* flz2_match(uint32_t len, uint32_t distance, uint8_t* op) {
+    --distance;
+    if (distance < MAX_L2_DISTANCE) {
+        if (len < 7) {
+            *op++ = (len << 5) + (distance >> 8);
+            *op++ = (distance & 255);
+        } else {
+            *op++ = (7 << 5) + (distance >> 8);
+            for (len -= 7; len >= 255; len -= 255)
+                *op++ = 255;
+            *op++ = len;
+            *op++ = (distance & 255);
+        }
+    } else {
+        /* far away, but not yet in the another galaxy... */
+        if (len < 7) {
+            distance -= MAX_L2_DISTANCE;
+            *op++ = (len << 5) + 31;
+            *op++ = 255;
+            *op++ = distance >> 8;
+            *op++ = distance & 255;
+        } else {
+            distance -= MAX_L2_DISTANCE;
+            *op++ = (7 << 5) + 31;
+            for (len -= 7; len >= 255; len -= 255)
+                *op++ = 255;
+            *op++ = len;
+            *op++ = 255;
+            *op++ = distance >> 8;
+            *op++ = distance & 255;
+        }
+    }
+    return op;
+}
+
+int fastlz2_compress(const void* input, int length, void* output) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_start = ip;
+    const uint8_t* ip_bound = ip + length - 4; /* because readU32 */
+    const uint8_t* ip_limit = ip + length - 12 - 1;
+    uint8_t* op = (uint8_t*)output;
+
+    uint32_t htab[HASH_SIZE];
+    uint32_t seq, hash;
+
+    /* initializes hash table */
+    for (hash = 0; hash < HASH_SIZE; ++hash)
+        htab[hash] = 0;
+
+    /* we start with literal copy */
+    const uint8_t* anchor = ip;
+    ip += 2;
+
+    /* main loop */
+    while (FASTLZ_LIKELY(ip < ip_limit)) {
+        const uint8_t* ref;
+        uint32_t distance, cmp;
+
+        /* find potential match */
+        do {
+            seq = flz_readu32(ip) & 0xffffff;
+            hash = flz_hash(seq);
+            ref = ip_start + htab[hash];
+            htab[hash] = ip - ip_start;
+            distance = ip - ref;
+            cmp = FASTLZ_LIKELY(distance < MAX_FARDISTANCE)
+                      ? flz_readu32(ref) & 0xffffff
+                      : 0x1000000;
+            if (FASTLZ_UNLIKELY(ip >= ip_limit))
+                break;
+            ++ip;
+        } while (seq != cmp);
+
+        if (FASTLZ_UNLIKELY(ip >= ip_limit))
+            break;
+
+        --ip;
+
+        /* far, needs at least 5-byte match */
+        if (distance >= MAX_L2_DISTANCE) {
+            if (ref[3] != ip[3] || ref[4] != ip[4]) {
+                ++ip;
+                continue;
+            }
+        }
+
+        if (FASTLZ_LIKELY(ip > anchor)) {
+            op = flz_literals(ip - anchor, anchor, op);
+        }
+
+        uint32_t len = flz_cmp(ref + 3, ip + 3, ip_bound);
+        op = flz2_match(len, distance, op);
+
+        /* update the hash at match boundary */
+        ip += len;
+        seq = flz_readu32(ip);
+        hash = flz_hash(seq & 0xffffff);
+        htab[hash] = ip++ - ip_start;
+        seq >>= 8;
+        hash = flz_hash(seq);
+        htab[hash] = ip++ - ip_start;
+
+        anchor = ip;
+    }
+
+    uint32_t copy = (uint8_t*)input + length - anchor;
+    op = flz_literals(copy, anchor, op);
+
+    /* marker for fastlz2 */
+    *(uint8_t*)output |= (1 << 5);
+
+    return op - (uint8_t*)output;
+}
+
+int fastlz2_decompress(const void* input,
+                       int length,
+                       void* output,
+                       int maxout) {
+    const uint8_t* ip = (const uint8_t*)input;
+    const uint8_t* ip_limit = ip + length;
+    const uint8_t* ip_bound = ip_limit - 2;
+    uint8_t* op = (uint8_t*)output;
+    uint8_t* op_limit = op + maxout;
+    uint32_t ctrl = (*ip++) & 31;
+
+    while (1) {
+        if (ctrl >= 32) {
+            uint32_t len = (ctrl >> 5) - 1;
+            uint32_t ofs = (ctrl & 31) << 8;
+            const uint8_t* ref = op - ofs - 1;
+
+            uint8_t code;
+            if (len == 7 - 1)
+                do {
+                    FASTLZ_BOUND_CHECK(ip <= ip_bound);
+                    code = *ip++;
+                    len += code;
+                } while (code == 255);
+            code = *ip++;
+            ref -= code;
+            len += 3;
+
+            /* match from 16-bit distance */
+            if (FASTLZ_UNLIKELY(code == 255))
+                if (FASTLZ_LIKELY(ofs == (31 << 8))) {
+                    FASTLZ_BOUND_CHECK(ip < ip_bound);
+                    ofs = (*ip++) << 8;
+                    ofs += *ip++;
+                    ref = op - ofs - MAX_L2_DISTANCE - 1;
+                }
+
+            FASTLZ_BOUND_CHECK(op + len <= op_limit);
+            FASTLZ_BOUND_CHECK(ref >= (uint8_t*)output);
+            fastlz_memmove(op, ref, len);
+            op += len;
+        } else {
+            ctrl++;
+            FASTLZ_BOUND_CHECK(op + ctrl <= op_limit);
+            FASTLZ_BOUND_CHECK(ip + ctrl <= ip_limit);
+            fastlz_memcpy(op, ip, ctrl);
+            ip += ctrl;
+            op += ctrl;
+        }
+
+        if (FASTLZ_UNLIKELY(ip >= ip_limit))
+            break;
+        ctrl = *ip++;
+    }
+
+    return op - (uint8_t*)output;
+}
+
+int fastlz_compress(const void* input, int length, void* output) {
+    /* for short block, choose fastlz1 */
+    if (length < 65536)
+        return fastlz1_compress(input, length, output);
+
+    /* else... */
+    return fastlz2_compress(input, length, output);
+}
+
+int fastlz_decompress(const void* input, int length, void* output, int maxout) {
+    /* magic identifier for compression level */
+    int level = ((*(const uint8_t*)input) >> 5) + 1;
+
+    if (level == 1)
+        return fastlz1_decompress(input, length, output, maxout);
+    if (level == 2)
+        return fastlz2_decompress(input, length, output, maxout);
+
+    /* unknown level, trigger error */
+    return 0;
+}
+
+int fastlz_compress_level(int level,
+                          const void* input,
+                          int length,
+                          void* output) {
+    if (level == 1)
+        return fastlz1_compress(input, length, output);
+    if (level == 2)
+        return fastlz2_compress(input, length, output);
+
+    return 0;
+}
+
+#pragma GCC diagnostic pop

+ 100 - 0
port/linux/package/pikascript/pikascript-lib/zlib/fastlz.h

@@ -0,0 +1,100 @@
+/*
+  FastLZ - Byte-aligned LZ77 compression library
+  Copyright (C) 2005-2020 Ariya Hidayat <ariya.hidayat@gmail.com>
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+
+#ifndef FASTLZ_H
+#define FASTLZ_H
+
+#define FASTLZ_VERSION 0x000500
+
+#define FASTLZ_VERSION_MAJOR 0
+#define FASTLZ_VERSION_MINOR 5
+#define FASTLZ_VERSION_REVISION 0
+
+#define FASTLZ_VERSION_STRING "0.5.0"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+  Compress a block of data in the input buffer and returns the size of
+  compressed block. The size of input buffer is specified by length. The
+  minimum input buffer size is 16.
+
+  The output buffer must be at least 5% larger than the input buffer
+  and can not be smaller than 66 bytes.
+
+  If the input is not compressible, the return value might be larger than
+  length (input buffer size).
+
+  The input buffer and the output buffer can not overlap.
+
+  Compression level can be specified in parameter level. At the moment,
+  only level 1 and level 2 are supported.
+  Level 1 is the fastest compression and generally useful for short data.
+  Level 2 is slightly slower but it gives better compression ratio.
+
+  Note that the compressed data, regardless of the level, can always be
+  decompressed using the function fastlz_decompress below.
+*/
+
+int fastlz_compress_level(int level,
+                          const void* input,
+                          int length,
+                          void* output);
+
+/**
+  Decompress a block of compressed data and returns the size of the
+  decompressed block. If error occurs, e.g. the compressed data is
+  corrupted or the output buffer is not large enough, then 0 (zero)
+  will be returned instead.
+
+  The input buffer and the output buffer can not overlap.
+
+  Decompression is memory safe and guaranteed not to write the output buffer
+  more than what is specified in maxout.
+
+  Note that the decompression will always work, regardless of the
+  compression level specified in fastlz_compress_level above (when
+  producing the compressed block).
+ */
+
+int fastlz_decompress(const void* input, int length, void* output, int maxout);
+
+/**
+  DEPRECATED.
+
+  This is similar to fastlz_compress_level above, but with the level
+  automatically chosen.
+
+  This function is deprecated and it will be completely removed in some future
+  version.
+*/
+
+int fastlz_compress(const void* input, int length, void* output);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* FASTLZ_H */

+ 9 - 0
port/linux/package/pikascript/zlib.py

@@ -0,0 +1,9 @@
+import _zlib
+
+
+def compress(data: bytes, level: int = 1) -> bytes:
+    return _zlib.compress(data, level)
+
+
+def decompress(data: bytes) -> bytes:
+    return _zlib.decompress(data)

+ 2 - 0
test/main.cpp

@@ -11,6 +11,8 @@ void test_purec(void);
 
 int main(int argc, char** argv) {
     int res = 0;
+    mkdir("./test/out/packout", 0777);
+    mkdir("./test/out/unpackout", 0777);
 #if USE_GOOGLE_TEST
     ::testing::InitGoogleTest(&argc, argv);
     res = RUN_ALL_TESTS();

+ 0 - 2
test/packtool-test.cpp

@@ -18,7 +18,6 @@ TEST(packtool, packfiles) {
     pikaMaker_linkRaw_New(maker, "test/out/G.bmp", "/bmp-file");
 
     // create "./test/out/packout" path if not exist
-    mkdir("./test/out/packout", 0777);
     ret = pikaMaker_linkCompiledModulesFullPath(maker, "./test/out/packout/a0424.pack");
 
     pikaMaker_deinit(maker);
@@ -33,7 +32,6 @@ TEST(packtool, packread) {
         pika_platform_printf("open file: %s error\r\n", "test/out/packout/a0424.pack");
     }
 
-    mkdir("./test/out/unpackout", 0777);
     FILE* file = pika_platform_fopen("test/out/unpackout/file3_test.txt", "wb+");
     if (NULL == file) {
         pika_platform_printf("open file: %s error\r\n", "test/out/unpackout/file3_test.txt");

+ 3 - 0
test/py-test.cpp

@@ -91,6 +91,9 @@ TEST_RUN_SINGLE_FILE_PASS(except,
                           isinstance,
                           "test/python/except/except_isinstance.py");
 
+TEST_RUN_SINGLE_FILE_PASS(zlib, zlib1, "test/python/zlib/zlib1.py")
+TEST_RUN_SINGLE_FILE(zlib, zlib_err, "test/python/zlib/zlib_err.py")
+
 #endif
 
 TEST_END

+ 131 - 0
test/python/zlib/zlib1.py

@@ -0,0 +1,131 @@
+import zlib
+import json
+
+data = {
+    "name": "John Doe",
+    "age": 30,
+    "cars": [
+        {"model": "BMW 230", "mpg": 27.5},
+        {"model": "Ford Edge", "mpg": 24.1}
+    ],
+    "data": [
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+        [1, 231],
+        [2, 232],
+        [3, 233],
+        [4, 234],
+        [5, 235],
+        [6, 236],
+        [7, 237],
+        [8, 238],
+        [9, 239],
+        [10, 240],
+        [11, 241],
+        [12, 242],
+        [13, 243],
+        [14, 244],
+    ]
+}
+
+# Convert the data to a JSON string
+json_str = json.dumps(data)
+
+# Original data
+original_data = json_str.encode()
+# print("size of original data: ", len(original_data))
+
+# Compress the data
+compressed_data = zlib.compress(original_data, level=1)
+
+# Decompress the data
+decompressed_data = zlib.decompress(compressed_data)
+
+# Check if the decompressed data is the same as the original data
+assert decompressed_data == original_data, 'Decompressed data does not match original data'
+
+# print("size of compressed data: ", len(compressed_data))
+
+print("PASS")

+ 16 - 0
test/python/zlib/zlib_err.py

@@ -0,0 +1,16 @@
+import zlib
+try:
+    zlib.compress("This should cause a TypeError because it's a str not bytes")
+except:
+    print("TypeError")
+
+try:
+    zlib.decompress("This should also cause a TypeError because it's a str not bytes")
+except:
+    print("TypeError")
+
+try:
+    zlib.decompress(b"This should cause a zlib.error because it's not valid zlib compressed data")
+except:
+    print("zlib.error")
+