Advertisement
BigDan256

Untitled

Mar 21st, 2022
1,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ZeroBE64$ = Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0)
  2. OneBE64$  = Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(1)
  3.  
  4. Function DumpBE64(A$)
  5.     out$ = "0x"
  6.     For I = 1 To 8
  7.         out$ = out$ + Right$(Hex$(Asc(Mid$(A$, I))), 2)
  8.     Next
  9.     Print out$
  10. End Function
  11.  
  12. Print "DumpBE64 Tests:"
  13. DumpBE64 ZeroBE64$
  14. DumpBE64 OneBE64$
  15.  
  16. ;**
  17. ;* Converts an integer To String representation
  18. ;*
  19. ;* Returns the String representation of num. The conversion is done byte-wise
  20. ;* with the high-nibble First.
  21. ;*
  22. ;* @param num   An integer
  23. ;*
  24. ;* @Return The String representation of the given integer
  25. ;*/
  26. Function EncodeBE64$(num)
  27.     result$ = ""
  28.     For I = 1 To 8
  29.        result$ = Chr$(num And 255) + result$
  30.        num     = num Shr 8
  31.     Next
  32.     Return result$
  33. End Function
  34.  
  35. Print "EncodeBE64 Tests:"
  36. DumpBE64 EncodeBE64$(1)
  37. DumpBE64 EncodeBE64$(256)
  38.  
  39. ;**
  40. ;* Converts a String representation to an integer
  41. ;*
  42. ;* Returns the integer representation of num$. The conversion is done byte-wise
  43. ;* with the high-nibble First.
  44. ;*
  45. ;* @param num$  An integer
  46. ;*
  47. ;* @Return The integer equivalent of the given string
  48. ;*/
  49. Function DecodeBE64(num$)
  50.     result = 0
  51.     For I = 1 To 8
  52.        A = Asc(Mid$(num$, I))
  53.        If A < 0 Then A = 0
  54.        result = result Shl 8 + A
  55.     Next
  56.     Return result
  57. End Function
  58.  
  59. Print "DecodeBE64 Tests:"
  60. Print DecodeBE64(ZeroBE64$)
  61. Print DecodeBE64(OneBE64$)
  62. Print DecodeBE64(EncodeBE64$(65535))
  63.  
  64. ;Stop here if you have 64-bit interpreter
  65. ;A$ = EncodeBE64$($7fffffffffffffff)
  66. ;B$ = EncodeBE64$($0123456789ABCDEF)
  67. ;Q  = DecodeBE64(A$) / DecodeBE64(B$)
  68. ;R  = DecodeBE64(A$) Mod DecodeBE64(B$)
  69. ;Print Q ;Rem Windows Calc says "0x70"
  70. ;Print R ;Rem Windows Calc says "0x91A2B3C4D5E76F"
  71. ;C$ = EncodeBE64$(Q)
  72. ;D$ = EncodeBE64$(R)
  73. ;WaitKey
  74. ;End
  75.  
  76. ;**
  77. ;* Compare two numbers
  78. ;*
  79. ;* Compares the num1 To the num2 And returns the result as an integer
  80. ;*
  81. ;* @param num1$ The Left operand, as a big-endian String
  82. ;* @param num2$ The Right operand, as a big-endian String
  83. ;*
  84. ;* @return Returns 0 If the two operands are equal, 1 If the num1 is larger
  85. ;*         than the num2, -1 otherwise
  86. ;*/
  87. Function CompBE64(num1$, num2$)
  88.     For I = 1 To 8
  89.         A = Asc(Mid$(num1$, I))
  90.         If A < 0 Then A = 0
  91.         B = Asc(Mid$(num2$, I))
  92.         If B < 0 Then B = 0
  93.         If A < B Then Return -1
  94.         If A > B Then Return 1
  95.     Next
  96.     Return 0
  97. End Function
  98.  
  99. Print "CompBE64 Tests:"
  100. Print CompBE64(ZeroBE64$, ZeroBE64$)
  101. Print CompBE64(ZeroBE64$, OneBE64$)
  102. Print CompBE64(OneBE64$, ZeroBE64$)
  103. Print CompBE64(OneBE64$, OneBE64$)
  104. Print CompBE64("", ZeroBE64$)
  105. Print CompBE64("", OneBE64$)
  106. Print CompBE64(ZeroBE64$, "")
  107. Print CompBE64(OneBE64$, "")
  108.  
  109. ;**
  110. ;* Shift the string left by a number of bits
  111. ;*
  112. ;* Shift the bits of num$ bits steps to the left
  113. ;*
  114. ;* @param num$ The number to shift
  115. ;* @param bits Number of bits to shift left
  116. ;*
  117. ;* @return This number, shifted left bits bits
  118. ;*/
  119. Function ShlBE64$(num$, bits)
  120.     result$ = ""
  121.     H = bits Shr 3
  122.     L = bits And 7
  123.     For I = 1 To 8
  124.         A = Asc(Mid$(num$, I + H))
  125.         If A < 0 Then A = 0
  126.         B = Asc(Mid$(num$, I + H + 1))
  127.         If B < 0 Then B = 0
  128.         result$ = result$ + Chr$((A Shl L) Or (B Shr (8 - L)))
  129.     Next
  130.     Return result$
  131. End Function
  132.  
  133. Print "ShlBE64 Tests:"
  134. DumpBE64 ShlBE64$(OneBE64$, 0)
  135. DumpBE64 ShlBE64$(OneBE64$, 1)
  136. DumpBE64 ShlBE64$(OneBE64$, 2)
  137. DumpBE64 ShlBE64$(OneBE64$, 4)
  138. DumpBE64 ShlBE64$(OneBE64$, 8)
  139. DumpBE64 ShlBE64$(OneBE64$, 16)
  140. DumpBE64 ShlBE64$(OneBE64$, 32)
  141. DumpBE64 ShlBE64$(OneBE64$, 63)
  142. DumpBE64 ShlBE64$(OneBE64$, 64)
  143.  
  144. ;**
  145. ;* Subtraction on string
  146. ;*
  147. ;* Difference of num1$ and num2$
  148. ;*
  149. ;* @param num1$ Initial number
  150. ;* @param num2$ Number to subtract
  151. ;*
  152. ;* @return Difference of num1$ and num2$
  153. ;*/
  154. Function SubBE64$(num1$, num2$)
  155.     result$ = ""
  156.     carry = 0
  157.     For I = 8 To 1 Step -1
  158.         A = Asc(Mid$(num1$, I))
  159.         If A < 0 Then A = 0
  160.         B = Asc(Mid$(num2$, I))
  161.         If B < 0 Then B = 0
  162.         result$ = Chr$((A - B - carry) And 255) + result$
  163.         If A > B
  164.            carry = 0
  165.         ElseIf A < B
  166.            carry = 1
  167.         EndIf
  168.     Next
  169.     Return result$
  170. End Function
  171.  
  172. Print "SubBE64 Tests:"
  173. DumpBE64 SubBE64$(ZeroBE64$, ZeroBE64$)
  174. DumpBE64 SubBE64$(OneBE64$, OneBE64$)
  175. DumpBE64 SubBE64$(OneBE64$, ZeroBE64$)
  176. DumpBE64 SubBE64$(EncodeBE64$(65536), EncodeBE64$(65535))
  177.  
  178. ;**
  179. ;* Addition on string
  180. ;*
  181. ;* Sum of num1$ and num2$
  182. ;*
  183. ;* @param num1$ Initial number
  184. ;* @param num2$ Number to add
  185. ;*
  186. ;* @return Sum of num1$ and num2$
  187. ;*/
  188. Function AddBE64$(num1$, num2$)
  189.     result$ = ""
  190.     carry = 0
  191.     For I = 8 To 1 Step -1
  192.         A = Asc(Mid$(num1$, I))
  193.         If A < 0 Then A = 0
  194.         B = Asc(Mid$(num2$, I))
  195.         If B < 0 Then B = 0
  196.         result$ = Chr$((A + B + carry) And 255) + result$
  197.         If A + B + carry > 255
  198.            carry = 1
  199.         Else
  200.            carry = 0
  201.         EndIf
  202.     Next
  203.     Return result$
  204. End Function
  205.  
  206. Print "AddBE64 Tests:"
  207. DumpBE64 AddBE64$(ZeroBE64$, ZeroBE64$)
  208. DumpBE64 AddBE64$(OneBE64$, OneBE64$)
  209. DumpBE64 AddBE64$(OneBE64$, ZeroBE64$)
  210. DumpBE64 AddBE64$(EncodeBE64$(65535), EncodeBE64$(65535))
  211. DumpBE64 AddBE64$(EncodeBE64$(65536), EncodeBE64$(65535))
  212.  
  213. ;**
  214. ;* Division on string
  215. ;*
  216. ;* Quotient of num1$ and num2$
  217. ;*
  218. ;* @fixme Currently does not pass tests
  219. ;*
  220. ;* @param num1$ Initial number
  221. ;* @param num2$ Number to subtract
  222. ;*
  223. ;* @return Difference of num1$ and num2$
  224. ;*/
  225. Function DivBE64(num1$, num2$)
  226.     Result$    = ""
  227.     remainder$ = num1$
  228.     If CompBE64(num2$, "") <> 0
  229.         For lp = 63 To 0 Step -1
  230.             If CompBE64(ShlBE64$(num2$, lp), num1$) <= 0
  231.                 num1$   = SubBE64$(num1$, ShlBE64$(num2$, lp))
  232.                 Result$ = AddBE64$(Result$, EncodeBE64$(1 Shl lp))
  233.             EndIf
  234.         Next
  235.         remainder$ = num1$
  236.     EndIf
  237.     Return Result$ ;remainder$ also contains remainder
  238. End Function
  239.  
  240. Print "DivBE64 Tests:"
  241. DumpBE64 DivBE64(ZeroBE64$, OneBE64$)
  242. DumpBE64 DivBE64(OneBE64$, OneBE64$)
  243. DumpBE64 DivBE64(EncodeBE64$(12345), OneBE64$)
  244. DumpBE64 DivBE64(EncodeBE64$(12345), EncodeBE64$(456))
  245.  
  246. WaitKey
  247. End
  248.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement