123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- /**
- * $Id$
- *
- * Copyright (C) 2013 Konstantin Mosesov
- *
- * This file is part of Kamailio, a free SIP server.
- *
- * This file is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- *
- * This file is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- */
- #include <string.h>
- #include <errno.h>
- #include <float.h>
- #include "../../str.h"
- #include "../../sr_module.h"
- #include <jni.h>
- #include "global.h"
- #include "utils.h"
- #include "java_mod.h"
- #include "java_iface.h"
- #include "java_support.h"
- #include "java_native_methods.h"
- #include "java_sig_parser.h"
- int is_sig_allowed(char *s)
- {
- if (s == NULL || strlen(s) < 1)
- return 0;
- if (!strcmp(s, " ") || !strcmp(s, "\n") || !strcmp(s, "\r") || !strcmp(s, "\t"))
- {
- LM_ERR("%s: signature error: '%s' contains whitespaces or any unparsable chars.\n", APP_NAME, s);
- return 0;
- }
- // LM_ERR("s='%s', strlen(s)=%d\n", s, strlen(s));
- if (strlen(s) == 1) // signature is single modifier (primitive)
- {
- if (!strcmp(s, "[")) // invalid signature modifier definition
- {
- LM_ERR("%s: signature error: '%s': no type of array specified.\n", APP_NAME, s);
- return 0;
- }
- if (!strcmp(s, "L")) // invalid signature modifier definition
- {
- LM_ERR("%s: signature error '%s': no object specified.\n", APP_NAME, s);
- return 0;
- }
- #ifndef JAVA_INV_SUPP_TYPE_VOID
- if (!strcmp(s, "V"))
- {
- LM_ERR("%s: signature error '%s': no object specified.\n", APP_NAME, s);
- return 0;
- }
- #endif
- }
- else // a complex signature (object)
- {
- #ifndef JAVA_INV_SUPP_TYPE_ARRAYS
- if (strcmp(s, "[") > 0)
- {
- LM_ERR("%s: signature error: '%s' denotes array which isn't supported yet.\n", APP_NAME, s);
- return 0;
- }
- #endif
- if (strrchr(&s[0], 'L') > 0)
- {
- #ifndef JAVA_INV_SUPP_TYPE_OBJECTS
- LM_ERR("%s: signature error: '%s' denotes object which isn't supported yet.\n", APP_NAME, s);
- return 0;
- #else
- int f = 0;
- #ifdef JAVA_INV_SUPP_TYPE_BOOLEAN
- if (!strcmp(s, "Ljava/lang/Boolean;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_BYTE
- if (!strcmp(s, "Ljava/lang/Byte;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_CHARACTER
- if (!strcmp(s, "Ljava/lang/Character;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_DOUBLE
- if (!strcmp(s, "Ljava/lang/Double;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_FLOAT
- if (!strcmp(s, "Ljava/lang/Float;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_INTEGER
- if (!strcmp(s, "Ljava/lang/Integer;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_LONG
- if (!strcmp(s, "Ljava/lang/Long;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_SHORT
- if (!strcmp(s, "Ljava/lang/Short;"))
- f = 1;
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_STRING
- if (!strcmp(s, "Ljava/lang/String;"))
- f = 1;
- #endif
- if (f == 0)
- {
- LM_ERR("%s: signature '%s' isn't supported yet.\n", APP_NAME, s);
- return 0;
- }
- #endif
- }
- }
- return 1;
- }
- static char *get_conv_err_str(int en)
- {
- switch(en)
- {
- case EINVAL: return "The value of base constant is not supported or no conversion could be performed";
- case ERANGE: return "The given string was out of range; the value converted has been clamped.";
- default: return "General parse error";
- }
- }
- /* explaination of jvalue fields:
- typedef union jvalue {
- jboolean z;
- jbyte b;
- jchar c;
- jshort s;
- jint i;
- jlong j;
- jfloat f;
- jdouble d;
- jobject l;
- } jvalue;
- */
- jvalue *get_value_by_sig_type(char *sig, char *pval)
- {
- char *endptr;
- char scptr;
- int siptr;
- long slptr;
- short ssptr;
- double sdptr;
- float sfptr;
- jstring sjptr;
- jvalue *ret;
- ret = (jvalue *)pkg_malloc(sizeof(jvalue));
- if (!ret)
- {
- LM_ERR("%s: pkg_malloc() has failed. Not enouph memory!\n", APP_NAME);
- return NULL;
- }
- if (sig == NULL || strlen(sig) <= 0)
- {
- LM_ERR("%s: Can't process empty or NULL signature.\n", APP_NAME);
- pkg_free(ret);
- return NULL;
- }
- if (pval == NULL || strlen(pval) <= 0)
- {
- LM_ERR("%s: Can't process empty or NULL parameter value.\n", APP_NAME);
- pkg_free(ret);
- return NULL;
- }
- // boolean
- if (!strncmp(sig, "Z", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_BOOLEAN)
- || !strcmp(sig, "Ljava/lang/Boolean;")
- #endif
- ) {
- if (!strncasecmp(pval, "true", 4))
- (*ret).z = (jboolean)JNI_TRUE;
- /* comment this block to avoid conversation '1' to 'true' */
- else if (!strncmp(pval, "1", 1))
- (*ret).z = (jboolean)JNI_TRUE;
- else if (!strncasecmp(pval, "false", 5))
- (*ret).z = (jboolean)JNI_FALSE;
- /* comment this block to avoid conversation '0' to 'false' */
- else if (!strncmp(pval, "0", 1))
- (*ret).z = (jboolean)JNI_FALSE;
- else
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'.\n", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- return ret;
- }
- else
- // byte
- if (!strncmp(sig, "B", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_BYTE)
- || !strcmp(sig, "Ljava/lang/Byte;")
- #endif
- ) {
- // skptr = (signed char)char2jbyte(pval);
- sscanf(pval, "%x", &siptr);
- if (siptr == 0 && errno != 0)
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
- pkg_free(ret);
- return NULL;
- }
- if (siptr < SCHAR_MAX || siptr > SCHAR_MAX)
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- (*ret).b = (jbyte)siptr;
- return ret;
- }
- else
- // char
- if (!strncmp(sig, "C", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_CHARACTER)
- || !strcmp(sig, "Ljava/lang/Character;")
- #endif
- ) {
- sscanf(pval, "%c", &scptr);
- if (scptr == 0 && errno != 0)
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
- pkg_free(ret);
- return NULL;
- }
- if (scptr < CHAR_MIN || scptr > CHAR_MAX) // overflow
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- (*ret).c = (jchar)scptr;
- return ret;
- }
- else
- // double
- if (!strncmp(sig, "D", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_DOUBLE)
- || !strcmp(sig, "Ljava/lang/Double;")
- #endif
- ) {
- sdptr = (double)strtod(pval, &endptr);
- if ((sdptr == 0 && errno != 0) || (pval == endptr))
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
- pkg_free(ret);
- return NULL;
- }
- if (sdptr < LLONG_MIN || sdptr > LLONG_MAX) // overflow
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- (*ret).d = (jdouble)sdptr;
- return ret;
- }
- else
- // float
- if (!strncmp(sig, "F", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_FLOAT)
- || !strcmp(sig, "Ljava/lang/Float;")
- #endif
- ) {
- sfptr = (float)strtof(pval, &endptr);
- if ((sfptr == 0 && errno != 0) || (pval == endptr))
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
- pkg_free(ret);
- return NULL;
- }
- if (sfptr < FLT_MIN || sfptr > FLT_MAX) // overflow
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- (*ret).f = (jfloat)sfptr;
- return ret;
- }
- else
- // integer
- if (!strncmp(sig, "I", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_INTEGER)
- || !strcmp(sig, "Ljava/lang/Integer;")
- #endif
- ) {
- slptr = strtol(pval, &endptr, 10);
- if ((slptr == 0 && errno != 0) || (pval == endptr))
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
- pkg_free(ret);
- return NULL;
- }
- if (slptr < INT_MIN || slptr > INT_MAX) // overflow
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- (*ret).i = (jint)slptr;
- return ret;
- }
- else
- // long
- if (!strncmp(sig, "J", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_LONG)
- || !strcmp(sig, "Ljava/lang/Long;")
- #endif
- ) {
- slptr = (long)strtol(pval, &endptr, 10);
- if ((slptr == 0 && errno != 0) || (pval == endptr))
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
- pkg_free(ret);
- return NULL;
- }
- if (slptr < LONG_MIN || slptr > LONG_MAX) // overflow
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- (*ret).j = (jlong)slptr;
- return ret;
- }
- else
- // short
- if (!strncmp(sig, "S", 1)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_SHORT)
- || !strcmp(sig, "Ljava/lang/Short;")
- #endif
- ) {
- ssptr = (short)strtod(pval, &endptr);
- if ((ssptr == 0 && errno != 0) || (pval == endptr))
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
- pkg_free(ret);
- return NULL;
- }
- if (ssptr < SHRT_MIN || ssptr > SHRT_MAX) // overflow
- {
- LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- (*ret).s = (jshort)ssptr;
- return ret;
- }
- // String (object)
- #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_STRING)
- else
- if (!strcmp(sig, "Ljava/lang/String;"))
- {
- sjptr = (*env)->NewStringUTF(env, pval);
- if ((*env)->ExceptionCheck(env))
- {
- pkg_free(ret);
- handle_exception();
- return NULL;
- }
- /*
- if (pval != NULL && sjptr == NULL)
- {
- pkg_free(ret);
- return NULL;
- }
- */
- (*ret).l = (jstring)sjptr;
- return ret;
- }
- #endif
- #ifdef JAVA_INV_SUPP_TYPE_VOID
- else
- if (!strncmp(sig, "V", 1))
- {
- pkg_free(ret);
- return NULL;
- }
- #endif
- else
- {
- // unknown sig
- LM_ERR("%s: Can't cast '%s' to signature '%s'\n", APP_NAME, pval, sig);
- pkg_free(ret);
- return NULL;
- }
- return NULL;
- }
|