2024-04-29

Navigation

Skip Navigation Links

Hash algorithms

Asymmetric Algorithms

Symmetric Cipher Algorithms

Encoding Algorithms

Compression Algorithms

Pseudo Random Number Algorithms

Steganography

Library Wrappers

String Comparison

Others

Syntax highlighting by Prism
PBCrypto.com Mirror

MIME Base64

Algorithm creator(s)

The MIME group


PB author(s)

Dave Navarro


Description

Base64 is related to UUencoding in that it uses the same mechanism of distributing the bits in 3 bytes into 4 bytes, but it uses a different table to map the resulting data into printable characters.


Note

Base64 encoded data takes one-third more space than the data before the conversion.


Source

https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/23687-base64-encode-decode-inline-asm?t=23062


See also


Source Code

Download source code file mimebase64.bas (Right-click -> "Save as ...")

'----------------------------------------------------------------------------
' Convert a file to MIME text (Base64 Encoded) for PB/DLL or PB/CC
' by Dave Navarro (dave@powerbasic.com)
'
FUNCTION FileToMIME(InFile AS ASCIIZ, OutFile AS ASCIIZ) AS LONG
  LOCAL Enc     AS STRING * 64
  LOCAL b       AS ASCIIZ * 4
  LOCAL InBuff  AS STRING
  LOCAL OutBuff AS STRING
  LOCAL i       AS LONG

  Enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

  OPEN InFile FOR BINARY AS #1
  OPEN OutFile FOR OUTPUT AS #2

    PRINT# 2, "MIME-Version: 1.0"
    PRINT# 2, "Content-Type: application/octet-stream; name=" + InFile
    PRINT# 2, "Content-transfer-encoding: base64"
    PRINT# 2, ""

    WHILE NOT EOF(1)
      GET$ 1, 57, InBuff
      OutBuff = ""
      WHILE LEN(InBuff)
        b = LEFT$(InBuff, 3)
        ! mov AL, b[0]
        ! shr AL, 2
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        ! mov AL, b[1]
        ! mov AH, b[0]
        ! shr AX, 4
        ! and AL, &H3F
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        IF LEN(InBuff) = 1 THEN
          OutBuff = OutBuff + "=="
          EXIT DO
        END IF
        ! mov AL, b[2]
        ! mov AH, b[1]
        ! shr AX, 6
        ! and AL, &H3F
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        IF LEN(InBuff) = 2 THEN
          OutBuff = OutBuff + "="
          EXIT DO
        END IF
        ! mov AL, b[2]
        ! and AL, &H3F
        ! movzx i, AL
        OutBuff = OutBuff + MID$(Enc, i+1, 1)
        InBuff = MID$(InBuff, 4)
      WEND
      PRINT#2, OutBuff
    WEND
  CLOSE

END FUNCTION

Mirror provided by Knuth Konrad