yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 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> |
bashi@chromium.org | 8d71e04 | 2012-05-07 08:45:16 +0000 | [diff] [blame] | 10 | #if defined(_WIN32) |
| 11 | #include <io.h> |
| 12 | #else |
yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 13 | #include <unistd.h> |
bashi@chromium.org | 8d71e04 | 2012-05-07 08:45:16 +0000 | [diff] [blame] | 14 | #endif // defined(_WIN32) |
yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 15 | |
| 16 | #include <cstdio> |
| 17 | #include <cstdlib> |
| 18 | |
| 19 | #include "file-stream.h" |
| 20 | #include "opentype-sanitiser.h" |
| 21 | |
ksakamoto@chromium.org | 123cae8 | 2013-04-02 01:43:21 +0000 | [diff] [blame^] | 22 | #if defined(_WIN32) |
| 23 | #define ADDITIONAL_OPEN_FLAGS O_BINARY |
| 24 | #else |
| 25 | #define ADDITIONAL_OPEN_FLAGS 0 |
| 26 | #endif |
| 27 | |
yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | int Usage(const char *argv0) { |
| 31 | std::fprintf(stderr, "Usage: %s ttf_file > dest_ttf_file\n", argv0); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | int main(int argc, char **argv) { |
| 38 | if (argc != 2) return Usage(argv[0]); |
| 39 | if (::isatty(1)) return Usage(argv[0]); |
| 40 | |
ksakamoto@chromium.org | 123cae8 | 2013-04-02 01:43:21 +0000 | [diff] [blame^] | 41 | const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS); |
yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 42 | if (fd < 0) { |
| 43 | ::perror("open"); |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | struct stat st; |
| 48 | ::fstat(fd, &st); |
| 49 | |
| 50 | uint8_t *data = new uint8_t[st.st_size]; |
| 51 | if (::read(fd, data, st.st_size) != st.st_size) { |
| 52 | ::perror("read"); |
| 53 | return 1; |
| 54 | } |
| 55 | ::close(fd); |
| 56 | |
| 57 | ots::FILEStream output(stdout); |
ksakamoto@chromium.org | 123cae8 | 2013-04-02 01:43:21 +0000 | [diff] [blame^] | 58 | #if defined(_WIN32) |
| 59 | ::setmode(fileno(stdout), O_BINARY); |
| 60 | #endif |
yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 61 | const bool result = ots::Process(&output, data, st.st_size); |
| 62 | |
| 63 | if (!result) { |
| 64 | std::fprintf(stderr, "Failed to sanitise file!\n"); |
| 65 | } |
| 66 | return !result; |
| 67 | } |