Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BASFILE - Converts small files to BAS code.
#38
Hey, I'm kinda tickled my little area has some traffic. Big Grin 

@mnrvovrfc: Good suggestion adding the DIM line.  The goal is to output a portable FUNCTION people can add to any program easy, and that will help make it so.

I admit I haven't used OPTION EXPLICIT before, and didn't really understand what it was for until I heard Fellippe talk about it in a podcast.  I think it will help me prevent coding errors, so I'm going to get to know it.   Also, I am very bad about naming variables well (I rush coding) so that is something I will try to work on.

I think these Base91 functions are working without errors. I scrapped the ones I came up with because they kept corrupting data, so I went back to these I adapted from sources on the internet in another language.  These seem to work ok.

These haven't been speed up using the MID$ technique yet, will do that next.  Will have to do something different than the Base85 function do.  Since Base91 encoded output size is not 100% predictable like Base64 & Base85 output is (it can fluctuate according to input data), I will have to save the original input size in the B91 encoded data, and then strip it out during decoding.

- Dav

Code: (Select All)
'==========
'BASE91.BAS
'==========
'Base-91 Encoder/Decoder Functions.
'Coded by Dav for QB64-PE 3.8.0, SEP/2023
'(adpated from sources found on the web)

'NOTE: This uses a modified Base-91 character set so code output doesn't
'include symbols that conflict with the QB64-PE forum message posting.

'Unlike Base64 & Base85, Base91 space efficiency changes according to input.
'It is somewhere near 23% percent added to the input source, a wee bit better
'than Base85 which is 25% more, and much better than Base64 which is 33%.

Screen 12

_ControlChr Off

Do
    Cls
    Color 15, 1: Print "Checking bytes loop -- Hit any key to stop checking ..."
    a$ = ""
    Print
    Color 7, 0: Print "Original:"
    bytes = 200 + Int(Rnd * 55)
    For i = 0 To bytes
        a$ = a$ + Chr$(Int(Rnd * 255))
    Next
    Print a$
    Print Len(a$); "bytes"

    Print
    Print "Encoded:"
    a2$ = Base91Encode$(a$): Print a2$
    Print Len(a2$); "bytes"
    Print
    Print "Decoded:"
    a3$ = Base91Decode$(a2$): Print a3$
    Print Len(a3$); "bytes"
    Print

    If Len(a$) <> Len(a3$) Then Color 12, 0: Print "Size don't match!"; t: End
    If a$ <> a3$ Then Color 12, 0: Print "Bytes don't match!"; t: End
    _Display

    _Limit 15

    If _KeyHit Then Exit Do

    loopcount = loopcount + 1
    bytecount = bytecount + bytes

Loop

Print "Checked"; loopcount; "loops of"; bytecount; "bytes."
Print "No errors detected."
End

Function Base91Encode$ (in$)

    'Build 91 characters to use
    For i = 33 To 126
        If i <> 34 And i <> 64 And i <> 96 Then Chars$ = Chars$ + Chr$(i)
    Next

    For i& = 1 To Len(in$)
        bits = (bits Or (Asc(Mid$(in$, i&, 1)) * (2 ^ nbits)))
        nbits = nbits + 8
        If nbits > 13 Then
            value = bits Mod 8192
            If value > 88 Then
                bits = (bits \ (2 ^ 13))
                nbits = nbits - 13
            Else
                value = bits Mod 16384
                bits = (bits \ (2 ^ 14))
                nbits = nbits - 14
            End If
            out$ = out$ + Mid$(Chars$, (value Mod 91) + 1, 1) + Mid$(Chars$, (value \ 91) + 1, 1)
        End If
    Next

    If nbits > 0 Then
        out$ = out$ + Mid$(Chars$, (bits Mod 91) + 1, 1)
        If nbits > 7 Or bits > 90 Then
            out$ = out$ + Mid$(Chars$, (bits \ 91) + 1, 1)
        End If
    End If

    Base91Encode$ = out$

End Function



Function Base91Decode$ (in$)
    'Build 91 characters to use
    For i = 33 To 126
        If i <> 34 And i <> 64 And i <> 96 Then Chars$ = Chars$ + Chr$(i)
    Next
    value = -1
    For i& = 1 To Len(in$)
        ascii = InStr(Chars$, Mid$(in$, i&, 1)) - 1
        If value < 0 Then
            value = ascii
        Else
            value = value + (ascii * 91)
            bits = bits Or _ShL(value, nbits)
            nbits = nbits + 13 + (((value And 8191) <= 88) * -1)
            Do Until (nbits > 7) = 0
                out$ = out$ + Chr$(bits And 255)
                bits = _ShR(bits, 8)
                nbits = nbits - 8
            Loop
            value = -1
        End If
    Next

    If (value + 1) Then
        out$ = out$ + Chr$((bits Or _ShL(value, nbits)) And 255)
    End If

    Base91Decode$ = out$

End Function

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
RE: BASFILE - Converts small files to BAS code. - by Dav - 09-29-2023, 02:44 PM



Users browsing this thread: 2 Guest(s)