1 | ;;; |
---|
2 | ;;; quoted-printable.scm - quoted-printable encoding/decoding routine |
---|
3 | ;;; |
---|
4 | ;;; Copyright (c) 2000-2003 Shiro Kawai, All rights reserved. |
---|
5 | ;;; |
---|
6 | ;;; Redistribution and use in source and binary forms, with or without |
---|
7 | ;;; modification, are permitted provided that the following conditions |
---|
8 | ;;; are met: |
---|
9 | ;;; |
---|
10 | ;;; 1. Redistributions of source code must retain the above copyright |
---|
11 | ;;; notice, this list of conditions and the following disclaimer. |
---|
12 | ;;; |
---|
13 | ;;; 2. Redistributions in binary form must reproduce the above copyright |
---|
14 | ;;; notice, this list of conditions and the following disclaimer in the |
---|
15 | ;;; documentation and/or other materials provided with the distribution. |
---|
16 | ;;; |
---|
17 | ;;; 3. Neither the name of the authors nor the names of its contributors |
---|
18 | ;;; may be used to endorse or promote products derived from this |
---|
19 | ;;; software without specific prior written permission. |
---|
20 | ;;; |
---|
21 | ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
22 | ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
23 | ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
24 | ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
25 | ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
26 | ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
---|
27 | ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
28 | ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
29 | ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
30 | ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
31 | ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | ;;; |
---|
33 | ;;; $Id: quoted-printable.scm,v 1.6 2003/07/05 03:29:12 shirok Exp $ |
---|
34 | ;;; |
---|
35 | ;;; Ported to Chicken by Hans Bulfone <jsb@nil.at> |
---|
36 | |
---|
37 | |
---|
38 | ;; Ref: RFC2045 section 6.7 <http://www.rfc-editor.org/rfc/rfc2045.txt> |
---|
39 | |
---|
40 | (declare |
---|
41 | (unit mime:quoted-printable) |
---|
42 | (usual-integrations) |
---|
43 | (export quoted-printable-encode quoted-printable-encode-string |
---|
44 | quoted-printable-decode quoted-printable-decode-string)) |
---|
45 | (require-extension srfi-13) |
---|
46 | |
---|
47 | (define read-byte |
---|
48 | (let ((read-char read-char) |
---|
49 | (char->integer char->integer)) |
---|
50 | (lambda () |
---|
51 | (let ((ch (read-char))) |
---|
52 | (if (eof-object? ch) |
---|
53 | ch |
---|
54 | (char->integer ch)))))) |
---|
55 | (define write-byte |
---|
56 | (let ((write-char write-char) |
---|
57 | (integer->char integer->char)) |
---|
58 | (lambda (b) |
---|
59 | (write-char (integer->char b))))) |
---|
60 | (define (hexdigit->integer ch) |
---|
61 | (cond |
---|
62 | ((and (char>=? ch #\0) (char<=? ch #\9)) |
---|
63 | (fx- (char->integer ch) (char->integer #\0))) |
---|
64 | ((and (char>=? ch #\A) (char<=? ch #\F)) |
---|
65 | (fx+ 10 (fx- (char->integer ch) (char->integer #\A)))) |
---|
66 | ((and (char>=? ch #\a) (char<=? ch #\f)) |
---|
67 | (fx+ 10 (fx- (char->integer ch) (char->integer #\a)))) |
---|
68 | (else #f))) |
---|
69 | |
---|
70 | (define (quoted-printable-encode #!key (binary? #f)) |
---|
71 | (define (esc c) |
---|
72 | (display "=") |
---|
73 | (display (string-pad (string-upcase (number->string c 16)) 2 #\0))) |
---|
74 | (let loop ((c (read-byte)) (lcnt 0)) |
---|
75 | (cond |
---|
76 | ((eof-object? c)) |
---|
77 | |
---|
78 | ((>= lcnt 73) ; soft newline |
---|
79 | (display "=\r\n") |
---|
80 | (loop c 0)) |
---|
81 | |
---|
82 | ((= c #x3d) ; '=' |
---|
83 | (display "=3D") |
---|
84 | (loop (read-byte) (+ lcnt 3))) |
---|
85 | |
---|
86 | ((or (= c #x20) (= c #x09)) |
---|
87 | (let ((c1 (read-byte))) |
---|
88 | (if (and (not binary?) (or (eqv? c1 #x0d) (eqv? c1 #x0a))) |
---|
89 | (begin (esc c) (loop c1 (+ lcnt 3))) |
---|
90 | (begin (write-byte c) (loop c1 (+ lcnt 1)))))) |
---|
91 | |
---|
92 | ((and (not binary?) (= c #x0d)) |
---|
93 | (display "\r\n") |
---|
94 | (let ((c1 (read-byte))) |
---|
95 | (if (eqv? c1 #x0a) |
---|
96 | (loop (read-byte) 0) |
---|
97 | (loop c1 0)))) |
---|
98 | ((and (not binary?) (= c #x0a)) |
---|
99 | (display "\r\n") |
---|
100 | (loop (read-byte) 0)) |
---|
101 | |
---|
102 | ((<= #x21 c #x7e) |
---|
103 | (write-byte c) |
---|
104 | (loop (read-byte) (+ lcnt 1))) |
---|
105 | |
---|
106 | (else |
---|
107 | (esc c) |
---|
108 | (loop (read-byte) (+ lcnt 3)))))) |
---|
109 | |
---|
110 | (define (quoted-printable-encode-string string . args) |
---|
111 | (with-output-to-string |
---|
112 | (lambda () |
---|
113 | (with-input-from-string string |
---|
114 | (lambda () |
---|
115 | (apply quoted-printable-encode args)))))) |
---|
116 | |
---|
117 | (define tab+space '(#\tab #\space)) |
---|
118 | (define (check-whitespace c chars) |
---|
119 | (cond |
---|
120 | ((or (eof-object? c) (char=? c #\newline) (char=? c #\return)) c) |
---|
121 | ((memv c tab+space) |
---|
122 | (check-whitespace (read-char) (cons c chars))) |
---|
123 | (else |
---|
124 | (for-each write-char (reverse chars)) |
---|
125 | c))) |
---|
126 | |
---|
127 | (define (quoted-printable-decode) |
---|
128 | (let loop ((c (read-char))) |
---|
129 | (cond |
---|
130 | ((eof-object? c)) |
---|
131 | ((char=? c #\=) |
---|
132 | (let ((c1 (read-char))) |
---|
133 | (cond |
---|
134 | ((eof-object? c1)) ; illegal, but we recognize it as a soft newline |
---|
135 | |
---|
136 | ((char=? c1 #\newline) ; soft newline |
---|
137 | (loop (read-char))) |
---|
138 | |
---|
139 | ((char=? c1 #\return) ; soft newline |
---|
140 | (let ((c2 (read-char))) |
---|
141 | (if (char=? c2 #\newline) (loop (read-char)) (loop c2)))) |
---|
142 | |
---|
143 | ((memv c1 tab+space) ; possibly soft newline |
---|
144 | (let ((c2 (check-whitespace (read-char) (list c1 c)))) |
---|
145 | (cond |
---|
146 | ((eof-object? c2)) |
---|
147 | ((char=? c2 #\newline) (loop (read-char))) |
---|
148 | ((char=? c2 #\return) |
---|
149 | (let ((c3 (read-char))) |
---|
150 | (if (eqv? c3 #\newline) |
---|
151 | (loop (read-char)) |
---|
152 | (loop c3)))) |
---|
153 | (else (loop c2))))) |
---|
154 | |
---|
155 | ((hexdigit->integer c1) |
---|
156 | => (lambda (num1) |
---|
157 | (let ((c2 (read-char))) |
---|
158 | (cond |
---|
159 | ((eof-object? c2) (write-char c) (write-char c1)) |
---|
160 | ((hexdigit->integer c2) |
---|
161 | => (lambda (num2) |
---|
162 | (write-byte (+ (* num1 16) num2)) |
---|
163 | (loop (read-char)))) |
---|
164 | (else |
---|
165 | (write-char c) (write-char c1) (loop c2)))))) |
---|
166 | (else |
---|
167 | (write-char c) (loop c1))))) |
---|
168 | |
---|
169 | ((memv c tab+space) |
---|
170 | (loop (check-whitespace (read-char) (list c)))) |
---|
171 | |
---|
172 | (else |
---|
173 | (write-char c) (loop (read-char)))))) |
---|
174 | |
---|
175 | (define (quoted-printable-decode-string string) |
---|
176 | (with-output-to-string |
---|
177 | (lambda () |
---|
178 | (with-input-from-string string |
---|
179 | quoted-printable-decode)))) |
---|