1*795d594fSAndroid Build Coastguard Worker#!/bin/bash 2*795d594fSAndroid Build Coastguard Worker# 3*795d594fSAndroid Build Coastguard Worker# Copyright 2019 The Android Open Source Project 4*795d594fSAndroid Build Coastguard Worker# 5*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*795d594fSAndroid Build Coastguard Worker# 9*795d594fSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*795d594fSAndroid Build Coastguard Worker# 11*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*795d594fSAndroid Build Coastguard Worker# limitations under the License. 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker# This script generates dex files which all contain an empty class called 18*795d594fSAndroid Build Coastguard Worker# MyClassXX, where XX is the ID of the dex file. It prints the first file 19*795d594fSAndroid Build Coastguard Worker# (ID=01) and a list of checksum/signature bytes for all of the dex files 20*795d594fSAndroid Build Coastguard Worker# (ID 01 through NUM_FILES). 21*795d594fSAndroid Build Coastguard Worker# The idea is that the associated test will be able to efficiently generate 22*795d594fSAndroid Build Coastguard Worker# up to NUM_FILES dex files from the first dex file by substituting its 23*795d594fSAndroid Build Coastguard Worker# checksum/signature bytes with that of the dex file of a given ID. Note that 24*795d594fSAndroid Build Coastguard Worker# it is also necessary to replace the ID in the class descriptor, but this 25*795d594fSAndroid Build Coastguard Worker# script does not help with that. 26*795d594fSAndroid Build Coastguard Worker 27*795d594fSAndroid Build Coastguard Workerset -e 28*795d594fSAndroid Build Coastguard WorkerTMP=`mktemp -d` 29*795d594fSAndroid Build Coastguard WorkerNUM_FILES=30 30*795d594fSAndroid Build Coastguard Worker 31*795d594fSAndroid Build Coastguard Workerecho ' private static final byte[][] DEX_CHECKSUMS = new byte[][] {' 32*795d594fSAndroid Build Coastguard Workerfor i in $(seq 1 ${NUM_FILES}); do 33*795d594fSAndroid Build Coastguard Worker if [ ${i} -lt 10 ]; then 34*795d594fSAndroid Build Coastguard Worker suffix=0${i} 35*795d594fSAndroid Build Coastguard Worker else 36*795d594fSAndroid Build Coastguard Worker suffix=${i} 37*795d594fSAndroid Build Coastguard Worker fi 38*795d594fSAndroid Build Coastguard Worker (cd "$TMP" && \ 39*795d594fSAndroid Build Coastguard Worker echo "public class MyClass${suffix} { }" > "$TMP/MyClass${suffix}.java" && \ 40*795d594fSAndroid Build Coastguard Worker javac -d "${TMP}" "$TMP/MyClass${suffix}.java" && \ 41*795d594fSAndroid Build Coastguard Worker d8 --output "$TMP" "$TMP/MyClass${suffix}.class" && \ 42*795d594fSAndroid Build Coastguard Worker mv "$TMP/classes.dex" "$TMP/file${suffix}.dex") 43*795d594fSAndroid Build Coastguard Worker 44*795d594fSAndroid Build Coastguard Worker # Dump bytes 8-32 (checksum + signature) that need to change for other files. 45*795d594fSAndroid Build Coastguard Worker checksum=`head -c 32 -z "$TMP/file${suffix}.dex" | tail -c 24 -z | base64` 46*795d594fSAndroid Build Coastguard Worker echo ' Base64.getDecoder().decode("'${checksum}'"),' 47*795d594fSAndroid Build Coastguard Workerdone 48*795d594fSAndroid Build Coastguard Workerecho ' };' 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker# Dump first dex file as base. 51*795d594fSAndroid Build Coastguard Workerecho ' private static final byte[] DEX_BYTES_01 = Base64.getDecoder().decode(' 52*795d594fSAndroid Build Coastguard Workerbase64 "${TMP}/file01.dex" | sed -E 's/^/ "/' | sed ':a;N;$!ba;s/\n/" +\n/g' | sed -E '$ s/$/");/' 53*795d594fSAndroid Build Coastguard Worker 54*795d594fSAndroid Build Coastguard Workerrm -rf "$TMP" 55