forked from khaledhosny/ots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathot-sanitise.cc
100 lines (78 loc) · 2.17 KB
/
ot-sanitise.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A very simple driver program while sanitises the file given as argv[1] and
// writes the sanitised version to stdout.
#include <fcntl.h>
#include <sys/stat.h>
#if defined(_WIN32)
#include <io.h>
#else
#include <unistd.h>
#endif // defined(_WIN32)
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include "file-stream.h"
#include "opentype-sanitiser.h"
#if defined(_WIN32)
#define ADDITIONAL_OPEN_FLAGS O_BINARY
#else
#define ADDITIONAL_OPEN_FLAGS 0
#endif
namespace {
int Usage(const char *argv0) {
std::fprintf(stderr, "Usage: %s ttf_file [dest_ttf_file]\n", argv0);
return 1;
}
bool Message(void *aUserData, const char *format, ...) {
va_list va;
va_start(va, format);
std::vfprintf(stderr, format, va);
std::fprintf(stderr, "\n");
va_end(va);
return false;
}
#define TAG(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))
ots::TableAction TableActionCallback(uint32_t tag, void *user_data) {
switch (tag) {
case TAG('S','i','l','f'):
case TAG('S','i','l','l'):
case TAG('G','l','o','c'):
case TAG('G','l','a','t'):
case TAG('F','e','a','t'):
return ots::TABLE_ACTION_PASSTHRU;
default:
return ots::TABLE_ACTION_DEFAULT;
}
}
} // namespace
int main(int argc, char **argv) {
if (argc < 2 || argc > 3) return Usage(argv[0]);
ots::EnableWOFF2();
const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS);
if (fd < 0) {
::perror("open");
return 1;
}
struct stat st;
::fstat(fd, &st);
uint8_t *data = new uint8_t[st.st_size];
if (::read(fd, data, st.st_size) != st.st_size) {
::perror("read");
return 1;
}
::close(fd);
ots::OTSContext context;
context.SetMessageCallback(&Message, NULL);
context.SetTableActionCallback(&TableActionCallback, NULL);
FILE* out = NULL;
if (argc == 3)
out = fopen(argv[2], "wb");
ots::FILEStream output(out);
const bool result = context.Process(&output, data, st.st_size);
if (!result) {
std::fprintf(stderr, "Failed to sanitise file!\n");
}
return !result;
}