Loading services/core/java/com/android/server/integrity/IntegrityFileManager.java +10 −12 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.util.Slog; import com.android.internal.annotations.VisibleForTesting; import com.android.server.integrity.model.RuleMetadata; import com.android.server.integrity.parser.RandomAccessObject; import com.android.server.integrity.parser.RuleBinaryParser; import com.android.server.integrity.parser.RuleIndexRange; import com.android.server.integrity.parser.RuleIndexingController; Loading Loading @@ -64,10 +65,8 @@ public class IntegrityFileManager { // update rules atomically. private final File mStagingDir; @Nullable private RuleMetadata mRuleMetadataCache; @Nullable private RuleIndexingController mRuleIndexingController; @Nullable private RuleMetadata mRuleMetadataCache; @Nullable private RuleIndexingController mRuleIndexingController; /** Get the singleton instance of this class. */ public static synchronized IntegrityFileManager getInstance() { Loading Loading @@ -164,13 +163,12 @@ public class IntegrityFileManager { } // Read the rules based on the index information when available. try (FileInputStream inputStream = new FileInputStream(new File(mRulesDir, RULES_FILE))) { List<Rule> rules = mRuleParser.parse(inputStream, ruleReadingIndexes); File ruleFile = new File(mRulesDir, RULES_FILE); List<Rule> rules = mRuleParser.parse(RandomAccessObject.ofFile(ruleFile), ruleReadingIndexes); return rules; } } } /** Read the metadata of the current rules in storage. */ @Nullable Loading services/core/java/com/android/server/integrity/model/BitInputStream.java +17 −34 Original line number Diff line number Diff line Loading @@ -19,26 +19,21 @@ package com.android.server.integrity.model; import java.io.IOException; import java.io.InputStream; /** A wrapper class for reading a stream of bits. */ /** A wrapper class for reading a stream of bits. * * <p>Note: this class reads from underlying stream byte-by-byte. It is advised to apply buffering * to underlying streams. */ public class BitInputStream { private long mBitPointer; private boolean mReadFromStream; private long mBitsRead; private byte[] mRuleBytes; private InputStream mRuleInputStream; private InputStream mInputStream; private byte mCurrentRuleByte; public BitInputStream(byte[] ruleBytes) { this.mRuleBytes = ruleBytes; this.mBitPointer = 0; this.mReadFromStream = false; } private byte mCurrentByte; public BitInputStream(InputStream ruleInputStream) { this.mRuleInputStream = ruleInputStream; this.mReadFromStream = true; public BitInputStream(InputStream inputStream) { mInputStream = inputStream; } /** Loading @@ -52,15 +47,15 @@ public class BitInputStream { int count = 0; while (count++ < numOfBits) { if (mBitPointer % 8 == 0) { mCurrentRuleByte = getNextByte(); if (mBitsRead % 8 == 0) { mCurrentByte = getNextByte(); } int offset = 7 - (int) (mBitPointer % 8); int offset = 7 - (int) (mBitsRead % 8); component <<= 1; component |= (mCurrentRuleByte >>> offset) & 1; component |= (mCurrentByte >>> offset) & 1; mBitPointer++; mBitsRead++; } return component; Loading @@ -68,22 +63,10 @@ public class BitInputStream { /** Check if there are bits left in the stream. */ public boolean hasNext() throws IOException { if (mReadFromStream) { return mRuleInputStream.available() > 0; } else { return mBitPointer / 8 < mRuleBytes.length; } return mInputStream.available() > 0; } private byte getNextByte() throws IOException { if (mReadFromStream) { return (byte) mRuleInputStream.read(); } else { int idx = (int) (mBitPointer / 8); if (idx >= mRuleBytes.length) { throw new IllegalArgumentException(String.format("Invalid byte index: %d", idx)); } return mRuleBytes[idx]; } return (byte) mInputStream.read(); } } services/core/java/com/android/server/integrity/model/BitOutputStream.java +2 −1 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.server.integrity.model; import static com.android.server.integrity.model.ComponentBitSize.BYTE_BITS; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; Loading @@ -24,7 +26,6 @@ import java.util.Arrays; public class BitOutputStream { private static final int BUFFER_SIZE = 4 * 1024; private static final int BYTE_BITS = 8; private int mNextBitIndex; Loading services/core/java/com/android/server/integrity/model/BitTrackedInputStream.javadeleted 100644 → 0 +0 −76 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.server.integrity.model; import java.io.IOException; import java.io.InputStream; /** * An input stream that tracks the total number read bytes since construction and allows moving * fast forward to a certain byte any time during the execution. * * This class is used for efficient reading of rules based on the rule indexing. */ public class BitTrackedInputStream extends BitInputStream { private int mReadBitsCount; /** Constructor with byte array. */ public BitTrackedInputStream(byte[] inputStream) { super(inputStream); mReadBitsCount = 0; } /** Constructor with input stream. */ public BitTrackedInputStream(InputStream inputStream) { super(inputStream); mReadBitsCount = 0; } /** Obtains an integer value of the next {@code numOfBits}. */ @Override public int getNext(int numOfBits) throws IOException { mReadBitsCount += numOfBits; return super.getNext(numOfBits); } /** Returns the current cursor position showing the number of bits that are read. */ public int getReadBitsCount() { return mReadBitsCount; } /** * Returns true if we can read more rules by checking whether the end index is not reached yet. */ public boolean canReadMoreRules(int endIndexBytes) { return mReadBitsCount < endIndexBytes * 8; } /** * Sets the cursor to the specified byte location. * * Note that the integer parameter specifies the location in bytes -- not bits. */ public void setCursorToByteLocation(int byteLocation) throws IOException { int bitCountToRead = byteLocation * 8 - mReadBitsCount; if (bitCountToRead < 0) { throw new IllegalStateException("The byte position is already read."); } super.getNext(bitCountToRead); mReadBitsCount = byteLocation * 8; } } services/core/java/com/android/server/integrity/model/ByteTrackedOutputStream.java +2 −5 Original line number Diff line number Diff line Loading @@ -27,8 +27,6 @@ import java.io.OutputStream; */ public class ByteTrackedOutputStream extends OutputStream { private static final int INT_BYTES = 4; private int mWrittenBytesCount; private final OutputStream mOutputStream; Loading @@ -39,7 +37,7 @@ public class ByteTrackedOutputStream extends OutputStream { @Override public void write(int b) throws IOException { mWrittenBytesCount += INT_BYTES; mWrittenBytesCount++; mOutputStream.write(b); } Loading @@ -49,8 +47,7 @@ public class ByteTrackedOutputStream extends OutputStream { */ @Override public void write(byte[] bytes) throws IOException { mWrittenBytesCount += bytes.length; mOutputStream.write(bytes); write(bytes, 0, bytes.length); } @Override Loading Loading
services/core/java/com/android/server/integrity/IntegrityFileManager.java +10 −12 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.util.Slog; import com.android.internal.annotations.VisibleForTesting; import com.android.server.integrity.model.RuleMetadata; import com.android.server.integrity.parser.RandomAccessObject; import com.android.server.integrity.parser.RuleBinaryParser; import com.android.server.integrity.parser.RuleIndexRange; import com.android.server.integrity.parser.RuleIndexingController; Loading Loading @@ -64,10 +65,8 @@ public class IntegrityFileManager { // update rules atomically. private final File mStagingDir; @Nullable private RuleMetadata mRuleMetadataCache; @Nullable private RuleIndexingController mRuleIndexingController; @Nullable private RuleMetadata mRuleMetadataCache; @Nullable private RuleIndexingController mRuleIndexingController; /** Get the singleton instance of this class. */ public static synchronized IntegrityFileManager getInstance() { Loading Loading @@ -164,13 +163,12 @@ public class IntegrityFileManager { } // Read the rules based on the index information when available. try (FileInputStream inputStream = new FileInputStream(new File(mRulesDir, RULES_FILE))) { List<Rule> rules = mRuleParser.parse(inputStream, ruleReadingIndexes); File ruleFile = new File(mRulesDir, RULES_FILE); List<Rule> rules = mRuleParser.parse(RandomAccessObject.ofFile(ruleFile), ruleReadingIndexes); return rules; } } } /** Read the metadata of the current rules in storage. */ @Nullable Loading
services/core/java/com/android/server/integrity/model/BitInputStream.java +17 −34 Original line number Diff line number Diff line Loading @@ -19,26 +19,21 @@ package com.android.server.integrity.model; import java.io.IOException; import java.io.InputStream; /** A wrapper class for reading a stream of bits. */ /** A wrapper class for reading a stream of bits. * * <p>Note: this class reads from underlying stream byte-by-byte. It is advised to apply buffering * to underlying streams. */ public class BitInputStream { private long mBitPointer; private boolean mReadFromStream; private long mBitsRead; private byte[] mRuleBytes; private InputStream mRuleInputStream; private InputStream mInputStream; private byte mCurrentRuleByte; public BitInputStream(byte[] ruleBytes) { this.mRuleBytes = ruleBytes; this.mBitPointer = 0; this.mReadFromStream = false; } private byte mCurrentByte; public BitInputStream(InputStream ruleInputStream) { this.mRuleInputStream = ruleInputStream; this.mReadFromStream = true; public BitInputStream(InputStream inputStream) { mInputStream = inputStream; } /** Loading @@ -52,15 +47,15 @@ public class BitInputStream { int count = 0; while (count++ < numOfBits) { if (mBitPointer % 8 == 0) { mCurrentRuleByte = getNextByte(); if (mBitsRead % 8 == 0) { mCurrentByte = getNextByte(); } int offset = 7 - (int) (mBitPointer % 8); int offset = 7 - (int) (mBitsRead % 8); component <<= 1; component |= (mCurrentRuleByte >>> offset) & 1; component |= (mCurrentByte >>> offset) & 1; mBitPointer++; mBitsRead++; } return component; Loading @@ -68,22 +63,10 @@ public class BitInputStream { /** Check if there are bits left in the stream. */ public boolean hasNext() throws IOException { if (mReadFromStream) { return mRuleInputStream.available() > 0; } else { return mBitPointer / 8 < mRuleBytes.length; } return mInputStream.available() > 0; } private byte getNextByte() throws IOException { if (mReadFromStream) { return (byte) mRuleInputStream.read(); } else { int idx = (int) (mBitPointer / 8); if (idx >= mRuleBytes.length) { throw new IllegalArgumentException(String.format("Invalid byte index: %d", idx)); } return mRuleBytes[idx]; } return (byte) mInputStream.read(); } }
services/core/java/com/android/server/integrity/model/BitOutputStream.java +2 −1 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.server.integrity.model; import static com.android.server.integrity.model.ComponentBitSize.BYTE_BITS; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; Loading @@ -24,7 +26,6 @@ import java.util.Arrays; public class BitOutputStream { private static final int BUFFER_SIZE = 4 * 1024; private static final int BYTE_BITS = 8; private int mNextBitIndex; Loading
services/core/java/com/android/server/integrity/model/BitTrackedInputStream.javadeleted 100644 → 0 +0 −76 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.server.integrity.model; import java.io.IOException; import java.io.InputStream; /** * An input stream that tracks the total number read bytes since construction and allows moving * fast forward to a certain byte any time during the execution. * * This class is used for efficient reading of rules based on the rule indexing. */ public class BitTrackedInputStream extends BitInputStream { private int mReadBitsCount; /** Constructor with byte array. */ public BitTrackedInputStream(byte[] inputStream) { super(inputStream); mReadBitsCount = 0; } /** Constructor with input stream. */ public BitTrackedInputStream(InputStream inputStream) { super(inputStream); mReadBitsCount = 0; } /** Obtains an integer value of the next {@code numOfBits}. */ @Override public int getNext(int numOfBits) throws IOException { mReadBitsCount += numOfBits; return super.getNext(numOfBits); } /** Returns the current cursor position showing the number of bits that are read. */ public int getReadBitsCount() { return mReadBitsCount; } /** * Returns true if we can read more rules by checking whether the end index is not reached yet. */ public boolean canReadMoreRules(int endIndexBytes) { return mReadBitsCount < endIndexBytes * 8; } /** * Sets the cursor to the specified byte location. * * Note that the integer parameter specifies the location in bytes -- not bits. */ public void setCursorToByteLocation(int byteLocation) throws IOException { int bitCountToRead = byteLocation * 8 - mReadBitsCount; if (bitCountToRead < 0) { throw new IllegalStateException("The byte position is already read."); } super.getNext(bitCountToRead); mReadBitsCount = byteLocation * 8; } }
services/core/java/com/android/server/integrity/model/ByteTrackedOutputStream.java +2 −5 Original line number Diff line number Diff line Loading @@ -27,8 +27,6 @@ import java.io.OutputStream; */ public class ByteTrackedOutputStream extends OutputStream { private static final int INT_BYTES = 4; private int mWrittenBytesCount; private final OutputStream mOutputStream; Loading @@ -39,7 +37,7 @@ public class ByteTrackedOutputStream extends OutputStream { @Override public void write(int b) throws IOException { mWrittenBytesCount += INT_BYTES; mWrittenBytesCount++; mOutputStream.write(b); } Loading @@ -49,8 +47,7 @@ public class ByteTrackedOutputStream extends OutputStream { */ @Override public void write(byte[] bytes) throws IOException { mWrittenBytesCount += bytes.length; mOutputStream.write(bytes); write(bytes, 0, bytes.length); } @Override Loading