1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 import com.google.zxing.BinaryBitmap; 18 import com.google.zxing.BufferedImageLuminanceSource; 19 import com.google.zxing.MultiFormatReader; 20 import com.google.zxing.ReaderException; 21 import com.google.zxing.Result; 22 import com.google.zxing.common.HybridBinarizer; 23 24 import javax.imageio.ImageIO; 25 import java.io.IOException; 26 import java.awt.image.BufferedImage; 27 import java.io.ByteArrayInputStream; 28 29 public final class MultiFormatDecodeFuzzer { 30 private static MultiFormatReader barcodeReader = new MultiFormatReader(); 31 fuzzerInitialize()32 public static void fuzzerInitialize() { 33 } 34 fuzzerTestOneInput(byte[] input)35 public static void fuzzerTestOneInput(byte[] input) { 36 BufferedImage image; 37 try { 38 image = ImageIO.read(new ByteArrayInputStream(input)); 39 } catch (IOException e) { 40 return; 41 } 42 if (image == null) 43 return; 44 if ((long) image.getHeight() * (long) image.getWidth() > 10000000) 45 return; 46 47 BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); 48 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 49 try { 50 Result result = barcodeReader.decode(bitmap); 51 result.getText(); 52 result.getResultMetadata(); 53 } catch (ReaderException ignored) { 54 } 55 } 56 } 57