Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,70 @@ jobs:
run: swift test
- name: Build and test without FreeType
run: swift test --disable-default-traits --scratch-path .build-notrait

armv7:
name: Linux (armv7, build)
runs-on: ubuntu-latest
# Swift's modules only load in the compiler that built them, so the
# image's version and the SDK's must be the same.
container: swift:6.4.3-trixie
env:
SWIFT_VERSION: "6.4.3"
SDK_SHA256: 243184bf809a3a17d36b9b411e353e8ad07585cdbff1129dd1bb969882352a0f
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ca-certificates curl git
- name: Cache the armv7 SDK
id: cache-sdk
uses: actions/cache@v4
with:
path: /opt/armv7-sdk
key: armv7-sdk-${{ env.SWIFT_VERSION }}-trixie-${{ env.SDK_SHA256 }}
# Swift has no official 32-bit ARM SDK: this is the community's.
- name: Download the armv7 SDK
if: steps.cache-sdk.outputs.cache-hit != 'true'
run: |
name="swift-$SWIFT_VERSION-RELEASE-debian-trixie-armv7-sdk.tar.gz"
curl -fsSLo /tmp/sdk.tar.gz \
"https://github.com/swift-embedded-linux/armhf-debian/releases/download/$SWIFT_VERSION/$name"
echo "$SDK_SHA256 /tmp/sdk.tar.gz" | sha256sum -c -
mkdir -p /opt/armv7-sdk
tar -xzf /tmp/sdk.tar.gz -C /opt/armv7-sdk
- name: Describe the destination
run: |
sdk="/opt/armv7-sdk/swift-$SWIFT_VERSION-RELEASE-debian-trixie-armv7/debian-trixie"
plugins="$(dirname "$(command -v swiftc)")/../lib/swift/host/plugins"
cat > armv7.json <<JSON
{
"version": 1,
"sdk": "$sdk",
"toolchain-bin-dir": "$(dirname "$(command -v swiftc)")",
"target": "armv7-unknown-linux-gnueabihf",
"dynamic-library-extension": "so",
"extra-cc-flags": ["-march=armv7-a", "-mfpu=neon", "-mfloat-abi=hard", "-D_FILE_OFFSET_BITS=64", "-fPIC"],
"extra-swiftc-flags": [
"-target", "armv7-unknown-linux-gnueabihf", "-use-ld=lld", "-Xcc", "--gcc-toolchain=$sdk/usr",
"-resource-dir", "$sdk/usr/lib/swift", "-sdk", "$sdk", "-latomic",
"-plugin-path", "$plugins", "-plugin-path", "$plugins/testing"
],
"extra-cpp-flags": []
}
JSON
- name: Swift version
run: swift --version
# Without FreeType, whose armv7 library the SDK lacks, and without the
# Cacao host, renderer and demo, whose libraries it lacks too. The
# test targets are compiled, not run. A destination file needs the
# native build system.
- name: Build
run: |
targets="Graphite GraphiteMemory GraphiteMixedMode GraphiteQuickDrawABI"
for name in Core Tasks Files Resources Automation 2D Text Sound Windows Menus Controls Dialogs Events Memory MixedMode QuickDrawABI; do
targets="$targets Graphite${name}Tests"
done
for target in $targets; do
swift build --build-system native --destination armv7.json --disable-default-traits --target "$target"
done
8 changes: 5 additions & 3 deletions Sources/Graphite2D/Cursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public struct Cursor: Hashable, Sendable {
do {
var reader = BigEndianReader(source)
try reader.skip(2) // crsrType
let mapOffset = Int(try reader.read(UInt32.self))
let dataOffset = Int(try reader.read(UInt32.self))
let mapOffset = Int(clamping: try reader.read(UInt32.self))
let dataOffset = Int(clamping: try reader.read(UInt32.self))
try reader.skip(4 + 2 + 4) // crsrXData, crsrXValid, crsrXHandle
let data = try (0 ..< 16).map { _ throws(BigEndianReadError) in try reader.read(UInt16.self) }
let mask = try (0 ..< 16).map { _ throws(BigEndianReadError) in try reader.read(UInt16.self) }
Expand All @@ -78,9 +78,11 @@ public struct Cursor: Hashable, Sendable {
try map.skip(2 + 2 + 4 + 4 + 4 + 2)
let depth = Int(try map.read(UInt16.self))
try map.skip(2 + 2 + 4)
let tableOffset = Int(try map.read(UInt32.self))
let tableOffset = Int(clamping: try map.read(UInt32.self))
let width = right - left, height = bottom - top
guard width > 0, height > 0, width <= 64, height <= 64, [1, 2, 4, 8].contains(depth) else { throw MacError.parameter }
// Each row holds all its pixels, so indexing them stays inside.
guard rowBytes >= (width * depth + 7) / 8 else { throw MacError.parameter }
var tableReader = try BigEndianReader(source).subreader(at: tableOffset, count: 8)
try tableReader.skip(6)
let entries = Int(try tableReader.read(UInt16.self)) + 1
Expand Down
7 changes: 5 additions & 2 deletions Sources/Graphite2D/IconFamily.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public struct IconFamily: Sendable {
var reader = BigEndianReader(bytes)
do {
guard try reader.read(UInt32.self) == FourCharCode("icns").rawValue else { throw MacError.parameter }
let length = min(Int(try reader.read(UInt32.self)), reader.count)
let length = min(Int(clamping: try reader.read(UInt32.self)), reader.count)
members = [:]
while reader.offset + 8 <= length {
let type = FourCharCode(try reader.read(UInt32.self))
let size = Int(try reader.read(UInt32.self))
let size = Int(clamping: try reader.read(UInt32.self))
guard size >= 8 else { throw MacError.parameter }
members[type] = Array(try reader.readBytes(size - 8))
}
Expand Down Expand Up @@ -161,6 +161,9 @@ public struct ColorIcon: Sendable {
try reader.skip(8 + 4) // its bounds, iconData
let height = bounds.height, width = bounds.width
guard width > 0, height > 0, width <= 1024, height <= 1024, [1, 2, 4, 8].contains(depth) else { throw MacError.parameter }
// Each row holds all its pixels and mask bits, so indexing them
// stays inside.
guard rowBytes >= (width * depth + 7) / 8, maskRowBytes >= (width + 7) / 8 else { throw MacError.parameter }
let mask = Array(try reader.readBytes(maskRowBytes * height))
try reader.skip(bitmapRowBytes * height)
let tableStart = reader.offset
Expand Down
17 changes: 10 additions & 7 deletions Sources/Graphite2D/Picture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private struct Parser {

private mutating func skipWordLength() throws { try reader.skip(Int(try reader.read(UInt16.self))) }

private mutating func skipLongLength() throws { try reader.skip(Int(try reader.read(UInt32.self))) }
private mutating func skipLongLength() throws { try reader.skip(Int(clamping: try reader.read(UInt32.self))) }

private mutating func operation(_ opcode: Int) throws -> Picture.Operation? {
switch opcode {
Expand Down Expand Up @@ -434,27 +434,30 @@ private struct Parser {
/// CompressedQuickTime: its matrix's translation and scale place the
/// image's source rectangle; its image description says the codec.
private mutating func compressedImage() throws -> Picture.Operation? {
let length = Int(try reader.read(UInt32.self))
let length = Int(clamping: try reader.read(UInt32.self))
var data = try reader.subreader(length)
try data.skip(2) // version
var matrix: [Int32] = []
for _ in 0 ..< 9 { matrix.append(Int32(bitPattern: try data.read(UInt32.self))) }
let matteSize = Int(try data.read(UInt32.self))
let matteSize = Int(clamping: try data.read(UInt32.self))
try data.skip(8) // matteRect
let mode = TransferMode(rawValue: Int16(bitPattern: try data.read(UInt16.self)))
let source = try PicturePixels.readRect(&data)
try data.skip(4) // accuracy
let maskSize = Int(try data.read(UInt32.self))
try data.skip(matteSize + maskSize)
let maskSize = Int(clamping: try data.read(UInt32.self))
// One at a time: their sum can overflow on a 32-bit CPU.
try data.skip(matteSize)
try data.skip(maskSize)
let descriptionStart = data.offset
let descriptionSize = Int(try data.read(UInt32.self))
let descriptionSize = Int(clamping: try data.read(UInt32.self))
let codec = FourCharCode(try data.read(UInt32.self))
try data.seek(to: descriptionStart + 32)
let width = Int(try data.read(UInt16.self)), height = Int(try data.read(UInt16.self))
try data.seek(to: descriptionStart + 44)
let dataSize = Int(try data.read(UInt32.self))
let dataSize = Int(clamping: try data.read(UInt32.self))
try data.seek(to: descriptionStart + 82)
let depth = Int(try data.read(UInt16.self))
guard descriptionSize <= data.count - descriptionStart else { throw MacError.parameter }
try data.seek(to: descriptionStart + descriptionSize)
let image = Array(try data.readBytes(min(dataSize, data.remaining)))
// The matrix's scale (Fixed) and translation (Fixed) place the
Expand Down
6 changes: 3 additions & 3 deletions Sources/GraphiteAutomation/DescriptorClassicData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public extension Descriptor {
let start = try data.long()
data.position = 16
let count = try data.long()
data.position = Int(start)
data.position = Int(clamping: start)
self.init(type: type, items: try data.items(count, keyed: !isList, depth: 0), isKeyed: !isList)
}

Expand Down Expand Up @@ -159,7 +159,7 @@ private struct ClassicData {
mutating func code() throws(MacError) -> FourCharCode { FourCharCode(rawValue: try long()) }

mutating func value() throws(MacError) -> [UInt8] {
let size = Int(try long())
let size = Int(clamping: try long())
guard size <= bytes.count - position else { throw .corruptData }
defer { position += size + (size & 1) }
return Array(bytes[position ..< position + size])
Expand Down Expand Up @@ -224,7 +224,7 @@ private struct ClassicData {
descriptor: Descriptor(type: DescriptorType.sInt32, bytes: big(0))))
}
attributes += listed
position = Int(parameters)
position = Int(clamping: parameters)
return Descriptor(type: DescriptorType.appleEvent, items: try items(parameterCount, keyed: true, depth: 0),
attributes: attributes, isKeyed: true)
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/GraphiteCore/Extended80.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public struct Extended80: Hashable, Sendable {
if leading > Double.greatestFiniteMagnitude.exponent {
return Double(sign: sign, exponent: 0, significand: .infinity)
}
if leading < Double.leastNonzeroMagnitude.exponent - 1 {
// Below half the smallest denormal, 2^-1074. Not asked of
// `leastNonzeroMagnitude`, which on 32-bit ARM is the smallest
// normal number.
if leading < -1075 {
return Double(sign: sign, exponent: 0, significand: 0)
}
return Double(sign: sign, exponent: exponent - 63, significand: Double(mantissa))
Expand Down
4 changes: 2 additions & 2 deletions Sources/GraphiteFiles/AppleDouble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public struct AppleDouble: Hashable, Sendable {
entries = []
for _ in 0 ..< count {
let id = try reader.read(UInt32.self)
let offset = Int(try reader.read(UInt32.self))
let length = Int(try reader.read(UInt32.self))
let offset = Int(clamping: try reader.read(UInt32.self))
let length = Int(clamping: try reader.read(UInt32.self))
let entry = try reader.subreader(at: offset, count: length)
entries.append(Entry(id: EntryID(rawValue: id), bytes: Array(entry.bytes)))
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/GraphiteResources/ResourceMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ public struct ResourceMap: Hashable, Sendable {
guard bytes.count >= 16 else { throw .endOfFile }
do {
var header = BigEndianReader(bytes)
let dataOffset = Int(try header.read(UInt32.self))
let mapOffset = Int(try header.read(UInt32.self))
let dataLength = Int(try header.read(UInt32.self))
let mapLength = Int(try header.read(UInt32.self))
let dataOffset = Int(clamping: try header.read(UInt32.self))
let mapOffset = Int(clamping: try header.read(UInt32.self))
let dataLength = Int(clamping: try header.read(UInt32.self))
let mapLength = Int(clamping: try header.read(UInt32.self))
guard dataOffset <= bytes.count, dataLength <= bytes.count - dataOffset,
mapOffset <= bytes.count, mapLength <= bytes.count - mapOffset, mapLength >= 28 else { throw MacError.mapRead }
let map = try BigEndianReader(bytes).subreader(at: mapOffset, count: mapLength)
Expand Down Expand Up @@ -154,7 +154,7 @@ public struct ResourceMap: Hashable, Sendable {
name = PascalString(bytes: try map.subreader(at: at + 1, count: length).bytes)
}
guard offset + 4 <= dataLength else { throw MacError.mapRead }
let length = Int(try header.peek(UInt32.self, at: dataOffset + offset))
let length = Int(clamping: try header.peek(UInt32.self, at: dataOffset + offset))
guard length <= dataLength - offset - 4 else { throw MacError.mapRead }
let start = dataOffset + offset + 4
entries.append(Entry(resource: Resource(type: type, id: id, name: name, attributes: attributes), data: start ..< start + length))
Expand Down
15 changes: 7 additions & 8 deletions Sources/GraphiteSound/AIFF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public extension SampledSound {
var reader = BigEndianReader(data)
do {
guard try reader.read(UInt32.self) == FourCharCode("FORM").rawValue else { throw MacError.badFileFormat }
let formSize = Int(try reader.read(UInt32.self))
let end = min(data.count, 8 + formSize)
let formSize = Int(clamping: try reader.read(UInt32.self))
let end = 8 + min(formSize, data.count - 8)
let formType = FourCharCode(try reader.read(UInt32.self))
guard formType == "AIFF" || formType == "AIFC" else { throw MacError.badFileFormat }

Expand All @@ -52,13 +52,13 @@ public extension SampledSound {

while reader.offset + 8 <= end {
let id = FourCharCode(try reader.read(UInt32.self))
let size = Int(try reader.read(UInt32.self))
let size = Int(clamping: try reader.read(UInt32.self))
let body = reader.offset
guard size <= data.count - body else { throw MacError.badFileFormat }
switch id {
case "COMM":
let channels = Int(try reader.read(UInt16.self))
let frames = Int(try reader.read(UInt32.self))
let frames = Int(clamping: try reader.read(UInt32.self))
let bits = Int(try reader.read(UInt16.self))
let rate = try reader.readExtended80().doubleValue
var encoding = SoundEncoding.signedBigEndian
Expand All @@ -73,7 +73,7 @@ public extension SampledSound {
let count = Int(try reader.read(UInt16.self))
for _ in 0 ..< count {
let marker = try reader.read(UInt16.self)
markers[marker] = Int(try reader.read(UInt32.self))
markers[marker] = Int(clamping: try reader.read(UInt32.self))
// The name, a Pascal string padded to an even length.
let length = Int(try reader.read(UInt8.self))
try reader.skip(length + (length + 1) % 2)
Expand All @@ -86,10 +86,9 @@ public extension SampledSound {
let loopEnd = try reader.read(UInt16.self)
if playMode == 1 { loopMarkers = (begin, loopEnd) }
case "SSND":
let offset = Int(try reader.read(UInt32.self))
let offset = Int(clamping: try reader.read(UInt32.self))
try reader.skip(4) // blockSize
let start = body + 8 + offset
if start <= body + size { samples = start ..< body + size }
if offset <= size - 8 { samples = body + 8 + offset ..< body + size }
default:
break
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/GraphiteSound/SoundResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public enum SoundResource {
for _ in 0 ..< commands {
let command = try reader.read(UInt16.self)
try reader.skip(2) // param1
let parameter = Int(try reader.read(UInt32.self))
let parameter = Int(clamping: try reader.read(UInt32.self))
// With its high bit set, a command's param2 is an offset
// from the resource's start to its sound data.
if command & 0x8000 != 0, headerCommands.contains(command & 0x7FFF) {
Expand Down Expand Up @@ -114,8 +114,8 @@ public extension SampledSound {
guard try reader.read(UInt32.self) == 0 else { throw MacError.badFormat } // samplePtr
let length = Int(try reader.read(Int32.self)) // the byte count, or the channels
let rate = Double(try reader.read(UInt32.self)) / 65536
let loopStart = Int(try reader.read(UInt32.self))
let loopEnd = Int(try reader.read(UInt32.self))
let loopStart = Int(clamping: try reader.read(UInt32.self))
let loopEnd = Int(clamping: try reader.read(UInt32.self))
let encode = try reader.read(UInt8.self)
let baseNote = Int(try reader.read(UInt8.self))
let encoding: SoundEncoding
Expand Down
12 changes: 7 additions & 5 deletions Sources/GraphiteText/SfntTables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public struct SfntTables: Sendable {
var tag = try reader.read(UInt32.self)
if tag == FourCharCode("ttcf").rawValue {
try reader.skip(4) // version
let count = Int(try reader.read(UInt32.self))
guard index >= 0, index < count else { throw MacError.parameter }
try reader.seek(to: Int(try reader.peek(UInt32.self, at: 12 + 4 * index)))
let count = Int(clamping: try reader.read(UInt32.self))
// The count may be more than the file can hold, and `4 * index`
// must not overflow on a 32-bit CPU.
guard index >= 0, index < count, index <= (font.count - 16) / 4 else { throw MacError.parameter }
try reader.seek(to: Int(clamping: try reader.peek(UInt32.self, at: 12 + 4 * index)))
tag = try reader.read(UInt32.self)
}
guard tag == 0x0001_0000 || [FourCharCode("true"), "OTTO", "typ1"].contains(FourCharCode(tag)) else {
Expand All @@ -37,8 +39,8 @@ public struct SfntTables: Sendable {
for _ in 0 ..< count {
let name = FourCharCode(try reader.read(UInt32.self))
try reader.skip(4) // checksum
let offset = Int(try reader.read(UInt32.self))
let length = Int(try reader.read(UInt32.self))
let offset = Int(clamping: try reader.read(UInt32.self))
let length = Int(clamping: try reader.read(UInt32.self))
// A table past the end of the file is not there; the first of a tag wins.
guard offset <= font.count, length <= font.count - offset, directory[name] == nil else { continue }
directory[name] = offset ..< offset + length
Expand Down
Loading
Loading