Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraMath/Bytes.swift8// Module : Qyvora Math9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Byte-string helpers shared by every engine module.44public typealias Bytes = [UInt8]4546public enum Hex {47 private static let digits = Array("0123456789abcdef".utf8)4849 public static func encode<S: Sequence>(_ bytes: S) -> String where S.Element == UInt8 {50 var out = [UInt8]()51 out.reserveCapacity(bytes.underestimatedCount * 2)52 for b in bytes {53 out.append(digits[Int(b >> 4)])54 out.append(digits[Int(b & 0x0f)])55 }56 return String(decoding: out, as: UTF8.self)57 }5859 /// Strict decoder: even length, hexadecimal digits only (either case). Returns nil otherwise.60 public static func decode(_ string: String) -> Bytes? {61 let u = Array(string.utf8)62 guard u.count % 2 == 0 else { return nil }63 var out = Bytes()64 out.reserveCapacity(u.count / 2)65 var i = 066 while i < u.count {67 guard let hi = nibble(u[i]), let lo = nibble(u[i + 1]) else { return nil }68 out.append(hi << 4 | lo)69 i += 270 }71 return out72 }7374 private static func nibble(_ c: UInt8) -> UInt8? {75 switch c {76 case 0x30...0x39: return c - 0x3077 case 0x61...0x66: return c - 0x61 + 1078 case 0x41...0x46: return c - 0x41 + 1079 default: return nil80 }81 }82}8384public extension Array where Element == UInt8 {85 var hex: String { Hex.encode(self) }8687 init?(hex: String) {88 guard let b = Hex.decode(hex) else { return nil }89 self = b90 }91}9293/// Fixed-width integer loads and stores (no alignment assumption).94public enum Endian {95 @inlinable public static func loadLE32(_ b: UnsafePointer<UInt8>) -> UInt32 {96 UInt32(b[0]) | UInt32(b[1]) << 8 | UInt32(b[2]) << 16 | UInt32(b[3]) << 2497 }9899 @inlinable public static func loadBE32(_ b: UnsafePointer<UInt8>) -> UInt32 {100 UInt32(b[3]) | UInt32(b[2]) << 8 | UInt32(b[1]) << 16 | UInt32(b[0]) << 24101 }102103 @inlinable public static func loadLE64(_ b: UnsafePointer<UInt8>) -> UInt64 {104 UInt64(loadLE32(b)) | UInt64(loadLE32(b + 4)) << 32105 }106107 @inlinable public static func loadBE64(_ b: UnsafePointer<UInt8>) -> UInt64 {108 UInt64(loadBE32(b)) << 32 | UInt64(loadBE32(b + 4))109 }110111 @inlinable public static func be32(_ v: UInt32) -> Bytes {112 [UInt8(truncatingIfNeeded: v >> 24), UInt8(truncatingIfNeeded: v >> 16), UInt8(truncatingIfNeeded: v >> 8), UInt8(truncatingIfNeeded: v)]113 }114115 @inlinable public static func le32(_ v: UInt32) -> Bytes {116 [UInt8(truncatingIfNeeded: v), UInt8(truncatingIfNeeded: v >> 8), UInt8(truncatingIfNeeded: v >> 16), UInt8(truncatingIfNeeded: v >> 24)]117 }118119 @inlinable public static func be64(_ v: UInt64) -> Bytes {120 be32(UInt32(truncatingIfNeeded: v >> 32)) + be32(UInt32(truncatingIfNeeded: v))121 }122123 @inlinable public static func le64(_ v: UInt64) -> Bytes {124 le32(UInt32(truncatingIfNeeded: v)) + le32(UInt32(truncatingIfNeeded: v >> 32))125 }126}127