You are looking at historical revision 14253 of this page. It may differ significantly from its current revision.
Encoding and decoding of base64 strings.
Documentation[procedure] (base64-encode STRING)
Returns STRING encoded as base64 text (a string).[procedure] (base64-decode STRING)
Returns the decoded string from the base64 data STRING. Invalid input data is silently skipped.[parameter] (base64-line-breaks BOOLEAN) [default: #f]
If #t, the decoder inserts a CRLF into the output string every 76 output characters (57 input characters). A CRLF will also be appended to the final line, only if it was a partial one (between 1 and 75 output characters). In a UTF8 environment, character means "byte".
(define s "thequickbrownfoxjumpsoverthelazydog") (base64-encode s)
(base64-decode (base64-encode s))
A script that encodes a file given on the command-line in the style of uuencode -m:
#!/usr/local/bin/csi -script (use base64) (base64-line-breaks #t) (display (base64-encode (call-with-input-file (car (command-line-arguments)) (lambda (p) (read-string #f p)))))
The last script reads the file completely into memory. You can use a constant amount of memory and obtain the same output by reading in multiples of 57 characters and displaying incrementally:
#!/usr/local/bin/csi -script (use base64) (base64-line-breaks #t) (call-with-input-file (car (command-line-arguments)) (lambda (p) (let loop () (let ((s (read-string (* 57 60) p))) (cond ((string=? s "")) (else (display (base64-encode s)) (loop)))))))
For properly formatted output in a UTF-8 environment, you should read 57 bytes instead of 57 characters, as these procedures operate on octets.
About this egg
- Bigloo code by James Bailey
- Ported to CHICKEN by felix
- Optimized 3.0 implementation by Jim Ursetto
- API change, reimplement in Scheme
- Fix encoding problem with chars > 127 [zb]
- Ported to hygienic Chicken [Peter Bex]
- Reimplemented in C for large speedup. [zbigniew]
- Replaced implementation with a much faster version by James Bailey
- Removed read syntax
- Decoding accepts whitespace now.
- Initial release.
Copyright (c) 2004 James Bailey. Copyright (c) 2009 Jim Ursetto. 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.