commit 0362f17bd20d0d9392332801c6baacf70b254186 Author: Herwin Bozet Date: Wed Oct 7 20:45:06 2026 +0200 Initial Commit, Implements numbers without endianness checks and FixInts Update .gitignore, MessagePack.pbi, LICENSE, and 2 more files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bda28d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# IDEs +.vs/ +.idea/ + +# Build artifacts +*.dll +*.exe +*.cfg + +#Trash +Trash/ +*.7z +*.zip diff --git a/Includes/MessagePack.pbi b/Includes/MessagePack.pbi new file mode 100644 index 0000000..1ce24a7 --- /dev/null +++ b/Includes/MessagePack.pbi @@ -0,0 +1,847 @@ +;{- Code Header +; ==- Basic Info -================================ +; Name: MessagePack.pbi +; Version: 0.0.0 +; Author: Herwin Bozet (NibblePoker) +; +; ==- Compatibility -============================= +; Tested compiler version: +; * //PureBasic 5.73 LTS (x86/x64) +; * //PureBasic 6.21 (x86/x64) +; * //PureBasic 6.21 C Backend (arm64) +; +; ==- Links & License -=========================== +; License: CC0 1.0 Universal (Public Domain) +; GitHub: https://github.com/aziascreations/PB-MessagePack +;} + +; ------------------------------------------------------------------------------ +;- Notes + +; Specification: +; * https://github.com/msgpack/msgpack/blob/master/spec.md + + + +; ------------------------------------------------------------------------------ +;- Compiler directive + +;EnableExplicit + + + +; ------------------------------------------------------------------------------ +;- Constants + +; ??? + + + +; ------------------------------------------------------------------------------ +;- Enumerations + +Enumeration MsgPack_Types + #MsgPack_Types_Null + #MsgPack_Types_Nil = #MsgPack_Types_Null + + #MsgPack_Types_False + #MsgPack_Types_True + + #MsgPack_Types_FixIntPositive + #MsgPack_Types_FixIntNegative + + #MsgPack_Types_UInt8 + #MsgPack_Types_UInt16 + #MsgPack_Types_UInt32 + #MsgPack_Types_UInt64 + + #MsgPack_Types_Int8 + #MsgPack_Types_Int16 + #MsgPack_Types_Int32 + #MsgPack_Types_Int64 + + #MsgPack_Types_Float32 + #MsgPack_Types_Float64 + + #MsgPack_Types_Custom +EndEnumeration + +Enumeration MsgPack_FormatCodes + + ; 1 type byte, nothing else + #MsgPack_FormatCode_Null = $C0 + #MsgPack_FormatCode_Nil = #MsgPack_Types_Null + + ; 1 type byte, nothing else + #MsgPack_FormatCode_False = $C2 + + ; 1 type byte, nothing else + #MsgPack_FormatCode_True = $C3 + + ; 1 type byte + 4 data bytes (big-endian IEEE 754 single precision) + #MsgPack_FormatCode_Float32 = $CA + + ; 1 type byte + 8 data bytes (big-endian IEEE 754 double precision) + #MsgPack_FormatCode_Float64 = $CB + + ; Special + #MsgPack_FormatCode_FixIntPositive = $CC + #MsgPack_FormatCode_FixIntNegative = $CC + + #MsgPack_FormatCode_UInt8 = $CC + #MsgPack_FormatCode_UInt16 = $CD + #MsgPack_FormatCode_UInt32 = $CE + #MsgPack_FormatCode_UInt64 = $CF + + #MsgPack_FormatCode_Int8 = $D0 + #MsgPack_FormatCode_Int16 = $D1 + #MsgPack_FormatCode_Int32 = $D2 + #MsgPack_FormatCode_Int64 = $D3 + +EndEnumeration + + + + + +; Private structure representing read/write buffers +Structure MsgPackThingy + ; Offset used when reading or writing. + BufferOffset.i + + ; Used in place of `MemorySize()` to reduce syscalls. + BufferSize.i + + ; Temp mechanism for buffer growth. + BufferGrowthIncrements.i + + ; Pointer to a data buffer. + ; Can be freed by the caller or this module. + *Buffer +EndStructure + + + + + + +; Tmp +Procedure MsgPackAAAAA(DataFormatCode.a) + + ; FixInts + If DataFormatCode & %10000000 = 0 + ProcedureReturn #MsgPack_Types_FixIntPositive + EndIf + + + If DataFormatCode & %11100000 = %11100000 + ProcedureReturn #MsgPack_Types_FixIntNegative + EndIf + +EndProcedure + + + + + + +; ------------------------------------------------------------------------------ +;- Macros + +Macro _HasSpaceLeft(MsgPackWrapper, DesiredSize) + ((MsgPackWrapper\BufferSize >= MsgPackWrapper\BufferOffset) And (MsgPackWrapper\BufferSize - MsgPackWrapper\BufferOffset >= DesiredSize)) +EndMacro + +Macro _IsAtTheEnd(MsgPackWrapper) + (MsgPackWrapper\BufferOffset >= MsgPackWrapper\BufferSize) +EndMacro + + + +; ------------------------------------------------------------------------------ +;- Procedures + +;--> Thingies + +Procedure.i MsgPackCreateMsgBuffer(InitialSize.i) + Debug "########################################" + Debug "MsgPackCreateMsgBuffer()" + Debug " InitialSize: " + Str(InitialSize) + + Protected *ReturnedValue.MsgPackThingy = #Null + + If InitialSize <= 0 + DebuggerWarning("Returning #Null, requested size was zero or negative") + ProcedureReturn #Null + EndIf + + *ReturnedValue = AllocateStructure(MsgPackThingy) + + If *ReturnedValue <> #Null + *ReturnedValue\BufferOffset = 0 + *ReturnedValue\BufferSize = InitialSize + *ReturnedValue\BufferGrowthIncrements = InitialSize + + *ReturnedValue\Buffer = AllocateMemory(InitialSize) + If Not *ReturnedValue\Buffer + DebuggerError("Failed to allocate memory for buffer !") + FreeStructure(*ReturnedValue) + *ReturnedValue = #Null + EndIf + EndIf + + ProcedureReturn *ReturnedValue +EndProcedure + +; Wraps an existing, caller-owned buffer so it can be read with ReadA/ReadI8. +; Does not copy or take ownership of *Buffer: free it yourself, and pass +; FreeDataBuffer = #False to MsgPackFreeMsgBuffer for the wrapper it returns. +Procedure.i MsgPackWrapMsgBuffer(*Buffer, BufferSize.i) + Debug "########################################" + Debug "MsgPackWrapMsgBuffer()" + Debug " *Buffer: 0x" + RSet(Hex(*Buffer), SizeOf(Integer) * 2, "0") + Debug " BufferSize: " + Str(BufferSize) + ;If *Buffer + ; Debug " MemorySize: " + Str(MemorySize(*Buffer)) + ;EndIf + + Protected *ReturnedValue.MsgPackThingy = #Null + + If *Buffer = #Null Or BufferSize <= 0 + ProcedureReturn #Null + EndIf + + *ReturnedValue = AllocateStructure(MsgPackThingy) + If *ReturnedValue = #Null + ProcedureReturn #Null + EndIf + + *ReturnedValue\BufferOffset = 0 + *ReturnedValue\BufferSize = BufferSize + *ReturnedValue\BufferGrowthIncrements = 0 ; wrapped buffer isn't grown + *ReturnedValue\Buffer = *Buffer + + ProcedureReturn *ReturnedValue +EndProcedure + +Procedure.b MsgPackFreeMsgBuffer(*MsgPackData.MsgPackThingy, FreeDataBuffer.b = #False) + If Not *MsgPackData + ProcedureReturn #False + EndIf + + If *MsgPackData <> #Null + If *MsgPackData\Buffer <> #Null And FreeDataBuffer + FreeMemory(*MsgPackData\Buffer) + EndIf + FreeStructure(*MsgPackData) + EndIf + + ProcedureReturn #True +EndProcedure + + + +;--> Read/Write + +;---> UInt8 + +Procedure.b MsgPackWriteUInt8(*MsgPackData.MsgPackThingy, Value.a) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 2) + Debug("Growing buffer for UInt8...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_UInt8) + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 2 + + ProcedureReturn #True +EndProcedure + + +Procedure.a MsgPackReadUInt8(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.a = $00 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_UInt8 + DebuggerError("The format code isn't the one for a UInt8 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_UInt8), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 2) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 2 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> Int8 + +Procedure.b MsgPackWriteInt8(*MsgPackData.MsgPackThingy, Value.b) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 2) + Debug("Growing buffer for Int8...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_Int8) + PokeB(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 2 + + ProcedureReturn #True +EndProcedure + + +Procedure.b MsgPackReadInt8(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.b = $00 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_Int8 + DebuggerError("The format code isn't the one for a Int8 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_Int8), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 2) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekB(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 2 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> UInt16 + +Procedure.b MsgPackWriteUInt16(*MsgPackData.MsgPackThingy, Value.u) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 3) + Debug("Growing buffer for UInt16...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_UInt16) + PokeU(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 3 + + ProcedureReturn #True +EndProcedure + + +Procedure.u MsgPackReadUInt16(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.u = $0000 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_UInt16 + DebuggerError("The format code isn't the one for a UInt16 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_UInt16), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 3) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekU(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 3 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> Int16 + +Procedure.b MsgPackWriteInt16(*MsgPackData.MsgPackThingy, Value.w) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 3) + Debug("Growing buffer for Int16...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_Int16) + PokeW(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 3 + + ProcedureReturn #True +EndProcedure + + +Procedure.w MsgPackReadInt16(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.w = $0000 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_Int16 + DebuggerError("The format code isn't the one for a Int16 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_Int16), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 3) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekW(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 3 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> UInt32 + +; No unsigned Long in PureBasic: value is peeked/poked as-is via .l and returned unchanged. +Procedure.b MsgPackWriteUInt32(*MsgPackData.MsgPackThingy, Value.l) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 5) + Debug("Growing buffer for UInt32...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_UInt32) + PokeL(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 5 + + ProcedureReturn #True +EndProcedure + + +Procedure.l MsgPackReadUInt32(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.l = $00000000 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_UInt32 + DebuggerError("The format code isn't the one for a UInt32 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_UInt32), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 5) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekL(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 5 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> Int32 + +Procedure.b MsgPackWriteInt32(*MsgPackData.MsgPackThingy, Value.l) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 5) + Debug("Growing buffer for Int32...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_Int32) + PokeL(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 5 + + ProcedureReturn #True +EndProcedure + + +Procedure.l MsgPackReadInt32(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.l = $00000000 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_Int32 + DebuggerError("The format code isn't the one for a Int32 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_Int32), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 5) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekL(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 5 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> UInt64 + +; No unsigned Quad in PureBasic: value is peeked/poked as-is via .q and returned unchanged. +Procedure.b MsgPackWriteUInt64(*MsgPackData.MsgPackThingy, Value.q) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 9) + Debug("Growing buffer for UInt64...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_UInt64) + PokeQ(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 9 + + ProcedureReturn #True +EndProcedure + + +Procedure.q MsgPackReadUInt64(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.q = 0 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_UInt64 + DebuggerError("The format code isn't the one for a UInt64 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_UInt64), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 9) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekQ(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 9 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> Int64 + +Procedure.b MsgPackWriteInt64(*MsgPackData.MsgPackThingy, Value.q) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 9) + Debug("Growing buffer for Int64...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_Int64) + PokeQ(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 9 + + ProcedureReturn #True +EndProcedure + + +Procedure.q MsgPackReadInt64(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.q = 0 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_Int64 + DebuggerError("The format code isn't the one for a Int64 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_Int64), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 9) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekQ(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 9 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> Float32 + +Procedure.b MsgPackWriteFloat32(*MsgPackData.MsgPackThingy, Value.f) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 5) + Debug("Growing buffer for Float32...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_Float32) + PokeF(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 5 + + ProcedureReturn #True +EndProcedure + + +Procedure.f MsgPackReadFloat32(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.f = 0.0 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_Float32 + DebuggerError("The format code isn't the one for a Float32 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_Float32), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 5) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekF(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 5 + + ProcedureReturn ReturnedValue +EndProcedure + + + + +;---> Float64 + +Procedure.b MsgPackWriteFloat64(*MsgPackData.MsgPackThingy, Value.d) + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn #False + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 9) + Debug("Growing buffer for Float64...") + ProcedureReturn #False + EndIf + + PokeA(*MsgPackData\Buffer + *MsgPackData\BufferOffset, #MsgPack_FormatCode_Float64) + PokeD(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1, Value) + + *MsgPackData\BufferOffset + 9 + + ProcedureReturn #True +EndProcedure + + +Procedure.d MsgPackReadFloat64(*MsgPackData.MsgPackThingy) + Protected ReturnedValue.d = 0.0 + + If Not *MsgPackData + DebuggerError("A #Null MsgPackThingy pointer was passed !") + ProcedureReturn 0 + EndIf + + If _IsAtTheEnd(*MsgPackData) + DebuggerError("End of buffer reached, cannot check data format code !") + ProcedureReturn 0 + EndIf + + If PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) <> #MsgPack_FormatCode_Float64 + DebuggerError("The format code isn't the one for a Float64 ! (" + + RSet(Hex(PeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset)), 2, "0") + + " vs "+ RSet(Hex(#MsgPack_FormatCode_Float64), 2, "0") + ")") + ProcedureReturn 0 + EndIf + + If Not _HasSpaceLeft(*MsgPackData, 9) + DebuggerError("End of buffer reached, cannot read out of bounds !") + ProcedureReturn 0 + EndIf + + ReturnedValue = PeekD(*MsgPackData\Buffer + *MsgPackData\BufferOffset + 1) + *MsgPackData\BufferOffset + 9 + + ProcedureReturn ReturnedValue +EndProcedure + + +; ------------------------------------------------------------------------------ +;- Aliases + +;-> UInt8 +Macro MsgPackWriteU8 : MsgPackWriteUInt8 : EndMacro +Macro MsgPackReadU8 : MsgPackReadUInt8 : EndMacro + +;-> Int8 +Macro MsgPackWriteI8 : MsgPackWriteInt8 : EndMacro +Macro MsgPackReadI8 : MsgPackReadInt8 : EndMacro + +;-> UInt16 +Macro MsgPackWriteU16 : MsgPackWriteUInt16 : EndMacro +Macro MsgPackReadU16 : MsgPackReadUInt16 : EndMacro + +;-> Int16 +Macro MsgPackWriteI16 : MsgPackWriteInt16 : EndMacro +Macro MsgPackReadI16 : MsgPackReadInt16 : EndMacro + +;-> UInt32 +Macro MsgPackWriteU32 : MsgPackWriteUInt32 : EndMacro +Macro MsgPackReadU32 : MsgPackReadUInt32 : EndMacro + +;-> Int32 +Macro MsgPackWriteI32 : MsgPackWriteInt32 : EndMacro +Macro MsgPackReadI32 : MsgPackReadInt32 : EndMacro + +;-> UInt64 +Macro MsgPackWriteU64 : MsgPackWriteUInt64 : EndMacro +Macro MsgPackReadU64 : MsgPackReadUInt64 : EndMacro + +;-> Int64 +Macro MsgPackWriteI64 : MsgPackWriteInt64 : EndMacro +Macro MsgPackReadI64 : MsgPackReadInt64 : EndMacro + +;-> Float32 +Macro MsgPackWriteF32 : MsgPackWriteFloat32 : EndMacro +Macro MsgPackReadF32 : MsgPackReadFloat32 : EndMacro + +;-> Float64 +Macro MsgPackWriteF64 : MsgPackWriteFloat64 : EndMacro +Macro MsgPackReadF64 : MsgPackReadFloat64 : EndMacro diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0e259d4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/Test.pb b/Test.pb new file mode 100644 index 0000000..8949147 --- /dev/null +++ b/Test.pb @@ -0,0 +1,23 @@ + + +EnableExplicit + +Define *Buffer = MsgPackCreateMsgBuffer(16) +MsgPackWriteI8(*Buffer, 42) +MsgPackWriteI8(*Buffer, 7) +MsgPackFreeMsgBuffer(*Buffer, #True) + +; Wrapping an existing buffer for reading +Define *RawData = AllocateMemory(4) +PokeA(*RawData + 0, #MsgPack_FormatCode_UInt8) +PokeA(*RawData + 1, 42) +PokeA(*RawData + 2, #MsgPack_FormatCode_Int8) +PokeB(*RawData + 3, -3) + +Define *ReadBuffer = MsgPackWrapMsgBuffer(*RawData, 4) +Debug MsgPackReadU8(*ReadBuffer) +Debug MsgPackReadI8(*ReadBuffer) + +MsgPackFreeMsgBuffer(*ReadBuffer, #False) ; #False: *RawData is owned by us +FreeMemory(*RawData) + \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5c36447 --- /dev/null +++ b/readme.md @@ -0,0 +1,11 @@ +# PureBasic - MsgPack + + +## ??? + + + + + +## License +All the code in this repo is released in the [Public Domain](LICENSE).