aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/jni12tools.c
blob: 09b948a38da446f653b2fe3601f9386ab5695746 (plain)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <string.h>
#include "jnitools.h"

#include "gltool.h"

#ifdef __BUILTIN_VA_ARG_INCR
        /* Special stuff for the SunOS port ... */
        va_list __builtin_va_arg_incr = 0;
        va_list __builtin_va_alist = 0;
#endif

static int isinit = 0;
static jclass classString=0;
static jmethodID String_getBytes_ID=0;
static jmethodID String_init_ID=0;

static int init(JNIEnv *env)
{
    classString = (*env)->FindClass(env, "java/lang/String");
    if(classString)
    {
	    String_getBytes_ID = 
		(*env)->GetMethodID(env, classString, "getBytes", "()[B");
	    if(!String_getBytes_ID) return 0;
	    String_init_ID = (*env)->GetMethodID(env, classString, "<init>", "([B)V");
	    if(!String_init_ID) return 0;
    } else return 0;
    return 1;
}

/* Throw an exception by name */
void LIBAPIENTRY jnitoolsThrowByName(JNIEnv *env, const char *name, const char *msg, ...)
{
    static char buffer[1024];
    va_list va;
    jclass cls = 0;

    if(!isinit && init(env)) isinit=1;

    va_start(va, msg);  

    #ifdef NO_VSNPRINTF
	    (void) vsprintf(buffer, msg, va); /* dangerous -> stack overwrite ... */
    #else
	    (void) vsnprintf(buffer, 1023, msg, va);
    #endif
    buffer[1023]=0;

    va_end(va);

    cls = (*env)->FindClass(env, name);

    if (cls != 0) /* Otherwise an exception has already been thrown */
        (*env)->ThrowNew(env, cls, buffer);

    /* It's a good practice to clean up the local references. */
    (*env)->DeleteLocalRef(env, cls);
}

/* Translates a Java string to a C string using the String.getBytes
 * method, which uses default local encoding.
 */
char * LIBAPIENTRY jnitoolsGetJavaString(JNIEnv *env, jstring jstr)
{
    jbyteArray hab = 0;
    jthrowable exc;
    char *result = 0;

    if(!isinit && init(env)) isinit=1;

    hab = (*env)->CallObjectMethod(env, jstr, String_getBytes_ID);
    exc = (*env)->ExceptionOccurred(env);
    if (!exc) {
        jint len = (*env)->GetArrayLength(env, hab);
        result = (char *)malloc(len + 1);
        if (result == 0) {
            jnitoolsThrowByName(env, "java/lang/OutOfMemoryError", 0);
            (*env)->DeleteLocalRef(env, hab);
            return 0;
        }
        (*env)->GetByteArrayRegion(env, hab, 0, len, (jbyte *)result);
        result[len] = 0; /* NULL-terminate */
    } else {
        (*env)->DeleteLocalRef(env, exc);
    }
    (*env)->DeleteLocalRef(env, hab);
    return result;
}

/**
 * signature: e.g. "(II[I)V" for "void <name>(int, int, int[])"
 *
 * argIndex:  0-n argument index of the method signature
 *            -1  return type
 */
static JavaBasicType _jnitoolsGetJavaBasicTypeAndArgLen(JNIEnv *env, 
                                                   const char * signature, int argIndex, 
						   const char * errText,
						   int * argLen)
{
	int i, j, arg, argend, arraydim;
	char c, *help;
	JavaBasicType jbt = T_NOPE;

    	if(!isinit && init(env)) isinit=1;

	*argLen=0;

	if(signature==NULL) return T_NOPE;
	j=strlen(signature);

	for(i=0, arg=0, argend=0, arraydim=0; i<j; i++)
	{
		c=signature[i];
		if(c=='(') continue;
		if(c==')') { argend=1; continue; }
		if(c=='[') { arraydim++; continue; }
		switch (c)
		{
			case 'L':
				help = strchr(signature+i, ';');
				if(help==NULL)
				{
				  jnitoolsThrowByName(env, 
				        "java/lang/IllegalArgumentException",
				        "%s(arg #%d): invalid signature (inv. object) at %d <%s>\n", 
					 errText, argIndex+1, i, signature);
				  return T_NOPE;
				}
				jbt = (arraydim==0)?T_OBJECT:T_OBJECT_ARRAY;
				i = help-signature;
				arraydim=0;
				break;
			case 'V':
				if(arraydim>0)
				{
				  jnitoolsThrowByName(env, 
				        "java/lang/IllegalArgumentException",
				        "%s(arg #%d): invalid signature (void array) at %d <%s>\n", 
					 errText, argIndex+1, i, signature);
				  return T_NOPE;
				}
				jbt = T_VOID;
				arraydim=0;
				break;
			case 'Z':
				jbt = (arraydim==0)?T_BOOLEAN:T_BOOLEAN_ARRAY;
				arraydim=0;
				break;
			case 'B':
				jbt = (arraydim==0)?T_BYTE:T_BYTE_ARRAY;
				arraydim=0;
				break;
			case 'C':
				jbt = (arraydim==0)?T_CHAR:T_CHAR_ARRAY;
				arraydim=0;
				break;
			case 'S':
				jbt = (arraydim==0)?T_SHORT:T_SHORT_ARRAY;
				arraydim=0;
				break;
			case 'I':
				jbt = (arraydim==0)?T_INT:T_INT_ARRAY;
				arraydim=0;
				break;
			case 'J':
				jbt = (arraydim==0)?T_LONG:T_LONG_ARRAY;
				arraydim=0;
				break;
			case 'F':
				jbt = (arraydim==0)?T_FLOAT:T_FLOAT_ARRAY;
				arraydim=0;
				break;
			case 'D':
				jbt = (arraydim==0)?T_DOUBLE:T_DOUBLE_ARRAY;
				arraydim=0;
				break;
		}
		if( (argIndex >=0 && arg==argIndex) ||
		    (argIndex==-1 && argend==1)
		  )
		{
		  *argLen=arg;
		  return jbt;
		}
		arg++;
	}
	return T_NOPE;
}

/**
 * signature: e.g. "(II[I)V" for "void <name>(int, int, int[])"
 *
 * argIndex:  0-n argument index of the method signature
 *            -1  return type
 */
int LIBAPIENTRY jnitoolsGetArgNumber(JNIEnv *env, 
                                     const char * signature,
				     const char * errText)
{
	int num = 0;
	(void)  _jnitoolsGetJavaBasicTypeAndArgLen(env, 
                                                   signature, -1,
						   errText,
						   &num);
	return num;
}

/**
 * signature: e.g. "(II[I)V" for "void <name>(int, int, int[])"
 *
 * argIndex:  0-n argument index of the method signature
 *            -1  return type
 */
JavaBasicType LIBAPIENTRY jnitoolsGetJavaBasicType(JNIEnv *env, 
                                                   const char * signature, int argIndex, 
						   const char * errText)
{
	int num = 0;
	JavaBasicType jbt = _jnitoolsGetJavaBasicTypeAndArgLen(env, 
                                                   signature, argIndex,
						   errText,
						   &num);
	return jbt;
}

jarray LIBAPIENTRY jnitoolsNativeArray2JavaArray (JNIEnv *env, JavaBasicType jbt, 
                                     void *data, int dataArrayLen, 
				     const char * errText, int arg)
{
	jarray res = 0;

    	if(!isinit && init(env)) isinit=1;

	switch(jbt)
	{
		case T_BOOLEAN_ARRAY:
			res = (*env)->NewBooleanArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetBooleanArrayRegion(env, (jbooleanArray)res, 
							   0, dataArrayLen, 
							   (jboolean*)data);
			break;
		case T_BYTE_ARRAY:
			res = (*env)->NewByteArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetByteArrayRegion(env, (jbyteArray)res, 
						   0, dataArrayLen, (jbyte*)data);
			break;
		case T_CHAR_ARRAY:
			res = (*env)->NewCharArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetCharArrayRegion(env, (jcharArray)res, 
						   0, dataArrayLen, (jchar*)data);
			break;
		case T_SHORT_ARRAY:
			res = (*env)->NewShortArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetShortArrayRegion(env, (jshortArray)res, 
						   0, dataArrayLen, (jshort*)data);
			break;
		case T_INT_ARRAY:
			res = (*env)->NewIntArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetIntArrayRegion(env, (jintArray)res, 
						   0, dataArrayLen, (jint*)data);
			break;
		case T_LONG_ARRAY:
			res = (*env)->NewLongArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetLongArrayRegion(env, (jlongArray)res, 
						   0, dataArrayLen, (jlong*)data);
			break;
		case T_FLOAT_ARRAY:
			res = (*env)->NewFloatArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetFloatArrayRegion(env, (jfloatArray)res, 
						   0, dataArrayLen, (jfloat*)data);
			break;
		case T_DOUBLE_ARRAY:
			res = (*env)->NewDoubleArray(env, dataArrayLen);
			if(data!=NULL)
				(*env)->SetDoubleArrayRegion(env, (jdoubleArray)res, 
						   0, dataArrayLen, (jdouble*)data);
			break;
		default:
			jnitoolsThrowByName(env, "java/lang/IllegalArgumentException",
				                "%s(arg #%d): Type is not an array",
						errText, arg);
			return 0;
	}
	return res;
}

