Search the Community
Showing results for tags 'Base91'.
-
Here's Base91 and Base128 functions I ported from code I found on the net. Quick search of the forum did not pull up anything so don't think these have been posted before. Both just for fun. The base128 is pretty straight forward as its pretty much the same concept as base64 only using 7bits instead of 6, but I thought the base91 was real interesting and Im actually still trying to wrap my head around the algorithm. The functions Ive posted are shortened/combined a bit so if your gonna try and learn the flow I would break it back down to one operation per line. Let me know if you find a string it does not work with. I mainly wanted them for embedding dlls in scripts and the the results are below for a dll that I tested. Have fun! Base91: ; ; #FUNCTION# ==================================================================================================================== ; Name...........: _Base91Encode ; Description ...: Encodes string to Base91 ; Author ........: Brian J Christy (Beege) ; Source ........: http://base91.sourceforge.net/ [Joachim Henke] ; =============================================================================================================================== Func _Base91Encode($sStr) Local $aB91 = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"', '', 2) Local $sEncoded, $b, $n, $v, $aStr = StringToASCIIArray($sStr) For $i = 0 To UBound($aStr) - 1 $b = BitOR($b, BitShift($aStr[$i], ($n * - 1))) $n += 8 If $n > 13 Then $v = BitAND($b, 8191) $s = 13 + ($v <= 88) If $v <= 88 Then $v = BitAND($b, 16383) $b = BitShift($b, $s) $n -= $s $sEncoded &= $aB91[Mod($v, 91)] & $aB91[$v / 91] EndIf Next If $n Then $sEncoded &= $aB91[Mod($b, 91)] If $n > 7 Or $b > 90 Then $sEncoded &= $aB91[$b / 91] EndIf Return $sEncoded EndFunc ;==>_Base91Encode ; #FUNCTION# ==================================================================================================================== ; Name...........: _Base91Encode ; Description ...: Decodes string from Base91 ; Author ........: Brian J Christy (Beege) ; Source ........: http://base91.sourceforge.net/ [Joachim Henke] ; =============================================================================================================================== Func _Base91Decode($sStr) Local $sB91 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"' Local $sDecoded, $n, $c, $b, $v = -1, $aStr = StringSplit($sStr, '', 2) For $i = 0 To UBound($aStr) - 1 $c = StringInStr($sB91, $aStr[$i], 1) - 1 If $v < 0 Then $v = $c Else $v += $c * 91 $b = BitOR($b, BitShift($v, ($n * - 1))) $n += 13 + (BitAND($v, 8191) <= 88) Do $sDecoded &= Chr(BitAND($b, 255)) $b = BitShift($b, 8) $n -= 8 Until Not ($n > 7) $v = -1 EndIf Next If ($v + 1) Then $sDecoded &= Chr(BitAND(BitOR($b, BitShift($v, ($n * - 1))), 255)) Return $sDecoded EndFunc ;==>_Base91Decode Base128: ; #FUNCTION# ==================================================================================================================== ; Name...........: _Base128Encode ; Description ...: Decodes string from Base128 ; Author ........: Brian J Christy (Beege) ; Source ........: https://github.com/seizu/base128/blob/master/base128.php [Erich Pribitzer] ; =============================================================================================================================== Func _Base128Encode($sStr) Local $aB128 = StringSplit('!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ', '', 2) Local $sEncoded, $ls, $r, $rs = 7, $aStr = StringToASCIIArray($sStr & ' ') For $i = 0 To UBound($aStr) - 1 If $ls > 7 Then $i -= 1 $ls = 0 $rs = 7 EndIf $nc = BitOR(BitAND(BitShift($aStr[$i], ($ls * -1)), 0x7f), $r) $r = BitAND(BitShift($aStr[$i], $rs), 0x7f) $rs -= 1 $ls += 1 $sEncoded &= $aB128[$nc] Next Return $sEncoded EndFunc ;==>_Base128Encode ; #FUNCTION# ==================================================================================================================== ; Name...........: _Base91Encode ; Description ...: Decodes string from Base128 ; Author ........: Brian J Christy (Beege) ; Source ........: https://github.com/seizu/base128/blob/master/base128.php [Erich Pribitzer] ; =============================================================================================================================== Func _Base128Decode($sStr) Local $sB128 = '!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ' Local $sDecoded, $r, $rs = 8, $ls = 7, $aStr = StringSplit($sStr, '', 2) For $i = 0 To UBound($aStr) - 1 $nc = StringInStr($sB128, $aStr[$i], 1) - 1 If $rs > 7 Then $rs = 1 $ls = 7 $r = $nc ContinueLoop EndIf $r1 = $nc $nc = BitOR(BitAND(BitShift($nc, ($ls * -1)), 0xFF), $r) $r = BitShift($r1, $rs) $rs += 1 $ls -= 1 $sDecoded &= Chr($nc) Next Return $sDecoded EndFunc ;==>_Base128Decode Dll Size Tests: Binary Len = 57346 Base64 Len = 38232 Base91 Len = 35180 Base128 Len = 32768 Base91 - Base128.au3 Edit: Fixed Base91 error pointed out by trancexx