Esempio n. 1
0
JNIEXPORT void JNICALL Java_org_clearsilver_HDF__1setSymLink(
    JNIEnv *env, jclass objClass,
    jint hdf_obj_ptr, jstring j_hdf_name_src, jstring j_hdf_name_dest) {
  HDF *hdf = (HDF *)hdf_obj_ptr;
  NEOERR *err;
  const char *hdf_name_src;
  const char *hdf_name_dest;

  if (!j_hdf_name_src) {
    throwNullPointerException(env, "hdf_name_src argument was null");
    return;
  }
  hdf_name_src = (*env)->GetStringUTFChars(env, j_hdf_name_src, 0);

  if (!j_hdf_name_dest) {
    throwNullPointerException(env, "hdf_name_dest argument was null");
    return;
  }
  hdf_name_dest = (*env)->GetStringUTFChars(env, j_hdf_name_dest, 0);

  err = hdf_set_symlink(hdf, hdf_name_src, hdf_name_dest);

  (*env)->ReleaseStringUTFChars(env, j_hdf_name_src, hdf_name_src);
  (*env)->ReleaseStringUTFChars(env, j_hdf_name_dest, hdf_name_dest);

  if (err != STATUS_OK) {
    // Throw an exception
    jNeoErr(env, err);
  }
}
Esempio n. 2
0
JNIEXPORT void JNICALL Java_org_clearsilver_HDF__1setValue(
    JNIEnv *env, jclass objClass,
    jint hdf_obj_ptr, jstring j_hdfname, jstring j_value) {
  HDF *hdf = (HDF *)hdf_obj_ptr;
  NEOERR *err;
  const char *hdfname;
  const char *value;

  if (!j_hdfname) {
    throwNullPointerException(env, "hdfname argument was null");
    return;
  }
  hdfname = (*env)->GetStringUTFChars(env, j_hdfname, 0);
  if (j_value) {
    value = (*env)->GetStringUTFChars(env, j_value, 0);
  } else {
    value = NULL;
  }
  err = hdf_set_value(hdf, hdfname, value);

  (*env)->ReleaseStringUTFChars(env, j_hdfname, hdfname);
  if (value) {
    (*env)->ReleaseStringUTFChars(env, j_value, value);
  }

  if (err != STATUS_OK) {
    // Throw an exception
    jNeoErr(env, err);
  }
}
Esempio n. 3
0
JNIEXPORT jstring JNICALL Java_org_clearsilver_HDF__1getValue(
    JNIEnv *env, jclass objClass, jint hdf_obj_ptr, jstring j_hdfname,
    jstring j_default_value) {
  HDF *hdf = (HDF *)hdf_obj_ptr;
  const char *r;
  const char *hdfname;
  const char *default_value;
  jstring retval;

  if (!j_hdfname) {
    throwNullPointerException(env, "hdfname argument was null");
    return 0;
  }
  hdfname = (*env)->GetStringUTFChars(env,j_hdfname,0);
  if (!j_default_value) {
    default_value = NULL;
  } else {
    default_value = (*env)->GetStringUTFChars(env, j_default_value, 0);
  }

  r = hdf_get_value(hdf, hdfname, default_value);

  (*env)->ReleaseStringUTFChars(env, j_hdfname, hdfname);
  retval = (r ? (*env)->NewStringUTF(env, r) : 0);
  if (default_value) {
    (*env)->ReleaseStringUTFChars(env, j_default_value, default_value);
  }
  return retval;
}
Esempio n. 4
0
const methodInClass* getVirtualMethodEntry(u2 cp_index) {
	BEGIN;
	int linkId;

	const memberReference* mref = lookupMethodReference(context.classIndex, cp_index);
	linkId = mref->linkId;
	u2 argCount = mref->numberOfArguments;

	// Find the class (-id) on which the referenced method shall be invoked:
	stackable st;

	getOperandRelativeToStackPointer((s1) (-argCount), &st);

	VALIDATE_TYPE(st.type, OBJECTREF);

	jobject jref = st.operand.jref;
	const methodInClass* mic;
	if (jref == 0) {
		mic = NULL;
		throwNullPointerException();
	} else {
		mic = getVirtualMethodEntryByLinkId(jref, linkId);
	}

	END;

	return mic;
}
jobjectArray InetAddress_getallbyname(JNIEnv* env, jobject obj,
                                      jstring javaName,
                                      jboolean preferIPv4Stack)
{
    if (javaName == NULL) {
        throwNullPointerException(env);
        return NULL;
    }

    const char* name = env->GetStringUTFChars(javaName, NULL);
    jobjectArray out = NULL;

    char useAdbNetworkingProperty[PROPERTY_VALUE_MAX];
    char adbConnected[PROPERTY_VALUE_MAX];
    property_get("android.net.use-adb-networking",
            useAdbNetworkingProperty, "");
    property_get("adb.connected",
            adbConnected, "");

    // Any non-empty string value for use-adb-networking is considered "set"
    if ((strlen(useAdbNetworkingProperty) > 0)
            && (strlen(adbConnected) > 0) ) {
        out = getAllByNameUsingAdb(env, name);
    } else {
        out = getAllByNameUsingDns(env, name, preferIPv4Stack);
    }

    if (!out) {
        LOGI("Unknown host %s, throwing UnknownHostException", name);
        jniThrowException(env, "java/net/UnknownHostException", name);
    }
    env->ReleaseStringUTFChars(javaName, name);
    return out;
}
Esempio n. 6
0
w_int RandomAccessFile_readIntoBuffer (JNIEnv *env, w_instance thisRAF, w_instance buffer, w_int offset, w_int length) {

  vfs_FILE    *file;
  w_int       result;
  w_sbyte     *bytes;
  w_sbyte     *data;

  if ((offset < 0) || (offset > instance2Array_length(buffer) - length)) {
    throwArrayIndexOutOfBoundsException(JNIEnv2w_thread(env));

    return -1;
  }

  bytes = instance2Array_byte(buffer);
  file = RAF2FILE(thisRAF);
  
  if(file == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
    result = 0;
  } else {
    data = bytes + offset;
    result = vfs_fread(data, 1, (w_word)length, file);
    if(result == 0) result = -1;
  }

  return result;
}
Esempio n. 7
0
void RandomAccessFile_setLength (JNIEnv *env, w_instance thisRAF, w_long newlen) {

  w_instance  fd_obj;
  vfs_FILE    *file;

  fd_obj = getReferenceField(thisRAF, F_RandomAccessFile_fd);
  file = getWotsitField(fd_obj, F_FileDescriptor_fd);
  
  if(file == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
  } else {
    char *pathname;
    w_long oldptr;

    pathname = getFDName(fd_obj);

    oldptr = vfs_ftell(file);

    if(vfs_truncate(pathname, newlen) == -1) {
      throwIOException(JNIEnv2w_thread(env));
    }

    freeFDName(pathname);

    if(newlen < oldptr && vfs_fseek(file, newlen, SEEK_SET) == -1) {
      throwIOException(JNIEnv2w_thread(env));
    }
  }
}
Esempio n. 8
0
void *findFunction(JNIEnv *env, void *hModule, char *functionName) {
    void *fAddress = dlsym(hModule, functionName);
    if (fAddress == NULL) {
        char errorMessage[256];
        snprintf(errorMessage, sizeof(errorMessage), "Symbol not found: %s", functionName);
        throwNullPointerException(env, errorMessage);
        return NULL;
    }
    return fAddress;
}
Esempio n. 9
0
void RandomAccessFile_seek (JNIEnv *env, w_instance thisRAF, w_long pos) {
  vfs_FILE    *file;

  file = RAF2FILE(thisRAF);
  
  if(file == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
  } else {
    if(vfs_fseek(file, (long)pos, SEEK_SET) == -1) {
      throwIOException(JNIEnv2w_thread(env));
    }
  }
}
/*
 * public static native void EVP_VerifyUpdate(int, byte[], int, int)
 */
