static ERL_NIF_TERM brotli_encode(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { unsigned int quality; ErlNifBinary data, encoded; if (argc != 2) { return (BADARG); } if (!enif_inspect_iolist_as_binary(env, argv[0], &data)) { return (BADARG); } if (!enif_get_uint(env, argv[1], &quality)) { return (BADARG); } size_t length = data.size; size_t output_length = BrotliEncoderMaxCompressedSize(data.size); uint8_t* output = malloc(output_length); BROTLI_BOOL ok = BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, length, data.data, &output_length, output); if(!ok) { free(output); return (BADARG); } if (!enif_alloc_binary(output_length, &encoded)) { free(output); return (BADARG); } memcpy(encoded.data, output, output_length); free(output); return enif_make_binary(env, &encoded); }
static size_t squash_brotli_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_size) { return BrotliEncoderMaxCompressedSize(uncompressed_size); }