package com.oracle.truffle.tools.chromeinspector.types;
import java.text.MessageFormat;
import com.oracle.truffle.api.source.Source;
public final class Script {
private final int id;
private final String url;
private final Source source;
public Script(int id, String url, Source source) {
this.id = id;
this.url = url;
this.source = source;
}
public int getId() {
return id;
}
public String getUrl() {
return url;
}
public Source getSource() {
return source;
}
public CharSequence getCharacters() {
if (source.hasCharacters()) {
return source.getCharacters();
} else {
return MessageFormat.format("Can not load source from {0}\n" +
"Please use the --inspect.SourcePath option to point to the source locations.\n" +
"Example: --inspect.SourcePath=/home/joe/project/src\n", url);
}
}
public String getHash() {
CharSequence code = getCharacters();
long[] p = {0xd8b862fd, 0xd950f97f, 0xeb329c71, 0xf71e5e6b, 0xfab1e57b};
long[] random = {0xa5f881ccL, 0x63fff827L, 0x9568f4cbL, 0x1a2e3318L, 0x2af3fbd1L};
int[] randomOdd = {0x3713d83b, 0x19033ac5, 0xe847047d, 0xcde9ca1f, 0x058bf00b};
int numHashes = 5;
long[] hashes = new long[numHashes];
long[] zi = new long[]{1, 1, 1, 1, 1};
int current = 0;
int l4 = code.length() / 4;
for (int i = 0; i < l4; i += 4) {
int v = code.charAt(i);
long xi = (v * randomOdd[current]) & 0x7FFFFFFF;
hashes[current] = (hashes[current] + zi[current] * xi) % p[current];
zi[current] = (zi[current] * random[current]) % p[current];
current = current == numHashes - 1 ? 0 : current + 1;
}
if ((code.length() % 4) != 0) {
int v = 0;
for (int i = l4; i < code.length(); i++) {
v <<= 8;
v |= code.charAt(i);
}
long xi = (v * randomOdd[current]) & 0x7FFFFFFF;
hashes[current] = (hashes[current] + zi[current] * xi) % p[current];
zi[current] = (zi[current] * random[current]) % p[current];
}
for (int i = 0; i < numHashes; i++) {
hashes[i] = (hashes[i] + zi[i] * (p[i] - 1)) % p[i];
}
StringBuilder hash = new StringBuilder();
for (int i = 0; i < numHashes; i++) {
hash.append(Integer.toHexString((int) hashes[i]));
}
return hash.toString();
}
}