OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // A very simple driver program while sanitises the file given as argv[1] and | |
6 // writes the sanitised version to stdout. | |
7 | |
8 #include <fcntl.h> | |
9 #include <sys/stat.h> | |
10 #if defined(_WIN32) | |
11 #include <io.h> | |
12 #else | |
13 #include <unistd.h> | |
14 #endif // defined(_WIN32) | |
15 | |
16 #include <cstdarg> | |
17 #include <cstdio> | |
18 #include <cstdlib> | |
19 | |
20 #include "file-stream.h" | |
21 #include "opentype-sanitiser.h" | |
22 | |
23 #if defined(_WIN32) | |
24 #define ADDITIONAL_OPEN_FLAGS O_BINARY | |
25 #else | |
26 #define ADDITIONAL_OPEN_FLAGS 0 | |
27 #endif | |
28 | |
29 namespace { | |
30 | |
31 int Usage(const char *argv0) { | |
32 std::fprintf(stderr, "Usage: %s ttf_file [dest_ttf_file]\n", argv0); | |
33 return 1; | |
34 } | |
35 | |
36 class Context: public ots::OTSContext { | |
37 public: | |
38 virtual void Message(int level, const char *format, ...) { | |
39 va_list va; | |
40 | |
41 if (level == 0) | |
42 std::fprintf(stderr, "ERROR: "); | |
43 else | |
44 std::fprintf(stderr, "WARNING: "); | |
45 va_start(va, format); | |
46 std::vfprintf(stderr, format, va); | |
47 std::fprintf(stderr, "\n"); | |
48 va_end(va); | |
49 } | |
50 | |
51 virtual ots::TableAction GetTableAction(uint32_t tag) { | |
52 #define TAG(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d)) | |
53 switch (tag) { | |
54 case TAG('S','i','l','f'): | |
55 case TAG('S','i','l','l'): | |
56 case TAG('G','l','o','c'): | |
57 case TAG('G','l','a','t'): | |
58 case TAG('F','e','a','t'): | |
59 return ots::TABLE_ACTION_PASSTHRU; | |
60 default: | |
61 return ots::TABLE_ACTION_DEFAULT; | |
62 } | |
63 #undef TAG | |
64 } | |
65 }; | |
66 | |
67 } // namespace | |
68 | |
69 int main(int argc, char **argv) { | |
70 if (argc < 2 || argc > 3) return Usage(argv[0]); | |
71 | |
72 const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS); | |
73 if (fd < 0) { | |
74 ::perror("open"); | |
75 return 1; | |
76 } | |
77 | |
78 struct stat st; | |
79 ::fstat(fd, &st); | |
80 | |
81 uint8_t *data = new uint8_t[st.st_size]; | |
82 if (::read(fd, data, st.st_size) != st.st_size) { | |
83 ::perror("read"); | |
84 return 1; | |
85 } | |
86 ::close(fd); | |
87 | |
88 Context context; | |
89 | |
90 FILE* out = NULL; | |
91 if (argc == 3) | |
92 out = fopen(argv[2], "wb"); | |
93 | |
94 ots::FILEStream output(out); | |
95 const bool result = context.Process(&output, data, st.st_size); | |
96 | |
97 if (!result) { | |
98 std::fprintf(stderr, "Failed to sanitise file!\n"); | |
99 } | |
100 return !result; | |
101 } | |
OLD | NEW |