static void NativeCrypto_EVP_VerifyUpdate(JNIEnv* env, jclass clazz, EVP_MD_CTX* ctx, jbyteArray buffer, jint offset, jint length) {
    // LOGI("NativeCrypto_EVP_VerifyUpdate %x, %x, %d, %d", ctx, buffer, offset, length);

    if (ctx == NULL || buffer == NULL) {
        throwNullPointerException(env);
        return;
    }

    jbyte* bufferBytes = env->GetByteArrayElements(buffer, NULL);
    EVP_VerifyUpdate(ctx, (unsigned char*) (bufferBytes + offset), length);
    env->ReleaseByteArrayElements(buffer, bufferBytes, JNI_ABORT);

    throwExceptionIfNecessary(env);
}
/*
 * public static native void EVP_DigestReset(int)
 */
static jint NativeCrypto_EVP_DigestBlockSize(JNIEnv* env, jclass clazz, EVP_MD_CTX* ctx) {
    // LOGI("NativeCrypto_EVP_DigestBlockSize");

    if (ctx == NULL) {
        throwNullPointerException(env);
        return -1;
    }

    int result = EVP_MD_CTX_block_size(ctx);

    throwExceptionIfNecessary(env);

    return result;
}
Esempio n. 12
0
void RandomAccessFile_write (JNIEnv *env, w_instance thisRAF, w_int oneByte) {
  vfs_FILE    *file;
  w_sbyte     minibuf = oneByte;

  file = RAF2FILE(thisRAF);
  
  if(file == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
  } else {
    vfs_fseek(file, vfs_ftell(file), SEEK_SET);
    vfs_fwrite(&minibuf, 1, 1, file);
    vfs_fflush(file);
  }
}
Esempio n. 13
0
w_long RandomAccessFile_getFilePointer (JNIEnv *env, w_instance thisRAF) {
  vfs_FILE    *file;
  w_long       result = -1;

  file = RAF2FILE(thisRAF);
  
  if(file == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
    result = 0;
  } else {
    result = vfs_ftell(file);
  }

  return result;
}
Esempio n. 14
0
w_int RandomAccessFile_skipBytes (JNIEnv *env, w_instance thisRAF, w_int n) {
  w_thread thread = JNIEnv2w_thread(env);
  w_instance  fd_obj;
  vfs_FILE    *file;
  w_long       result = 0;
  w_long       prev_pos;

  fd_obj = getReferenceField(thisRAF, F_RandomAccessFile_fd);
  file = getWotsitField(fd_obj, F_FileDescriptor_fd);
  
  if (file == NULL) {
    throwNullPointerException(thread);
  } else {
    struct vfs_STAT statbuf;
    w_long size = 0;

    if(statFD(fd_obj, &statbuf)) {
      size = statbuf.st_size;
    }
    else {
      throwIOException(thread);
      return -1;
    }

    prev_pos = vfs_ftell(file);
    if (prev_pos < 0) {
      throwIOException(thread);
      return -1;
    }

    if(n > (size - prev_pos)) {
      n = size - prev_pos;
    }
     
    result = vfs_fseek(file, (long)n, SEEK_CUR);
    if (result < 0) {
      throwIOException(thread);
      return -1;
    }

    result = vfs_ftell(file) - prev_pos;
    if (result < 0) {
      throwIOException(thread);
    }
  }

  return result;
}
/*
 * public static native void EVP_VerifyFinal(int, byte[], int, int, int)
 */
static int NativeCrypto_EVP_VerifyFinal(JNIEnv* env, jclass clazz, EVP_MD_CTX* ctx, jbyteArray buffer, jint offset, jint length, EVP_PKEY* pkey) {
    // LOGI("NativeCrypto_EVP_VerifyFinal %x, %x, %d, %d %x", ctx, buffer, offset, length, pkey);

    if (ctx == NULL || buffer == NULL || pkey == NULL) {
        throwNullPointerException(env);
        return -1;
    }

    jbyte* bufferBytes = env->GetByteArrayElements(buffer, NULL);
    int result = EVP_VerifyFinal(ctx, (unsigned char*) (bufferBytes + offset), length, pkey);
    env->ReleaseByteArrayElements(buffer, bufferBytes, JNI_ABORT);

    throwExceptionIfNecessary(env);

    return result;
}
Esempio n. 16
0
w_int RandomAccessFile_read (JNIEnv *env, w_instance thisRAF) {
  vfs_FILE    *file;
  w_int       result = -1;
  w_sbyte     minibuf;

  file = RAF2FILE(thisRAF);
  
  if(file == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
  } else {
    if (vfs_fread(&minibuf, 1, 1, file) > 0) {
      result = minibuf;
    }
  }

  return result;
}
/*
 * public static native int EVP_DigestFinal(int, byte[], int)
 */
static jint NativeCrypto_EVP_DigestFinal(JNIEnv* env, jclass clazz, EVP_MD_CTX* ctx, jbyteArray hash, jint offset) {
    // LOGI("NativeCrypto_EVP_DigestFinal%x, %x, %d, %d", ctx, hash, offset);

    if (ctx == NULL || hash == NULL) {
        throwNullPointerException(env);
        return -1;
    }

    int result = -1;

    jbyte* hashBytes = env->GetByteArrayElements(hash, NULL);
    EVP_DigestFinal(ctx, (unsigned char*) (hashBytes + offset), (unsigned int*)&result);
    env->ReleaseByteArrayElements(hash, hashBytes, 0);

    throwExceptionIfNecessary(env);

    return result;
}
Esempio n. 18
0
JNIEXPORT jint JNICALL Java_org_clearsilver_HDF__1getIntValue(
    JNIEnv *env, jclass objClass, jint hdf_obj_ptr, jstring j_hdfname,
     jint default_value) {
  HDF *hdf = (HDF *)hdf_obj_ptr;
  int r;
  const char *hdfname;

  if (!j_hdfname) {
    throwNullPointerException(env, "hdfname argument was null");
    return 0;
  }

  hdfname = (*env)->GetStringUTFChars(env,j_hdfname, 0);

  r = hdf_get_int_value(hdf, hdfname, default_value);

  (*env)->ReleaseStringUTFChars(env,j_hdfname,hdfname);
  return r;
}
Esempio n. 19
0
void RandomAccessFile_close (JNIEnv *env, w_instance thisRAF) { 
  w_instance  fd_obj;
  vfs_FILE    *file;

  fd_obj = getReferenceField(thisRAF, F_RandomAccessFile_fd);

  if(fd_obj == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
  } else {
    
    file = getWotsitField(fd_obj, F_FileDescriptor_fd);
  
    if(file) {  
      vfs_fclose(file);
      clearWotsitField(fd_obj, F_FileDescriptor_fd);
    }
    
  }
  
}
/*
 * public static native void EVP_VerifyInit(int, java.lang.String)
 */
