chickadee » base64

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

Base64

Encoding and decoding of base64 strings, as per RFC4648.

This means output strings use the character set [A-Za-z0-9/+] for encoding and the character = for padding strings to multiples of 4 characters.

Documentation

base64-encode IN #!optional OUTprocedure

Encodes IN (a string or port) to base64 text on optional output port OUT, and returns OUT.

If the output port is omitted, the encoded output is written to a fresh string and returned.

base64-decode IN #!optional OUTprocedure

Decodes base64 text from IN (a string or port) to optional output port OUT, and returns OUT. Invalid input data is silently skipped.

If the output port is omitted, the decoded output is written to a fresh string and returned.

base64-line-breaks BOOLEANparameter

If #t, the encoder 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".

Examples

(require-extension base64)
(define s "thequickbrownfoxjumpsoverthelazydog")
(base64-encode s)
   ; => "dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
(base64-decode (base64-encode s))
   ; => "thequickbrownfoxjumpsoverthelazydog"
(get-output-string (base64-encode (open-input-string s)
                                  (open-output-string)))
   ; => "dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
(base64-encode s (current-output-port))
dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c=        ; stdout

A script that encodes a file given on the command-line in the style of uuencode -m. It reads the entire file into memory:

#!/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)))))

Instead, you can use a constant amount of memory by using port I/O:

#!/usr/local/bin/csi -script
(use base64)                           ;; uuencode.scm
(base64-line-breaks #t)
(base64-encode (current-input-port)
               (current-output-port)) 
#!/usr/local/bin/csi -script
(use base64)                           ;; uudecode.scm
(base64-decode (current-input-port)
               (current-output-port))
$ md5sum chicken-4.0.0.tar.gz
658deaae356f3360e48d6d1628fc062e  chicken-4.0.0.tar.gz
$ csi4 -script uuencode.scm < chicken-4.0.0.tar.gz | csi4 -script uudecode.scm | md5sum
658deaae356f3360e48d6d1628fc062e  -

About this egg

Author

Version history

3.2
Encoding and decoding support port I/O
3.1
Add optional decoder linebreaks
3.0
API change, reimplement in Scheme
2.2
Fix encoding problem with chars > 127 [zb]
2.1
Ported to hygienic Chicken Peter Bex]
2.0
Reimplemented in C for large speedup. [zbigniew]
1.3
Replaced implementation with a much faster version by James Bailey
1.2
Removed read syntax
1.1
Decoding accepts whitespace now.
1.0
Initial release.

License

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.

Contents »