| 
									
										
										
										
											2008-08-29 00:22:53 -04:00
										 |  |  | ! Copyright (C) 2008 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 20:16:18 -04:00
										 |  |  | USING: kernel math sequences io.binary splitting grouping | 
					
						
							|  |  |  | accessors ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: base64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-10 20:16:18 -04:00
										 |  |  | : count-end ( seq quot -- n )
 | 
					
						
							|  |  |  |     trim-right-slice [ seq>> length ] [ to>> ] bi - ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ch>base64 ( ch -- ch )
 | 
					
						
							|  |  |  |     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" nth ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : base64>ch ( ch -- ch )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f f
 | 
					
						
							|  |  |  |         f f f f f f f f f f 62 f f f 63 52 53 54 55 56 57 58 59 60 61 f f
 | 
					
						
							|  |  |  |         f 0 f f f 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 | 
					
						
							|  |  |  |         22 23 24 25 f f f f f f 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 | 
					
						
							|  |  |  |         40 41 42 43 44 45 46 47 48 49 50 51
 | 
					
						
							|  |  |  |     } nth ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : encode3 ( seq -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-09-10 20:16:18 -04:00
										 |  |  |     be> 4 <reversed> [ | 
					
						
							|  |  |  |         -6 * shift HEX: 3f bitand ch>base64 | 
					
						
							|  |  |  |     ] with B{ } map-as ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : decode4 ( str -- str )
 | 
					
						
							| 
									
										
										
										
											2008-06-16 06:16:21 -04:00
										 |  |  |     0 [ base64>ch swap 6 shift bitor ] reduce 3 >be ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : >base64-rem ( str -- str )
 | 
					
						
							| 
									
										
										
										
											2008-09-10 20:16:18 -04:00
										 |  |  |     [ 3 0 pad-right encode3 ] [ length 1+ ] bi
 | 
					
						
							|  |  |  |     head-slice 4 CHAR: = pad-right ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >base64 ( seq -- base64 )
 | 
					
						
							|  |  |  |     #! cut string into two pieces, convert 3 bytes at a time | 
					
						
							|  |  |  |     #! pad string with = when not enough bits | 
					
						
							| 
									
										
										
										
											2008-06-16 06:16:21 -04:00
										 |  |  |     dup length dup 3 mod - cut
 | 
					
						
							|  |  |  |     [ 3 <groups> [ encode3 ] map concat ] | 
					
						
							| 
									
										
										
										
											2008-09-06 20:13:59 -04:00
										 |  |  |     [ [ "" ] [ >base64-rem ] if-empty ] | 
					
						
							| 
									
										
										
										
											2008-06-16 06:16:21 -04:00
										 |  |  |     bi* append ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : base64> ( base64 -- str )
 | 
					
						
							|  |  |  |     #! input length must be a multiple of 4 | 
					
						
							| 
									
										
										
										
											2008-06-16 06:16:21 -04:00
										 |  |  |     [ 4 <groups> [ decode4 ] map concat ] | 
					
						
							| 
									
										
										
										
											2008-09-10 20:16:18 -04:00
										 |  |  |     [ [ CHAR: = = ] count-end ] | 
					
						
							| 
									
										
										
										
											2008-06-16 06:16:21 -04:00
										 |  |  |     bi head* ;
 |