Skip to content

Commit

Permalink
Make Process() a method of ots::OTSContext
Browse files Browse the repository at this point in the history
Makes it easier to access the context’s private members without the ugly
getters.
  • Loading branch information
khaledhosny committed Feb 5, 2014
1 parent 4100fb9 commit cefbd7d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 37 deletions.
34 changes: 9 additions & 25 deletions include/opentype-sanitiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,6 @@ enum TableAction {
// user_data: user defined data that are passed to SetTableActionCallback()
typedef TableAction (*TableActionFunc)(uint32_t tag, void *user_data);

// -----------------------------------------------------------------------------
// Context that holds various OTS settings, to be passed to ots::Process().
// -----------------------------------------------------------------------------
class OTSContext {
public:
OTSContext()
Expand All @@ -216,48 +213,35 @@ class OTSContext {

~OTSContext() {}

// Process a given OpenType file and write out a sanitised version
// output: a pointer to an object implementing the OTSStream interface. The
// sanitisied output will be written to this. In the even of a failure,
// partial output may have been written.
// input: the OpenType file
// length: the size, in bytes, of |input|
// context: optional context that holds various OTS settings like user callbacks
bool Process(OTSStream *output, const uint8_t *input, size_t length);

// Set a callback function that will be called when OTS is reporting an error.
void SetMessageCallback(MessageFunc func, void *user_data) {
message_func = func;
message_user_data = user_data;
}

void* GetMessageCallback(MessageFunc *func) {
*func = message_func;
return message_user_data;
}

// Set a callback function that will be called when OTS needs to decide what to
// do for a font table.
void SetTableActionCallback(TableActionFunc func, void *user_data) {
table_action_func = func;
table_action_user_data = user_data;
}

void* GetTableActionCallback(TableActionFunc *func) {
*func = table_action_func;
return table_action_user_data;
}

private:
MessageFunc message_func;
void *message_user_data;
TableActionFunc table_action_func;
void *table_action_user_data;
};

// -----------------------------------------------------------------------------
// Process a given OpenType file and write out a sanitised version
// output: a pointer to an object implementing the OTSStream interface. The
// sanitisied output will be written to this. In the even of a failure,
// partial output may have been written.
// input: the OpenType file
// length: the size, in bytes, of |input|
// context: optional context that holds various OTS settings like user callbacks
// -----------------------------------------------------------------------------
bool Process(OTSStream *output, const uint8_t *input, size_t length,
OTSContext *context = NULL);

// Force to disable debug output even when the library is compiled with
// -DOTS_DEBUG.
void DisableDebugOutput();
Expand Down
13 changes: 7 additions & 6 deletions src/ots.cc
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,15 @@ void EnableWOFF2() {
g_enable_woff2 = true;
}

bool Process(OTSStream *output, const uint8_t *data, size_t length,
OTSContext *context) {
bool OTSContext::Process(OTSStream *output,
const uint8_t *data,
size_t length) {
OpenTypeFile header;

if (context != NULL) {
header.message_user_data = context->GetMessageCallback(&header.message_func);
header.table_action_user_data = context->GetTableActionCallback(&header.table_action_func);
}
header.message_func = message_func;
header.message_user_data = message_user_data;
header.table_action_func = table_action_func;
header.table_action_user_data = table_action_user_data;

if (length < 4) {
return OTS_FAILURE_MSG_(&header, "file less than 4 bytes");
Expand Down
6 changes: 4 additions & 2 deletions test/idempotent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ int main(int argc, char **argv) {
uint8_t *result = new uint8_t[file_size * 8];
ots::MemoryStream output(result, file_size * 8);

bool r = ots::Process(&output, data, file_size);
ots::OTSContext context;

bool r = context.Process(&output, data, file_size);
if (!r) {
std::fprintf(stderr, "Failed to sanitise file!\n");
return 1;
Expand All @@ -182,7 +184,7 @@ int main(int argc, char **argv) {

uint8_t *result2 = new uint8_t[result_len];
ots::MemoryStream output2(result2, result_len);
r = ots::Process(&output2, result, result_len);
r = context.Process(&output2, result, result_len);
if (!r) {
std::fprintf(stderr, "Failed to sanitise previous output!\n");
return 1;
Expand Down
2 changes: 1 addition & 1 deletion test/ot-sanitise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int main(int argc, char **argv) {
out = fopen(argv[2], "wb");

ots::FILEStream output(out);
const bool result = ots::Process(&output, data, st.st_size, &context);
const bool result = context.Process(&output, data, st.st_size);

if (!result) {
std::fprintf(stderr, "Failed to sanitise file!\n");
Expand Down
3 changes: 2 additions & 1 deletion test/perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ int main(int argc, char **argv) {
::gettimeofday(&start, 0);
for (int i = 0; i < num_repeat; ++i) {
ots::MemoryStream output(result, st.st_size + kPadLen);
bool r = ots::Process(&output, data, st.st_size);
ots::OTSContext context;
bool r = context.Process(&output, data, st.st_size);
if (!r) {
std::fprintf(stderr, "Failed to sanitise file!\n");
return 1;
Expand Down
3 changes: 2 additions & 1 deletion test/side-by-side.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ int main(int argc, char **argv) {
static const size_t kPadLen = 20 * 1024;
uint8_t *trans_font = new uint8_t[orig_len + kPadLen];
ots::MemoryStream output(trans_font, orig_len + kPadLen);
ots::OTSContext context;

bool result = ots::Process(&output, orig_font, orig_len);
bool result = context.Process(&output, orig_font, orig_len);
if (!result) {
std::fprintf(stderr, "Failed to sanitise file! %s\n", argv[1]);
return 1;
Expand Down
3 changes: 2 additions & 1 deletion test/validator-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ int main(int argc, char **argv) {
static const size_t kBigPadLen = 1024 * 1024; // 1MB
uint8_t *trans_font = new uint8_t[orig_len + kBigPadLen];
ots::MemoryStream output(trans_font, orig_len + kBigPadLen);
ots::OTSContext context;

bool result = ots::Process(&output, orig_font, orig_len);
bool result = context.Process(&output, orig_font, orig_len);
if (!result) {
std::fprintf(stderr, "OK: the malicious font was filtered: %s\n", argv[1]);
return 0;
Expand Down

0 comments on commit cefbd7d

Please sign in to comment.