1 /*
2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 #include <jni.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "jnihelper.h"
29 
30 extern "C" {
31 
32 #define DIGESTLENGTH 16
33 
34 JNIEXPORT jcharArray JNICALL
Java_nsk_stress_jni_JNIter004_CheckSum(JNIEnv * env,jobject jobj,jstring jstr)35 Java_nsk_stress_jni_JNIter004_CheckSum (JNIEnv *env, jobject jobj, jstring jstr) {
36 
37     unsigned char digest[DIGESTLENGTH];
38     jchar *tmp;
39     static jint upper = 0;
40     jcharArray jArr;
41     int i;
42     const jchar *critstr;
43     char *str;
44     jint len=env->GetStringUTFLength(jstr); CE
45 
46     for (i=0;i<DIGESTLENGTH;i++) {
47       digest[i]=0;
48     }
49     str=(char *)malloc(len*sizeof(char));
50 /*     const char *threadName=env->GetStringUTFChars(jstr, 0); */
51 
52     env->MonitorEnter(jobj); CE
53     if (upper == 0) tmp = (jchar *) malloc(DIGESTLENGTH*sizeof(char));
54     if (env->ExceptionOccurred()) {
55         env->ExceptionDescribe();
56         env->ExceptionClear();
57     }
58     critstr=env->GetStringCritical(jstr, 0); CE
59     for (i=0;i<len;i++)
60     str[i] = (char) critstr[i];
61     env->ReleaseStringCritical(jstr,critstr); CE
62     for (i=0;i<len;i++) {
63         digest[i % DIGESTLENGTH]+=str[i];
64     }
65     free(str);
66 
67     if (env->ExceptionOccurred()) {
68         env->ExceptionDescribe();
69         env->ExceptionClear();
70     }
71     memcpy(tmp,digest,DIGESTLENGTH);
72     jArr=env->NewCharArray(DIGESTLENGTH/sizeof(jchar)); CE
73     len=env->GetArrayLength(jArr); CE
74     env->SetCharArrayRegion(jArr,0,len,tmp); CE
75 /*     ++upper; */
76     env->MonitorExit(jobj); CE
77     return jArr;
78 }
79 
80 JNIEXPORT jboolean JNICALL
Java_nsk_stress_jni_JNIter004_CheckCompare(JNIEnv * env,jobject jobj,jstring jstr,jcharArray cArr,jint limit)81 Java_nsk_stress_jni_JNIter004_CheckCompare (JNIEnv *env, jobject jobj, jstring jstr,
82             jcharArray cArr, jint limit) {
83 
84     unsigned char digest[DIGESTLENGTH];
85     jchar *tmp;
86 /*     jcharArray jArr; */
87     const jchar *critstr;
88     jint strlen;
89     char *str;
90     jboolean ret=JNI_TRUE;
91     int i;
92     static jint upper = 0;
93     jint len;
94     jchar *ch;
95 
96     for (i=0;i<DIGESTLENGTH;i++) {
97       digest[i]=0;
98     }
99     strlen =  env->GetStringUTFLength(jstr); CE
100     str = (char *)malloc(strlen*sizeof(char));
101 
102     len =env->GetArrayLength(cArr); CE
103 
104     env->MonitorEnter(jobj); CE
105     if (upper>limit) {
106     env->MonitorExit(jobj); CE
107     return JNI_FALSE;
108     }
109     tmp=(jchar *)malloc(DIGESTLENGTH*sizeof(char));
110     if (env->ExceptionOccurred()) {
111         env->ExceptionDescribe();
112         env->ExceptionClear();
113     }
114     critstr=env->GetStringCritical(jstr, 0); CE
115     for (i=0;i<strlen;i++)
116     str[i] = (char) critstr[i];
117     env->ReleaseStringCritical(jstr,critstr); CE
118     for (i=0;i<strlen; i++) {
119       digest[i % DIGESTLENGTH]+=str[i % DIGESTLENGTH];
120     }
121 
122     free(str);
123 
124     if (env->ExceptionOccurred()) {
125         env->ExceptionDescribe();
126         env->ExceptionClear();
127     }
128     memcpy(tmp,digest,DIGESTLENGTH);
129 
130 /*     jArr=env->NewCharArray(DIGESTLENGTH/sizeof(jchar)); */
131 /*     len=env->GetArrayLength(jArr); */
132 /*     env->SetCharArrayRegion(jArr,0,len,tmp); */
133 /*     ++upper; */
134 /*     env->MonitorExit(jobj); */
135 
136 /* Compare  */
137 /*     env->MonitorEnter(jobj); */
138 
139     ch=(jchar *)env->GetPrimitiveArrayCritical(cArr,0); CE
140 
141     printf("Comparing: ");
142     for (i=0;i<len;i++)
143     if (ch[i] != tmp[i]) {
144         printf("Error in %d\n",i);
145         printf("ch[%d]=%02x tmp[%d]=%02x\n",i,ch[i],i,tmp[i]);
146         ret=JNI_FALSE;
147     }
148     else {
149         printf("ch[%d]=%02x tmp[%d]=%02x\n",i,ch[i],i,tmp[i]);
150     }
151     printf("\n");
152     env->ReleasePrimitiveArrayCritical(cArr,ch,0); CE
153     ++upper;
154     if (!(upper % 500))
155     fprintf(stderr,"There are %d elements now.\n", upper);
156     if (upper == limit) {
157     jclass clazz;
158     jmethodID methodID;
159     char *name = (char*) "halt";
160     char *sig = (char*) "()V";
161 
162     clazz=env->GetObjectClass(jobj); CE
163     methodID=env->GetStaticMethodID(clazz, name, sig); CE
164     env->CallStaticVoidMethod(clazz, methodID); CE
165     free(tmp);
166     ret=JNI_TRUE;
167     }
168     env->MonitorExit(jobj); CE
169     return ret;
170 }
171 
172 }
173