source: project/wiki/eggref/4/base64 @ 14443

Last change on this file since 14443 was 14443, checked in by Jim Ursetto, 11 years ago

base64: document port I/O

File size: 3.7 KB
Line 
1[[tags:eggs]]
2[[toc:]]
3
4== Description
5
6Encoding and decoding of base64 strings.
7
8== Documentation
9
10<procedure>(base64-encode STRING)</procedure>
11
12Returns {{STRING}} encoded as base64 text (a string).
13
14<procedure>(base64-decode STRING)</procedure>
15
16Returns the decoded string from the base64 data {{STRING}}.  Invalid input data is silently skipped.
17
18<parameter>(base64-line-breaks BOOLEAN) [default: #f]</parameter>
19
20If {{#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".
21
22== Examples
23
24 (require-extension base64)
25 (define s "thequickbrownfoxjumpsoverthelazydog")
26 (base64-encode s)
27    ; => "dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
28 (base64-decode (base64-encode s))
29    ; => "thequickbrownfoxjumpsoverthelazydog"
30 (get-output-string (base64-encode (open-input-string s)
31                                   (open-output-string)))
32    ; => "dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
33
34A script that encodes a file given on the command-line in the style of {{uuencode -m}}.
35It reads the entire file into memory:
36
37 #!/usr/local/bin/csi -script
38 (use base64)
39 (base64-line-breaks #t)
40 
41 (display
42  (base64-encode
43   (call-with-input-file (car (command-line-arguments))
44     (lambda (p) (read-string #f p)))))
45
46Instead, you can use a constant amount of memory by using port I/O:
47
48 #!/usr/local/bin/csi -script
49 (use base64)                           ;; uuencode.scm
50 (base64-line-breaks #t)
51 (base64-encode (current-input-port)
52                (current-output-port))
53
54 #!/usr/local/bin/csi -script
55 (use base64)                           ;; uudecode.scm
56 (base64-decode (current-input-port)
57                (current-output-port))
58
59 $ md5sum chicken-4.0.0.tar.gz
60 658deaae356f3360e48d6d1628fc062e  chicken-4.0.0.tar.gz
61 $ csi4 -script uuencode.scm < chicken-4.0.0.tar.gz | csi4 -script uudecode.scm | md5sum
62 658deaae356f3360e48d6d1628fc062e  -
63
64== About this egg
65
66=== Author
67
68* Bigloo code by James Bailey
69* Ported to CHICKEN by felix
70* 3.0 implementation by Jim Ursetto
71
72=== Version history
73
74; 3.2 : Encoding and decoding support port I/O
75; 3.1 : Add optional decoder linebreaks
76; 3.0 : API change, reimplement in Scheme
77; 2.2 : Fix encoding problem with chars > 127 [zb]
78; 2.1 : Ported to hygienic Chicken [[[Peter Bex]]]
79; 2.0 : Reimplemented in C for large speedup. [zbigniew]
80; 1.3 : Replaced implementation with a much faster version by James Bailey
81; 1.2 : Removed read syntax
82; 1.1 : Decoding accepts whitespace now.
83; 1.0 : Initial release.
84
85=== License
86
87 Copyright (c) 2004 James Bailey.
88 Copyright (c) 2009 Jim Ursetto.
89 
90 Permission is hereby granted, free of charge, to any person obtaining a
91 copy of this software and associated documentation files (the "Software"),
92 to deal in the Software without restriction, including without limitation
93 the rights to use, copy, modify, merge, publish, distribute, sublicense,
94 and/or sell copies of the Software, and to permit persons to whom the
95 Software is furnished to do so, subject to the following conditions:
96 
97 The above copyright notice and this permission notice shall be included in
98 all copies or substantial portions of the Software.
99 
100 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
101 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
102 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
103 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
104 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
105 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
106 DEALINGS IN THE SOFTWARE.
107
Note: See TracBrowser for help on using the repository browser.