/* * Return a 32 bit handle to an open a file in storage in different modes. * * See "I/O Modes" and "Filenames" above for move information. * * If not successful *ppszError will set to point to an error string, * on success it will be set to NULL. */ int storage_open(char** ppszError, const pcsl_string* filename_str, int ioMode) { int flags = 0; int openStatus; void *handle; *ppszError = NULL; if (OPEN_READ == ioMode) { DEBUGP2F("opening for read only %s\n", filename_str); flags |= PCSL_FILE_O_RDONLY; } else { if (!storage_file_exists(filename_str)) { flags |= PCSL_FILE_O_CREAT; } else if (OPEN_READ_WRITE_TRUNCATE == ioMode) { flags |= PCSL_FILE_O_TRUNC; } if (OPEN_WRITE == ioMode) { DEBUGP2F("opening write only %s\n", filename_str); flags |= PCSL_FILE_O_WRONLY; } else { DEBUGP2F("opening read write %s\n", filename_str); flags |= PCSL_FILE_O_RDWR; } } /** * Verify that the resource is available well within limit as per * the policy in ResourceLimiter */ if (midpCheckResourceLimit(RSC_TYPE_FILE, 1) == 0) { REPORT_INFO(LC_CORE, "Resource limit exceeded for file handles"); *ppszError = (char *)FILE_LIMIT_ERROR; return -1; } openStatus = pcsl_file_open(filename_str, flags, &handle); REPORT_INFO1(LC_CORE, "storage_open allocated file_desc %d\n", (int)handle); if (-1 == openStatus) { *ppszError = storage_get_last_file_error("storage_open()", filename_str); return -1; } /* Update the resource count */ if (midpIncResourceCount(RSC_TYPE_FILE, 1) == 0) { REPORT_INFO(LC_CORE, "FILE : resource limit update error"); } #if REPORT_LEVEL <= LOG_INFORMATION DEBUGP2F("created %s\n", filename_str); #endif return (int)handle; }
extern "C" void gxpport_createimmutable_from_mutable (gxpport_mutableimage_native_handle srcMutableImagePtr, gxpport_image_native_handle *newImmutableImagePtr, gxutl_native_image_error_codes* creationErrorPtr) { /* Convert from source QPixmap to destination QImage */ QPixmap* srcPixmap = gxpportqt_get_mutableimage_pixmap(srcMutableImagePtr); int rscSize = ImgRscSize(srcPixmap); /* new img is the same size */ /* Check resource limit before copying */ if (midpCheckResourceLimit(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { /* Exceeds resource limit */ *creationErrorPtr = GXUTL_NATIVE_IMAGE_RESOURCE_LIMIT; return; } _Platform_ImmutableImage* immutableImage = (_Platform_ImmutableImage*)midpMalloc(sizeof(_Platform_ImmutableImage)); if (immutableImage == NULL) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } immutableImage->qimage = new QImage(srcPixmap->convertToImage()); if (NULL == immutableImage->qimage) { midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } if (immutableImage->qimage->isNull()) { delete immutableImage->qimage; midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } /* Copying succeeds */ if (midpIncResourceCount(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { REPORT_ERROR(LC_LOWUI, "Error in updating resource limit" " for Immutable image"); } immutableImage->marker = 1; immutableImage->qpixmap = NULL; *newImmutableImagePtr = immutableImage; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; }
extern "C" void gxpport_loadimmutable_from_platformbuffer (unsigned char* srcBuffer, int length, jboolean isStatic, int* ret_imgWidth, int* ret_imgHeight, gxpport_image_native_handle *newImmutableImagePtr, gxutl_native_image_error_codes* creationErrorPtr) { int rscSize; gxutl_image_buffer_raw* dataBuffer = (gxutl_image_buffer_raw*)srcBuffer; /* Check resource limit */ rscSize = ImgRegionRscSize(NULL, dataBuffer->width, dataBuffer->height); if (midpCheckResourceLimit(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { /* Exceed Resource limit */ *creationErrorPtr = GXUTL_NATIVE_IMAGE_RESOURCE_LIMIT; return; } _Platform_ImmutableImage* immutableImage = (_Platform_ImmutableImage*)midpMalloc(sizeof(_Platform_ImmutableImage)); if (immutableImage == NULL) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } if (!load_raw(&immutableImage->qimage, dataBuffer, length, isStatic, ret_imgWidth, ret_imgHeight)) { midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR; return; } /* Image creation succeeds */ if (midpIncResourceCount(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { REPORT_ERROR(LC_LOWUI, "Error in updating resource limit" " for Immutable image"); } immutableImage->marker = 7; immutableImage->qpixmap = NULL; *newImmutableImagePtr = immutableImage; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; }
extern "C" void gxpport_create_mutable(gxpport_mutableimage_native_handle *newImagePtr, int width, int height, gxutl_native_image_error_codes* creationErrorPtr) { /* set optimization for all QPixmaps move this to startup/initialization code. */ QPixmap::setDefaultOptimization(QPixmap::BestOptim); /* * Populate Java data pointer before calling checkResourceLimit * because it might trigger GC and makes the Java pointer invalid. */ QPixmap* qpixmap = new QPixmap(); /* Start with a NULL image object */ if (NULL == qpixmap) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } /* Check resource limit before really creating image memory */ if (midpCheckResourceLimit(RSC_TYPE_IMAGE_MUT, ImgRegionRscSize(width, height)) == 0) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_RESOURCE_LIMIT; delete qpixmap; return; } /* ALLOCATE MEMORY FOR QT IMAGE DATA */ qpixmap->resize(width, height); /* INITIALIZE CONTENTS TO WHITE */ qpixmap->fill(); /* Update the resource count */ if (midpIncResourceCount(RSC_TYPE_IMAGE_MUT, ImgRscSize(qpixmap)) == 0) { REPORT_INFO(LC_LOWUI,"Error in updating resource" " limit for Mutable image"); } /* return the qpixmap pointer */ *newImagePtr = qpixmap; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; }
extern "C" void gxpport_decodeimmutable_from_argb (jint* srcBuffer, int width, int height, jboolean processAlpha, gxpport_image_native_handle* newImmutableImagePtr, gxutl_native_image_error_codes* creationErrorPtr) { // use bytes as is, QT images have the same binary data layout QImage qimage = QImage((unsigned char*)srcBuffer, width, height, IMAGE_DEPTH, NULL, 0, QImage::IgnoreEndian); if (qimage.isNull()) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR; return; } /* Check Resource limit */ int rscSize = ImgRscSize(&qimage); if (midpCheckResourceLimit(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { /* Exceed Resource limit */ *creationErrorPtr = GXUTL_NATIVE_IMAGE_RESOURCE_LIMIT; return; } qimage.setAlphaBuffer(processAlpha == KNI_TRUE?TRUE:FALSE); _Platform_ImmutableImage* immutableImage = (_Platform_ImmutableImage*)midpMalloc(sizeof(_Platform_ImmutableImage)); if (immutableImage == NULL) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } // make a deep copy of the image immutableImage->qimage = new QImage(qimage.copy()); if (NULL == immutableImage->qimage) { midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } if (immutableImage->qimage->isNull()) { delete immutableImage->qimage; midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } /* Image creation succeeds */ if (midpIncResourceCount(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { REPORT_ERROR(LC_LOWUI, "Error in updating " "resource limit for Immutable image"); } immutableImage->marker = 6; immutableImage->qpixmap = NULL; *newImmutableImagePtr = immutableImage; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; }
extern "C" void gxpport_decodeimmutable_from_selfidentifying (unsigned char* srcBuffer, int length, int* imgWidth, int* imgHeight, gxpport_image_native_handle *newImmutableImagePtr, gxutl_native_image_error_codes* creationErrorPtr) { MIDP_ERROR err; gxutl_image_format format; unsigned int w, h; err = gxutl_image_get_info(srcBuffer, (unsigned int)length, &format, &w, &h); if (err != MIDP_ERROR_NONE || format == GXUTL_IMAGE_FORMAT_UNSUPPORTED) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_UNSUPPORTED_FORMAT_ERROR; return; } /* Check resource limit */ int rscSize = ImgRegionRscSize(NULL, w, h); if (midpCheckResourceLimit(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { /* Exceed Resource limit */ *creationErrorPtr = GXUTL_NATIVE_IMAGE_RESOURCE_LIMIT; return; } bool loadResult = FALSE; _Platform_ImmutableImage* immutableImage = (_Platform_ImmutableImage*)midpMalloc(sizeof(_Platform_ImmutableImage)); if (immutableImage == NULL) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } switch(format) { case GXUTL_IMAGE_FORMAT_JPEG: case GXUTL_IMAGE_FORMAT_PNG: immutableImage->qimage = new QImage(); if (NULL == immutableImage->qimage) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } if (immutableImage->qimage->loadFromData((uchar*)srcBuffer, (unsigned int)length)) { *imgWidth = immutableImage->qimage->width(); *imgHeight = immutableImage->qimage->height(); if ((0 == *imgWidth) || (0 == *imgHeight)) { delete immutableImage->qimage; } else { immutableImage->marker = 4; loadResult = TRUE; } } else { delete immutableImage->qimage; immutableImage->qimage = NULL; } break; case GXUTL_IMAGE_FORMAT_RAW: loadResult = load_raw(&immutableImage->qimage, (gxutl_image_buffer_raw*)srcBuffer, (unsigned int)length, KNI_FALSE, imgWidth, imgHeight); immutableImage->marker = 5; break; default: /* Shouldn't be here */ *creationErrorPtr = GXUTL_NATIVE_IMAGE_UNSUPPORTED_FORMAT_ERROR; return; } if (!loadResult) { midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_DECODING_ERROR; return; } /* Image creation succeeds */ if (midpIncResourceCount(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { REPORT_ERROR(LC_LOWUI, "Error in updating resource limit" " for Immutable image"); } immutableImage->qpixmap = NULL; *newImmutableImagePtr = immutableImage; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; }
extern "C" void gxpport_createimmutable_from_mutableregion (gxpport_mutableimage_native_handle srcMutableImagePtr, int src_x, int src_y, int src_width, int src_height, int transform, gxpport_image_native_handle* newImmutableImagePtr, gxutl_native_image_error_codes* creationErrorPtr) { QPixmap *srcqpixmap = gxpportqt_get_mutableimage_pixmap(srcMutableImagePtr); int rscSize = ImgRegionRscSize(srcqpixmap, src_width, src_height); /* Check resource limit */ if (midpCheckResourceLimit(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { /* Exceed Resource limit */ *creationErrorPtr = GXUTL_NATIVE_IMAGE_RESOURCE_LIMIT; return; } QImage srcQImage = srcqpixmap->convertToImage(); if (srcQImage.isNull()) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } _Platform_ImmutableImage* immutableImage = (_Platform_ImmutableImage*)midpMalloc(sizeof(_Platform_ImmutableImage)); if (immutableImage == NULL) { *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } immutableImage->qimage = new QImage(srcQImage.copy(src_x, src_y, src_width, src_height)); if (NULL == immutableImage->qimage) { midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } if (immutableImage->qimage->isNull()) { delete immutableImage->qimage; midpFree(immutableImage); *creationErrorPtr = GXUTL_NATIVE_IMAGE_OUT_OF_MEMORY_ERROR; return; } if (transform != 0) { transform_image(immutableImage->qimage, transform); } /* Copy succeeds */ if (midpIncResourceCount(RSC_TYPE_IMAGE_IMMUT, rscSize) == 0) { REPORT_ERROR(LC_LOWUI, "Error in updating resource limit" " for Immutable image"); } immutableImage->marker = 3; immutableImage->qpixmap = NULL; *newImmutableImagePtr = immutableImage; *creationErrorPtr = GXUTL_NATIVE_IMAGE_NO_ERROR; }
/** * Opens a TCP connection to a server. * <p> * Java declaration: * <pre> * open([BI)V * </pre> * * @param ipBytes Byte array that represents a raw IP address * @param port TCP port at host * * @return a native handle to the network connection. */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_io_j2me_socket_Protocol_open0(void) { int port; void *pcslHandle = INVALID_HANDLE; int status; void* context = NULL; MidpReentryData* info; port = (int)KNI_GetParameterAsInt(2); KNI_StartHandles(2); KNI_DeclareHandle(thisObject); KNI_DeclareHandle(bufferObject); KNI_GetThisPointer(thisObject); KNI_GetParameterAsObject(1, bufferObject); info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ getMidpSocketProtocolPtr(thisObject)->handle = (jint)INVALID_HANDLE; /** * Verify that the resource is available well within limit as per * the policy in ResourceLimiter */ if (midpCheckResourceLimit(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit exceeded for TCP client sockets"); KNI_ThrowNew(midpIOException, "Resource limit exceeded for TCP client sockets"); } else { SNI_BEGIN_RAW_POINTERS; status = pcsl_socket_open_start( (unsigned char*)JavaByteArray(bufferObject), port, &pcslHandle, &context); SNI_END_RAW_POINTERS; if (status == PCSL_NET_SUCCESS) { getMidpSocketProtocolPtr(thisObject)->handle = (jint)pcslHandle; if (midpIncResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } } else if (status == PCSL_NET_IOERROR) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IOError in socket::open = %d\n", (int)pcsl_network_error(pcslHandle)); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpIOException, gKNIBuffer); } else if (status == PCSL_NET_CONNECTION_NOTFOUND) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "ConnectionNotFound error in socket::open :" " error = %d\n", (int)pcsl_network_error(pcslHandle)); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpConnectionNotFoundException, gKNIBuffer); } else if (status == PCSL_NET_WOULDBLOCK) { INC_NETWORK_INDICATOR; getMidpSocketProtocolPtr(thisObject)->handle = (jint)pcslHandle; if (midpIncResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } REPORT_INFO1(LC_PROTOCOL, " handle = %d\n", pcslHandle); midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle, context); } else { REPORT_INFO(LC_PROTOCOL, "Unknown error during socket::open"); KNI_ThrowNew(midpIOException, NULL); } } } else { /* Reinvocation after unblocking the thread */ pcslHandle = (void *) info->descriptor; context = (void *)info->status; if (getMidpSocketProtocolPtr(thisObject)->handle != (jint)pcslHandle) { REPORT_CRIT2(LC_PROTOCOL, "socket::open Handles mismatched 0x%x != 0x%x\n", pcslHandle, getMidpSocketProtocolPtr(thisObject)->handle); } status = pcsl_socket_open_finish(pcslHandle, context); if (status == PCSL_NET_SUCCESS) { DEC_NETWORK_INDICATOR; } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle, context); } else { DEC_NETWORK_INDICATOR; getMidpSocketProtocolPtr(thisObject)->handle = (jint)INVALID_HANDLE; if (midpDecResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); } midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "error %d in socket::open", (int)pcsl_network_error(pcslHandle)); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpConnectionNotFoundException, gKNIBuffer); } } KNI_EndHandles(); KNI_ReturnVoid(); }
/** * Opens a datagram connection on the given port. * <p> * Java declaration: * <pre> * open0(I[B)V * </pre> * * @param port port to listen on, or 0 to have one selected * @param suiteId the ID of the current MIDlet suite */ KNIEXPORT KNI_RETURNTYPE_VOID Java_com_sun_midp_io_j2me_datagram_Protocol_open0(void) { int port; SuiteIdType suiteId; jboolean tryOpen = KNI_TRUE; KNI_StartHandles(1); KNI_DeclareHandle(thisObject); KNI_GetThisPointer(thisObject); port = (int)KNI_GetParameterAsInt(1); suiteId = KNI_GetParameterAsInt(2); if (getMidpDatagramProtocolPtr(thisObject)->nativeHandle != (jint)INVALID_HANDLE) { KNI_ThrowNew(midpIOException, "already open"); tryOpen = KNI_FALSE; } if (tryOpen) { int pushReturn; pushReturn = pushcheckout("datagram", port, (char*)midp_suiteid2chars(suiteId)); /* * pushcheckout() returns -1 if the handle wasn't found, -2 if it's * already in use by another suite, otherwise a valid checked-out * handle. */ if (pushReturn == -1) { /* leave tryOpen == KNI_TRUE and try again below */ } else if (pushReturn == -2) { KNI_ThrowNew(midpIOException, "already in use"); tryOpen = KNI_FALSE; } else { /* IMPL NOTE: need to do resource accounting for this case */ getMidpDatagramProtocolPtr(thisObject)->nativeHandle = (jint)pushReturn; tryOpen = KNI_FALSE; } } if (tryOpen) { if (midpCheckResourceLimit(RSC_TYPE_UDP, 1) == 0) { KNI_ThrowNew(midpIOException, "resource limit exceeded"); } else { MidpReentryData* info; int status; void *socketHandle; void *context; info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* initial invocation */ INC_NETWORK_INDICATOR; status = pcsl_datagram_open_start(port, &socketHandle, &context); } else { /* reinvocation */ socketHandle = (void *)info->descriptor; context = info->pResult; status = pcsl_datagram_open_finish(socketHandle, context); } if (status == PCSL_NET_SUCCESS) { if (midpIncResourceCount(RSC_TYPE_UDP, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "Datagrams: resource limit update error"); } getMidpDatagramProtocolPtr(thisObject)->nativeHandle = (jint)socketHandle; DEC_NETWORK_INDICATOR; } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)socketHandle, context); } else { /* status == PCSL_NET_IOERROR */ midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "error code %d", pcsl_network_error(socketHandle)); REPORT_INFO1(LC_PROTOCOL, "datagram::open0 %s", gKNIBuffer); KNI_ThrowNew(midpIOException, gKNIBuffer); DEC_NETWORK_INDICATOR; } } } KNI_EndHandles(); KNI_ReturnVoid(); }