static int io_read (lua_State *L) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); int lastarg = lua_gettop(L) - 1; int firstarg = 1; FILE *f = gethandle(L, ctrl, firstarg); int n; if (f) firstarg++; else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */ lua_pop(L, 1); if (firstarg > lastarg) { /* no arguments? */ lua_settop(L, 0); /* erase upvalue and other eventual garbage */ firstarg = lastarg = 1; /* correct indices */ lua_pushstring(L, "*l"); /* push default argument */ } else /* ensure stack space for all results and for auxlib's buffer */ luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments"); for (n = firstarg; n<=lastarg; n++) { int success; if (lua_isnumber(L, n)) success = read_chars(L, f, (size_t)lua_tonumber(L, n)); else { const char *p = luaL_check_string(L, n); if (p[0] != '*') success = read_pattern(L, f, p); /* deprecated! */ else { switch (p[1]) { case 'n': /* number */ if (!read_number(L, f)) goto endloop; /* read fails */ continue; /* number is already pushed; avoid the "pushstring" */ case 'l': /* line */ success = read_line(L, f); break; case 'a': /* file */ read_file(L, f); success = 1; /* always success */ break; case 'w': /* word */ success = read_word(L, f); break; default: luaL_argerror(L, n, "invalid format"); success = 0; /* to avoid warnings */ } } } if (!success) { lua_pop(L, 1); /* remove last result */ break; /* read fails */ } } endloop: return n - firstarg; }
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy (JNIEnv *env, jobject obj) { tjhandle handle=0; gethandle(); if(tjDestroy(handle)==-1) _throw(tjGetErrorStr()); (*env)->SetLongField(env, obj, _fid, 0); bailout: return; }
static int ch_additem (lua_State* l) { HWND h = gethandle(l,1); const char* str = NULL; if (lua_type(l,2)!=LUA_TTABLE) str = luaL_checkstring(l,2); int pos = luaL_optint(l,3,0) -1; if (pos<-1) pos += 1 + SendMessage(h, LB_GETCOUNT, 0, 0); if (!str) return ch_addmultitems(l, 2, h, pos); const wchar_t* wstr = strcvt(str, CP_UTF8, CP_UTF16, NULL); SendMessage(h, LB_INSERTSTRING, pos, wstr); free(wstr); lua_settop(l,1); return 1; }
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3IIIIIII_3BIII (JNIEnv *env, jobject obj, jintArray src, jint x, jint y, jint width, jint stride, jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual, jint flags) { tjhandle handle=0; unsigned long jpegSize=0; jsize arraySize=0, actualStride; unsigned char *srcBuf=NULL, *jpegBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || stride<0) _throw("Invalid argument in compress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throw("Mismatch between Java and C API"); if(tjPixelSize[pf]!=sizeof(jint)) _throw("Pixel format must be 32-bit when compressing from an integer buffer."); actualStride=(stride==0)? width:stride; arraySize=(y+height-1)*actualStride + x+width; if((*env)->GetArrayLength(env, src)<arraySize) _throw("Source buffer is not large enough"); jpegSize=tjBufSize(width, height, jpegSubsamp); if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) _throw("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjCompress2(handle, &srcBuf[(y*actualStride + x)*sizeof(int)], width, stride*sizeof(jint), height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, flags|TJFLAG_NOREALLOC)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); jpegBuf=srcBuf=NULL; _throw(tjGetErrorStr()); } bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); return (jint)jpegSize; }
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) { tjhandle handle=0; jsize arraySize=0; unsigned char *srcBuf=NULL, *dstBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0) _throw("Invalid argument in compress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throw("Mismatch between Java and C API"); if(tjPixelSize[pf]!=sizeof(jint)) _throw("Pixel format must be 32-bit when encoding from an integer buffer."); arraySize=(pitch==0)? width*height:pitch*height; if((*env)->GetArrayLength(env, src)<arraySize) _throw("Source buffer is not large enough"); if((*env)->GetArrayLength(env, dst) <(jsize)tjBufSizeYUV(width, height, subsamp)) _throw("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjEncodeYUV2(handle, srcBuf, width, pitch*sizeof(jint), height, pf, dstBuf, subsamp, flags)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); dstBuf=srcBuf=NULL; _throw(tjGetErrorStr()); } bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); return; }
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3IIIIIIII (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jintArray dst, jint x, jint y, jint width, jint stride, jint height, jint pf, jint flags) { tjhandle handle=0; jsize arraySize=0, actualStride; unsigned char *jpegBuf=NULL, *dstBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) _throw("Invalid argument in decompress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throw("Mismatch between Java and C API"); if(tjPixelSize[pf]!=sizeof(jint)) _throw("Pixel format must be 32-bit when decompressing to an integer buffer."); if((*env)->GetArrayLength(env, src)<jpegSize) _throw("Source buffer is not large enough"); actualStride=(stride==0)? width:stride; arraySize=(y+height-1)*actualStride + x+width; if((*env)->GetArrayLength(env, dst)<arraySize) _throw("Destination buffer is not large enough"); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, &dstBuf[(y*actualStride + x)*sizeof(int)], width, stride*sizeof(jint), height, pf, flags)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); dstBuf=jpegBuf=NULL; _throw(tjGetErrorStr()); } bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); return; }
JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compress___3BIIII_3BIII (JNIEnv *env, jobject obj, jbyteArray src, jint width, jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual, jint flags) { tjhandle handle=0; unsigned long jpegSize=0; jsize arraySize=0; unsigned char *srcBuf=NULL, *jpegBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0) _throw("Invalid argument in compress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throw("Mismatch between Java and C API"); arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height; if((*env)->GetArrayLength(env, src)<arraySize) _throw("Source buffer is not large enough"); jpegSize=tjBufSize(width, height, jpegSubsamp); if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) _throw("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjCompress2(handle, srcBuf, width, pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, flags|TJFLAG_NOREALLOC)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); jpegBuf=srcBuf=NULL; _throw(tjGetErrorStr()); } bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); return (jint)jpegSize; }
static jint TJCompressor_compress (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint x, jint y, jint width, jint pitch, jint height, jint pf, jbyteArray dst, jint jpegSubsamp, jint jpegQual, jint flags) { tjhandle handle=0; unsigned long jpegSize=0; jsize arraySize=0, actualPitch; unsigned char *srcBuf=NULL, *jpegBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0) _throwarg("Invalid argument in compress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throwarg("Mismatch between Java and C API"); actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize) _throwarg("Source buffer is not large enough"); jpegSize=tjBufSize(width, height, jpegSubsamp); if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) _throwarg("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(ProcessSystemProperties(env)<0) goto bailout; if(tjCompress2(handle, &srcBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, jpegQual, flags|TJFLAG_NOREALLOC)==-1) _throwtj(); bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); return (jint)jpegSize; }
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress___3BI_3BIIIIIII (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags) { tjhandle handle=0; jsize arraySize=0, actualPitch; unsigned char *jpegBuf=NULL, *dstBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF) _throw("Invalid argument in decompress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throw("Mismatch between Java and C API"); if((*env)->GetArrayLength(env, src)<jpegSize) _throw("Source buffer is not large enough"); actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; if((*env)->GetArrayLength(env, dst)<arraySize) _throw("Destination buffer is not large enough"); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjDecompress2(handle, jpegBuf, (unsigned long)jpegSize, &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, flags)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); dstBuf=jpegBuf=NULL; _throw(tjGetErrorStr()); } bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); return; }
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jbyteArray dst, jint flags) { tjhandle handle=0; unsigned char *jpegBuf=NULL, *dstBuf=NULL; int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0; gethandle(); if((*env)->GetArrayLength(env, src)<jpegSize) _throw("Source buffer is not large enough"); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); if((*env)->GetArrayLength(env, dst) <(jsize)tjBufSizeYUV(jpegWidth, jpegHeight, jpegSubsamp)) _throw("Destination buffer is not large enough"); bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjDecompressToYUV(handle, jpegBuf, (unsigned long)jpegSize, dstBuf, flags)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); dstBuf=jpegBuf=NULL; _throw(tjGetErrorStr()); } bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); return; }
static int io_write (lua_State *L) { int lastarg = lua_gettop(L) - 1; IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); int arg = 1; int status = 1; FILE *f = gethandle(L, ctrl, arg); if (f) arg++; else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */ for (; arg <= lastarg; arg++) { if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */ /* optimization: could be done exactly as for strings */ status = status && fprintf(f, "%ld", lua_tonumber(L, arg)) > 0; } else { size_t l; const char *s = luaL_check_lstr(L, arg, &l); status = status && (fwrite(s, sizeof(char), l, f) == l); } } pushresult(L, status); return 1; }
/* * NAME: rsrc->resize() * DESCRIPTION: change the length of a resource */ void *rsrc_resize(void *rdata, unsigned long newsize) { rsrchandle *newrsrc, *rsrc = gethandle(rdata); if (rsrc->len == newsize) goto done; newrsrc = (rsrchandle *) REALLOC(rsrc, byte, sizeof(rsrchandle) + newsize); if (newrsrc == 0) ERROR(ENOMEM, 0); newrsrc->attrs |= RSRC_RES_CHANGED; newrsrc->len = newsize; rdata = getrdata(newrsrc); done: return rdata; fail: return 0; }
static int ch_newindex (lua_State* l) { HWND h = gethandle(l,1); if (lua_isnumber(l,2)) { int pos = luaL_checkint(l,2); if (pos==0) pos=-1; if (pos<0) pos += SendMessage(h, LB_GETCOUNT, 0, 0); else pos--; const char* str = luaL_checkstring(l,3); const wchar_t* wstr = strcvt(str, CP_UTF8, CP_UTF16, NULL); int sel = SendMessage(h, LB_GETCURSEL, 0, 0); SendMessage(h, LB_DELETESTRING, pos, 0); SendMessage(h, LB_INSERTSTRING, pos, wstr); SendMessage(h, LB_SETCURSEL, sel, 0); free(wstr); return 0; } const char* nm = luaL_checkstring(l,2); if (streq(nm, "selectedIndex")) { int n = lua_tonumber(l,3); if (n==0) n=-1; else if (n<0) n += SendMessage(h, LB_GETCOUNT, 0, 0); else n--; SendMessage(h, LB_SETCURSEL, n, 0); } else if (streq(nm, "selectedItem")) { const char* str = luaL_checkstring(l,3); const wchar_t* wstr = strcvt(str, CP_UTF8, CP_UTF16, NULL); SendMessage(h, LB_SELECTSTRING, -1, wstr); free(wstr); } else if (streq(nm, "onClose")) SetProp(h, L"onClose", lua_topointer(l,3)); else if (streq(nm, "onAction")) SetProp(h, L"onAction", lua_topointer(l,3)); else if (streq(nm, "onSelect")) SetProp(h, L"onSelect", lua_topointer(l,3)); else if (streq(nm, "onContextMenu")) SetProp(h, L"onContextMenu", lua_topointer(l,3)); //SUITE else luaL_argerror(l, 2, "undefined property"); return 0; }
/* * NAME: rsrc->size() * DESCRIPTION: return the length of a resource in bytes */ unsigned long rsrc_size(void *rdata) { rsrchandle *rsrc = gethandle(rdata); return rsrc->len; }
static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) { FILE *f = gethandle(L, ctrl, arg); luaL_arg_check(L, f, arg, "invalid file handle"); return f; }
int luacommonhandle_setfocus (lua_State* l) { HWND h = gethandle(l,1); SetForegroundWindow(GetParent(h)); lua_settop(l,1); return 1; }
static int ch_clear (lua_State* l) { HWND h = gethandle(l,1); lua_settop(l,0); SendMessage(h, LB_RESETCONTENT, 0, 0); return 0; }
static int ch_getlen (lua_State* l) { HWND h = gethandle(l,1); lua_settop(l,0); lua_pushinteger(l, SendMessage(h, LB_GETCOUNT, 0, 0)); return 1; }
static int ch_index (lua_State* l) { HWND h = gethandle(l,1); if (lua_isnumber(l,2)) { int pos = luaL_checkint(l,2); if (pos==0) pos=-1; if (pos<0) pos += SendMessage(h, LB_GETCOUNT, 0, 0); else pos--; int len = SendMessage(h, LB_GETTEXTLEN, pos, 0); wchar_t* wstr = malloc(sizeof(wchar_t) * (len+1)); SendMessage(h, LB_GETTEXT, pos, wstr); wstr[len]=0; const char* str = strncvt(wstr, len, CP_UTF16, CP_UTF8, &len); lua_settop(l,0); lua_pushlstring(l, str, len); free(str); free(wstr); return 1; } const char* nm = luaL_checkstring(l,2); if (streq(nm, "selectedIndex")) { int cur = SendMessage(h, LB_GETCURSEL, 0, 0); if (cur==LB_ERR || cur<0) lua_pushinteger(l,0); else lua_pushinteger(l,cur+1); return 1; } else if (streq(nm, "selectedItem")) { int cur = SendMessage(h, LB_GETCURSEL, 0, 0); if (cur==LB_ERR || cur<0) return 0; int len = SendMessage(h, LB_GETTEXTLEN, cur, 0); wchar_t* wstr = malloc(sizeof(wchar_t) * (len+1)); SendMessage(h, LB_GETTEXT, cur, wstr); wstr[len]=0; const char* str = strncvt(wstr, len, CP_UTF16, CP_UTF8, &len); lua_settop(l,0); lua_pushlstring(l, str, len); free(str); free(wstr); return 1; } else if (streq(nm, "closed")) { lua_pushboolean(l, IsWindow(h)!=0); return 1; } else if (streq(nm, "onClose")) { void* p = GetProp(h, L"onClose"); if (p) lua_pushluafunction(l,p); else lua_pushnil(l); return 1; } else if (streq(nm, "onAction")) { void* p = GetProp(h, L"onAction"); if (p) lua_pushluafunction(l,p); else lua_pushnil(l); return 1; } else if (streq(nm, "onSelect")) { void* p = GetProp(h, L"onSelect"); if (p) lua_pushluafunction(l,p); else lua_pushnil(l); return 1; } else if (streq(nm, "onContextMenu")) { void* p = GetProp(h, L"onContextMenu"); if (p) lua_pushluafunction(l,p); else lua_pushnil(l); return 1; } else if (streq(nm, "hwnd")) { lua_pushlightuserdata(l, h); return 1; } //SUITE return luaL_getmetafield(l, 1, nm); }
/* TurboJPEG 1.4.x: TJCompressor::compressFromYUV() */ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFromYUV___3_3B_3II_3III_3BII (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets, jint width, jintArray jSrcStrides, jint height, jint subsamp, jbyteArray dst, jint jpegQual, jint flags) { tjhandle handle=0; unsigned long jpegSize=0; jbyteArray jSrcPlanes[3]={NULL, NULL, NULL}; unsigned char *srcPlanes[3], *jpegBuf=NULL; int *srcOffsets=NULL, *srcStrides=NULL; int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i; gethandle(); if(subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) _throwarg("Invalid argument in compressFromYUV()"); if(org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) _throwarg("Mismatch between Java and C API"); if((*env)->GetArrayLength(env, srcobjs)<nc) _throwarg("Planes array is too small for the subsampling type"); if((*env)->GetArrayLength(env, jSrcOffsets)<nc) _throwarg("Offsets array is too small for the subsampling type"); if((*env)->GetArrayLength(env, jSrcStrides)<nc) _throwarg("Strides array is too small for the subsampling type"); jpegSize=tjBufSize(width, height, subsamp); if((*env)->GetArrayLength(env, dst)<(jsize)jpegSize) _throwarg("Destination buffer is not large enough"); bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0)); bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0)); for(i=0; i<nc; i++) { int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp); int pw=tjPlaneWidth(i, width, subsamp); if(planeSize<0 || pw<0) _throwarg(tjGetErrorStr()); if(srcOffsets[i]<0) _throwarg("Invalid argument in compressFromYUV()"); if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0) _throwarg("Negative plane stride would cause memory to be accessed below plane boundary"); bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i)); if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+planeSize) _throwarg("Source plane is not large enough"); bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i], 0)); srcPlanes[i]=&srcPlanes[i][srcOffsets[i]]; } bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(ProcessSystemProperties(env)<0) goto bailout; if(tjCompressFromYUVPlanes(handle, srcPlanes, width, srcStrides, height, subsamp, &jpegBuf, &jpegSize, jpegQual, flags|TJFLAG_NOREALLOC)==-1) _throwtj(); bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, jpegBuf, 0); for(i=0; i<nc; i++) { if(srcPlanes[i] && jSrcPlanes[i]) (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i], srcPlanes[i], 0); } if(srcStrides) (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrides, 0); if(srcOffsets) (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffsets, 0); return (jint)jpegSize; }
static PyObject * sp_CreateProcess(PyObject* self, PyObject* args) { BOOL result; PROCESS_INFORMATION pi; STARTUPINFO si; PyObject* environment; char* application_name; char* command_line; PyObject* process_attributes; /* ignored */ PyObject* thread_attributes; /* ignored */ int inherit_handles; int creation_flags; PyObject* env_mapping; char* current_directory; PyObject* startup_info; if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess", &application_name, &command_line, &process_attributes, &thread_attributes, &inherit_handles, &creation_flags, &env_mapping, ¤t_directory, &startup_info)) return NULL; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); /* note: we only support a small subset of all SI attributes */ si.dwFlags = getint(startup_info, "dwFlags"); si.wShowWindow = getint(startup_info, "wShowWindow"); si.hStdInput = gethandle(startup_info, "hStdInput"); si.hStdOutput = gethandle(startup_info, "hStdOutput"); si.hStdError = gethandle(startup_info, "hStdError"); if (PyErr_Occurred()) return NULL; if (env_mapping == Py_None) environment = NULL; else { environment = getenvironment(env_mapping); if (! environment) return NULL; } Py_BEGIN_ALLOW_THREADS result = CreateProcess(application_name, command_line, NULL, NULL, inherit_handles, creation_flags, environment ? PyString_AS_STRING(environment) : NULL, current_directory, &si, &pi); Py_END_ALLOW_THREADS Py_XDECREF(environment); if (! result) return PyErr_SetFromWindowsErr(GetLastError()); return Py_BuildValue("NNii", sp_handle_new(pi.hProcess), sp_handle_new(pi.hThread), pi.dwProcessId, pi.dwThreadId); }
JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transform (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize, jobjectArray dstobjs, jobjectArray tobjs, jint flags) { tjhandle handle=0; int i; unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0; unsigned long *dstSizes=NULL; tjtransform *t=NULL; jbyteArray *jdstBufs=NULL; int jpegWidth=0, jpegHeight=0, jpegSubsamp; jintArray jdstSizes=0; jint *dstSizesi=NULL; JNICustomFilterParams *params=NULL; gethandle(); if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) _throw("Source buffer is not large enough"); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); n=(*env)->GetArrayLength(env, dstobjs); if(n!=(*env)->GetArrayLength(env, tobjs)) _throw("Mismatch between size of transforms array and destination buffers array"); if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) _throw("Memory allocation failure"); if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) _throw("Memory allocation failure"); if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) _throw("Memory allocation failure"); if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) _throw("Memory allocation failure"); if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)*n)) ==NULL) _throw("Memory allocation failure"); for(i=0; i<n; i++) { dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0; memset(&t[i], 0, sizeof(tjtransform)); memset(¶ms[i], 0, sizeof(JNICustomFilterParams)); } for(i=0; i<n; i++) { jobject tobj, cfobj; bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i)); bailif0(_cls=(*env)->GetObjectClass(env, tobj)); bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I")); t[i].op=(*env)->GetIntField(env, tobj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I")); t[i].options=(*env)->GetIntField(env, tobj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I")); t[i].r.x=(*env)->GetIntField(env, tobj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I")); t[i].r.y=(*env)->GetIntField(env, tobj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I")); t[i].r.w=(*env)->GetIntField(env, tobj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I")); t[i].r.h=(*env)->GetIntField(env, tobj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf", "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;")); cfobj=(*env)->GetObjectField(env, tobj, _fid); if(cfobj) { params[i].env=env; params[i].tobj=tobj; params[i].cfobj=cfobj; t[i].customFilter=JNICustomFilter; t[i].data=(void *)¶ms[i]; } } bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0)); for(i=0; i<n; i++) { int w=jpegWidth, h=jpegHeight; if(t[i].r.w!=0) w=t[i].r.w; if(t[i].r.h!=0) h=t[i].r.h; bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); if((unsigned long)(*env)->GetArrayLength(env, jdstBufs[i]) <tjBufSize(w, h, jpegSubsamp)) _throw("Destination buffer is not large enough"); bailif0(dstBufs[i]=(*env)->GetPrimitiveArrayCritical(env, jdstBufs[i], 0)); } if(tjTransform(handle, jpegBuf, jpegSize, n, dstBufs, dstSizes, t, flags|TJFLAG_NOREALLOC)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); jpegBuf=NULL; for(i=0; i<n; i++) { (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); dstBufs[i]=NULL; } _throw(tjGetErrorStr()); } jdstSizes=(*env)->NewIntArray(env, n); bailif0(dstSizesi=(*env)->GetIntArrayElements(env, jdstSizes, 0)); for(i=0; i<n; i++) dstSizesi[i]=(int)dstSizes[i]; bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, jsrcBuf, jpegBuf, 0); if(dstBufs) { for(i=0; i<n; i++) { if(dstBufs[i] && jdstBufs && jdstBufs[i]) (*env)->ReleasePrimitiveArrayCritical(env, jdstBufs[i], dstBufs[i], 0); } free(dstBufs); } if(jdstBufs) free(jdstBufs); if(dstSizes) free(dstSizes); if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi, 0); if(t) free(t); return jdstSizes; }
// retrieve and cache a module handle from already loaded modules // (This function won't attempt to load the requested module/library.) HMODULE getlib(HMODULE *modptr, const char *name) { gethandle(modptr, name); if (*modptr == NULL) error("FAILED to retrieve module handle for '%s'", name); return *modptr; }
/* TurboJPEG 1.4.x: TJDecompressor::decompressToYUV() */ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompressToYUV___3BI_3_3B_3II_3III (JNIEnv *env, jobject obj, jbyteArray src, jint jpegSize, jobjectArray dstobjs, jintArray jDstOffsets, jint desiredWidth, jintArray jDstStrides, jint desiredHeight, jint flags) { tjhandle handle=0; jbyteArray jDstPlanes[3]={NULL, NULL, NULL}; unsigned char *jpegBuf=NULL, *dstPlanes[3]; int *dstOffsets=NULL, *dstStrides=NULL; int jpegSubsamp=-1, jpegWidth=0, jpegHeight=0; int nc=0, i, width, height, scaledWidth, scaledHeight, nsf=0; tjscalingfactor *sf; gethandle(); if((*env)->GetArrayLength(env, src)<jpegSize) _throwarg("Source buffer is not large enough"); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); nc=(jpegSubsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3); width=desiredWidth; height=desiredHeight; if(width==0) width=jpegWidth; if(height==0) height=jpegHeight; sf=tjGetScalingFactors(&nsf); if(!sf || nsf<1) _throwarg(tjGetErrorStr()); for(i=0; i<nsf; i++) { scaledWidth=TJSCALED(jpegWidth, sf[i]); scaledHeight=TJSCALED(jpegHeight, sf[i]); if(scaledWidth<=width && scaledHeight<=height) break; } bailif0(dstOffsets=(*env)->GetPrimitiveArrayCritical(env, jDstOffsets, 0)); bailif0(dstStrides=(*env)->GetPrimitiveArrayCritical(env, jDstStrides, 0)); for(i=0; i<nc; i++) { int planeSize=tjPlaneSizeYUV(i, scaledWidth, dstStrides[i], scaledHeight, jpegSubsamp); int pw=tjPlaneWidth(i, scaledWidth, jpegSubsamp); if(planeSize<0 || pw<0) _throwarg(tjGetErrorStr()); if(dstOffsets[i]<0) _throwarg("Invalid argument in decompressToYUV()"); if(dstStrides[i]<0 && dstOffsets[i]-planeSize+pw<0) _throwarg("Negative plane stride would cause memory to be accessed below plane boundary"); bailif0(jDstPlanes[i]=(*env)->GetObjectArrayElement(env, dstobjs, i)); if((*env)->GetArrayLength(env, jDstPlanes[i])<dstOffsets[i]+planeSize) _throwarg("Destination plane is not large enough"); bailif0(dstPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jDstPlanes[i], 0)); dstPlanes[i]=&dstPlanes[i][dstOffsets[i]]; } bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); if(tjDecompressToYUVPlanes(handle, jpegBuf, (unsigned long)jpegSize, dstPlanes, desiredWidth, dstStrides, desiredHeight, flags)==-1) _throwtj(); bailout: if(jpegBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, jpegBuf, 0); for(i=0; i<nc; i++) { if(dstPlanes[i] && jDstPlanes[i]) (*env)->ReleasePrimitiveArrayCritical(env, jDstPlanes[i], dstPlanes[i], 0); } if(dstStrides) (*env)->ReleasePrimitiveArrayCritical(env, jDstStrides, dstStrides, 0); if(dstOffsets) (*env)->ReleasePrimitiveArrayCritical(env, jDstOffsets, dstOffsets, 0); return; }
static void TJDecompressor_decodeYUV (JNIEnv *env, jobject obj, jobjectArray srcobjs, jintArray jSrcOffsets, jintArray jSrcStrides, jint subsamp, jarray dst, jint dstElementSize, jint x, jint y, jint width, jint pitch, jint height, jint pf, jint flags) { tjhandle handle=0; jsize arraySize=0, actualPitch; jbyteArray jSrcPlanes[3]={NULL, NULL, NULL}; unsigned char *srcPlanes[3], *dstBuf=NULL; int *srcOffsets=NULL, *srcStrides=NULL; int nc=(subsamp==org_libjpegturbo_turbojpeg_TJ_SAMP_GRAY? 1:3), i; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || subsamp<0 || subsamp>=org_libjpegturbo_turbojpeg_TJ_NUMSAMP) _throwarg("Invalid argument in decodeYUV()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF || org_libjpegturbo_turbojpeg_TJ_NUMSAMP!=TJ_NUMSAMP) _throwarg("Mismatch between Java and C API"); if((*env)->GetArrayLength(env, srcobjs)<nc) _throwarg("Planes array is too small for the subsampling type"); if((*env)->GetArrayLength(env, jSrcOffsets)<nc) _throwarg("Offsets array is too small for the subsampling type"); if((*env)->GetArrayLength(env, jSrcStrides)<nc) _throwarg("Strides array is too small for the subsampling type"); actualPitch=(pitch==0)? width*tjPixelSize[pf]:pitch; arraySize=(y+height-1)*actualPitch + (x+width)*tjPixelSize[pf]; if((*env)->GetArrayLength(env, dst)*dstElementSize<arraySize) _throwarg("Destination buffer is not large enough"); bailif0(srcOffsets=(*env)->GetPrimitiveArrayCritical(env, jSrcOffsets, 0)); bailif0(srcStrides=(*env)->GetPrimitiveArrayCritical(env, jSrcStrides, 0)); for(i=0; i<nc; i++) { int planeSize=tjPlaneSizeYUV(i, width, srcStrides[i], height, subsamp); int pw=tjPlaneWidth(i, width, subsamp); if(planeSize<0 || pw<0) _throwarg(tjGetErrorStr()); if(srcOffsets[i]<0) _throwarg("Invalid argument in decodeYUV()"); if(srcStrides[i]<0 && srcOffsets[i]-planeSize+pw<0) _throwarg("Negative plane stride would cause memory to be accessed below plane boundary"); bailif0(jSrcPlanes[i]=(*env)->GetObjectArrayElement(env, srcobjs, i)); if((*env)->GetArrayLength(env, jSrcPlanes[i])<srcOffsets[i]+planeSize) _throwarg("Source plane is not large enough"); bailif0(srcPlanes[i]=(*env)->GetPrimitiveArrayCritical(env, jSrcPlanes[i], 0)); srcPlanes[i]=&srcPlanes[i][srcOffsets[i]]; } bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjDecodeYUVPlanes(handle, srcPlanes, srcStrides, subsamp, &dstBuf[y*actualPitch + x*tjPixelSize[pf]], width, pitch, height, pf, flags)==-1) _throwtj(); bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); for(i=0; i<nc; i++) { if(srcPlanes[i] && jSrcPlanes[i]) (*env)->ReleasePrimitiveArrayCritical(env, jSrcPlanes[i], srcPlanes[i], 0); } if(srcStrides) (*env)->ReleasePrimitiveArrayCritical(env, jSrcStrides, srcStrides, 0); if(srcOffsets) (*env)->ReleasePrimitiveArrayCritical(env, jSrcOffsets, srcOffsets, 0); return; }
int CMemUtil::readmemory(DWORD processId, DWORD memAddress, char* result, DWORD size, bool addBaseAddress, bool useCache /*= true*/) { HANDLE dwHandle = gethandle(processId); if (dwHandle == NULL) return 1; void *ptr; if (addBaseAddress) ptr = (void *)(memAddress - 0x400000 + GetProcessBaseAddr(processId)); else ptr = (void *)memAddress; if (useCache) { DWORD alignedAddr = ((DWORD)ptr) & (0xFFFFFFFF - (MEMORY_CACHE_ENTRY_SIZE - 1)); DWORD alignOffset = ((DWORD)ptr) & (MEMORY_CACHE_ENTRY_SIZE - 1); DWORD bufOffset = 0; LARGE_INTEGER currentTimestamp; QueryPerformanceCounter(¤tTimestamp); while(bufOffset < size) { if (m_memoryCache[alignedAddr].expirationTime < currentTimestamp.QuadPart) { // Refresh the cache entry DWORD ret = readmemory(processId, alignedAddr, m_memoryCache[alignedAddr].value, MEMORY_CACHE_ENTRY_SIZE, 0, false); if (ret != 0) return ret; m_memoryCache[alignedAddr].expirationTime = currentTimestamp.QuadPart + m_cacheTimeoutTicks; } DWORD entryReadSize = min(size - bufOffset, MEMORY_CACHE_ENTRY_SIZE - alignOffset); // Load the entry data memcpy(result + bufOffset, (m_memoryCache[alignedAddr].value + alignOffset), entryReadSize); // After the first load, there's no longer an align alignOffset = 0; alignedAddr += MEMORY_CACHE_ENTRY_SIZE; bufOffset += entryReadSize; } return 0; } if (ReadProcessMemory(dwHandle, ptr, result, size, NULL)) { return 0; } else { if (::GetLastError() == ERROR_INVALID_HANDLE) { //FILE *f=fopen("C:/out.txt","a+"); //fprintf(f,"time %lld old %d,",time(NULL),dwHandle); dwHandle = NULL; for (int iter = 1000; iter > 0; iter--) { dwHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, m_prevProcessId); if (dwHandle) { char buf[111]; sprintf(buf, "iter %d", iter); if (iter != 1000) MessageBox(NULL, buf, "", 0); break; } Sleep(10); } //fprintf(f,"new %d\n",dwHandle); //fclose(f); m_prevProcessHandle = dwHandle; if (ReadProcessMemory(dwHandle, ptr, result, size, NULL)) return 0; } if (::GetLastError() == ERROR_NOACCESS) { CloseHandle(dwHandle); dwHandle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, m_prevProcessId); m_prevProcessHandle = dwHandle; if (ReadProcessMemory(dwHandle, ptr, result, size, NULL)) return 0; } if (::GetLastError() == ERROR_PARTIAL_COPY) { //Possibly Tibia has been killed //Test valid address; taCMemUtil::GetMemIntValue(-1) void *ptrTest = (void*)(0x0410000 + GetProcessBaseAddr(processId)); int resultTest; if (!ReadProcessMemory(dwHandle, ptrTest, &resultTest, 4, NULL)) { //try getting new handle dwHandle = NULL; for (int iter = 1000; iter > 0; iter--) { dwHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, m_prevProcessId); if (dwHandle) { char buf[111]; sprintf(buf, "iter %d", iter); if (iter != 1000) MessageBox(NULL, buf, "", 0); break; } Sleep(10); } m_prevProcessHandle = dwHandle; if (ReadProcessMemory(dwHandle, ptr, result, size, NULL)) { return 0; } else { DWORD terminatedStatus = 9999; if (GetExitCodeProcess(dwHandle, &terminatedStatus)) if (terminatedStatus != STILL_ACTIVE) //If Tibia is no longer active then close TA PostQuitMessage(0); } } else { //Tibia is still running but a bad memory address was given return 1; } //fprintf(f,"new %d\n",dwHandle); //fclose(f); } DWORD err = ::GetLastError(); CloseHandle(dwHandle); m_prevProcessId = -1; PostQuitMessage(0); return err; } }
/* * NAME: rsrc->changed() * DESCRIPTION: indicate a changed resource which must be rewritten to disk */ void rsrc_changed(void *rdata) { rsrchandle *rsrc = gethandle(rdata); rsrc->attrs |= RSRC_RES_CHANGED; }
#include <lua.h> #include <lauxlib.h> #include "spacenav.h" static spacenav_t *gethandle(lua_State *L) { return *((spacenav_t**)luaL_checkudata(L,1,"spacenav")); } static int delete(lua_State *L) { spacenav_t *sn = gethandle(L); spacenav_destroy(sn); lua_pushnil(L); lua_setmetatable(L,1); return 0; } static int get_vals(lua_State *L) { spacenav_t *sn = gethandle(L); sn_axes_t v; int res; res = spacenav_get(sn, &v); if (res) return 0; lua_createtable(L, 0, 8);