static void NativeCrypto_EVP_VerifyInit(JNIEnv* env, jclass clazz, EVP_MD_CTX* ctx, jstring algorithm) {
    // LOGI("NativeCrypto_EVP_VerifyInit");

    if (ctx == NULL || algorithm == NULL) {
        throwNullPointerException(env);
        return;
    }

    const char* algorithmChars = env->GetStringUTFChars(algorithm, NULL);

    const EVP_MD *digest = EVP_get_digestbynid(OBJ_txt2nid(algorithmChars));
    env->ReleaseStringUTFChars(algorithm, algorithmChars);

    if (digest == NULL) {
        throwRuntimeException(env, "Hash algorithm not found");
        return;
    }

    EVP_VerifyInit(ctx, digest);

    throwExceptionIfNecessary(env);
}
Esempio n. 21
0
void RandomAccessFile_writeFromBuffer (JNIEnv *env, w_instance thisRAF, w_instance buffer, w_int offset, w_int length) {
  vfs_FILE    *file;
  w_sbyte     *bytes;
  w_sbyte     *data;

  if ((offset < 0) || (offset > instance2Array_length(buffer) - length)) {
    throwArrayIndexOutOfBoundsException(JNIEnv2w_thread(env));

    return;
  }

  bytes = instance2Array_byte(buffer);
  file = RAF2FILE(thisRAF);
  
  if(file == NULL) {
    throwNullPointerException(JNIEnv2w_thread(env));
  } else {
    data = bytes + offset;
    vfs_fwrite(data, 1, (w_word)length, file);
    vfs_fflush(file);
  }
}
static jstring InetAddress_gethostbyaddr(JNIEnv* env, jobject obj,
                                         jbyteArray javaAddress)
{
    if (javaAddress == NULL) {
        throwNullPointerException(env);
        return NULL;
    }

    size_t addrlen = env->GetArrayLength(javaAddress);
    jbyte* rawAddress = env->GetByteArrayElements(javaAddress, NULL);
    if (rawAddress == NULL) {
        throwNullPointerException(env);
        return NULL;
    }

    // Convert the raw address bytes into a socket address structure.
    struct sockaddr_storage ss;
    struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
    size_t socklen;
    switch (addrlen) {
        case 4:
            socklen = sizeof(struct sockaddr_in);
            memset(sin, 0, sizeof(struct sockaddr_in));
            sin->sin_family = AF_INET;
            memcpy(&sin->sin_addr.s_addr, rawAddress, 4);
            break;
        case 16:
            socklen = sizeof(struct sockaddr_in6);
            memset(sin6, 0, sizeof(struct sockaddr_in6));
            sin6->sin6_family = AF_INET6;
            memcpy(&sin6->sin6_addr.s6_addr, rawAddress, 4);
            break;
        default:
            jniThrowException(env, "java/net/UnknownHostException",
                                   "Invalid address length");
            return NULL;
    }


    // Convert the socket address structure to an IP string for logging.
    int ret;
    char ipstr[INET6_ADDRSTRLEN];
    ret = getnameinfo((struct sockaddr *) &ss, socklen, ipstr, sizeof(ipstr),
                      NULL, 0, NI_NUMERICHOST);
    if (ret) {
        LOGE("gethostbyaddr: getnameinfo: %s", gai_strerror(ret));
        return NULL;
    }

    // Look up the IP address from the socket address structure.
    jstring result = NULL;
    char name[NI_MAXHOST];
    ret = getnameinfo((struct sockaddr *) &ss, socklen, name, sizeof(name),
                      NULL, 0, 0);
    if (ret == 0) {
        LOGI("gethostbyaddr: getnameinfo: %s = %s", ipstr, name);
        result = env->NewStringUTF(name);
    } else {
        LOGE("gethostbyaddr: getnameinfo: unknown host %s: %s", ipstr,
             gai_strerror(ret));
    }

    return result;
}