Merge "Revert "Migrate libcore code to new classes libcore.util.Mutable{Int,Long}.""
diff --git a/JavaLibrary.bp b/JavaLibrary.bp
index ded08c9..5d13615 100644
--- a/JavaLibrary.bp
+++ b/JavaLibrary.bp
@@ -144,6 +144,39 @@
     ],
 }
 
+// A guaranteed unstripped version of core-oj and core-libart.
+// The build system may or may not strip the core-oj and core-libart jars,
+// but these will not be stripped. See b/24535627.
+java_library {
+    name: "core-oj-testdex",
+    static_libs: ["core-oj"],
+    no_standard_libs: true,
+    libs: ["core-all"],
+    system_modules: "core-all-system-modules",
+    dxflags: ["--core-library"],
+    dex_preopt: false,
+    notice: "ojluni/NOTICE",
+    required: [
+        "tzdata",
+        "tzlookup.xml",
+    ],
+}
+
+java_library {
+    name: "core-libart-testdex",
+    static_libs: ["core-libart"],
+    no_standard_libs: true,
+    libs: ["core-all"],
+    system_modules: "core-all-system-modules",
+    dxflags: ["--core-library"],
+    dex_preopt: false,
+    notice: "ojluni/NOTICE",
+    required: [
+        "tzdata",
+        "tzlookup.xml",
+    ],
+}
+
 // A library that exists to satisfy javac when
 // compiling source code that contains lambdas.
 java_library {
@@ -175,3 +208,41 @@
         "core-lambda-stubs",
     ],
 }
+
+// Build libcore test rules
+java_library_static {
+    name: "core-test-rules",
+    hostdex: true,
+    no_framework_libs: true,
+    srcs: [
+        "dalvik/test-rules/src/main/**/*.java",
+        "test-rules/src/main/**/*.java",
+    ],
+    static_libs: ["junit"],
+}
+
+// Make the core-tests-support library.
+java_library_static {
+    name: "core-tests-support",
+    hostdex: true,
+    no_framework_libs: true,
+    srcs: ["support/src/test/java/**/*.java"],
+    libs: [
+        "junit",
+        "bouncycastle",
+    ],
+    static_libs: [
+        "bouncycastle-bcpkix",
+        "bouncycastle-ocsp",
+    ],
+}
+
+// Make the jsr166-tests library.
+java_library_static {
+    name: "jsr166-tests",
+    srcs: ["jsr166-tests/src/test/java/**/*.java"],
+    no_framework_libs: true,
+    libs: [
+        "junit",
+    ],
+}
diff --git a/JavaLibrary.mk b/JavaLibrary.mk
index 4ebf2d8..9aaf534 100644
--- a/JavaLibrary.mk
+++ b/JavaLibrary.mk
@@ -92,60 +92,6 @@
 #
 
 ifeq ($(LIBCORE_SKIP_TESTS),)
