Skip to content

Commit

Permalink
Fix build break for OTS and reduce Starboardization (#2133)
Browse files Browse the repository at this point in the history
Fix OTS build break on newer glibc, and removes obsolete
Starboardizations.
Config header added to reduce required inline modifications.

b/37501879
  • Loading branch information
kaidokert committed Dec 28, 2023
1 parent b8b600d commit bcd59ce
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 146 deletions.
3 changes: 2 additions & 1 deletion third_party/ots/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

config("ots_config") {
include_dirs = [ "include" ]
defines = [ "HAVE_CONFIG_H" ]
}

static_library("ots") {
Expand Down Expand Up @@ -104,8 +105,8 @@ static_library("ots") {

deps = [
"//starboard/common",
"//third_party/woff2:woff2_dec",
"//third_party/brotli:dec",
"//third_party/woff2:woff2_dec",
"//third_party/zlib",
]
}
28 changes: 28 additions & 0 deletions third_party/ots/include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STARBOARD_OTS_CONFIG_H
#define STARBOARD_OTS_CONFIG_H

#if !defined(STARBOARD)
#error "This config is only used for Starboard builds"
#endif

// Force debugging off, Starboard doesn't implement it
#undef OTS_DEBUG

// Address compilation errors
#include <stdint.h>

#endif // STARBOARD_OTS_CONFIG_H
78 changes: 20 additions & 58 deletions third_party/ots/include/opentype-sanitiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#if defined(STARBOARD)
#include "starboard/common/byte_swap.h"
#define NTOHL_OPENTYPE_SANITISER(x) SB_NET_TO_HOST_U32(x)
#define NTOHS_OPENTYPE_SANITISER(x) SB_NET_TO_HOST_U16(x)
#define HTONL_OPENTYPE_SANITISER(x) SB_HOST_TO_NET_U32(x)
#define HTONS_OPENTYPE_SANITISER(x) SB_HOST_TO_NET_U16(x)
#define ots_ntohl(x) SB_NET_TO_HOST_U32(x)
#define ots_ntohs(x) SB_NET_TO_HOST_U16(x)
#define ots_htonl(x) SB_HOST_TO_NET_U32(x)
#define ots_htons(x) SB_HOST_TO_NET_U16(x)
#elif defined(_WIN32)
#include <stdlib.h>
typedef signed char int8_t;
Expand All @@ -21,17 +21,17 @@ typedef int int32_t;
typedef unsigned int uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#define NTOHL_OPENTYPE_SANITISER(x) _byteswap_ulong (x)
#define NTOHS_OPENTYPE_SANITISER(x) _byteswap_ushort (x)
#define HTONL_OPENTYPE_SANITISER(x) _byteswap_ulong (x)
#define HTONS_OPENTYPE_SANITISER(x) _byteswap_ushort (x)
#define ots_ntohl(x) _byteswap_ulong (x)
#define ots_ntohs(x) _byteswap_ushort (x)
#define ots_htonl(x) _byteswap_ulong (x)
#define ots_htons(x) _byteswap_ushort (x)
#else
#include <arpa/inet.h>
#include <stdint.h>
#define NTOHL_OPENTYPE_SANITISER(x) ntohl (x)
#define NTOHS_OPENTYPE_SANITISER(x) ntohs (x)
#define HTONL_OPENTYPE_SANITISER(x) htonl (x)
#define HTONS_OPENTYPE_SANITISER(x) htons (x)
#define ots_ntohl(x) ntohl (x)
#define ots_ntohs(x) ntohs (x)
#define ots_htonl(x) htonl (x)
#define ots_htons(x) htons (x)
#endif

#include <sys/types.h>
Expand Down Expand Up @@ -78,14 +78,7 @@ class OTSStream {
const size_t l = std::min(length, static_cast<size_t>(4) - chksum_offset);
uint32_t tmp = 0;
std::memcpy(reinterpret_cast<uint8_t *>(&tmp) + chksum_offset, data, l);
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4267) // possible loss of data
#endif
chksum_ += NTOHL_OPENTYPE_SANITISER(tmp);
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
chksum_ += ots_ntohl(tmp);
length -= l;
offset += l;
}
Expand All @@ -94,7 +87,7 @@ class OTSStream {
uint32_t tmp;
std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset,
sizeof(uint32_t));
chksum_ += NTOHL_OPENTYPE_SANITISER(tmp);
chksum_ += ots_ntohl(tmp);
length -= 4;
offset += 4;
}
Expand All @@ -104,19 +97,7 @@ class OTSStream {
uint32_t tmp = 0;
std::memcpy(&tmp,
reinterpret_cast<const uint8_t*>(data) + offset, length);
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4267) // possible loss of data
#elif defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#endif
chksum_ += NTOHL_OPENTYPE_SANITISER(tmp);
#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#endif
chksum_ += ots_ntohl(tmp);
}

