示例#1
0
JNIEXPORT void JNICALL
Java_java_util_zip_Deflater_setInputImpl (JNIEnv * env, jobject recv,
					  jbyteArray buf, jint off, jint len,
					  jlong handle)
{
  PORT_ACCESS_FROM_ENV (env);

  jbyte *in;
  JCLZipStream *stream;

  stream = (JCLZipStream *) ((IDATA) handle);
  if (stream->inaddr != NULL)	/*Input has already been provided, free the old buffer */
    jclmem_free_memory (env, stream->inaddr);
  stream->inaddr = jclmem_allocate_memory (env, len);
  if (stream->inaddr == NULL)
    {
      throwNewOutOfMemoryError (env, "");
      return;
    }
  in = ((*env)->GetPrimitiveArrayCritical (env, buf, 0));
  if (in == NULL) {
    throwNewOutOfMemoryError(env, "");
    return;
  }
  memcpy (stream->inaddr, (in + off), len);
  ((*env)->ReleasePrimitiveArrayCritical (env, buf, in, JNI_ABORT));
  stream->stream->next_in = (Bytef *) stream->inaddr;
  stream->stream->avail_in = len;

  return;
}
示例#2
0
void * sieb_malloc (JNIEnv * env, size_t byteCnt) {
    void * adr = malloc(byteCnt);
    if (adr == 0) {
         if (byteCnt == 0)
             throwNewOutOfMemoryError(env, "sieb_malloc(0) NOT ALLOWED");
         else
             throwNewOutOfMemoryError(env, "sieb_malloc");
    }
    return adr;
}
示例#3
0
JNIEXPORT jint JNICALL
Java_java_util_zip_Deflater_deflateImpl (JNIEnv * env, jobject recv,
					 jbyteArray buf, int off, int len,
					 jlong handle, int flushParm)
{
  jbyte *out;
  JCLZipStream *stream;
  jint err = 0;
  jint sin, sout, inBytes = 0;

  /* We need to get the number of bytes already read */
  inBytes =
    ((*env)->
     GetIntField (env, recv,
		  JCL_CACHE_GET (env, FID_java_util_zip_Deflater_inRead)));

  stream = (JCLZipStream *) ((IDATA) handle);
  stream->stream->avail_out = len;
  sin = stream->stream->total_in;
  sout = stream->stream->total_out;
  out = ((*env)->GetPrimitiveArrayCritical (env, buf, 0));
  if (out == NULL) {
    throwNewOutOfMemoryError(env, "");
    return -1;
  }
  stream->stream->next_out = (Bytef *) out + off;
  err = deflate (stream->stream, flushParm);
  ((*env)->ReleasePrimitiveArrayCritical (env, buf, out, 0));
  if (err != Z_OK) {
    if (err == Z_MEM_ERROR) {
      throwNewOutOfMemoryError(env, "");
      return 0;
    }
    if (err == Z_STREAM_END)
      {
        ((*env)->
         SetBooleanField (env, recv,
                          JCL_CACHE_GET (env,
                                         FID_java_util_zip_Deflater_finished),
                          JNI_TRUE));
        return stream->stream->total_out - sout;
      }
  }
  if (flushParm != Z_FINISH)
    {
      /* Need to update the number of input bytes read. */
      ((*env)->
       SetIntField (env, recv,
                    JCL_CACHE_GET (env, FID_java_util_zip_Deflater_inRead),
                    (jint) stream->stream->total_in - sin + inBytes));
    }
  return stream->stream->total_out - sout;
}
示例#4
0
/* Create a new stream . This stream cannot be used until it has been properly initialized. */
JNIEXPORT jlong JNICALL
Java_java_util_zip_Deflater_createStream (JNIEnv * env, jobject recv,
					  jint level, jint strategy,
					  jboolean noHeader)
{  
  PORT_ACCESS_FROM_ENV (env);
  JCLZipStream *jstream;
  z_stream *stream;
  int err = 0;
  int wbits = 15;		/*Use MAX for fastest */
#ifdef HY_ZIP_API
  VMI_ACCESS_FROM_ENV (env);
  VMIZipFunctionTable *zipFuncs;
  zipFuncs = (*VMI)->GetZipFunctions(VMI);
#endif

  /*Allocate mem for wrapped struct */
  jstream = jclmem_allocate_memory (env, sizeof (JCLZipStream));
  if (jstream == NULL)
    {
      throwNewOutOfMemoryError (env, "");
      return -1;
    }
  /*Allocate the z_stream */
  stream = jclmem_allocate_memory (env, sizeof (z_stream));
  if (stream == NULL)
    {
      jclmem_free_memory (env, jstream);
      throwNewOutOfMemoryError (env, "");
      return -1;
    }
  stream->opaque = (void *) privatePortLibrary;
  stream->zalloc = zalloc;
  stream->zfree = zfree;
  jstream->stream = stream;
  jstream->dict = NULL;
  jstream->inaddr = NULL;

  /*Unable to find official doc that this is the way to avoid zlib header use. However doc in zipsup.c claims it is so */
  if (noHeader)
    wbits = wbits / -1;
  err = deflateInit2 (stream, level, Z_DEFLATED,	/*Only supported ZLIB method */
		      wbits,	/*Window bits to use. 15 is fastest but consumes the most memory */
		      9,	/*Memory allocation for internal compression state. 9 uses the most. */
		      strategy);
  if (err != Z_OK) {
    THROW_ZIP_EXCEPTION(env, err, IllegalArgumentException);
    return -1;
  }

  return (jlong) ((IDATA) jstream);
}
示例#5
0
JNIEXPORT void JNICALL
Java_java_util_zip_Deflater_setDictionaryImpl (JNIEnv * env, jobject recv,
					       jbyteArray dict, int off,
					       int len, jlong handle)
{
  PORT_ACCESS_FROM_ENV (env);
  int err = 0;
  char *dBytes;
  JCLZipStream *stream = (JCLZipStream *) ((IDATA) handle);

  dBytes = jclmem_allocate_memory (env, len);
  if (dBytes == NULL)
    {
      throwNewOutOfMemoryError (env, "");
      return;
    }
  (*env)->GetByteArrayRegion (env, dict, off, len, (jbyte *) dBytes);
  err = deflateSetDictionary (stream->stream, (Bytef *) dBytes, len);
  if (err != Z_OK)
    {
      jclmem_free_memory (env, dBytes);
      THROW_ZIP_EXCEPTION(env, err, IllegalArgumentException);
      return;
    }
  stream->dict = (U_8*) dBytes;
}
示例#6
0
文件: OSMemory.c 项目: freeVM/freeVM
JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSMemory_malloc
  (JNIEnv * env, jobject thiz, jlong size)
{
  PORT_ACCESS_FROM_ENV (env);

  void *address = hymem_allocate_memory ((UDATA) size);

  if (address == NULL)
    {
      throwNewOutOfMemoryError(env, "Insufficient memory available.");
    }

  return (jlong) ((IDATA) address);
}
JNIEXPORT jlong JNICALL
Java_java_util_zip_CRC32_updateImpl (JNIEnv * env, jobject recv,
                                     jbyteArray buf, int off, int len,
                                     jlong crc)
{
  jbyte *b;
  jlong result;

  b = ((*env)->GetPrimitiveArrayCritical (env, buf, 0));
  if (b == NULL) {
    throwNewOutOfMemoryError(env, "");
    return -1;
  }
  result = crc32 ((uLong) crc, (Bytef *) (b + off), (uInt) len);
  ((*env)->ReleasePrimitiveArrayCritical (env, buf, b, JNI_ABORT));
  return result;
}
示例#8
0
/*
 * Class:     org_apache_harmony_x_imageio_plugins_jpeg_JPEGImageWriter
 * Method:    setIOS
 * Signature: (Ljavax/imageio/stream/ImageOutputStream;J)V
 */
