瀏覽代碼

Changed to not use Shl 24. Added Encode for Strings.

Brucey 1 年之前
父節點
當前提交
0bf4d2d7a6
共有 2 個文件被更改,包括 83 次插入9 次删除
  1. 28 9
      base64.mod/base64.bmx
  2. 55 0
      base64.mod/tests/test.bmx

+ 28 - 9
base64.mod/base64.bmx

@@ -1,4 +1,4 @@
-' Copyright (c) 2008-2019 Bruce A Henderson
+' Copyright (c) 2008-2023 Bruce A Henderson
 ' 
 ' Permission is hereby granted, free of charge, to any person obtaining a copy
 ' of this software and associated documentation files (the "Software"), to deal
@@ -25,11 +25,14 @@ bbdoc: Base64 Encoding
 End Rem
 Module BRL.Base64
 
-ModuleInfo "Version: 1.01"
+ModuleInfo "Version: 1.02"
 ModuleInfo "License: MIT"
 ModuleInfo "Copyright: Original - Robert Harder (http://iharder.sourceforge.net/current/java/base64/)"
-ModuleInfo "Copyright: BlitzMax port - 2008-2019 Bruce A Henderson"
+ModuleInfo "Copyright: BlitzMax port - 2008-2023 Bruce A Henderson"
 
+ModuleInfo "History: 1.02"
+ModuleInfo "History: Added Encode for Strings."
+ModuleInfo "History: Changed to not use Shl 24."
 ModuleInfo "History: 1.01"
 ModuleInfo "History: Fixed Encode() sometimes returning an extra null character."
 ModuleInfo "History: 1.00 Initial Release"
@@ -80,6 +83,17 @@ Type TBase64
 	
 	Public
 
+	Rem
+	bbdoc: Encode byte data to a Base64 encoded String, starting at @offset of @length bytes.
+	End Rem
+	Function Encode:String( source:String, options:EBase64Options = EBase64Options.None)
+		Local s:Byte Ptr = source.ToUTF8String()
+		Local length:Int = strlen_(s)
+		Local result:String = Encode(s, length, 0, options)
+		MemFree(s)
+		Return result
+	End Function
+
 	Rem
 	bbdoc: Encodes byte array data @source to a Base64 encoded String, starting at @offset.
 	End Rem
@@ -139,7 +153,7 @@ Type TBase64
 	Rem
 	bbdoc: Decodes Base64 encoded String @source to an array of Bytes, starting at @offset.
 	End Rem
-	Function Decode:Byte[]( source:String, offset:Int = 0, options:EBase64Options = EBase64Options.None )
+	Function Decode:Byte[]( source:String, offset:Int = 0 )
 		
 		Local length:Int = source.length
 		Local len34:Int   = Length * 3 / 4
@@ -188,17 +202,17 @@ Type TBase64
     
 		Local inBuff:Int
 		If numSigBytes > 0 Then
-			inBuff = (source[ srcOffset     ] Shl 24) Shr 8
-			
+			inBuff = (source[ srcOffset     ] & $FF) Shl 16
+
 			If numSigBytes > 1 Then
-				inBuff :| (source[ srcOffset + 1 ] Shl 24) Shr 16
+				inBuff :| ((source[ srcOffset + 1 ] & $FF) Shl 8)
 
 				If numSigBytes > 2 Then
-					inBuff :| (source[ srcOffset + 2 ] Shl 24) Shr 24
+					inBuff :| (source[ srcOffset + 2 ] & $FF)
 				End If
 			End If
 		End If
-		
+
 		Select numSigBytes
 			Case 3
 				destination[ destOffset     ] = _STANDARD_ALPHABET[ (inBuff Shr 18)       ]
@@ -277,3 +291,8 @@ Enum EBase64Options Flags
 	End Rem
 	DontBreakLines = 8
 End Enum
+
+Private
+Extern
+	Function strlen_:Size_T( str:Byte Ptr )="size_t strlen( const char *) !"
+End Extern

+ 55 - 0
base64.mod/tests/test.bmx

@@ -0,0 +1,55 @@
+SuperStrict
+
+Framework brl.standardio
+Import BRL.Base64
+Import BRL.MaxUnit
+
+New TTestSuite.run()
+
+Type TBase64Test Extends TTest
+
+	Method testStringEncodeWithoutPadding() { test }
+		Local text:String = "The quick brown fox jumps over the lazy"
+		Local encoded:String = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5"
+	
+		Local result:String = TBase64.Encode(text)
+	
+		AssertEquals(encoded, result)
+	End Method
+	
+	Method testStringEncodeWithSinglePadding() { test }
+		Local text:String = "The quick brown fox jumps over the lazy d"
+		Local encoded:String = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGQ="
+	
+		Local result:String = TBase64.Encode(text)
+	
+		AssertEquals(encoded, result)
+	End Method
+	
+	Method testStringEncodeWithDoublePadding() { test }
+		Local text:String = "The quick brown fox jumps over the lazy dog"
+		Local encoded:String = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=="
+	
+		Local result:String = TBase64.Encode(text)
+	
+		AssertEquals(encoded, result)
+	End Method
+	
+	Method testStringEncodeDontBreakLines() { test }
+		Local text:String = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
+		Local encoded:String = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4gVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4="
+	
+		Local result:String = TBase64.Encode(text, EBase64Options.DontBreakLines)
+	
+		AssertEquals(encoded, result)
+	End Method
+
+	Method testStringEncodeWithLineBreaks() { test }
+		Local text:String = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
+		Local encoded:String = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4gVGhlIHF1aWNrIGJy~nb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4gVGhlIHF1aWNrIGJyb3duIGZveCBqdW1w~ncyBvdmVyIHRoZSBsYXp5IGRvZy4gVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBs~nYXp5IGRvZy4="
+	
+		Local result:String = TBase64.Encode(text)
+	
+		AssertEquals(encoded, result)
+	End Method
+End Type