Fixing the ClassLoader.defineClass() method issue that could not find the necessary...
[jpf-core.git] / src / peers / gov / nasa / jpf / vm / JPF_java_security_MessageDigest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.vm;
19
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.annotation.MJI;
22 import gov.nasa.jpf.vm.MJIEnv;
23 import gov.nasa.jpf.vm.NativePeer;
24
25 import java.security.MessageDigest;
26 import java.security.NoSuchAlgorithmException;
27
28 public class JPF_java_security_MessageDigest extends NativePeer {
29   
30   MessageDigest[] digests;
31   
32   public JPF_java_security_MessageDigest (Config conf){
33     digests = new MessageDigest[32];
34   }
35   
36   int getNewIndex() {
37     int n = digests.length;
38     for (int i=0; i<n; i++){
39       if (digests[i] == null){
40         return i;
41       }
42     }
43     
44     MessageDigest[] newd = new MessageDigest[n + 32];
45     System.arraycopy(digests,0,newd,0,digests.length);
46     digests = newd;
47     return n;
48   }
49   
50   MessageDigest getDigest (MJIEnv env, int objRef){
51     int id = env.getIntField(objRef, "id");
52     return digests[id];
53   }
54   
55   @MJI
56   public int init0__Ljava_lang_String_2__I (MJIEnv env, int objRef, int algRef) {
57     String algorithm = env.getStringObject(algRef);
58     
59     try {
60       MessageDigest md = MessageDigest.getInstance(algorithm);
61     
62       int id = getNewIndex();
63       digests[id] = md;
64     
65       return id;
66     } catch (NoSuchAlgorithmException x){
67       env.throwException("java.security.NoSuchAlgorithmException", algorithm);
68       return -1;
69     }
70   }
71   
72   @MJI
73   public int digest___3B___3B (MJIEnv env, int objRef, int inputRef){
74     MessageDigest md = getDigest(env, objRef);
75     byte[] input = env.getByteArrayObject(inputRef);
76     
77     byte[] res = md.digest(input);
78     return env.newByteArray(res);
79   }
80
81   @MJI
82   public int digest_____3B (MJIEnv env, int objRef){
83     MessageDigest md = getDigest(env, objRef);    
84     byte[] res = md.digest();
85     return env.newByteArray(res);
86   }
87   
88   @MJI
89   public void finalize____ (MJIEnv env, int objRef){
90     int id = env.getIntField(objRef, "id");
91     digests[id] = null;
92   }
93
94   @MJI
95   public void update___3B__V (MJIEnv env, int objRef, int inputRef){
96     MessageDigest md = getDigest(env, objRef);
97     byte[] input = env.getByteArrayObject(inputRef);
98     md.update(input);
99   }
100 }