1 /* 2 * Copyright (C) 2024 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 package android.net.apf; 17 18 import com.android.internal.annotations.VisibleForTesting; 19 20 import java.util.Objects; 21 22 /** 23 * APFv6 assembler/generator. A tool for generating an APFv6 program. 24 * 25 * @hide 26 */ 27 public final class ApfV6Generator extends ApfV6GeneratorBase<ApfV6Generator> { 28 /** 29 * Returns true if we support the specified {@code version}, otherwise false. 30 */ supportsVersion(int version)31 public static boolean supportsVersion(int version) { 32 return version >= APF_VERSION_6; 33 } 34 35 /** 36 * Creates an ApfV6Generator instance which emits instructions for APFv6. 37 */ ApfV6Generator(int maximumApfProgramSize)38 public ApfV6Generator(int maximumApfProgramSize) throws IllegalInstructionException { 39 this(new byte[0], maximumApfProgramSize); 40 } 41 42 @Override updateExceptionBufferSize(int programSize)43 void updateExceptionBufferSize(int programSize) throws IllegalInstructionException { 44 mInstructions.get(1).updateExceptionBufferSize( 45 mMaximumApfProgramSize - ApfCounterTracker.Counter.totalSize() - programSize); 46 } 47 48 /** 49 * Creates an ApfV6Generator instance which emits instructions APFv6. 50 * Initializes the data region with {@code bytes}. 51 */ 52 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) ApfV6Generator(byte[] bytes, int maximumApfProgramSize)53 public ApfV6Generator(byte[] bytes, int maximumApfProgramSize) 54 throws IllegalInstructionException { 55 super(maximumApfProgramSize); 56 Objects.requireNonNull(bytes); 57 addData(bytes); 58 addExceptionBuffer(0); 59 } 60 } 61