JNIEXPORT void JNICALL 
Java_org_apache_harmony_x_imageio_plugins_jpeg_JPEGImageWriter_setIOS(JNIEnv *env, jobject encoder, jobject iosObj, jlong handle) {

    struct jpeg_compress_struct * cinfo = (struct jpeg_compress_struct *) (IDATA)handle;
    enc_client_data_ptr cdata = (enc_client_data_ptr) cinfo->client_data;

    if (cdata->ios != NULL) {
        (*env)->DeleteGlobalRef(env, cdata->ios);
        cdata->ios = NULL;
    }
    cdata->ios = (*env)->NewGlobalRef(env, iosObj);
    if (cdata->ios == NULL) {
        throwNewOutOfMemoryError(env, "Unable to allocate memory for IJG structures");
        return;
    }
    cinfo->dest->next_output_byte = cdata->jpeg_buffer;
    cinfo->dest->free_in_buffer = MAX_BUFFER;
}
示例#9
0
/*
 * Class:     org_apache_harmony_x_imageio_plugins_jpeg_JPEGImageWriter
 * Method:    encode
 * Signature: ([BIIIIIIIZ[[IJ)Z
 */
JNIEXPORT jboolean JNICALL 
Java_org_apache_harmony_x_imageio_plugins_jpeg_JPEGImageWriter_encode(JNIEnv *env, 
    jobject callerObj, jbyteArray arr, jint srcWidth, jint width, jint height, jint deltaX, 
    jint in_cs, jint out_cs, jint numBands, jboolean progressive, jobjectArray dqts, jlong handle) 
{

    JSAMPROW row_pointer;
    struct jpeg_compress_struct * cinfo;
    enc_error_mgr_ptr err_mgr;
    
    int i, j;
    int cur_scanline;
    unsigned char * native_buffer;
    unsigned char * rowPtr;

    jboolean optimizeHuffman = FALSE;

    row_pointer = (JSAMPROW) malloc(width * numBands);

    if (!row_pointer) {
        throwNewOutOfMemoryError(env, "Unable to allocate memory for IJG structures");
        return FALSE;
    }

    
    cinfo = (struct jpeg_compress_struct *) (IDATA)handle;
    err_mgr = (enc_error_mgr_ptr) cinfo->err;

    if (setjmp(err_mgr->jmp_buffer)) {
        if (!(*env)->ExceptionOccurred(env)) {
            char msg_buffer[JMSG_LENGTH_MAX];
            cinfo->err->format_message((j_common_ptr)cinfo, msg_buffer);
            throwNewExceptionByName(env, "javax/imageio/IIOException",
                                    msg_buffer);
        }
        if (row_pointer) {
            free(row_pointer);
        }
        return FALSE;
    }

    cinfo->image_width = width;
    cinfo->image_height = height;
    cinfo->input_components = numBands;
    cinfo->in_color_space = in_cs;
    
    jpeg_set_defaults(cinfo);
    jpeg_set_colorspace(cinfo, out_cs);
    cinfo->optimize_coding = optimizeHuffman;

    //-- TRUE - for pure abbrivated images (wo tables) creation 
    //-- if you want to write some tables set "sent_table = FALSE" after 
    //-- this call for the table to be emitted. 
    //jpeg_suppress_tables(&cinfo, TRUE);
    jpeg_suppress_tables(cinfo, FALSE);

    setupDQTs(env, cinfo, dqts);

    //-- only simple progression sequence
    if (progressive) {
        jpeg_simple_progression(cinfo);
    }

    //-- TRUE forces all "sent_table = FALSE" so all tables will be written
    jpeg_start_compress(cinfo, TRUE);

    //-- use this for tables-only files and abbrivated images.
    //-- If using jpeg_suppress_tables(&cinfo, TRUE):
    //-- Only DQT sent_table = FALSE right now -> so DQT will be written but not DHT.
    //-- uncomment when custom huffman tables be used.
    //jpeg_start_compress(&cinfo, FALSE);

    cur_scanline = 0;

    while (cinfo->next_scanline < cinfo->image_height) {
       (*env)->CallVoidMethod(env, callerObj, getScanlineID, cur_scanline);

       // checking for an exception in the java method
       if ((*env)->ExceptionOccurred(env)) {
           //c_struct->exception_in_callback = TRUE;
           cinfo->err->error_exit((j_common_ptr) cinfo);
       }

       native_buffer = (JOCTET*) (*env)->GetPrimitiveArrayCritical(env, arr, NULL);

       // subsampling and copying to internal array
       rowPtr = row_pointer;
       for (i = 0; i < srcWidth * numBands; i += numBands * deltaX) {
           for (j = 0; j < numBands; j++) {
               *rowPtr++ = native_buffer[i + j];
           }
       }
       (*env)->ReleasePrimitiveArrayCritical(env, arr, native_buffer, 0);

       jpeg_write_scanlines(cinfo, &row_pointer, 1);

       cur_scanline++;
    }

    jpeg_finish_compress(cinfo);
    free(row_pointer);

    return TRUE;
}
示例#10
0
//GLOBAL(CompressStruct*) ios_create_compress(JNIEnv *env) {
GLOBAL(struct jpeg_compress_struct *) ios_create_compress(JNIEnv *env) {

    struct jpeg_compress_struct * cinfo;
    struct jpeg_destination_mgr * dest_mgr;

    enc_error_mgr_ptr err_mgr;
    enc_client_data_ptr client_data;

    //-- create compress struct
    cinfo = malloc(sizeof(struct jpeg_compress_struct));
    if (!cinfo) {
        throwNewOutOfMemoryError(env, "Unable to allocate memory for IJG structures");
        return 0;
    }

    //-- create error manager
    err_mgr = malloc(sizeof(enc_error_mgr));
    if (!err_mgr) {
        free(cinfo);
        throwNewOutOfMemoryError(env, "Unable to allocate memory for IJG structures");
        return 0;
    }
    

    cinfo->err = jpeg_std_error(&(err_mgr->base));
    err_mgr->base.error_exit = ios_jpeg_error_exit;


    //-- TODO setjmp before every call to libjpeg
    jpeg_create_compress(cinfo);

    dest_mgr = malloc(sizeof(struct jpeg_destination_mgr));
    if (!dest_mgr) {
        free(cinfo);
        free(err_mgr);
        throwNewOutOfMemoryError(env, "Unable to allocate memory for IJG structures");
        return 0;
    }
    cinfo->dest = dest_mgr;

    client_data = malloc(sizeof(enc_client_data));

    if (!client_data) {
        free(cinfo);
        free(err_mgr);
        free(dest_mgr);
        throwNewOutOfMemoryError(env, "Unable to allocate memory for IJG structures");
        return 0;
    }
    cinfo->client_data = client_data;
    client_data->ios = NULL;
    client_data->jpeg_buffer = malloc(MAX_BUFFER);
    if (!client_data->jpeg_buffer) {
        free(cinfo);
        free(err_mgr);
        free(dest_mgr);
        free(client_data);
        throwNewOutOfMemoryError(env, "Unable to allocate memory for IJG structures");
        return 0;
    }
    dest_mgr->next_output_byte = client_data->jpeg_buffer;
    dest_mgr->free_in_buffer = MAX_BUFFER;

    dest_mgr->init_destination = ios_init_destination;
    dest_mgr->empty_output_buffer = ios_empty_output_buffer;
    dest_mgr->term_destination = ios_term_destination;
    
    return cinfo;
}
示例#11
0
文件: zip.c 项目: freeVM/freeVM
JNIEXPORT jbyteArray JNICALL
Java_java_util_zip_ZipFile_inflateEntryImpl2 (JNIEnv * env, jobject recv,
					                          jlong descriptor,
                                              jstring entryName)
{
  PORT_ACCESS_FROM_ENV (env);

  I_32 retval;
  HyZipFile *zipFile;
  HyZipEntry zipEntry;
  const char *entryCopy;
  jbyteArray buf;
  JCLZipFile *jclZipFile = (JCLZipFile *) (IDATA) descriptor;

  /* Build the zipFile */
  if (jclZipFile == (void *) -1)
    {
      throwNewIllegalStateException (env, "");
      return NULL;
    }
  zipFile = &(jclZipFile->hyZipFile);
  entryCopy = (*env)->GetStringUTFChars (env, entryName, NULL);
  if (entryCopy == NULL)
    return NULL;

  zip_initZipEntry (privatePortLibrary, &zipEntry);
  retval =
    zip_getZipEntry (privatePortLibrary, zipFile, &zipEntry, entryCopy, TRUE);
  (*env)->ReleaseStringUTFChars (env, entryName, entryCopy);
  if (retval)
    {
      zip_freeZipEntry (privatePortLibrary, &zipEntry);
      if (retval == ZIP_ERR_OUT_OF_MEMORY)
        throwNewOutOfMemoryError (env, "");
      return NULL;
    }

  buf = (*env)->NewByteArray (env, zipEntry.uncompressedSize);
  if (!buf)
    {
      throwNewOutOfMemoryError (env, "");
      return NULL;
    }

  retval =
    zip_getZipEntryData (privatePortLibrary, zipFile, &zipEntry, NULL,
                         zipEntry.uncompressedSize);
  if (retval == 0)
    (*env)->SetByteArrayRegion (env, buf, 0, zipEntry.uncompressedSize,
                                zipEntry.data);
  zip_freeZipEntry (privatePortLibrary, &zipEntry);
  if (!retval)
    return buf;

  if (retval == ZIP_ERR_OUT_OF_MEMORY)
    throwNewOutOfMemoryError (env, "");
  else
    throwJavaZIOException (env, "");

  return NULL;
}
示例#12
0
文件: process.c 项目: freeVM/freeVM
/**
  * Create a System Process with the specified
  * environment and arguments 
  */
