Author Topic: BASE64 Decode & Encode (Rosetta Code Task)  (Read 3078 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
BASE64 Decode & Encode (Rosetta Code Task)
« on: April 14, 2021, 12:52:45 pm »
Inspired by @AndyA , I decided to try my hand at one of the tasks for Rosetta Code. I've already posted it on their site but here is the code I sent off. I translated the C++ implementation.

https://rosettacode.org/wiki/Base64_decode_data#QB64

Code: QB64: [Select]
  1.  
  2. Dim As String udata, decoded
  3. udata = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo"
  4.  
  5. decoded = decode(udata)
  6.  
  7. Print udata
  8. Print decoded
  9.  
  10. Function findIndex& (value As _Unsigned _Byte)
  11.     If Asc("A") <= value And value <= Asc("Z") Then
  12.         findIndex = value - Asc("A")
  13.         Exit Function
  14.     End If
  15.     If Asc("a") <= value And value <= Asc("z") Then
  16.         findIndex = value - Asc("a") + 26
  17.         Exit Function
  18.     End If
  19.     If Asc("0") <= value And value <= Asc("9") Then
  20.         findIndex = value - Asc("0") + 52
  21.         Exit Function
  22.     End If
  23.     If value = Asc("+") Then
  24.         findIndex = 62
  25.         Exit Function
  26.     End If
  27.     If value = Asc("/") Then
  28.         findIndex = 63
  29.         Exit Function
  30.     End If
  31.     findIndex = -1
  32.  
  33. Function encode$ (source As String)
  34.     Dim As String Base64: Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  35.     Dim As _Unsigned _Integer64 length: length = Len(source)
  36.     Dim As _Unsigned _Integer64 it, strend
  37.     Dim As Long acc
  38.     Dim As String sink
  39.     strend = length
  40.     While it <> strend
  41.         Dim As _Unsigned _Byte b1, b2, b3, b4
  42.         it = it + 1
  43.         b1 = Asc(Mid$(source, it, 1))
  44.         sink = sink + Mid$(Base64, _SHR(b1, 2), 1)
  45.         acc = _SHL(b1 And &H3, 4)
  46.         If it <> strend Then
  47.             it = it + 1
  48.             b2 = Asc(Mid$(source, it, 1))
  49.             acc = acc Or _SHR(b2, 4)
  50.             sink = sink + Mid$(Base64, acc, 1)
  51.             acc = _SHL(b2 And &HF, 2)
  52.             If it <> strend Then
  53.                 it = it + 1
  54.                 b3 = Asc(Mid$(source, it, 1))
  55.                 acc = acc Or _SHR(b3, 6)
  56.                 sink = sink + Mid$(Base64, acc, 1)
  57.                 sink = sink + Mid$(Base64, b3 And &H3F, 1)
  58.             Else
  59.                 sink = sink + Mid$(Base64, acc, 1)
  60.                 sink = sink + "="
  61.             End If
  62.         Else
  63.             sink = sink + Mid$(Base64, acc, 1)
  64.             sink = sink + "="
  65.             sink = sink + "="
  66.         End If
  67.     Wend
  68.     encode = sink
  69.  
  70. Function decode$ (source As String)
  71.     Dim As _Unsigned _Integer64 length: length = Len(source)
  72.     Dim As _Unsigned _Integer64 it, strend
  73.     Dim As Long acc
  74.     Dim As String sink
  75.     strend = length
  76.     While it <> strend
  77.         Dim As _Unsigned _Byte b1, b2, b3, b4
  78.         it = it + 1
  79.         b1 = Asc(Mid$(source, it, 1))
  80.         it = it + 1
  81.         b2 = Asc(Mid$(source, it, 1))
  82.         it = it + 1
  83.         b3 = Asc(Mid$(source, it, 1))
  84.         it = it + 1
  85.         b4 = Asc(Mid$(source, it, 1))
  86.         Dim As Long i1, i2
  87.         i1 = findIndex(b1)
  88.         i2 = findIndex(b2)
  89.         acc = _SHL(i1, 2)
  90.         acc = acc Or _SHR(i2, 4)
  91.         sink = sink + Chr$(acc)
  92.         If b3 <> Asc("=") Then
  93.             Dim As Long i3
  94.             i3 = findIndex(b3)
  95.             acc = _SHL(i2 And &HF, 4)
  96.             acc = acc Or _SHR(i3, 2)
  97.             sink = sink + Chr$(acc)
  98.             If b4 <> Asc("=") Then
  99.                 Dim As Long i4
  100.                 i4 = findIndex(b4)
  101.                 acc = _SHL(i3 And &H3, 6)
  102.                 acc = acc Or i4
  103.                 sink = sink + Chr$(acc)
  104.             End If
  105.         End If
  106.     Wend
  107.     decode = sink
« Last Edit: April 14, 2021, 04:41:56 pm by SpriggsySpriggs »
Shuwatch!