SPB Git

Repository Public License

woff2shim.cc 6.2 KB 160 lines cpp

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project          : ORIX Convert10 * File             : Packages/EngineFont/Sources/CWOFF2Shim/woff2shim.cc11 * Description      : C interface over google/woff2 (C++): bounded WOFF2 encode12 *                    and decode, no exception crosses the boundary.13 * Module           : CWOFF2Shim14 * Repository       : https://git.spboucher.ai/orix-convert15 * Created          : 2026-11-2116 * Last Modified    : 2026-11-2117 *18 * =============================================================================19 * AUTHOR & PROFESSIONAL AFFILIATION20 * =============================================================================21 *22 * Author           : Simon-Pierre Boucher23 * Position         : Professor of Finance24 * Institution      : Université du Québec en Outaouais (UQO)25 * Department       : Département des sciences administratives26 * Organization     : SP Boucher27 *28 * Website          : https://spboucher.ai29 * Git              : https://git.spboucher.ai30 *31 * =============================================================================32 * OFFICIAL CONTACT DIRECTORY33 * =============================================================================34 *35 * General          : info@spboucher.ai36 * Professional     : contact@spboucher.ai37 * Direct           : simon@spboucher.ai38 * Research         : research@spboucher.ai39 * Development      : dev@spboucher.ai40 * Support          : support@spboucher.ai41 * Security/Admin   : admin@spboucher.ai42 * Notifications    : notifications@spboucher.ai43 *44 * Preferred Contact: support@spboucher.ai45 *46 * =============================================================================47 * COPYRIGHT & INTELLECTUAL PROPERTY48 * =============================================================================49 *50 * Copyright (c) 202651 * Simon-Pierre Boucher52 *53 * All rights reserved where applicable.54 *55 * This file is governed by the applicable project license,56 * copyright notices, institutional agreements and relevant law.57 *58 * Unless expressly authorized by the applicable license or59 * rights holder, copying, redistribution, sublicensing,60 * publication and commercial exploitation are not permitted.61 *62 * Existing third-party rights and notices remain unaffected.63 *64 * =============================================================================65 * CONFIDENTIALITY & SECURITY66 * =============================================================================67 *68 * Classification   : Public69 * Distribution     : Public repository; use is governed by the project license70 * Security Contact : admin@spboucher.ai71 *72 * Access to this source does not independently grant permission73 * to redistribute or disclose confidential materials.74 *75 * Do not embed credentials, private keys, access tokens,76 * certificates or confidential personal information.77 *78 * =============================================================================79 * LICENSE & MAINTENANCE80 * =============================================================================81 *82 * License          : Proprietary, all rights reserved (see license file)83 * License File     : LICENSE84 * Maintainer       : Simon-Pierre Boucher85 * Technical Contact: dev@spboucher.ai86 *87 * =============================================================================88 * END OF SP BOUCHER SOURCE HEADER89 * =============================================================================90 */9192#include "woff2shim.h"9394#include <algorithm>95#include <cstdlib>96#include <cstring>97#include <string>9899#include <woff2/decode.h>100#include <woff2/encode.h>101#include <woff2/output.h>102103extern "C" size_t orix_woff2_max_compressed_size(const uint8_t *data, size_t length, const char *metadata, size_t metadata_length) {104    try {105        std::string meta = metadata ? std::string(metadata, metadata_length) : std::string();106        return woff2::MaxWOFF2CompressedSize(data, length, meta);107    } catch (...) {108        return 0;109    }110}111112extern "C" int orix_woff2_encode(const uint8_t *data, size_t length, uint8_t *out, size_t *out_length, int quality,113                                 int allow_transforms, const char *metadata, size_t metadata_length) {114    if (!data || !out || !out_length) return 0;115    try {116        woff2::WOFF2Params params;117        if (metadata) params.extended_metadata = std::string(metadata, metadata_length);118        params.brotli_quality = std::min(11, std::max(0, quality));119        params.allow_transforms = allow_transforms != 0;120        return woff2::ConvertTTFToWOFF2(data, length, out, out_length, params) ? 1 : 0;121    } catch (...) {122        return 0;123    }124}125126extern "C" size_t orix_woff2_final_size(const uint8_t *data, size_t length) {127    if (!data) return 0;128    try {129        return woff2::ComputeWOFF2FinalSize(data, length);130    } catch (...) {131        return 0;132    }133}134135extern "C" int orix_woff2_decode(const uint8_t *data, size_t length, size_t max_size, uint8_t **out, size_t *out_length) {136    if (!data || !out || !out_length) return 0;137    try {138        std::string buffer;139        size_t announced = woff2::ComputeWOFF2FinalSize(data, length);140        if (announced == 0 || announced > max_size) return 0;141        // The announced size is only a hint: reserve no more than a sane multiple of the compressed size.142        buffer.reserve(std::min(announced, std::max<size_t>(length * 16, 1 << 20)));143        woff2::WOFF2StringOut output(&buffer);144        output.SetMaxSize(max_size);145        if (!woff2::ConvertWOFF2ToTTF(data, length, &output)) return 0;146        size_t size = output.Size();147        if (size == 0 || size > buffer.size()) return 0;148        uint8_t *copy = static_cast<uint8_t *>(std::malloc(size));149        if (!copy) return 0;150        std::memcpy(copy, buffer.data(), size);151        *out = copy;152        *out_length = size;153        return 1;154    } catch (...) {155        return 0;156    }157}158159extern "C" void orix_woff2_free(uint8_t *buffer) { std::free(buffer); }160
Files main
Menu

Theme