ffi_snappy.nut 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. auto bpack=sqpack.pack;
  2. auto bpack_to_userdata=sqpack.pack_to_userdata;
  3. auto bunpack=sqpack.unpack;
  4. auto bpushmem=sqpack.pushmem;
  5. auto bgetaddress=sqpack.getaddress;
  6. local libsnappy = ffi.load(".libs/libsnappy.so");
  7. enum snappy_status {
  8. SNAPPY_OK = 0,
  9. SNAPPY_INVALID_INPUT = 1,
  10. SNAPPY_BUFFER_TOO_SMALL = 2
  11. };
  12. /*
  13. snappy_status snappy_compress(const char* input,
  14. size_t input_length,
  15. char* compressed,
  16. size_t* compressed_length);
  17. snappy_status snappy_uncompress(const char* compressed,
  18. size_t compressed_length,
  19. char* uncompressed,
  20. size_t* uncompressed_length);
  21. size_t snappy_max_compressed_length(size_t source_length);
  22. snappy_status snappy_uncompressed_length(const char* compressed,
  23. size_t compressed_length,
  24. size_t* result);
  25. snappy_status snappy_validate_compressed_buffer(const char* compressed,
  26. size_t compressed_length);
  27. */
  28. auto udata = bpushmem(1024);
  29. auto udata_address = bgetaddress(udata);
  30. print(udata, type(udata), udata_address);
  31. local snappy_compress_fptr = libsnappy.bind_func("i", "snappy_compress", ["s", "i", "s", "i"]);
  32. local snappy_uncompress_fptr = libsnappy.bind_func("i", "snappy_uncompress", ["s", "i", "s", "i"]);
  33. local snappy_max_compressed_length = libsnappy.bind_func("i", "snappy_max_compressed_length", ["i"]);
  34. local snappy_uncompressed_length = libsnappy.bind_func("i", "snappy_uncompressed_length", ["s", "i", "i"]);
  35. local function snappy_compress(str)
  36. {
  37. auto slen = str.len();
  38. auto smlen = snappy_max_compressed_length(slen);
  39. auto compressed = bpushmem(smlen);
  40. auto compressed_length = bpack_to_userdata("i", smlen);
  41. auto status = snappy_compress_fptr(str, slen, compressed, bgetaddress(compressed_length));
  42. auto clen = bunpack(compressed_length, "i")[0];
  43. auto cstr = bunpack(compressed, "A" + clen)[0];
  44. print(status, compressed, smlen, clen, cstr.len());
  45. return cstr;
  46. }
  47. local function snappy_uncompress(str)
  48. {
  49. auto slen = str.len();
  50. auto compressed_length = bpack_to_userdata("i", slen);
  51. auto status = snappy_uncompressed_length(str, slen, bgetaddress(compressed_length));
  52. auto clen = bunpack(compressed_length, "i")[0];
  53. auto uncompressed = bpushmem(clen);
  54. status = snappy_uncompress_fptr(str, slen, uncompressed, bgetaddress(compressed_length));
  55. clen = bunpack(compressed_length, "i")[0];
  56. auto cstr = bunpack(uncompressed, "A" + clen)[0];
  57. print(status, uncompressed, slen, clen, cstr.len());
  58. return cstr;
  59. }
  60. auto str = readfile(vargv[0]);
  61. print(snappy_uncompress(snappy_compress(str)));
  62. local smlen = snappy_max_compressed_length(1200);
  63. print(smlen);