luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

compression.c (2834B)


      1 #include "compression.h"
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <lauxlib.h>
      5 
      6 /* Create compression result */
      7 compression_result_t* compression_result_create(void) {
      8     compression_result_t* result = (compression_result_t*)malloc(sizeof(compression_result_t));
      9     if (result) {
     10         memset(result, 0, sizeof(compression_result_t));
     11     }
     12     return result;
     13 }
     14 
     15 /* Destroy compression result */
     16 void compression_result_destroy(compression_result_t* result) {
     17     if (!result) return;
     18     if (result->data) free(result->data);
     19     free(result);
     20 }
     21 
     22 /* Calculate maximum compressed size (worst case) */
     23 uint32_t calculate_max_compressed_size(uint32_t input_size) {
     24     /* Worst case: data expands slightly
     25      * For deflate: input_size + (input_size / 1000) + 12
     26      * For safety, use input_size * 1.1 + 16
     27      */
     28     return input_size + (input_size / 10) + 16;
     29 }
     30 
     31 /* Adler-32 checksum (used by zlib) */
     32 uint32_t adler32(uint32_t adler, const uint8_t* buf, uint32_t len) {
     33     const uint32_t BASE = 65521;  // Largest prime smaller than 65536
     34     uint32_t s1 = adler & 0xFFFF;
     35     uint32_t s2 = (adler >> 16) & 0xFFFF;
     36 
     37     if (buf == NULL) return 1;  // Initial value
     38 
     39     for (uint32_t i = 0; i < len; i++) {
     40         s1 = (s1 + buf[i]) % BASE;
     41         s2 = (s2 + s1) % BASE;
     42     }
     43 
     44     return (s2 << 16) | s1;
     45 }
     46 
     47 /* CRC32 table and calculation (used by gzip) */
     48 static uint32_t crc32_table[256];
     49 static int crc32_table_computed = 0;
     50 
     51 static void make_crc32_table(void) {
     52     for (uint32_t n = 0; n < 256; n++) {
     53         uint32_t c = n;
     54         for (int k = 0; k < 8; k++) {
     55             if (c & 1)
     56                 c = 0xEDB88320L ^ (c >> 1);
     57             else
     58                 c = c >> 1;
     59         }
     60         crc32_table[n] = c;
     61     }
     62     crc32_table_computed = 1;
     63 }
     64 
     65 uint32_t crc32_calc(uint32_t crc, const uint8_t* buf, uint32_t len) {
     66     if (!crc32_table_computed)
     67         make_crc32_table();
     68 
     69     uint32_t c = crc ^ 0xFFFFFFFFL;
     70 
     71     if (buf) {
     72         for (uint32_t n = 0; n < len; n++) {
     73             c = crc32_table[(c ^ buf[n]) & 0xFF] ^ (c >> 8);
     74         }
     75     }
     76 
     77     return c ^ 0xFFFFFFFFL;
     78 }
     79 
     80 /* Lua binding: Calculate Adler-32 checksum */
     81 int lua_adler32(lua_State* L) {
     82     size_t data_size;
     83     const uint8_t* data = (const uint8_t*)luaL_checklstring(L, 1, &data_size);
     84     uint32_t adler = luaL_optinteger(L, 2, 1);  // Initial value defaults to 1
     85 
     86     uint32_t result = adler32(adler, data, data_size);
     87     lua_pushinteger(L, result);
     88     return 1;
     89 }
     90 
     91 /* Lua binding: Calculate CRC32 checksum */
     92 int lua_crc32(lua_State* L) {
     93     size_t data_size;
     94     const uint8_t* data = (const uint8_t*)luaL_checklstring(L, 1, &data_size);
     95     uint32_t crc = luaL_optinteger(L, 2, 0);  // Initial value defaults to 0
     96 
     97     uint32_t result = crc32_calc(crc, data, data_size);
     98     lua_pushinteger(L, result);
     99     return 1;
    100 }