/* --- GATEWAY FUNCTION --- */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { char *action; unsigned char *IN = NULL, *OUT = NULL; size_t INlen, OUTlen; /* Check for proper number of arguments */ if (nrhs < 2) mexErrMsgTxt("Not enough input arguments."); else if (nrhs > 2) mexErrMsgTxt("Too many input arguments."); else if (nlhs > 1) mexErrMsgTxt("Too many output arguments."); /* The input ACTION must be a string */ if (!mxIsChar(prhs[0])) mexErrMsgTxt("Input ACTION must be a string."); action = mxArrayToString(prhs[0]); /* The input IN must be a real uint8 array */ if (!mxIsUint8(prhs[1]) || mxIsComplex(prhs[1])) mexErrMsgTxt("Input IN must be a real uint8 array."); INlen = mxGetNumberOfElements(prhs[1]); IN = mxGetData(prhs[1]); if (!strcmp(action,"D")) { /* Decompress data */ OUT = tinfl_decompress_mem_to_heap(IN, INlen, &OUTlen, TINFL_FLAG_PARSE_ZLIB_HEADER); if (OUT == NULL) mexErrMsgTxt("Error when decompressing data."); } else if (!strcmp(action,"C")) { /* Compress data */ OUT = tdefl_compress_mem_to_heap(IN, INlen, &OUTlen, TDEFL_WRITE_ZLIB_HEADER); if (OUT == NULL) mexErrMsgTxt("Error when compressing data."); } else { mexErrMsgTxt("Unknown ACTION type."); } /* */ plhs[0] = mxCreateNumericMatrix(OUTlen,1,mxUINT8_CLASS,mxREAL); if (plhs[0] == NULL) mexErrMsgTxt("Error when creating output variable."); memcpy(mxGetData(plhs[0]), OUT, OUTlen); mxFree(action); mz_free(OUT); }
static int ltdefl(lua_State* L) { size_t in_len; const char* in_buf = luaL_checklstring(L, 1, &in_len); size_t out_len; int flags = luaL_optint(L, 2, 0); char* out_buf = tdefl_compress_mem_to_heap(in_buf, in_len, &out_len, flags); lua_pushlstring(L, out_buf, out_len); free(out_buf); return 1; }