here is the first program of two programs i had written to save a file in base64 format
the second program to convert back from a base64 format to an original format is not yet written but will not take as long as the first part.
i am placing the file here to convert to base64 format first.
because i am including more information into the file than just the base64 code, i am call this format base64p and it will have an file extension of .p64
well it should not be that hard to guess where the p comes from.
i will make more comments on this later, it is late and i need some rest.
the second program to convert back from a base64 format to an original format is not yet written but will not take as long as the first part.
i am placing the file here to convert to base64 format first.
because i am including more information into the file than just the base64 code, i am call this format base64p and it will have an file extension of .p64
well it should not be that hard to guess where the p comes from.
i will make more comments on this later, it is late and i need some rest.
Code:
'file2p64.bas 'written in pbcc 4.04 'this program converts a file into base64p format. 'base64p format is extended format from base64 created by p purvis ' 'full credit goes to Semen Matusovski for his base64 routines ' #COMPILE EXE #DIM ALL #REGISTER NONE GLOBAL gsfiledatetime1 AS STRING GLOBAL gsfiledatetime2 AS STRING GLOBAL gsfiledatetime3 AS STRING GLOBAL gsfiledatetime4 AS STRING GLOBAL gsfiledatetime5 AS STRING GLOBAL gsfiledatetime6 AS STRING GLOBAL gqinputfilesize AS QUAD #INCLUDE "WIN32API.INC" #INCLUDE "SHA256P64.INC" DECLARE FUNCTION PathMatchSpec LIB "SHLWAPI.DLL" ALIAS "PathMatchSpecA"(pszFile AS ASCIIZ, pszSpec AS ASCIIZ) AS LONG ' OutBuf is not necessary to allocate ' $CRLF after each nGroups per 3 input bytes FUNCTION Encode_BASE64(BYREF InBuf AS STRING, BYVAL nGroups AS LONG, OutBuf AS STRING) AS LONG #REGISTER NONE LOCAL qtemp AS QUAD qtemp=(4 * FIX((LEN(InBuf) + 2) / 3) + 2 * FIX(LEN(InBuf) / (3 * nGroups))) OutBuf = SPACE$(qtemp) ! PUSH EBX ! LEA EBX, Encode_BASE64_Trt ! MOV EDI, OutBuf ! MOV EDI, [EDI] ' StrPtr(OutBuf) ! MOV ESI, InBuf ! MOV ESI, [ESI] ' StrPtr(InBuf) ! MOV EDX, ESI ! Sub EDX, 4 ! MOV EDX, [EDX] ' Len(InBuf) ! MOV ECX, nGroups Encode_BASE64_Lb1: INCR qtemp ! CMP EDX, 0 ! JLE Encode_BASE64_Lb4 ! MOV AL, [ESI] ! SHR AL, 2 ! XLATB ! MOV [EDI], AL ! INC EDI ! MOV AL, [ESI + 1] ! MOV AH, [ESI] ! SHR AX, 4 ! And AL, &H3F ! XLATB ! MOV [EDI], AL ! INC EDI ! CMP EDX, 1 ! JNE Encode_BASE64_Lb2 ! MOV AL, 61 ' Add == ! MOV [EDI], AL ! INC EDI ! MOV [EDI], AL ! INC EDI ! JMP Encode_BASE64_Lb4 Encode_BASE64_Lb2: ! MOV AL, [ESI + 2] ! MOV AH, [ESI + 1] ! SHR AX, 6 ! And AL, &H3F ! XLATB ! MOV [EDI], AL ! INC EDI ! CMP EDX, 2 ! JNE Encode_BASE64_Lb3 ! MOV AL, 61 ' Add = ! MOV [EDI], AL ! INC EDI ! JMP Encode_BASE64_Lb4 Encode_BASE64_Lb3: ! MOV AL, [ESI + 2] ! And AL, &H3F ! XLATB ! MOV [EDI], AL ! INC EDI ! Add ESI, 3 ! Sub EDX, 3 ! DEC ECX ! CMP ECX, 0 ! JNE Encode_BASE64_Lb1 ! MOV AL, 13 ' Add $CRLF ! MOV [EDI], AL ! MOV AL, 10 ! MOV [EDI + 1], AL ! Add EDI, 2 ! MOV ECX, nGroups ! JMP Encode_BASE64_Lb1 Encode_BASE64_Lb4: ! POP EBX EXIT FUNCTION Encode_Base64_Trt: ! DB 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ! DB 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122 ! DB 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47 END FUNCTION FUNCTION FileTimeToInternationalTime(File_Time AS FILETIME) AS STRING LOCAL Sys_Time AS SystemTime FileTimeToSystemTime File_Time, Sys_Time FUNCTION = _ FORMAT$(Sys_Time.wyear, "0000") & "-" & FORMAT$(Sys_Time.wMonth, "00") & "-" & _ FORMAT$(Sys_Time.wDay, "00") & " " & FORMAT$(Sys_Time.wHour, "00") & ":" & _ FORMAT$(Sys_Time.wMinute, "00") & ":" & FORMAT$(Sys_Time.wSecond, "00") END FUNCTION FUNCTION FileTimeToLocal(File_Time AS FILETIME) AS STRING LOCAL Sys_Time AS SystemTime FileTimeToSystemTime File_Time, Sys_Time FUNCTION = _ FORMAT$(Sys_Time.wyear, "0000") & "-" & FORMAT$(Sys_Time.wMonth, "00") & "-" & _ FORMAT$(Sys_Time.wDay, "00") & " " & FORMAT$(Sys_Time.wHour, "00") & ":" & _ FORMAT$(Sys_Time.wMinute, "00") & ":" & FORMAT$(Sys_Time.wSecond, "00") END FUNCTION FUNCTION getinputfileinfointoglobalvar(somefile AS STRING) AS LONG LOCAL hFile AS DWORD LOCAL FindData AS WIN32_FIND_DATA LOCAL lsys AS systemtime LOCAL SYS_TIME AS SYSTEMTIME FUNCTION=0 hFile = FindFirstFile(BYVAL STRPTR(SomeFile), FindData) 'Get file data, file time included IF hFile <> %INVALID_HANDLE_VALUE THEN 'We have an handle coresponding to the file gqinputfilesize= FindData.nFileSizeHigh * (%MAXDWORD + 1) + FindData.nFileSizeLow FileTimeToSystemTime FindData.ftCreationTime, Lsys gsfiledatetime1=FileTimeToInternationalTime(FindData.ftCreationTime) gsfiledatetime2=FileTimeToInternationalTime(FindData.ftLastAccessTime) gsfiledatetime3=FileTimeToInternationalTime(FindData.ftLastWriteTime) FileTimeToLocalFileTime FindData.ftCreationTime, FindData.ftCreationTime FileTimeToLocalFileTime FindData.ftLastAccessTime, FindData.ftLastAccessTime FileTimeToLocalFileTime FindData.ftLastWriteTime, FindData.ftLastWriteTime gsfiledatetime4=FileTimeToLocal(FindData.ftCreationTime) gsfiledatetime5=FileTimeToLocal(FindData.ftLastAccessTime) gsfiledatetime6=FileTimeToLocal(FindData.ftLastWriteTime) FUNCTION=1 END IF END FUNCTION FUNCTION PBMAIN () AS LONG LOCAL sinputfilename AS STRING LOCAL soutputfilename AS STRING LOCAL sbase64p AS STRING 'string build from file to pass to decodebase64 routine LOCAL sunbase64p AS STRING 'string returned from decodebase64 routine LOCAL qlengthinputfile AS QUAD 'sets the array size for reading file in increments LOCAL sinputfilechecksummethod AS STRING LOCAL sinputfilechecksumvalue AS STRING LOCAL stempinputfilename AS STRING LOCAL isha256errorcode AS LONG LOCAL psha256binarystringptr AS DWORD PTR LOCAL ssha256alphastring AS STRING LOCAL ssha256binarystring AS STRING LOCAL qbyteslefttoread AS QUAD LOCAL ibytestoread AS LONG LOCAL qbytestoreadinblock AS QUAD LOCAL iblockprocessing AS LONG LOCAL itemp AS LONG LOCAL stemp 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 qbytestoreadinblock=10485760&& sinputfilename=sinputfilename+" " stemp=UCASE$(sinputfilename) itemp=INSTR(stemp,"/BLOCKSIZE=") IF itemp THEN stemp=MID$(sinputfilename,itemp,(INSTR(itemp,sinputfilename," ")-itemp)) REPLACE stemp WITH "" IN sinputfilename itemp=LEN(stemp)- INSTR(stemp,"=") qbytestoreadinblock=VAL(RIGHT$(stemp,itemp)) IF qbytestoreadinblock THEN qbytestoreadinblock=qbytestoreadinblock*1048576&& END IF sinputfilename=TRIM$(sinputfilename) 'set the output file name soutputfilename=sinputfilename+".p64" sinputfilechecksummethod="none" sinputfilechecksumvalue="" IF qbytestoreadinblock<4194304&& THEN qbytestoreadinblock=4194304&& STDOUT "convert file to base64p format" STDOUT "file from commmand line " STDOUT "["+sinputfilename+"]" STDOUT "checking if file exist" itemp=getinputfileinfointoglobalvar(sinputfilename) IF itemp=0 THEN GOTO errorfiledoesnotexist STDOUT "length of file is "+TRIM$(STR$(gqinputfilesize)) IF gqinputfilesize =0 THEN GOTO createbase64pfile END IF STDOUT "calculating checksum of file" TRY OPEN sinputfilename FOR INPUT AS #1 CLOSE #1 CATCH GOTO errorinreadingfile END TRY isha256errorcode = SHA_File&(sinputfilename, ssha256binarystring) IF isha256errorcode =0 THEN psha256binarystringptr = STRPTR(ssha256binarystring) ssha256alphastring="" FOR itemp = 0 TO 7 ssha256alphastring = ssha256alphastring + HEX$(@psha256binarystringptr[itemp], 8) NEXT itemp sinputfilechecksummethod="sha256" sinputfilechecksumvalue=ssha256alphastring ELSE 'PRINT "sha256p64 error" END IF STDOUT "calculating done" STDOUT "opening input file "+sinputfilename TRY OPEN sinputfilename FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #1 CATCH GOTO errorinreadingfile END TRY qlengthinputfile=LOF(#1) IF qlengthinputfile<>gqinputfilesize THEN GOTO errorinreadingfile qbyteslefttoread=qlengthinputfile STDOUT "opening output file "+soutputfilename TRY OPEN soutputfilename FOR OUTPUT AS #2 PRINT #2,""; CLOSE #2 CATCH GOTO errorinwritingfile END TRY STDOUT "block size used"+STR$(qbytestoreadinblock/1048576)+"Mb" STDOUT "processing"+STR$((qbyteslefttoread\qbytestoreadinblock)+1)+" block(s)" 'read the file convert then save the base64 code into the file GOTO createbase64pfile 'processing blocks of data in the input file readsectionofinputfile: sunbase64p="" sbase64p="" IF qbyteslefttoread=0 THEN GOTO wrapupbasep64file IF qbyteslefttoread>qbytestoreadinblock THEN ibytestoread=qbytestoreadinblock ELSE ibytestoread=qbyteslefttoread END IF qbyteslefttoread=qbyteslefttoread-ibytestoread TRY GET$ #1, ibytestoread,sunbase64p IF LEN(sunbase64p)=0 THEN GOTO errorinreadingfile CATCH GOTO errorinreadingfile END TRY Encode_BASE64(sunbase64p, 18, sbase64p) sunbase64p="" GOTO updatebasep64file createbase64pfile: stempinputfilename=STRREVERSE$("\"+sinputfilename) itemp=INSTR(stempinputfilename, ANY "\:") stempinputfilename=LEFT$(stempinputfilename,itemp-1) stempinputfilename=STRREVERSE$(stempinputfilename) TRY OPEN soutputfilename FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #2 PUT$ 2, "start of file marker WARNING DO NOT EDIT ANY OF THIS FILE"+$CRLF PUT$ 2, "any editing could and should and will destroy the use of this file"+$CRLF+$CRLF IF gqinputfilesize =0 THEN PUT$ 2, "**base64pstartbase64pstartbase64pstart"+$CRLF PUT$ 2, "*sectionstartsectionstartsectionstart"+$CRLF PUT$ 2, "*sectonendsectionendsectionend"+$CRLF GOTO wrapupbasep64file PUT$ 2, REPEAT$(72,"0") END IF PUT$ 2, "**base64pstartbase64pstartbase64pstart"+$CRLF CATCH GOTO errorinwritingfile END TRY GOTO readsectionofinputfile updatebasep64file: TRY PUT$ 2, "*sectionstartsectionstartsectionstart"+$CRLF PUT$ 2, sbase64p+$CRLF PUT$ 2, "*sectonendsectionendsectionend"+$CRLF IF qbyteslefttoread THEN PUT$ 2, $CRLF CATCH GOTO errorinwritingfile END TRY GOTO readsectionofinputfile wrapupbasep64file: TRY PUT$ 2, "**base64pendbase64pendbase64pend"+$CRLF PUT$ 2, $CRLF+$CRLF PUT$ 2, "filename:"+LCASE$(stempinputfilename)+$CRLF PUT$ 2, "filesize:"+TRIM$(STR$(gqinputfilesize))+$CRLF PUT$ 2, "filetimecreationutc:"+gsfiledatetime1+$CRLF PUT$ 2, "filetimelastaccessedutc:"+gsfiledatetime2+$CRLF PUT$ 2, "filetimelastwriteutc:"+gsfiledatetime3+$CRLF PUT$ 2, "checksummethod:"+LCASE$(sinputfilechecksummethod)+$CRLF PUT$ 2, "checkvl:"+LCASE$(sinputfilechecksumvalue)+$CRLF+$CRLF PUT$ 2, "local date/time creation last accessed last write"+$CRLF PUT$ 2, " "+gsfiledatetime1+" "+gsfiledatetime2+" "+gsfiledatetime3+$CRLF+$CRLF PUT$ 2, "end of file marker WARNING DO NOT EDIT ANY OF THIS FILE"+$CRLF PUT$ 2, "any editing could and should and will destroy the use of this file"+$CRLF+$CRLF CLOSE #2 CATCH GOTO errorinwritingfile END TRY STDOUT "completed conversion" STDOUT "DOS ERRLEVEL returned 10" FUNCTION=10 GOTO finish nocommandlinegiven: STDOUT "program converts a file into base64 format" STDOUT "place the file to convert on the commandline" STDOUT "the default block size used in coverting is 10MB, with a of mimium of 4MB" STDOUT "to change block size in MBs, add /blocksize=### on the comammandline" STDOUT "the output file name will have .p64 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 sunbase64p="" sbase64p="" 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 sunbase64p="" sbase64p="" 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 GOTO finish errorfiledoesnotexist: STDOUT "error! error! error!" STDOUT "cannot locate the file "+sinputfilename STDOUT "pausing for 10 seconds" FUNCTION=4 SLEEP 10000 GOTO finish FINISH: 'stdout "press any key to continue" 'sbase64p="" 'sunbase64p="" 'waitkey$ END FUNCTION
Comment