blob: aef477a3bf12767f2efdf22ae481051e572c76b7 [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.org123cae82013-04-02 01:43:21 +000041 const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS);
yusukes@chromium.orgd257d182009-11-04 04:56:32 +000042 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.org123cae82013-04-02 01:43:21 +000058#if defined(_WIN32)
59 ::setmode(fileno(stdout), O_BINARY);
60#endif
yusukes@chromium.orgd257d182009-11-04 04:56:32 +000061 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}