| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <fcntl.h> | 5 #if !defined(_MSC_VER) |
| 6 #ifdef __linux__ |
| 7 // Linux |
| 8 #include <freetype/ftoutln.h> |
| 6 #include <ft2build.h> | 9 #include <ft2build.h> |
| 7 #include FT_FREETYPE_H | 10 #include FT_FREETYPE_H |
| 11 #else |
| 12 // Mac OS X |
| 13 #include <ApplicationServices/ApplicationServices.h> // g++ -framework Cocoa |
| 14 #endif // __linux__ |
| 15 #else |
| 16 // Windows |
| 17 // TODO(yusukes): Support Windows. |
| 18 #endif // _MSC_VER |
| 19 |
| 20 #include <fcntl.h> |
| 8 #include <sys/stat.h> | 21 #include <sys/stat.h> |
| 9 #include <sys/types.h> | 22 #include <sys/types.h> |
| 10 #include <unistd.h> | 23 #include <unistd.h> |
| 11 | 24 |
| 12 #include <cstdio> | 25 #include <cstdio> |
| 13 #include <cstdlib> | 26 #include <cstdlib> |
| 14 #include <cstring> | 27 #include <cstring> |
| 15 | 28 |
| 16 #include "opentype-sanitiser.h" | 29 #include "opentype-sanitiser.h" |
| 17 #include "ots-memory-stream.h" | 30 #include "ots-memory-stream.h" |
| 18 | 31 |
| 19 namespace { | 32 namespace { |
| 20 | 33 |
| 34 #if !defined(_MSC_VER) |
| 35 #ifdef __linux__ |
| 36 // Linux |
| 21 void LoadChar(FT_Face face, int pt, FT_ULong c) { | 37 void LoadChar(FT_Face face, int pt, FT_ULong c) { |
| 22 FT_Matrix matrix; | 38 FT_Matrix matrix; |
| 23 matrix.xx = matrix.yy = 1 << 16; | 39 matrix.xx = matrix.yy = 1 << 16; |
| 24 matrix.xy = matrix.yx = 0 << 16; | 40 matrix.xy = matrix.yx = 0 << 16; |
| 25 | 41 |
| 26 FT_Set_Char_Size(face, pt * (1 << 6), 0, 72, 0); | 42 FT_Set_Char_Size(face, pt * (1 << 6), 0, 72, 0); |
| 27 FT_Set_Transform(face, &matrix, 0); | 43 FT_Set_Transform(face, &matrix, 0); |
| 28 FT_Load_Char(face, c, FT_LOAD_RENDER); | 44 FT_Load_Char(face, c, FT_LOAD_RENDER); |
| 29 } | 45 } |
| 30 | 46 |
| 31 int OpenAndLoadChars(FT_Library library, const char *file_name, | 47 int OpenAndLoadChars( |
| 32 uint8_t *trans_font, size_t trans_len) { | 48 const char *file_name, uint8_t *trans_font, size_t trans_len) { |
| 49 FT_Library library; |
| 50 FT_Error error = FT_Init_FreeType(&library); |
| 51 if (error) { |
| 52 std::fprintf(stderr, "Failed to initialize FreeType2!\n"); |
| 53 return 1; |
| 54 } |
| 55 |
| 33 FT_Face trans_face; | 56 FT_Face trans_face; |
| 34 FT_Error error | 57 error = FT_New_Memory_Face(library, trans_font, trans_len, 0, &trans_face); |
| 35 = FT_New_Memory_Face(library, trans_font, trans_len, 0, &trans_face); | |
| 36 if (error) { | 58 if (error) { |
| 37 std::fprintf(stderr, | 59 std::fprintf(stderr, |
| 38 "OK: FreeType2 couldn't open the transcoded font: %s\n", | 60 "OK: FreeType2 couldn't open the transcoded font: %s\n", |
| 39 file_name); | 61 file_name); |
| 40 return 0; | 62 return 0; |
| 41 } | 63 } |
| 42 | 64 |
| 43 static const int kPts[] = {100, 20, 18, 16, 12, 10, 8}; // pt | 65 static const int kPts[] = {100, 20, 18, 16, 12, 10, 8}; // pt |
| 44 static const size_t kPtsLen = sizeof(kPts) / sizeof(kPts[0]); | 66 static const size_t kPtsLen = sizeof(kPts) / sizeof(kPts[0]); |
| 45 | 67 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 59 for (size_t j = 0; j < kUnicodeRangesLen; j += 2) { | 81 for (size_t j = 0; j < kUnicodeRangesLen; j += 2) { |
| 60 for (int k = 0; k <= kUnicodeRanges[j + 1] - kUnicodeRanges[j]; ++k) { | 82 for (int k = 0; k <= kUnicodeRanges[j + 1] - kUnicodeRanges[j]; ++k) { |
| 61 LoadChar(trans_face, kPts[i], kUnicodeRanges[j] + k); | 83 LoadChar(trans_face, kPts[i], kUnicodeRanges[j] + k); |
| 62 } | 84 } |
| 63 } | 85 } |
| 64 } | 86 } |
| 65 | 87 |
| 66 std::fprintf(stderr, "OK: FreeType2 didn't crash: %s\n", file_name); | 88 std::fprintf(stderr, "OK: FreeType2 didn't crash: %s\n", file_name); |
| 67 return 0; | 89 return 0; |
| 68 } | 90 } |
| 91 #else |
| 92 // Mac OS X |
| 93 int OpenAndLoadChars( |
| 94 const char *file_name, uint8_t *trans_font, size_t trans_len) { |
| 95 ATSFontContainerRef container_ref = 0; |
| 96 ATSFontActivateFromMemory(trans_font, trans_len, 3, kATSFontFormatUnspecified, |
| 97 NULL, kATSOptionFlagsDefault, &container_ref); |
| 98 if (!container_ref) { |
| 99 std::fprintf(stderr, |
| 100 "OK: font renderer couldn't open the transcoded font: %s\n", |
| 101 file_name); |
| 102 return 0; |
| 103 } |
| 104 |
| 105 ItemCount count; |
| 106 ATSFontFindFromContainer( |
| 107 container_ref, kATSOptionFlagsDefault, 0, NULL, &count); |
| 108 if (!count) { |
| 109 std::fprintf(stderr, |
| 110 "OK: font renderer couldn't open the transcoded font: %s\n", |
| 111 file_name); |
| 112 return 0; |
| 113 } |
| 114 |
| 115 ATSFontRef ats_font_ref = 0; |
| 116 ATSFontFindFromContainer( |
| 117 container_ref, kATSOptionFlagsDefault, 1, &ats_font_ref, NULL); |
| 118 if (!ats_font_ref) { |
| 119 std::fprintf(stderr, |
| 120 "OK: font renderer couldn't open the transcoded font: %s\n", |
| 121 file_name); |
| 122 return 0; |
| 123 } |
| 124 |
| 125 CGFontRef cg_font_ref = CGFontCreateWithPlatformFont(&ats_font_ref); |
| 126 if (!CGFontGetNumberOfGlyphs(cg_font_ref)) { |
| 127 std::fprintf(stderr, |
| 128 "OK: font renderer couldn't open the transcoded font: %s\n", |
| 129 file_name); |
| 130 return 0; |
| 131 } |
| 132 |
| 133 std::fprintf(stderr, "OK: font renderer didn't crash: %s\n", file_name); |
| 134 // TODO(yusukes): would be better to perform LoadChar() like Linux. |
| 135 return 0; |
| 136 } |
| 137 #endif // __linux__ |
| 138 #else |
| 139 // Windows |
| 140 // TODO(yusukes): Support Windows. |
| 141 #endif // _MSC_VER |
| 69 | 142 |
| 70 } // namespace | 143 } // namespace |
| 71 | 144 |
| 72 int main(int argc, char **argv) { | 145 int main(int argc, char **argv) { |
| 73 ots::DisableDebugOutput(); // turn off ERROR and WARNING outputs. | 146 ots::DisableDebugOutput(); // turn off ERROR and WARNING outputs. |
| 74 | 147 |
| 75 if (argc != 2) { | 148 if (argc != 2) { |
| 76 std::fprintf(stderr, "Usage: %s ttf_or_otf_filename\n", argv[0]); | 149 std::fprintf(stderr, "Usage: %s ttf_or_otf_filename\n", argv[0]); |
| 77 return 1; | 150 return 1; |
| 78 } | 151 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 100 uint8_t *trans_font = new uint8_t[orig_len + kBigPadLen]; | 173 uint8_t *trans_font = new uint8_t[orig_len + kBigPadLen]; |
| 101 ots::MemoryStream output(trans_font, orig_len + kBigPadLen); | 174 ots::MemoryStream output(trans_font, orig_len + kBigPadLen); |
| 102 | 175 |
| 103 bool result = ots::Process(&output, orig_font, orig_len); | 176 bool result = ots::Process(&output, orig_font, orig_len); |
| 104 if (!result) { | 177 if (!result) { |
| 105 std::fprintf(stderr, "OK: the malicious font was filtered: %s\n", argv[1]); | 178 std::fprintf(stderr, "OK: the malicious font was filtered: %s\n", argv[1]); |
| 106 return 0; | 179 return 0; |
| 107 } | 180 } |
| 108 const size_t trans_len = output.Tell(); | 181 const size_t trans_len = output.Tell(); |
| 109 | 182 |
| 110 FT_Library library; | 183 return OpenAndLoadChars(argv[1], trans_font, trans_len); |
| 111 FT_Error error = FT_Init_FreeType(&library); | |
| 112 if (error) { | |
| 113 std::fprintf(stderr, "Failed to initialize FreeType2!\n"); | |
| 114 return 1; | |
| 115 } | |
| 116 | |
| 117 return OpenAndLoadChars(library, argv[1], trans_font, trans_len); | |
| 118 } | 184 } |
| OLD | NEW |