From cefbd7d4558ff8f54719d6e7fadd48f42cb8c201 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Wed, 5 Feb 2014 09:31:34 +0200 Subject: [PATCH] Make Process() a method of ots::OTSContext MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Makes it easier to access the context’s private members without the ugly getters. --- include/opentype-sanitiser.h | 34 +++++++++------------------------- src/ots.cc | 13 +++++++------ test/idempotent.cc | 6 ++++-- test/ot-sanitise.cc | 2 +- test/perf.cc | 3 ++- test/side-by-side.cc | 3 ++- test/validator-checker.cc | 3 ++- 7 files changed, 27 insertions(+), 37 deletions(-) diff --git a/include/opentype-sanitiser.h b/include/opentype-sanitiser.h index 5d246150..b5090c96 100644 --- a/include/opentype-sanitiser.h +++ b/include/opentype-sanitiser.h @@ -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() @@ -216,17 +213,21 @@ 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) { @@ -234,11 +235,6 @@ class OTSContext { 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; @@ -246,18 +242,6 @@ class OTSContext { 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(); diff --git a/src/ots.cc b/src/ots.cc index 5ab8599a..18d86142 100644 --- a/src/ots.cc +++ b/src/ots.cc @@ -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"); diff --git a/test/idempotent.cc b/test/idempotent.cc index a569d02c..ec50ab44 100644 --- a/test/idempotent.cc +++ b/test/idempotent.cc @@ -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; @@ -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; diff --git a/test/ot-sanitise.cc b/test/ot-sanitise.cc index 63b0ec1a..af790e8b 100644 --- a/test/ot-sanitise.cc +++ b/test/ot-sanitise.cc @@ -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"); diff --git a/test/perf.cc b/test/perf.cc index b7d8cfc9..88a15f8c 100644 --- a/test/perf.cc +++ b/test/perf.cc @@ -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; diff --git a/test/side-by-side.cc b/test/side-by-side.cc index 5c26ac8a..c34f64a0 100644 --- a/test/side-by-side.cc +++ b/test/side-by-side.cc @@ -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; diff --git a/test/validator-checker.cc b/test/validator-checker.cc index 89abb0f2..639641b2 100644 --- a/test/validator-checker.cc +++ b/test/validator-checker.cc @@ -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;