Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Base64 Encoding Base64BBFromClearBB B64GetCookie Base64BBToClearBB
;*************************************************************************** ;*************************************************************************** ;*************************************************************************** ;** Base64 conversion UDFs ;** ;** Reference RFC 1421 RFC 1521 ;** http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc1421.html ;** ;** B64GetCookie() ;** Takes no parameters. Returns a value that must be ;** passed to all the other B64 functions in these UDFs ;** Basically it initializes an array for the other ;** functions to use ;** ;** Retuns B64Cookie used by the other B64 functions. ;** ;** Base64BBFromClearBB(B64Cookie,clearbb) ;** A core, low level conversion function not necessarily ;** normally used by most users of these UDFs. ;** Converts a binary buffer holding data to a new binary ;** buffer holding Base64 encoded data. ;** ;** Parameters are: ;** B64Cookie - Value returned by the B64GetCookie ;** function. ;** clearbb - A binary buffer holding non-encoded ;** text. (May be binary data) ;** Returns: A binary buffer containing clear text ;** ;** ;** Base64BBToClearBB(B64Cookie,b64bb) ;* A core, low level conversion function not necessarily ;** normally used by most users of these UDFs. ;** Converts a binary buffer holding base64 encoded data ;** to a new binary buffer holding non-encoded data. ;** ;** Parameters are: ;** B64Cookie - Value returned by the B64GetCookie ;** function. ;** b64bb - A binary buffer holding base64 encoded ;** text. ;** Returns: A binary buffer containing clear text. May ;** be binary data. ;** ;** ;** Base64StringFromClearString(B64Cookie,clearstring) ;** A useful function that will encode a text string ;** ;** Parameters are: ;** B64Cookie - Value returned by the B64GetCookie ;** function. ;** clearstring - text string to encode ;** ;** Returns: Base64 encoded string ;** ;** ;** Base64StringToClearString(B64Cookie,b64String) ;** A useful function that will decode a text string ;** ;** Parameters are: ;** B64Cookie - Value returned by the B64GetCookie ;** function. ;** b64tring - Base64 encoded string to decode ;** ;** Returns: Decoded string ;** ;** ;** Base64FileFromClearFile(B64Cookie,ClearFile,b64file) ;** A useful function thet will Base64 encode a file. ;** The file may be a text or binary file (any file) ;** ;** Parameters are: ;** B64Cookie - Value returned by the B64GetCookie ;** function. ;** ClearFile - File to encode ;** b64file - File to write encoded data to ;** ;** Returns: Size of encoded file ;** ;** ;** ;** ;** ;** ;** ;** Base64FileToClearFile(B64Cookie,b64File,ClearFile) ;** A useful function that will decode a file that is ;** Base64 encoded. ;** ;** Parameters are: ;** B64Cookie - Value returned by the B64GetCookie ;** function. ;** b64file - File to decode ;** ClearFile - File to write decoded data to ;** ;** Returns: Size of decoded file ;** ;** ;** ;** Ver 1.01 Marty Williams July 12, 2002 ;** First commented release ;*************************************************************************** ;*************************************************************************** ;*************************************************************************** #DefineFunction B64GetCookie() B64Cookie=ArrDimension(256,2) ; ,0 is to B64 ,1 if from B64 ArrInitialize(B64Cookie,-1) AUPPER=Char2Num("A") alower=Char2Num("a") ZEROChr=Char2Num("0") for xx=0 to 25 B64Cookie[xx,0]=Num2Char(AUPPER+xx) B64Cookie[xx+26,0]=Num2Char(alower+xx) next for xx=52 to 61 B64Cookie[xx,0]=Num2Char(ZEROChr+xx-52) next B64Cookie[62,0]="+" B64Cookie[63,0]="/" for xx=0 to 63 val=Char2Num(B64Cookie[xx,0]) B64Cookie[val,1]=xx next b64Cookie[Char2Num("="),1]=9999 ; flag the = sign return(B64Cookie) #EndFunction ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #DefineFunction Base64BBToClearBB(B64Cookie,b64bb) quadsize=BinaryEODGet(b64bb) clearbb=BinaryAlloc(quadsize) ; a tad too big. So... qindex=0 cindex=0 tcheck=0 while 1 Got4=0 while 1 if qindex == quadsize then break d1=B64Cookie[BinaryPeek(b64bb,qindex+0),1] if d1 != -1 then break qindex=qindex+1 endwhile while 1 if qindex == quadsize then break d2=B64Cookie[BinaryPeek(b64bb,qindex+1),1] if d2 != -1 then break qindex=qindex+1 endwhile while 1 if qindex == quadsize then break d3=B64Cookie[BinaryPeek(b64bb,qindex+2),1] if d3 != -1 then break qindex=qindex+1 endwhile while 1 if qindex == quadsize then break d4=B64Cookie[BinaryPeek(b64bb,qindex+3),1] if d4 != -1 Got4=1 break endif qindex=qindex+1 endwhile if Got4==0 then break qindex=qindex+4 tcheck=d1+d2+d3+d4 if tcheck>=9999 then break c1= (d1<<2) | ((d2&48)>>4) c2= ((d2&15)<<4) | ((d3&60)>>2) c3= ((d3&3)<<6) | d4 BinaryPoke(clearbb,cindex+0,c1) BinaryPoke(clearbb,cindex+1,c2) BinaryPoke(clearbb,cindex+2,c3) cindex=cindex+3 endwhile if tcheck>=9999 && Got4==1 c1= (d1<<2) | ((d2&48)>>4) BinaryPoke(clearbb,cindex,c1) cindex=cindex+1 if d3!=9999 c2= ((d2&15)<<4) | ((d3&60)>>2) BinaryPoke(clearbb,cindex,c2) cindex=cindex+1 endif if d4!=9999 c3= ((d3&3)<<6) | d4 BinaryPoke(clearbb,cindex,c3) cindex=cindex+1 endif endif return(clearbb) #endfunction ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #DefineFunction Base64BBFromClearBB(B64Cookie,clearbb) ;clearbb contains bytes to be converted into base64 format ;how many triplets clearbytes=BinaryEODGet(clearbb) cleartriplets= clearbytes/3 clearremnants= clearbytes mod 3 ;if clearremnants !=0 then cleartriplets=cleartriplets+1 b64quads=ClearTriplets if clearremnants!=0 then b64quads=b64quads+1 b64bytes= b64quads*4 + (b64quads/16)*2 + 6; 4bytes per quad + CRLF every 16 quads plus 6 in case I got the math wrong ;Only 16 quads per line allowed quadlinecount=0 B64BB=BinaryAlloc(b64bytes) for xx= 1 to cleartriplets c1 = BinaryPeek(clearbb,(xx-1)*3+0) c2 = BinaryPeek(clearbb,(xx-1)*3+1) c3 = BinaryPeek(clearbb,(xx-1)*3+2) d1 = c1 >> 2 d2 = ( (c1 & 3) << 4) | (c2 >> 4) d3 = ( (c2 & 15) << 2) | (c3 >> 6) d4 = ( (c3 & 63) ) quad=strcat(B64Cookie[d1,0],B64Cookie[d2,0],B64Cookie[d3,0],B64Cookie[d4,0]) BinaryPokeStr(b64BB,BinaryEODGet(b64bb),quad) quadlinecount=quadlinecount+1 if (quadlinecount mod 16)==0 then BinaryPokeStr(b64BB,BinaryEODGet(b64bb),@crlf) next xx switch clearremnants case 1 c1=BinaryPeek(clearbb,(cleartriplets)*3+0) d1 = c1 >> 2 d2 = ( (c1 & 3) << 4) quad=strcat(B64Cookie[d1,0],B64Cookie[d2,0],"==") continue case 2 c1=BinaryPeek(clearbb,(cleartriplets)*3+0) c2 = BinaryPeek(clearbb,(cleartriplets)*3+1) d1 = c1 >> 2 d2 = ( (c1 & 3) << 4) | (c2 >> 4) d3 = ( (c2 & 15) << 2) quad=strcat(B64Cookie[d1,0],B64Cookie[d2,0],B64Cookie[d3,0],"=") continue case 1 case 2 BinaryPokeStr(b64BB,BinaryEODGet(b64bb),quad) quadlinecount=quadlinecount+1 if (quadlinecount mod 16)==0 then BinaryPokeStr(b64BB,BinaryEODGet(b64bb),@crlf) endswitch return(b64BB) #EndFunction ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #DefineFunction Base64StringFromClearString(B64Cookie,clearstring) s=Strlen(clearstring) bb=BinaryAlloc(s) BinaryPokeStr(bb,0,clearstring) b64bb=Base64BBFromClearBB(B64Cookie,bb) s=BinaryPeekStr(b64bb,0,BinaryEODGet(b64bb)) BinaryFree(bb) BinaryFree(b64bb) return(s) #EndFunction ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #DefineFunction Base64StringToClearString(B64Cookie,b64String) s=Strlen(b64String) bb=BinaryAlloc(s) BinaryPokeStr(bb,0,b64String) clearbb=Base64BBToClearBB(B64Cookie,bb) s=BinaryPeekStr(clearbb,0,BinaryEODGet(clearbb)) BinaryFree(bb) BinaryFree(clearbb) return(s) #EndFunction ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #DefineFunction Base64FileToClearFile(B64Cookie,b64File,ClearFile) fs=FileSize(b64File) bb=BinaryAlloc(fs) BinaryRead(bb,b64File) clearbb=Base64BBToClearBB(B64Cookie,bb) s=BinaryWrite(clearbb,ClearFile) BinaryFree(bb) BinaryFree(clearbb) return(s) #EndFunction ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #DefineFunction Base64FileFromClearFile(B64Cookie,ClearFile,b64file) fs=FileSize(ClearFile) bb=BinaryAlloc(fs) BinaryRead(bb,ClearFile) base64bb=Base64BBFromClearBB(B64Cookie,bb) s=BinaryWrite(base64bb,b64file) BinaryFree(bb) BinaryFree(base64bb) return(s) #EndFunction ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Sample usage ; Step 1 Call b64GetCookie to get B64Cookie first B64Cookie=B64GetCookie() ;Then use the various functions test="A quick brown fox orders a triple mocha." b64test=Base64StringFromClearString(B64Cookie,test) Message(test,b64test) test2=Base64StringToClearString(B64Cookie,b64test) Message(test,test2) exit
Article ID: W15758
File Created: 2003:05:13:11:29:56
Last Updated: 2003:05:13:11:29:56