• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdio>
17 #include <cstdlib>
18 
19 #include "file-stream.h"
20 #include "opentype-sanitiser.h"
21 
22 #if defined(_WIN32)
23 #define ADDITIONAL_OPEN_FLAGS O_BINARY
24 #else
25 #define ADDITIONAL_OPEN_FLAGS 0
26 #endif
27 
28 namespace {
29 
Usage(const char * argv0)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 
main(int argc,char ** argv)37 int main(int argc, char **argv) {
38   if (argc != 2) return Usage(argv[0]);
39   if (::isatty(1)) return Usage(argv[0]);
40 
41   ots::EnableWOFF2();
42 
43   const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS);
44   if (fd < 0) {
45     ::perror("open");
46     return 1;
47   }
48 
49   struct stat st;
50   ::fstat(fd, &st);
51 
52   uint8_t *data = new uint8_t[st.st_size];
53   if (::read(fd, data, st.st_size) != st.st_size) {
54     ::perror("read");
55     return 1;
56   }
57   ::close(fd);
58 
59   ots::FILEStream output(stdout);
60 #if defined(_WIN32)
61   ::setmode(fileno(stdout), O_BINARY);
62 #endif
63   const bool result = ots::Process(&output, data, st.st_size);
64 
65   if (!result) {
66     std::fprintf(stderr, "Failed to sanitise file!\n");
67   }
68   return !result;
69 }
70