damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 46b53e1326d44697f2764f47dd9f6a2dea1b60bf
parent 225a028f3ef7b9deae30cdd443f9941f69ebb83b
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 26 Apr 2023 10:37:28 -0700

Add BinaryParser

Didn't end up using this, but might be useful in the future

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 11+++++++++++
Adamus/Util/Parser/BinaryParser.swift | 48++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -38,6 +38,7 @@ 4C0A3F8F280F640A000448DE /* ThreadModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F8E280F640A000448DE /* ThreadModel.swift */; }; 4C0A3F91280F6528000448DE /* ChatView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F90280F6528000448DE /* ChatView.swift */; }; 4C0A3F93280F66F5000448DE /* ReplyMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F92280F66F5000448DE /* ReplyMap.swift */; }; + 4C198DF829F89323004C165C /* BinaryParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C198DF729F89323004C165C /* BinaryParser.swift */; }; 4C1A9A1A29DCA17E00516EAC /* ReplyCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C1A9A1929DCA17E00516EAC /* ReplyCounter.swift */; }; 4C1A9A1D29DDCF9B00516EAC /* NotificationSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C1A9A1C29DDCF9B00516EAC /* NotificationSettingsView.swift */; }; 4C1A9A1F29DDD24B00516EAC /* AppearanceSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C1A9A1E29DDD24B00516EAC /* AppearanceSettingsView.swift */; }; @@ -416,6 +417,7 @@ 4C0A3F8E280F640A000448DE /* ThreadModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadModel.swift; sourceTree = "<group>"; }; 4C0A3F90280F6528000448DE /* ChatView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatView.swift; sourceTree = "<group>"; }; 4C0A3F92280F66F5000448DE /* ReplyMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyMap.swift; sourceTree = "<group>"; }; + 4C198DF729F89323004C165C /* BinaryParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryParser.swift; sourceTree = "<group>"; }; 4C1A9A1929DCA17E00516EAC /* ReplyCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyCounter.swift; sourceTree = "<group>"; }; 4C1A9A1C29DDCF9B00516EAC /* NotificationSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationSettingsView.swift; sourceTree = "<group>"; }; 4C1A9A1E29DDD24B00516EAC /* AppearanceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppearanceSettingsView.swift; sourceTree = "<group>"; }; @@ -855,6 +857,14 @@ path = Models; sourceTree = "<group>"; }; + 4C198DF629F89317004C165C /* Parser */ = { + isa = PBXGroup; + children = ( + 4C198DF729F89323004C165C /* BinaryParser.swift */, + ); + path = Parser; + sourceTree = "<group>"; + }; 4C1A9A1B29DDCF8B00516EAC /* Settings */ = { isa = PBXGroup; children = ( @@ -1644,6 +1654,7 @@ 4C06670628FCB08600038D2A /* ImageCarousel.swift in Sources */, F79C7FAD29D5E9620000F946 /* EditProfilePictureControl.swift in Sources */, 4C9F18E229AA9B6C008C55EC /* CustomizeZapView.swift in Sources */, + 4C198DF829F89323004C165C /* BinaryParser.swift in Sources */, 4C75EFAF28049D350006080F /* NostrFilter.swift in Sources */, 4C3EA64C28FF59AC00C48A62 /* bech32_util.c in Sources */, 4CE1399029F0661A00AC6A0B /* RepostAction.swift in Sources */, diff --git a/damus/Util/Parser/BinaryParser.swift b/damus/Util/Parser/BinaryParser.swift @@ -0,0 +1,48 @@ +// +// BinaryParser.swift +// damus +// +// Created by William Casarin on 2023-04-25. +// + +import Foundation + +class BinaryParser { + var pos: Int + var buf: [UInt8] + + init(buf: [UInt8], pos: Int = 0) { + self.pos = pos + self.buf = buf + } + + func read_byte() -> UInt8? { + guard pos < buf.count else { + return nil + } + + let v = buf[pos] + pos += 1 + return v + } + + func read_bytes(_ n: Int) -> [UInt8]? { + guard pos + n < buf.count else { + return nil + } + + let v = [UInt8](self.buf[pos...pos+n]) + return v + } + + func read_u16() -> UInt16? { + let start = self.pos + + guard let b1 = read_byte(), let b2 = read_byte() else { + self.pos = start + return nil + } + + return (UInt16(b1) << 8) | UInt16(b2) + } +}