jarray LIBAPIENTRY jnitoolsNativePtrArray2JavaArray (JNIEnv *env, 
						 JavaBasicType jbt, 
                                                 void *data[/*pointerNumber*/], 
						 int pointerNumber, 
						 int dataArrayLen, 
						 const char * errText, int arg)

{
	unsigned char * buffer = NULL;
	jarray res = 0;
	int i;
	int setLenElems = dataArrayLen/pointerNumber;
	int setLenBytes = 0;


    	if(!isinit && init(env)) isinit=1;

	switch(jbt)
	{
		case T_BYTE_ARRAY:
			setLenBytes = setLenElems * sizeof(jbyte) ;
			break;
		case T_CHAR_ARRAY:
			setLenBytes = setLenElems * sizeof(jchar) ;
			break;
		case T_SHORT_ARRAY:
			setLenBytes = setLenElems * sizeof(jshort) ;
			break;
		case T_BOOLEAN_ARRAY:
			setLenBytes = setLenElems * sizeof(jboolean) ;
			break;
		case T_INT_ARRAY:
			setLenBytes = setLenElems * sizeof(jint) ;
			break;
		case T_LONG_ARRAY:
			setLenBytes = setLenElems * sizeof(jlong) ;
			break;
		case T_FLOAT_ARRAY:
			setLenBytes = setLenElems * sizeof(jfloat) ;
			break;
		case T_DOUBLE_ARRAY:
			setLenBytes = setLenElems * sizeof(jdouble) ;
			break;
		default:
			jnitoolsThrowByName(env, "java/lang/IllegalArgumentException",
				                "%s(arg #%d): Type is not an array",
						errText, arg);
			return 0;
	}
	buffer = calloc (pointerNumber, setLenBytes);
	if(buffer==NULL)
	{
			jnitoolsThrowByName(env, "java/lang/Exception",
				                "%s(arg #%d): Out of memory",
						errText, arg);
			return 0;
	}
	for(i=0; data!=NULL && i<pointerNumber; i++)
	{
	    if(data[i]!=NULL)
	    {
		memcpy(buffer+i*setLenBytes,
		       data[i], setLenBytes);
	    } else {
	        memset(buffer+i*setLenBytes, 0, setLenBytes);
	    }
	}

	res = jnitoolsNativeArray2JavaArray (env, jbt, 
                                             buffer, dataArrayLen, 
					     errText, arg);
	free(buffer);
	return res;
}

void LIBAPIENTRY jnitoolsReleaseJavaArray2NativeArray (JNIEnv *env, jarray arr, JavaBasicType jbt, 
                                          void *data, int dataArrayLen, 
					  const char * errText, int arg)
{
	int len = (*env)->GetArrayLength(env, arr);
	int lenb = 0;
	void * arrdata = 0;

    	if(!isinit && init(env)) isinit=1;

    	if(data==NULL) return;

	if(len!=dataArrayLen)
	{
		jnitoolsThrowByName(env, "java/lang/IllegalArgumentException",
					"%s(arg #%d): Array-Length differs of Java-Callback input(%d) vs output(%d)", errText, arg, dataArrayLen, len);
		return;
	}

	arrdata = (void *) (*env)->GetPrimitiveArrayCritical(env, arr, 0);

	switch(jbt)
	{
		case T_BOOLEAN_ARRAY:
			lenb = len*sizeof(jboolean);
			break;
		case T_BYTE_ARRAY:
			lenb = len*sizeof(jbyte);
			break;
		case T_CHAR_ARRAY:
			lenb = len*sizeof(jchar);
			break;
		case T_SHORT_ARRAY:
			lenb = len*sizeof(jshort);
			break;
		case T_INT_ARRAY:
			lenb = len*sizeof(jint);
			break;
		case T_LONG_ARRAY:
			lenb = len*sizeof(jlong);
			break;
		case T_FLOAT_ARRAY:
			lenb = len*sizeof(jfloat);
			break;
		case T_DOUBLE_ARRAY:
			lenb = len*sizeof(jdouble);
			break;
		default:
			jnitoolsThrowByName(env, "java/lang/IllegalArgumentException",
				                "%s(arg #%d): Type is not an array",
						errText, arg);
	}
	memcpy (data, arrdata, lenb);
	(*env)->ReleasePrimitiveArrayCritical(env,  arr, arrdata, JNI_ABORT);
}

