-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Stream Base64Encoder Class and Stream Base64Decoder Class
Kindly provided by Rick A. https://forum.xojo.com/t/storing-multiple-values-in-an-integer/79495/42?u=sascha_s
- Loading branch information
Showing
3 changed files
with
292 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters