/*========================================================================= * FUNCTION: nativetx([B[I[I[BII[BI)V (STATIC) * CLASS: com/sun/midp/crypto/ARC4 * TYPE: static native function * OVERVIEW: Transform the given buffer * INTERFACE (operand stack manipulation): * parameters: S array of S box values * X first set intermediate results * Y second set of intermediate results * inbuf input buffer of data * inoff offset in the provided input buffer * inlen length of data to be processed * outbuf output buffer of data * outoff offset in the provided output buffer * returns: <nothing> *=======================================================================*/ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_crypto_ARC4_nativetx() { unsigned int x; unsigned int y; unsigned int tx; unsigned int ty; long outoff; long len; long inoff; long temp; long i; outoff = KNI_GetParameterAsInt(8); len = KNI_GetParameterAsInt(6); inoff = KNI_GetParameterAsInt(5); KNI_StartHandles(5); KNI_DeclareHandle(outbuf); KNI_DeclareHandle(inbuf); KNI_DeclareHandle(Y); KNI_DeclareHandle(X); KNI_DeclareHandle(S); KNI_GetParameterAsObject(7, outbuf); KNI_GetParameterAsObject(4, inbuf); KNI_GetParameterAsObject(3, Y); KNI_GetParameterAsObject(2, X); KNI_GetParameterAsObject(1, S); /*copy in the counters*/ KNI_GetRawArrayRegion(X, 0, 4, (jbyte*)&temp); x = temp; KNI_GetRawArrayRegion(Y, 0, 4, (jbyte*)&temp); y = temp; for (i = 0 ; i < len; i++) { x = (x + 1) & 0xff; y = (y + JavaByteArray(S)[x]) & 0xff; tx = JavaByteArray(S)[x]; JavaByteArray(S)[x] = JavaByteArray(S)[y]; JavaByteArray(S)[y] = tx; ty = (unsigned int)(JavaByteArray(S)[x] + JavaByteArray(S)[y]) & 0xff; JavaByteArray(outbuf)[i+outoff] = JavaByteArray(S)[ty] ^ JavaByteArray(inbuf)[i+inoff]; } temp = x; KNI_SetRawArrayRegion(X, 0, 4, (jbyte*)&temp); temp = y; KNI_SetRawArrayRegion(Y, 0, 4, (jbyte*)&temp); KNI_EndHandles(); KNI_ReturnVoid(); }
/* * Retrieves service record from the service search result. * * @param recHandle native handle of the service record * @param array byte array which will receive the data, * or null for size query * @return size of the data read/required */ KNIEXPORT KNI_RETURNTYPE_INT KNIDECL(com_sun_jsr082_bluetooth_SDPTransaction_getServiceRecord0) { jint retval = 0; javacall_handle id = 0; javacall_uint8 *data = NULL; javacall_uint16 size = 0; KNI_StartHandles(1); KNI_DeclareHandle(dataHandle); KNI_GetParameterAsObject(2, dataHandle); id = (javacall_handle)KNI_GetParameterAsInt(1); if (!KNI_IsNullHandle(dataHandle)) size = KNI_GetArrayLength(dataHandle); data = JAVAME_MALLOC(size); if (data == NULL) { KNI_ThrowNew(jsropOutOfMemoryError, "Out of memory inside SDDB.readRecord()"); } else { if (javacall_bt_sdp_get_service(id, data, &size) == JAVACALL_OK) { retval = size; if (!KNI_IsNullHandle(dataHandle)) { KNI_SetRawArrayRegion(dataHandle, 0, size, data); } } else { retval = 0; } JAVAME_FREE(data); } KNI_EndHandles(); KNI_ReturnInt(retval); }
/** * Converts an array of bytes to converted array of characters. * <p> * Java declaration: * <pre> * byteToChar(I[BII[CII)I * </pre> * * @param handler handle returned from getHandler * @param input buffer of bytes to be converted * @param in_offset offset into the provided buffer * @param in_len length of data to be processed * @param output buffer of converted bytes * @param out_offset offset into the provided output buffer * @param out_len length of data processed * * @return length of the converted string, or zero if the * arguments were not valid */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_cldc_i18n_j2me_Conv_byteToChar() { int outLength = KNI_GetParameterAsInt(7); int outOffset = KNI_GetParameterAsInt(6); int inLength = KNI_GetParameterAsInt(4); int inOffset = KNI_GetParameterAsInt(3); int id = KNI_GetParameterAsInt(1); char* inBuf; jchar* outBuf; jint result = 0; KNI_StartHandles(2); KNI_DeclareHandle(output); KNI_DeclareHandle(input); KNI_GetParameterAsObject(5, output); KNI_GetParameterAsObject(2, input); inBuf = (char*)midpMalloc(inLength); if (inBuf != NULL) { /* Instead of always multiplying the length by sizeof(jchar), * we shift left by 1. This can be done because jchar has a * size of 2 bytes. */ outBuf = (jchar*)midpMalloc(outLength<<1); if (outBuf != NULL) { KNI_GetRawArrayRegion(input, inOffset, inLength, (jbyte*)inBuf); result = (lcConv[id] ? lcConv[id]->nativeToUnicode((const unsigned char *) inBuf, inLength, outBuf, outLength): 0); KNI_SetRawArrayRegion(output, outOffset<<1, outLength<<1, (jbyte*)outBuf); midpFree(inBuf); midpFree(outBuf); } else { midpFree(inBuf); KNI_ThrowNew(midpOutOfMemoryError, NULL); } } else { KNI_ThrowNew(midpOutOfMemoryError, NULL); } KNI_EndHandles(); KNI_ReturnInt(result); }
/** * Gets a raw IPv4 address for the given hostname. * <p> * Java declaration: * <pre> * getIpNumber([B)I * </pre> * * @param szHost the hostname to lookup as a 'C' string * @param ipBytes Output parameter that represents an unsigned char * array for an IP address * @return len Length of ipBytes * */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_socket_Protocol_getIpNumber0(void) { int len = -1; int status = PCSL_NET_INVALID; unsigned char ipBytes[MAX_ADDR_LENGTH]; void* context = NULL; void* pcslHandle; MidpReentryData* info; KNI_StartHandles(2); KNI_DeclareHandle(hostObject); KNI_DeclareHandle(ipAddressObject); KNI_GetParameterAsObject(1, hostObject); KNI_GetParameterAsObject(2, ipAddressObject); info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ SNI_BEGIN_RAW_POINTERS; status = pcsl_network_gethostbyname_start( (char*)JavaByteArray(hostObject), ipBytes, MAX_ADDR_LENGTH, &len, &pcslHandle, &context); SNI_END_RAW_POINTERS; } else { /* Reinvocation after unblocking the thread */ pcslHandle = (void*)info->descriptor; /* IMPL NOTE: Please see 6440539 for details. */ /* All but windows implementations of pcsl_network_gethostbyname_finish */ /* ignore context parameter. Windows one expects status code there. */ context = (void*)info->status; status = pcsl_network_gethostbyname_finish(ipBytes, MAX_ADDR_LENGTH, &len, pcslHandle, context); } if (status == PCSL_NET_SUCCESS) { KNI_SetRawArrayRegion(ipAddressObject, 0, len, (jbyte *)ipBytes); } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(HOST_NAME_LOOKUP_SIGNAL, (int)pcslHandle, context); } else { /* must be PCSL_NET_IOERROR or PCSL_NET_INVALID */ len = -1; } KNI_EndHandles(); KNI_ReturnInt((jint)len); }
/** * Gets the MIDlet name for the given registered push connection. * <p> * Java declaration: * <pre> * getEntry0([B[BI)I * </pre> * * @param connection The connection to add to the push registry * @param midlet A byte array to store the MIDlet name * @param midletsize The size of <tt>midlet</tt> * * @return <tt>0</tt> if successful, otherwise <tt>-1</tt> * * @throw IOException if the registry entry is too long. */ KNIEXPORT KNI_RETURNTYPE_INT KNIDECL(com_sun_midp_io_j2me_push_ConnectionRegistry_getEntry0) { int midletsize; char *regentry; int regsize ; int ret = -1; int connLen; char *szConn = NULL; midletsize = KNI_GetParameterAsInt(3); KNI_StartHandles(2); KNI_DeclareHandle(conn); KNI_DeclareHandle(regObject); KNI_GetParameterAsObject(1, conn); connLen = KNI_GetArrayLength(conn); ret = -1; if ((szConn = midpMalloc(connLen)) != NULL) { KNI_GetRawArrayRegion(conn, 0, connLen, (jbyte*)szConn); KNI_GetParameterAsObject(2, regObject); regentry = pushfindconn(szConn); if (NULL != regentry) { regsize = strlen(regentry) + 1; if (regsize < midletsize) { KNI_SetRawArrayRegion(regObject, 0, regsize, (jbyte*)regentry); ret = 0; } else { KNI_ThrowNew(midpIOException, "registration too long"); } } midpFree(szConn); } else { KNI_ThrowNew(midpOutOfMemoryError, NULL); } KNI_EndHandles(); KNI_ReturnInt(ret); }
/** * Gets all registered push connections associated with the given MIDlet * suite. * <p> * Java declaration: * <pre> * list0([BZ[BI)I * </pre> * * @param midlet The MIDlet suite to get push registry entries * @param available Which connections to return. If <tt>true</tt>, * only return the connections with available input. * Otherwise, return all connection. * @param connectionlist A comma separated string of connections stored * as a byte array. * @param listsz The maximum length of the byte array that will be * accepted for <tt>connectionlist</tt> * * @return <tt>0</tt> if successful, otherwise <tt>-1</tt> * * @throw IllegalArgumentException If the length <tt>midlet</tt> is * too long. */ KNIEXPORT KNI_RETURNTYPE_INT KNIDECL(com_sun_midp_io_j2me_push_ConnectionRegistry_list0) { char *midletName = NULL; int available; int connsize; int nameLength; char *conn; int ret = -1; available = KNI_GetParameterAsBoolean(2); connsize = KNI_GetParameterAsInt(4); KNI_StartHandles(2); KNI_DeclareHandle(name); KNI_DeclareHandle(connections); KNI_GetParameterAsObject(1, name); KNI_GetParameterAsObject(3, connections); nameLength = KNI_GetArrayLength(name); midletName = (char*)midpMalloc(nameLength); if (midletName == NULL) { KNI_ThrowNew(midpOutOfMemoryError, NULL); } else { KNI_GetRawArrayRegion(name, 0, nameLength, (jbyte*)midletName); conn = pushfindsuite(midletName, available); if (conn != NULL) { KNI_SetRawArrayRegion(connections, 0, strlen(conn), (jbyte*)conn); midpFree(conn); ret = 0; } midpFree(midletName); } KNI_EndHandles(); KNI_ReturnInt(ret); }
/** * Retrieves resource data for the given ID and locale. * <p> * Java declaration: * <pre> * getRawResourceData0(I,I,[B)Z * </pre> * * @param hdata byte array to store resource data * @param resource_id resource identifier * @param locale_index index of locale in array of supported locales * @param offset offset of resource to start with * @param length length in bytes to copy * @return length in bytes of copied data */ KNIEXPORT KNI_RETURNTYPE_INT KNIDECL(com_sun_j2me_global_DevResourceBundleReader_getRawResourceData0) { jint locale_index = KNI_GetParameterAsInt(1); jint resource_id = KNI_GetParameterAsInt(2); jint offset = KNI_GetParameterAsInt(4); jint length = KNI_GetParameterAsInt(5); jint array_len; jbyte* buffer; KNI_StartHandles(1); KNI_DeclareHandle(hdata); KNI_GetParameterAsObject(3, hdata); array_len = KNI_GetArrayLength(hdata); if (array_len < length){ length = 0; KNI_ThrowNew(jsropIllegalArgumentException, "Error! Array size is too few!"); } else { buffer = JAVAME_MALLOC(array_len); if (NULL == buffer) { length = 0; KNI_ThrowNew(jsropOutOfMemoryError, "Cannot allocate buffer for device resource"); } else { if (JSR238_STATUSCODE_OK != jsr238_get_resource(locale_index,resource_id, buffer, offset, &length)) { length = 0; } else { KNI_SetRawArrayRegion(hdata, 0, length, buffer); } JAVAME_FREE(buffer); } } KNI_EndHandles(); KNI_ReturnInt(length); }
KNIEXPORT KNI_RETURNTYPE_OBJECT KNIDECL(com_sun_midp_chameleon_skins_resources_LoadedSkinData_readIntArray) { int arrayLength; int totalBytes; KNI_StartHandles(1); KNI_DeclareHandle(returnArray); do { /* * First, read array length */ ENSURE_SKIN_DATA_AVAILABILITY(sizeof(jint)); memcpy((void*)&arrayLength, (void*)gsSkinFileDataPos, sizeof(jint)); gsSkinFileDataPos += sizeof(jint); /* * Then create array */ totalBytes = arrayLength * sizeof(jint); ENSURE_SKIN_DATA_AVAILABILITY(totalBytes); SNI_NewArray(SNI_INT_ARRAY, arrayLength, returnArray); if (KNI_IsNullHandle(returnArray)) { KNI_ThrowNew(midpOutOfMemoryError, NULL); break; } /* * And finally read data into it */ KNI_SetRawArrayRegion(returnArray, 0, totalBytes, (jbyte*)gsSkinFileDataPos); gsSkinFileDataPos += totalBytes; } while (0); KNI_EndHandlesAndReturnObject(returnArray); }
/** * Stores the romized byte array in corresponding Java array * @param the required resource (initialFont or defaultFont) * */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_perseus_platform_ResourceHandler_getRomizedResource() { jbyte *font_data; int font_type = -1; int buflen = -1; int size = -1; KNI_StartHandles(2); KNI_DeclareHandle(clazz); KNI_DeclareHandle(bufHandle); KNI_GetClassPointer(clazz); font_type = KNI_GetParameterAsInt(1); if (font_type == DEFAULT_FONT) { KNI_GetStaticObjectField(clazz, KNI_GetStaticFieldID(clazz, "defaultFont", "[B"), bufHandle); size = sizeof(defaultFont); font_data = defaultFont; } else { KNI_GetStaticObjectField(clazz, KNI_GetStaticFieldID(clazz, "initialFont", "[B"), bufHandle); size = sizeof(initialFont); font_data = initialFont; } if (KNI_IsNullHandle(bufHandle)) { KNI_ThrowNew(midpNullPointerException, 0); } else { buflen = KNI_GetArrayLength(bufHandle); if (buflen < size) { //REPORT_ERROR(LC_HIGHUI, "java array too small"); KNI_ThrowNew(midpArrayIndexOutOfBoundsException, 0); } else { KNI_SetRawArrayRegion(bufHandle, 0, size, (jbyte*)font_data); } } KNI_EndHandles(); KNI_ReturnVoid(); }
KNIEXPORT KNI_RETURNTYPE_OBJECT KNIDECL(com_sun_midp_chameleon_skins_resources_LoadedSkinData_readByteArray) { int arrayLength = KNI_GetParameterAsInt(1); KNI_StartHandles(1); KNI_DeclareHandle(returnArray); do { ENSURE_SKIN_DATA_AVAILABILITY(arrayLength); SNI_NewArray(SNI_BYTE_ARRAY, arrayLength, returnArray); if (KNI_IsNullHandle(returnArray)) { KNI_ThrowNew(midpOutOfMemoryError, NULL); break; } KNI_SetRawArrayRegion(returnArray, 0, arrayLength, (jbyte*)gsSkinFileDataPos); gsSkinFileDataPos += arrayLength; } while (0); KNI_EndHandlesAndReturnObject(returnArray); }
KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf() { int result = -1; SocketBufferParameter *p; if (!ANI_Start()) { ANI_Wait(); KNI_ReturnInt(-1); } KNI_StartHandles(1); KNI_DeclareHandle(buffer_object); KNI_GetParameterAsObject(2, buffer_object); p = (SocketBufferParameter *) ANI_GetParameterBlock(NULL); if (p == NULL) { int buffer_size = KNI_GetParameterAsInt(4); p = new_buffer_parameter(/*fd*/ KNI_GetParameterAsInt(1), buffer_size); if (!ANI_UseFunction(asynchronous_socket_read, /*try_non_blocking*/ KNI_FALSE)) { ANI_BlockThread(); goto EXIT; } } if ((result = p->buffer_size) > 0) { jint offset = KNI_GetParameterAsInt(3); KNI_SetRawArrayRegion(buffer_object, offset, p->buffer_size, (jbyte *) p->buffer); } EXIT: KNI_EndHandles(); ANI_End(); KNI_ReturnInt(result); }
/* private native int _eglChooseConfig ( int display , int [ ] attrib_list , int [ ] configs , int config_size , int [ ] num_config ) ; */ KNIEXPORT KNI_RETURNTYPE_INT Java_javax_microedition_khronos_egl_EGL10Impl__1eglChooseConfig() { jint display = KNI_GetParameterAsInt(1); jint config_size = KNI_GetParameterAsInt(4); EGLint *attrib_list = (EGLint *)0; EGLConfig *configs = (EGLConfig *)0; EGLint num_config; jint returnValue = EGL_FALSE; KNI_StartHandles(3); KNI_DeclareHandle(attrib_listHandle); KNI_DeclareHandle(configsHandle); KNI_DeclareHandle(num_configHandle); KNI_GetParameterAsObject(2, attrib_listHandle); KNI_GetParameterAsObject(3, configsHandle); KNI_GetParameterAsObject(5, num_configHandle); if (!KNI_IsNullHandle(attrib_listHandle)) { jint attrib_list_length, attrib_list_size; /* Construct attrib_list as a copy of attrib_listHandle */ attrib_list_length = KNI_GetArrayLength(attrib_listHandle); attrib_list_size = (attrib_list_length + 2)*sizeof(jint); attrib_list = (EGLint *)JSR239_malloc(attrib_list_size); if (!attrib_list) { KNI_ThrowNew("java.lang.OutOfMemoryException", "eglChooseConfig"); goto exit; } KNI_GetRawArrayRegion(attrib_listHandle, 0, attrib_list_size, (jbyte *)attrib_list); } #ifdef ALL_WINDOWS_ARE_PIXMAPS // Force queries with SURFACE_TYPE == WINDOW to actually query pixmap if (attrib_list) { jint attr, val, idx; idx = 0; while (1) { attr = attrib_list[idx]; #ifdef DEBUG printf("attr[%d] = %d\n", idx, attr); #endif if (attr == EGL_NONE) { #ifdef DEBUG printf("attr = EGL_NONE, done\n"); #endif break; } if (attr == EGL_SURFACE_TYPE) { #ifdef DEBUG printf("attr = EGL_SURFACE_TYPE\n"); #endif val = attrib_list[idx + 1]; if ((val & EGL_WINDOW_BIT) != 0) { #ifdef DEBUG printf("Switching WINDOW_BIT to PIXMAP_BIT\n"); #endif val |= EGL_PIXMAP_BIT; val &= ~EGL_WINDOW_BIT; } } idx += 2; } } #endif /* Allocate config_size elements if configsHandle is non-null */ if (!KNI_IsNullHandle(configsHandle)) { configs = (EGLConfig *)JSR239_malloc(config_size*sizeof(EGLint)); if (!configs) { KNI_ThrowNew("java.lang.OutOfMemoryException", "eglChooseConfig"); goto exit; } } returnValue = (jint)eglChooseConfig((EGLDisplay)display, attrib_list, configs, config_size, &num_config); #ifdef DEBUG printf("eglChooseConfig(0x%x, attrib_list, configs, %d, num_config<-%d) = %d\n", display, config_size, num_config, returnValue); #endif /* Copy configs back to Java */ if (returnValue == EGL_TRUE && !KNI_IsNullHandle(configsHandle)) { KNI_SetRawArrayRegion(configsHandle, 0, config_size*sizeof(EGLint), (const jbyte *)configs); } /* Set output parameter via num_configHandle */ if ((returnValue == EGL_TRUE) && !KNI_IsNullHandle(num_configHandle)) { KNI_SetIntArrayElement(num_configHandle, 0, num_config); } exit: if (attrib_list) { JSR239_free(attrib_list); } if (configs) { JSR239_free(configs); } KNI_EndHandles(); KNI_ReturnInt(returnValue); }
/* private native int _eglGetConfigs ( int display , int [ ] configs , int config_size , int [ ] num_config ) ; */ KNIEXPORT KNI_RETURNTYPE_INT Java_javax_microedition_khronos_egl_EGL10Impl__1eglGetConfigs() { jint display = KNI_GetParameterAsInt(1); jint config_size = KNI_GetParameterAsInt(3); EGLConfig *configs = (EGLConfig *)0; EGLint num_config = 0; jint returnValue = EGL_FALSE; KNI_StartHandles(2); KNI_DeclareHandle(configsHandle); KNI_DeclareHandle(num_configHandle); KNI_GetParameterAsObject(2, configsHandle); KNI_GetParameterAsObject(4, num_configHandle); if (!KNI_IsNullHandle(configsHandle)) { configs = (EGLConfig *)JSR239_malloc(config_size*sizeof(EGLint)); if (!configs) { KNI_ThrowNew("java.lang.OutOfMemoryException", "eglGetConfigs"); goto exit; } } returnValue = (jint)eglGetConfigs((EGLDisplay)display, configs, (EGLint)config_size, &num_config); #ifdef DEBUG printf("num_config = %d\n", num_config); #endif #ifdef ALL_WINDOWS_ARE_PIXMAPS { EGLBoolean ok; EGLint surfaceType; int i, j; // Remove all configs that are for windows only if (returnValue && configs) { for (i = 0; i < num_config; i++) { ok = eglGetConfigAttrib((EGLDisplay)display, configs[i], EGL_SURFACE_TYPE, &surfaceType); if ((surfaceType & (EGL_PIXMAP_BIT | EGL_PBUFFER_BIT)) == 0) { for (j = i; j < num_config - 1; j++) { configs[j] = configs[j + 1]; } configs[--num_config] = 0; #ifdef DEBUG printf("Deleting a config, num_config = %d\n", num_config); #endif } } } } #endif #ifdef DEBUG printf("eglGetConfigs(0x%x, configs, %d, num_config<-%d) = %d\n", display, config_size, num_config, returnValue); #endif if ((returnValue == EGL_TRUE) && !KNI_IsNullHandle(configsHandle)) { KNI_SetRawArrayRegion(configsHandle, 0, config_size*sizeof(EGLint), (const jbyte *)configs); } if ((returnValue == EGL_TRUE) && !KNI_IsNullHandle(num_configHandle)) { KNI_SetIntArrayElement(num_configHandle, 0, num_config); } exit: if (configs) { JSR239_free(configs); } KNI_EndHandles(); KNI_ReturnInt(returnValue); }
/*========================================================================= * FUNCTION: modExp([B[B[B[B[B)V (STATIC) * CLASS: com/sun/midp/crypto/RSA * TYPE: static native function * OVERVIEW: Perform modular exponentiation. * INTERFACE (operand stack manipulation): * parameters: data contains the data on which exponentiation is to * be performed * exponent contains the exponent, e.g. 65537 (decimal) is * written as a three-byte array containing * 0x01, 0x00, 0x01 * modulus contains the modulus * result the result of the modular exponentiation is * returned in this array * returns: the length of the result *=======================================================================*/ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_crypto_RSA_modExp() { jint dataLen, expLen, modLen, resLen; jint maxLen; INTEGER numbytes = 0; unsigned char *buf; BIGNUM *a, *b, *c, *d; BN_CTX *ctx; KNI_StartHandles(4); KNI_DeclareHandle(ires); KNI_DeclareHandle(imod); KNI_DeclareHandle(iexp); KNI_DeclareHandle(idata); KNI_GetParameterAsObject(4, ires); KNI_GetParameterAsObject(3, imod); KNI_GetParameterAsObject(2, iexp); KNI_GetParameterAsObject(1, idata); resLen = KNI_GetArrayLength(ires); modLen = KNI_GetArrayLength(imod); expLen = KNI_GetArrayLength(iexp); dataLen = KNI_GetArrayLength(idata); /* Find which parameter is largest and allocate that much space */ maxLen = MAX(MAX(MAX(resLen, modLen), expLen), dataLen); if (maxLen > (BN_MAX_INTEGER / 8)) { /* The number of BITS must fit in a BN integer. */ KNI_ThrowNew(midpIllegalArgumentException, "arg too long"); } else { buf = (unsigned char *) midpMalloc(maxLen * sizeof(unsigned char)); if (buf == NULL) { KNI_ThrowNew(midpOutOfMemoryError, NULL); } else { KNI_GetRawArrayRegion(idata, 0, dataLen, (jbyte*)buf); a = BN_bin2bn(buf, (INTEGER)dataLen, NULL); KNI_GetRawArrayRegion(iexp, 0, expLen, (jbyte*)buf); b = BN_bin2bn(buf, (INTEGER)expLen, NULL); KNI_GetRawArrayRegion(imod, 0, modLen, (jbyte*)buf); c = BN_bin2bn(buf, (INTEGER)modLen, NULL); d = BN_new((INTEGER)resLen); ctx = BN_CTX_new((INTEGER)maxLen); /* do the actual exponentiation */ if (a != NULL && b != NULL && c != NULL && d != NULL && ctx != NULL && BN_mod_exp_mont(d,a,b,c,ctx)) { /* Covert result from BIGNUM d to char array */ numbytes = BN_bn2bin(d, buf); KNI_SetRawArrayRegion(ires, 0, numbytes, (jbyte*)buf); } else { /* assume out of mem */ KNI_ThrowNew(midpOutOfMemoryError, "Mod Exp"); } midpFree(buf); BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_CTX_free(ctx); } } KNI_EndHandles(); KNI_ReturnInt((jint)numbytes); }
KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_pisces_AbstractSurface_getRGB() { KNI_StartHandles(2); KNI_DeclareHandle(objectHandle); KNI_DeclareHandle(arrayHandle); jint offset = KNI_GetParameterAsInt(2); jint scanLength = KNI_GetParameterAsInt(3); jint x = KNI_GetParameterAsInt(4); jint y = KNI_GetParameterAsInt(5); jint width = KNI_GetParameterAsInt(6); jint height = KNI_GetParameterAsInt(7); jint dstX = 0; jint dstY = 0; Surface* surface; KNI_GetParameterAsObject(1, arrayHandle); KNI_GetThisPointer(objectHandle); surface = (Surface*)JLongToPointer( KNI_GetLongField(objectHandle, fieldIds[SURFACE_NATIVE_PTR])); CORRECT_DIMS(surface, x, y, width, height, dstX, dstY); if ((width > 0) && (height > 0)) { jint size = ((height - 1) * scanLength + width) * sizeof(jint); jint* tempArray = (jint*)PISCESmalloc(size); if (NULL == tempArray) { KNI_ThrowNew("java/lang/OutOfMemoryError", "Allocation of temporary renderer memory buffer failed."); } else { jint* src; jint* dst; jint srcScanRest = surface->width - width; jint dstScanRest = scanLength - width; ACQUIRE_SURFACE(surface, objectHandle); src = (jint*)surface->data + y * surface->width + x; dst = tempArray; for (; height > 0; --height) { jint w2 = width; for (; w2 > 0; --w2) { *dst++ = *src++; } src += srcScanRest; dst += dstScanRest; } offset += dstY * scanLength + dstX; KNI_SetRawArrayRegion(arrayHandle, offset * sizeof(jint), size, (const jbyte*)tempArray); RELEASE_SURFACE(surface, objectHandle); PISCESfree(tempArray); } } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Copy a native Invocation to the supplied Invocation instance. * @param invoc the native InvocStore * @param mode the mode of copyout * @param invocObj the Invocation object to copy to * @param argsObj an object to use to refer to the arguments array * @param obj a temporary object handle * @return 0 if there were problems allocating Java Strings; * 1 if all the copies succeeded; * -1 if the Java Arrays allocated for args or data were insufficient */ static int copyOut(StoredInvoc *invoc, int mode, jobject invocObj, jobject argsObj, jobject obj) { int datalen = 0; int arraylen = 0; /* Set the required lengths for args and data arrays. */ KNI_SetIntField(invocObj, argsLenFid, invoc->argsLen); KNI_SetIntField(invocObj, dataLenFid, invoc->dataLen); /* Check if size of argument array and data array are correct. */ KNI_GetObjectField(invocObj, dataFid, obj); datalen = KNI_GetArrayLength(obj); if (datalen != invoc->dataLen) { /* Data array allocated by Java is not correct size. */ return -1; } KNI_GetObjectField(invocObj, argumentsFid, obj); arraylen = KNI_GetArrayLength(obj); if (arraylen != invoc->argsLen) { /* Args array allocated by Java is not correct size. */ return -1; } /* Copy out all the string fields. */ if (!(storeField(&invoc->url, invocObj, urlFid, obj) && storeField(&invoc->type, invocObj, typeFid, obj) && storeField(&invoc->action, invocObj, actionFid, obj) && storeField(&invoc->ID, invocObj, IDFid, obj) && storeField(&invoc->invokingClassname, invocObj, invokingClassnameFid, obj) && storeField(&invoc->invokingAuthority, invocObj, invokingAuthorityFid, obj) && storeField(&invoc->invokingAppName, invocObj, invokingAppNameFid, obj) && storeField(&invoc->invokingID, invocObj, invokingIDFid, obj) && storeField(&invoc->username, invocObj, usernameFid, obj) && storeField(&invoc->password, invocObj, passwordFid, obj))) { /* Some String allocation failed. */ return 0; } KNI_SetIntField(invocObj, invokingSuiteIdFid, invoc->invokingSuiteId); /* See if the suite and classname are needed. */ if (mode == MODE_TID || mode == MODE_TID_PREV || mode == MODE_TID_NEXT) { if (!storeField(&invoc->classname, invocObj, classnameFid, obj)) { /* A string allocation failed. */ return 0; } KNI_SetIntField(invocObj, suiteIdFid, invoc->suiteId); } /* Return the arguments if any; array length already checked. */ if (invoc->argsLen > 0) { int ndx; pcsl_string* args; /* For each stored arg create a string and store in the array. * No stored arg is null. If a string cannot be created * it is due to insufficient heap memory. */ KNI_GetObjectField(invocObj, argumentsFid, argsObj); args = invoc->args; for (ndx = 0; ndx < invoc->argsLen; ndx++, args++) { if (!pcsl_string_is_null(args)) { if (PCSL_STRING_OK != midp_jstring_from_pcsl_string(args, obj)) { /* String create failed; exit now. */ return 0; } } else { KNI_ReleaseHandle(obj); } KNI_SetObjectArrayElement(argsObj, ndx, obj); } } /* Return the data array if any; array length was already checked. */ if (invoc->dataLen > 0) { KNI_GetObjectField(invocObj, dataFid, obj); KNI_SetRawArrayRegion(obj, 0, invoc->dataLen, invoc->data); } KNI_SetBooleanField(invocObj, responseRequiredFid, invoc->responseRequired); KNI_SetIntField(invocObj, statusFid, invoc->status); KNI_SetIntField(invocObj, tidFid, invoc->tid); KNI_SetIntField(invocObj, previousTidFid, invoc->previousTid); /* Successful copy out. */ return 1; }
/* * Accepts incoming client connection request. * * Note: the method gets native connection handle directly from * <code>handle<code> field of <code>BTSPPNotifierImpl</code> object. * * Note: new native connection handle to work with accepted incoming * client connection is setted directly to <code>handle</code> field of * appropriate <code>RFCOMMConnectionImpl</code> object. * * @throws IOException if an I/O error occurs */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_jsr082_bluetooth_btspp_BTSPPNotifierImpl_accept0(void) { javacall_handle handle = JAVACALL_BT_INVALID_HANDLE; javacall_handle peer_handle = JAVACALL_BT_INVALID_HANDLE; MidpReentryData* info; int status = JAVACALL_FAIL; int processStatus = KNI_FALSE; void *context = NULL; javacall_bt_address peer_addr; jfieldID notifHandleID = NULL; jfieldID peerHandleID = NULL; jfieldID peerAddrID = NULL; jfieldID pushHandleID = NULL; KNI_StartHandles(3); KNI_DeclareHandle(thisHandle); KNI_DeclareHandle(arrayHandle); KNI_DeclareHandle(classHandle); KNI_GetClassPointer(classHandle); GET_FIELDID(classHandle, "handle", "I", notifHandleID) GET_FIELDID(classHandle, "peerHandle", "I", peerHandleID) GET_FIELDID(classHandle, "peerAddress", "[B", peerAddrID) GET_FIELDID(classHandle, "pushHandle", "I", pushHandleID) KNI_GetThisPointer(thisHandle); handle = (javacall_handle)KNI_GetIntField(thisHandle, notifHandleID); KNI_GetObjectField(thisHandle, peerAddrID, arrayHandle); if (handle == JAVACALL_BT_INVALID_HANDLE) { REPORT_ERROR(LC_PROTOCOL, "RFCOMM notifier was closed before btspp_notif::accept"); KNI_ThrowNew(midpInterruptedIOException, EXCEPTION_MSG( "RFCOMM notifier was closed")); } else { #ifndef NO_PUSH bt_pushid_t pushid = KNI_GetIntField(thisHandle, pushHandleID); if (pushid != BT_INVALID_PUSH_HANDLE) { if (bt_push_checkout_client(pushid, &peer_handle, peer_addr, NULL, NULL) == JAVACALL_OK) { pushcheckoutaccept((int)handle); processStatus = KNI_TRUE; status = JAVACALL_OK; } } #endif if (peer_handle == JAVACALL_BT_INVALID_HANDLE) { info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ REPORT_INFO1(LC_PROTOCOL, "btspp_notif::accept handle=%d\n", handle); // Need revisit: add resource counting /* * An incoming socket connection counts against the client socket * resource limit. */ /* if (midpCheckResourceLimit(RSC_TYPE_BT_CLI, 1) == 0) { const char* pMsg = "Resource limit exceeded for BT client sockets"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); */ // } else { status = javacall_bt_rfcomm_accept( handle, &peer_handle, &peer_addr); processStatus = KNI_TRUE; // } } else { /* Reinvocation after unblocking the thread */ if ((javacall_handle)info->descriptor != handle) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "btspp_notif::accept handles mismatched %d != %d\n", handle, info->descriptor); REPORT_CRIT(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIllegalStateException, EXCEPTION_MSG( "Internal error in btspp_notif::accept")); } else { // Need revisit: add resource counting /* if (midpCheckResourceLimit(RSC_TYPE_BT_CLI, 1) == 0) { const char* pMsg = "Resource limit exceeded for BT client sockets" REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } else { */ context = info->pResult; status = javacall_bt_rfcomm_accept( handle, &peer_handle, &peer_addr); processStatus = KNI_TRUE; // } } } } if (processStatus) { REPORT_INFO1(LC_PROTOCOL, "btspp_notif::accept server handle=%d\n", handle); if (status == JAVACALL_OK) { // Need revisit: add resource counting /* if (midpIncResourceCount(RSC_TYPE_BT_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "btspp_notif: Resource limit update error"); } */ // store client native connection handle for temporary storing KNI_SetIntField(thisHandle, peerHandleID, (jint)peer_handle); // copy address to Java object field KNI_SetRawArrayRegion(arrayHandle, 0, JAVACALL_BT_ADDRESS_SIZE, (jbyte*) peer_addr); REPORT_INFO(LC_PROTOCOL, "btspp_notif::accept incoming connection accepted!"); } else if (status == JAVACALL_WOULD_BLOCK) { midp_thread_wait(NETWORK_READ_SIGNAL, (int)handle, context); } else if (status == JAVACALL_FAIL) { char* pError; javacall_bt_rfcomm_get_error(handle, &pError); midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IO error in btspp_notif::accept (%s)\n", pError); REPORT_INFO(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(gKNIBuffer)); } else { char* pMsg = "Unknown error during btspp_notif::accept"; REPORT_INFO(LC_PROTOCOL, pMsg); KNI_ThrowNew(midpIOException, EXCEPTION_MSG(pMsg)); } } } KNI_EndHandles(); KNI_ReturnVoid(); }