ÿþ{{ ** Base64 decoding/encoding functions v0.2 ** decoding by Robert Quattlebaum <darco@deepdarc.com> ** encoding by Tim Farley <tofarley@gmail.com> ** PUBLIC DOMAIN ** 2009-02-08 ** ** TODO: Implement this in ASM! ** TODO: Fix encode so that it does not require the outstring ** size to be defined before encoding. ** }} PUB encode(in_ptr, out_ptr) | i,in,char,size,in_size {{ Encodes a string to base64 at the memory location referenced by out_ptr. Returns the size of the encoded data. Requires pointer to an input string, and an output string. The output string must be long enough to hold the result. Usage example: OBJ b64 : "base64" VAR byte outstring[25] PUB start b64.encode(string("admin:admin"), @outstring) -------------------------------------------------------------------- The size out outstring must be 4 characters for every 3 characters in the input string. If you can fix this, please do! -------------------------------------------------------------------- }} in_size:=strsize(in_ptr)//3 size:=0 repeat ifnot BYTE[in_ptr] quit repeat i from 0 to 2 char:=BYTE[in_ptr++] ifnot char if i == 1 BYTE[@in][i]:=0 BYTE[@in][i+1]:=0 size+=2 elseif i == 2 BYTE[@in][i]:=0 size++ in_ptr-- quit else BYTE[@in][i]:=char size++ base64_encode_3(@in,out_ptr) size++ out_ptr+=4 if in_size == 1 ' Replace last two characters with '=' sign out_ptr-=2 BYTE[out_ptr++]:=61 BYTE[out_ptr++]:=61 elseif in_size == 2 ' Replace last character with '=' sign out_ptr-- BYTE[out_ptr++]:=61 BYTE[out_ptr]:=0 return size PRI base64_encode_3(inptr,outptr) | i,out,tmp out:=0 repeat i from 0 to 2 out|=BYTE[inptr][i]<<((2-i)*8) repeat i from 0 to 3 tmp:=\base64_encode(63&(out>>((3-i)*6))) 'text.str(@tmp) BYTE[outptr][i]:=BYTE[@tmp] pri base64_encode(char) | i 'text.str(string(char)) case char 0..25: return char+"A" 26..51: return (char-26)+"a" 52..61: return (char-52)+"0" 62: return "+" 63: return "/" other: return 0 PUB inplaceDecode(in_ptr) | out_ptr,i,in,char,size {{ Decodes a base64 encoded string in-place. Returns the size of the decoded data. }} out_ptr:=in_ptr size:=0 repeat ifnot BYTE[in_ptr] quit repeat i from 0 to 3 repeat while isWhitespace(char:=BYTE[in_ptr++]) ifnot char BYTE[@in][i]:="=" in_ptr-- quit else BYTE[@in][i]:=char i:=base64_decode_4(@in,out_ptr) out_ptr+=i size+=i while char AND i==3 BYTE[out_ptr]:=0 return size PRI isWhitespace(char) case char 9..13,32: return TRUE other: return FALSE pri base64_tlu(char) | i case char "A".."Z": return char-"A" "a".."z": return char-"a"+26 "0".."9": return char-"0"+52 "+": return 62 "/": return 63 other: return 0 PRI base64_decode_4(inptr,outptr) | retVal,i,out out:=0 retVal:=3 repeat i from 0 to 3 if(BYTE[inptr][i]=="=") case i 3: retVal:=2 2: retVal:=1 1: retVal:=0 0: retVal:=0 quit out|=\base64_tlu(BYTE[inptr][i])<<((3-i)*6) if retVal repeat i from 0 to retVal-1 BYTE[outptr][i]:=BYTE[@out][2-i] return retVal CON {{ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % TERMS OF USE: MIT License % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$% %Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation % %files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, % %modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software% %is furnished to do so, subject to the following conditions: % % % %The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.% % % %THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE % %WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR % %COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, % %ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% }}