JNIEXPORT jlongArray JNICALL
Java_org_apache_harmony_luni_internal_process_SystemProcess_createImpl (JNIEnv * env, jclass clazz,
            jobject recv,
            jobjectArray arg1,
            jobjectArray arg2,
            jbyteArray dir)
{
  jbyteArray envString;
  jlongArray pVals = NULL;
  jlong npVals[4];
  char *envArray[256];
  char *command[256];
  int i, retVal;
  IDATA pHandle, inHandle, outHandle, errHandle;
  int envLength, commandLineLength, len;
  char *workingDir = NULL;
  PORT_ACCESS_FROM_ENV (env);

  /* validate sizes */
  commandLineLength = (*env)->GetArrayLength (env, arg1);
  envLength = (*env)->GetArrayLength (env, arg2);
  if (commandLineLength >= 255)
    {
      throwJavaIoIOException(env, "Too many arguments");
      return NULL;
    }
  if (envLength >= 255)
    {
      throwJavaIoIOException(env, "Too many environment arguments");
      return NULL;
    }

  memset (command, 0, sizeof (command));
  memset (envArray, 0, sizeof (envArray));

  /* Get the command string and arguments */
  /* convert java.lang.String into C char* */
  for (i = commandLineLength; --i >= 0;)
    {
      jbyteArray element = (*env)->GetObjectArrayElement (env, arg1, i);
      len = (*env)->GetArrayLength (env, element);
      command[i] = jclmem_allocate_memory (env, len + 1);
      if (command[i] == NULL)
        {
          throwNewOutOfMemoryError (env, "");
          goto failed;
        }
      (*env)->GetByteArrayRegion (env, element, 0, len, (jbyte *)command[i]);
      command[i][len] = 0;
    }
  if (envLength)
    for (i = 0; i < envLength; i++)
      {
        envString = (*env)->GetObjectArrayElement (env, arg2, i);
        len = (*env)->GetArrayLength (env, envString);
        envArray[i] = jclmem_allocate_memory (env, len + 1);
        if (envArray[i] == NULL)
          {
            throwNewOutOfMemoryError (env, "");
            goto failed;
          }
        (*env)->GetByteArrayRegion (env, envString, 0, len, (jbyte *)envArray[i]);
        envArray[i][len] = 0;
      }
  /* NULL terminate for UNIX (does work on windows too; in fact, it doesn't care) */
  command[commandLineLength] = NULL;
  envArray[envLength] = NULL;

  if (dir != NULL)
    {
      jsize dirLength = (*env)->GetArrayLength (env, dir);

      workingDir = jclmem_allocate_memory (env, dirLength + 1);
      if (workingDir)
        {
          (*env)->GetByteArrayRegion (env, dir, 0, dirLength,
            (jbyte *) workingDir);
          workingDir[dirLength] = '\0';
        }
    }

  /*
   *  now call execProgram.  Any non-zero return code 
   *  indicates some kind of failure
   */
   
  retVal = execProgram (env, recv,
      command, commandLineLength, envArray, envLength,
      workingDir, &pHandle, &inHandle, &outHandle,
      &errHandle);

  if (workingDir)
    {
      jclmem_free_memory (env, workingDir);
    }

  if (retVal)
    {
        char errMsg[256];

        /* Failed to exec program */
        
        switch(retVal) {
        case 1001 :
            sprintf(errMsg, "Unable to start program : %s", "fork() failed with errno = EOMEM");
            break;
        case 1002 : 
            sprintf(errMsg, "Unable to start program : %s", "fork() failed with errno = EAGAIN");
            break;
        default:
            sprintf(errMsg, "Unable to start program : %s", "unknown");
            break;
        }

        throwJavaIoIOException(env, errMsg);
        goto failed;
    }

  pVals = (*env)->NewLongArray (env, 4);

  if (pVals)
    {
      npVals[0] = (jlong) pHandle;
      npVals[1] = (jlong) inHandle;
      npVals[2] = (jlong) outHandle;
      npVals[3] = (jlong) errHandle;
      (*env)->SetLongArrayRegion (env, pVals, 0, 4, (jlong *) (&npVals));
    }

failed:

  for (i = 0; i < envLength; i++)
    {
      if (envArray[i])
        jclmem_free_memory (env, envArray[i]);
    }
  for (i = commandLineLength; --i >= 0;)
    {
      if (command[i])
        jclmem_free_memory (env, command[i]);
    }

  return pVals;
}