// Copyright 2021 Google LLC
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////

import com.code_intelligence.jazzer.api.FuzzedDataProvider;

import java.io.*;
import org.brotli.dec.BrotliInputStream;

public class FuzzDecode {
  public static void fuzzerTestOneInput(FuzzedDataProvider data) {
    byte[] buffer = new byte[65536];
    ByteArrayInputStream input = new ByteArrayInputStream(data.consumeBytes(65536));
    try {
      BrotliInputStream brotliInput = new BrotliInputStream(input);
      while (true) {
        int len = brotliInput.read(buffer, 0, buffer.length);
        if (len <= 0) {
          break;
        }
      }
    } catch (IOException expected) {}
  }
}

@@ -917,16 +917,314 @@
 65536%5D;%0A
+    byte%5B%5D inputBytes = data.consumeBytes(65536);%0A    // Brotli allows 0-bit prefix codes - thus even small input could produce large output.%0A    long totalOutputCap = Math.min(4096L * inputBytes.length, 3L %3C%3C 24);%0A    totalOutputCap = Math.max(totalOutputCap, 1L %3C%3C 20);%0A    long totalOutput = 0;%0A
     Byte
@@ -1277,32 +1277,18 @@
 eam(
-data.consumeBytes(65536)
+inputBytes
 );%0A 
@@ -1488,16 +1488,16 @@
  break;%0A
-
         
@@ -1494,24 +1494,102 @@
 ;%0A        %7D%0A
+        totalOutput += len;%0A        if (totalOutput %3E= totalOutputCap) break;%0A
       %7D%0A    

Stop fuzzing once output size limit is reached (#6958)