Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

convert file in base64 format back to original

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • convert file in base64 format back to original

    pardon me if this was already done.

    this version is done for speed purposes and my testing of how to do a conversion from base64 to original format and for code to be applied to my email reading project named "mailboy".

    program reads a file in base64 format then saves the file out to the original format

    the file name saved will have ".tmp" added to the end of the filename converted

    credit goes to Semen Matusovski for his base64 routines

    if there are any problems, just IM me.

    some people were working on identifying a files type, man, that be nice to have where the .tmp file could be identified.



    Code:
    'b64totmp.bas
    'written in pbcc 4.04
    'this program converts a file in base64 format back to the original file format.
    '
    'full credit goes to Semen Matusovski for his base64 routines
    '
    
    #COMPILE EXE
    #DIM ALL
    
    
       ' OutBuf is not necessary to allocate
       ' Function = offset in InBuf for last not recognized character
    FUNCTION Decode_Base64(BYVAL InBuf AS DWORD, OutBuf AS STRING) AS LONG
          #REGISTER NONE
          DIM i AS LONG
    
          ! MOV EDI, 0
          ! MOV AH, 0
    
     Decode_BASE64_Lb1:
          ! PUSH EBX
          ! LEA EBX, i
          ! MOV ESI, InBuf
          ! PUSH ESI
          ! MOV CL, 0
    
     Decode_BASE64_Lb2:
          ! MOV AL, [ESI]
    
          ! CMP AL, 13                         ' Pass $CRLF
          ! JE Decode_BASE64_Lb15
          ! CMP AL, 10
          ! JE Decode_BASE64_Lb15
    
          ! CMP AL, 65                         ' A .. Z
          ! JL Decode_BASE64_Lb3
          ! CMP AL, 90
          ! JG Decode_BASE64_Lb3
          ! SUB AL, 65
          ! JMP Decode_BASE64_Lb8
    
     Decode_BASE64_Lb3:
          ! CMP AL, 97                         ' a .. z
          ! JL Decode_BASE64_Lb4
          ! CMP AL, 122
          ! JG Decode_BASE64_Lb4
          ! SUB AL, 71
          ! JMP Decode_BASE64_Lb8
    
     Decode_BASE64_Lb4:
          ! CMP AL, 48                         ' 0 .. 9
          ! JL Decode_BASE64_Lb5
          ! CMP AL, 57
          ! JG Decode_BASE64_Lb5
          ! ADD AL, 4
          ! JMP Decode_BASE64_Lb8
    
     Decode_BASE64_Lb5:
          ! CMP AL, 43                         ' +
          ! JNE Decode_BASE64_Lb6
          ! MOV AL, 62
          ! JMP Decode_BASE64_Lb8
    
     Decode_BASE64_Lb6:
          ! CMP AL, 47                         ' /
          ! JNE Decode_BASE64_Lb7
          ! MOV AL, 63
          ! JMP Decode_BASE64_Lb8
    
     Decode_BASE64_Lb7:
          ! CMP AL, 61                         ' =
          ! JNE Decode_BASE64_Lb18
          ! MOV AL, 255
    
     Decode_BASE64_Lb8:
          ! INC CL
          ! CMP CL, 1
          ! JNE Decode_BASE64_Lb9
          ! MOV [EBX], AL
          ! JMP Decode_BASE64_Lb15
    
     Decode_BASE64_Lb9:
          ! CMP CL, 2
          ! JNE Decode_BASE64_Lb10
          ! MOV [EBX + 1], AL
          ! JMP Decode_BASE64_Lb15
    
     Decode_BASE64_Lb10:
          ! CMP CL, 3
          ! JNE Decode_BASE64_Lb11
          ! MOV [EBX + 2], AL
          ! JMP Decode_BASE64_Lb15
    
     Decode_BASE64_Lb11:
          ! MOV [EBX + 3], AL
    
          ! MOV CL, [EBX]
          ! SHL CL, 2
          ! MOV CH, [EBX + 1]
          ! SHR CH, 4
          ! OR  CL, CH
    
          ! CMP AH, 0
          ! JE Decode_BASE64_Lb12
          ! MOV [EDI], CL
    
     Decode_BASE64_Lb12:
          ! MOV CL, [EBX + 2]
          ! CMP CL, 255
          ! JE  Decode_BASE64_Lb17
    
          ! MOV CL, [EBX + 1]
          ! SHL CL, 4
          ! MOV CH, [EBX + 2]
          ! SHR CH, 2
          ! OR  CL, CH
    
          ! CMP AH, 0
          ! JE Decode_BASE64_Lb13
          ! MOV [EDI + 1], CL
    
     Decode_BASE64_Lb13:
          ! MOV CL, [EBX + 3]
          ! CMP CL, 255
          ! JE  Decode_BASE64_Lb16
    
          ! MOV CL, [EBX + 2]
          ! SHL CL, 6
          ! MOV CH, [EBX + 3]
          ! OR  CL, CH
    
          ! CMP AH, 0
          ! JE Decode_BASE64_Lb14
          ! MOV [EDI + 2], CL
    
     Decode_BASE64_Lb14:
          ! ADD EDI, 3
          ! MOV CL, 0
    
     Decode_BASE64_Lb15:
          ! INC ESI
          ! JMP Decode_BASE64_Lb2
    
    
     Decode_BASE64_Lb16:
          ! INC EDI
    
     Decode_BASE64_Lb17:
          ! INC EDI
    
     Decode_BASE64_Lb18:
          ! MOV ECX, ESI
          ! POP ESI
          ! SUB ECX, ESI
          ! MOV FUNCTION, ECX
          ! POP EBX
    
          ! CMP AH, 0
          ! JNE Decode_BASE64_Lb19
          ! MOV AH, 1
    
          ! MOV i, EDI
    
          OutBuf = SPACE$(i)
          ! MOV EDI, OutBuf
          ! MOV EDI, [EDI]
          ! JMP Decode_BASE64_Lb1
    
     Decode_BASE64_Lb19:
          EXIT FUNCTION
       END FUNCTION
    
    
    FUNCTION PBMAIN () AS LONG
    DIM sinputfilename AS STRING
    DIM soutputfilename AS STRING
    
    FUNCTION=2
    
    'get file name or display help message from the command line
    sinputfilename=TRIM$(COMMAND$)
    IF LEN(sinputfilename)=0 THEN GOTO nocommandlinegiven
    IF INSTR(sinputfilename,"?")<>0 THEN GOTO nocommandlinegiven
    
    'set the output file name
    soutputfilename=sinputfilename+".tmp"
    
    STDOUT "converting file in a base64 format"
    STDOUT "file to convert from commmand line ["+sinputfilename+"]"
    STDOUT "reading file "+sinputfilename
    
    TRY
      OPEN sinputfilename FOR INPUT LOCK READ WRITE AS #1 LEN=4096*4
      CATCH
      GOTO errorinreadingfile
    END TRY
    
    
    DIM smime AS STRING           'string build from file to pass to decodebase64 routine
    DIM sunmime AS STRING         'string returned from decodebase64 routine
    DIM stmparray(1:1) AS STRING
    DIM sreadline AS STRING       'variable for single line reads
    DIM istmparrayupbound AS LONG 'sets the upper boundary for the array being used to create smine variable
    DIM ilinecount AS LONG        'for placing lines read into a array for a join statement later
    DIM ilengthinputfile AS LONG  'sets the array size for reading file in increments
    DIM ifilereadtoendoffile AS LONG  ' a flag to indicate no blank line(s) following base64 lines
    
    'adjust array size to file size for better performance
      ilengthinputfile=LOF(#1)
      istmparrayupbound=1000
      IF ilengthinputfile>100000 THEN    istmparrayupbound=5000
      IF ilengthinputfile>500000 THEN    istmparrayupbound=10000
      IF ilengthinputfile>1000000 THEN   istmparrayupbound=20000
      IF ilengthinputfile>5000000 THEN   istmparrayupbound=33000
      IF ilengthinputfile>10000000 THEN  istmparrayupbound=66000
      IF ilengthinputfile>50000000 THEN  istmparrayupbound=122000
    
      
    
    REDIM  stmparray(1:istmparrayupbound) AS STRING
    ilinecount=0&
    TRY
      DO WHILE sreadline=""
      IF ISTRUE EOF(1) THEN GOTO errorinreadingfile
      LINE INPUT #1, sreadline
      sreadline=TRIM$(sreadline)
      WEND
      CATCH
      GOTO errorinreadingfile
    END TRY
    
    stmparray(1)=sreadline
    ilinecount=1&
    
    TRY
      DO WHILE LEN(sreadline)
      IF ISTRUE EOF(1) THEN ifilereadtoendoffile=1&:GOTO endofreadinginputfile
      LINE INPUT #1, sreadline
      sreadline=TRIM$(sreadline)
      IF LEN(sreadline)=0 THEN EXIT DO
      INCR ilinecount
      IF ilinecount=istmparrayupbound+1& THEN smime=smime+JOIN$(stmparray(),""):ilinecount=1&:RESET stmparray()
      stmparray(ilinecount)=sreadline
      WEND
      endofreadinginputfile:
      CLOSE #1
      smime=smime+JOIN$(stmparray(),"")
      ERASE stmparray()
      CATCH
      GOTO errorinreadingfile
      END TRY
    STDOUT "reading done"
    
    IF LEN(smime)=0 THEN GOTO errorinreadingfile
    
    STDOUT "converting from base64 in memory"
    Decode_Base64(STRPTR(smime), sunmime)
    STDOUT "converting done"
    
    STDOUT "writing to file "+soutputfilename
    'create file for output then close the file for purposes
    TRY
      OPEN soutputfilename FOR OUTPUT AS #1
      PRINT #1,"";
      CLOSE 1
      CATCH
      GOTO errorinwritingfile
    END TRY
    
    TRY
      OPEN soutputfilename FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #1
      PUT$ 1, sunmime
      CLOSE #1
      CATCH
      GOTO errorinwritingfile
    END TRY
    STDOUT "writing done"
    
    STDOUT "completed conversion to "+soutputfilename
    STDOUT "DOS ERRLEVEL returned 10"
    
    FUNCTION=11
    IF  ifilereadtoendoffile THEN FUNCTION=10
    GOTO finish
    
    nocommandlinegiven:
      STDOUT "this program converts a file in base64 format back to the original format"
      STDOUT "place the file to convert on the commandline"
      STDOUT "the output file name will have .tmp added to the file on the command line"
      STDOUT "this program does not validate the input file for a base64 format"
      STDOUT "DOS ERRLEVEL returned for batch processing or returning to a program"
      STDOUT "  0=program failure"
      STDOUT "  1=no command line given or viewing of this screen with ? on command line"
      STDOUT "  2=program failure"
      STDOUT "  3=error in reading input file"
      STDOUT "  4=error in reading no commandline given or viewing of this screen"
      STDOUT " 10=program completed: input file has no blank lines at end of file"
      STDOUT " 11=program completed: read input file successfully"
      STDOUT "pausing for 10 seconds"
      FUNCTION=1
      SLEEP 10000
      GOTO finish
    
    errorinreadingfile:
      CLOSE #1
      STDOUT "error! error! error!"
      STDOUT "error-in reading the input file "+sinputfilename
      STDOUT "error-no specfic error given"
      STDOUT "      make sure the file exist"
      STDOUT "      if file exist make sure the file is not being used by another program"
      STDOUT "      if file exist make sure the file is not filled with spaces
      STDOUT "pausing for 10 seconds"
      FUNCTION=3
      SLEEP 10000
      GOTO finish
    
    errorinwritingfile:
      CLOSE #1
      STDOUT "error! error! error!"
      STDOUT "error-in writing the output file "+soutputfilename
      STDOUT "error-no specfic error given"
      STDOUT "      make sure the file is not being used by another program"
      STDOUT "      make sure there is disk space available"
      STDOUT "      make sure there is file block space available"
      STDOUT "      make sure there is enough memory in comptuer"
      STDOUT "pausing for 10 seconds"
      FUNCTION=4
      SLEEP 10000
    
    FINISH:
      'stdout "press any key to continue"
      'waitkey$
    END FUNCTION
    Last edited by Paul Purvis; 23 May 2008, 06:33 PM.
    p purvis

  • #2
    exe file from the above code
    the program requires a command line
    maybe a sleep statement on errors would come in handy
    i will add those in the code above but will leave this compile version as is with no pauses
    the program was written with batch file processing in mind or can be called from another program without stopping
    no ERRLEVEL returned but would be a nice feature to add in the program
    Attached Files
    Last edited by Paul Purvis; 23 May 2008, 06:34 PM.
    p purvis

    Comment


    • #3
      made a correction to code where a blank line does not follow base64 code in a file.
      added ERRLEVEL return features
      adjusted file read array size to better match file size.
      made better comments in file and formating is better
      change program name to b64totmp/.bas/.exe
      added sleep statements for error messages, program continues, no stopping points
      added a little help message when no command line is given or a "?" is in command line

      i do not want to really start a discussion, but this is the first time i had to deal with base64 code and a good thought came over me today.

      why not place a file's contents(.exe,.db,.com,.vbs, etc) in a base64 file, then at the end of the file, add a blank line or two with a couple of more lines that have the name of the file to make after the conversion and to only create the file after checking the converted file's checksum of characters using md5 or some other high quality hash, if that is the right term(hash). naturally there would be a line for that. this program would be a good starter for that.
      maybe at the end of the file would be:
      blank line
      blank line
      FILENAME: DESTRUCTIVEFILENAME.EXE
      blank line
      checksumused:md5
      checksumvalue: 8d67691cbc01f284db99c11d4eca0be8
      blank line

      maybe at the beginning of the file:
      any editing would destroy the use of this file DO NOT EDIT THIS FILE
      blankline


      i have some programs that are very destructive and i would like to leave those programs on computers without having to have a password to use them and i do not want somebody else accidentally using those programs.
      using the method above would make sure the programs are only used with qualified people.
      even though the files would be larger it would be safer and also easier to transfer over communications and can be zipped tightly probably.

      so i guess that will be the next piece of code to follow this up.
      Last edited by Paul Purvis; 23 May 2008, 07:11 PM.
      p purvis

      Comment

      Working...
      X