1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/**
* $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;
}
|