1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <woff2/decode.h>
16 #include <woff2/encode.h>
17 #include <woff2/output.h>
18
19 #include <cinttypes>
20 #include <cstddef>
21 #include <memory>
22
WOFF2_ConvertWOFF2ToTTF(const uint8_t * data,size_t length,uint8_t ** result,size_t * result_length,size_t max_size)23 extern "C" bool WOFF2_ConvertWOFF2ToTTF(const uint8_t* data, size_t length,
24 uint8_t** result, size_t* result_length,
25 size_t max_size) {
26 if (result) {
27 *result = nullptr;
28 }
29 if (result_length) {
30 *result_length = 0;
31 }
32 if (!data || !length || !result || !result_length) {
33 return false;
34 }
35 size_t final_size = ::woff2::ComputeWOFF2FinalSize(data, length);
36 if (final_size > (max_size ? max_size : woff2::kDefaultMaxSize)) {
37 return false;
38 }
39 auto buffer = std::make_unique<uint8_t[]>(final_size);
40 woff2::WOFF2MemoryOut output(buffer.get(), final_size);
41 if (!::woff2::ConvertWOFF2ToTTF(data, length, &output)) {
42 return false;
43 }
44 *result = buffer.release();
45 *result_length = final_size;
46 return true;
47 }
48
WOFF2_ConvertTTFToWOFF2(const uint8_t * data,size_t length,uint8_t ** result,size_t * result_length)49 extern "C" bool WOFF2_ConvertTTFToWOFF2(const uint8_t* data, size_t length,
50 uint8_t** result,
51 size_t* result_length) {
52 if (result) {
53 *result = nullptr;
54 }
55 if (result_length) {
56 *result_length = 0;
57 }
58 if (!data || !length || !result || !result_length) {
59 return false;
60 }
61 size_t size = woff2::MaxWOFF2CompressedSize(data, length);
62 auto buffer = std::make_unique<uint8_t[]>(size);
63 if (!buffer) {
64 return false;
65 }
66 if (!woff2::ConvertTTFToWOFF2(data, length, buffer.get(), &size)) {
67 return false;
68 }
69 *result = buffer.release();
70 *result_length = size;
71 return true;
72 }
73
WOFF2_Free(uint8_t * data)74 extern "C" void WOFF2_Free(uint8_t* data) noexcept {
75 std::unique_ptr<uint8_t[]> p{data};
76 }
77