-# A guaranteed unstripped version of core-oj and core-libart.
-# The build system may or may not strip the core-oj and core-libart jars,
-# but these will not be stripped. See b/24535627.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(patsubst $(LOCAL_PATH)/%,%,$(openjdk_java_files))
-LOCAL_JAVA_RESOURCE_DIRS := $(core_resource_dirs)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_JAVACFLAGS := $(local_javac_flags)
-LOCAL_JACK_FLAGS := $(local_jack_flags)
-LOCAL_DX_FLAGS := --core-library
-LOCAL_MODULE_TAGS := optional
-LOCAL_DEX_PREOPT := false
-LOCAL_MODULE := core-oj-testdex
-LOCAL_JAVA_LIBRARIES := core-all
-LOCAL_NOTICE_FILE := $(LOCAL_PATH)/ojluni/NOTICE
-LOCAL_REQUIRED_MODULES := tzdata tzlookup.xml
-LOCAL_CORE_LIBRARY := true
-include $(BUILD_JAVA_LIBRARY)
-
-# Build libcore test rules for target
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(call all-java-files-under, dalvik/test-rules/src/main test-rules/src/main)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_MODULE := core-test-rules
-LOCAL_JAVA_LIBRARIES := core-all
-LOCAL_STATIC_JAVA_LIBRARIES := junit
-include $(BUILD_STATIC_JAVA_LIBRARY)
-
-# Build libcore test rules for host
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(call all-java-files-under, dalvik/test-rules/src/main test-rules/src/main)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_MODULE := core-test-rules-hostdex
-LOCAL_JAVA_LIBRARIES := core-oj-hostdex core-libart-hostdex
-LOCAL_STATIC_JAVA_LIBRARIES := junit-hostdex
-include $(BUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(patsubst $(LOCAL_PATH)/%,%,$(non_openjdk_java_files) $(android_icu4j_src_files))
-LOCAL_JAVA_RESOURCE_DIRS := $(android_icu4j_resource_dirs)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_JAVACFLAGS := $(local_javac_flags)
-LOCAL_JACK_FLAGS := $(local_jack_flags)
-LOCAL_DX_FLAGS := --core-library
-LOCAL_MODULE_TAGS := optional
-LOCAL_DEX_PREOPT := false
-LOCAL_MODULE := core-libart-testdex
-LOCAL_JAVA_LIBRARIES := core-all
-LOCAL_CORE_LIBRARY := true
-LOCAL_REQUIRED_MODULES := tzdata tzlookup.xml
-include $(BUILD_JAVA_LIBRARY)
-endif
-
-ifeq ($(LIBCORE_SKIP_TESTS),)
 # Build a library just containing files from luni/src/test/filesystems for use in tests.
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := $(call all-java-files-under, luni/src/test/filesystems/src)
@@ -189,33 +135,6 @@
 include $(BUILD_STATIC_JAVA_LIBRARY)
 endif
 
-ifeq ($(LIBCORE_SKIP_TESTS),)
-# Make the core-tests-support library.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(call all-test-java-files-under,support)
-LOCAL_JAVA_RESOURCE_DIRS := $(test_resource_dirs)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_JAVA_LIBRARIES := core-oj core-libart junit bouncycastle
-LOCAL_STATIC_JAVA_LIBRARIES := bouncycastle-bcpkix bouncycastle-ocsp
-LOCAL_JAVACFLAGS := $(local_javac_flags)
-LOCAL_JACK_FLAGS := $(local_jack_flags)
-LOCAL_MODULE := core-tests-support
-include $(BUILD_STATIC_JAVA_LIBRARY)
-endif
-
-ifeq ($(LIBCORE_SKIP_TESTS),)
-# Make the jsr166-tests library.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES :=  $(call all-test-java-files-under, jsr166-tests)
-LOCAL_JAVA_RESOURCE_DIRS := $(test_resource_dirs)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_JAVA_LIBRARIES := core-oj core-libart junit
-LOCAL_JAVACFLAGS := $(local_javac_flags)
-LOCAL_JACK_FLAGS := $(local_jack_flags)
-LOCAL_MODULE := jsr166-tests
-include $(BUILD_STATIC_JAVA_LIBRARY)
-endif
-
 # Make the core-ojtests library.
 ifeq ($(LIBCORE_SKIP_TESTS),)
     include $(CLEAR_VARS)
@@ -293,26 +212,6 @@
     include $(BUILD_HOST_DALVIK_JAVA_LIBRARY)
 endif
 
-# Make the core-tests-support library.
-ifeq ($(LIBCORE_SKIP_TESTS),)
-    include $(CLEAR_VARS)
-    LOCAL_SRC_FILES := $(call all-test-java-files-under,support)
-    LOCAL_JAVA_RESOURCE_DIRS := $(test_resource_dirs)
-    LOCAL_NO_STANDARD_LIBRARIES := true
-    LOCAL_JAVA_LIBRARIES := \
-        bouncycastle-hostdex \
-        core-libart-hostdex \
-        core-oj-hostdex \
-        junit-hostdex
-    LOCAL_STATIC_JAVA_LIBRARIES := \
-        bouncycastle-bcpkix-hostdex \
-        bouncycastle-ocsp-hostdex
-    LOCAL_JAVACFLAGS := $(local_javac_flags)
-    LOCAL_MODULE_TAGS := optional
-    LOCAL_MODULE := core-tests-support-hostdex
-    include $(BUILD_HOST_DALVIK_JAVA_LIBRARY)
-endif
-
 # Make the core-ojtests-hostdex library.
 ifeq ($(LIBCORE_SKIP_TESTS),)
     include $(CLEAR_VARS)
diff --git a/ojluni/src/main/java/sun/security/jca/JCAUtil.java b/ojluni/src/main/java/sun/security/jca/JCAUtil.java
index fde9be5..59e7cbf 100644
--- a/ojluni/src/main/java/sun/security/jca/JCAUtil.java
+++ b/ojluni/src/main/java/sun/security/jca/JCAUtil.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
diff --git a/ojluni/src/main/java/sun/security/pkcs/PKCS7.java b/ojluni/src/main/java/sun/security/pkcs/PKCS7.java
index f28bea7..bf3e8f8 100644
--- a/ojluni/src/main/java/sun/security/pkcs/PKCS7.java
+++ b/ojluni/src/main/java/sun/security/pkcs/PKCS7.java
@@ -241,6 +241,7 @@
     private void parseNetscapeCertChain(DerValue val)
     throws ParsingException, IOException {
         DerInputStream dis = new DerInputStream(val.toByteArray());
+        // Android-changed: Maintain original encoded form.
         DerValue[] contents = dis.getSequence(2, true);
         certificates = new X509Certificate[contents.length];
 
@@ -254,6 +255,7 @@
         for (int i=0; i < contents.length; i++) {
             ByteArrayInputStream bais = null;
             try {
+                // BEGIN Android-changed: Maintain original encoded form.
                 byte[] original = contents[i].getOriginalEncodedForm();
                 if (certfac == null)
                     certificates[i] = new X509CertImpl(contents[i], original);
@@ -265,6 +267,7 @@
                     bais.close();
                     bais = null;
                 }
+                // END Android-changed: Maintain original encoded form.
             } catch (CertificateException ce) {
                 ParsingException pe = new ParsingException(ce.getMessage());
                 pe.initCause(ce);
@@ -320,6 +323,7 @@
          * (certificates are OPTIONAL)
          */
         if ((byte)(dis.peekByte()) == (byte)0xA0) {
+            // Android-changed: Maintain original encoded form.
             DerValue[] certVals = dis.getSet(2, true, true);
 
             len = certVals.length;
@@ -333,6 +337,7 @@
                     // We only parse the normal certificate. Other types of
                     // CertificateChoices ignored.
                     if (tag == DerValue.tag_Sequence) {
+                        // BEGIN Android-changed: Maintain original encoded form.
                         byte[] original = certVals[i].getOriginalEncodedForm();
                         if (certfac == null) {
                             certificates[count] = new X509CertImpl(certVals[i], original);
@@ -344,6 +349,7 @@
                             bais.close();
                             bais = null;
                         }
+                        // END Android-changed: Maintain original encoded form.
                         count++;
                     }
                 } catch (CertificateException ce) {
@@ -443,6 +449,7 @@
         } catch (CertificateException ce) {
             // do nothing
         }
+        // Android-changed: Maintain original encoded form.
         DerValue[] certVals = dis.getSet(2, false, true);
         len = certVals.length;
         certificates = new X509Certificate[len];
@@ -450,6 +457,7 @@
         for (int i = 0; i < len; i++) {
             ByteArrayInputStream bais = null;
             try {
+                // BEGIN Android-changed: Maintain original encoded form.
                 byte[] original = certVals[i].getOriginalEncodedForm();
                 if (certfac == null)
                     certificates[i] = new X509CertImpl(certVals[i], original);
@@ -461,6 +469,7 @@
                     bais.close();
                     bais = null;
                 }
+                // END Android-changed: Maintain original encoded form.
             } catch (CertificateException ce) {
                 ParsingException pe = new ParsingException(ce.getMessage());
                 pe.initCause(ce);
@@ -594,6 +603,8 @@
         return info.verify(this, bytes);
     }
 
+    // BEGIN Android-added: Add overload that takes an InputStream.
+    // This is used by android.os.RecoverySystem
     /**
      * This verifies a given SignerInfo.
      *
@@ -607,6 +618,7 @@
     throws NoSuchAlgorithmException, SignatureException, IOException {
         return info.verify(this, dataInputStream);
     }
+    // END Android-added: Add overload that takes an InputStream.
 
     /**
      * Returns all signerInfos which self-verify.
@@ -802,7 +814,7 @@
         return this.oldStyle;
     }
 
-    // BEGIN Android-added
+    // BEGIN Android-added: Add subclass that returns the original encoded bytes.
     /**
      * For legacy reasons we need to return exactly the original encoded certificate bytes, instead
      * of letting the underlying implementation have a shot at re-encoding the data.
@@ -994,7 +1006,7 @@
             wrapped.verify(key, sigProvider);
         }
     }
-    // END Android-added
+    // END Android-added: Add subclass that returns the original encoded bytes.
 
     // BEGIN Android-removed: unused in Android
     /**
@@ -1022,13 +1034,13 @@
      *         data message.
      *
     public static byte[] generateSignedData(byte[] signature,
-            X509Certificate[] signerChain,
-            byte[] content,
-            String signatureAlgorithm,
-            URI tsaURI,
-            String tSAPolicyID,
-            String tSADigestAlg)
-            throws CertificateException, IOException, NoSuchAlgorithmException
+                                            X509Certificate[] signerChain,
+                                            byte[] content,
+                                            String signatureAlgorithm,
+                                            URI tsaURI,
+                                            String tSAPolicyID,
+                                            String tSADigestAlg)
+        throws CertificateException, IOException, NoSuchAlgorithmException
     {
 
         // Generate the timestamp token
@@ -1042,32 +1054,32 @@
             // Insert the timestamp token into the PKCS #7 signer info element
             // (as an unsigned attribute)
             unauthAttrs =
-                    new PKCS9Attributes(new PKCS9Attribute[]{
-                            new PKCS9Attribute(
-                                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
-                                    tsToken)});
+                new PKCS9Attributes(new PKCS9Attribute[]{
+                    new PKCS9Attribute(
+                        PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
+                        tsToken)});
         }
 
         // Create the SignerInfo
         X500Name issuerName =
-                X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
+            X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
         BigInteger serialNumber = signerChain[0].getSerialNumber();
         String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
         String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
         SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
-                AlgorithmId.get(digAlg), null,
-                AlgorithmId.get(encAlg),
-                signature, unauthAttrs);
+                                               AlgorithmId.get(digAlg), null,
+                                               AlgorithmId.get(encAlg),
+                                               signature, unauthAttrs);
 
         // Create the PKCS #7 signed data message
         SignerInfo[] signerInfos = {signerInfo};
         AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
         // Include or exclude content
         ContentInfo contentInfo = (content == null)
-                ? new ContentInfo(ContentInfo.DATA_OID, null)
-                : new ContentInfo(content);
+            ? new ContentInfo(ContentInfo.DATA_OID, null)
+            : new ContentInfo(content);
         PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
-                signerChain, signerInfos);
+                                signerChain, signerInfos);
         ByteArrayOutputStream p7out = new ByteArrayOutputStream();
         pkcs7.encodeSignedData(p7out);
 
@@ -1094,10 +1106,10 @@
      *                     certificate is not permitted for timestamping.
      *
     private static byte[] generateTimestampToken(Timestamper tsa,
-            String tSAPolicyID,
-            String tSADigestAlg,
-            byte[] toBeTimestamped)
-            throws IOException, CertificateException
+                                                 String tSAPolicyID,
+                                                 String tSADigestAlg,
+                                                 byte[] toBeTimestamped)
+        throws IOException, CertificateException
     {
         // Generate a timestamp
         MessageDigest messageDigest = null;
@@ -1122,8 +1134,8 @@
         // Handle TSP error
         if (status != 0 && status != 1) {
             throw new IOException("Error generating timestamp: " +
-                    tsReply.getStatusCodeAsText() + " " +
-                    tsReply.getFailureCodeAsText());
+                tsReply.getStatusCodeAsText() + " " +
+                tsReply.getFailureCodeAsText());
         }
 
         if (tSAPolicyID != null &&
@@ -1143,7 +1155,7 @@
             throw new IllegalArgumentException();   // should have been caught before
         }
         if (!MessageDigest.isEqual(tst.getHashedMessage(),
-                tsQuery.getHashedMessage())) {
+                                   tsQuery.getHashedMessage())) {
             throw new IOException("Digest octets changed in timestamp token");
         }
 
@@ -1161,18 +1173,18 @@
             if (cert == null) {
                 // Error, we've already set tsRequestCertificate = true
                 throw new CertificateException(
-                        "Certificate not included in timestamp token");
+                "Certificate not included in timestamp token");
             } else {
                 if (!cert.getCriticalExtensionOIDs().contains(
                         EXTENDED_KEY_USAGE_OID)) {
                     throw new CertificateException(
-                            "Certificate is not valid for timestamping");
+                    "Certificate is not valid for timestamping");
                 }
                 List<String> keyPurposes = cert.getExtendedKeyUsage();
                 if (keyPurposes == null ||
                         !keyPurposes.contains(KP_TIMESTAMPING_OID)) {
                     throw new CertificateException(
-                            "Certificate is not valid for timestamping");
+                    "Certificate is not valid for timestamping");
                 }
             }
         }
diff --git a/tools/docs/crypto/run_update_crypto_support.sh b/tools/docs/crypto/run_update_crypto_support.sh
index 501891c..d389c3a 100755
--- a/tools/docs/crypto/run_update_crypto_support.sh
+++ b/tools/docs/crypto/run_update_crypto_support.sh
@@ -14,4 +14,4 @@
 fi
 
 make -j48 vogar dx
-vogar --mode=activity --toolchain=jdk --multidex=false libcore/tools/docs/crypto/src/java/libcore/java/security/ListProviders.java | libcore/tools/docs/crypto/update_crypto_support.py --api_level=$1 --rewrite_file libcore/tools/docs/crypto/data/crypto_support.json
+vogar --mode=activity --multidex=false libcore/tools/docs/crypto/src/java/libcore/java/security/ListProviders.java | libcore/tools/docs/crypto/update_crypto_support.py --api_level=$1 --rewrite_file libcore/tools/docs/crypto/data/crypto_support.json