Skip to content

Commit

Permalink
Added Stream Base64Encoder Class and Stream Base64Decoder Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Schneppi committed Mar 7, 2024
1 parent c95bdca commit 6240ca0
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 1 deletion.
130 changes: 130 additions & 0 deletions Base64Decoder.xojo_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#tag Class
Protected Class Base64Decoder
#tag Method, Flags = &h0
Function Base64CodeToSixBits(value As UInteger) As Integer
If value >= 48 And value <= 57 Then // 0-9
Return value + 4
ElseIf value >= 65 And value <= 90 Then // A-Z
Return value - 65
ElseIf value >= 97 And value <= 122 Then // a-z
Return value - 71
ElseIf value = 43 Then // +
Return 62
ElseIf value = 47 Then // -
Return 63
End
Return -1 // Invalid, ignore

End Function
#tag EndMethod

#tag Method, Flags = &h0
Sub Decode(byteToDecode As UInteger)
If numBitsStored > 58 Then
Raise New RuntimeException("Bit Overflow", 1)
End

If byteToDecode > &h00ff Then
Raise New RuntimeException("Invalid Byte Value", 2)
End

Var bits As Integer = Base64CodeToSixBits(byteToDecode)

If bits >= 0 Then // Ignore unknown chars, -1
numBitsStored = numBitsStored + 6
bitBuffer = ShiftLeft(bitBuffer,6) Or bits
End
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Function Decoded() As Integer
If numBitsStored>7 Then // Bytes ready

var bits as UInteger = ShiftRight(bitBuffer, numBitsStored - 8) And &h0FF
numBitsStored = numBitsStored - 8

Return bits

End

Return -1 // No content, ignore


End Function
#tag EndMethod

#tag Method, Flags = &h0
Sub InitDecoding()
// Reset the Decoder to start a new decoding
bitBuffer = 0
numBitsStored = 0



End Sub
#tag EndMethod


#tag Note, Name = Author
Stream Base64Decoder Class

By Rick A.
(C)2024

#tag EndNote


#tag Property, Flags = &h21
Private bitBuffer As UInt64 = 0
#tag EndProperty

#tag Property, Flags = &h21
Private numBitsStored As Integer = 0
#tag EndProperty


#tag ViewBehavior
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass
159 changes: 159 additions & 0 deletions Base64Encoder.xojo_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#tag Class
Protected Class Base64Encoder
#tag Method, Flags = &h0
Sub BeginEncoding(pad As Boolean = True)
// Reset the encoder to start a new encoding
// Default is using the padding system
bitBuffer = 0
numBitsStored = 0
encodeEnded = False
padding = 0
padCode = pad

End Sub
#tag EndMethod

#tag Method, Flags = &h0
Sub Encode(byteToEncode As UInteger)
If numBitsStored > 58 Then
Raise New RuntimeException("Bit Overflow", 1)
End

If byteToEncode > &h00ff Then
Raise New RuntimeException("Invalid Value", 2)
End

numBitsStored = numBitsStored + 8
bitBuffer = ShiftLeft(bitBuffer,8) Or byteToEncode
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Function Encoded() As Integer
If numBitsStored < 1 Then // Empty buffer
If encodeEnded Then
If padding>0 Then
padding = padding - 1
Return 61 // padding char "="
End
Return -2 // Encoding ended
End
Return -1 // Nothing to read, keep encoding
End

If numBitsStored<6 Then // Not enought bits
If encodeEnded Then // Ended, Flush it
padding = If(padCode, numBitsStored \ 2, 0) // num pad chars
bitBuffer = ShiftLeft(bitBuffer, 6 - numBitsStored) // complete
numBitsStored = 6
Else
Return -1 // Nothing to read, keep encoding
End
End

var bits as UInteger = ShiftRight(bitBuffer, numBitsStored - 6) And &h3F
numBitsStored = numBitsStored - 6

Return SixBitsToBase64Code(bits)


End Function
#tag EndMethod

#tag Method, Flags = &h0
Sub EndEncoding()
encodeEnded = True
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Function SixBitsToBase64Code(value As UInteger) As Integer
If value< 26 Then
Return value + 65 // A-Z
ElseIf value < 52 Then
Return value + 71 // a-z
ElseIf value < 62 Then
Return value - 4 // 0-9
ElseIf value < 64 Then
Return IF(value=62, 43, 47) // + /
End
Raise New RuntimeException("Invalid bit code", 3)

End Function
#tag EndMethod


#tag Note, Name = Author
Stream Base64Encoder Class

By Rick A.
(C)2024

#tag EndNote


#tag Property, Flags = &h21
Private bitBuffer As UInt64 = 0
#tag EndProperty

#tag Property, Flags = &h21
Private encodeEnded As Boolean = False
#tag EndProperty

#tag Property, Flags = &h21
Private numBitsStored As Integer = 0
#tag EndProperty

#tag Property, Flags = &h21
Private padCode As Boolean = True
#tag EndProperty

#tag Property, Flags = &h21
Private padding As Integer = 0
#tag EndProperty


#tag ViewBehavior
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass
4 changes: 3 additions & 1 deletion INI_JSON.xojo_project
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ Type=Desktop
RBProjectVersion=2023.04
MinIDEVersion=20210300
OrigIDEVersion=20230400
Class=INI_JSON;INI_JSON.xojo_code;&h000000000E1CAFFF;&h0000000000000000;false
Class=App;App.xojo_code;&h000000003C4E37FF;&h0000000000000000;false
DesktopWindow=Window1;Window1.xojo_window;&h000000002098AFFF;&h0000000000000000;false
MenuBar=MainMenuBar;MainMenuBar.xojo_menu;&h000000001D1D0FFF;&h0000000000000000;false
BuildSteps=Build Automation;Build Automation.xojo_code;&h0000000077CE5FFF;&h0000000000000000;false
Class=INI_JSON;INI_JSON.xojo_code;&h000000000E1CAFFF;&h0000000000000000;false
Class=Base64Encoder;Base64Encoder.xojo_code;&h000000001A2007FF;&h0000000000000000;false
Class=Base64Decoder;Base64Decoder.xojo_code;&h000000003A9017FF;&h0000000000000000;false
DefaultWindow=Window1
AppMenuBar=MainMenuBar
MajorVersion=1
Expand Down

0 comments on commit 6240ca0

Please sign in to comment.