Files
ziran/std/binary.zi
T

134 lines
4.2 KiB
Ziran

// Little-endian numbers in byte text, such as file or asset contents, read
// the same way on every native target and in the engine. Floats are
// decoded from their IEEE 754 bits with exact arithmetic, so no target
// reinterprets memory. TryRead names invalid offsets; the legacy Read helpers
// retain their zero fallback for source-compatible, already-validated layouts.
#import "math"
#import "result"
BinaryReadFailure :: enum {
None
NegativeOffset
Misaligned
ShortField
}
#scope_export
// The value of an IEEE 754 single from its bits.
#program_export
Float32FromBits :: (bits: u32) -> float64 {
exponent := cast(s32)((bits >> cast(u32)23) & cast(u32)255)
mantissa := cast(float64)(bits & cast(u32)8388607)
negative := (bits & cast(u32)2147483648) != cast(u32)0
value: float64 = 0.0
if exponent == 255 {
value = ifx mantissa == 0.0 then Infinity() else NotANumber()
} else if exponent == 0 {
value = ScaleByPowerOfTwo(mantissa, -149)
} else {
value = ScaleByPowerOfTwo(mantissa + 8388608.0, exponent - 150)
}
if negative {
return -value
}
return value
}
// The value of an IEEE 754 double from its bits.
#program_export
Float64FromBits :: (bits: u64) -> float64 {
exponent := cast(s32)((bits >> cast(u64)52) & cast(u64)2047)
mantissa := cast(float64)(bits & cast(u64)4503599627370495)
negative := (bits >> cast(u64)63) != cast(u64)0
value: float64 = 0.0
if exponent == 2047 {
value = ifx mantissa == 0.0 then Infinity() else NotANumber()
} else if exponent == 0 {
value = ScaleByPowerOfTwo(mantissa, -1074)
} else {
value = ScaleByPowerOfTwo(mantissa + 4503599627370496.0, exponent - 1075)
}
if negative {
return -value
}
return value
}
#program_export
TryReadU16LE :: (bytes: string, at: s32) -> Result(u32, BinaryReadFailure) {
result: Result(u32, BinaryReadFailure)
if at < 0 { result.error = .NegativeOffset; return result }
if at % 2 != 0 { result.error = .Misaligned; return result }
if at > cast(s32)bytes.count || 2 > cast(s32)bytes.count - at {
result.error = .ShortField
return result
}
result.is_ok = true
result.value = cast(u32)bytes[at] | (cast(u32)bytes[at + 1] << cast(u32)8)
return result
}
#program_export
TryReadU32LE :: (bytes: string, at: s32) -> Result(u32, BinaryReadFailure) {
result: Result(u32, BinaryReadFailure)
if at < 0 { result.error = .NegativeOffset; return result }
if at % 4 != 0 { result.error = .Misaligned; return result }
if at > cast(s32)bytes.count || 4 > cast(s32)bytes.count - at {
result.error = .ShortField
return result
}
value: u32 = 0
index: s32 = 3
while index >= 0 {
value = (value << cast(u32)8) | cast(u32)bytes[at + index]
index -= 1
}
result.is_ok = true
result.value = value
return result
}
#program_export
TryReadS32LE :: (bytes: string, at: s32) -> Result(s32, BinaryReadFailure) {
result: Result(s32, BinaryReadFailure)
value := TryReadU32LE(bytes, at)
if !value.is_ok { result.error = value.error; return result }
result.is_ok = true
result.value = cast(s32)value.value
return result
}
#program_export
TryReadF32LE :: (bytes: string, at: s32) -> Result(float64, BinaryReadFailure) {
result: Result(float64, BinaryReadFailure)
value := TryReadU32LE(bytes, at)
if !value.is_ok { result.error = value.error; return result }
result.is_ok = true
result.value = Float32FromBits(value.value)
return result
}
// Compatibility readers for already-validated internal layouts. Untrusted
// boundaries use the TryRead variants above so malformed input stays named.
#program_export
ReadU16LE :: (bytes: string, at: s32) -> u32 {
value := TryReadU16LE(bytes, at)
return ifx value.is_ok then value.value else cast(u32)0
}
#program_export
ReadU32LE :: (bytes: string, at: s32) -> u32 {
value := TryReadU32LE(bytes, at)
return ifx value.is_ok then value.value else cast(u32)0
}
#program_export
ReadS32LE :: (bytes: string, at: s32) -> s32 {
return cast(s32)ReadU32LE(bytes, at)
}
#program_export
ReadF32LE :: (bytes: string, at: s32) -> float64 {
return Float32FromBits(ReadU32LE(bytes, at))
}