1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.integrity.parser; 18 19 import static com.android.server.integrity.model.ComponentBitSize.IS_HASHED_BITS; 20 import static com.android.server.integrity.model.ComponentBitSize.VALUE_SIZE_BITS; 21 22 import android.content.integrity.IntegrityUtils; 23 24 import com.android.server.integrity.model.BitInputStream; 25 26 import java.io.IOException; 27 import java.nio.ByteBuffer; 28 29 /** 30 * Helper methods for reading standard data structures from {@link BitInputStream}. 31 */ 32 public class BinaryFileOperations { 33 34 /** 35 * Read an string value with the given size and hash status from a {@code BitInputStream}. 36 * 37 * If the value is hashed, get the hex-encoding of the value. Serialized values are in raw form. 38 * All hashed values are hex-encoded. 39 */ getStringValue(BitInputStream bitInputStream)40 public static String getStringValue(BitInputStream bitInputStream) throws IOException { 41 boolean isHashedValue = bitInputStream.getNext(IS_HASHED_BITS) == 1; 42 int valueSize = bitInputStream.getNext(VALUE_SIZE_BITS); 43 return getStringValue(bitInputStream, valueSize, isHashedValue); 44 } 45 46 /** 47 * Read an string value with the given size and hash status from a {@code BitInputStream}. 48 * 49 * If the value is hashed, get the hex-encoding of the value. Serialized values are in raw form. 50 * All hashed values are hex-encoded. 51 */ getStringValue( BitInputStream bitInputStream, int valueSize, boolean isHashedValue)52 public static String getStringValue( 53 BitInputStream bitInputStream, int valueSize, boolean isHashedValue) 54 throws IOException { 55 if (!isHashedValue) { 56 StringBuilder value = new StringBuilder(); 57 while (valueSize-- > 0) { 58 value.append((char) bitInputStream.getNext(/* numOfBits= */ 8)); 59 } 60 return value.toString(); 61 } 62 ByteBuffer byteBuffer = ByteBuffer.allocate(valueSize); 63 while (valueSize-- > 0) { 64 byteBuffer.put((byte) (bitInputStream.getNext(/* numOfBits= */ 8) & 0xFF)); 65 } 66 return IntegrityUtils.getHexDigest(byteBuffer.array()); 67 } 68 69 /** Read an integer value from a {@code BitInputStream}. */ getIntValue(BitInputStream bitInputStream)70 public static int getIntValue(BitInputStream bitInputStream) throws IOException { 71 return bitInputStream.getNext(/* numOfBits= */ 32); 72 } 73 74 /** Read an boolean value from a {@code BitInputStream}. */ getBooleanValue(BitInputStream bitInputStream)75 public static boolean getBooleanValue(BitInputStream bitInputStream) throws IOException { 76 return bitInputStream.getNext(/* numOfBits= */ 1) == 1; 77 } 78 } 79