return WriteRaw(data, orig_length);
Expand Down Expand Up @@ -144,41 +125,27 @@ class OTSStream {
}

bool WriteU16(uint16_t v) {
v = HTONS_OPENTYPE_SANITISER(v);
v = ots_htons(v);
return Write(&v, sizeof(v));
}

bool WriteS16(int16_t v) {
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4365) // signed/unsigned mismatch
#endif
v = HTONS_OPENTYPE_SANITISER(v);
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
v = ots_htons(v);
return Write(&v, sizeof(v));
}

bool WriteU24(uint32_t v) {
v = HTONL_OPENTYPE_SANITISER(v);
v = ots_htonl(v);
return Write(reinterpret_cast<uint8_t*>(&v)+1, 3);
}

bool WriteU32(uint32_t v) {
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4365) // signed/unsigned mismatch
#endif
v = HTONL_OPENTYPE_SANITISER(v);
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
v = ots_htonl(v);
return Write(&v, sizeof(v));
}

bool WriteS32(int32_t v) {
v = HTONL_OPENTYPE_SANITISER(v);
v = ots_htonl(v);
return Write(&v, sizeof(v));
}

Expand Down Expand Up @@ -242,9 +209,4 @@ class OTSContext {

} // namespace ots

#undef NTOHL_OPENTYPE_SANITISER
#undef NTOHS_OPENTYPE_SANITISER
#undef HTONL_OPENTYPE_SANITISER
#undef HTONS_OPENTYPE_SANITISER

#endif // OPENTYPE_SANITISER_H_
18 changes: 2 additions & 16 deletions third_party/ots/include/ots-memory-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ class MemoryStream : public OTSStream {
return false;
}
std::memcpy(static_cast<char*>(ptr_) + off_, data, length);
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4267) // possible loss of data
#endif
off_ += length;
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
off_ += static_cast<off_t>(length);
return true;
}

Expand Down Expand Up @@ -85,14 +78,7 @@ class ExpandingMemoryStream : public OTSStream {
return WriteRaw(data, length);
}
std::memcpy(static_cast<char*>(ptr_) + off_, data, length);
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4267) // possible loss of data
#endif
off_ += length;
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
off_ += static_cast<off_t>(length);
return true;
}

Expand Down
8 changes: 0 additions & 8 deletions third_party/ots/src/cff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
#include "cff_charstring.h"
#include "variations.h"

#if defined(STARBOARD)
#include "starboard/client_porting/poem/string_poem.h"
#define STRCHR_OTS strchr
#else
#define STRCHR_OTS std::strchr
#endif

// CFF - PostScript font program (Compact Font Format) table
// http://www.microsoft.com/typography/otspec/cff.htm
// http://www.microsoft.com/typography/otspec/cffspec.htm
Expand Down Expand Up @@ -1369,5 +1362,4 @@ bool OpenTypeCFF2::Serialize(OTSStream *out) {

} // namespace ots

#undef STRCHR_OTS
#undef TABLE_NAME
24 changes: 1 addition & 23 deletions third_party/ots/src/cmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@
#include <utility>
#include <vector>

