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