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

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

Changes applied for zbigniew (71.201.84.72) through svnwiki:

base64: version

File size: 3.6 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<examples><example>
25<init>
26(require-extension base64)
27</init>
28<expr>
29(define s "thequickbrownfoxjumpsoverthelazydog")
30(base64-encode s)
31</expr>
32<result>
33"dGhlcXVpY2ticm93bmZveGp1bXBzb3ZlcnRoZWxhenlkb2c="
34</result>
35<expr>
36(base64-decode (base64-encode s))
37</expr>
38<result>
39"thequickbrownfoxjumpsoverthelazydog"
40</result>
41</example></examples>
42
43A script that encodes a file given on the command-line in the style of {{uuencode -m}}:
44
45 #!/usr/local/bin/csi -script
46 (use base64)
47 (base64-line-breaks #t)
48 
49 (display
50  (base64-encode
51   (call-with-input-file (car (command-line-arguments))
52     (lambda (p) (read-string #f p)))))
53
54The last script reads the file completely into memory.  You can use a constant amount of memory and obtain the same output by
55reading in multiples of 57 characters and displaying incrementally:
56
57 #!/usr/local/bin/csi -script
58 (use base64)
59 (base64-line-breaks #t)
60 
61 (call-with-input-file (car (command-line-arguments))
62   (lambda (p)
63     (let loop ()
64       (let ((s (read-string (* 57 60) p)))
65         (cond ((string=? s ""))
66               (else
67                (display (base64-encode s))
68                (loop)))))))
69
70For properly formatted output in a UTF-8 environment, you should read 57 bytes instead of 57 characters, as these procedures operate on octets.
71
72== About this egg
73
74=== Author
75
76* Bigloo code by James Bailey
77* Ported to CHICKEN by felix
78* Optimized 3.0 implementation by Jim Ursetto
79
80=== Version history
81
82; 3.1 : Add optional decoder linebreaks
83; 3.0 : API change, reimplement in Scheme
84; 2.2 : Fix encoding problem with chars > 127 [zb]
85; 2.1 : Ported to hygienic Chicken [[[Peter Bex]]]
86; 2.0 : Reimplemented in C for large speedup. [zbigniew]
87; 1.3 : Replaced implementation with a much faster version by James Bailey
88; 1.2 : Removed read syntax
89; 1.1 : Decoding accepts whitespace now.
90; 1.0 : Initial release.
91
92=== License
93
94 Copyright (c) 2004 James Bailey.
95 Copyright (c) 2009 Jim Ursetto.
96 
97 Permission is hereby granted, free of charge, to any person obtaining a
98 copy of this software and associated documentation files (the "Software"),
99 to deal in the Software without restriction, including without limitation
100 the rights to use, copy, modify, merge, publish, distribute, sublicense,
101 and/or sell copies of the Software, and to permit persons to whom the
102 Software is furnished to do so, subject to the following conditions:
103 
104 The above copyright notice and this permission notice shall be included in
105 all copies or substantial portions of the Software.
106 
107 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
108 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
109 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
110 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
111 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
112 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
113 DEALINGS IN THE SOFTWARE.
114
Note: See TracBrowser for help on using the repository browser.