#if defined(STARBOARD)
#include "starboard/common/byte_swap.h"
#define NTOHS_CMAP(x) SB_NET_TO_HOST_U16(x)
#elif defined(_WIN32)
#include <stdlib.h>
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#define NTOHS_CMAP(x) _byteswap_ushort (x)
#else
#include <arpa/inet.h>
#include <stdint.h>
#define NTOHS_CMAP(x) ntohs (x)
#endif

#include "maxp.h"
#include "os2.h"

Expand Down Expand Up @@ -258,7 +238,7 @@ bool OpenTypeCMAP::ParseFormat4(int platform, int encoding,
}
uint16_t glyph;
std::memcpy(&glyph, data + glyph_id_offset, 2);
glyph = NTOHS_CMAP(glyph);
glyph = ots_ntohs(glyph);
if (glyph >= num_glyphs) {
return Error("Range glyph reference too high (%d > %d)", glyph, num_glyphs - 1);
}
Expand Down Expand Up @@ -1091,5 +1071,3 @@ bool OpenTypeCMAP::Serialize(OTSStream *out) {
}

} // namespace ots

#undef NTOHS_CMAP
9 changes: 0 additions & 9 deletions third_party/ots/src/name.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
#include <cstring>
#include <cctype>

#if defined(STARBOARD)
#include "starboard/client_porting/poem/string_poem.h"
#define STRCHR_OTS strchr
#else
#define STRCHR_OTS std::strchr
#endif

// name - Naming Table
// http://www.microsoft.com/typography/otspec/name.htm

Expand Down Expand Up @@ -374,5 +367,3 @@ bool OpenTypeNAME::IsValidNameId(uint16_t nameID, bool addIfMissing) {
}

} // namespace

#undef STRCHR_OTS
35 changes: 4 additions & 31 deletions third_party/ots/src/ots.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,6 @@
#include <stddef.h>
#include <cstdarg>
#include <cstddef>

#if defined(STARBOARD)
#include "starboard/common/byte_swap.h"
#define NTOHL_OTS(x) SB_NET_TO_HOST_U32(x)
#define NTOHS_OTS(x) SB_NET_TO_HOST_U16(x)
#elif defined(_WIN32)
#include <stdlib.h>
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#define NTOHL_OTS(x) _byteswap_ulong (x)
#define NTOHS_OTS(x) _byteswap_ushort (x)
#else
#include <arpa/inet.h>
#include <stdint.h>
#define NTOHL_OTS(x) ntohl (x)
#define NTOHS_OTS(x) ntohs (x)
#endif

#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand All @@ -51,7 +27,7 @@ char (&ArraySizeHelper(T (&array)[N]))[N];

namespace ots {

#if defined(STARBOARD) || !defined(OTS_DEBUG)
#if !defined(OTS_DEBUG)
#define OTS_FAILURE() false
#else
#define OTS_FAILURE() \
Expand All @@ -66,7 +42,7 @@ namespace ots {
// message-less OTS_FAILURE(), so that the current parser will return 'false' as
// its result (indicating a failure).

#if defined(STARBOARD) || !defined(OTS_DEBUG)
#if !defined(OTS_DEBUG)
#define OTS_MESSAGE_(level,otf_,...) \
(otf_)->context->Message(level,__VA_ARGS__)
#else
Expand Down Expand Up @@ -136,7 +112,7 @@ class Buffer {
return OTS_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
*value = NTOHS_OTS(*value);
*value = ots_ntohs(*value);
offset_ += 2;
return true;
}
Expand All @@ -161,7 +137,7 @@ class Buffer {
return OTS_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
*value = NTOHL_OTS(*value);
*value = ots_ntohl(*value);
offset_ += 4;
return true;
}
Expand Down Expand Up @@ -377,7 +353,4 @@ struct FontFile {

} // namespace ots

#undef NTOHL_OTS
#undef NTOHS_OTS

#endif // OTS_H_

0 comments on commit bcd59ce

Please sign in to comment.