2
0

java_sig_parser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2013 Konstantin Mosesov
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <float.h>
  27. #include "../../str.h"
  28. #include "../../sr_module.h"
  29. #include <jni.h>
  30. #include "global.h"
  31. #include "utils.h"
  32. #include "java_mod.h"
  33. #include "java_iface.h"
  34. #include "java_support.h"
  35. #include "java_native_methods.h"
  36. #include "java_sig_parser.h"
  37. int is_sig_allowed(char *s)
  38. {
  39. if (s == NULL || strlen(s) < 1)
  40. return 0;
  41. if (!strcmp(s, " ") || !strcmp(s, "\n") || !strcmp(s, "\r") || !strcmp(s, "\t"))
  42. {
  43. LM_ERR("%s: signature error: '%s' contains whitespaces or any unparsable chars.\n", APP_NAME, s);
  44. return 0;
  45. }
  46. // LM_ERR("s='%s', strlen(s)=%d\n", s, strlen(s));
  47. if (strlen(s) == 1) // signature is single modifier (primitive)
  48. {
  49. if (!strcmp(s, "[")) // invalid signature modifier definition
  50. {
  51. LM_ERR("%s: signature error: '%s': no type of array specified.\n", APP_NAME, s);
  52. return 0;
  53. }
  54. if (!strcmp(s, "L")) // invalid signature modifier definition
  55. {
  56. LM_ERR("%s: signature error '%s': no object specified.\n", APP_NAME, s);
  57. return 0;
  58. }
  59. #ifndef JAVA_INV_SUPP_TYPE_VOID
  60. if (!strcmp(s, "V"))
  61. {
  62. LM_ERR("%s: signature error '%s': no object specified.\n", APP_NAME, s);
  63. return 0;
  64. }
  65. #endif
  66. }
  67. else // a complex signature (object)
  68. {
  69. #ifndef JAVA_INV_SUPP_TYPE_ARRAYS
  70. if (strcmp(s, "[") > 0)
  71. {
  72. LM_ERR("%s: signature error: '%s' denotes array which isn't supported yet.\n", APP_NAME, s);
  73. return 0;
  74. }
  75. #endif
  76. if (strrchr(&s[0], 'L') > 0)
  77. {
  78. #ifndef JAVA_INV_SUPP_TYPE_OBJECTS
  79. LM_ERR("%s: signature error: '%s' denotes object which isn't supported yet.\n", APP_NAME, s);
  80. return 0;
  81. #else
  82. int f = 0;
  83. #ifdef JAVA_INV_SUPP_TYPE_BOOLEAN
  84. if (!strcmp(s, "Ljava/lang/Boolean;"))
  85. f = 1;
  86. #endif
  87. #ifdef JAVA_INV_SUPP_TYPE_BYTE
  88. if (!strcmp(s, "Ljava/lang/Byte;"))
  89. f = 1;
  90. #endif
  91. #ifdef JAVA_INV_SUPP_TYPE_CHARACTER
  92. if (!strcmp(s, "Ljava/lang/Character;"))
  93. f = 1;
  94. #endif
  95. #ifdef JAVA_INV_SUPP_TYPE_DOUBLE
  96. if (!strcmp(s, "Ljava/lang/Double;"))
  97. f = 1;
  98. #endif
  99. #ifdef JAVA_INV_SUPP_TYPE_FLOAT
  100. if (!strcmp(s, "Ljava/lang/Float;"))
  101. f = 1;
  102. #endif
  103. #ifdef JAVA_INV_SUPP_TYPE_INTEGER
  104. if (!strcmp(s, "Ljava/lang/Integer;"))
  105. f = 1;
  106. #endif
  107. #ifdef JAVA_INV_SUPP_TYPE_LONG
  108. if (!strcmp(s, "Ljava/lang/Long;"))
  109. f = 1;
  110. #endif
  111. #ifdef JAVA_INV_SUPP_TYPE_SHORT
  112. if (!strcmp(s, "Ljava/lang/Short;"))
  113. f = 1;
  114. #endif
  115. #ifdef JAVA_INV_SUPP_TYPE_STRING
  116. if (!strcmp(s, "Ljava/lang/String;"))
  117. f = 1;
  118. #endif
  119. if (f == 0)
  120. {
  121. LM_ERR("%s: signature '%s' isn't supported yet.\n", APP_NAME, s);
  122. return 0;
  123. }
  124. #endif
  125. }
  126. }
  127. return 1;
  128. }
  129. static char *get_conv_err_str(int en)
  130. {
  131. switch(en)
  132. {
  133. case EINVAL: return "The value of base constant is not supported or no conversion could be performed";
  134. case ERANGE: return "The given string was out of range; the value converted has been clamped.";
  135. default: return "General parse error";
  136. }
  137. }
  138. /* explaination of jvalue fields:
  139. typedef union jvalue {
  140. jboolean z;
  141. jbyte b;
  142. jchar c;
  143. jshort s;
  144. jint i;
  145. jlong j;
  146. jfloat f;
  147. jdouble d;
  148. jobject l;
  149. } jvalue;
  150. */
  151. jvalue *get_value_by_sig_type(char *sig, char *pval)
  152. {
  153. char *endptr;
  154. char scptr;
  155. int siptr;
  156. long slptr;
  157. short ssptr;
  158. double sdptr;
  159. float sfptr;
  160. jstring sjptr;
  161. jvalue *ret;
  162. ret = (jvalue *)pkg_malloc(sizeof(jvalue));
  163. if (!ret)
  164. {
  165. LM_ERR("%s: pkg_malloc() has failed. Not enouph memory!\n", APP_NAME);
  166. return NULL;
  167. }
  168. if (sig == NULL || strlen(sig) <= 0)
  169. {
  170. LM_ERR("%s: Can't process empty or NULL signature.\n", APP_NAME);
  171. pkg_free(ret);
  172. return NULL;
  173. }
  174. if (pval == NULL || strlen(pval) <= 0)
  175. {
  176. LM_ERR("%s: Can't process empty or NULL parameter value.\n", APP_NAME);
  177. pkg_free(ret);
  178. return NULL;
  179. }
  180. // boolean
  181. if (!strncmp(sig, "Z", 1)
  182. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_BOOLEAN)
  183. || !strcmp(sig, "Ljava/lang/Boolean;")
  184. #endif
  185. ) {
  186. if (!strncasecmp(pval, "true", 4))
  187. (*ret).z = (jboolean)JNI_TRUE;
  188. /* comment this block to avoid conversation '1' to 'true' */
  189. else if (!strncmp(pval, "1", 1))
  190. (*ret).z = (jboolean)JNI_TRUE;
  191. else if (!strncasecmp(pval, "false", 5))
  192. (*ret).z = (jboolean)JNI_FALSE;
  193. /* comment this block to avoid conversation '0' to 'false' */
  194. else if (!strncmp(pval, "0", 1))
  195. (*ret).z = (jboolean)JNI_FALSE;
  196. else
  197. {
  198. LM_ERR("%s: Can't cast '%s' to type '%s'.\n", APP_NAME, pval, sig);
  199. pkg_free(ret);
  200. return NULL;
  201. }
  202. return ret;
  203. }
  204. else
  205. // byte
  206. if (!strncmp(sig, "B", 1)
  207. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_BYTE)
  208. || !strcmp(sig, "Ljava/lang/Byte;")
  209. #endif
  210. ) {
  211. // skptr = (signed char)char2jbyte(pval);
  212. sscanf(pval, "%x", &siptr);
  213. if (siptr == 0 && errno != 0)
  214. {
  215. LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
  216. pkg_free(ret);
  217. return NULL;
  218. }
  219. if (siptr < SCHAR_MAX || siptr > SCHAR_MAX)
  220. {
  221. LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
  222. pkg_free(ret);
  223. return NULL;
  224. }
  225. (*ret).b = (jbyte)siptr;
  226. return ret;
  227. }
  228. else
  229. // char
  230. if (!strncmp(sig, "C", 1)
  231. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_CHARACTER)
  232. || !strcmp(sig, "Ljava/lang/Character;")
  233. #endif
  234. ) {
  235. sscanf(pval, "%c", &scptr);
  236. if (scptr == 0 && errno != 0)
  237. {
  238. LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
  239. pkg_free(ret);
  240. return NULL;
  241. }
  242. if (scptr < CHAR_MIN || scptr > CHAR_MAX) // overflow
  243. {
  244. LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
  245. pkg_free(ret);
  246. return NULL;
  247. }
  248. (*ret).c = (jchar)scptr;
  249. return ret;
  250. }
  251. else
  252. // double
  253. if (!strncmp(sig, "D", 1)
  254. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_DOUBLE)
  255. || !strcmp(sig, "Ljava/lang/Double;")
  256. #endif
  257. ) {
  258. sdptr = (double)strtod(pval, &endptr);
  259. if ((sdptr == 0 && errno != 0) || (pval == endptr))
  260. {
  261. LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
  262. pkg_free(ret);
  263. return NULL;
  264. }
  265. if (sdptr < LLONG_MIN || sdptr > LLONG_MAX) // overflow
  266. {
  267. LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
  268. pkg_free(ret);
  269. return NULL;
  270. }
  271. (*ret).d = (jdouble)sdptr;
  272. return ret;
  273. }
  274. else
  275. // float
  276. if (!strncmp(sig, "F", 1)
  277. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_FLOAT)
  278. || !strcmp(sig, "Ljava/lang/Float;")
  279. #endif
  280. ) {
  281. sfptr = (float)strtof(pval, &endptr);
  282. if ((sfptr == 0 && errno != 0) || (pval == endptr))
  283. {
  284. LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
  285. pkg_free(ret);
  286. return NULL;
  287. }
  288. if (sfptr < FLT_MIN || sfptr > FLT_MAX) // overflow
  289. {
  290. LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
  291. pkg_free(ret);
  292. return NULL;
  293. }
  294. (*ret).f = (jfloat)sfptr;
  295. return ret;
  296. }
  297. else
  298. // integer
  299. if (!strncmp(sig, "I", 1)
  300. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_INTEGER)
  301. || !strcmp(sig, "Ljava/lang/Integer;")
  302. #endif
  303. ) {
  304. slptr = strtol(pval, &endptr, 10);
  305. if ((slptr == 0 && errno != 0) || (pval == endptr))
  306. {
  307. LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
  308. pkg_free(ret);
  309. return NULL;
  310. }
  311. if (slptr < INT_MIN || slptr > INT_MAX) // overflow
  312. {
  313. LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
  314. pkg_free(ret);
  315. return NULL;
  316. }
  317. (*ret).i = (jint)slptr;
  318. return ret;
  319. }
  320. else
  321. // long
  322. if (!strncmp(sig, "J", 1)
  323. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_LONG)
  324. || !strcmp(sig, "Ljava/lang/Long;")
  325. #endif
  326. ) {
  327. slptr = (long)strtol(pval, &endptr, 10);
  328. if ((slptr == 0 && errno != 0) || (pval == endptr))
  329. {
  330. LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
  331. pkg_free(ret);
  332. return NULL;
  333. }
  334. if (slptr < LONG_MIN || slptr > LONG_MAX) // overflow
  335. {
  336. LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
  337. pkg_free(ret);
  338. return NULL;
  339. }
  340. (*ret).j = (jlong)slptr;
  341. return ret;
  342. }
  343. else
  344. // short
  345. if (!strncmp(sig, "S", 1)
  346. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_SHORT)
  347. || !strcmp(sig, "Ljava/lang/Short;")
  348. #endif
  349. ) {
  350. ssptr = (short)strtod(pval, &endptr);
  351. if ((ssptr == 0 && errno != 0) || (pval == endptr))
  352. {
  353. LM_ERR("%s: Can't cast '%s' to type '%s'. Error: %s.\n", APP_NAME, pval, sig, get_conv_err_str(errno));
  354. pkg_free(ret);
  355. return NULL;
  356. }
  357. if (ssptr < SHRT_MIN || ssptr > SHRT_MAX) // overflow
  358. {
  359. LM_ERR("%s: Can't cast '%s' to type '%s'. Reason: overflow.", APP_NAME, pval, sig);
  360. pkg_free(ret);
  361. return NULL;
  362. }
  363. (*ret).s = (jshort)ssptr;
  364. return ret;
  365. }
  366. // String (object)
  367. #if defined(JAVA_INV_SUPP_TYPE_OBJECTS) && defined(JAVA_INV_SUPP_TYPE_STRING)
  368. else
  369. if (!strcmp(sig, "Ljava/lang/String;"))
  370. {
  371. sjptr = (*env)->NewStringUTF(env, pval);
  372. if ((*env)->ExceptionCheck(env))
  373. {
  374. pkg_free(ret);
  375. handle_exception();
  376. return NULL;
  377. }
  378. /*
  379. if (pval != NULL && sjptr == NULL)
  380. {
  381. pkg_free(ret);
  382. return NULL;
  383. }
  384. */
  385. (*ret).l = (jstring)sjptr;
  386. return ret;
  387. }
  388. #endif
  389. #ifdef JAVA_INV_SUPP_TYPE_VOID
  390. else
  391. if (!strncmp(sig, "V", 1))
  392. {
  393. pkg_free(ret);
  394. return NULL;
  395. }
  396. #endif
  397. else
  398. {
  399. // unknown sig
  400. LM_ERR("%s: Can't cast '%s' to signature '%s'\n", APP_NAME, pval, sig);
  401. pkg_free(ret);
  402. return NULL;
  403. }
  404. return NULL;
  405. }