blob: ad3054e7c9e1df8918c70bd4be26e3c9fdcb91fe [file] [log] [blame]
yusukes@chromium.orgd257d182009-11-04 04:56:32 +00001// 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.org8d71e042012-05-07 08:45:16 +000010#if defined(_WIN32)
11#include <io.h>
12#else
yusukes@chromium.orgd257d182009-11-04 04:56:32 +000013#include <unistd.h>
bashi@chromium.org8d71e042012-05-07 08:45:16 +000014#endif // defined(_WIN32)
yusukes@chromium.orgd257d182009-11-04 04:56:32 +000015
16#include <cstdio>
17#include <cstdlib>
18
19#include "file-stream.h"
20#include "opentype-sanitiser.h"
21
ksakamoto@chromium.org123cae82013-04-02 01:43:21 +000022#if defined(_WIN32)
23#define ADDITIONAL_OPEN_FLAGS O_BINARY
24#else
25#define ADDITIONAL_OPEN_FLAGS 0
26#endif
27
yusukes@chromium.orgd257d182009-11-04 04:56:32 +000028namespace {
29
30int 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
37int main(int argc, char **argv) {
38 if (argc != 2) return Usage(argv[0]);
39 if (::isatty(1)) return Usage(argv[0]);
40
ksakamoto@chromium.org74343ba2013-05-08 01:20:29 +000041 ots::EnableWOFF2();
42
ksakamoto@chromium.org123cae82013-04-02 01:43:21 +000043 const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS);
yusukes@chromium.orgd257d182009-11-04 04:56:32 +000044 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);
ksakamoto@chromium.org123cae82013-04-02 01:43:21 +000060#if defined(_WIN32)
61 ::setmode(fileno(stdout), O_BINARY);
62#endif
yusukes@chromium.orgd257d182009-11-04 04:56:32 +000063 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}