Loading services/core/java/com/android/server/integrity/model/BitInputStream.javadeleted 100644 → 0 +0 −72 Original line number Diff line number Diff line /* * Copyright (C) 2019 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; /** 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 mBitsRead; private InputStream mInputStream; private byte mCurrentByte; public BitInputStream(InputStream inputStream) { mInputStream = inputStream; } /** * Read the next number of bits from the stream. * * @param numOfBits The number of bits to read. * @return The value read from the stream. */ public int getNext(int numOfBits) throws IOException { int component = 0; int count = 0; while (count++ < numOfBits) { if (mBitsRead % 8 == 0) { mCurrentByte = getNextByte(); } int offset = 7 - (int) (mBitsRead % 8); component <<= 1; component |= (mCurrentByte >>> offset) & 1; mBitsRead++; } return component; } /** Check if there are bits left in the stream. */ public boolean hasNext() throws IOException { return mInputStream.available() > 0; } private byte getNextByte() throws IOException { return (byte) mInputStream.read(); } } services/core/java/com/android/server/integrity/model/BitOutputStream.javadeleted 100644 → 0 +0 −105 Original line number Diff line number Diff line /* * Copyright (C) 2019 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 static com.android.server.integrity.model.ComponentBitSize.BYTE_BITS; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; /** A wrapper class for writing a stream of bits. */ public class BitOutputStream { private static final int BUFFER_SIZE = 4 * 1024; private int mNextBitIndex; private final OutputStream mOutputStream; private final byte[] mBuffer; public BitOutputStream(OutputStream outputStream) { mBuffer = new byte[BUFFER_SIZE]; mNextBitIndex = 0; mOutputStream = outputStream; } /** * Set the next number of bits in the stream to value. * * @param numOfBits The number of bits used to represent the value. * @param value The value to convert to bits. */ public void setNext(int numOfBits, int value) throws IOException { if (numOfBits <= 0) { return; } // optional: we can do some clever size checking to "OR" an entire segment of bits instead // of setting bits one by one, but it is probably not worth it. int nextBitMask = 1 << (numOfBits - 1); while (numOfBits-- > 0) { setNext((value & nextBitMask) != 0); nextBitMask >>>= 1; } } /** * Set the next bit in the stream to value. * * @param value The value to set the bit to */ public void setNext(boolean value) throws IOException { int byteToWrite = mNextBitIndex / BYTE_BITS; if (byteToWrite == BUFFER_SIZE) { mOutputStream.write(mBuffer); reset(); byteToWrite = 0; } if (value) { mBuffer[byteToWrite] |= 1 << (BYTE_BITS - 1 - (mNextBitIndex % BYTE_BITS)); } mNextBitIndex++; } /** Set the next bit in the stream to true. */ public void setNext() throws IOException { setNext(/* value= */ true); } /** * Flush the data written to the underlying {@link java.io.OutputStream}. Any unfinished bytes * will be padded with 0. */ public void flush() throws IOException { int endByte = mNextBitIndex / BYTE_BITS; if (mNextBitIndex % BYTE_BITS != 0) { // If next bit is not the first bit of a byte, then mNextBitIndex / BYTE_BITS would be // the byte that includes already written bits. We need to increment it so this byte // gets written. endByte++; } mOutputStream.write(mBuffer, 0, endByte); reset(); } /** Reset this output stream to start state. */ private void reset() { mNextBitIndex = 0; Arrays.fill(mBuffer, (byte) 0); } } services/core/java/com/android/server/integrity/model/ByteTrackedOutputStream.javadeleted 100644 → 0 +0 −63 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.OutputStream; /** * An output stream that tracks the total number written bytes since construction and allows * querying this value any time during the execution. * * <p>This class is used for constructing the rule indexing. */ public class ByteTrackedOutputStream extends OutputStream { private int mWrittenBytesCount; private final OutputStream mOutputStream; public ByteTrackedOutputStream(OutputStream outputStream) { mWrittenBytesCount = 0; mOutputStream = outputStream; } @Override public void write(int b) throws IOException { mWrittenBytesCount++; mOutputStream.write(b); } /** * Writes the given bytes into the output stream provided in constructor and updates the total * number of written bytes. */ @Override public void write(byte[] bytes) throws IOException { write(bytes, 0, bytes.length); } @Override public void write(byte[] b, int off, int len) throws IOException { mWrittenBytesCount += len; mOutputStream.write(b, off, len); } /** Returns the total number of bytes written into the output stream at the requested time. */ public int getWrittenBytesCount() { return mWrittenBytesCount; } } services/core/java/com/android/server/integrity/model/ComponentBitSize.javadeleted 100644 → 0 +0 −45 Original line number Diff line number Diff line /* * Copyright (C) 2019 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 android.content.integrity.Rule; /** * A helper class containing information about the binary representation of different {@link Rule} * components. */ public final class ComponentBitSize { public static final int FORMAT_VERSION_BITS = 8; public static final int EFFECT_BITS = 3; public static final int KEY_BITS = 4; public static final int OPERATOR_BITS = 3; public static final int CONNECTOR_BITS = 2; public static final int SEPARATOR_BITS = 3; public static final int VALUE_SIZE_BITS = 8; public static final int IS_HASHED_BITS = 1; public static final int ATOMIC_FORMULA_START = 0; public static final int COMPOUND_FORMULA_START = 1; public static final int COMPOUND_FORMULA_END = 2; public static final int INSTALLER_ALLOWED_BY_MANIFEST_START = 3; public static final int DEFAULT_FORMAT_VERSION = 1; public static final int SIGNAL_BIT = 1; public static final int BYTE_BITS = 8; } services/core/java/com/android/server/integrity/model/IndexingFileConstants.javadeleted 100644 → 0 +0 −27 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; /** A helper class containing special indexing file constants. */ public final class IndexingFileConstants { // We empirically experimented with different block sizes and identified that 50 is in the // optimal range of efficient computation. public static final int INDEXING_BLOCK_SIZE = 50; public static final String START_INDEXING_KEY = "START_KEY"; public static final String END_INDEXING_KEY = "END_KEY"; } Loading
services/core/java/com/android/server/integrity/model/BitInputStream.javadeleted 100644 → 0 +0 −72 Original line number Diff line number Diff line /* * Copyright (C) 2019 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; /** 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 mBitsRead; private InputStream mInputStream; private byte mCurrentByte; public BitInputStream(InputStream inputStream) { mInputStream = inputStream; } /** * Read the next number of bits from the stream. * * @param numOfBits The number of bits to read. * @return The value read from the stream. */ public int getNext(int numOfBits) throws IOException { int component = 0; int count = 0; while (count++ < numOfBits) { if (mBitsRead % 8 == 0) { mCurrentByte = getNextByte(); } int offset = 7 - (int) (mBitsRead % 8); component <<= 1; component |= (mCurrentByte >>> offset) & 1; mBitsRead++; } return component; } /** Check if there are bits left in the stream. */ public boolean hasNext() throws IOException { return mInputStream.available() > 0; } private byte getNextByte() throws IOException { return (byte) mInputStream.read(); } }
services/core/java/com/android/server/integrity/model/BitOutputStream.javadeleted 100644 → 0 +0 −105 Original line number Diff line number Diff line /* * Copyright (C) 2019 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 static com.android.server.integrity.model.ComponentBitSize.BYTE_BITS; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; /** A wrapper class for writing a stream of bits. */ public class BitOutputStream { private static final int BUFFER_SIZE = 4 * 1024; private int mNextBitIndex; private final OutputStream mOutputStream; private final byte[] mBuffer; public BitOutputStream(OutputStream outputStream) { mBuffer = new byte[BUFFER_SIZE]; mNextBitIndex = 0; mOutputStream = outputStream; } /** * Set the next number of bits in the stream to value. * * @param numOfBits The number of bits used to represent the value. * @param value The value to convert to bits. */ public void setNext(int numOfBits, int value) throws IOException { if (numOfBits <= 0) { return; } // optional: we can do some clever size checking to "OR" an entire segment of bits instead // of setting bits one by one, but it is probably not worth it. int nextBitMask = 1 << (numOfBits - 1); while (numOfBits-- > 0) { setNext((value & nextBitMask) != 0); nextBitMask >>>= 1; } } /** * Set the next bit in the stream to value. * * @param value The value to set the bit to */ public void setNext(boolean value) throws IOException { int byteToWrite = mNextBitIndex / BYTE_BITS; if (byteToWrite == BUFFER_SIZE) { mOutputStream.write(mBuffer); reset(); byteToWrite = 0; } if (value) { mBuffer[byteToWrite] |= 1 << (BYTE_BITS - 1 - (mNextBitIndex % BYTE_BITS)); } mNextBitIndex++; } /** Set the next bit in the stream to true. */ public void setNext() throws IOException { setNext(/* value= */ true); } /** * Flush the data written to the underlying {@link java.io.OutputStream}. Any unfinished bytes * will be padded with 0. */ public void flush() throws IOException { int endByte = mNextBitIndex / BYTE_BITS; if (mNextBitIndex % BYTE_BITS != 0) { // If next bit is not the first bit of a byte, then mNextBitIndex / BYTE_BITS would be // the byte that includes already written bits. We need to increment it so this byte // gets written. endByte++; } mOutputStream.write(mBuffer, 0, endByte); reset(); } /** Reset this output stream to start state. */ private void reset() { mNextBitIndex = 0; Arrays.fill(mBuffer, (byte) 0); } }
services/core/java/com/android/server/integrity/model/ByteTrackedOutputStream.javadeleted 100644 → 0 +0 −63 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.OutputStream; /** * An output stream that tracks the total number written bytes since construction and allows * querying this value any time during the execution. * * <p>This class is used for constructing the rule indexing. */ public class ByteTrackedOutputStream extends OutputStream { private int mWrittenBytesCount; private final OutputStream mOutputStream; public ByteTrackedOutputStream(OutputStream outputStream) { mWrittenBytesCount = 0; mOutputStream = outputStream; } @Override public void write(int b) throws IOException { mWrittenBytesCount++; mOutputStream.write(b); } /** * Writes the given bytes into the output stream provided in constructor and updates the total * number of written bytes. */ @Override public void write(byte[] bytes) throws IOException { write(bytes, 0, bytes.length); } @Override public void write(byte[] b, int off, int len) throws IOException { mWrittenBytesCount += len; mOutputStream.write(b, off, len); } /** Returns the total number of bytes written into the output stream at the requested time. */ public int getWrittenBytesCount() { return mWrittenBytesCount; } }
services/core/java/com/android/server/integrity/model/ComponentBitSize.javadeleted 100644 → 0 +0 −45 Original line number Diff line number Diff line /* * Copyright (C) 2019 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 android.content.integrity.Rule; /** * A helper class containing information about the binary representation of different {@link Rule} * components. */ public final class ComponentBitSize { public static final int FORMAT_VERSION_BITS = 8; public static final int EFFECT_BITS = 3; public static final int KEY_BITS = 4; public static final int OPERATOR_BITS = 3; public static final int CONNECTOR_BITS = 2; public static final int SEPARATOR_BITS = 3; public static final int VALUE_SIZE_BITS = 8; public static final int IS_HASHED_BITS = 1; public static final int ATOMIC_FORMULA_START = 0; public static final int COMPOUND_FORMULA_START = 1; public static final int COMPOUND_FORMULA_END = 2; public static final int INSTALLER_ALLOWED_BY_MANIFEST_START = 3; public static final int DEFAULT_FORMAT_VERSION = 1; public static final int SIGNAL_BIT = 1; public static final int BYTE_BITS = 8; }
services/core/java/com/android/server/integrity/model/IndexingFileConstants.javadeleted 100644 → 0 +0 −27 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; /** A helper class containing special indexing file constants. */ public final class IndexingFileConstants { // We empirically experimented with different block sizes and identified that 50 is in the // optimal range of efficient computation. public static final int INDEXING_BLOCK_SIZE = 50; public static final String START_INDEXING_KEY = "START_KEY"; public static final String END_INDEXING_KEY = "END_KEY"; }