void LIBAPIENTRY jnitoolsReleaseJavaArray2NativeArrayPtr (JNIEnv *env, 
				                 jarray arr, 
						 JavaBasicType jbt, 
                                                 void *data[/*pointerNumber*/], 
						 int pointerNumber, 
						 int dataArrayLen, 
						 const char * errText,
						 int arg)
{
	int len = (*env)->GetArrayLength(env, arr);
	unsigned char * arrdata = 0;
	int i;
	int setLenElems = dataArrayLen/pointerNumber;
	int setLenBytes = 0;

    	if(!isinit && init(env)) isinit=1;

    	if(data==NULL) return;

	if(len!=dataArrayLen)
	{
		jnitoolsThrowByName(env, "java/lang/IllegalArgumentException",
					"%s(arg #%d): Array-Length differs of Java-Callback input(%d) vs output(%d)", errText, arg, dataArrayLen, len);
		return;
	}

	arrdata = (unsigned char *) 
		(*env)->GetPrimitiveArrayCritical(env, arr, 0);

	switch(jbt)
	{
		case T_BOOLEAN_ARRAY:
			setLenBytes = setLenElems * sizeof(jboolean);
			break;
		case T_BYTE_ARRAY:
			setLenBytes = setLenElems * sizeof(jbyte);
			break;
		case T_CHAR_ARRAY:
			setLenBytes = setLenElems * sizeof(jchar);
			break;
		case T_SHORT_ARRAY:
			setLenBytes = setLenElems * sizeof(jshort);
			break;
		case T_INT_ARRAY:
			setLenBytes = setLenElems * sizeof(jint);
			break;
		case T_LONG_ARRAY:
			setLenBytes = setLenElems * sizeof(jlong);
			break;
		case T_FLOAT_ARRAY:
			setLenBytes = setLenElems * sizeof(jfloat);
			break;
		case T_DOUBLE_ARRAY:
			setLenBytes = setLenElems * sizeof(jdouble);
			break;
		default:
			jnitoolsThrowByName(env, "java/lang/IllegalArgumentException",
				                "%s(arg #%d): Type is not an array", errText, arg);
	}
	for(i=0; i<pointerNumber; i++)
		if(data[i]!=NULL)
			memcpy(data[i], arrdata+i*setLenBytes,
			       setLenBytes);

	(*env)->ReleasePrimitiveArrayCritical(env,  arr, arrdata, JNI_ABORT);
}


void * LIBAPIENTRY jnitoolsCreateNativeArray (JavaBasicType jbt, int dataArrayLen, const char * errText)
{
	void * arrdata = 0;

	(void)errText;

	switch(jbt)
	{
		case T_BOOLEAN_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(int));
			break;
		case T_BYTE_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(char));
			break;
		case T_CHAR_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(char));
			break;
		case T_SHORT_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(short));
			break;
		case T_INT_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(int));
			break;
		case T_LONG_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(long));
			break;
		case T_FLOAT_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(float));
			break;
		case T_DOUBLE_ARRAY:
			arrdata = calloc(dataArrayLen, sizeof(double));
			break;
		default:
			return 0;
	}
	return arrdata;
}


JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM * vm, void *reserved)
{
    return JNI_VERSION_1_2;
}

JNIEXPORT void JNICALL
JNI_OnUnload(JavaVM *vm, void *reserved)
{
	unloadGLLibrary();
}