Commit a8b956227b62eec6f729064724c8c4775a96bbae

Werner Lemberg 2016-03-01T09:37:12

[ftfuzzer] Add unit for testing smooth and black rasterizers. * src/tools/ftfuzzer/rasterfuzzer.cc: New file.

diff --git a/ChangeLog b/ChangeLog
index c0b20b9..b827992 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,11 @@
 2016-03-01  Werner Lemberg  <wl@gnu.org>
+	    Kostya Serebryany  <kcc@google.com>
+
+	[ftfuzzer] Add unit for testing smooth and black rasterizers.
+
+	* src/tools/ftfuzzer/rasterfuzzer.cc: New file.
+
+2016-03-01  Werner Lemberg  <wl@gnu.org>
 
 	[autofit] Fix reallocation error introduced in 2016-02-27 (#47310).
 
diff --git a/src/tools/ftfuzzer/rasterfuzzer.cc b/src/tools/ftfuzzer/rasterfuzzer.cc
new file mode 100644
index 0000000..37cc754
--- /dev/null
+++ b/src/tools/ftfuzzer/rasterfuzzer.cc
@@ -0,0 +1,104 @@
+// rasterfuzzer.cc
+//
+//   A fuzzing function to test FreeType's rasterizers with libFuzzer.
+//
+// Copyright 2016 by
+// David Turner, Robert Wilhelm, and Werner Lemberg.
+//
+// This file is part of the FreeType project, and may only be used,
+// modified, and distributed under the terms of the FreeType project
+// license, LICENSE.TXT.  By continuing to use, modify, or distribute
+// this file you indicate that you have read the license and
+// understand and accept it fully.
+
+
+#include <stdint.h>
+
+#include <vector>
+
+
+  using namespace std;
+
+
+#include <ft2build.h>
+
+#include FT_FREETYPE_H
+#include FT_IMAGE_H
+#include FT_OUTLINE_H
+
+
+  static FT_Library  library;
+  static int         InitResult;
+
+
+  struct FT_Global {
+    FT_Global() {
+      InitResult = FT_Init_FreeType( &library );
+    }
+    ~FT_Global() {
+      FT_Done_FreeType( library );
+    }
+  };
+
+  FT_Global  global_ft;
+
+
+  extern "C" int
+  LLVMFuzzerTestOneInput( const uint8_t*  data,
+                          size_t          size_ )
+  {
+    unsigned char  pixels[4];
+
+    FT_Bitmap  bitmap_mono = {
+      1,                  // rows
+      1,                  // width
+      4,                  // pitch
+      pixels,             // buffer
+      2,                  // num_grays
+      FT_PIXEL_MODE_MONO, // pixel_mode
+      0,                  // palette_mode
+      NULL                // palette
+    };
+
+    FT_Bitmap  bitmap_gray = {
+      1,                  // rows
+      1,                  // width
+      4,                  // pitch
+      pixels,             // buffer
+      256,                // num_grays
+      FT_PIXEL_MODE_GRAY, // pixel_mode
+      0,                  // palette_mode
+      NULL                // palette
+    };
+
+    short  n_points = short( size_ / sizeof ( FT_Vector ) );
+    if ( n_points <= 2 )
+      return 0;
+
+    FT_Vector*  points = reinterpret_cast<FT_Vector*>(
+                           const_cast<uint8_t*>( data ) );
+
+    short  contours[1];
+    contours[0] = n_points - 1;
+
+    vector<char>  tags( (size_t)n_points );
+    fill( tags.begin(), tags.end(), 1 );
+
+    FT_Outline  outline =
+    {
+      1,                                      // n_contours
+      n_points,                               // n_points
+      points,                                 // points
+      reinterpret_cast<char*>( tags.data() ), // tags
+      contours,                               // contours
+      FT_OUTLINE_NONE                         // flags
+    };
+
+    FT_Outline_Get_Bitmap( library, &outline, &bitmap_mono );
+    FT_Outline_Get_Bitmap( library, &outline, &bitmap_gray );
+
+    return 0;
+  }
+
+
+// END
diff --git a/src/tools/ftfuzzer/runinput.cc b/src/tools/ftfuzzer/runinput.cc
index 8fa75ad..d5f9f15 100644
--- a/src/tools/ftfuzzer/runinput.cc
+++ b/src/tools/ftfuzzer/runinput.cc
@@ -1,6 +1,6 @@
 // runinput.cc
 //
-//   A `main' function for `ftfuzzer.cc'.
+//   A `main' function for fuzzers like `ftfuzzer.cc'.
 //
 // Copyright 2015-2016 by
 // David Turner, Robert Wilhelm, and Werner Lemberg.